diff options
author | Muchun Song <songmuchun@bytedance.com> | 2020-12-14 19:06:35 -0800 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2020-12-30 11:53:54 +0100 |
commit | 6d48fff6d350ba69079cab6c71e9177a42b10215 (patch) | |
tree | ec664bc4244d51ed674c9b0549e55fcdd0c23c83 /mm | |
parent | 02314d05e8a01449dc0418d9b47b24c253b20826 (diff) | |
download | linux-stable-6d48fff6d350ba69079cab6c71e9177a42b10215.tar.gz linux-stable-6d48fff6d350ba69079cab6c71e9177a42b10215.tar.bz2 linux-stable-6d48fff6d350ba69079cab6c71e9177a42b10215.zip |
mm: memcg/slab: fix use after free in obj_cgroup_charge
[ Upstream commit eefbfa7fd678805b38a46293e78543f98f353d3e ]
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/20201028035013.99711-2-songmuchun@bytedance.com
Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API")
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Acked-by: Roman Gushchin <guro@fb.com>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Yafang Shao <laoar.shao@gmail.com>
Cc: Chris Down <chris@chrisdown.name>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'mm')
-rw-r--r-- | mm/memcontrol.c | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 74b85077f89a..a717728cc7b4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3247,8 +3247,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; |