memcg: protect concurrent access to mem_cgroup_idr
Subsystems (3)
Shakeel Butt
maintainerPATCH 1/1 memcg: protect concurrent access to mem_cgroup_idr
The commit 73f576c04b94 ("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 deeper, 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.
--- mm/memcontrol.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
mm/memcontrol.c +20 -2
@@ -3364,11 +3364,28 @@ static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)#define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1)static DEFINE_IDR (mem_cgroup_idr); +static DEFINE_SPINLOCK (memcg_idr_lock); + +static int mem_cgroup_alloc_id (void ) +{ +int ret; + +idr_preload (GFP_KERNEL ); +spin_lock (&memcg_idr_lock); + ret =idr_alloc (&mem_cgroup_idr,NULL ,1 ,MEM_CGROUP_ID_MAX +1 , +GFP_NOWAIT ); +spin_unlock (&memcg_idr_lock); +idr_preload_end (); +return ret; +}static void mem_cgroup_id_remove (struct mem_cgroup *memcg) {if (memcg->id.id >0 ) { +spin_lock (&memcg_idr_lock);idr_remove (&mem_cgroup_idr, memcg->id.id); +spin_unlock (&memcg_idr_lock); + memcg->id.id =0 ; } }
@@ -3502,8 +3519,7 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)if (!memcg)return ERR_PTR (error); - memcg->id.id =idr_alloc (&mem_cgroup_idr,NULL , -1 ,MEM_CGROUP_ID_MAX +1 ,GFP_KERNEL ); + memcg->id.id =mem_cgroup_alloc_id ();if (memcg->id.id <0 ) { error = memcg->id.id; goto fail;
@@ -3648,7 +3664,9 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)* publish it here at the end of onlining. This matches the * regular ID destruction during offlining. */ +spin_lock (&memcg_idr_lock);idr_replace (&mem_cgroup_idr, memcg, memcg->id.id); +spin_unlock (&memcg_idr_lock);return 0 ; offline_kmem:
Signature · 1 line
2.43.5
Quoted 35 lines · Shakeel Butt
The commit
73f576c04b94("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 themem_cgroup_idr, theidr_alloc()andidr_replace()happen within css callback and thus are protected throughcgroup_mutexfrom concurrent modifications. Howeveridr_remove()formem_cgroup_idrwas 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_lrubased kernel crashes at a low frequency in our fleet for a long time. These crashes were in different part oflist_lrucode includinglist_lru_add(),list_lru_del()and reparenting code. Upon further inspection, it looked like for a given object (dentry and inode), the super_block'slist_lrudidn't havelist_lru_onefor the memcg of that object. The initial suspicions were either the object is not allocated throughkmem_cache_alloc_lru()or somehowmemcg_list_lru_alloc()failed to allocatelist_lru_one()for a memcg but returned success. No evidence were found for these cases.Looking more deeper, we started seeing situations where valid memcg's id is not present in
mem_cgroup_idrand in some cases multiple valid memcgs have same id andmem_cgroup_idris pointing to one of them. So, the most reasonable explanation is that these situations can happen due to race between multipleidr_remove()calls or race between idr_alloc()/idr_replace() andidr_remove(). These races are causing multiple memcgs to acquire the same ID and then offlining of one of them would cleanuplist_lruson the system for all of them. Later access from other memcgs to thelist_lrucause crashes due to missinglist_lru_one.
Quoted 2 lines
Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")Signed-off-by Shakeel Butt <[email protected]>
Thanks.
Quoted 31 lines · Shakeel Butt
The commit
73f576c04b94("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 themem_cgroup_idr, theidr_alloc()andidr_replace()happen within css callback and thus are protected throughcgroup_mutexfrom concurrent modifications. Howeveridr_remove()formem_cgroup_idrwas 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_lrubased kernel crashes at a low frequency in our fleet for a long time. These crashes were in different part oflist_lrucode includinglist_lru_add(),list_lru_del()and reparenting code. Upon further inspection, it looked like for a given object (dentry and inode), the super_block'slist_lrudidn't havelist_lru_onefor the memcg of that object. The initial suspicions were either the object is not allocated throughkmem_cache_alloc_lru()or somehowmemcg_list_lru_alloc()failed to allocatelist_lru_one()for a memcg but returned success. No evidence were found for these cases.Looking more deeper, we started seeing situations where valid memcg's id is not present in
mem_cgroup_idrand in some cases multiple valid memcgs have same id andmem_cgroup_idris pointing to one of them. So, the most reasonable explanation is that these situations can happen due to race between multipleidr_remove()calls or race between idr_alloc()/idr_replace() andidr_remove(). These races are causing multiple memcgs to acquire the same ID and then offlining of one of them would cleanuplist_lruson the system for all of them. Later access from other memcgs to thelist_lrucause crashes due to missinglist_lru_one.
Quoted 2 lines
Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")Signed-off-by Shakeel Butt <[email protected]>
Great catch. This has been busted for ages, but the race is so unlikely that it stayed low profile.
It probably should be Cc: stable as well.
Quoted 21 lines · Shakeel Butt
The commit
73f576c04b94("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 themem_cgroup_idr, theidr_alloc()andidr_replace()happen within css callback and thus are protected throughcgroup_mutexfrom concurrent modifications. Howeveridr_remove()formem_cgroup_idrwas 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_lrubased kernel crashes at a low frequency in our fleet for a long time. These crashes were in different part oflist_lrucode includinglist_lru_add(),list_lru_del()and reparenting code. Upon further inspection, it looked like for a given object (dentry and inode), the super_block'slist_lrudidn't havelist_lru_onefor the memcg of that object. The initial suspicions were either the object is not allocated throughkmem_cache_alloc_lru()or somehowmemcg_list_lru_alloc()failed to allocatelist_lru_one()for a memcg but returned success. No evidence were found for these cases.
Quoted 9 lines
Looking more deeper, we started seeing situations where valid memcg's id is not present in
mem_cgroup_idrand in some cases multiple valid memcgs have same id andmem_cgroup_idris pointing to one of them. So, the most reasonable explanation is that these situations can happen due to race between multipleidr_remove()calls or race between idr_alloc()/idr_replace() andidr_remove(). These races are causing multiple memcgs to acquire the same ID and then offlining of one of them would cleanuplist_lruson the system for all of them. Later access from other memcgs to thelist_lrucause crashes due to missinglist_lru_one.
Great catch!
Thanks
Quoted 6 lines · Shakeel Butt
#define
MEM_CGROUP_ID_MAX((1UL <<MEM_CGROUP_ID_SHIFT) - 1)
staticDEFINE_IDR(mem_cgroup_idr);mm/memcontrol.c +4
-0+static DEFINE_SPINLOCK (memcg_idr_lock); + +static int mem_cgroup_alloc_id (void ) +{
mm/memcontrol.c static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) · line 3380
Quoted 10 lines
mm/memcontrol.c +10
-0+int ret; + +idr_preload (GFP_KERNEL ); +spin_lock (&memcg_idr_lock); + ret =idr_alloc (&mem_cgroup_idr,NULL ,1 ,MEM_CGROUP_ID_MAX +1 , +GFP_NOWAIT ); +spin_unlock (&memcg_idr_lock); +idr_preload_end (); +return ret; +}
You know, this works much better as an xarray:
static int mem_cgroup_alloc_id(void)
{
u32 id;
int ret;
ret = xa_alloc(&mem_cgroup_ids, &id, XA_LIMIT(1, MEM_CGROUP_ID_MAX),
GFP_KERNEL);
if (ret < 0)
return ret;
return id;
}No messing around with preloading, the spinlock is built in and the MAX works the way you want it to.
Shakeel Butt
maintainerQuoted 32 lines · Matthew Wilcox
Quoted 6 lines · Shakeel Butt
#define
MEM_CGROUP_ID_MAX((1UL <<MEM_CGROUP_ID_SHIFT) - 1)
staticDEFINE_IDR(mem_cgroup_idr);mm/memcontrol.c +4
-0+static DEFINE_SPINLOCK (memcg_idr_lock); + +static int mem_cgroup_alloc_id (void ) +{mm/memcontrol.c static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) · line 3380
Quoted 10 lines
mm/memcontrol.c +10
-0+int ret; + +idr_preload (GFP_KERNEL ); +spin_lock (&memcg_idr_lock); + ret =idr_alloc (&mem_cgroup_idr,NULL ,1 ,MEM_CGROUP_ID_MAX +1 , +GFP_NOWAIT ); +spin_unlock (&memcg_idr_lock); +idr_preload_end (); +return ret; +}You know, this works much better as an xarray:
static int
mem_cgroup_alloc_id(void){ u32 id; int ret; ret = xa_alloc(&mem_cgroup_ids, &id, XA_LIMIT(1, MEM_CGROUP_ID_MAX), GFP_KERNEL); if (ret < 0) return ret; return id; }
Quoted 2 lines
No messing around with preloading, the spinlock is built in and the MAX works the way you want it to.
Thanks. I would keep the current for the backports and change to xarray from idr for next release. Please correct me if the following alternatives are not correct.
1. You already mentioned idr_alloc() -> xa_alloc()
2. idr_remove() -> xa_erase()
3. idr_repalce() -> xa_cmpxchg()
thanks,
Shakeel
Quoted 31 lines · Shakeel Butt
The commit
73f576c04b94("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 themem_cgroup_idr, theidr_alloc()andidr_replace()happen within css callback and thus are protected throughcgroup_mutexfrom concurrent modifications. Howeveridr_remove()formem_cgroup_idrwas 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_lrubased kernel crashes at a low frequency in our fleet for a long time. These crashes were in different part oflist_lrucode includinglist_lru_add(),list_lru_del()and reparenting code. Upon further inspection, it looked like for a given object (dentry and inode), the super_block'slist_lrudidn't havelist_lru_onefor the memcg of that object. The initial suspicions were either the object is not allocated throughkmem_cache_alloc_lru()or somehowmemcg_list_lru_alloc()failed to allocatelist_lru_one()for a memcg but returned success. No evidence were found for these cases.Looking more deeper, we started seeing situations where valid memcg's id is not present in
mem_cgroup_idrand in some cases multiple valid memcgs have same id andmem_cgroup_idris pointing to one of them. So, the most reasonable explanation is that these situations can happen due to race between multipleidr_remove()calls or race between idr_alloc()/idr_replace() andidr_remove(). These races are causing multiple memcgs to acquire the same ID and then offlining of one of them would cleanuplist_lruson the system for all of them. Later access from other memcgs to thelist_lrucause crashes due to missinglist_lru_one.
Quoted 2 lines
Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")Signed-off-by Shakeel Butt <[email protected]>
Sorry for being late here. Thanks for catching this up.
Cc: stable is definitely due here as those races are really nasty to debug.
Thanks!
Quoted 59 lines
--- mm/memcontrol.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)mm/memcontrol.c +20
-2@@ -3364,11 +3364,28 @@ static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)#define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1)static DEFINE_IDR (mem_cgroup_idr); +static DEFINE_SPINLOCK (memcg_idr_lock); + +static int mem_cgroup_alloc_id (void ) +{ +int ret; + +idr_preload (GFP_KERNEL ); +spin_lock (&memcg_idr_lock); + ret =idr_alloc (&mem_cgroup_idr,NULL ,1 ,MEM_CGROUP_ID_MAX +1 , +GFP_NOWAIT ); +spin_unlock (&memcg_idr_lock); +idr_preload_end (); +return ret; +}static void mem_cgroup_id_remove (struct mem_cgroup *memcg) {if (memcg->id.id >0 ) { +spin_lock (&memcg_idr_lock);idr_remove (&mem_cgroup_idr, memcg->id.id); +spin_unlock (&memcg_idr_lock); + memcg->id.id =0 ; } }@@ -3502,8 +3519,7 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)if (!memcg)return ERR_PTR (error); - memcg->id.id =idr_alloc (&mem_cgroup_idr,NULL , -1 ,MEM_CGROUP_ID_MAX +1 ,GFP_KERNEL ); + memcg->id.id =mem_cgroup_alloc_id ();if (memcg->id.id <0 ) { error = memcg->id.id; goto fail;@@ -3648,7 +3664,9 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)* publish it here at the end of onlining. This matches the * regular ID destruction during offlining. */ +spin_lock (&memcg_idr_lock);idr_replace (&mem_cgroup_idr, memcg, memcg->id.id); +spin_unlock (&memcg_idr_lock);return 0 ; offline_kmem:Signature · 1 line
2.43.5
Signature · 2 lines
Michal Hocko SUSE Labs