[PATCH v3 3/5] jump_label: Add static_key_batch API
From: Jim Cromie via B4 Relay
Date: Fri Dec 04 2026 - 13:28:32 EST
From: Jim Cromie <jim.cromie@xxxxxxxxx>
Toggling many static keys in a loop -- such as dynamic-debug enabling
thousands of pr_debug() sites -- currently runs
arch_jump_label_transform_apply() and cross-CPU sync IPIs once per key.
Instead of adding a parallel queued static-key API, introduce
static_key_batch and static_key_batch_cpuslocked guards that hold
cpus_read_lock() and jump_label_mutex across a block, record
batch_owner = current, and defer __jump_label_finish() until the
outermost guard exits. Routing static_key_{enable,disable,slow_inc,
slow_dec}() through batch-aware lock guards lets callers coalesce pokes
using the existing API, and collapses the duplicated _cpuslocked()
implementations into inline static_key_batch_cpuslocked wrappers.
Existing key->enabled protocol (unchanged for single-key callers):
Lockless paths assume key->enabled >= 1 means text is already patched:
- static_key_fast_inc_not_disabled(): v >= 1 -> CAS v + 1, return
- static_key_dec_not_one(): v > 1 -> CAS v - 1, return
- static_key_enable() entry: v > 0 -> early return
- static_key_disable() entry: v == 0 -> early return
- static_key_count() / _enabled(): treats -1 as 1 (also read by
jump_label_type() during patching to choose JMP vs NOP)
Under jump_label_mutex, transitions set direction before patching:
- Enable (0 -> -1 -> [patch] -> 1): -1 selects JMP for patching while
keeping concurrent lockless callers on jump_label_mutex until
patching finishes and releases 1.
- Disable (1 -> 0 -> [patch]): writes 0 up front because 0 already
selects NOP and keeps concurrent inc/enable callers on the mutex.
Changes needed to extend that protocol across a multi-key batch:
1. Deferred completion and -1 handling: Queue each updated key in
jump_label_queue[256] (via jump_label_queue_key(), including 0-site
keys) and defer arch_jump_label_transform_apply() plus the -1 -> 1
release to __jump_label_finish(). Because enabled keys stay -1 until
flush, static_key_disable() must fall through on -1 onto
jump_label_mutex instead of warning on != 1 and returning early, and
static_key_dec_not_one() suppresses its -1 warning for batch_owner.
2. Same-batch key revisits: Revisiting an already-queued key (e.g.
"+p;-p", or inc followed by dec/inc) must segment the batch in
jump_label.c before arch queueing, because x86 validates against live
text and -1 keys are not 1 yet. jump_label_flush_key() flushes first
whenever the target key is already in jump_label_queue[].
3. Nesting and _cpuslocked() unification: Track batch_depth so only the
outermost guard acquires and releases cpus_read_lock(),
jump_label_mutex, and batch_owner and runs __jump_label_finish().
Checking batch_owner == current before cpus_read_lock() avoids
recursive cpu_hotplug_lock lockdep warnings and self-deadlock when
_cpuslocked() wrappers or batch guards nest.
4. Module loading and empty-batch elision: Wrap
jump_label_module_notify() in static_key_batch so module-load pokes
flush and coalesce, while jump_label_pokes_queued skips text_mutex
and sync IPIs on MODULE_STATE_GOING, 0-site keys, or empty batches.
Extend jump_label_test() to cover batching, reversals, and nesting.
Suggested-by: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Signed-off-by: Jim Cromie <jim.cromie@xxxxxxxxx>
---
include/linux/jump_label.h | 48 +++++--
kernel/jump_label.c | 316 ++++++++++++++++++++++++++++++++-------------
2 files changed, 267 insertions(+), 97 deletions(-)
diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
index c95350a10987..e7a8a7d4450c 100644
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -222,15 +222,17 @@ extern int jump_label_text_reserved(void *start, void *end);
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(struct static_key *key)
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(struct static_key *key)
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,38 @@ 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_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 bool static_key_slow_inc_cpuslocked(struct static_key *key)
+{
+ guard(static_key_batch_cpuslocked)();
+ return 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);
+}
+
/* -------------------------------------------------------------------------- */
/*
diff --git a/kernel/jump_label.c b/kernel/jump_label.c
index ab6b774bcfd7..fe70b251ba77 100644
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -113,6 +113,111 @@ int static_key_count(struct static_key *key)
}
EXPORT_SYMBOL_GPL(static_key_count);
+static void __jump_label_finish(void);
+static void jump_label_flush_key(struct static_key *key);
+static void jump_label_queue_key(struct static_key *key);
+
+static struct task_struct *batch_owner;
+static unsigned int batch_depth;
+
+void static_key_batch_begin_cpuslocked(void)
+{
+ lockdep_assert_cpus_held();
+ if (READ_ONCE(batch_owner) == current) {
+ lockdep_assert_held(&jump_label_mutex);
+ batch_depth++;
+ return;
+ }
+
+ jump_label_lock();
+ batch_depth = 1;
+ WRITE_ONCE(batch_owner, current);
+}
+
+void static_key_batch_begin(void)
+{
+ if (READ_ONCE(batch_owner) == current) {
+ lockdep_assert_cpus_held();
+ lockdep_assert_held(&jump_label_mutex);
+ batch_depth++;
+ return;
+ }
+
+ cpus_read_lock();
+ static_key_batch_begin_cpuslocked();
+}
+
+static bool __static_key_batch_end_cpuslocked(void)
+{
+ lockdep_assert_cpus_held();
+ lockdep_assert_held(&jump_label_mutex);
+ if (WARN_ON_ONCE(READ_ONCE(batch_owner) != current))
+ return false;
+ if (--batch_depth)
+ return false;
+
+ __jump_label_finish();
+ WRITE_ONCE(batch_owner, NULL);
+ jump_label_unlock();
+ return true;
+}
+
+void static_key_batch_end_cpuslocked(void)
+{
+ __static_key_batch_end_cpuslocked();
+}
+
+void static_key_batch_end(void)
+{
+ if (__static_key_batch_end_cpuslocked())
+ cpus_read_unlock();
+}
+
+static void jump_label_batch_lock(void)
+{
+ if (READ_ONCE(batch_owner) == current) {
+ lockdep_assert_held(&jump_label_mutex);
+ return;
+ }
+
+ jump_label_lock();
+}
+
+static void jump_label_batch_unlock(void)
+{
+ if (READ_ONCE(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 (READ_ONCE(batch_owner) == current) {
+ lockdep_assert_cpus_held();
+ return;
+ }
+
+ cpus_read_lock();
+}
+
+static void cpus_read_batch_unlock(void)
+{
+ if (READ_ONCE(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 +241,7 @@ bool static_key_fast_inc_not_disabled(struct static_key *key)
*
* 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 +253,8 @@ bool static_key_fast_inc_not_disabled(struct static_key *key)
}
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,16 +265,12 @@ bool static_key_slow_inc_cpuslocked(struct static_key *key)
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)();
+ jump_label_flush_key(key);
/* Try to mark it as 'enabling in progress. */
if (!atomic_cmpxchg(&key->enabled, 0, -1)) {
jump_label_update(key);
- /*
- * Ensure that when static_key_fast_inc_not_disabled() or
- * static_key_dec_not_one() observe the positive value,
- * they must also observe all the text changes.
- */
- atomic_set_release(&key->enabled, 1);
} else {
/*
* While holding the mutex this should never observe
@@ -182,71 +281,45 @@ bool static_key_slow_inc_cpuslocked(struct static_key *key)
}
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)
{
+ int val;
+
STATIC_KEY_CHECK_USE(key);
- lockdep_assert_cpus_held();
- if (atomic_read(&key->enabled) > 0) {
- WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+ val = atomic_read(&key->enabled);
+ if (val > 0) {
+ WARN_ON_ONCE(val != 1);
return;
}
- jump_label_lock();
+ guard(cpus_read_batch_lock)();
+ guard(jump_label_batch_lock)();
if (atomic_read(&key->enabled) == 0) {
+ jump_label_flush_key(key);
atomic_set(&key->enabled, -1);
jump_label_update(key);
- /*
- * See static_key_slow_inc().
- */
- 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)
{
+ int val;
+
STATIC_KEY_CHECK_USE(key);
- lockdep_assert_cpus_held();
- if (atomic_read(&key->enabled) != 1) {
- WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
+ val = atomic_read(&key->enabled);
+ if (val == 0 || WARN_ON_ONCE(val > 1))
return;
- }
- jump_label_lock();
+ guard(cpus_read_batch_lock)();
+ guard(jump_label_batch_lock)();
+ jump_label_flush_key(key);
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 +333,7 @@ static bool static_key_dec_not_one(struct static_key *key)
* 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.
@@ -273,7 +346,7 @@ static bool static_key_dec_not_one(struct static_key *key)
* people are trying to disable something that wasn't yet fully
* enabled. This suggests an ordering problem on the user side.
*/
- WARN_ON_ONCE(v < 0);
+ WARN_ON_ONCE(v < 0 && READ_ONCE(batch_owner) != current);
/*
* Warn about underflow, and lie about success in an attempt to
@@ -289,19 +362,22 @@ static bool static_key_dec_not_one(struct static_key *key)
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(mutex)(&jump_label_mutex);
+ guard(cpus_read_batch_lock)();
+ guard(jump_label_batch_lock)();
+ jump_label_flush_key(key);
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 +390,16 @@ static void __static_key_slow_dec_cpuslocked(struct static_key *key)
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 +546,15 @@ static bool jump_label_can_update(struct jump_entry *entry, bool init)
}
#ifndef HAVE_JUMP_LABEL_BATCH
+static void __jump_label_finish(void) { }
+static void jump_label_flush_key(struct static_key *key) { }
+
+static void jump_label_queue_key(struct static_key *key)
+{
+ if (atomic_read(&key->enabled) == -1)
+ atomic_set_release(&key->enabled, 1);
+}
+
static void __jump_label_update(struct static_key *key,
struct jump_entry *entry,
struct jump_entry *stop,
@@ -500,6 +566,47 @@ static void __jump_label_update(struct static_key *key,
}
}
#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 bool jump_label_pokes_queued;
+
+static void __jump_label_finish(void)
+{
+ if (jump_label_pokes_queued) {
+ arch_jump_label_transform_apply();
+ jump_label_pokes_queued = false;
+ }
+ 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_flush_key(struct static_key *key)
+{
+ for (unsigned int i = 0; i < jump_label_nr_queued; i++) {
+ if (jump_label_queue[i] == key) {
+ __jump_label_finish();
+ return;
+ }
+ }
+}
+
+static void jump_label_queue_key(struct static_key *key)
+{
+ if (jump_label_nr_queued >= JUMP_LABEL_BATCH_SIZE)
+ __jump_label_finish();
+ jump_label_queue[jump_label_nr_queued++] = key;
+}
+
static void __jump_label_update(struct static_key *key,
struct jump_entry *entry,
struct jump_entry *stop,
@@ -514,11 +621,12 @@ static void __jump_label_update(struct static_key *key,
/*
* Queue is full: Apply the current queue and try again.
*/
- arch_jump_label_transform_apply();
+ jump_label_pokes_queued = true;
+ __jump_label_finish();
BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry)));
}
+ jump_label_pokes_queued = true;
}
- arch_jump_label_transform_apply();
}
#endif
@@ -819,8 +927,7 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val,
struct module *mod = data;
int ret = 0;
- cpus_read_lock();
- jump_label_lock();
+ guard(static_key_batch)();
switch (val) {
case MODULE_STATE_COMING:
@@ -835,9 +942,6 @@ jump_label_module_notify(struct notifier_block *self, unsigned long val,
break;
}
- jump_label_unlock();
- cpus_read_unlock();
-
return notifier_from_errno(ret);
}
@@ -892,6 +996,7 @@ static void jump_label_update(struct static_key *key)
if (static_key_linked(key)) {
__jump_label_mod_update(key);
+ jump_label_queue_key(key);
return;
}
@@ -907,6 +1012,7 @@ static void jump_label_update(struct static_key *key)
/* if there are no users, entry can be NULL */
if (entry)
__jump_label_update(key, entry, stop, init);
+ jump_label_queue_key(key);
}
#ifdef CONFIG_STATIC_KEYS_SELFTEST
@@ -939,6 +1045,40 @@ 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(&sk_true);
+ static_branch_enable(&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 reversals, nesting, and inc/dec */
+ scoped_guard(static_key_batch) {
+ static_branch_enable_cpuslocked(&sk_true);
+ scoped_guard(static_key_batch) {
+ static_branch_disable(&sk_true);
+ static_branch_enable(&sk_true);
+ }
+
+ static_branch_disable(&sk_false);
+ static_branch_inc(&sk_false);
+ static_branch_inc(&sk_false);
+ static_branch_dec(&sk_false);
+ static_branch_dec(&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));
}
return 0;
--
2.56.1