From patchwork Tue Jul 7 06:27:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11647697 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 9640E92A for ; Tue, 7 Jul 2020 06:28:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 53F17206E9 for ; Tue, 7 Jul 2020 06:28:17 +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="Qb0ZCKir" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 53F17206E9 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 591B96B0024; Tue, 7 Jul 2020 02:28:16 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 51B426B0031; Tue, 7 Jul 2020 02:28:16 -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 3E24D6B0055; Tue, 7 Jul 2020 02:28:16 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0179.hostedemail.com [216.40.44.179]) by kanga.kvack.org (Postfix) with ESMTP id 258CA6B0024 for ; Tue, 7 Jul 2020 02:28:16 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id A9E63181AEF10 for ; Tue, 7 Jul 2020 06:28:15 +0000 (UTC) X-FDA: 77010300150.03.mom77_2f1139226eb2 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id 7B3BB28A4E9 for ; Tue, 7 Jul 2020 06:28:15 +0000 (UTC) X-Spam-Summary: 1,0,0,cc6da072cce84919,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1437:1515:1535:1543:1711:1730:1747:1777:1792:2194:2199:2393:2559:2562:2693:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:4117:4321:5007:6261:6653:7903:7904:8634:9592:10004:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12517:12519:12555:12679:12683:12895:13146:13230:13255:13894:14181:14394:14721:21080:21444:21451:21627:21740:21990:30029:30054:30070,0,RBL:209.85.215.194:@bytedance.com:.lbl8.mailshell.net-66.100.201.201 62.2.0.100;04y8esah8sx9x7zt88himi53p7aw7opp13gga8kggp5jhk768rqyd9onormjz7n.kjdhptwxiupdzmrjak7o58mbhugufyk6sa5nmuzgjbm5d5e413bdqksigqerwmd.s-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: mom77_2f1139226eb2 X-Filterd-Recvd-Size: 6135 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Tue, 7 Jul 2020 06:28:15 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id l63so19506079pge.12 for ; Mon, 06 Jul 2020 23:28:14 -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=eGXjfQahGJEMI3dFWEvZHra9f4CPaADa6kwDLG6Sz0U=; b=Qb0ZCKirQkg/pyAtVAEC6B9WFBqOte+gOTfm6ZQjdZwhmUBGB2+XiS5S2ddzoczwUz XyxiUhgm8JT4NcXfZolw43un2HQayIVDPk+L/muXIogcLMr4A9ttB6jfqvPljjNRosLc Fl0BKy3VI0Q/4jkWy3FNJmHepQDgTAMhNnfqQAKs8AfmX0bkPT7BkscOUd9AmnWmSZzN Tuvlg5+ViEHU6AVIwABE5VUFi6gLIERWWsPvLRLhbWApi1Tywf7dtixzL885EdC3tWJm znrEJfdeKRtauxg1nJhgtVy4gbzjTKge2Mw2c1BjYHWM/NbLq38/5FEPJ7f7PIkth0sJ LFTw== 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=eGXjfQahGJEMI3dFWEvZHra9f4CPaADa6kwDLG6Sz0U=; b=YA3mlGvIKyiCnQq7FnOevIf9Vf3yfvQ+wAmQqBk96LznRBegj9pB1QMJ+3NwDr6T5U 1/kqoG0DDoQV7BQo7RiwvsuZFVAULBwh3ircQUmpPleDH3DRtoGOMuT9IUvnbJAiTkvs 9bVu/V+wIKy+Lc8oMMcV+z4uijGrJVk3ga9uJ/d5Jly9iZK76IZedVoJxl2hSCJqZafn T0KT6RMI+G0fV+OHNPcerDjS6uU19gYE2mm11KWzMVu9exZVmbpxsz1q4Dj5tpBrP5fr 2syRs5Dux/G5/lzqC/50E7keQy+/+BCyiaQbJ7biaWDHQi291cd9Oj4szgIImFVJDh34 Kntw== X-Gm-Message-State: AOAM533agB43czTrhy6hFuZQf+Lvp/6QvlbQ/4mZAKkI4m3L9hw9ycV8 bKYzDieQip6V0RSeyDazck4hGg== X-Google-Smtp-Source: ABdhPJzTlPmc4fvi8qfUEFpH90MaEppPYMQ/O0VUx6imHli5jRS7GKutNv3Wd8FBrZ35ppS9bKW62w== X-Received: by 2002:a63:db57:: with SMTP id x23mr33353040pgi.178.1594103294073; Mon, 06 Jul 2020 23:28:14 -0700 (PDT) Received: from Smcdef-MBP.local.net ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id m12sm1362628pjf.17.2020.07.06.23.28.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jul 2020 23:28:13 -0700 (PDT) From: Muchun Song To: cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, shakeelb@google.com, Muchun Song Subject: [PATCH v5.4.y, v4.19.y] mm: memcg/slab: fix memory leak at non-root kmem_cache destroy Date: Tue, 7 Jul 2020 14:27:54 +0800 Message-Id: <20200707062754.8383-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Queue-Id: 7B3BB28A4E9 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: 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 A. It leads to create a non-root kmem_cache for allocating. 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 --- mm/slab_common.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index 8c1ffbf7de45..83ee6211aec7 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; } @@ -944,8 +961,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 +970,30 @@ void kmem_cache_destroy(struct kmem_cache *s) if (s->refcount) goto out_unlock; +#ifdef CONFIG_MEMCG_KMEM + 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 flush_memcg_workqueue(). + */ + s->memcg_params.dying = false; + goto out_unlock; + } +#endif + err = shutdown_memcg_caches(s); if (!err) err = shutdown_cache(s);