From patchwork Tue Oct 17 15:44:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13425538 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 1C964CDB474 for ; Tue, 17 Oct 2023 15:45:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A79AE6B0248; Tue, 17 Oct 2023 11:45:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FF936B0249; Tue, 17 Oct 2023 11:45:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C6956B024A; Tue, 17 Oct 2023 11:45:28 -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 78A346B0248 for ; Tue, 17 Oct 2023 11:45:28 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 47CC61A0D99 for ; Tue, 17 Oct 2023 15:45:28 +0000 (UTC) X-FDA: 81355377936.25.8554443 Received: from out-195.mta1.migadu.com (out-195.mta1.migadu.com [95.215.58.195]) by imf01.hostedemail.com (Postfix) with ESMTP id 8B43540021 for ; Tue, 17 Oct 2023 15:45:26 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=FTuhEnnd; spf=pass (imf01.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.195 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697557526; 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:dkim-signature; bh=Tb71WTBIRi+Tnmk2GZg+a0sbcsGECWTHjBVZ/Kgj+ow=; b=UJ8t9JkXTxEufEZWwVqb065UY+Vtbd+Hmpfs2cNB5EbL3QxDwAm4A57AcgftDc3fWP3spY IdiT8HkEy4Lbe4NGtOTvS4eNiNIRk1y7CYg7iMEFBNuq+Enc1ilqMgaUzDBxzjLVQbGjnP UnC+cwHuwfDBz3uh9olLBkY885SQxKM= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=FTuhEnnd; spf=pass (imf01.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.195 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697557526; a=rsa-sha256; cv=none; b=vmS0noPUJnspXh/8d5XUyRx7ED8nNxFfybUsmb6PEMkrmybh16IQPXHUYjef2LOYBTyTa/ /TOfoKlJIfYZ1T/RZ54eu/SravVKj3nmGbPDbM0RWfclXp1I/Gvm4cFbEt04xnMb9W4r7B wMROGfC+tnjlE+HFHq1tfohsFQwo5RE= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1697557525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Tb71WTBIRi+Tnmk2GZg+a0sbcsGECWTHjBVZ/Kgj+ow=; b=FTuhEnndbqSCtiqr1O7e+RaC5Xz6OtTHQQeyiMhRejvTs1gPYpLuqXRStCw16NBr7Q3k3+ O4RYPhHqfkT5J7H1e8VhHiBdbBs0AckyoX/neMpbkg24WHjGGLBQOELIyx+/BM31Uj3GT8 t6UbHf9+/jBpU1VLbPxe5Kb/sPbBt9o= From: chengming.zhou@linux.dev To: cl@linux.com, penberg@kernel.org Cc: rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, vbabka@suse.cz, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, chengming.zhou@linux.dev, Chengming Zhou Subject: [RFC PATCH 3/5] slub: Optimize deactivate_slab() Date: Tue, 17 Oct 2023 15:44:37 +0000 Message-Id: <20231017154439.3036608-4-chengming.zhou@linux.dev> In-Reply-To: <20231017154439.3036608-1-chengming.zhou@linux.dev> References: <20231017154439.3036608-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 8B43540021 X-Rspam-User: X-Stat-Signature: uwh9ut7owmihesa3tfi4fr5xt4g9399s X-Rspamd-Server: rspam01 X-HE-Tag: 1697557526-438853 X-HE-Meta: U2FsdGVkX19LE92YkTg7z9CQvGmcUfalC0l4gaBwU+n3inGK84nQsGTfI+dehofiAVC85JZID7T2/YhaAEcfS/3tGktZA+BCb25Igcxv/YAYfB2+nJxzd5ipDPxGL/YxbOkqzNRaupMwj0/pTawJsHIeM36Ue0kFgDH+HBHKm6W+F+IKfy60u/6ObSKBL8cnacBdHxe1+Ml5WSYnpCIQbc6PJpkq3fMJwT24glyRLf2C9+kxlXoxE/g7cv4ReDIGXYCNHl8hv+S6Qbdq9kP0xEWt1xA893Qkl2liNL8ahUUDL35LAm4AHsT15YO6wzxRVElqmtqDB6MVD+nFj/2t6WD/qbsT467AL9Rr39tPBVKKAO3p+mot6o4YnVlZedvoHZIULOoT1ZJvO6ZC6yKS0LYnv3XG+piT1jPy+uJBmRATPNYsMyCSS/bpr8GwuZbgZtgWAfgw3imS7c1JFCgU7EzygSHRoSdQ1HEcT0PfHb13bbpPKmq37ohBdCXWzMdsWL6OXcuKslE7KBVVZh7OVhRydgNbl5EgZdlSdoO9vAfkqpJjyeYdQWDVWWquHqC4JcncafrG1UURQ/vIH0vN896iIcYoQNnXys4aj49lfCQRVGFuQrgOIQQYe07Lnwmckdm6fhAVGGmP9lhHa80SdK/R9ctSKF3H2vDw/rV70ukaJnf9lX1fNi+aZbUy1s6ayvcQwfVGopJBkGTw6G/DHJUtVDPjP7BxYJqk97EWgbDdIDiVjy5RgjTRNp4q+xIWbzuPIRU3Flr/JwymhhSXzxGb1usGIsYFN+uskBx22jmRGUz2ZaJXKjJ1gBWxJDIaHZZTDpGP/jqfxbxtQvinMCaEOONJPC/SB3Wt7DY9Kj1ibPFQvjqpg+FKOiUD2tS2ThF9Q4salRd8h/OelQriYFHxV1xnF7gfMzPcwC9K2HkpFcL07ohsARm2y1ykT6OiOCqRraciffJunaXeFfj H5rg5INC UbtexJNTVs5QQjt1EueiMtsTWPUaUXluZFaiSpXqpy+xA833TorKhEIr4p+UsRUxSsQyLDM95UcIEtImWiRHHyB9W9bapTzWvtgpIdx5i8Je5HLclJZritpFL+EjKzd9j2NN8pOoY+NADewZj2IQMMUBN6bjNS0f/Cp8l6HDNSUhUy/sw6M0sNpg0N/O5Rscjg3nKSPw9Hy+PNi8EmkLMzVl6BUZ5CLuSni9apXipCamGai9QoeS2XmwOHHWV4L3jb3hn 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: From: Chengming Zhou Since the introduce of unfrozen slabs on cpu partial list, we don't need to synchronize the slab frozen state under the node list_lock. The caller of deactivate_slab() and the caller of __slab_free() won't manipulate the slab list concurrently. So we can get node list_lock in the stage three if we need to manipulate the slab list in this path. Signed-off-by: Chengming Zhou --- mm/slub.c | 70 ++++++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 45 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 27eac93baa13..5a9711b35c74 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2520,10 +2520,8 @@ static void init_kmem_cache_cpus(struct kmem_cache *s) static void deactivate_slab(struct kmem_cache *s, struct slab *slab, void *freelist) { - enum slab_modes { M_NONE, M_PARTIAL, M_FREE, M_FULL_NOLIST }; struct kmem_cache_node *n = get_node(s, slab_nid(slab)); int free_delta = 0; - enum slab_modes mode = M_NONE; void *nextfree, *freelist_iter, *freelist_tail; int tail = DEACTIVATE_TO_HEAD; unsigned long flags = 0; @@ -2570,58 +2568,40 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, * unfrozen and number of objects in the slab may have changed. * Then release lock and retry cmpxchg again. */ -redo: - - old.freelist = READ_ONCE(slab->freelist); - old.counters = READ_ONCE(slab->counters); - VM_BUG_ON(!old.frozen); - - /* Determine target state of the slab */ - new.counters = old.counters; - if (freelist_tail) { - new.inuse -= free_delta; - set_freepointer(s, freelist_tail, old.freelist); - new.freelist = freelist; - } else - new.freelist = old.freelist; + do { + old.freelist = READ_ONCE(slab->freelist); + old.counters = READ_ONCE(slab->counters); + VM_BUG_ON(!old.frozen); + + /* Determine target state of the slab */ + new.counters = old.counters; + new.frozen = 0; + if (freelist_tail) { + new.inuse -= free_delta; + set_freepointer(s, freelist_tail, old.freelist); + new.freelist = freelist; + } else + new.freelist = old.freelist; - new.frozen = 0; + } while (!slab_update_freelist(s, slab, + old.freelist, old.counters, + new.freelist, new.counters, + "unfreezing slab")); + /* + * Stage three: Manipulate the slab list based on the updated state. + */ if (!new.inuse && n->nr_partial >= s->min_partial) { - mode = M_FREE; + stat(s, DEACTIVATE_EMPTY); + discard_slab(s, slab); + stat(s, FREE_SLAB); } else if (new.freelist) { - mode = M_PARTIAL; - /* - * Taking the spinlock removes the possibility that - * acquire_slab() will see a slab that is frozen - */ spin_lock_irqsave(&n->list_lock, flags); - } else { - mode = M_FULL_NOLIST; - } - - - if (!slab_update_freelist(s, slab, - old.freelist, old.counters, - new.freelist, new.counters, - "unfreezing slab")) { - if (mode == M_PARTIAL) - spin_unlock_irqrestore(&n->list_lock, flags); - goto redo; - } - - - if (mode == M_PARTIAL) { add_partial(n, slab, tail); spin_unlock_irqrestore(&n->list_lock, flags); stat(s, tail); - } else if (mode == M_FREE) { - stat(s, DEACTIVATE_EMPTY); - discard_slab(s, slab); - stat(s, FREE_SLAB); - } else if (mode == M_FULL_NOLIST) { + } else stat(s, DEACTIVATE_FULL); - } } #ifdef CONFIG_SLUB_CPU_PARTIAL