From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=90.155.50.34 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796039725; b=0tc5hbM67m6t/SXhWYAC/Zk2OKf5YpkHx6FjjHj71btEdIqDs3Eu7qCtNg2Roq3DhO4F jArnrblJg9G/Q4xhh46bm4KavN3zOuLbM+mTT2ahukq+9IcmXy4FWmFh0g7G3/XzXVD9N LnG8RuVe6+IUiT1eU55DKDa2wxbK0gW5REgbZy1oodRBWUwOJ5lm7ZL56BIGLtwFsdPUX 0RDMeRTcXrVAcql/T58PFCU2f7CpM6XfOBG1QuOA2jLIgmW6LjULoUNuj3Qs0APLgySti r6cpY4q1o0r7+tmaTKwyQIDoTohF8PB9RI0sHmOxhSnZJAciOxbYrbixVlE9TAV6djw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796039725; h=DMARC-Filter:DKIM-Signature:Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=TVD70w2N4Jm2HNPu0Xr+b49vDElcVSsAue7s0gXNSqQ=; b=dE7qfV3Ec0Ks5Dvel7bAvNIbo1TxMUZXXg0CkcGU/XWWUdHY8j1PVSJ2h/xaBQzTRUoy sV3Ux1KlQPkMSj9Q/RVbVBUNRIc9v6OtiH75KyPTVpdUUgVrtlroEw0p05I6u3Dv0qKbu sD0jam0uVh1vG8AI7XrwEtO/0LunCC//xbYJ1wHRIytPlO50ZWV6bBfg6mlk6tcpSCqih Zy9Pw3m0IN/Ol89R11MkNwzEQnkmbu3fvs7bzEJz1XVAq5xaMLdQBJM0ObMjElMkthN5p X+DPanHM735q1d3IkZhYo8PMGAVeNt23ftShshA9/rzKhnCnBg8WchiX8st6hz0+Cfw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=hETkQZMq; arc=none smtp.remote-ip=90.155.50.34 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2195A606A31C for ; Mon, 30 Nov 2026 11:55:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2195A606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=hETkQZMq DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=TVD70w2N4Jm2HNPu0Xr+b49vDElcVSsAue7s0gXNSqQ=; b=hETkQZMqOm2wn0L/NW3Dkkpgkh iMVyRnvbJYSbNF5ohC7WtAMN56OeP9aLsuxJ6FdGPMEWyGn2G60J7A0K2pp47V/zLRdjDwaJzKkVI yH9BaMPiai1+U+Wo9GMCv2VMTrhS/fNcGfiJ46IrE0bZPoX6dsxXktt6vCAlUOmoY49MsiGUSajIx wwdfIIeb6RXkaCoGJ+M/th+N40R5YUZVtA6lMgzkXcExt2vW1QsUDsSbq5JXrSKsBUQXZabmu724a 0Pn6xieXkl2NmojM22FFw0Li03JsM6n/f2qfXodisJoau5KIlZKjIZ9U1V1qjHtdVmcNVxinfPL0k KcNW0Msg==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXzyT-0000000BqHT-48uA; Mon, 30 Nov 2026 11:55:14 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id AF19A3011E4; Mon, 30 Nov 2026 12:55:09 +0100 (CET) Date: Mon, 30 Nov 2026 12:55:09 +0100 From: Peter Zijlstra To: jim.cromie@gmail.com Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Josh Poimboeuf , Jason Baron , Alice Ryhl , Steven Rostedt , Ard Biesheuvel , Andrew Morton , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 4/7] jump_label: Add batching API for queued static key updates Message-ID: <20261130115509.GL2668804@noisy.programming.kicks-ass.net> References: <20261129-jump-batch-261128-v2-0-b1829d2429f0@gmail.com> <20261129-jump-batch-261128-v2-4-b1829d2429f0@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261129-jump-batch-261128-v2-4-b1829d2429f0@gmail.com> On Sun, Nov 29, 2026 at 12:44:17AM -0700, Jim Cromie via B4 Relay wrote: > From: Jim Cromie > > Built-in subsystems such as dynamic-debug may toggle thousands of > boolean static keys in one operation, incurring O(N) cross-CPU > text-poke IPIs when calling static_branch_enable() or > static_branch_disable() one key at a time. > > Add a core-only batching API that amortizes cross-CPU text-poke > synchronization across a batch of static keys (up to > TEXT_POKE_ARRAY_MAX = 256 pokes per flush on x86) while keeping > key->enabled consistent with live kernel text: > > 0. Add static_key_batch_begin() and static_key_batch_end() (wrapped by > DEFINE_LOCK_GUARD_0(static_key_batch, ...) and static_branch_batch_* > helpers) to hold cpus_read_lock() and jump_label_lock() across a > batch and flush pending updates on exit. > 1. Add static_key_enable_queued() and static_key_disable_queued() > (plus static_branch_*_queued() wrappers and !CONFIG_JUMP_LABEL > stubs), unified in __static_key_set_queued(key, enable) and > unexported so only built-in core code may batch-flip keys. > 2. Defer publishing final key->enabled values (1 or 0) until after IPI > completion using negative transitional markers: > STATIC_KEY_TRANS_ENABLE (-1: 0 -> -1 -> 1, counted as 1) and > STATIC_KEY_TRANS_DISABLE (-2: 1 -> -2 -> 0, counted as 0). Record > in-flight keys in jump_label_queued_keys[MAX_QUEUED_KEYS] and > release-store their stable state in jump_label_finalize_queued_keys() > after arch_jump_label_transform_apply(). > 3. Flush the active batch via __static_key_flush_queued() before > queueing when jump_label_queued_keys[] is full, when the arch > transform queue fills mid-key, or when a batch reverses a key already > in-flight in the opposite direction (val == opp_trans) so live text > and key->enabled settle before the reverse patch is generated. > 4. Fall back to immediate jump_label_update() on architectures without > HAVE_JUMP_LABEL_BATCH, and extend jump_label_test() to exercise > scoped_guard(static_key_batch) and same-batch reversals. So this is just an iteration of what has been done; which I'm sure your agent or something wanted, but it tells me nothing of *WHY*. That is, for me the human, this all sucks balls. For instance, why do we need STATIC_KEY_TRANS_DISABLE ? We don't have this state currently. We set key->enable to 0 before we start patching, and this is fine. The above only states we add this gunk, but no explanation for why you think this is needed. Why do I have to guess at this? Also, I would much prefer not having static_key_{en,dis}able_queued() at all. It would be ever so much better if you could just use the existing static_key_{en,dis}able() inside of the batch guard, no? Smaller API more better. What's more; I think your previous patch is completely unneeded complexity, and I hate your endless #ifdef sprinkling. Does something like so work? --- --- a/include/linux/jump_label.h +++ b/include/linux/jump_label.h @@ -222,15 +222,17 @@ extern int jump_label_text_reserved(void extern bool static_key_slow_inc(struct static_key *key); extern bool static_key_fast_inc_not_disabled(struct static_key *key); extern void static_key_slow_dec(struct static_key *key); -extern bool static_key_slow_inc_cpuslocked(struct static_key *key); -extern void static_key_slow_dec_cpuslocked(struct static_key *key); extern int static_key_count(struct static_key *key); extern void static_key_enable(struct static_key *key); extern void static_key_disable(struct static_key *key); -extern void static_key_enable_cpuslocked(struct static_key *key); -extern void static_key_disable_cpuslocked(struct static_key *key); extern enum jump_label_type jump_label_init_type(struct jump_entry *entry); +extern void static_key_batch_begin(void); +extern void static_key_batch_end(void); + +extern void static_key_batch_begin_cpuslocked(void); +extern void static_key_batch_end_cpuslocked(void); + #define STATIC_KEY_INIT_TRUE \ { .enabled = ATOMIC_INIT(1), \ .type = JUMP_TYPE_TRUE } @@ -293,9 +295,6 @@ static inline void static_key_slow_dec(s atomic_dec(&key->enabled); } -#define static_key_slow_inc_cpuslocked(key) static_key_slow_inc(key) -#define static_key_slow_dec_cpuslocked(key) static_key_slow_dec(key) - static inline int jump_label_text_reserved(void *start, void *end) { return 0; @@ -326,8 +325,11 @@ static inline void static_key_disable(st atomic_set(&key->enabled, 0); } -#define static_key_enable_cpuslocked(k) static_key_enable((k)) -#define static_key_disable_cpuslocked(k) static_key_disable((k)) +static inline void static_key_batch_begin(void) { } +static inline void static_key_batch_end(void) { } + +static inline void static_key_batch_begin_cpuslocked(void) { } +static inline void static_key_batch_end_cpuslocked(void) { } #define STATIC_KEY_INIT_TRUE { .enabled = ATOMIC_INIT(1) } #define STATIC_KEY_INIT_FALSE { .enabled = ATOMIC_INIT(0) } @@ -335,10 +337,37 @@ static inline void static_key_disable(st #endif /* CONFIG_JUMP_LABEL */ DEFINE_LOCK_GUARD_0(jump_label_lock, jump_label_lock(), jump_label_unlock()) +DEFINE_LOCK_GUARD_0(static_key_batch, static_key_batch_begin(), static_key_batch_end()) +DEFINE_LOCK_GUARD_0(static_key_batch_cpuslocked, + static_key_batch_begin_cpuslocked(), + static_key_batch_end_cpuslocked()) #define STATIC_KEY_INIT STATIC_KEY_INIT_FALSE #define jump_label_enabled static_key_enabled +static inline void static_key_slow_inc_cpuslocked(struct static_key *key) +{ + guard(static_key_batch_cpuslocked)(); + static_key_slow_inc(key); +} + +static inline void static_key_slow_dec_cpuslocked(struct static_key *key) +{ + guard(static_key_batch_cpuslocked)(); + static_key_slow_dec(key); +} + +static inline void static_key_enable_cpuslocked(struct static_key *key) +{ + guard(static_key_batch_cpuslocked)(); + static_key_enable(key); +} + +static inline void static_key_disable_cpuslocked(struct static_key *key) +{ + guard(static_key_batch_cpuslocked)(); + static_key_disable(key); +} /* -------------------------------------------------------------------------- */ /* @@ -512,8 +541,6 @@ extern bool ____wrong_branch_error(void) #define static_branch_inc(x) static_key_slow_inc(&(x)->key) #define static_branch_dec(x) static_key_slow_dec(&(x)->key) -#define static_branch_inc_cpuslocked(x) static_key_slow_inc_cpuslocked(&(x)->key) -#define static_branch_dec_cpuslocked(x) static_key_slow_dec_cpuslocked(&(x)->key) /* * Normal usage; boolean enable/disable. @@ -521,8 +548,6 @@ extern bool ____wrong_branch_error(void) #define static_branch_enable(x) static_key_enable(&(x)->key) #define static_branch_disable(x) static_key_disable(&(x)->key) -#define static_branch_enable_cpuslocked(x) static_key_enable_cpuslocked(&(x)->key) -#define static_branch_disable_cpuslocked(x) static_key_disable_cpuslocked(&(x)->key) #endif /* __ASSEMBLER__ */ --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -113,6 +113,85 @@ int static_key_count(struct static_key * } EXPORT_SYMBOL_GPL(static_key_count); +static void __jump_label_finish(void); + +static struct task_struct *batch_owner; + +void static_key_batch_begin_cpuslocked(void) +{ + lockdep_assert_cpus_held(); + jump_label_lock(); + batch_owner = current; + barrier(); +} + +void static_key_batch_begin(void) +{ + cpus_read_lock(); + static_key_batch_begin_cpuslocked(); +} + +void static_key_batch_end_cpuslocked(void) +{ + lockdep_assert(batch_owner == current); + + __jump_label_finish(); + barrier(); + batch_owner = NULL; + jump_label_unlock(); +} + +void static_key_batch_end(void) +{ + static_key_batch_begin_cpuslocked(); + cpus_read_unlock(); +} + +static void jump_label_batch_lock(void) +{ + if (batch_owner == current) { + lockdep_assert_held(&jump_label_mutex); + return; + } + + jump_label_lock(); +} + +static void jump_label_batch_unlock(void) +{ + if (batch_owner == current) { + lockdep_assert_held(&jump_label_mutex); + return; + } + + __jump_label_finish(); + jump_label_unlock(); +} + +DEFINE_LOCK_GUARD_0(jump_label_batch_lock, jump_label_batch_lock(), jump_label_batch_unlock()) + +static void cpus_read_batch_lock(void) +{ + if (batch_owner == current) { + lockdep_assert_cpus_held(); + return; + } + + cpus_read_lock(); +} + +static void cpus_read_batch_unlock(void) +{ + if (batch_owner == current) { + lockdep_assert_cpus_held(); + return; + } + + cpus_read_unlock(); +} + +DEFINE_LOCK_GUARD_0(cpus_read_batch_lock, cpus_read_batch_lock(), cpus_read_batch_unlock()) + /* * static_key_fast_inc_not_disabled - adds a user for a static key * @key: static key that must be already enabled @@ -136,7 +215,7 @@ bool static_key_fast_inc_not_disabled(st * * The INT_MAX overflow condition is either used by the networking * code to reset or detected in the slow path of - * static_key_slow_inc_cpuslocked(). + * static_key_slow_inc(). */ v = atomic_read(&key->enabled); do { @@ -148,10 +227,8 @@ bool static_key_fast_inc_not_disabled(st } EXPORT_SYMBOL_GPL(static_key_fast_inc_not_disabled); -bool static_key_slow_inc_cpuslocked(struct static_key *key) +bool static_key_slow_inc(struct static_key *key) { - lockdep_assert_cpus_held(); - /* * Careful if we get concurrent static_key_slow_inc/dec() calls; * later calls must wait for the first one to _finish_ the @@ -162,7 +239,8 @@ bool static_key_slow_inc_cpuslocked(stru if (static_key_fast_inc_not_disabled(key)) return true; - guard(mutex)(&jump_label_mutex); + guard(cpus_read_batch_lock)(); + guard(jump_label_batch_lock)(); /* Try to mark it as 'enabling in progress. */ if (!atomic_cmpxchg(&key->enabled, 0, -1)) { jump_label_update(key); @@ -182,29 +260,19 @@ bool static_key_slow_inc_cpuslocked(stru } return true; } - -bool static_key_slow_inc(struct static_key *key) -{ - bool ret; - - cpus_read_lock(); - ret = static_key_slow_inc_cpuslocked(key); - cpus_read_unlock(); - return ret; -} EXPORT_SYMBOL_GPL(static_key_slow_inc); -void static_key_enable_cpuslocked(struct static_key *key) +void static_key_enable(struct static_key *key) { STATIC_KEY_CHECK_USE(key); - lockdep_assert_cpus_held(); if (atomic_read(&key->enabled) > 0) { WARN_ON_ONCE(atomic_read(&key->enabled) != 1); return; } - jump_label_lock(); + guard(cpus_read_batch_lock)(); + guard(jump_label_batch_lock)(); if (atomic_read(&key->enabled) == 0) { atomic_set(&key->enabled, -1); jump_label_update(key); @@ -213,40 +281,22 @@ void static_key_enable_cpuslocked(struct */ atomic_set_release(&key->enabled, 1); } - jump_label_unlock(); -} -EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked); - -void static_key_enable(struct static_key *key) -{ - cpus_read_lock(); - static_key_enable_cpuslocked(key); - cpus_read_unlock(); } EXPORT_SYMBOL_GPL(static_key_enable); -void static_key_disable_cpuslocked(struct static_key *key) +void static_key_disable(struct static_key *key) { STATIC_KEY_CHECK_USE(key); - lockdep_assert_cpus_held(); if (atomic_read(&key->enabled) != 1) { WARN_ON_ONCE(atomic_read(&key->enabled) != 0); return; } - jump_label_lock(); + guard(cpus_read_batch_lock)(); + guard(jump_label_batch_lock)(); if (atomic_cmpxchg(&key->enabled, 1, 0) == 1) jump_label_update(key); - jump_label_unlock(); -} -EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked); - -void static_key_disable(struct static_key *key) -{ - cpus_read_lock(); - static_key_disable_cpuslocked(key); - cpus_read_unlock(); } EXPORT_SYMBOL_GPL(static_key_disable); @@ -260,7 +310,7 @@ static bool static_key_dec_not_one(struc * is an imbalance, which is handled at the call site. * * That includes the special case of '-1' which is set in - * static_key_slow_inc_cpuslocked(), but that's harmless as it is + * static_key_slow_inc(), but that's harmless as it is * fully serialized in the slow path below. By the time this task * acquires the jump label lock the value is back to one and the * retry under the lock must succeed. @@ -289,19 +339,21 @@ static bool static_key_dec_not_one(struc return true; } -static void __static_key_slow_dec_cpuslocked(struct static_key *key) +void static_key_slow_dec(struct static_key *key) { - lockdep_assert_cpus_held(); int val; + STATIC_KEY_CHECK_USE(key); + if (static_key_dec_not_one(key)) return; + guard(cpus_read_batch_lock)(); guard(mutex)(&jump_label_mutex); val = atomic_read(&key->enabled); /* * It should be impossible to observe -1 with jump_label_mutex held, - * see static_key_slow_inc_cpuslocked(). + * see static_key_slow_inc(). */ if (WARN_ON_ONCE(val == -1)) return; @@ -314,35 +366,16 @@ static void __static_key_slow_dec_cpuslo if (atomic_dec_and_test(&key->enabled)) jump_label_update(key); } - -static void __static_key_slow_dec(struct static_key *key) -{ - cpus_read_lock(); - __static_key_slow_dec_cpuslocked(key); - cpus_read_unlock(); -} +EXPORT_SYMBOL_GPL(static_key_slow_dec); void jump_label_update_timeout(struct work_struct *work) { struct static_key_deferred *key = container_of(work, struct static_key_deferred, work.work); - __static_key_slow_dec(&key->key); + static_key_slow_dec(&key->key); } EXPORT_SYMBOL_GPL(jump_label_update_timeout); -void static_key_slow_dec(struct static_key *key) -{ - STATIC_KEY_CHECK_USE(key); - __static_key_slow_dec(key); -} -EXPORT_SYMBOL_GPL(static_key_slow_dec); - -void static_key_slow_dec_cpuslocked(struct static_key *key) -{ - STATIC_KEY_CHECK_USE(key); - __static_key_slow_dec_cpuslocked(key); -} - void __static_key_slow_dec_deferred(struct static_key *key, struct delayed_work *work, unsigned long timeout) @@ -489,6 +522,10 @@ static bool jump_label_can_update(struct } #ifndef HAVE_JUMP_LABEL_BATCH +static void __jump_label_finish(void) +{ +} + static void __jump_label_update(struct static_key *key, struct jump_entry *entry, struct jump_entry *stop, @@ -499,7 +536,28 @@ static void __jump_label_update(struct s arch_jump_label_transform(entry, jump_label_type(entry)); } } + #else + +#ifndef JUMP_LABEL_BATCH_SIZE +#define JUMP_LABEL_BATCH_SIZE 256 +#endif + +static struct static_key *jump_label_queue[JUMP_LABEL_BATCH_SIZE]; +static unsigned int jump_label_nr_queued; + +static void __jump_label_finish(void) +{ + arch_jump_label_transform_apply(); + for (unsigned int i = 0; i < jump_label_nr_queued; i++) { + struct static_key *key = jump_label_queue[i]; + + if (atomic_read(&key->enabled) == -1) + atomic_set_release(&key->enabled, 1); + } + jump_label_nr_queued = 0; +} + static void __jump_label_update(struct static_key *key, struct jump_entry *entry, struct jump_entry *stop, @@ -510,15 +568,16 @@ static void __jump_label_update(struct s if (!jump_label_can_update(entry, init)) continue; - if (!arch_jump_label_transform_queue(entry, jump_label_type(entry))) { + if (jump_label_nr_queued >= JUMP_LABEL_BATCH_SIZE || + !arch_jump_label_transform_queue(entry, jump_label_type(entry))) { /* * Queue is full: Apply the current queue and try again. */ - arch_jump_label_transform_apply(); + __jump_label_finish(); BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry))); } + jump_label_queue[jump_label_nr_queued++] = key; } - arch_jump_label_transform_apply(); } #endif