Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp271490rdb; Tue, 31 Oct 2023 07:10:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGBNzTqwxyA25duiK3uEyEzsg/vQUKFt+w2xsH7xqt2wcqO35+UGG0TGWbfgujD0yRM8lu/ X-Received: by 2002:a05:6a21:338e:b0:17a:f1eb:e143 with SMTP id yy14-20020a056a21338e00b0017af1ebe143mr16532611pzb.26.1698761434571; Tue, 31 Oct 2023 07:10:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698761434; cv=none; d=google.com; s=arc-20160816; b=i96oeYxNvy/U1Xoyj7cC4PznTkSb5gZoDNrXEIQcX1gFVjq/vmaKaNetP75MHb87Xy AZ0SHVxGvgMueDwvxGDv6SlOEESO0ypiMg2Qu1CWXrelX2ogQztbqUR7804aH2WIFwmK O4MTLXYNk6NXQcm1J4NJZ4iIAA+eAbd3x8w5U4zSWueQqqcRmFSen7wIn1Z+hmo5tFZA Ra91SnLcmF5fcIrjh/KQ6utpELoSj5Bdpu/o6SA9qsoKQkbhL/VEUi4d5U3zNMcR8Fcz Xknegit98Bcv3lFGqmqqHeHvnKvCnURYWe9G8hrUuL1DBB3Ho2D4z/WQyYZzmy8SzAky 6GnA== 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=71jccDuvmHUN0B2hAN0chVUZ3a/I3ZWuScusSDMRbZM=; fh=1GJaSiew/4nhYvae+yJYXPcARWi0PNZhXd1mB5ASXRg=; b=NZMqFelvxX6BerNouI230PFOmYIxyguCHia9zagchtZGulHygVnSSt+OusgMHCTvK9 6ktgbzS1QxqSjyPVGynIfx0E2g5jcXDCPbIgzXtNQmn2ccBUUFosbR2JllaNlab3jE4V YzHyhQrNP/mJvh217BS75OhekiUGWef7/vzoE9R6ssXw0lQPiW5GQX5rtWHrwAamWvhW bqmDgz9WcHKFdn/rhjEcjwhvJCpYxhQJIJklYWoeu7mfuCN78o9eh3/aAR9o2VeBsaEj uDqGdQi6tlj5ZOfJPoIX/XO8UUoUGlXfvwN+vujIUl9eCh6TUhn8WGkbTnA/PGU1Rcq9 LxaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=EgWg7Jyu; 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 g17-20020a631111000000b005b7e70085dcsi1055292pgl.433.2023.10.31.07.10.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 07:10:34 -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=EgWg7Jyu; 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 A180B804C657; Tue, 31 Oct 2023 07:10:02 -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 S233467AbjJaOJc (ORCPT + 99 others); Tue, 31 Oct 2023 10:09:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234896AbjJaOJY (ORCPT ); Tue, 31 Oct 2023 10:09:24 -0400 Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2B0A183 for ; Tue, 31 Oct 2023 07:09:19 -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=1698761358; 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=71jccDuvmHUN0B2hAN0chVUZ3a/I3ZWuScusSDMRbZM=; b=EgWg7JyuljiAgmtHeNxZPVDCEzu19y0onJXhXs8aae8cO3ZMQHroKY26QyUFiFaebUhzCc RGV6bWjUO05MthcYp2hTuKmvk++h80PzPUQqKT6vJe0w3lTBc8M2DdpDA2JE+ug45MtF26 X/0C77Wy9EZYl/zFaNFqEn7aLM2cGT8= From: chengming.zhou@linux.dev To: vbabka@suse.cz, cl@linux.com, penberg@kernel.org, willy@infradead.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: [RFC PATCH v4 7/9] slub: Optimize deactivate_slab() Date: Tue, 31 Oct 2023 14:07:39 +0000 Message-Id: <20231031140741.79387-8-chengming.zhou@linux.dev> In-Reply-To: <20231031140741.79387-1-chengming.zhou@linux.dev> References: <20231031140741.79387-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 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]); Tue, 31 Oct 2023 07:10:02 -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 --- mm/slub.c | 76 +++++++++++++++++++------------------------------------ 1 file changed, 26 insertions(+), 50 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index bcb5b2c4e213..c429f8baba5f 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; @@ -2512,62 +2510,40 @@ static void deactivate_slab(struct kmem_cache *s, struct slab *slab, * * 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