Skip to content

Commit eefbfa7

Browse files
Muchun Songtorvalds
authored andcommitted
mm: memcg/slab: fix use after free in obj_cgroup_charge
The rcu_read_lock/unlock only can guarantee that the memcg will not be freed, but it cannot guarantee the success of css_get to memcg. If the whole process of a cgroup offlining is completed between reading a objcg->memcg pointer and bumping the css reference on another CPU, and there are exactly 0 external references to this memory cgroup (how we get to the obj_cgroup_charge() then?), css_get() can change the ref counter from 0 back to 1. Link: https://lkml.kernel.org/r/[email protected] Fixes: bf4f059 ("mm: memcg/slab: obj_cgroup API") Signed-off-by: Muchun Song <[email protected]> Acked-by: Roman Gushchin <[email protected]> Reviewed-by: Shakeel Butt <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Vladimir Davydov <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Yafang Shao <[email protected]> Cc: Chris Down <[email protected]> Cc: Christian Brauner <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2f7659a commit eefbfa7

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

mm/memcontrol.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3235,8 +3235,10 @@ int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
32353235
* independently later.
32363236
*/
32373237
rcu_read_lock();
3238+
retry:
32383239
memcg = obj_cgroup_memcg(objcg);
3239-
css_get(&memcg->css);
3240+
if (unlikely(!css_tryget(&memcg->css)))
3241+
goto retry;
32403242
rcu_read_unlock();
32413243

32423244
nr_pages = size >> PAGE_SHIFT;

0 commit comments

Comments
 (0)