Skip to content

Commit 9972605

Browse files
shakeelbakpm00
authored andcommitted
memcg: protect concurrent access to mem_cgroup_idr
Commit 73f576c ("mm: memcontrol: fix cgroup creation failure after many small jobs") decoupled the memcg IDs from the CSS ID space to fix the cgroup creation failures. It introduced IDR to maintain the memcg ID space. The IDR depends on external synchronization mechanisms for modifications. For the mem_cgroup_idr, the idr_alloc() and idr_replace() happen within css callback and thus are protected through cgroup_mutex from concurrent modifications. However idr_remove() for mem_cgroup_idr was not protected against concurrency and can be run concurrently for different memcgs when they hit their refcnt to zero. Fix that. We have been seeing list_lru based kernel crashes at a low frequency in our fleet for a long time. These crashes were in different part of list_lru code including list_lru_add(), list_lru_del() and reparenting code. Upon further inspection, it looked like for a given object (dentry and inode), the super_block's list_lru didn't have list_lru_one for the memcg of that object. The initial suspicions were either the object is not allocated through kmem_cache_alloc_lru() or somehow memcg_list_lru_alloc() failed to allocate list_lru_one() for a memcg but returned success. No evidence were found for these cases. Looking more deeply, we started seeing situations where valid memcg's id is not present in mem_cgroup_idr and in some cases multiple valid memcgs have same id and mem_cgroup_idr is pointing to one of them. So, the most reasonable explanation is that these situations can happen due to race between multiple idr_remove() calls or race between idr_alloc()/idr_replace() and idr_remove(). These races are causing multiple memcgs to acquire the same ID and then offlining of one of them would cleanup list_lrus on the system for all of them. Later access from other memcgs to the list_lru cause crashes due to missing list_lru_one. Link: https://lkml.kernel.org/r/[email protected] Fixes: 73f576c ("mm: memcontrol: fix cgroup creation failure after many small jobs") Signed-off-by: Shakeel Butt <[email protected]> Acked-by: Muchun Song <[email protected]> Reviewed-by: Roman Gushchin <[email protected]> Acked-by: Johannes Weiner <[email protected]> Cc: Michal Hocko <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 4cbf320 commit 9972605

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

mm/memcontrol.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,11 +3386,28 @@ static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
33863386

33873387
#define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1)
33883388
static DEFINE_IDR(mem_cgroup_idr);
3389+
static DEFINE_SPINLOCK(memcg_idr_lock);
3390+
3391+
static int mem_cgroup_alloc_id(void)
3392+
{
3393+
int ret;
3394+
3395+
idr_preload(GFP_KERNEL);
3396+
spin_lock(&memcg_idr_lock);
3397+
ret = idr_alloc(&mem_cgroup_idr, NULL, 1, MEM_CGROUP_ID_MAX + 1,
3398+
GFP_NOWAIT);
3399+
spin_unlock(&memcg_idr_lock);
3400+
idr_preload_end();
3401+
return ret;
3402+
}
33893403

33903404
static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
33913405
{
33923406
if (memcg->id.id > 0) {
3407+
spin_lock(&memcg_idr_lock);
33933408
idr_remove(&mem_cgroup_idr, memcg->id.id);
3409+
spin_unlock(&memcg_idr_lock);
3410+
33943411
memcg->id.id = 0;
33953412
}
33963413
}
@@ -3524,8 +3541,7 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
35243541
if (!memcg)
35253542
return ERR_PTR(error);
35263543

3527-
memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
3528-
1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL);
3544+
memcg->id.id = mem_cgroup_alloc_id();
35293545
if (memcg->id.id < 0) {
35303546
error = memcg->id.id;
35313547
goto fail;
@@ -3667,7 +3683,9 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
36673683
* publish it here at the end of onlining. This matches the
36683684
* regular ID destruction during offlining.
36693685
*/
3686+
spin_lock(&memcg_idr_lock);
36703687
idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
3688+
spin_unlock(&memcg_idr_lock);
36713689

36723690
return 0;
36733691
offline_kmem:

0 commit comments

Comments
 (0)