From patchwork Tue Jul 12 02:28:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rongwei Wang X-Patchwork-Id: 12914478 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41273C433EF for ; Tue, 12 Jul 2022 02:28:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89B5C940034; Mon, 11 Jul 2022 22:28:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 824E4940033; Mon, 11 Jul 2022 22:28:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 69CDE940034; Mon, 11 Jul 2022 22:28:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5C4A1940010 for ; Mon, 11 Jul 2022 22:28:19 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3D87660B41 for ; Tue, 12 Jul 2022 02:28:19 +0000 (UTC) X-FDA: 79676863518.24.91E0515 Received: from out199-3.us.a.mail.aliyun.com (out199-3.us.a.mail.aliyun.com [47.90.199.3]) by imf21.hostedemail.com (Postfix) with ESMTP id A61801C006B for ; Tue, 12 Jul 2022 02:28:17 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046049;MF=rongwei.wang@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0VJ6Ep2a_1657592890; Received: from localhost.localdomain(mailfrom:rongwei.wang@linux.alibaba.com fp:SMTPD_---0VJ6Ep2a_1657592890) by smtp.aliyun-inc.com; Tue, 12 Jul 2022 10:28:11 +0800 From: Rongwei Wang To: akpm@linux-foundation.org, vbabka@suse.cz, 42.hyeyoo@gmail.com, roman.gushchin@linux.dev, iamjoonsoo.kim@lge.com, rientjes@google.com, penberg@kernel.org, cl@gentwo.de Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] mm/slub: improve consistency of nr_slabs count Date: Tue, 12 Jul 2022 10:28:06 +0800 Message-Id: <20220712022807.44113-2-rongwei.wang@linux.alibaba.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220712022807.44113-1-rongwei.wang@linux.alibaba.com> References: <20220712022807.44113-1-rongwei.wang@linux.alibaba.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf21.hostedemail.com: domain of rongwei.wang@linux.alibaba.com designates 47.90.199.3 as permitted sender) smtp.mailfrom=rongwei.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657592898; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TLjsAAWIVSWxQxAIUG5TH8s7If6tIIolMExwAMTDWhU=; b=Km5Sy5MR363I94qZFj0TaYBCPDiphnGllKu8sUQjknmyhidqGSDjbukWP7sC9/d9iQK9wA ZuUcXYu3UAadyfm7tO7WI3iNIe53ZCQytw9UTIvDStEG08aQb6L2oHQtHGjx2N9h3TrX+k ki5lwTuof2+BEKSL2S5wZtk5lLTh1KU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657592898; a=rsa-sha256; cv=none; b=RE9liLC3nAy4pmV4zx9UyPSOGfSyjp2qrWNSRhXw02T7ONU4rEA0Azp+qb3blApNdKY3OB jsGUWil7C7saklPvRepUJWqm7NyMiJWwvRCvwdudLo+GLozzuFjekEbHKK87KkhJXbYj3Y TmGB2SFyvd4VWUFViAL0Jb2RIdv+oIE= Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf21.hostedemail.com: domain of rongwei.wang@linux.alibaba.com designates 47.90.199.3 as permitted sender) smtp.mailfrom=rongwei.wang@linux.alibaba.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Stat-Signature: tcdi8ybzbxat1he11kb7x91bwdhwztsx X-Rspamd-Queue-Id: A61801C006B X-HE-Tag: 1657592897-83984 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: Currently, discard_slab() can change nr_slabs count without holding node's list_lock. This will lead some error messages print when scanning node's partial or full list, e.g. validate all slabs. Literally, it affects the consistency of nr_slabs count. Here, discard_slab() is abandoned, And dec_slabs_node() is called before releasing node's list_lock. dec_slabs_nodes() and free_slab() can be called separately to ensure consistency of nr_slabs count. Signed-off-by: Rongwei Wang --- mm/slub.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index e950d8df8380..587416e39292 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2065,12 +2065,6 @@ static void free_slab(struct kmem_cache *s, struct slab *slab) __free_slab(s, slab); } -static void discard_slab(struct kmem_cache *s, struct slab *slab) -{ - dec_slabs_node(s, slab_nid(slab), slab->objects); - free_slab(s, slab); -} - /* * Management of partially allocated slabs. */ @@ -2439,6 +2433,7 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, if (!new.inuse && n->nr_partial >= s->min_partial) { mode = M_FREE; + spin_lock_irqsave(&n->list_lock, flags); } else if (new.freelist) { mode = M_PARTIAL; /* @@ -2463,7 +2458,7 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, old.freelist, old.counters, new.freelist, new.counters, "unfreezing slab")) { - if (mode == M_PARTIAL || mode == M_FULL) + if (mode != M_FULL_NOLIST) spin_unlock_irqrestore(&n->list_lock, flags); goto redo; } @@ -2475,7 +2470,10 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, stat(s, tail); } else if (mode == M_FREE) { stat(s, DEACTIVATE_EMPTY); - discard_slab(s, slab); + dec_slabs_node(s, slab_nid(slab), slab->objects); + spin_unlock_irqrestore(&n->list_lock, flags); + + free_slab(s, slab); stat(s, FREE_SLAB); } else if (mode == M_FULL) { add_full(s, n, slab); @@ -2528,6 +2526,7 @@ static void __unfreeze_partials(struct kmem_cache *s, struct slab *partial_slab) if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) { slab->next = slab_to_discard; slab_to_discard = slab; + dec_slabs_node(s, slab_nid(slab), slab->objects); } else { add_partial(n, slab, DEACTIVATE_TO_TAIL); stat(s, FREE_ADD_PARTIAL); @@ -2542,7 +2541,7 @@ static void __unfreeze_partials(struct kmem_cache *s, struct slab *partial_slab) slab_to_discard = slab_to_discard->next; stat(s, DEACTIVATE_EMPTY); - discard_slab(s, slab); + free_slab(s, slab); stat(s, FREE_SLAB); } } @@ -3443,9 +3442,10 @@ static void __slab_free(struct kmem_cache *s, struct slab *slab, remove_full(s, n, slab); } + dec_slabs_node(s, slab_nid(slab), slab->objects); spin_unlock_irqrestore(&n->list_lock, flags); stat(s, FREE_SLAB); - discard_slab(s, slab); + free_slab(s, slab); } /* @@ -4302,6 +4302,7 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) if (!slab->inuse) { remove_partial(n, slab); list_add(&slab->slab_list, &discard); + dec_slabs_node(s, slab_nid(slab), slab->objects); } else { list_slab_objects(s, slab, "Objects remaining in %s on __kmem_cache_shutdown()"); @@ -4310,7 +4311,7 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) spin_unlock_irq(&n->list_lock); list_for_each_entry_safe(slab, h, &discard, slab_list) - discard_slab(s, slab); + free_slab(s, slab); } bool __kmem_cache_empty(struct kmem_cache *s) @@ -4640,6 +4641,7 @@ static int __kmem_cache_do_shrink(struct kmem_cache *s) if (free == slab->objects) { list_move(&slab->slab_list, &discard); n->nr_partial--; + dec_slabs_node(s, slab_nid(slab), slab->objects); } else if (free <= SHRINK_PROMOTE_MAX) list_move(&slab->slab_list, promote + free - 1); } @@ -4655,7 +4657,7 @@ static int __kmem_cache_do_shrink(struct kmem_cache *s) /* Release empty slabs */ list_for_each_entry_safe(slab, t, &discard, slab_list) - discard_slab(s, slab); + free_slab(s, slab); if (slabs_node(s, node)) ret = 1;