Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm: shrinker: fix shrinker_info teardown race with expansion
@ 2026-06-17  8:56 Qi Zheng
  2026-06-17  9:18 ` Muchun Song
  0 siblings, 1 reply; 2+ messages in thread
From: Qi Zheng @ 2026-06-17  8:56 UTC (permalink / raw)
  To: akpm, david, roman.gushchin, muchun.song
  Cc: linux-mm, linux-kernel, Qi Zheng, stable

From: Qi Zheng <zhengqi.arch@bytedance.com>

The expand_shrinker_info() iterates all visible memcgs under
shrinker_mutex, including memcgs that have not finished ->css_online()
yet.

Once pn->shrinker_info has been published, teardown must stay serialized
with expand_shrinker_info() until that memcg is either fully online or
no longer visible to iteration. Today alloc_shrinker_info() breaks that
rule by dropping shrinker_mutex before freeing a partially initialized
shrinker_info array, which may cause the following race:

CPU0                   CPU1
====                   ====

css_create
--> list_add_tail_rcu(&css->sibling, &parent_css->children);
    online_css
    --> mem_cgroup_css_online
        --> alloc_shrinker_info
            --> alloc node0 info
                rcu_assign_pointer(C->node0->shrinker_info, old0)
                alloc node1 info -> FAIL -> goto err
                mutex_unlock(shrinker_mutex)

                       shrinker_alloc()
                       --> shrinker_memcg_alloc
                           --> mutex_lock(shrinker_mutex)
                               expand_shrinker_info
                               --> mem_cgroup_iter see the memcg
                                   expand_one_shrinker_info
                                   --> old0 = C->node0->shrinker_info
                                       memcpy(new->unit, old0->unit, ...);

                free_shrinker_info
                --> kvfree(old0);

                                       /* double free !! */
                                       kvfree_rcu(old0, rcu);

The same problem exists later in mem_cgroup_css_online(). If
alloc_shrinker_info() succeeds but a subsequent objcg allocation fails,
the free_objcg -> free_shrinker_info() unwind path tears down the already
published pn->shrinker_info arrays without shrinker_mutex. The
expand_one_shrinker_info() can race with that teardown in the same way,
leading to use-after-free or double-free of the old shrinker_info.

Fix this by serializing shrinker_info teardown with shrinker_mutex, and by
keeping alloc_shrinker_info() error cleanup inside the locked section.

Fixes: 307bececcd12 ("mm: shrinker: add a secondary array for shrinker_info::{map, nr_deferred}")
Cc: stable@vger.kernel.org
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
 mm/shrinker.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/mm/shrinker.c b/mm/shrinker.c
index 7082d01c8c9d..a70aab124a0e 100644
--- a/mm/shrinker.c
+++ b/mm/shrinker.c
@@ -59,12 +59,14 @@ static inline int shrinker_unit_alloc(struct shrinker_info *new,
 	return 0;
 }
 
-void free_shrinker_info(struct mem_cgroup *memcg)
+static void __free_shrinker_info(struct mem_cgroup *memcg)
 {
 	struct mem_cgroup_per_node *pn;
 	struct shrinker_info *info;
 	int nid;
 
+	lockdep_assert_held(&shrinker_mutex);
+
 	for_each_node(nid) {
 		pn = memcg->nodeinfo[nid];
 		info = rcu_dereference_protected(pn->shrinker_info, true);
@@ -74,6 +76,13 @@ void free_shrinker_info(struct mem_cgroup *memcg)
 	}
 }
 
+void free_shrinker_info(struct mem_cgroup *memcg)
+{
+	mutex_lock(&shrinker_mutex);
+	__free_shrinker_info(memcg);
+	mutex_unlock(&shrinker_mutex);
+}
+
 int alloc_shrinker_info(struct mem_cgroup *memcg)
 {
 	int nid, ret = 0;
@@ -98,8 +107,8 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
 	return ret;
 
 err:
+	__free_shrinker_info(memcg);
 	mutex_unlock(&shrinker_mutex);
-	free_shrinker_info(memcg);
 	return -ENOMEM;
 }
 
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] mm: shrinker: fix shrinker_info teardown race with expansion
  2026-06-17  8:56 [PATCH] mm: shrinker: fix shrinker_info teardown race with expansion Qi Zheng
@ 2026-06-17  9:18 ` Muchun Song
  0 siblings, 0 replies; 2+ messages in thread
From: Muchun Song @ 2026-06-17  9:18 UTC (permalink / raw)
  To: Qi Zheng
  Cc: akpm, david, roman.gushchin, linux-mm, linux-kernel, Qi Zheng,
	stable



> On Jun 17, 2026, at 16:56, Qi Zheng <qi.zheng@linux.dev> wrote:
> 
> From: Qi Zheng <zhengqi.arch@bytedance.com>
> 
> The expand_shrinker_info() iterates all visible memcgs under
> shrinker_mutex, including memcgs that have not finished ->css_online()
> yet.
> 
> Once pn->shrinker_info has been published, teardown must stay serialized
> with expand_shrinker_info() until that memcg is either fully online or
> no longer visible to iteration. Today alloc_shrinker_info() breaks that
> rule by dropping shrinker_mutex before freeing a partially initialized
> shrinker_info array, which may cause the following race:
> 
> CPU0                   CPU1
> ====                   ====
> 
> css_create
> --> list_add_tail_rcu(&css->sibling, &parent_css->children);
>    online_css
>    --> mem_cgroup_css_online
>        --> alloc_shrinker_info
>            --> alloc node0 info
>                rcu_assign_pointer(C->node0->shrinker_info, old0)
>                alloc node1 info -> FAIL -> goto err
>                mutex_unlock(shrinker_mutex)
> 
>                       shrinker_alloc()
>                       --> shrinker_memcg_alloc
>                           --> mutex_lock(shrinker_mutex)
>                               expand_shrinker_info
>                               --> mem_cgroup_iter see the memcg
>                                   expand_one_shrinker_info
>                                   --> old0 = C->node0->shrinker_info
>                                       memcpy(new->unit, old0->unit, ...);
> 
>                free_shrinker_info
>                --> kvfree(old0);
> 
>                                       /* double free !! */
>                                       kvfree_rcu(old0, rcu);
> 
> The same problem exists later in mem_cgroup_css_online(). If
> alloc_shrinker_info() succeeds but a subsequent objcg allocation fails,
> the free_objcg -> free_shrinker_info() unwind path tears down the already
> published pn->shrinker_info arrays without shrinker_mutex. The
> expand_one_shrinker_info() can race with that teardown in the same way,
> leading to use-after-free or double-free of the old shrinker_info.
> 
> Fix this by serializing shrinker_info teardown with shrinker_mutex, and by
> keeping alloc_shrinker_info() error cleanup inside the locked section.
> 
> Fixes: 307bececcd12 ("mm: shrinker: add a secondary array for shrinker_info::{map, nr_deferred}")
> Cc: stable@vger.kernel.org
> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>

Acked-by: Muchun Song <muchun.song@linux.dev>

Thanks.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-06-17  9:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-17  8:56 [PATCH] mm: shrinker: fix shrinker_info teardown race with expansion Qi Zheng
2026-06-17  9:18 ` Muchun Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox