From nobody Tue Feb 10 06:07:41 2026 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74DF13382DB for ; Fri, 23 Jan 2026 06:53:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769151241; cv=none; b=JQGsB22tPBMubob/wV/4XzWF1SVSaJxb53wt01ybOdRq2Wk26iYbyh/3fYYGGgjtU8Hgxsbf20RRrvMWxf2JHf7auWCCWsQ3ZBZ+rvuO4Wti2BqUoj/Zv5XDfoMm8ceZha55wlvpGPAINUvPPh9XXfJHvZA5o7+6u35SX17Ul2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769151241; c=relaxed/simple; bh=h4baqkql7IQVWLoS6pgUrAcKIqRJ6t6+9gnVEilTvZU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DLR5nbG+YZ3JQxs5p2fbcfHP2rOvh6QxspJS13LdQu0Rl5jV7QDdhggSuWFZo/qwtP4PGHUyEgD4DZtWb3nOdYflM9FHY6z5wiO1N3287VTb5oy2Jb5aKaXiiHjnI0+fX3tZsOAE3qWZ9HIYn+nyYpVj6yqeZ4z943wM6p7f8p4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=Z2g9m/rc; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=u/P/pwZ9; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=Z2g9m/rc; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=u/P/pwZ9; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="Z2g9m/rc"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="u/P/pwZ9"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="Z2g9m/rc"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="u/P/pwZ9" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id C9F8033772; Fri, 23 Jan 2026 06:53:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1769151190; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lz6+CfboTVUMod3u7B664uLDJDmXudIqpgLE6OkuCxw=; b=Z2g9m/rcdQXb7ha3+S6UqASNZmOJNr7F30VJDOieU1AGvt+srX1olSuMb2LtLW0vnUEl2Y ZN/48UbRACPGgqHBdyew5JdSytha46m9hSDac//pLlIjia4RBo7098CZXcXccCRpXHDP7G Vypc5k7Lzp7SYcx4wPLpp+7Ndn5klNk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1769151190; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lz6+CfboTVUMod3u7B664uLDJDmXudIqpgLE6OkuCxw=; b=u/P/pwZ9ky+Ip6KENw9mi4Y3FkSM4RJB211LC5waIXA/I+Q4De3PBR+9IK/NaIAqDCm8GB LJOLnePCSyAPYgCw== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1769151190; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lz6+CfboTVUMod3u7B664uLDJDmXudIqpgLE6OkuCxw=; b=Z2g9m/rcdQXb7ha3+S6UqASNZmOJNr7F30VJDOieU1AGvt+srX1olSuMb2LtLW0vnUEl2Y ZN/48UbRACPGgqHBdyew5JdSytha46m9hSDac//pLlIjia4RBo7098CZXcXccCRpXHDP7G Vypc5k7Lzp7SYcx4wPLpp+7Ndn5klNk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1769151190; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lz6+CfboTVUMod3u7B664uLDJDmXudIqpgLE6OkuCxw=; b=u/P/pwZ9ky+Ip6KENw9mi4Y3FkSM4RJB211LC5waIXA/I+Q4De3PBR+9IK/NaIAqDCm8GB LJOLnePCSyAPYgCw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 7C249139F7; Fri, 23 Jan 2026 06:53:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 2McKHtYac2k4YgAAD6G6ig (envelope-from ); Fri, 23 Jan 2026 06:53:10 +0000 From: Vlastimil Babka Date: Fri, 23 Jan 2026 07:52:51 +0100 Subject: [PATCH v4 13/22] slab: remove the do_slab_free() fastpath Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260123-sheaves-for-all-v4-13-041323d506f7@suse.cz> References: <20260123-sheaves-for-all-v4-0-041323d506f7@suse.cz> In-Reply-To: <20260123-sheaves-for-all-v4-0-041323d506f7@suse.cz> To: Harry Yoo , Petr Tesarik , Christoph Lameter , David Rientjes , Roman Gushchin Cc: Hao Li , Andrew Morton , Uladzislau Rezki , "Liam R. Howlett" , Suren Baghdasaryan , Sebastian Andrzej Siewior , Alexei Starovoitov , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev, bpf@vger.kernel.org, kasan-dev@googlegroups.com, Vlastimil Babka X-Mailer: b4 0.14.3 X-Spamd-Result: default: False [-8.30 / 50.00]; REPLY(-4.00)[]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; RCPT_COUNT_TWELVE(0.00)[18]; ARC_NA(0.00)[]; RCVD_TLS_ALL(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; R_RATELIMIT(0.00)[to_ip_from(RLwn5r54y1cp81no5tmbbew5oc)]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[linux.dev,linux-foundation.org,gmail.com,oracle.com,google.com,linutronix.de,kernel.org,kvack.org,vger.kernel.org,lists.linux.dev,googlegroups.com,suse.cz]; MID_RHS_MATCH_FROM(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.cz:mid,suse.cz:email,imap1.dmz-prg2.suse.org:helo,oracle.com:email] X-Spam-Flag: NO X-Spam-Score: -8.30 X-Spam-Level: We have removed cpu slab usage from allocation paths. Now remove do_slab_free() which was freeing objects to the cpu slab when the object belonged to it. Instead call __slab_free() directly, which was previously the fallback. This simplifies kfree_nolock() - when freeing to percpu sheaf fails, we can call defer_free() directly. Also remove functions that became unused. Reviewed-by: Harry Yoo Reviewed-by: Hao Li Reviewed-by: Suren Baghdasaryan Signed-off-by: Vlastimil Babka Acked-by: Alexei Starovoitov --- mm/slub.c | 149 ++++++----------------------------------------------------= ---- 1 file changed, 13 insertions(+), 136 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 914b51aedb25..a63a0eed2c55 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3678,29 +3678,6 @@ static inline unsigned int init_tid(int cpu) return cpu; } =20 -static inline void note_cmpxchg_failure(const char *n, - const struct kmem_cache *s, unsigned long tid) -{ -#ifdef SLUB_DEBUG_CMPXCHG - unsigned long actual_tid =3D __this_cpu_read(s->cpu_slab->tid); - - pr_info("%s %s: cmpxchg redo ", n, s->name); - - if (IS_ENABLED(CONFIG_PREEMPTION) && - tid_to_cpu(tid) !=3D tid_to_cpu(actual_tid)) { - pr_warn("due to cpu change %d -> %d\n", - tid_to_cpu(tid), tid_to_cpu(actual_tid)); - } else if (tid_to_event(tid) !=3D tid_to_event(actual_tid)) { - pr_warn("due to cpu running other code. Event %ld->%ld\n", - tid_to_event(tid), tid_to_event(actual_tid)); - } else { - pr_warn("for unknown reason: actual=3D%lx was=3D%lx target=3D%lx\n", - actual_tid, tid, next_tid(tid)); - } -#endif - stat(s, CMPXCHG_DOUBLE_CPU_FAIL); -} - static void init_kmem_cache_cpus(struct kmem_cache *s) { #ifdef CONFIG_PREEMPT_RT @@ -4239,18 +4216,6 @@ static inline bool pfmemalloc_match(struct slab *sla= b, gfp_t gfpflags) return true; } =20 -static inline bool -__update_cpu_freelist_fast(struct kmem_cache *s, - void *freelist_old, void *freelist_new, - unsigned long tid) -{ - struct freelist_tid old =3D { .freelist =3D freelist_old, .tid =3D tid }; - struct freelist_tid new =3D { .freelist =3D freelist_new, .tid =3D next_t= id(tid) }; - - return this_cpu_try_cmpxchg_freelist(s->cpu_slab->freelist_tid, - &old.freelist_tid, new.freelist_tid); -} - /* * Get the slab's freelist and do not freeze it. * @@ -6185,99 +6150,6 @@ void defer_free_barrier(void) irq_work_sync(&per_cpu_ptr(&defer_free_objects, cpu)->work); } =20 -/* - * Fastpath with forced inlining to produce a kfree and kmem_cache_free th= at - * can perform fastpath freeing without additional function calls. - * - * The fastpath is only possible if we are freeing to the current cpu slab - * of this processor. This typically the case if we have just allocated - * the item before. - * - * If fastpath is not possible then fall back to __slab_free where we deal - * with all sorts of special processing. - * - * Bulk free of a freelist with several objects (all pointing to the - * same slab) possible by specifying head and tail ptr, plus objects - * count (cnt). Bulk free indicated by tail pointer being set. - */ -static __always_inline void do_slab_free(struct kmem_cache *s, - struct slab *slab, void *head, void *tail, - int cnt, unsigned long addr) -{ - /* cnt =3D=3D 0 signals that it's called from kfree_nolock() */ - bool allow_spin =3D cnt; - struct kmem_cache_cpu *c; - unsigned long tid; - void **freelist; - -redo: - /* - * Determine the currently cpus per cpu slab. - * The cpu may change afterward. However that does not matter since - * data is retrieved via this pointer. If we are on the same cpu - * during the cmpxchg then the free will succeed. - */ - c =3D raw_cpu_ptr(s->cpu_slab); - tid =3D READ_ONCE(c->tid); - - /* Same with comment on barrier() in __slab_alloc_node() */ - barrier(); - - if (unlikely(slab !=3D c->slab)) { - if (unlikely(!allow_spin)) { - /* - * __slab_free() can locklessly cmpxchg16 into a slab, - * but then it might need to take spin_lock - * for further processing. - * Avoid the complexity and simply add to a deferred list. - */ - defer_free(s, head); - } else { - __slab_free(s, slab, head, tail, cnt, addr); - } - return; - } - - if (unlikely(!allow_spin)) { - if ((in_nmi() || !USE_LOCKLESS_FAST_PATH()) && - local_lock_is_locked(&s->cpu_slab->lock)) { - defer_free(s, head); - return; - } - cnt =3D 1; /* restore cnt. kfree_nolock() frees one object at a time */ - } - - if (USE_LOCKLESS_FAST_PATH()) { - freelist =3D READ_ONCE(c->freelist); - - set_freepointer(s, tail, freelist); - - if (unlikely(!__update_cpu_freelist_fast(s, freelist, head, tid))) { - note_cmpxchg_failure("slab_free", s, tid); - goto redo; - } - } else { - __maybe_unused unsigned long flags =3D 0; - - /* Update the free list under the local lock */ - local_lock_cpu_slab(s, flags); - c =3D this_cpu_ptr(s->cpu_slab); - if (unlikely(slab !=3D c->slab)) { - local_unlock_cpu_slab(s, flags); - goto redo; - } - tid =3D c->tid; - freelist =3D c->freelist; - - set_freepointer(s, tail, freelist); - c->freelist =3D head; - c->tid =3D next_tid(tid); - - local_unlock_cpu_slab(s, flags); - } - stat_add(s, FREE_FASTPATH, cnt); -} - static __fastpath_inline void slab_free(struct kmem_cache *s, struct slab *slab, void *object, unsigned long addr) @@ -6294,7 +6166,7 @@ void slab_free(struct kmem_cache *s, struct slab *sla= b, void *object, return; } =20 - do_slab_free(s, slab, object, object, 1, addr); + __slab_free(s, slab, object, object, 1, addr); } =20 #ifdef CONFIG_MEMCG @@ -6303,7 +6175,7 @@ static noinline void memcg_alloc_abort_single(struct kmem_cache *s, void *object) { if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), false))) - do_slab_free(s, virt_to_slab(object), object, object, 1, _RET_IP_); + __slab_free(s, virt_to_slab(object), object, object, 1, _RET_IP_); } #endif =20 @@ -6318,7 +6190,7 @@ void slab_free_bulk(struct kmem_cache *s, struct slab= *slab, void *head, * to remove objects, whose reuse must be delayed. */ if (likely(slab_free_freelist_hook(s, &head, &tail, &cnt))) - do_slab_free(s, slab, head, tail, cnt, addr); + __slab_free(s, slab, head, tail, cnt, addr); } =20 #ifdef CONFIG_SLUB_RCU_DEBUG @@ -6344,14 +6216,14 @@ static void slab_free_after_rcu_debug(struct rcu_he= ad *rcu_head) =20 /* resume freeing */ if (slab_free_hook(s, object, slab_want_init_on_free(s), true)) - do_slab_free(s, slab, object, object, 1, _THIS_IP_); + __slab_free(s, slab, object, object, 1, _THIS_IP_); } #endif /* CONFIG_SLUB_RCU_DEBUG */ =20 #ifdef CONFIG_KASAN_GENERIC void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr) { - do_slab_free(cache, virt_to_slab(x), x, x, 1, addr); + __slab_free(cache, virt_to_slab(x), x, x, 1, addr); } #endif =20 @@ -6557,7 +6429,12 @@ void kfree_nolock(const void *object) return; } =20 - do_slab_free(s, slab, x, x, 0, _RET_IP_); + /* + * __slab_free() can locklessly cmpxchg16 into a slab, but then it might + * need to take spin_lock for further processing. + * Avoid the complexity and simply add to a deferred list. + */ + defer_free(s, x); } EXPORT_SYMBOL_GPL(kfree_nolock); =20 @@ -6983,7 +6860,7 @@ static void __kmem_cache_free_bulk(struct kmem_cache = *s, size_t size, void **p) if (kfence_free(df.freelist)) continue; =20 - do_slab_free(df.s, df.slab, df.freelist, df.tail, df.cnt, + __slab_free(df.s, df.slab, df.freelist, df.tail, df.cnt, _RET_IP_); } while (likely(size)); } @@ -7069,7 +6946,7 @@ __refill_objects(struct kmem_cache *s, void **p, gfp_= t gfp, unsigned int min, cnt++; object =3D get_freepointer(s, object); } while (object); - do_slab_free(s, slab, head, tail, cnt, _RET_IP_); + __slab_free(s, slab, head, tail, cnt, _RET_IP_); } =20 if (refilled >=3D max) --=20 2.52.0