Skip to contentSkip to conversation

mm, memcg: fix error return value of mem_cgroup_css_alloc()

PATCH Superseded Yafang Shao 3 participants 5 messages Apr 6, 2020 – Apr 7, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm, memcg: fix error return value of mem_cgroup_css_alloc()

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 -EBUSY "Device or resource busy". That is same with memcg_alloc_cache_id().

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': Device or resource busy

---
 mm/memcontrol.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
mm/memcontrol.c +18 -7
index ca194864d802..94319e4dd1bd 100644
@@ -2717,8 +2717,12 @@ static int memcg_alloc_cache_id(void)
 
 	id = ida_simple_get(&memcg_cache_ida,
 			    0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
-	if (id < 0)
+	if (id < 0) {
+		if (id == -ENOSPC)
+			id = -EBUSY;
+
 		return id;
+	}
 
 	if (id < memcg_nr_cache_ids)
 		return id;
@@ -4986,19 +4990,26 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 	unsigned int size;
 	int node;
 	int __maybe_unused i;
+	long error = -ENOMEM;
 
 	size = sizeof(struct mem_cgroup);
 	size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
 
 	memcg = kzalloc(size, GFP_KERNEL);
 	if (!memcg)
-		return NULL;
+		return ERR_PTR(error);
 
 	memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
 				 1, MEM_CGROUP_ID_MAX,
 				 GFP_KERNEL);
-	if (memcg->id.id < 0)
+	if (memcg->id.id < 0) {
+		if (memcg->id.id == -ENOSPC)
+			error = -EBUSY;
+		else
+			error = memcg->id.id;
+
 		goto fail;
+	}
 
 	memcg->vmstats_local = alloc_percpu(struct memcg_vmstats_percpu);
 	if (!memcg->vmstats_local)
@@ -5042,7 +5053,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 fail:
 	mem_cgroup_id_remove(memcg);
 	__mem_cgroup_free(memcg);
-	return NULL;
+	return ERR_PTR(error);
 }
 
 static struct cgroup_subsys_state * __ref
@@ -5053,8 +5064,8 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 	long error = -ENOMEM;
 
 	memcg = mem_cgroup_alloc();
-	if (!memcg)
-		return ERR_PTR(error);
+	if (IS_ERR(memcg))
+		return ERR_CAST(memcg);
 
 	WRITE_ONCE(memcg->high, PAGE_COUNTER_MAX);
 	memcg->soft_limit = PAGE_COUNTER_MAX;
@@ -5104,7 +5115,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 fail:
 	mem_cgroup_id_remove(memcg);
 	mem_cgroup_free(memcg);
-	return ERR_PTR(-ENOMEM);
+	return ERR_PTR(error);
 }
 
 static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
Signature · 1 line
2.18.1
#1 lore raw

mm/memcontrol.c static int memcg_alloc_cache_id(void) · line 2725

