From patchwork Wed Jul 15 16:50:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11665947 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 DD48813A4 for ; Wed, 15 Jul 2020 16:50:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AAF1B20672 for ; Wed, 15 Jul 2020 16:50:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="Eq7YBqvG" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AAF1B20672 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A84436B0006; Wed, 15 Jul 2020 12:50:39 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A0E3B8D0002; Wed, 15 Jul 2020 12:50:39 -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 8D4446B0008; Wed, 15 Jul 2020 12:50:39 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0170.hostedemail.com [216.40.44.170]) by kanga.kvack.org (Postfix) with ESMTP id 71FE26B0006 for ; Wed, 15 Jul 2020 12:50:39 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0231E180AD804 for ; Wed, 15 Jul 2020 16:50:39 +0000 (UTC) X-FDA: 77040898998.21.truck02_060c48126efb Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id A00C1180442C3 for ; Wed, 15 Jul 2020 16:50:38 +0000 (UTC) X-Spam-Summary: 1,0,0,cd735f18af578428,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:41:355:379:541:800:960:968:973:988:989:1260:1311:1314:1345:1437:1515:1535:1544:1711:1730:1747:1777:1792:2194:2199:2393:2559:2562:2693:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3870:3871:3872:4118:4250:4321:5007:6261:6653:7903:7904:9592:10004:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12517:12519:12555:12679:12683:12895:13146:13230:13894:13972:14181:14394:14721:21080:21444:21451:21627:21740:21987:21990:30029:30054:30070,0,RBL:209.85.216.65:@bytedance.com:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04y814a3mpaoxwhgnc5qogxq8bkb4ycnhnyqtkphubgo3pkpk4weoqiyraz61kt.i938j7hybscebwakkfhqnd8x5wb3mf3jfcfinsqn8aiiwgps9ix6xeney9e5xi3.y-lbl8.mailshell.net-223.238.255.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:24,LUA_SUMMARY:none X-HE-Tag: truck02_060c48126efb X-Filterd-Recvd-Size: 7342 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Wed, 15 Jul 2020 16:50:37 +0000 (UTC) Received: by mail-pj1-f65.google.com with SMTP id o22so3244723pjw.2 for ; Wed, 15 Jul 2020 09:50:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=EMqIy3T6Ix+Em0b+36gk3NtDJw0Mhrphx3pfNV9mw6g=; b=Eq7YBqvG1cx6mJBewkrWbQhs3iYhLyGyNq+EIEkepazXpIGK8JmduABRFSDOHBeg1T YAhY9L1CVBf/rIPgZSVxtaB6fSWgYAphkK59NtF05wTLYg6nzGOoz8unajE/EdEPY7Hw PLygagx34WGPYdL1+fIE65S487Khhx1ASVM3TOmF983JAHpmXyuhvggr33iKMRaZYr+f TmOXyxxVx/DuzA0OxCa3WvkWQcYn8yrf6mPIUqHuAEMLUPcRkg20QT33L5n2J1zS8aTl DZP0t8uEtWDGpTNxcjVIQG0aQVPmosq1/vcfYq90b9sPQ+rb+be/mK12wqslTfsMJelo 0UYA== 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:mime-version :content-transfer-encoding; bh=EMqIy3T6Ix+Em0b+36gk3NtDJw0Mhrphx3pfNV9mw6g=; b=Ym5yXDIQp3MOQt+4qRaCeNUZD+bHPj6Yu+M5oucEjSjMIiOh9kpeWPH8hJS3T+dz+e 7XWgGkwmmse59mRlAbR5+x/O3JL3+ZbJB2SND9Q0qsqRy6E1LM5AEk48W0IbmditlTeF eHiSUJRFHbN5P30V6U/rRDBXDzxoD5Hfr36E+qjK4YeYsXSDzMmBsqVX121fn71jXOox JQt9du2l4nbST2xjbRLwvzho9M4CEFxqQ0u5kFmpQrrlpy1SLaottMMEXQQ8+bAzzsAy yv6FDNUX4jcOrsC3NFx9cKjgoUG/P8NM5/j8Mdw3aURmFzq30aVQeiguU9Zcd6FAsSzo Mh9w== X-Gm-Message-State: AOAM530pPXQkiPc2DhyAkCK4M05wY/LNw8/wLixtOoiqxeOU9PzWNOcq j0Z/cn+8CD0Fd31IYUIYksILiw== X-Google-Smtp-Source: ABdhPJwHAiEC5QJTaasRq724Ql/4PZyQHjjGHY9KrBd2KjUUv48sqlnZjbs65SNtrvAgoEXaRSwa3w== X-Received: by 2002:a17:90a:12c7:: with SMTP id b7mr540254pjg.137.1594831836920; Wed, 15 Jul 2020 09:50:36 -0700 (PDT) Received: from localhost.localdomain ([103.136.221.67]) by smtp.gmail.com with ESMTPSA id b21sm2648979pfp.172.2020.07.15.09.50.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Jul 2020 09:50:36 -0700 (PDT) From: Muchun Song To: guro@fb.com, vbabka@suse.cz, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, shakeelb@google.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH v2] mm: memcg/slab: fix memory leak at non-root kmem_cache destroy Date: Thu, 16 Jul 2020 00:50:22 +0800 Message-Id: <20200715165023.29919-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Queue-Id: A00C1180442C3 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000005, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: If the kmem_cache refcount is greater than one, we should not mark the root kmem_cache as dying. If we mark the root kmem_cache dying incorrectly, the non-root kmem_cache can never be destroyed. It resulted in memory leak when memcg was destroyed. We can use the following steps to reproduce. 1) Use kmem_cache_create() to create a new kmem_cache named A. 2) Coincidentally, the kmem_cache A is an alias for kmem_cache B, so the refcount of B is just increased. 3) Use kmem_cache_destroy() to destroy the kmem_cache A, just decrease the B's refcount but mark the B as dying. 4) Create a new memory cgroup and alloc memory from the kmem_cache B. It leads to create a non-root kmem_cache for allocating memory. 5) When destroy the memory cgroup created in the step 4), the non-root kmem_cache can never be destroyed. If we repeat steps 4) and 5), this will cause a lot of memory leak. So only when refcount reach zero, we mark the root kmem_cache as dying. Fixes: 92ee383f6daa ("mm: fix race between kmem_cache destroy, create and deactivate") Signed-off-by: Muchun Song Reviewed-by: Shakeel Butt Acked-by: Roman Gushchin --- changelog in v2: 1) Fix a confusing typo in the commit log. 2) Remove flush_memcg_workqueue() for !CONFIG_MEMCG_KMEM. 3) Introduce a new helper memcg_set_kmem_cache_dying() to fix a race condition between flush_memcg_workqueue() and slab_unmergeable(). mm/slab_common.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index 8c1ffbf7de45..c4958116e3fd 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -258,6 +258,11 @@ static void memcg_unlink_cache(struct kmem_cache *s) list_del(&s->memcg_params.kmem_caches_node); } } + +static inline bool memcg_kmem_cache_dying(struct kmem_cache *s) +{ + return is_root_cache(s) && s->memcg_params.dying; +} #else static inline int init_memcg_params(struct kmem_cache *s, struct kmem_cache *root_cache) @@ -272,6 +277,11 @@ static inline void destroy_memcg_params(struct kmem_cache *s) static inline void memcg_unlink_cache(struct kmem_cache *s) { } + +static inline bool memcg_kmem_cache_dying(struct kmem_cache *s) +{ + return false; +} #endif /* CONFIG_MEMCG_KMEM */ /* @@ -326,6 +336,13 @@ int slab_unmergeable(struct kmem_cache *s) if (s->refcount < 0) return 1; + /* + * If the kmem_cache is dying. We should also skip this + * kmem_cache. + */ + if (memcg_kmem_cache_dying(s)) + return 1; + return 0; } @@ -886,12 +903,15 @@ static int shutdown_memcg_caches(struct kmem_cache *s) return 0; } -static void flush_memcg_workqueue(struct kmem_cache *s) +static void memcg_set_kmem_cache_dying(struct kmem_cache *s) { spin_lock_irq(&memcg_kmem_wq_lock); s->memcg_params.dying = true; spin_unlock_irq(&memcg_kmem_wq_lock); +} +static void flush_memcg_workqueue(struct kmem_cache *s) +{ /* * SLAB and SLUB deactivate the kmem_caches through call_rcu. Make * sure all registered rcu callbacks have been invoked. @@ -923,10 +943,6 @@ static inline int shutdown_memcg_caches(struct kmem_cache *s) { return 0; } - -static inline void flush_memcg_workqueue(struct kmem_cache *s) -{ -} #endif /* CONFIG_MEMCG_KMEM */ void slab_kmem_cache_release(struct kmem_cache *s) @@ -944,8 +960,6 @@ void kmem_cache_destroy(struct kmem_cache *s) if (unlikely(!s)) return; - flush_memcg_workqueue(s); - get_online_cpus(); get_online_mems(); @@ -955,6 +969,32 @@ void kmem_cache_destroy(struct kmem_cache *s) if (s->refcount) goto out_unlock; +#ifdef CONFIG_MEMCG_KMEM + memcg_set_kmem_cache_dying(s); + + mutex_unlock(&slab_mutex); + + put_online_mems(); + put_online_cpus(); + + flush_memcg_workqueue(s); + + get_online_cpus(); + get_online_mems(); + + mutex_lock(&slab_mutex); + + if (WARN(s->refcount, + "kmem_cache_destroy %s: Slab cache is still referenced\n", + s->name)) { + /* + * Reset the dying flag setted by memcg_set_kmem_cache_dying(). + */ + s->memcg_params.dying = false; + goto out_unlock; + } +#endif + err = shutdown_memcg_caches(s); if (!err) err = shutdown_cache(s);