Skip to content

Commit c1af2c1

Browse files
laoargregkh
authored andcommitted
mm, memcg: fix error return value of mem_cgroup_css_alloc()
commit 11d6761 upstream. When I run my memcg testcase which creates lots of memcgs, I found there're unexpected out of memory logs while there're still enough available free memory. The error log is mkdir: cannot create directory 'foo.65533': Cannot allocate memory The reason is when we try to create more than MEM_CGROUP_ID_MAX memcgs, an -ENOMEM errno will be set by mem_cgroup_css_alloc(), but the right errno should be -ENOSPC "No space left on device", which is an appropriate errno for userspace's failed mkdir. As the errno really misled me, we should make it right. After this patch, the error log will be mkdir: cannot create directory 'foo.65533': No space left on device [akpm@linux-foundation.org: s/EBUSY/ENOSPC/, per Michal] [akpm@linux-foundation.org: s/EBUSY/ENOSPC/, per Michal] Fixes: 73f576c ("mm: memcontrol: fix cgroup creation failure after many small jobs") Suggested-by: Matthew Wilcox <willy@infradead.org> Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Michal Hocko <mhocko@kernel.org> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Link: http://lkml.kernel.org/r/20200407063621.GA18914@dhcp22.suse.cz Link: http://lkml.kernel.org/r/1586192163-20099-1-git-send-email-laoar.shao@gmail.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1642f11 commit c1af2c1

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

mm/memcontrol.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5101,19 +5101,22 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
51015101
unsigned int size;
51025102
int node;
51035103
int __maybe_unused i;
5104+
long error = -ENOMEM;
51045105

51055106
size = sizeof(struct mem_cgroup);
51065107
size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
51075108

51085109
memcg = kzalloc(size, GFP_KERNEL);
51095110
if (!memcg)
5110-
return NULL;
5111+
return ERR_PTR(error);
51115112

51125113
memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
51135114
1, MEM_CGROUP_ID_MAX,
51145115
GFP_KERNEL);
5115-
if (memcg->id.id < 0)
5116+
if (memcg->id.id < 0) {
5117+
error = memcg->id.id;
51165118
goto fail;
5119+
}
51175120

51185121
memcg->vmstats_local = alloc_percpu(struct memcg_vmstats_percpu);
51195122
if (!memcg->vmstats_local)
@@ -5158,7 +5161,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
51585161
fail:
51595162
mem_cgroup_id_remove(memcg);
51605163
__mem_cgroup_free(memcg);
5161-
return NULL;
5164+
return ERR_PTR(error);
51625165
}
51635166

51645167
static struct cgroup_subsys_state * __ref
@@ -5169,8 +5172,8 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
51695172
long error = -ENOMEM;
51705173

51715174
memcg = mem_cgroup_alloc();
5172-
if (!memcg)
5173-
return ERR_PTR(error);
5175+
if (IS_ERR(memcg))
5176+
return ERR_CAST(memcg);
51745177

51755178
memcg->high = PAGE_COUNTER_MAX;
51765179
memcg->soft_limit = PAGE_COUNTER_MAX;
@@ -5220,7 +5223,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
52205223
fail:
52215224
mem_cgroup_id_remove(memcg);
52225225
mem_cgroup_free(memcg);
5223-
return ERR_PTR(-ENOMEM);
5226+
return ERR_PTR(error);
52245227
}
52255228

52265229
static int mem_cgroup_css_online(struct cgroup_subsys_state *css)

0 commit comments

Comments
 (0)