Quoted 11 lines · Yafang Shao
mm/memcontrol.c +5 -1
@@ -2717,8 +2717,12 @@ static int memcg_alloc_cache_id(void)
 
 	id = ida_simple_get(&memcg_cache_ida,
 			    0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
-	if (id < 0)
+	if (id < 0) {
+		if (id == -ENOSPC)
+			id = -EBUSY;
+
 		return id;
+	}

This seems more complex than my original suggestion?

#2 lore raw
Quoted 4 lines · Matthew Wilcox
Quoted 2 lines · Yafang Shao
(quoted diff) +0 -0
@@ -2717,8 +2717,12 @@ static int memcg_alloc_cache_id(void)
 
Quoted 12 lines

mm/memcontrol.c static int memcg_alloc_cache_id(void) · line 2725

Quoted 9 lines
mm/memcontrol.c +5 -1
      id = ida_simple_get(&memcg_cache_ida,
                          0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
-     if (id < 0)
+     if (id < 0) {
+             if (id == -ENOSPC)
+                     id = -EBUSY;
+
              return id;
+     }

This seems more complex than my original suggestion?

I thought the (id < 0) is an unlikely case, writing it like this only checks id once for the most common case, while your original suggestion will check id twice if the compiler is not good enough. But that is not a big problem, I can change it as you suggestion. Your suggestion is more readable.

Thanks
Yafang

maintainer
#3 lore raw
In reply to Yafang Shao
Quoted 9 lines · Yafang Shao

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 -EBUSY "Device or resource busy". That is same with memcg_alloc_cache_id().

I do not see EBUSY being listed as expected return value for mkdir(2) which is the primary way to create a cgroup.

Quoted 3 lines

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': Device or resource busy

I do see ENOMEM being slightly confusing but if we really need to fix this then ENOSPC sounds like a better fit to me.

man page says
ENOSPC The new directory cannot be created because the user's disk quota is exhausted.

and that actually matches because id is a form of a quota.

Quoted 85 lines
---
 mm/memcontrol.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
mm/memcontrol.c +18 -7
index ca194864d802..94319e4dd1bd 100644
@@ -2717,8 +2717,12 @@ static int memcg_alloc_cache_id(void)
 
 	id = ida_simple_get(&memcg_cache_ida,
 			    0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
-	if (id < 0)
+	if (id < 0) {
+		if (id == -ENOSPC)
+			id = -EBUSY;
+
 		return id;
+	}
 
 	if (id < memcg_nr_cache_ids)
 		return id;
@@ -4986,19 +4990,26 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 	unsigned int size;
 	int node;
 	int __maybe_unused i;
+	long error = -ENOMEM;
 
 	size = sizeof(struct mem_cgroup);
 	size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
 
 	memcg = kzalloc(size, GFP_KERNEL);
 	if (!memcg)
-		return NULL;
+		return ERR_PTR(error);
 
 	memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
 				 1, MEM_CGROUP_ID_MAX,
 				 GFP_KERNEL);
-	if (memcg->id.id < 0)
+	if (memcg->id.id < 0) {
+		if (memcg->id.id == -ENOSPC)
+			error = -EBUSY;
+		else
+			error = memcg->id.id;
+
 		goto fail;
+	}
 
 	memcg->vmstats_local = alloc_percpu(struct memcg_vmstats_percpu);
 	if (!memcg->vmstats_local)
@@ -5042,7 +5053,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 fail:
 	mem_cgroup_id_remove(memcg);
 	__mem_cgroup_free(memcg);
-	return NULL;
+	return ERR_PTR(error);
 }
 
 static struct cgroup_subsys_state * __ref
@@ -5053,8 +5064,8 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 	long error = -ENOMEM;
 
 	memcg = mem_cgroup_alloc();
-	if (!memcg)
-		return ERR_PTR(error);
+	if (IS_ERR(memcg))
+		return ERR_CAST(memcg);
 
 	WRITE_ONCE(memcg->high, PAGE_COUNTER_MAX);
 	memcg->soft_limit = PAGE_COUNTER_MAX;
@@ -5104,7 +5115,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 fail:
 	mem_cgroup_id_remove(memcg);
 	mem_cgroup_free(memcg);
-	return ERR_PTR(-ENOMEM);
+	return ERR_PTR(error);
 }
 
 static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
Signature · 1 line
2.18.1
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#4 lore raw
Quoted 14 lines · Michal Hocko
Quoted 9 lines · Yafang Shao

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 -EBUSY "Device or resource busy". That is same with memcg_alloc_cache_id().

I do not see EBUSY being listed as expected return value for mkdir(2) which is the primary way to create a cgroup.

Quoted 6 lines
Quoted 3 lines

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': Device or resource busy

I do see ENOMEM being slightly confusing but if we really need to fix this then ENOSPC sounds like a better fit to me.

Btw. I have just checked 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs") and it explicitly talks about ENOSPC being returned prior to this patch.

Signature · 2 lines
Michal Hocko
SUSE Labs