]> Git Repo - linux.git/commitdiff
mm: memcg/slab: fix use after free in obj_cgroup_charge
authorMuchun Song <[email protected]>
Tue, 15 Dec 2020 03:06:35 +0000 (19:06 -0800)
committerLinus Torvalds <[email protected]>
Tue, 15 Dec 2020 20:13:39 +0000 (12:13 -0800)
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: bf4f059954dc ("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]>
mm/memcontrol.c

index 4ac4d5edada416ca41d525ec13fd9e90f89eeccb..08a8fac29f6aec56c3d1af617ee4827aa83bfa1b 100644 (file)
@@ -3235,8 +3235,10 @@ int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
         * independently later.
         */
        rcu_read_lock();
+retry:
        memcg = obj_cgroup_memcg(objcg);
-       css_get(&memcg->css);
+       if (unlikely(!css_tryget(&memcg->css)))
+               goto retry;
        rcu_read_unlock();
 
        nr_pages = size >> PAGE_SHIFT;
This page took 0.056896 seconds and 4 git commands to generate.