[Devel] [PATCH RHEL7 COMMIT] ms/mm, memcg: fix error return value of mem_cgroup_css_alloc()

Konstantin Khorenko khorenko at virtuozzo.com
Fri Mar 29 20:53:47 MSK 2024


The commit is pushed to "branch-rh7-3.10.0-1160.108.1.vz7.221.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-1160.108.1.vz7.221.2
------>
commit bad18b9592a632bc81270adb4a80000fdfd333a7
Author: Yafang Shao <laoar.shao at gmail.com>
Date:   Thu May 7 18:35:43 2020 -0700

    ms/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 -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 at linux-foundation.org: s/EBUSY/ENOSPC/, per Michal]
    [akpm at linux-foundation.org: s/EBUSY/ENOSPC/, per Michal]
    mFixes: 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs")
    Suggested-by: Matthew Wilcox <willy at infradead.org>
    Signed-off-by: Yafang Shao <laoar.shao at gmail.com>
    Signed-off-by: Andrew Morton <akpm at linux-foundation.org>
    Acked-by: Michal Hocko <mhocko at kernel.org>
    Acked-by: Johannes Weiner <hannes at cmpxchg.org>
    Cc: Vladimir Davydov <vdavydov.dev at 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 at linux-foundation.org>
    
    https://virtuozzo.atlassian.net/browse/PSBM-153598
    (cherry picked from commit 11d6761218d19ca06ae5387f4e3692c4fa9e7493)
    Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
---
 mm/memcontrol.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 80eda9c802b9..f173322fb44e 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6751,19 +6751,22 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 	size_t size;
 	int i, ret;
 	int node;
+	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) {
+		error = memcg->id.id;
 		goto fail;
+	}
 
 	memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
 	if (!memcg->stat)
@@ -6794,7 +6797,7 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 	mem_cgroup_id_remove(memcg);
 fail:
 	kfree(memcg);
-	return NULL;
+	return ERR_PTR(error);
 }
 
 /*
@@ -6892,11 +6895,10 @@ static struct cgroup_subsys_state * __ref
 mem_cgroup_css_alloc(struct cgroup *cont)
 {
 	struct mem_cgroup *memcg;
-	long error = -ENOMEM;
 
 	memcg = mem_cgroup_alloc();
-	if (!memcg)
-		return ERR_PTR(error);
+	if (IS_ERR(memcg))
+		return ERR_CAST(memcg);
 
 	/* root ? */
 	if (cont->parent == NULL) {


More information about the Devel mailing list