From: Zhongkun He <hezhongkun.hzk@bytedance.com>
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 <hezhongkun.hzk@bytedance.com>
Subject: [RFC PATCH 1/2] memcg: Add support for zram object charge
Date: Fri, 7 Jul 2023 12:46:49 +0800 [thread overview]
Message-ID: <20230707044649.1169155-1-hezhongkun.hzk@bytedance.com> (raw)
The compressed RAM is currently charged to kernel, not to
any memory cgroup, which is not satisfy our usage scenario.
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 limit will have
no effect. 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 <hezhongkun.hzk@bytedance.com>
---
include/linux/memcontrol.h | 10 ++++++++++
mm/memcontrol.c | 23 +++++++++++++++++++++++
2 files changed, 33 insertions(+)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 5818af8eca5a..716cc08a6527 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1819,6 +1819,8 @@ static inline void count_objcg_event(struct obj_cgroup *objcg,
rcu_read_unlock();
}
+int obj_cgroup_charge_zram(struct obj_cgroup *objcg, 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 +1882,14 @@ static inline void count_objcg_event(struct obj_cgroup *objcg,
{
}
+int obj_cgroup_charge_zram(struct obj_cgroup *objcg, 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..e1d2c084c132 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,28 @@ 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, 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_KERNEL, 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 */
/*
--
2.25.1
reply other threads:[~2023-07-07 4:52 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230707044649.1169155-1-hezhongkun.hzk@bytedance.com \
--to=hezhongkun.hzk@bytedance.com \
--cc=david@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=minchan@kernel.org \
--cc=senozhatsky@chromium.org \
--cc=yosryahmed@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox