From patchwork Thu Nov 2 03:23:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13443355 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 A5A48C4332F for ; Thu, 2 Nov 2023 03:25:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3EEE58E001C; Wed, 1 Nov 2023 23:25:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39D4E8E0009; Wed, 1 Nov 2023 23:25:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 217DB8E001C; Wed, 1 Nov 2023 23:25:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 097FB8E0009 for ; Wed, 1 Nov 2023 23:25:13 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C6E68140E21 for ; Thu, 2 Nov 2023 03:25:12 +0000 (UTC) X-FDA: 81411573264.15.6B88144 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) by imf27.hostedemail.com (Postfix) with ESMTP id 2007440009 for ; Thu, 2 Nov 2023 03:25:10 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=gHbx+Mfk; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.170 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698895511; a=rsa-sha256; cv=none; b=0Y4oQAQR6t2Rp5Wk+lE8cubM6fpi1UJH+GEX+C3MjniH3tBQ39H4m9E5+lCE+aDF2kYMlq sze2J2S1E8uLm3uWgvudtAwB2+Fg19PsL27WCGOuzAjipjJ2yjJpF09isOqxYhx551Bsya gUHacYMiBNr1NoRgWTnJ0aWPMOOoRaQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=gHbx+Mfk; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.170 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698895511; 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=bbgfkr34Ut5bOat3PkSebFKZ4A5Cv7UHaj4yNDSPhuU=; b=rjsi81zf+gZAmQWL7MG2erabCBTSkGoK1OoRMHvCc8PCk6CwgivXYcOdEZEUWq8BFl73O3 fVZFz215HjwFk9xRhoeE0N7S0bqK2GkUWZ9asDiCxo0Z4ZmFdqtPeEms67QKbERIgv4BH0 e0Q4wKGwY6IULGXCfSWSXWBrq3Qxt4M= 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 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2007440009 X-Stat-Signature: 6ip7gy7simk5wtzgrqe54ixx54znp598 X-HE-Tag: 1698895510-600265 X-HE-Meta: U2FsdGVkX1858zjIWRy7MagTzht5Du6r8OylJ0S5e6+zmrFo5gT0uiaEUKff4kgqwx9InrVn/lj/MhTb5mqKUJxQN/k2GcLBUD0aJRNYnAkSD8Cwe1W49pLQP6P2eS3so82LiwdzVI1qMNWGdTuC8t1oebdRiNU+MxyKFX8sPZo0iYqZKL+p28SsuRDAodODFDQdfhGrkWsS7ieFiXkVBkN6LUijTZYR2cK7PZ+F45noz2zQpfFsSb0IiDJCOwPNgeGM+pfhZhMLXe0c30PhmbDdbWZJ6kzdrx2Aufih4VY34zs3hzdaYpBHq9RilIR+j1KHjqXCMb/AUGnp15+DLrrPCFaMma4POk420zqj4VGHfW3YZo6oX7pcMCq6V0QUmzxi9to+LuJU3HW/pRB+/wmErb04pLoylYIAEZBl2d2n3bx78yRAobJZPBobuyDqjKwFw6W/vakcsjU+l81WmMBPkE+nttmHGUAlxn5Fcf3z+KBFu7tsaJf6fgijwsdIYzaxi9igycp4Nt/SbJw6gnRzEe5pnfM+f1ZuofQtEBuuj0uvE8G8YK4K574zDjOx3kUu1/jnuWZsq+5EpI12vISG7OMHxqBsmXaRj8nQcMyznEmAvEVmxQ43sKE/dxXiR/n4/EgGHkclGeqKb7QkbWHJTWyup50kPfD6XL8js5lPPE88nU7GZujuZqzla34ykQ8VQkNUu+BFdTh/aFTxnaG3YIyF8xKjD7UydvIXsCp27wzv7fYMwyXQFSW6n4aRNht5SBjmF7Lcui67RxHJU3J/pKUWgoSiW1Q3mj/TiqljyIbAxxd+geLSTpoxw90CzZkMIvBDUP0DeeLR7IMVgKKmzzemHfOzlAQxOd1mz4kvBJl2weFoz7NVJ5LZdMWz8hZFtsNTTB35MwD+C1drJZGvyeI7j+Lq1vV5NCY+ic0GWooMeqAXkvYJaFrmErrdY/eua3A3wD869O2zmc1 lkIaI1e3 hL3Tb0n+DHy0TSkn9FTx+T3dDt8YbcP+qhZ7jsKvwlIQpVZ5Mlc4FfvITSaf3cxZXak1ci4bHA2eQEJPiS/7BGZFwlaXe56MWLYxh7homZiQQWE8Al29j+cQx8mQo5e6BU5xgNkp4o2WWfLcpYIV0P8sq4J0AGqREs6zEXbNhaOtmorS2WGxYeOXsqMQQj+wEzbSF2YYa35RVDrKsJUFletO7GfHwxfMNNR2yRED09vPvY8Cktyne7B5UJG9tPTEGjP9yQsbKFaHhEdMx3sS4RpSQAg== 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: List-Subscribe: List-Unsubscribe: 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> Reviewed-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); } }