From: Jim Cromie Date: Sun, 29 Nov 2026 00:44:17 -0700 Subject: [PATCH v2 4/7] jump_label: Add batching API for queued static key updates MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-jump-batch-261128-v2-4-b1829d2429f0@gmail.com> References: <20261129-jump-batch-261128-v2-0-b1829d2429f0@gmail.com> In-Reply-To: <20261129-jump-batch-261128-v2-0-b1829d2429f0@gmail.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Alice Ryhl , Steven Rostedt , Ard Biesheuvel , Andrew Morton Cc: linux-kernel@vger.kernel.org, Jim Cromie X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795938258; l=15789; i=jim.cromie@gmail.com; s=20260203; h=from:subject:message-id; bh=Au7jHC7KP8TdrnuialOqE+X6uqO13EovqYLPq6oxfgg=; b=dFX3hVyauV8IWZto40ML3yKYsCpSFN/aNC0Hlkb5C6miUTCAE1tFKcfPMjni2lHBZzgytaYYP 9AMSuPGzAZLBxDp2YVB63cBI4G+XZj0s8rGXG4TwWLXrOYvSatQUDk9 X-Developer-Key: i=jim.cromie@gmail.com; a=ed25519; pk=C6E5ODlPQo7ZBynATXH9wg7K6HxP0pIXyf4s38Qw0XE= X-Endpoint-Received: by B4 Relay for jim.cromie@gmail.com/20260203 with auth_id=958 List-Id: B4 Relay Submissions 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. What's Unchanged: 0. Unqueued static_key_enable(), static_key_disable(), and refcounted static_key_slow_inc() / static_key_slow_dec() keep identical behavior and locking, and arch SYSTEM_BOOTING handling is unchanged. 1. External jump_label_mutex acquisitions never observe transitional states (-1 or -2), because static_key_batch holds jump_label_mutex until all queued keys are finalized. Signed-off-by: Jim Cromie --- include/linux/jump_label.h | 22 +++++ kernel/jump_label.c | 240 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 246 insertions(+), 16 deletions(-) diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h index c95350a10987..7daff74670dc 100644 --- a/include/linux/jump_label.h +++ b/include/linux/jump_label.h @@ -227,6 +227,10 @@ 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); +void static_key_enable_queued(struct static_key *key); +void static_key_disable_queued(struct static_key *key); +void static_key_batch_begin(void); +void static_key_batch_end(void); 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); @@ -326,6 +330,19 @@ static inline void static_key_disable(struct static_key *key) atomic_set(&key->enabled, 0); } +static inline void static_key_enable_queued(struct static_key *key) +{ + static_key_enable(key); +} + +static inline void static_key_disable_queued(struct static_key *key) +{ + static_key_disable(key); +} + +static inline void static_key_batch_begin(void) {} +static inline void static_key_batch_end(void) {} + #define static_key_enable_cpuslocked(k) static_key_enable((k)) #define static_key_disable_cpuslocked(k) static_key_disable((k)) @@ -335,6 +352,7 @@ static inline void static_key_disable(struct static_key *key) #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 STATIC_KEY_INIT STATIC_KEY_INIT_FALSE #define jump_label_enabled static_key_enabled @@ -521,6 +539,10 @@ 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_queued(x) static_key_enable_queued(&(x)->key) +#define static_branch_disable_queued(x) static_key_disable_queued(&(x)->key) +#define static_branch_batch_begin() static_key_batch_begin() +#define static_branch_batch_end() static_key_batch_end() #define static_branch_enable_cpuslocked(x) static_key_enable_cpuslocked(&(x)->key) #define static_branch_disable_cpuslocked(x) static_key_disable_cpuslocked(&(x)->key) diff --git a/kernel/jump_label.c b/kernel/jump_label.c index d20b5b3ffbd3..f068e6484a29 100644 --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -91,6 +91,9 @@ jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop) } static void jump_label_update(struct static_key *key); +#ifdef HAVE_JUMP_LABEL_BATCH +static void jump_label_update_queued(struct static_key *key); +#endif /* * There are similar definitions for the !CONFIG_JUMP_LABEL case in jump_label.h. @@ -101,14 +104,22 @@ static void jump_label_update(struct static_key *key); * 'static_key_disable()', which require bug.h. This should allow jump_label.h * to be included from most/all places for CONFIG_JUMP_LABEL. */ +#define STATIC_KEY_TRANS_ENABLE (-1) +#define STATIC_KEY_TRANS_DISABLE (-2) + int static_key_count(struct static_key *key) { /* - * -1 means the first static_key_slow_inc() is in progress. - * static_key_enabled() must return true, so return 1 here. + * STATIC_KEY_TRANS_ENABLE (-1): first static_key_slow_inc() or + * static_key_enable_queued() in progress; static_key_enabled() must + * return true, so return 1. + * STATIC_KEY_TRANS_DISABLE (-2): static_key_disable_queued() in + * progress; static_key_enabled() must return false, so return 0. */ int n = atomic_read(&key->enabled); + if (n == STATIC_KEY_TRANS_DISABLE) + return 0; return n >= 0 ? n : 1; } EXPORT_SYMBOL_GPL(static_key_count); @@ -130,9 +141,10 @@ bool static_key_fast_inc_not_disabled(struct static_key *key) STATIC_KEY_CHECK_USE(key); /* - * Negative key->enabled has a special meaning: it sends - * static_key_slow_inc/dec() down the slow path, and it is non-zero - * so it counts as "enabled" in jump_label_update(). + * Negative key->enabled marks an in-flight transition and sends + * static_key_slow_inc/dec() down the slow path: + * STATIC_KEY_TRANS_ENABLE (-1) counts as enabled in static_key_count(), + * while STATIC_KEY_TRANS_DISABLE (-2) counts as disabled. * * The INT_MAX overflow condition is either used by the networking * code to reset or detected in the slow path of @@ -250,10 +262,173 @@ void static_key_disable(struct static_key *key) } EXPORT_SYMBOL_GPL(static_key_disable); +#ifdef HAVE_JUMP_LABEL_BATCH +/* + * On arches whose arch_jump_label_transform_queue() always returns true + * (e.g. arm64/riscv/s390 nosync pokes), cap how many keys stay in + * transitional states before __static_key_flush_queued() finalizes them. + */ +#define MAX_QUEUED_KEYS 256 +static struct static_key *jump_label_queued_keys[MAX_QUEUED_KEYS]; +static int jump_label_num_queued_keys; + +static void jump_label_finalize_queued_keys(void) +{ + int i, val; + + lockdep_assert_held(&jump_label_mutex); + + for (i = 0; i < jump_label_num_queued_keys; i++) { + struct static_key *k = jump_label_queued_keys[i]; + + if (WARN_ON_ONCE(!k)) + continue; + + val = atomic_read(&k->enabled); + switch (val) { + case STATIC_KEY_TRANS_ENABLE: + atomic_set_release(&k->enabled, 1); + break; + case STATIC_KEY_TRANS_DISABLE: + atomic_set_release(&k->enabled, 0); + break; + default: + WARN_ONCE(1, "jump_label: unexpected state %d for queued key %pS\n", + val, k); + break; + } + } + jump_label_num_queued_keys = 0; +} + +static void __static_key_flush_queued(void) +{ + arch_jump_label_transform_apply(); + jump_label_finalize_queued_keys(); +} + +static void jump_label_prepare_queued_slot(void) +{ + if (jump_label_num_queued_keys >= MAX_QUEUED_KEYS) + __static_key_flush_queued(); +} + +static void jump_label_record_queued_key(struct static_key *key) +{ + jump_label_queued_keys[jump_label_num_queued_keys++] = key; +} +#endif + +static void __static_key_set_queued(struct static_key *key, bool enable) +{ + int from = enable ? 0 : 1; + int to = enable ? 1 : 0; + int trans = enable ? STATIC_KEY_TRANS_ENABLE : STATIC_KEY_TRANS_DISABLE; +#ifdef HAVE_JUMP_LABEL_BATCH + int opp_trans = enable ? STATIC_KEY_TRANS_DISABLE + : STATIC_KEY_TRANS_ENABLE; +#endif + int val; + + STATIC_KEY_CHECK_USE(key); + lockdep_assert_cpus_held(); + lockdep_assert_held(&jump_label_mutex); + + val = atomic_read(&key->enabled); +#ifdef HAVE_JUMP_LABEL_BATCH + if (val == opp_trans) { + /* + * Reversing a key already queued in this batch: flush the + * pending batch first so live text matches __jump_label_patch() + * expectations and key->enabled settles to @from. + */ + __static_key_flush_queued(); + val = atomic_read(&key->enabled); + } +#endif + if (val != from) { + /* + * Already in target state or in-flight to it; warn if + * refcounted (> 1) or unexpected negative state. + */ + WARN_ON_ONCE(val != to && val != trans); + return; + } + +#ifdef HAVE_JUMP_LABEL_BATCH + jump_label_prepare_queued_slot(); + if (atomic_cmpxchg(&key->enabled, from, trans) == from) { + jump_label_update_queued(key); + jump_label_record_queued_key(key); + } +#else + if (atomic_cmpxchg(&key->enabled, from, trans) == from) { + jump_label_update(key); + atomic_set_release(&key->enabled, to); + } +#endif +} + +/** + * static_key_enable_queued - queue enabling a boolean static key + * @key: static key to enable + * + * Queues a 0 -> 1 branch transition inside an active static_key_batch + * critical section (see static_key_batch_begin() and guard(static_key_batch)). + * On SMP, text patching and final key->enabled publication are deferred until + * the batch flushes. + */ +void static_key_enable_queued(struct static_key *key) +{ + __static_key_set_queued(key, true); +} + +/** + * static_key_disable_queued - queue disabling a boolean static key + * @key: static key to disable + * + * Queues a 1 -> 0 branch transition inside an active static_key_batch + * critical section (see static_key_batch_begin() and guard(static_key_batch)). + * On SMP, text patching and final key->enabled publication are deferred until + * the batch flushes. + */ +void static_key_disable_queued(struct static_key *key) +{ + __static_key_set_queued(key, false); +} + +/** + * static_key_batch_begin - enter a batched static key update section + * + * Acquires cpus_read_lock() and jump_label_lock() across a batch of + * static_key_enable_queued() / static_key_disable_queued() calls. Prefer + * scoped_guard(static_key_batch) or guard(static_key_batch)(). + */ +void static_key_batch_begin(void) +{ + cpus_read_lock(); + jump_label_lock(); +} + +/** + * static_key_batch_end - flush queued static key updates and release locks + * + * Applies any pending batched jump-label text pokes, finalizes transitional + * key->enabled states, and releases jump_label_lock() and cpus_read_lock(). + */ +void static_key_batch_end(void) +{ +#ifdef HAVE_JUMP_LABEL_BATCH + if (jump_label_num_queued_keys) + __static_key_flush_queued(); +#endif + jump_label_unlock(); + cpus_read_unlock(); +} + static bool static_key_dec_not_one(struct static_key *key) { int v; - /* * Go into the slow path if key::enabled is less than or equal than * one. One is valid to shut down the key, anything less than one @@ -268,10 +443,9 @@ static bool static_key_dec_not_one(struct static_key *key) v = atomic_read(&key->enabled); do { /* - * Warn about the '-1' case though; since that means a - * decrement is concurrent with a first (0->1) increment. IOW - * people are trying to disable something that wasn't yet fully - * enabled. This suggests an ordering problem on the user side. + * Warn about negative transitional values (-1/-2); a decrement + * is racing an in-flight 0->1 increment or a queued boolean + * transition, which indicates a caller ordering bug. */ WARN_ON_ONCE(v < 0); @@ -291,19 +465,22 @@ static bool static_key_dec_not_one(struct static_key *key) static void __static_key_slow_dec_cpuslocked(struct static_key *key) { - lockdep_assert_cpus_held(); int val; + lockdep_assert_cpus_held(); + if (static_key_dec_not_one(key)) return; 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(). + * It should be impossible to observe transitional states with + * jump_label_mutex held; see static_key_slow_inc_cpuslocked() and + * static_key_batch_begin(). */ - if (WARN_ON_ONCE(val == -1)) + if (WARN_ON_ONCE(val == STATIC_KEY_TRANS_ENABLE || + val == STATIC_KEY_TRANS_DISABLE)) return; /* * Cannot already be 0, something went sideways. @@ -500,9 +677,10 @@ static void __jump_label_update(struct static_key *key, #ifdef HAVE_JUMP_LABEL_BATCH if (!arch_jump_label_transform_queue(entry, jump_label_type(entry))) { /* - * Queue is full: Apply the current queue and try again. + * Queue is full: Apply the current queue, finalize + * previously completed keys, and retry this entry. */ - arch_jump_label_transform_apply(); + __static_key_flush_queued(); BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry))); } #else @@ -907,6 +1085,13 @@ static void jump_label_update(struct static_key *key) jump_label_update_key(key, true); } +#ifdef HAVE_JUMP_LABEL_BATCH +static void jump_label_update_queued(struct static_key *key) +{ + jump_label_update_key(key, false); +} +#endif + #ifdef CONFIG_STATIC_KEYS_SELFTEST static DEFINE_STATIC_KEY_TRUE(sk_true); static DEFINE_STATIC_KEY_FALSE(sk_false); @@ -937,6 +1122,29 @@ static __init int jump_label_test(void) static_branch_enable(&sk_true); static_branch_disable(&sk_false); + + scoped_guard(static_key_batch) { + static_branch_disable_queued(&sk_true); + static_branch_enable_queued(&sk_false); + } + + WARN_ON(static_key_enabled(&sk_true.key) == true); + WARN_ON(static_key_enabled(&sk_false.key) == false); + WARN_ON(static_branch_likely(&sk_true)); + WARN_ON(static_branch_unlikely(&sk_true)); + WARN_ON(!static_branch_likely(&sk_false)); + WARN_ON(!static_branch_unlikely(&sk_false)); + + /* Same-batch reversal: flip back and forth in one batch */ + scoped_guard(static_key_batch) { + static_branch_enable_queued(&sk_true); + static_branch_disable_queued(&sk_true); + static_branch_enable_queued(&sk_true); + + static_branch_disable_queued(&sk_false); + static_branch_enable_queued(&sk_false); + static_branch_disable_queued(&sk_false); + } } return 0; -- 2.56.1