Skip to content

Commit 6465ff3

Browse files
Qi Zhenggregkh
authored andcommitted
mm: shrinker: fix shrinker_info teardown race with expansion
commit 65476d3 upstream. 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. Link: https://lore.kernel.org/20260617085658.27096-1-qi.zheng@linux.dev Fixes: 307bece ("mm: shrinker: add a secondary array for shrinker_info::{map, nr_deferred}") Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com> Acked-by: Muchun Song <muchun.song@linux.dev> Cc: Dave Chinner <david@fromorbit.com> Cc: Qi Zheng <zhengqi.arch@bytedance.com> Cc: Roman Gushchin <roman.gushchin@linux.dev> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 560e21e commit 6465ff3

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

mm/shrinker.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ static inline int shrinker_unit_alloc(struct shrinker_info *new,
5959
return 0;
6060
}
6161

62-
void free_shrinker_info(struct mem_cgroup *memcg)
62+
static void __free_shrinker_info(struct mem_cgroup *memcg)
6363
{
6464
struct mem_cgroup_per_node *pn;
6565
struct shrinker_info *info;
6666
int nid;
6767

68+
lockdep_assert_held(&shrinker_mutex);
69+
6870
for_each_node(nid) {
6971
pn = memcg->nodeinfo[nid];
7072
info = rcu_dereference_protected(pn->shrinker_info, true);
@@ -74,6 +76,13 @@ void free_shrinker_info(struct mem_cgroup *memcg)
7476
}
7577
}
7678

79+
void free_shrinker_info(struct mem_cgroup *memcg)
80+
{
81+
mutex_lock(&shrinker_mutex);
82+
__free_shrinker_info(memcg);
83+
mutex_unlock(&shrinker_mutex);
84+
}
85+
7786
int alloc_shrinker_info(struct mem_cgroup *memcg)
7887
{
7988
int nid, ret = 0;
@@ -98,8 +107,8 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
98107
return ret;
99108

100109
err:
110+
__free_shrinker_info(memcg);
101111
mutex_unlock(&shrinker_mutex);
102-
free_shrinker_info(memcg);
103112
return -ENOMEM;
104113
}
105114

0 commit comments

Comments
 (0)