From patchwork Mon Jul 24 06:22:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13323468 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B8D2C001DE for ; Mon, 24 Jul 2023 06:22:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE4486B0075; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D94566B0078; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C34226B007B; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AAB436B0075 for ; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6C6B7C0599 for ; Mon, 24 Jul 2023 06:22:14 +0000 (UTC) X-FDA: 81045510588.05.D78577F Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf14.hostedemail.com (Postfix) with ESMTP id 9D180100010 for ; Mon, 24 Jul 2023 06:22:11 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=KFYjcOVL; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690179731; a=rsa-sha256; cv=none; b=H1h3EygMZphT7Hi9s+YxSOWMGoCo3l6iHSaREKax2mhWbj9a/Mv0YG02ryMES9/j66LRS3 ZFAsszidkcOkeDxlK+VY380LZMP6iTXzJXO9+Dcr6u1QkgndSB3m1Lwcs/3utNSy3lOnhc 3GkNiML8InVcBBkfXFgqD9bNzC2o418= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=KFYjcOVL; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690179731; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=4q9Iub5GBKzGL6g43LkSUILbUaAjC1Z6Nep1Zd7bQCF89AjusUA8s8IwFYgdSVj9gCokkL cwTsgpIfJyvScA1Z/dXEU2UTDrijZhRS7MgQe/I2B0ieZnQSrpxCX9QHTnhFziuRddwy/Z gZ9JVVvKSD1x09Sop0QrNqqNumXnqKg= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1b8b2886364so19032645ad.0 for ; Sun, 23 Jul 2023 23:22:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1690179730; x=1690784530; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=KFYjcOVLyGA1d4rfaoLPdLB/e6IjDH5icirSxJhNketSHU37mMtGFferynYbZDdVDr SBHHg1RIVmDI1fNLSXGw64oy5igBfOWadAkh1lGrBGeXMBa1ulxQGdtmi8/J7UhGyGZ5 PEqbCcd8mvm1vPkw4Rr/lF18ynZhCmEe2Nseic1Cu+mBDAkH1xnTwwZWgykAo+8kBVSo K+5y+jfTjdVUDLBZ1x9Lr90D1oTQILAGJl5dG+myCccy2uIl1c+6SRN9JGvOolhslNK4 nVA38dojo5rsWjEmeGe9RFjEZoFNh+SK6kqQBoGX1ixHQeZl5xBToafr9ey+tHo56CfZ eOtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690179730; x=1690784530; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=if7r9BJ4Gjq+vGPF0JkxQYXubsF3iMVrJe+/h3AmzQfuCJHWrF4HOIFtgAZGWkBWJG g5MJDIasF8DmooS2IPA7kT1oaecF/P+GboL0ke8JeizNoKAbJW73Z3uq5oimMgLno6B+ vMRUdNGrnyt2VxyjmvhvGPPR+n7j8hOhIi4wIAGQS2D6hTKK6NH58TapVSvTm3zDqrwz wWPATgvyWDYzzdui1xubQIHr813eyRHQRrXHG7dmTarbbS4TmA8Yt3yjXjGxAAkFr69M kMQBM/ttQm2j5ZVT3mQ47+7FjrfYnCC6aFv+3kkewrQ21PX4q+2zd1ne5Ik3EShzZpCY CY1Q== X-Gm-Message-State: ABy/qLY7eCYqVY1bV1M5pXqq1HbWPUQ/4cDPz+MVLBV+j5wWHVk+q2Qn Sgk0bMxN/T8f4RZmJKseBa/Yzw== X-Google-Smtp-Source: APBJJlHWsnLRWbTHoG+BCImPXA8ex14v4jkicKDRgJKk7Nt/usxhGPfBJt1FuUdKcnDc5doUpcUiAQ== X-Received: by 2002:a17:902:e995:b0:1b6:ab53:c7a5 with SMTP id f21-20020a170902e99500b001b6ab53c7a5mr6059293plb.46.1690179730618; Sun, 23 Jul 2023 23:22:10 -0700 (PDT) Received: from Tower.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id l14-20020a170902f68e00b001b7e63cfa19sm7983504plg.234.2023.07.23.23.22.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Jul 2023 23:22:09 -0700 (PDT) From: Zhongkun He To: minchan@kernel.org, senozhatsky@chromium.org, mhocko@suse.com Cc: david@redhat.com, yosryahmed@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Zhongkun He Subject: [RFC PATCH v2 1/2] memcg: Add support for zram object charge Date: Mon, 24 Jul 2023 14:22:02 +0800 Message-Id: <20230724062202.2244188-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9D180100010 X-Stat-Signature: tpgpzbcp4jgsnf5gbsori567ejz9csa3 X-HE-Tag: 1690179731-921524 X-HE-Meta: U2FsdGVkX1+GdukPG+NUZPdv2rbGCDIhjcvMMPAIn6STFFL9JJslh4ICATZnIa8kJvF4Ld+2IhDLtk6nAGY/bJTvC3oPg/TUApzkf8GOima72GtxnMdtisQ70uMCN1yyV5rx/RMqtmBkow9R6bjpRqBgOW/4TjcBlse4UJIKvuh2Iy3Ii+J1phH4TXHoU+KKsJTKBcaGOSeGLxNXAiTwUMFRdxCOIzl45X6O5QnnI2FUFvkEg+pqprUOYQT0PF71DuCVJSur4L6pKtE5tn5ymY03otTx5w00jXSVrq3+B6KwRYXZeSTRzHf9ax/YUfYCbDk2ZFs80EUIiC4VMp1bKoCuyJ3ue/3zUJIWUN3BuH5oB+0oFnYfLjQwrBHWj4LQVqUxvIq5vwLx5VyzCbxMsSXU2EG9LhmuQV6ZgLmYybWUS+2aIOBPCaBw/w8zZnLzTWBNMjh7NR5IqLVgVFLjR5tg9E6fdWYE2cITtag9KnZPp0GTBqHQnHYdavQNmgdahZAQtZcW5e8MD3/2ZY+sxP1he+2ds07AKT64d/dsu6Ua5b6WMazKPuWIy4pC0BtlEWEtxSdPQAI7XXA0W2AbcsLcOT7QzUF68xWgRQVorHwpTwojvlsBWVUNPfBLz9+EqvK6agPnT8Od+d0M8U1fR5MDDPjqd7nXaOcgdCUH2Zzxn51uxXmlrArdzqJDE51kxBHYLk1zk0lvkKmTpMssHAGIFkt4Zz4mX54i+ZYFg4npiLE0Cpjuc8pKKCfmaV7hX2+lKA9wuEYwS4In3P6kIZTCpKQ+UUsVCVydANLdj6MV6hDrEFeuSrow9xOkNlj5NhL9HR5ctGarWuQlri3n4NTe/piij9RDvQbRmqOpxfEJBYQFSKDNnva61HmT8dR18wT5Ln7No8jeVtC+zVlpghoZ9VekGIIG75HFMJBRbtbjmEAajjGQcuLlrSURFwUhUU3tj+MCPbUiaj5pTQ4 pwXCkWPI jwZJGR2S9ohoBJKWQs6eYzASuW8adjfMfG6lqpeeTum89FK1rWRc2Z9zwT8KneoIpUb9ZgUt4fXanFXZnJqFfBWR82LlCBjgEoHPpLpkzjT1uCElSs8NOitIJRzSggm5PHH6dtb/QuihXvpaev2KehC9ECvIufnPTx0kiBNNySe/RmlfZI+bk5WEiozkNFvJqMGSupAWp6AgeQF+Eqrq+bTyhMeqxZoEGF0v/qpIwL6ejIrRwR3s4kWPJnTOS+pUqHbgHVEq3za3CazSASmks7kbAeSvBgOBVH3RG4IrbpNwKZya+O/5L9GRuFaLiX4v8P9XJVj3uJi8a61U5xcIUpWCNN7a04Rp6SWUtp0weQksJbOMUIMenaWvISkW3BISMDCzSDx4Y18vPI8qCd6BtNXamGazjQ48eSZuQ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The compressed RAM(ZRAM) is currently charged to kernel, not to any memory cgroup, which can escape their cgroup memory containment if the memory of a task is limited by memcgroup. it will swap out the memory to zram swap device when the memory is insufficient. In that case, the memory hard limit will be invalidated. So, it should makes sense to charge the compressed RAM to the page's memory cgroup. As we know, zram can be used in two ways, direct and indirect, this patchset can charge memory in both cases. Direct zram usage by process within a cgroup will fail to charge if there is no memory. Indirect zram usage by process within a cgroup via swap in PF_MEMALLOC context, will charge successfully. Signed-off-by: Zhongkun He --- include/linux/memcontrol.h | 12 ++++++++++++ mm/memcontrol.c | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 5818af8eca5a..24bac877bc83 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1819,6 +1819,9 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, rcu_read_unlock(); } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + size_t size); +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size); #else static inline bool mem_cgroup_kmem_disabled(void) { @@ -1880,6 +1883,15 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, { } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + size_t size) +{ + return 0; +} + +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size) +{ +} #endif /* CONFIG_MEMCG_KMEM */ #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e8ca4bdcb03c..118544acf895 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3059,6 +3059,7 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) } return objcg; } +EXPORT_SYMBOL(get_obj_cgroup_from_page); static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) { @@ -3409,6 +3410,29 @@ void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) refill_obj_stock(objcg, size, true); } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + size_t size) +{ + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return 0; + + /* + * Indirect zram usage in PF_MEMALLOC, charging must succeed. + * Direct zram usage, charging may failed. + */ + return obj_cgroup_charge(objcg, gfp, size); +} +EXPORT_SYMBOL(obj_cgroup_charge_zram); + +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size) +{ + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return; + + obj_cgroup_uncharge(objcg, size); +} +EXPORT_SYMBOL(obj_cgroup_uncharge_zram); + #endif /* CONFIG_MEMCG_KMEM */ /*