From patchwork Mon Apr 6 16:56:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11475933 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 48E421392 for ; Mon, 6 Apr 2020 17:09:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1571520768 for ; Mon, 6 Apr 2020 17:09:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SOVlnq8b" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1571520768 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6E7C68E004E; Mon, 6 Apr 2020 13:09:40 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6BE248E000D; Mon, 6 Apr 2020 13:09:40 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FB408E004E; Mon, 6 Apr 2020 13:09:40 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id 47D3F8E000D for ; Mon, 6 Apr 2020 13:09:40 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 144462498 for ; Mon, 6 Apr 2020 17:09:39 +0000 (UTC) X-FDA: 76678066878.17.store54_8e4b885996a16 X-Spam-Summary: 2,0,0,9345f75b723db3c0,d41d8cd98f00b204,laoar.shao@gmail.com,,RULES_HIT:41:355:379:541:800:960:966:968:973:988:989:1260:1345:1437:1535:1542:1711:1730:1747:1777:1792:2194:2196:2199:2200:2393:2559:2562:2693:3138:3139:3140:3141:3142:3353:3865:3866:3867:3871:3872:3874:4321:4385:5007:6119:6261:6653:7514:7903:8603:9413:10004:11026:11473:11658:11914:12043:12048:12114:12296:12297:12438:12517:12519:12555:12679:12691:12737:12895:13161:13229:14093:14096:14181:14394:14687:14721:14819:21080:21444:21451:21611:21627:21666:21990:30054:30080,0,RBL:209.85.215.194:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:26,LUA_SUMMARY:none X-HE-Tag: store54_8e4b885996a16 X-Filterd-Recvd-Size: 5485 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Mon, 6 Apr 2020 17:09:37 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id g32so245583pgb.6 for ; Mon, 06 Apr 2020 10:09:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=xL4+7vhcCTiXpMdkvhWjGQm87X+lOSM9hOkNDM8xMGw=; b=SOVlnq8by4SisFxI2AXlgmy8/qdDlcWXmTBV+XYSpf0t/R1qrziGcQtuultJ5atJ7B 09svw9Abz+9Rn1PZr9LryaOfU+57zwk2EfmYKTWJX3QUeahZsmBI/DU6PeIyT7wDE6PE 5ykT27ii68lOmPCJ+NG/02zewDXfi9PvEAZ/9S+4/jPheF9XIhrJPo5JAeffjrXDKvVA Na8/VhIsPbF3LlqPnjFuNv8V0vGQadiLB4Qlu5vj+n7yNKFU6xE1fhl5FRkTaIzOJut+ qrmuZ3GqgC6zEie9GSKsYCtS4wXksYfMvpSIzQpz5dy89WxS3IvWX5yCnouehofc4TqK DgMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=xL4+7vhcCTiXpMdkvhWjGQm87X+lOSM9hOkNDM8xMGw=; b=NS3lGcwjLGrt0J/3NiThzE4U68mO1RQfrsghwIbpqUiSdwr2irbOM4bOC7fglzSyYz aflVYiu+TduR1XrhStj6BJeEE9oRj7BBSQF3WyR2JdIs4AbbVSrnnb3tYFnjjBUquAMs bT1JWMoLKGgE9HVbsiFfB/vGJaQuxtw01nP4euikAqtRMdvp2Jn/IQzeKL/8g0PI0XCr WGKi0aClfy42WkSbLld9mJXdfL4nfTUKDJ2WzPVC7OQ4zLQ3nIfq4nlIG0BC5kTpsDqu fpyQCGRvEehlW0a95BRcsmVf/V/EVQQsFseQCac0+Yi3NXuWAKnhm1KBt06Iih0q/Y1X mnNQ== X-Gm-Message-State: AGi0PubFpYowiFgxeB8a/uY6DjtTxUPaF2CH/uzeZWya27+/tYF6SnBk YWYjsssI/FYGUNY2cg1Wq0Y= X-Google-Smtp-Source: APiQypKb0A2G+pf152aRuRgnw70iiyXjq862y75+r49tzxAsbA1Ax9tJBd5RIhDuk8vcveTOtDBmXA== X-Received: by 2002:a63:5457:: with SMTP id e23mr19679984pgm.451.1586192976563; Mon, 06 Apr 2020 10:09:36 -0700 (PDT) Received: from localhost.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id 144sm11223355pgd.29.2020.04.06.10.09.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Apr 2020 10:09:35 -0700 (PDT) From: Yafang Shao To: willy@infradead.org, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, Yafang Shao Subject: [PATCH v3] mm, memcg: fix error return value of mem_cgroup_css_alloc() Date: Tue, 7 Apr 2020 00:56:03 +0800 Message-Id: <1586192163-20099-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.7.1 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Suggested-by: Matthew Wilcox Signed-off-by: Yafang Shao Acked-by: Johannes Weiner --- mm/memcontrol.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ca194864d802..042af0bc4a05 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2717,6 +2717,8 @@ static int memcg_alloc_cache_id(void) id = ida_simple_get(&memcg_cache_ida, 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL); + if (id == -ENOSPC) + return -EBUSY; if (id < 0) return id; @@ -4986,19 +4988,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 == -ENOSPC) { + error = -EBUSY; + goto fail; + } + if (memcg->id.id < 0) { + error = memcg->id.id; goto fail; + } memcg->vmstats_local = alloc_percpu(struct memcg_vmstats_percpu); if (!memcg->vmstats_local) @@ -5042,7 +5051,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 +5062,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 +5113,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)