From patchwork Mon May 15 07:57:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 13240936 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 6D012C77B7D for ; Mon, 15 May 2023 08:07:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5553280003; Mon, 15 May 2023 04:06:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BADA9280006; Mon, 15 May 2023 04:06:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DF21280003; Mon, 15 May 2023 04:06:55 -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 3D367280001 for ; Mon, 15 May 2023 04:06:55 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 12DBB161335 for ; Mon, 15 May 2023 08:06:55 +0000 (UTC) X-FDA: 80791758390.28.7D6AD5D Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf04.hostedemail.com (Postfix) with ESMTP id E13324000A for ; Mon, 15 May 2023 08:06:52 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=pfsV7Qbq; spf=none (imf04.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=peterz@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684138013; 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-type:content-transfer-encoding:in-reply-to: references:references:dkim-signature; bh=1Hyxt0TX1t/0uiattJSuj2ZTNM+BZCXyokp3fCNZGCs=; b=08bgc3MBjhv+c2zOkxcmQ9sy0gvdOWmC+D+Xllu3I3dCeH7NDDmK2ei/nHCGu5cpPVdArB nIlVoAyeRLDB+mFUYUL7/jxuZZcW5Wb+UjIDEYw96CdVZlG29bABHejF/olNw0ABdmgQsa RnQctatr8VgI4/CuCtmDfyyxpN5GpbU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684138013; a=rsa-sha256; cv=none; b=zWjh2pafL2uKGwArDJ2CnH+EdTcpYnqbeEsoz345ZE2KwXJFuX/p/vDsjZyb7juPNtjseS AqvJbbhvBpz4egXFN47nw5OaZ2YX9CSz32CiKWLN8kBBaTTb6LsA7r9DJVVf1i4AEoSUda U4oed7Y6I09owbk74tNT3WUdg0qkrfo= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=pfsV7Qbq; spf=none (imf04.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=peterz@infradead.org; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=1Hyxt0TX1t/0uiattJSuj2ZTNM+BZCXyokp3fCNZGCs=; b=pfsV7Qbq81Z0IkI+Xvza6f76LG geyP1yqSpQ0cdv4M5vnGLyJVao4N1TuqaCz9CzumRH4FSACCzlrD9SnOzgfbEEzEvlPz+a1s+R1SD XU6IhOkvFQJ93yyWdsEupR2Mqx52rhYjF0Seid0p6zqtjuBqiFf3cZqv6TBGDQ8ShnrRoZ97CPad8 +l+HuRdyiowbD/zkPhjH4ZDmYadt9tcyGKRYa7V3/pNYx2IcgbVK+CnGZ/uUl0ZtJbAq33shpR5c0 1ZAI8rrHM/oDGodef2VoDHWu43MSu7ivZC62mvy5lcoU6nvfYdh41GMGxjhG5dmJecYvR0ryroQ/Y r3AbSCuA==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1pyTDl-003HUT-CX; Mon, 15 May 2023 08:06:17 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 870AD302680; Mon, 15 May 2023 10:06:15 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id CBFAF202FCEA6; Mon, 15 May 2023 10:06:10 +0200 (CEST) Message-ID: <20230515080554.453785148@infradead.org> User-Agent: quilt/0.66 Date: Mon, 15 May 2023 09:57:07 +0200 From: Peter Zijlstra To: torvalds@linux-foundation.org Cc: corbet@lwn.net, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, mark.rutland@arm.com, catalin.marinas@arm.com, dennis@kernel.org, tj@kernel.org, cl@linux.com, hca@linux.ibm.com, gor@linux.ibm.com, agordeev@linux.ibm.com, borntraeger@linux.ibm.com, svens@linux.ibm.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, joro@8bytes.org, suravee.suthikulpanit@amd.com, robin.murphy@arm.com, dwmw2@infradead.org, baolu.lu@linux.intel.com, Arnd Bergmann , Herbert Xu , davem@davemloft.net, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, Andrew Morton , vbabka@suse.cz, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-s390@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-crypto@vger.kernel.org Subject: [PATCH v3 08/11] slub: Replace cmpxchg_double() References: <20230515075659.118447996@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: E13324000A X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: 8gc4r9f8e1achjx1n9inxxbfq48aynbg X-HE-Tag: 1684138012-893557 X-HE-Meta: U2FsdGVkX1/hBmmz8Z3C9ioHqreQ9wI6kS8dQ0S5OaSZ5M85jedwhq2RJQKqgAg3P98hceHZAWrAY8z6qJljdBQYsHUa9QecS+YXIMWPah6KJ4+CphGwzWZ9OT0t3qNCT3Fj6G+1nw/IOay/T8qw/TnbCNUJS76sbfOo5LB2GZMRnVbQst1k/Vz50RQ0xicAERJkZmSSpqmIIMGWrLX6mJ4BzTbbYpn6xJxvHdz5m4wfEutsVP4x/97Z4Dy0CEp8uQJAT4/pqiiUe63JVOEfCh5z9ounIrhrQLwI69zaZGOpsPtLqq7it18cMUIfzVmlRNo9tKh1mLybIeK+gasg63DSfsfP7IL3fwLMXYUN7YF7mztmvdgux5SHBLSUZXcN72p0a/44gWdwZH80ujLdtkdTGecU7LnzE/Ofbj/ydCxzJaAhMFxCCxkmwdMLlgvmorCC1uDKW9Qtv25eTD/omSwrgwmSdTznoKg2xYZ9+ug8zXsp5bO41MuuV3ldyaYm16/TS6OrTvPdgUMc2d11mPLtNsiAPKGMtkJB74J6uW7vE2G+jspaqxXdq6e8ycFwvPZoThmSq37+NWKanNM5k6h2Uho0SWYtH2z2bPcFHUs7fOTCfBZXTxUcWhm8fWkxbK9Imy2z1es1IDMdvJ8h4oLG7fxk1Eh1G1NVcp1jGRokIf6R7C+2A4n6kv5A+U8CSzFbEdFYG3H3sbXdonIrjz0SV4elxTdPwL/p+cnTuyEMqOgFjHg9Eixe4eGggEXp3i6jegD/VujONHI361oKwzP5PPJJcKuzTNV37HecKL4qtrEiAS6xOYaCF7W5S8y1jWHeRDHQzfA36RLeyugWnArm1ulHoXJ7waVc+rDr28mfjILElvqR9ujaEKrfq/zEDo1a7Ww1Odb4w1eXl9EYs54iqSr3of4WWC0Cfdrjx8BUCYi9bNBXqF+iDVTZfDyO+OHj5IsM+93ZT1h2u7c yAsjviJf Ri2z5tkBRbOFqZ7uyHwytiYMDU5iyIqRZTv4Y/gOtgBXXDxW3Y/5elqMoVB1E/0/1zvegN404ssMXXSoXwxroUY9R3jPWgRokxYNsDDDl9aUzX6V5zL5+FVvyq27Lb1Ngw8DISnA20Z1Tf/t89zWqS2S4VsQF5i14kC2Mu3xqOGBamw4j2vK458nu5eMeISHBMy2I6vRh4iKiYV1HNknW8cq1GwESkZ1MwickwD8GJIEQjGiC1Kayab6/AFxPHEDjnu8x3T2YAg7Iy0meea9vgQvRo1dXDxkJTdutZuDc0NieiPh5zUipDu9Ih67Px03gCtSQhsRZ4JcQMdG/YzRT128507V75Rbk/SHbA14XkgGgAmlxZXUfjU6qjPzCGaVmrOhKX0Y5S+xhVJKJWb1dgLeZ5UuPJ2PD+/Ma 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: Signed-off-by: Peter Zijlstra (Intel) Acked-by: Vlastimil Babka Acked-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- include/linux/slub_def.h | 12 ++- mm/slab.h | 49 ++++++++++++++-- mm/slub.c | 143 ++++++++++++++++++++++++++++------------------- 3 files changed, 138 insertions(+), 66 deletions(-) --- a/include/linux/slub_def.h +++ b/include/linux/slub_def.h @@ -39,7 +39,8 @@ enum stat_item { CPU_PARTIAL_FREE, /* Refill cpu partial on free */ CPU_PARTIAL_NODE, /* Refill cpu partial from node partial */ CPU_PARTIAL_DRAIN, /* Drain cpu partial to node partial */ - NR_SLUB_STAT_ITEMS }; + NR_SLUB_STAT_ITEMS +}; #ifndef CONFIG_SLUB_TINY /* @@ -47,8 +48,13 @@ enum stat_item { * with this_cpu_cmpxchg_double() alignment requirements. */ struct kmem_cache_cpu { - void **freelist; /* Pointer to next available object */ - unsigned long tid; /* Globally unique transaction id */ + union { + struct { + void **freelist; /* Pointer to next available object */ + unsigned long tid; /* Globally unique transaction id */ + }; + freelist_aba_t freelist_tid; + }; struct slab *slab; /* The slab from which we are allocating */ #ifdef CONFIG_SLUB_CPU_PARTIAL struct slab *partial; /* Partially allocated frozen slabs */ --- a/mm/slab.h +++ b/mm/slab.h @@ -6,6 +6,38 @@ */ void __init kmem_cache_init(void); +#ifdef CONFIG_HAVE_ALIGNED_STRUCT_PAGE +#ifdef CONFIG_64BIT +# ifdef system_has_cmpxchg128 +# define system_has_freelist_aba() system_has_cmpxchg128() +# define try_cmpxchg_freelist try_cmpxchg128 +# define this_cpu_cmpxchg_freelist this_cpu_cmpxchg128 +typedef u128 freelist_full_t; +# endif +#else /* CONFIG_64BIT */ +# ifdef system_has_cmpxchg64 +# define system_has_freelist_aba() system_has_cmpxchg64() +# define try_cmpxchg_freelist try_cmpxchg64 +# define this_cpu_cmpxchg_freelist this_cpu_cmpxchg64 +typedef u64 freelist_full_t; +# endif +#endif /* CONFIG_64BIT */ +#endif /* CONFIG_HAVE_ALIGNED_STRUCT_PAGE */ + +/* + * Freelist pointer and counter to cmpxchg together, avoids the typical ABA + * problems with cmpxchg of just a pointer. + */ +typedef union { +#ifdef system_has_freelist_aba + struct { + void *freelist; + unsigned long counter; + }; + freelist_full_t full; +#endif +} freelist_aba_t; + /* Reuses the bits in struct page */ struct slab { unsigned long __page_flags; @@ -38,14 +70,19 @@ struct slab { #endif }; /* Double-word boundary */ - void *freelist; /* first free object */ union { - unsigned long counters; struct { - unsigned inuse:16; - unsigned objects:15; - unsigned frozen:1; + void *freelist; /* first free object */ + union { + unsigned long counters; + struct { + unsigned inuse:16; + unsigned objects:15; + unsigned frozen:1; + }; + }; }; + freelist_aba_t freelist_counter; }; }; struct rcu_head rcu_head; @@ -72,7 +109,7 @@ SLAB_MATCH(memcg_data, memcg_data); #endif #undef SLAB_MATCH static_assert(sizeof(struct slab) <= sizeof(struct page)); -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && defined(CONFIG_SLUB) +#if defined(system_has_freelist_aba) && defined(CONFIG_SLUB) static_assert(IS_ALIGNED(offsetof(struct slab, freelist), 2*sizeof(void *))); #endif --- a/mm/slub.c +++ b/mm/slub.c @@ -292,7 +292,12 @@ static inline bool kmem_cache_has_cpu_pa /* Poison object */ #define __OBJECT_POISON ((slab_flags_t __force)0x80000000U) /* Use cmpxchg_double */ + +#ifdef system_has_freelist_aba #define __CMPXCHG_DOUBLE ((slab_flags_t __force)0x40000000U) +#else +#define __CMPXCHG_DOUBLE ((slab_flags_t __force)0U) +#endif /* * Tracking user of a slab. @@ -512,6 +517,40 @@ static __always_inline void slab_unlock( __bit_spin_unlock(PG_locked, &page->flags); } +static inline bool +__update_freelist_fast(struct slab *slab, + void *freelist_old, unsigned long counters_old, + void *freelist_new, unsigned long counters_new) +{ +#ifdef system_has_freelist_aba + freelist_aba_t old = { .freelist = freelist_old, .counter = counters_old }; + freelist_aba_t new = { .freelist = freelist_new, .counter = counters_new }; + + return try_cmpxchg_freelist(&slab->freelist_counter.full, &old.full, new.full); +#else + return false; +#endif +} + +static inline bool +__update_freelist_slow(struct slab *slab, + void *freelist_old, unsigned long counters_old, + void *freelist_new, unsigned long counters_new) +{ + bool ret = false; + + slab_lock(slab); + if (slab->freelist == freelist_old && + slab->counters == counters_old) { + slab->freelist = freelist_new; + slab->counters = counters_new; + ret = true; + } + slab_unlock(slab); + + return ret; +} + /* * Interrupts must be disabled (for the fallback code to work right), typically * by an _irqsave() lock variant. On PREEMPT_RT the preempt_disable(), which is @@ -519,33 +558,25 @@ static __always_inline void slab_unlock( * allocation/ free operation in hardirq context. Therefore nothing can * interrupt the operation. */ -static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab, +static inline bool __slab_update_freelist(struct kmem_cache *s, struct slab *slab, void *freelist_old, unsigned long counters_old, void *freelist_new, unsigned long counters_new, const char *n) { + bool ret; + if (USE_LOCKLESS_FAST_PATH()) lockdep_assert_irqs_disabled(); -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) + if (s->flags & __CMPXCHG_DOUBLE) { - if (cmpxchg_double(&slab->freelist, &slab->counters, - freelist_old, counters_old, - freelist_new, counters_new)) - return true; - } else -#endif - { - slab_lock(slab); - if (slab->freelist == freelist_old && - slab->counters == counters_old) { - slab->freelist = freelist_new; - slab->counters = counters_new; - slab_unlock(slab); - return true; - } - slab_unlock(slab); + ret = __update_freelist_fast(slab, freelist_old, counters_old, + freelist_new, counters_new); + } else { + ret = __update_freelist_slow(slab, freelist_old, counters_old, + freelist_new, counters_new); } + if (likely(ret)) + return true; cpu_relax(); stat(s, CMPXCHG_DOUBLE_FAIL); @@ -557,36 +588,26 @@ static inline bool __cmpxchg_double_slab return false; } -static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab, +static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab, void *freelist_old, unsigned long counters_old, void *freelist_new, unsigned long counters_new, const char *n) { -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) + bool ret; + if (s->flags & __CMPXCHG_DOUBLE) { - if (cmpxchg_double(&slab->freelist, &slab->counters, - freelist_old, counters_old, - freelist_new, counters_new)) - return true; - } else -#endif - { + ret = __update_freelist_fast(slab, freelist_old, counters_old, + freelist_new, counters_new); + } else { unsigned long flags; local_irq_save(flags); - slab_lock(slab); - if (slab->freelist == freelist_old && - slab->counters == counters_old) { - slab->freelist = freelist_new; - slab->counters = counters_new; - slab_unlock(slab); - local_irq_restore(flags); - return true; - } - slab_unlock(slab); + ret = __update_freelist_slow(slab, freelist_old, counters_old, + freelist_new, counters_new); local_irq_restore(flags); } + if (likely(ret)) + return true; cpu_relax(); stat(s, CMPXCHG_DOUBLE_FAIL); @@ -2228,7 +2249,7 @@ static inline void *acquire_slab(struct VM_BUG_ON(new.frozen); new.frozen = 1; - if (!__cmpxchg_double_slab(s, slab, + if (!__slab_update_freelist(s, slab, freelist, counters, new.freelist, new.counters, "acquire_slab")) @@ -2554,7 +2575,7 @@ static void deactivate_slab(struct kmem_ } - if (!cmpxchg_double_slab(s, slab, + if (!slab_update_freelist(s, slab, old.freelist, old.counters, new.freelist, new.counters, "unfreezing slab")) { @@ -2611,7 +2632,7 @@ static void __unfreeze_partials(struct k new.frozen = 0; - } while (!__cmpxchg_double_slab(s, slab, + } while (!__slab_update_freelist(s, slab, old.freelist, old.counters, new.freelist, new.counters, "unfreezing slab")); @@ -3008,6 +3029,22 @@ static inline bool pfmemalloc_match(stru } #ifndef CONFIG_SLUB_TINY +static inline bool +__update_cpu_freelist_fast(struct kmem_cache *s, + void *freelist_old, void *freelist_new, + unsigned long tid) +{ +#ifdef system_has_freelist_aba + freelist_aba_t old = { .freelist = freelist_old, .counter = tid }; + freelist_aba_t new = { .freelist = freelist_new, .counter = next_tid(tid) }; + + return this_cpu_cmpxchg_freelist(s->cpu_slab->freelist_tid.full, + old.full, new.full) == old.full; +#else + return false; +#endif +} + /* * Check the slab->freelist and either transfer the freelist to the * per cpu freelist or deactivate the slab. @@ -3034,7 +3071,7 @@ static inline void *get_freelist(struct new.inuse = slab->objects; new.frozen = freelist != NULL; - } while (!__cmpxchg_double_slab(s, slab, + } while (!__slab_update_freelist(s, slab, freelist, counters, NULL, new.counters, "get_freelist")); @@ -3359,11 +3396,7 @@ static __always_inline void *__slab_allo * against code executing on this cpu *not* from access by * other cpus. */ - if (unlikely(!this_cpu_cmpxchg_double( - s->cpu_slab->freelist, s->cpu_slab->tid, - object, tid, - next_object, next_tid(tid)))) { - + if (unlikely(!__update_cpu_freelist_fast(s, object, next_object, tid))) { note_cmpxchg_failure("slab_alloc", s, tid); goto redo; } @@ -3631,7 +3664,7 @@ static void __slab_free(struct kmem_cach } } - } while (!cmpxchg_double_slab(s, slab, + } while (!slab_update_freelist(s, slab, prior, counters, head, new.counters, "__slab_free")); @@ -3736,11 +3769,7 @@ static __always_inline void do_slab_free set_freepointer(s, tail_obj, freelist); - if (unlikely(!this_cpu_cmpxchg_double( - s->cpu_slab->freelist, s->cpu_slab->tid, - freelist, tid, - head, next_tid(tid)))) { - + if (unlikely(!__update_cpu_freelist_fast(s, freelist, head, tid))) { note_cmpxchg_failure("slab_free", s, tid); goto redo; } @@ -4505,11 +4534,11 @@ static int kmem_cache_open(struct kmem_c } } -#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ - defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) - if (system_has_cmpxchg_double() && (s->flags & SLAB_NO_CMPXCHG) == 0) +#ifdef system_has_freelist_aba + if (system_has_freelist_aba() && !(s->flags & SLAB_NO_CMPXCHG)) { /* Enable fast mode */ s->flags |= __CMPXCHG_DOUBLE; + } #endif /*