From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796457931; b=WHvvh9pkw9tXqc0RICThAU6mNijtii08Jbd5LubISpW0Pfrp7XvwNKfRPjaWSdqWAfkv bVX2zCtdiIBeJcUA+AH1jj6fLtIRSnAfOd2DuuWjkBTA8g7inaQiu3hkYKbwZxjWq7EGo J0w5ycWOz8B1NEDBXu4mVQ0y5hrQDD8hGdbVSNuurTbdymYCVRhNt79wP7uQ8WXQMh6Sw XRAKZ53n9KqCYRsLlX+cZ3gm9vab531Oqlual7ABIE22oTsP7W2YKMqXWfoIcVV49lyuG 0y4B3ONpZHrdD50lZR3X9mSg/9WZ2HqDQqQxuhOoPn52vfIegmgAMdsMCLYu6CXe/tw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796457931; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=8KfE44yGwR/GL3x/OklRH2uCKn44Jqr+Nw4KNyxDLCE=; b=vf13co2odk/9tnL+rA4P1uyZhgJ3tJQBohkOns7GdLFbySs/YLt7ziCF6oRhCzmR7sqa 29YQ9SaqCiUEZg/+GgbRbK/0p2Yru0T5zCz+InWs2MAf+ZrvuDPiDaUJ1m6+zObAlmBXK T0wstrGHo2WqXOdNB79sQTOZuWEV718wmJdV8LM8jYfz+aDeJtEOPjSimmpnla5fvWpV1 s5xHw7A7kQgdadl/1c0qnf9nsId5GeYE4lU28r7E+xj2nnP9fpubgWglQz32SUojsVhTg md+BPf/dy4d9I30uBtqlceeeRVpQgOGLuCBz0ueybO/j0T5MK7F9Z/cwSGf2hvt9cAg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=ArFLiMrH; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F5A1606A322 for ; Sat, 5 Dec 2026 08:05:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=ArFLiMrH Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB54E1F00936; Sat, 5 Dec 2026 08:05:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796457931; bh=8KfE44yGwR/GL3x/OklRH2uCKn44Jqr+Nw4KNyxDLCE=; h=Date:To:From:In-Reply-To:References:Subject; b=ArFLiMrHCOck0kaqqPtP1Tk3RsKSqxGbs/tAAKf+w0+sjIoKfVTZJ/pdenp2kXvLR xY0n3SlSw18fPU5HOC/MsnEX2nWdOGLHmsRL/l6QB81nNyRf92dySQ1jLJo6QF+j+G rI98v7INxo7ATTuKSHR6gpMe1X1ePajqyp2VQJmI= Date: Sat, 05 Dec 2026 00:05:30 -0800 To: mm-commits@vger.kernel.org,ziy@nvidia.com,yosry@kernel.org, xlpang@linux.alibaba.com,usama.arif@linux.dev,shakeel.butt@linux.dev, ryncsn@gmail.com,roman.gushchin@linux.dev,qi.zheng@linux.dev, nphamcs@gmail.com,muchun.song@linux.dev,mkoutny@suse.com,mhocko@kernel.org, hannes@cmpxchg.org,gourry@gourry.net,david@kernel.org,david@fromorbit.com, chengming.zhou@linux.dev,baolin.wang@linux.alibaba.com, qinyuntan@linux.alibaba.com,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261123105334.2524066-2-qinyuntan@linux.alibaba.com> References: <20261123105334.2524066-2-qinyuntan@linux.alibaba.com> Subject: + mm-list_lru-index-per-memcg-lists-by-memory-css-id.patch added to mm-new branch Message-Id: <20261205080530.DB54E1F00936@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm: list_lru: index per-memcg lists by memory CSS ID has been added to the -mm mm-new branch. Its filename is mm-list_lru-index-per-memcg-lists-by-memory-css-id.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-list_lru-index-per-memcg-lists-by-memory-css-id.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Qinyun Tan Subject: mm: list_lru: index per-memcg lists by memory CSS ID Date: Mon, 23 Nov 2026 18:53:31 +0800 Patch series "mm: restore per-memcg reclaim for NONSLAB shrinkers under nokmem", v5. With cgroup.memory=nokmem, the THP deferred split shrinker and the zswap shrinker lose their memcg awareness because neither sets SHRINKER_NONSLAB. When a cgroup hits its memory limit, these shrinkers do not split its partially unmapped THPs or write back its zswap entries. Under nokmem, __list_lru_init() also disables per-memcg lists. Setting SHRINKER_NONSLAB alone would leave all memcgs sharing one list per node for each shrinker. Adding the first object to an empty list sets the shrinker bit for its memcg. A second memcg can add objects without setting its own bit, so reclaim in that memcg may skip the shrinker. A scan for the first memcg can then reach objects belonging to both. Before commit fafaeceb89a5 ("mm: switch deferred split shrinker to list_lru"), THP had fully per-memcg deferred split queues embedded in struct mem_cgroup, working independently of kmem accounting. nokmem only opts out of kernel slab accounting; THPs and zswapped pages are user memory and remain charged to their cgroups, so per-memcg reclaim is still what these shrinkers want. Patch 1 drops kmemcg_id and indexes non-root lists by the memory CSS ID. The root memcg keeps index zero and uses the per-node list directly. The lists are reparented when their memcg goes offline, including under nokmem. Patch 2 keeps a list_lru memcg aware under nokmem when its backing shrinker is registered SHRINKER_NONSLAB. Patch 3 restores SHRINKER_NONSLAB on the THP deferred split shrinker. Patch 4 sets the same flag on the zswap shrinker. Slab-backed lrus (e.g. the superblock dentry/inode lrus) still fall back to per-node lists under nokmem. The per-memcg lists for the two shrinkers are allocated on demand. With nokmem enabled, the test_memcontrol and test_zswap kselftests showed no new failures compared with the unpatched kernel. When one of two sibling cgroups exceeded memory.high, the patched kernel split THPs and wrote back zswap entries only in that cgroup. This patch (of 4): list_lru uses kmemcg_id, a copy of the private memcg ID, to index its per-memcg lists. kmemcg_id is only set when kmem accounting is enabled. Non-slab shrinkers need an index that also works with cgroup.memory=nokmem. Use memcg->css.id for non-root lists. The ID stays allocated until the CSS is freed, so disabling and re-enabling the memory controller on the same cgroup gives the new CSS a distinct index while the old CSS is still referenced. Reserve index zero for the root, which uses the per-node list directly. In list_lru_walk_node(), hold the RCU read lock across both lookups so the CSS ID cannot be reused between them. css_from_id() can see a CSS before its online callback succeeds, so check that the CSS is online before taking a reference. Drop the RCU read lock for the walk and keep the reference across restarts to recheck the same owner. Remove kmemcg_id and reparent the lists before the objcgs on memcg offline, including when kmem accounting is disabled. Link: https://lore.kernel.org/20261123105334.2524066-1-qinyuntan@linux.alibaba.com Link: https://patch.msgid.link/20261123105334.2524066-2-qinyuntan@linux.alibaba.com Link: https://lore.kernel.org/lkml/20260904033503.4067283-1-qinyuntan@linux.alibaba.com/ [1] Link: https://sashiko.dev/#/patchset/20260904033503.4067283-1-qinyuntan@linux.alibaba.com [2] Link: https://lore.kernel.org/lkml/20260910080722.3961351-1-qinyuntan@linux.alibaba.com/ [3] Link: https://lore.kernel.org/lkml/arNK64PxTXbVEhis@gourry-fedora-PF4VCD3F/ [4] Link: https://lore.kernel.org/lkml/20260907110111.2286932-1-qinyuntan@linux.alibaba.com/ [5] Link: https://lore.kernel.org/lkml/20261013031221.1997194-1-qinyuntan@linux.alibaba.com/ [6] Link: https://lore.kernel.org/lkml/20261120191122.f31167fc1c987196e5a0b86b@linux-foundation.org/ [7] Signed-off-by: Qinyun Tan Signed-off-by: Andrew Morton Acked-by: Muchun Song (ByteDance) Cc: Johannes Weiner Cc: Michal Hocko Cc: Roman Gushchin Cc: Shakeel Butt Cc: Michal Koutný Cc: David Hildenbrand Cc: Zi Yan Cc: Baolin Wang Cc: Usama Arif Cc: Dave Chinner Cc: Qi Zheng Cc: Yosry Ahmed Cc: Nhat Pham Cc: Chengming Zhou Cc: Xunlei Pang Cc: Kairui Song Cc: Gregory Price include/linux/memcontrol.h | 16 ------------ mm/list_lru.c | 45 +++++++++++++++++++++++------------ mm/memcontrol.c | 45 ++++++----------------------------- 3 files changed, 38 insertions(+), 68 deletions(-) --- a/include/linux/memcontrol.h~mm-list_lru-index-per-memcg-lists-by-memory-css-id +++ a/include/linux/memcontrol.h @@ -337,8 +337,6 @@ struct mem_cgroup { /* Private memcg ID. Used to ID objects that outlive the cgroup */ int private_id; - int kmemcg_id; - /* * Should the OOM killer kill all belonging tasks, had it kill one? */ @@ -1854,15 +1852,6 @@ static inline void memcg_kmem_uncharge_p __memcg_kmem_uncharge_page(page, order); } -/* - * A helper for accessing memcg's kmem_id, used for getting - * corresponding LRU lists. - */ -static inline int memcg_kmem_id(const struct mem_cgroup *memcg) -{ - return memcg ? memcg->kmemcg_id : -1; -} - struct mem_cgroup *mem_cgroup_from_virt(void *p); static inline void count_objcg_events(struct obj_cgroup *objcg, @@ -1925,11 +1914,6 @@ static inline bool memcg_kmem_online(voi return false; } -static inline int memcg_kmem_id(const struct mem_cgroup *memcg) -{ - return -1; -} - static inline struct mem_cgroup *mem_cgroup_from_virt(void *p) { return NULL; --- a/mm/list_lru.c~mm-list_lru-index-per-memcg-lists-by-memory-css-id +++ a/mm/list_lru.c @@ -41,6 +41,15 @@ static inline void unlock_list_lru(struc static LIST_HEAD(memcg_list_lrus); static DEFINE_MUTEX(list_lrus_mutex); +static inline unsigned long list_lru_memcg_idx(const struct mem_cgroup *memcg) +{ + /* Index 0 selects the per-node root list. */ + if (!memcg || mem_cgroup_is_root(memcg)) + return 0; + + return memcg->css.id; +} + static inline bool list_lru_memcg_aware(struct list_lru *lru) { return lru->memcg_aware; @@ -74,9 +83,9 @@ static int lru_shrinker_id(struct list_l static inline struct list_lru_one * list_lru_from_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg) { - int idx = memcg_kmem_id(memcg); + unsigned long idx = list_lru_memcg_idx(memcg); - if (list_lru_memcg_aware(lru) && idx >= 0) { + if (list_lru_memcg_aware(lru) && idx) { struct list_lru_memcg *mlru = xa_load(&lru->xa, idx); /* Lists at a reused index belong to another memcg. */ @@ -425,25 +434,30 @@ unsigned long list_lru_walk_node(struct #ifdef CONFIG_MEMCG if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) { struct list_lru_memcg *mlru; - struct mem_cgroup *memcg; + struct cgroup_subsys_state *css; unsigned long index; + /* + * Keep the ID from being reused between the two lookups. + * css_from_id() can see a CSS before ->css_online() succeeds. + */ + rcu_read_lock(); xa_for_each(&lru->xa, index, mlru) { - rcu_read_lock(); - memcg = mem_cgroup_from_private_id(index); - if (!memcg || !mem_cgroup_tryget(memcg)) { - rcu_read_unlock(); + css = css_from_id(index, &memory_cgrp_subsys); + if (!css || !css_is_online(css) || !css_tryget(css)) continue; - } rcu_read_unlock(); - isolated += __list_lru_walk_one(lru, nid, memcg, + isolated += __list_lru_walk_one(lru, nid, + mem_cgroup_from_css(css), isolate, cb_arg, nr_to_walk, false); - mem_cgroup_put(memcg); + css_put(css); + rcu_read_lock(); if (*nr_to_walk <= 0) break; } + rcu_read_unlock(); } #endif @@ -526,6 +540,7 @@ static void memcg_reparent_list_lru_one( void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent) { + unsigned long idx = list_lru_memcg_idx(memcg); struct list_lru *lru; int i; @@ -538,7 +553,7 @@ void memcg_reparent_list_lrus(struct mem * 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, idx); if (!mlru) continue; @@ -553,7 +568,7 @@ void memcg_reparent_list_lrus(struct mem 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, idx); /* * Here all list_lrus corresponding to the cgroup are guaranteed @@ -568,11 +583,11 @@ void memcg_reparent_list_lrus(struct mem static inline bool memcg_list_lru_allocated(struct mem_cgroup *memcg, struct list_lru *lru) { + unsigned long idx = list_lru_memcg_idx(memcg); struct list_lru_memcg *mlru; bool allocated; - int idx = memcg->kmemcg_id; - if (idx < 0) + if (!idx) return true; rcu_read_lock(); @@ -616,7 +631,7 @@ static int __memcg_list_lru_alloc(struct } /* Set before the lists are published, and never changed after. */ mlru->memcg = pos; - xas_set(&xas, pos->kmemcg_id); + xas_set(&xas, list_lru_memcg_idx(pos)); do { xas_lock_irqsave(&xas, flags); if (!xas_load(&xas) && !css_is_dying(&pos->css)) { --- a/mm/memcontrol.c~mm-list_lru-index-per-memcg-lists-by-memory-css-id +++ a/mm/memcontrol.c @@ -3889,33 +3889,6 @@ void folio_split_memcg_refs(struct folio obj_cgroup_get_many(folio_objcg(folio), new_refs); } -static void memcg_online_kmem(struct mem_cgroup *memcg) -{ - if (mem_cgroup_kmem_disabled()) - return; - - if (unlikely(mem_cgroup_is_root(memcg))) - return; - - static_branch_enable(&memcg_kmem_online_key); - - memcg->kmemcg_id = memcg->private_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; - - parent = parent_mem_cgroup(memcg); - memcg_reparent_list_lrus(memcg, parent); -} - #ifdef CONFIG_CGROUP_WRITEBACK #include @@ -4395,7 +4368,6 @@ static struct mem_cgroup *mem_cgroup_all seqlock_init(&memcg->socket_pressure_seqlock); #endif memcg1_memcg_init(memcg); - memcg->kmemcg_id = -1; #ifdef CONFIG_CGROUP_WRITEBACK INIT_LIST_HEAD(&memcg->cgwb_list); for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) @@ -4469,7 +4441,8 @@ static int mem_cgroup_css_online(struct struct obj_cgroup *objcg; int nid; - memcg_online_kmem(memcg); + if (!mem_cgroup_kmem_disabled() && likely(!mem_cgroup_is_root(memcg))) + static_branch_enable(&memcg_kmem_online_key); /* * A memcg must be visible for expand_shrinker_info() @@ -4477,7 +4450,7 @@ static int mem_cgroup_css_online(struct * here, when mem_cgroup_iter() can't skip it. */ if (alloc_shrinker_info(memcg)) - goto offline_kmem; + goto reparent_lrus; for_each_node(nid) { objcg = obj_cgroup_alloc(); @@ -4535,8 +4508,8 @@ free_objcg: } } free_shrinker_info(memcg); -offline_kmem: - memcg_offline_kmem(memcg); +reparent_lrus: + memcg_reparent_list_lrus(memcg, parent_mem_cgroup(memcg)); mem_cgroup_private_id_remove(memcg); return -ENOMEM; } @@ -4554,11 +4527,11 @@ static void mem_cgroup_css_offline(struc zswap_memcg_offline_cleanup(memcg); - memcg_offline_kmem(memcg); + memcg_reparent_list_lrus(memcg, parent_mem_cgroup(memcg)); /* * The reparenting of objcg must be after the reparenting of - * the list_lru in memcg_offline_kmem(), which ensures that - * they will not mistakenly get the parent list_lru. + * the list_lru above, which ensures that they will not + * mistakenly get the parent list_lru. */ memcg_reparent_objcgs(memcg); reparent_shrinker_deferred(memcg); @@ -6006,8 +5979,6 @@ static void __init memcg_struct_check(vo CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_read_mostly, private_id); CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_read_mostly, - kmemcg_id); - CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_read_mostly, oom_group); CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup_per_node, _ Patches currently in -mm which might be from qinyuntan@linux.alibaba.com are mm-list_lru-index-per-memcg-lists-by-memory-css-id.patch mm-list_lru-keep-per-memcg-lists-with-nokmem-for-nonslab-backed-lrus.patch mm-thp-restore-shrinker_nonslab-on-the-deferred-split-shrinker.patch mm-zswap-mark-the-zswap-shrinker-shrinker_nonslab.patch