Skip to content

Commit 7e97de0

Browse files
Kirill Tkhaitorvalds
authored andcommitted
memcg: remove memcg_cgroup::id from IDR on mem_cgroup_css_alloc() failure
In case of memcg_online_kmem() failure, memcg_cgroup::id remains hashed in mem_cgroup_idr even after memcg memory is freed. This leads to leak of ID in mem_cgroup_idr. This patch adds removal into mem_cgroup_css_alloc(), which fixes the problem. For better readability, it adds a generic helper which is used in mem_cgroup_alloc() and mem_cgroup_id_put_many() as well. Link: http://lkml.kernel.org/r/152354470916.22460.14397070748001974638.stgit@localhost.localdomain Fixes 73f576c ("mm: memcontrol: fix cgroup creation failure after many small jobs") Signed-off-by: Kirill Tkhai <[email protected]> Acked-by: Johannes Weiner <[email protected]> Acked-by: Vladimir Davydov <[email protected]> Cc: Michal Hocko <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent e30cb13 commit 7e97de0

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

mm/memcontrol.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,6 +4037,14 @@ static struct cftype mem_cgroup_legacy_files[] = {
40374037

40384038
static DEFINE_IDR(mem_cgroup_idr);
40394039

4040+
static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
4041+
{
4042+
if (memcg->id.id > 0) {
4043+
idr_remove(&mem_cgroup_idr, memcg->id.id);
4044+
memcg->id.id = 0;
4045+
}
4046+
}
4047+
40404048
static void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n)
40414049
{
40424050
VM_BUG_ON(atomic_read(&memcg->id.ref) <= 0);
@@ -4047,8 +4055,7 @@ static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
40474055
{
40484056
VM_BUG_ON(atomic_read(&memcg->id.ref) < n);
40494057
if (atomic_sub_and_test(n, &memcg->id.ref)) {
4050-
idr_remove(&mem_cgroup_idr, memcg->id.id);
4051-
memcg->id.id = 0;
4058+
mem_cgroup_id_remove(memcg);
40524059

40534060
/* Memcg ID pins CSS */
40544061
css_put(&memcg->css);
@@ -4185,8 +4192,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
41854192
idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
41864193
return memcg;
41874194
fail:
4188-
if (memcg->id.id > 0)
4189-
idr_remove(&mem_cgroup_idr, memcg->id.id);
4195+
mem_cgroup_id_remove(memcg);
41904196
__mem_cgroup_free(memcg);
41914197
return NULL;
41924198
}
@@ -4245,6 +4251,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
42454251

42464252
return &memcg->css;
42474253
fail:
4254+
mem_cgroup_id_remove(memcg);
42484255
mem_cgroup_free(memcg);
42494256
return ERR_PTR(-ENOMEM);
42504257
}

0 commit comments

Comments
 (0)