Skip to content

Commit 13678b9

Browse files
committed
memcg: protect concurrent access to mem_cgroup_idr
jira LE-2157 cve CVE-2024-43892 Rebuild_History Non-Buildable kernel-5.14.0-503.14.1.el9_5 commit-author Shakeel Butt <[email protected]> commit 9972605 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]> (cherry picked from commit 9972605) Signed-off-by: Jonathan Maple <[email protected]>
1 parent 72b38b5 commit 13678b9

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
@@ -5292,11 +5292,28 @@ static struct cftype mem_cgroup_legacy_files[] = {
52925292
*/
52935293

52945294
static DEFINE_IDR(mem_cgroup_idr);
5295+
static DEFINE_SPINLOCK(memcg_idr_lock);
5296+
5297+
static int mem_cgroup_alloc_id(void)
5298+
{
5299+
int ret;
5300+
5301+
idr_preload(GFP_KERNEL);
5302+
spin_lock(&memcg_idr_lock);
5303+
ret = idr_alloc(&mem_cgroup_idr, NULL, 1, MEM_CGROUP_ID_MAX + 1,
5304+
GFP_NOWAIT);
5305+
spin_unlock(&memcg_idr_lock);
5306+
idr_preload_end();
5307+
return ret;
5308+
}
52955309

52965310
static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
52975311
{
52985312
if (memcg->id.id > 0) {
5313+
spin_lock(&memcg_idr_lock);
52995314
idr_remove(&mem_cgroup_idr, memcg->id.id);
5315+
spin_unlock(&memcg_idr_lock);
5316+
53005317
memcg->id.id = 0;
53015318
}
53025319
}
@@ -5420,8 +5437,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
54205437
if (!memcg)
54215438
return ERR_PTR(error);
54225439

5423-
memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
5424-
1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL);
5440+
memcg->id.id = mem_cgroup_alloc_id();
54255441
if (memcg->id.id < 0) {
54265442
error = memcg->id.id;
54275443
goto fail;
@@ -5611,7 +5627,9 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
56115627
* publish it here at the end of onlining. This matches the
56125628
* regular ID destruction during offlining.
56135629
*/
5630+
spin_lock(&memcg_idr_lock);
56145631
idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
5632+
spin_unlock(&memcg_idr_lock);
56155633

56165634
return 0;
56175635
offline_kmem:

0 commit comments

Comments
 (0)