From: Johannes Weiner <hannes@cmpxchg.org>
To: Qinyun Tan <qinyuntan@linux.alibaba.com>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"Michal Hocko" <mhocko@kernel.org>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Shakeel Butt" <shakeel.butt@linux.dev>,
"Muchun Song" <muchun.song@linux.dev>,
"Michal Koutný" <mkoutny@suse.com>,
"David Hildenbrand" <david@kernel.org>, "Zi Yan" <ziy@nvidia.com>,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Usama Arif" <usama.arif@linux.dev>,
"Dave Chinner" <david@fromorbit.com>,
"Qi Zheng" <qi.zheng@linux.dev>, "Yosry Ahmed" <yosry@kernel.org>,
"Nhat Pham" <nphamcs@gmail.com>,
"Chengming Zhou" <chengming.zhou@linux.dev>,
"Xunlei Pang" <xlpang@linux.alibaba.com>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/4] mm: memcontrol: drop kmemcg_id and use the memcg ID for list_lru indexing
Date: Wed, 9 Sep 2026 14:21:04 -0400 [thread overview]
Message-ID: <aqGjkGA7XeYEHA2T@cmpxchg.org> (raw)
In-Reply-To: <20260907110111.2286932-2-qinyuntan@linux.alibaba.com>
On Mon, Sep 07, 2026 at 07:01:08PM +0800, Qinyun Tan wrote:
> kmemcg_id is a copy of the memcg ID assigned in memcg_online_kmem(),
> and is only used as the list_lru xarray index. With
> cgroup.memory=nokmem the assignment never happens, so every memcg
> resolves to the per-node lists. The next patch needs the index to
> work under nokmem as well, so drop the copy and use the memcg ID.
>
> The ID works just as well as the copy did: root and NULL still
> return -1 and use the per-node lists, and the ID is only released
> after the list_lru reparenting, so a stale or recycled ID can never
> reach a live list_lru entry.
>
> The early return of memcg_offline_kmem() under nokmem is dropped as
> well, so the reparenting also covers lrus that stay memcg aware
> without kmem accounting.
>
> Signed-off-by: Qinyun Tan <qinyuntan@linux.alibaba.com>
> ---
> include/linux/memcontrol.h | 8 +++++---
> mm/list_lru.c | 10 +++++-----
> mm/memcontrol.c | 6 ------
> 3 files changed, 10 insertions(+), 14 deletions(-)
>
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index fdf4812e1d818..edeb287978934 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -254,7 +254,6 @@ struct mem_cgroup {
> #if BITS_PER_LONG < 64
> seqlock_t socket_pressure_seqlock;
> #endif
> - int kmemcg_id;
>
> #ifdef CONFIG_CGROUP_WRITEBACK
> struct list_head cgwb_list;
> @@ -1775,12 +1774,15 @@ static inline void memcg_kmem_uncharge_page(struct page *page, int order)
> }
>
> /*
> - * A helper for accessing memcg's kmem_id, used for getting
> + * A helper for accessing the memcg ID, used for getting
> * corresponding LRU lists.
> */
> static inline int memcg_kmem_id(struct mem_cgroup *memcg)
> {
> - return memcg ? memcg->kmemcg_id : -1;
> + if (!memcg || mem_cgroup_is_root(memcg))
> + return -1;
> +
> + return memcg->id.id;
> }
This is a private ID with lifetime only guaranteed for online
groups. Reparenting happens right before it dies at offlining right
now, but this is not a great dependency to have.
Use mem_cgroup_id() instead and just get rid of that helper.
> struct mem_cgroup *mem_cgroup_from_virt(void *p);
> diff --git a/mm/list_lru.c b/mm/list_lru.c
> index a4522ca93ebcb..6fd4e9af84396 100644
> --- a/mm/list_lru.c
> +++ b/mm/list_lru.c
> @@ -502,7 +502,7 @@ static void memcg_reparent_list_lru_one(struct list_lru *lru, int nid,
> struct list_lru_one *src,
> struct mem_cgroup *dst_memcg)
> {
> - int dst_idx = dst_memcg->kmemcg_id;
> + int dst_idx = memcg_kmem_id(dst_memcg);
> struct list_lru_one *dst;
>
> spin_lock_irq(&src->lock);
> @@ -536,7 +536,7 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren
> * allocating a new mlru since CSS_DYING is already set for this
> * memcg a rcu grace period ago.
> */
> - mlru = xa_load(&lru->xa, memcg->kmemcg_id);
> + mlru = xa_load(&lru->xa, memcg_kmem_id(memcg));
> if (!mlru)
> continue;
>
> @@ -551,7 +551,7 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren
> for_each_node(i)
> memcg_reparent_list_lru_one(lru, i, &mlru->node[i], parent);
>
> - xa_erase_irq(&lru->xa, memcg->kmemcg_id);
> + xa_erase_irq(&lru->xa, memcg_kmem_id(memcg));
>
> /*
> * Here all list_lrus corresponding to the cgroup are guaranteed
> @@ -566,7 +566,7 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren
> static inline bool memcg_list_lru_allocated(struct mem_cgroup *memcg,
> struct list_lru *lru)
> {
> - int idx = memcg->kmemcg_id;
> + int idx = memcg_kmem_id(memcg);
>
> return idx < 0 || xa_load(&lru->xa, idx);
> }
> @@ -602,7 +602,7 @@ static int __memcg_list_lru_alloc(struct mem_cgroup *memcg,
> if (!mlru)
> return -ENOMEM;
> }
> - xas_set(&xas, pos->kmemcg_id);
> + xas_set(&xas, memcg_kmem_id(pos));
> do {
> xas_lock_irqsave(&xas, flags);
> if (!xas_load(&xas) && !css_is_dying(&pos->css)) {
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 7ce50bccf1264..619d4c1f2e8f2 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -3780,17 +3780,12 @@ static void memcg_online_kmem(struct mem_cgroup *memcg)
> return;
>
> static_branch_enable(&memcg_kmem_online_key);
> -
> - memcg->kmemcg_id = memcg->id.id;
> }
>
> static void memcg_offline_kmem(struct mem_cgroup *memcg)
> {
> struct mem_cgroup *parent;
>
> - if (mem_cgroup_kmem_disabled())
> - return;
> -
> if (unlikely(mem_cgroup_is_root(memcg)))
> return;
Both of these functions do very little now and the asymmetry you're
adding on the mem_cgroup_kmem_disabled() check looks odd. Please just
inline them into mem_cgroup_css_online()/offline():
onlining:
if (!mem_cgroup_kmem_disabled() && likely(!mem_cgroup_is_root()))
static_branch_enable(&memcg_kmem_online_key);
offlining:
memcg_reparent_list_lrus(memcg, parent);
The root check is unnecessary because roots are not destroyed. But if
you'd rather not make that change here, keep the root check, and leave
its removal to a separate cleanup patch, that's fine too.
next prev parent reply other threads:[~2026-09-09 18:21 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-07 11:01 [PATCH v2 0/4] mm: restore per-memcg reclaim for NONSLAB shrinkers under nokmem Qinyun Tan
2026-09-07 11:01 ` [PATCH v2 1/4] mm: memcontrol: drop kmemcg_id and use the memcg ID for list_lru indexing Qinyun Tan
2026-09-09 18:21 ` Johannes Weiner [this message]
2026-09-10 7:40 ` Qinyun Tan
2026-09-07 11:01 ` [PATCH v2 2/4] mm: list_lru: keep per-memcg lists with nokmem for NONSLAB-backed lrus Qinyun Tan
2026-09-09 18:22 ` Johannes Weiner
2026-09-07 11:01 ` [PATCH v2 3/4] mm: thp: restore SHRINKER_NONSLAB on the deferred split shrinker Qinyun Tan
2026-09-09 18:28 ` Johannes Weiner
2026-09-10 7:46 ` Qinyun Tan
2026-09-07 11:01 ` [PATCH v2 4/4] mm: zswap: mark the zswap shrinker SHRINKER_NONSLAB Qinyun Tan
2026-09-07 11:33 ` Yosry Ahmed
2026-09-09 18:29 ` Johannes Weiner
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=aqGjkGA7XeYEHA2T@cmpxchg.org \
--to=hannes@cmpxchg.org \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=cgroups@vger.kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=david@fromorbit.com \
--cc=david@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=qi.zheng@linux.dev \
--cc=qinyuntan@linux.alibaba.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=usama.arif@linux.dev \
--cc=xlpang@linux.alibaba.com \
--cc=yosry@kernel.org \
--cc=ziy@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.