Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp125852rdb; Wed, 1 Nov 2023 20:26:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGe2TJWJJnoHhadRlKYcOLbYlHKyxlbeqY2gaMF9w+Fv3NBvUUhifLGPgH8iiFXNI/ZkyIM X-Received: by 2002:a05:6808:2196:b0:3b5:655b:2f62 with SMTP id be22-20020a056808219600b003b5655b2f62mr7406618oib.31.1698895596232; Wed, 01 Nov 2023 20:26:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698895596; cv=none; d=google.com; s=arc-20160816; b=h2HzVN2YP6tWvlflIaA/V7h7JtuDeJXmZm6h2ItJzyy5ObJxSEh+z9IFTD+VAcrGwi q9/vGjvw4rYw1rzAmgewd6YwxAlyCRNxC5ftvfRX+BPfcDwZtEtUgdw9Vln2eSd3vA8u bPHFoGbzd7UDU8FlGLl/+cibc7T+E1Ia+t87uergWdAqJ0n+bh6Dd5FlBkjZOPM1kjNe C7590hAUw2Z/Z8b0MLst6kW0ZsEav7Odwz3XW0pvReMnvG/fRZeScgLOx1dAdw6U7/MR l1uX/AakN/IDSpVnV9cq2VtNi0v6im/bpfep2RsPSradVWqwA8oiABkFpUfI2ND/dD6i hKRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bbgfkr34Ut5bOat3PkSebFKZ4A5Cv7UHaj4yNDSPhuU=; fh=Dd5ryTbz4gXtDU9jzXqg2hD87XusQgPqunF08GDmhnA=; b=nQfjh2wnUIeQTVzFBvSSfCCMxzyNUr+xaYeDPTc/wmn2TsfXpi/0xXEQSZPijBO70A v+/PRxzIddoOItLZjmmvT0TvI1Er/MFJG6+xEBFZ4VNVYOS8PmShdyqY13lW7r2Svr8V FDTgPbyzo5Qt4CwTN53Gabe5Gf8+e0SBpPOADcO6JGHUqoTnKfZ0fP22AdGvTD4k443L ENN96+i1ypRntG5cx8h8FZ4khb3pwGDbE8NkPMN6b4YtUMHl1Y2ffPkMGrGep+laaaEt QBXbnZ/Qb1nqh24k9twTm9KDGm065/BimpkwnstfgBRgw0OvS76jHJ0tymF0nrPtlYhB xI0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=gHbx+Mfk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id t13-20020a056a0021cd00b0068a557febfcsi2851671pfj.111.2023.11.01.20.26.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 20:26:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=gHbx+Mfk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 6F9D28088A5A; Wed, 1 Nov 2023 20:26:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348413AbjKBDZY (ORCPT + 99 others); Wed, 1 Nov 2023 23:25:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348397AbjKBDZR (ORCPT ); Wed, 1 Nov 2023 23:25:17 -0400 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [IPv6:2001:41d0:203:375::ba]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89BB0127 for ; Wed, 1 Nov 2023 20:25:11 -0700 (PDT) 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=1698895509; 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=bbgfkr34Ut5bOat3PkSebFKZ4A5Cv7UHaj4yNDSPhuU=; b=gHbx+MfkKMaXjWLoeHR6R2/oAAyFc9gfvo3ohtd4K4S4h0A6KE0hMrTwtFkBS0N7jtWhrk xt6ezAgfua8IzYH47q+ur2/jbneBGFE275j69jA9HgOkeQ4CEUTLkH6FIThRIPBcrygeuj fPAujlzG9ChYjmdwj+8gHQRxUvZnQVo= From: chengming.zhou@linux.dev To: vbabka@suse.cz, cl@linux.com, penberg@kernel.org Cc: rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, chengming.zhou@linux.dev, Chengming Zhou Subject: [PATCH v5 7/9] slub: Optimize deactivate_slab() Date: Thu, 2 Nov 2023 03:23:28 +0000 Message-Id: <20231102032330.1036151-8-chengming.zhou@linux.dev> In-Reply-To: <20231102032330.1036151-1-chengming.zhou@linux.dev> References: <20231102032330.1036151-1-chengming.zhou@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 01 Nov 2023 20:26:19 -0700 (PDT) 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 last stage if we really need to manipulate the slab list in this path. Signed-off-by: Chengming Zhou Reviewed-by: Vlastimil Babka Tested-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- mm/slub.c | 79 ++++++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 53 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index bcb5b2c4e213..d137468fe4b9 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2468,10 +2468,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; @@ -2509,65 +2507,40 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, /* * Stage two: Unfreeze the slab while splicing the per-cpu * freelist to the head of slab's freelist. - * - * Ensure that the slab is unfrozen while the list presence - * reflects the actual number of objects during unfreeze. - * - * We first perform cmpxchg holding lock and insert to list - * when it succeed. If there is mismatch then the slab is not - * 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; - - new.frozen = 0; + 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; + } + } 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); } } -- 2.20.1