* [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates
@ 2026-12-04 18:27 Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 1/5] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-12-04 18:27 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Josh Poimboeuf, Jason Baron,
Alice Ryhl, Steven Rostedt, Ard Biesheuvel, Andrew Morton
Cc: linux-kernel, Jim Cromie
Toggling many boolean static keys at once -- such as enabling or
disabling pr_debug() sites across vmlinux and loaded modules --
currently patches one static key at a time. Each toggle sends a full
set of cross-CPU text-poke IPIs (~39.7k CAL IPIs to flip 1,885 active
builtin keys across 8 CPUs on x86). Even if kernel/jump_label.c defers
arch_jump_label_transform_apply() across a batch of static-keys, x86's
smp_text_poke_batch_add() flushes whenever a poke address arrives below
the tail entry (~2 entries per batch in dyndbg descriptor order) and
hides its 256-entry flush boundaries from jump_label.c.
This series updates all three layers so a static_key_batch guard
transparently batches static_key_{enable,disable,slow_inc,slow_dec}()
calls while handling out-of-order poke addresses and same-batch state
reversals cleanly:
1. x86/alternatives (1/5): Keep text_poke_array.vec[] sorted on insert
via binary search and memmove() so unordered multi-key callers fill
the batch while pre-sorted ftrace pokes stay on the one-comparison
tail-append fastpath. Reject already-queued addresses so the pending
batch flushes first and the next batch starts with the new update
(e.g. a "+p;-p" toggle by dyndbg, or a re-target by ftrace).
2. x86/jump_label (2/5): Add smp_text_poke_batch_try_add() and return
its status from arch_jump_label_transform_queue() so jump_label.c
flushes, finalizes completed keys, and starts a new batch in
lockstep with text_poke_array.
3. jump_label (3/5): Add static_key_batch_begin/end() and
DEFINE_LOCK_GUARD_0(static_key_batch, ...). Track the active batch
task via batch_owner so static_key_{enable,disable,slow_inc,
slow_dec}() calls coalesce their pokes, defer publishing 1 on newly
enabled keys until __jump_label_finish(), and flush early if a batch
revisits an already-queued key.
4. dyndbg (4/5..5/5): Hoist ddebug_lock and
scoped_guard(static_key_batch) in ddebug_exec_queries() to coalesce
static branch updates across sub-queries, and assemble changed
classmap bitmap bits into one multi-query buffer.
Measured on an 8-vCPU x86_64 guest (v7.4-rc5, 1,997 builtin pr_debug
descriptors / 1,885 non-init keys / 1,964 jump sites,
CONFIG_PROVE_LOCKING=y):
Workload master v3 (5 patches) Speedup
-------------------------------------------------------------------
echo "+p" > control 39,712 282 CAL 141x
echo "-p" > control 39,674 255 CAL 156x
"+p ; -p" (1x) 79,256 424 CAL 187x
"+p ; -p" (4x) 316,773 756 CAL 419x
Bare-metal desktops (vmlinux + loaded modules incl. amdgpu):
Host / CPUs / Sites "module amdgpu +p" "+p ; -p" (4x)
----------------------------------------------------------------
Ryzen 9 6900HX, 16c, 8,484 615 CAL 12,524 CAL
Ryzen 9 3900X, 24c, 8,197 909 CAL 18,243 CAL
Both match the theoretical floor ceil(N / 256) * 3 * (CPUs - 1) per
toggle within ~2% (~240-250x fewer CAL IPIs than unbatched master).
Two follow-up series have been prototyped on top of this work:
1. Pattern reuse (static_call_batch and tracepoint_batch): applies the
same sorted text_poke_array (1/5) and batch_owner design to inline
static calls and tracepoints, cutting 8-vCPU CAL IPIs on
"echo 1/0 > /sys/kernel/tracing/events/enable" (1,553 events) from
~68.8k to ~850-890 (~77-81x fewer, passing 25/25 ftracetest tests).
2. Direct API reuse: wraps existing multi-key callers in
static_key_batch / static_key_batch_cpuslocked (netfilter hook
tables, sched_ext, sched/topology, mm/vmscan, net rps/xps, resctrl).
Verification & Tooling Disclosure:
1. Tested across 10 Kconfig build combinations in 8-vCPU virtme-ng
(CONFIG_PROVE_LOCKING=y, CONFIG_KASAN=y, !CONFIG_MODULES,
CONFIG_STATIC_KEYS_SELFTEST=y, CONFIG_TEST_STATIC_KEYS=m,
CONFIG_TEST_DYNAMIC_DEBUG=m/y) and on 16-CPU and 24-CPU AMD Ryzen
hosts; 0 lockdep, KASAN, or dmesg warnings.
2. Ran tools/testing/selftests/ftrace/ftracetest test.d/ftrace/ in
4-vCPU virtme-ng to exercise ftrace_replace_code() on the shared
smp_text_poke_batch_add() path: 25 passed, 0 failed, 0 unsupported
(function and function-graph filters, profiling, event/traceon-off
triggers, module tracing, CPU hotplug, and trace instances).
3. Peter Zijlstra suggested the batch_owner / static_key_batch guard
design in 3/5. AI coding assistants (Gemini CLI / Antigravity)
assisted with the commit split, the binary-search insertion sort in
1/5, queue-limit sync in 2/5, and virtme-ng verification; all code
and commit messages were reviewed and tested by the author.
---
Changes in v3:
1. Address Steven Rostedt's review on 1/5 (keep tpl assignment after
declarations and drop untouched signature line breaks) and rewrite
series changelogs in plain prose with 1.-indexed spaced lists and no
"What's Unchanged" sections.
2. Rework jump_label batching per Peter Zijlstra: drop the separate
@apply plumbing patch (v2 3/7), static_key_{enable,disable}_queued(),
and STATIC_KEY_TRANS_DISABLE (-2). Track batch_owner (with nesting
for _cpuslocked() callers) under cpus_read_batch_lock and
jump_label_batch_lock guards so static_key_{enable,disable,
slow_inc,slow_dec}() calls coalesce inside guard(static_key_batch)(),
flushing when revisiting an already-queued key or loading a module.
3. Serialize static_key_disable() on jump_label_mutex when observing -1
instead of firing a false WARN_ON_ONCE() and returning early
(Sashiko review on v2 4/7).
4. Fold v2 5/7 and 7/7 into one dyndbg patch (4/5) that hoists
ddebug_lock and scoped_guard(static_key_batch) directly to
ddebug_exec_queries(), avoiding temporary batching churn in
ddebug_change().
5. Bail out on negative returns from ddebug_apply_class_bitmap() in
param_set_dyndbg_classnames() and param_set_dyndbg_classes() without
updating *dcp->bits or *dcp->lvl (Sashiko review on v2 6/7).
- Link to v2: https://lore.kernel.org/r/20261129-jump-batch-261128-v2-0-b1829d2429f0@gmail.com
Changes in v2:
1. CC x86, jump_label, ftrace, and dyndbg maintainers across the full
series.
2. Split the combined v1 patch into single-purpose commits ordered
x86 -> jump_label -> dyndbg, with mechanical @apply plumbing
isolated in 3/7.
3. Drop all EXPORT_SYMBOL_GPL() exports on the queued static-key API
(core-only).
4. Drop pr_debug() instrumentation from arch/x86/kernel/alternative.c.
5. Replace deferred sort() with binary-search insertion sort in
smp_text_poke_batch_add() so vec[] stays sorted and duplicate-free
at all times without touching smp_text_poke_batch_finish().
6. Propagate queue-full return from x86's
arch_jump_label_transform_queue() so multi-site keys do not
desynchronize the 256-site text_poke_array from the 256-key
jump_label queue.
- Link to v1: https://lore.kernel.org/lkml/20260306015022.1940986-1-jim.cromie@gmail.com/
---
Jim Cromie (5):
x86/alternatives: Keep text-poke batch sorted on insert
x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit
jump_label: Add static_key_batch API
dyndbg: Batch static branch updates across queries
dyndbg: Batch classmap param bit-changes into multi-query
arch/x86/include/asm/text-patching.h | 1 +
arch/x86/kernel/alternative.c | 79 +++++++--
arch/x86/kernel/jump_label.c | 6 +-
include/linux/jump_label.h | 48 +++++-
kernel/jump_label.c | 316 +++++++++++++++++++++++++----------
lib/dynamic_debug.c | 105 ++++++++----
6 files changed, 405 insertions(+), 150 deletions(-)
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261128-jump-batch-261128-1d89de6673f4
Best regards,
--
Jim Cromie <jim.cromie@gmail.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 1/5] x86/alternatives: Keep text-poke batch sorted on insert
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
@ 2026-12-04 18:27 ` Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 2/5] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit Jim Cromie via B4 Relay
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-12-04 18:27 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Josh Poimboeuf, Jason Baron,
Alice Ryhl, Steven Rostedt, Ard Biesheuvel, Andrew Morton
Cc: linux-kernel, Jim Cromie
From: Jim Cromie <jim.cromie@gmail.com>
smp_text_poke_int3_handler() runs __inline_bsearch() over
text_poke_array.vec[] to resolve and emulate in-flight poke sites in
INT3 trap context, requiring vec[] to stay strictly sorted by poke
address. Currently smp_text_poke_batch_add() enforces ordering by
flushing the batch whenever a new address is lower than the tail entry.
ftrace submits its poke addresses in ascending order, so the queue fills
to capacity before flushing. But batching across multiple static keys
(such as dyndbg walking descriptors) submits poke addresses out of code
order, triggering a flush and IPIs after ~2 entries on average.
Because text_poke_array.vec[] is private under text_mutex until
smp_text_poke_batch_finish() publishes text_poke_array_refs,
out-of-order entries can be inserted into sorted position instead of
flushing early.
Inserting in sorted order also allows detecting duplicate addresses in
a submitted stream (which binary search also forbids), so when an
address is already queued we can flush the current batch to apply that
pending state first and start the next batch with the new update (e.g.
a "+p;-p" toggle by dyndbg, or a re-target by ftrace) without warning.
1. Test new pokes for a tail append first in text_poke_find_slot(),
which replaces text_poke_addr_ordered(): when vec[] is empty or addr
exceeds the tail entry, return nr_entries in one comparison (the
ftrace fast path). Requiring addr to exceed the tail also catches
adjacent duplicate addresses that the old check let through.
2. Otherwise, use binary search on vec[] to find the sorted insertion
slot for addr; if addr is already queued, return false so
smp_text_poke_batch_add() flushes the pending batch and starts the
next batch with the new poke.
3. Pass the slot to __smp_text_poke_batch_add(), using memmove() to
shift trailing entries up when inserting ahead of the tail (skipped
on tail appends; ~1 KB moved on average within the 4 KB vec[] page).
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
arch/x86/kernel/alternative.c | 57 +++++++++++++++++++++++++++++++------------
1 file changed, 41 insertions(+), 16 deletions(-)
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index efbc11f8c336..c837678f379f 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -2955,13 +2955,17 @@ void smp_text_poke_batch_finish(void)
text_poke_array.nr_entries = 0;
}
-static void __smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate)
+static void __smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate, int pos)
{
struct smp_text_poke_loc *tpl;
struct insn insn;
int ret, i = 0;
- tpl = &text_poke_array.vec[text_poke_array.nr_entries++];
+ tpl = &text_poke_array.vec[pos];
+ if (pos < text_poke_array.nr_entries)
+ memmove(tpl + 1, tpl,
+ (text_poke_array.nr_entries - pos) * sizeof(*tpl));
+ text_poke_array.nr_entries++;
if (len == 6)
i = 1;
@@ -3032,26 +3036,42 @@ static void __smp_text_poke_batch_add(void *addr, const void *opcode, size_t len
}
}
-/*
- * We hard rely on the text_poke_array.vec being ordered; ensure this is so by flushing
- * early if needed.
+/**
+ * text_poke_find_slot() -- find sorted insertion index for @addr in vec[]
+ * @addr: address to patch
+ * @pos: output insertion index in text_poke_array.vec[]
+ *
+ * smp_text_poke_int3_handler() binary-searches text_poke_array.vec[], so
+ * entries must stay strictly sorted with no duplicates. Append at nr_entries
+ * when @addr exceeds the tail entry; otherwise binary-search vec[].
+ *
+ * Return: true with *@pos set if @addr is not yet queued, or false if @addr
+ * is already in vec[] (caller must finish() the current batch first).
*/
-static bool text_poke_addr_ordered(void *addr)
+static bool text_poke_find_slot(void *addr, int *pos)
{
+ int nr = text_poke_array.nr_entries;
+ int lo = 0, hi = nr;
+
WARN_ON_ONCE(!addr);
- if (!text_poke_array.nr_entries)
+ *pos = nr;
+ if (!nr || addr > text_poke_addr(&text_poke_array.vec[nr - 1]))
return true;
- /*
- * If the last current entry's address is higher than the
- * new entry's address we'd like to add, then ordering
- * is violated and we must first flush all pending patching
- * requests:
- */
- if (text_poke_addr(text_poke_array.vec + text_poke_array.nr_entries-1) > addr)
+ while (lo < hi) {
+ int mid = (lo + hi) >> 1;
+
+ if (addr > text_poke_addr(&text_poke_array.vec[mid]))
+ lo = mid + 1;
+ else
+ hi = mid;
+ }
+
+ if (addr == text_poke_addr(&text_poke_array.vec[lo]))
return false;
+ *pos = lo;
return true;
}
@@ -3070,9 +3090,14 @@ static bool text_poke_addr_ordered(void *addr)
*/
void __ref smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate)
{
- if (text_poke_array.nr_entries == TEXT_POKE_ARRAY_MAX || !text_poke_addr_ordered(addr))
+ int pos = 0;
+
+ if (text_poke_array.nr_entries == TEXT_POKE_ARRAY_MAX ||
+ !text_poke_find_slot(addr, &pos)) {
smp_text_poke_batch_finish();
- __smp_text_poke_batch_add(addr, opcode, len, emulate);
+ pos = 0;
+ }
+ __smp_text_poke_batch_add(addr, opcode, len, emulate, pos);
}
/**
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 2/5] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 1/5] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
@ 2026-12-04 18:27 ` Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 3/5] jump_label: Add static_key_batch API Jim Cromie via B4 Relay
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-12-04 18:27 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Josh Poimboeuf, Jason Baron,
Alice Ryhl, Steven Rostedt, Ard Biesheuvel, Andrew Morton
Cc: linux-kernel, Jim Cromie
From: Jim Cromie <jim.cromie@gmail.com>
__jump_label_update() expects arch_jump_label_transform_queue() to
return false when the current architecture batch cannot accept another
poke, so the caller can apply the pending batch and queue that entry at
the start of a fresh batch. On x86, however,
arch_jump_label_transform_queue() calls smp_text_poke_batch_add(),
which silently flushes text_poke_array when it fills
(TEXT_POKE_ARRAY_MAX = 256) or hits an already-queued address, and
unconditionally returns true.
Hiding those flush boundaries is harmless when updating one static key
at a time, where key->enabled stays fixed across internal flushes.
Multi-key static_key_batch updates, however, defer publishing
key->enabled = 1 until the batch flushes: because multi-site (e.g.
inlined) keys fill the 256-entry text_poke_array ahead of the 256-key
queue, jump_label.c needs the false return so it can flush the pending
batch, publish completed key states, and start the next batch in
lockstep without extra runt flushes.
1. Extract smp_text_poke_batch_try_add() in alternative.c, returning
false without flushing when text_poke_array is full or addr is
already queued, and keep smp_text_poke_batch_add() as an
auto-flushing wrapper for callers such as ftrace_replace_code().
2. Call smp_text_poke_batch_try_add() in x86's
arch_jump_label_transform_queue() and return its result so
__jump_label_update() flushes and starts a fresh batch on boundary
hits.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
arch/x86/include/asm/text-patching.h | 1 +
arch/x86/kernel/alternative.c | 34 ++++++++++++++++++++++++++++------
arch/x86/kernel/jump_label.c | 6 ++++--
3 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h
index ea09381070e8..de43b5266867 100644
--- a/arch/x86/include/asm/text-patching.h
+++ b/arch/x86/include/asm/text-patching.h
@@ -41,6 +41,7 @@ extern void *text_poke_set(void *addr, int c, size_t len);
extern int smp_text_poke_int3_handler(struct pt_regs *regs);
extern void smp_text_poke_single(void *addr, const void *opcode, size_t len, const void *emulate);
+bool smp_text_poke_batch_try_add(void *addr, const void *opcode, size_t len, const void *emulate);
extern void smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate);
extern void smp_text_poke_batch_finish(void);
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index c837678f379f..034a9f1d8568 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -3075,6 +3075,32 @@ static bool text_poke_find_slot(void *addr, int *pos)
return true;
}
+/**
+ * smp_text_poke_batch_try_add() -- queue a batched live-kernel text poke
+ * @addr: address to patch
+ * @opcode: opcode of new instruction
+ * @len: length to copy
+ * @emulate: instruction to be emulated
+ *
+ * Queue a patching request in sorted position for execution on the next
+ * smp_text_poke_batch_finish() call without finish()ing an existing batch.
+ *
+ * Return: true if queued, or false if the batch is full or @addr is already
+ * queued.
+ */
+bool __ref smp_text_poke_batch_try_add(void *addr, const void *opcode,
+ size_t len, const void *emulate)
+{
+ int pos;
+
+ if (text_poke_array.nr_entries == TEXT_POKE_ARRAY_MAX ||
+ !text_poke_find_slot(addr, &pos))
+ return false;
+
+ __smp_text_poke_batch_add(addr, opcode, len, emulate, pos);
+ return true;
+}
+
/**
* smp_text_poke_batch_add() -- update instruction on live kernel on SMP, batched
* @addr: address to patch
@@ -3090,14 +3116,10 @@ static bool text_poke_find_slot(void *addr, int *pos)
*/
void __ref smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate)
{
- int pos = 0;
-
- if (text_poke_array.nr_entries == TEXT_POKE_ARRAY_MAX ||
- !text_poke_find_slot(addr, &pos)) {
+ if (!smp_text_poke_batch_try_add(addr, opcode, len, emulate)) {
smp_text_poke_batch_finish();
- pos = 0;
+ __smp_text_poke_batch_add(addr, opcode, len, emulate, 0);
}
- __smp_text_poke_batch_add(addr, opcode, len, emulate, pos);
}
/**
diff --git a/arch/x86/kernel/jump_label.c b/arch/x86/kernel/jump_label.c
index a7949a54a0ff..4a7daea5ce6e 100644
--- a/arch/x86/kernel/jump_label.c
+++ b/arch/x86/kernel/jump_label.c
@@ -124,6 +124,7 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
enum jump_label_type type)
{
struct jump_label_patch jlp;
+ bool queued;
if (system_state == SYSTEM_BOOTING) {
/*
@@ -135,9 +136,10 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
mutex_lock(&text_mutex);
jlp = __jump_label_patch(entry, type);
- smp_text_poke_batch_add((void *)jump_entry_code(entry), jlp.code, jlp.size, NULL);
+ queued = smp_text_poke_batch_try_add((void *)jump_entry_code(entry),
+ jlp.code, jlp.size, NULL);
mutex_unlock(&text_mutex);
- return true;
+ return queued;
}
void arch_jump_label_transform_apply(void)
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 3/5] jump_label: Add static_key_batch API
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 1/5] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 2/5] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit Jim Cromie via B4 Relay
@ 2026-12-04 18:27 ` Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 4/5] dyndbg: Batch static branch updates across queries Jim Cromie via B4 Relay
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-12-04 18:27 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Josh Poimboeuf, Jason Baron,
Alice Ryhl, Steven Rostedt, Ard Biesheuvel, Andrew Morton
Cc: linux-kernel, Jim Cromie
From: Jim Cromie <jim.cromie@gmail.com>
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@infradead.org>
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 4/5] dyndbg: Batch static branch updates across queries
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
` (2 preceding siblings ...)
2026-12-04 18:27 ` [PATCH v3 3/5] jump_label: Add static_key_batch API Jim Cromie via B4 Relay
@ 2026-12-04 18:27 ` Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 5/5] dyndbg: Batch classmap param bit-changes into multi-query Jim Cromie via B4 Relay
2026-12-04 20:29 ` [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Andrew Morton
5 siblings, 0 replies; 7+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-12-04 18:27 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Josh Poimboeuf, Jason Baron,
Alice Ryhl, Steven Rostedt, Ard Biesheuvel, Andrew Morton
Cc: linux-kernel, Jim Cromie
From: Jim Cromie <jim.cromie@gmail.com>
Enabling or disabling pr_debug() callsites in ddebug_change() currently
patches each callsite's static branch synchronously, triggering one
cross-CPU text-poke IPI sequence per matched site. Because
ddebug_change() also acquires and releases ddebug_lock on every query,
semicolon-separated multi-queries cannot coalesce updates across
sub-queries either.
Hoist ddebug_lock and wrap the sub-query loop in static_key_batch
inside ddebug_exec_queries(). Disjoint sub-queries (e.g.
"class A +p; class B +p") coalesce into one shared text-poke batch,
while overlapping toggles (e.g. "module foo +p; func bar -p" or
"+p;-p") automatically flush the pending batch when revisiting a key so
the first state settles before queuing the reverse transition.
1. Replace mutex_lock(&ddebug_lock) and mutex_unlock(&ddebug_lock) in
ddebug_change() with lockdep_assert_held(&ddebug_lock).
2. Acquire ddebug_lock once in ddebug_exec_queries() and wrap the
sub-query loop in scoped_guard(static_key_batch), flushing all
queued static branch updates before releasing ddebug_lock.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
lib/dynamic_debug.c | 42 +++++++++++++++++++++++-------------------
1 file changed, 23 insertions(+), 19 deletions(-)
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 18a71a9108d3..1659c0604b18 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -170,7 +170,7 @@ static struct ddebug_class_map *ddebug_find_valid_class(struct ddebug_table cons
* Search the tables for _ddebug's which match the given `query' and
* apply the `flags' and `mask' to them. Returns number of matching
* callsites, normally the same as number of changes. If verbose,
- * logs the changes. Takes ddebug_lock.
+ * logs the changes. Caller must hold ddebug_lock.
*/
static int ddebug_change(const struct ddebug_query *query,
struct flag_settings *modifiers)
@@ -183,8 +183,9 @@ static int ddebug_change(const struct ddebug_query *query,
struct ddebug_class_map *map = NULL;
int __outvar valid_class;
+ lockdep_assert_held(&ddebug_lock);
+
/* search for matching ddebugs */
- mutex_lock(&ddebug_lock);
list_for_each_entry(dt, &ddebug_tables, link) {
/* match against the module name */
@@ -263,7 +264,6 @@ static int ddebug_change(const struct ddebug_query *query,
dp->flags = newflags;
}
}
- mutex_unlock(&ddebug_lock);
if (!nfound && verbose)
pr_info("no matches for query\n");
@@ -573,26 +573,30 @@ static int ddebug_exec_queries(char *query, const char *modname)
char *split;
int i, errs = 0, exitcode = 0, rc, nfound = 0;
- for (i = 0; query; query = split) {
- split = strpbrk(query, ";\n");
- if (split)
- *split++ = '\0';
-
- query = skip_spaces(query);
- if (!query || !*query || *query == '#')
- continue;
+ mutex_lock(&ddebug_lock);
+ scoped_guard(static_key_batch) {
+ for (i = 0; query; query = split) {
+ split = strpbrk(query, ";\n");
+ if (split)
+ *split++ = '\0';
+
+ query = skip_spaces(query);
+ if (!query || !*query || *query == '#')
+ continue;
- vpr_info("query %d: \"%s\" mod:%s\n", i, query, modname ?: "*");
+ vpr_info("query %d: \"%s\" mod:%s\n", i, query, modname ?: "*");
- rc = ddebug_exec_query(query, modname);
- if (rc < 0) {
- errs++;
- exitcode = rc;
- } else {
- nfound += rc;
+ rc = ddebug_exec_query(query, modname);
+ if (rc < 0) {
+ errs++;
+ exitcode = rc;
+ } else {
+ nfound += rc;
+ }
+ i++;
}
- i++;
}
+ mutex_unlock(&ddebug_lock);
if (i)
v2pr_info("processed %d queries, with %d matches, %d errs\n",
i, nfound, errs);
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 5/5] dyndbg: Batch classmap param bit-changes into multi-query
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
` (3 preceding siblings ...)
2026-12-04 18:27 ` [PATCH v3 4/5] dyndbg: Batch static branch updates across queries Jim Cromie via B4 Relay
@ 2026-12-04 18:27 ` Jim Cromie via B4 Relay
2026-12-04 20:29 ` [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Andrew Morton
5 siblings, 0 replies; 7+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-12-04 18:27 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Peter Zijlstra, Josh Poimboeuf, Jason Baron,
Alice Ryhl, Steven Rostedt, Ard Biesheuvel, Andrew Morton
Cc: linux-kernel, Jim Cromie
From: Jim Cromie <jim.cromie@gmail.com>
ddebug_apply_class_bitmap() currently invokes ddebug_exec_queries()
once per changed bit in a classmap parameter, re-acquiring ddebug_lock,
flushing a separate static_key_batch, and re-scanning ddebug_tables for
every toggled class on a single sysfs write.
Now that ddebug_exec_queries() holds ddebug_lock and static_key_batch
across semicolon-separated sub-queries, formatting all toggled class
bits into one multi-query buffer lets a classmap bitmap write coalesce
all class updates into a single batched submission:
1. Return early when the new and old bitmaps match or ddebug_tables is
empty. Built-in module parameters invoke param_set_dyndbg_classes()
from parse_args() before SLUB is initialized and before
dynamic_debug_init() populates ddebug_tables; returning 0 preserves
the existing empty-table no-op while avoiding pre-SLUB kmalloc().
2. Allocate a PAGE_SIZE query buffer and format each toggled class bit
as a semicolon-separated class query, trimming the trailing
semicolon before passing the buffer to ddebug_exec_queries().
3. Bail out on negative returns from ddebug_apply_class_bitmap() in
param_set_dyndbg_classnames() and param_set_dyndbg_classes() without
updating *dcp->bits or *dcp->lvl, so allocation or query failures do
not desynchronize cached state and turn retries into no-ops.
NB: The PAGE_SIZE query buffer cannot reasonably overflow: even with
32 classes toggling at once, it fits class names over ~111 characters
each -- well past anything readable or usable -- and still returns
-ENOSPC on WARN_ON_ONCE() defensively. A forthcoming classmaps-fixes
series will add compile time limits to ensure this.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
lib/dynamic_debug.c | 63 +++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 47 insertions(+), 16 deletions(-)
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 1659c0604b18..f4516803f6e8 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -610,11 +610,20 @@ static int ddebug_exec_queries(char *query, const char *modname)
static int ddebug_apply_class_bitmap(const struct ddebug_class_param *dcp,
unsigned long *new_bits, unsigned long *old_bits)
{
-#define QUERY_SIZE 128
- char query[QUERY_SIZE];
const struct ddebug_class_map *map = dcp->map;
- int matches = 0;
- int bi, ct;
+ int bi, pos = 0, matches = 0;
+ char *buf;
+
+ /*
+ * Short-circuit no-op writes and early-boot param parsing before
+ * dynamic_debug_init() populates ddebug_tables and SLUB is up.
+ */
+ if (*new_bits == *old_bits || list_empty(&ddebug_tables))
+ return 0;
+
+ buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
v2pr_info("apply: 0x%lx to: 0x%lx\n", *new_bits, *old_bits);
@@ -622,15 +631,21 @@ static int ddebug_apply_class_bitmap(const struct ddebug_class_param *dcp,
if (test_bit(bi, new_bits) == test_bit(bi, old_bits))
continue;
- snprintf(query, QUERY_SIZE, "class %s %c%s", map->class_names[bi],
- test_bit(bi, new_bits) ? '+' : '-', dcp->flags);
-
- ct = ddebug_exec_queries(query, NULL);
- matches += ct;
+ pos += scnprintf(buf + pos, PAGE_SIZE - pos, "class %s %c%s;",
+ map->class_names[bi],
+ test_bit(bi, new_bits) ? '+' : '-', dcp->flags);
+ }
- v2pr_info("bit_%d: %d matches on class: %s -> 0x%lx\n", bi,
- ct, map->class_names[bi], *new_bits);
+ if (WARN_ON_ONCE(pos >= PAGE_SIZE - 1)) {
+ kfree(buf);
+ return -ENOSPC;
}
+ if (pos) {
+ buf[pos - 1] = '\0'; /* trim trailing ';' */
+ matches = ddebug_exec_queries(buf, NULL);
+ }
+
+ kfree(buf);
return matches;
}
@@ -646,7 +661,7 @@ static int param_set_dyndbg_classnames(const char *instr, const struct kernel_pa
const struct ddebug_class_map *map = dcp->map;
unsigned long curr_bits, old_bits;
char *cl_str, *p, *tmp;
- int cls_id, totct = 0;
+ int cls_id, rc, totct = 0;
bool wanted;
cl_str = tmp = kstrdup_and_replace(instr, '\n', '\0', GFP_KERNEL);
@@ -685,7 +700,12 @@ static int param_set_dyndbg_classnames(const char *instr, const struct kernel_pa
continue;
}
curr_bits ^= BIT(cls_id);
- totct += ddebug_apply_class_bitmap(dcp, &curr_bits, dcp->bits);
+ rc = ddebug_apply_class_bitmap(dcp, &curr_bits, dcp->bits);
+ if (rc < 0) {
+ kfree(tmp);
+ return rc;
+ }
+ totct += rc;
*dcp->bits = curr_bits;
v2pr_info("%s: changed bit %d:%s\n", KP_NAME(kp), cls_id,
map->class_names[cls_id]);
@@ -695,7 +715,12 @@ static int param_set_dyndbg_classnames(const char *instr, const struct kernel_pa
old_bits = CLASSMAP_BITMASK(*dcp->lvl);
curr_bits = CLASSMAP_BITMASK(cls_id + (wanted ? 1 : 0 ));
- totct += ddebug_apply_class_bitmap(dcp, &curr_bits, &old_bits);
+ rc = ddebug_apply_class_bitmap(dcp, &curr_bits, &old_bits);
+ if (rc < 0) {
+ kfree(tmp);
+ return rc;
+ }
+ totct += rc;
*dcp->lvl = (cls_id + (wanted ? 1 : 0));
v2pr_info("%s: changed bit-%d: \"%s\" %lx->%lx\n", KP_NAME(kp), cls_id,
map->class_names[cls_id], old_bits, curr_bits);
@@ -758,7 +783,10 @@ int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp)
inrep &= CLASSMAP_BITMASK(map->length);
}
v2pr_info("bits:%lx > %s\n", inrep, KP_NAME(kp));
- totct += ddebug_apply_class_bitmap(dcp, &inrep, dcp->bits);
+ rc = ddebug_apply_class_bitmap(dcp, &inrep, dcp->bits);
+ if (rc < 0)
+ return rc;
+ totct += rc;
*dcp->bits = inrep;
break;
case DD_CLASS_TYPE_LEVEL_NUM:
@@ -771,7 +799,10 @@ int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp)
old_bits = CLASSMAP_BITMASK(*dcp->lvl);
new_bits = CLASSMAP_BITMASK(inrep);
v2pr_info("lvl:%ld bits:0x%lx > %s\n", inrep, new_bits, KP_NAME(kp));
- totct += ddebug_apply_class_bitmap(dcp, &new_bits, &old_bits);
+ rc = ddebug_apply_class_bitmap(dcp, &new_bits, &old_bits);
+ if (rc < 0)
+ return rc;
+ totct += rc;
*dcp->lvl = inrep;
break;
default:
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
` (4 preceding siblings ...)
2026-12-04 18:27 ` [PATCH v3 5/5] dyndbg: Batch classmap param bit-changes into multi-query Jim Cromie via B4 Relay
@ 2026-12-04 20:29 ` Andrew Morton
5 siblings, 0 replies; 7+ messages in thread
From: Andrew Morton @ 2026-12-04 20:29 UTC (permalink / raw)
To: jim.cromie
Cc: Jim Cromie via B4 Relay, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Peter Zijlstra, Josh Poimboeuf, Jason Baron, Alice Ryhl,
Steven Rostedt, Ard Biesheuvel, linux-kernel
On Fri, 04 Dec 2026 11:27:51 -0700 Jim Cromie via B4 Relay <devnull+jim.cromie.gmail.com@kernel.org> wrote:
> Toggling many boolean static keys at once -- such as enabling or
> disabling pr_debug() sites across vmlinux and loaded modules --
> currently patches one static key at a time. Each toggle sends a full
> set of cross-CPU text-poke IPIs (~39.7k CAL IPIs to flip 1,885 active
> builtin keys across 8 CPUs on x86). Even if kernel/jump_label.c defers
> arch_jump_label_transform_apply() across a batch of static-keys, x86's
> smp_text_poke_batch_add() flushes whenever a poke address arrives below
> the tail entry (~2 entries per batch in dyndbg descriptor order) and
> hides its 256-entry flush boundaries from jump_label.c.
>
> This series updates all three layers so a static_key_batch guard
> transparently batches static_key_{enable,disable,slow_inc,slow_dec}()
> calls while handling out-of-order poke addresses and same-batch state
> reversals cleanly:
>
> ...
>
> Measured on an 8-vCPU x86_64 guest (v7.4-rc5, 1,997 builtin pr_debug
> descriptors / 1,885 non-init keys / 1,964 jump sites,
> CONFIG_PROVE_LOCKING=y):
>
> Workload master v3 (5 patches) Speedup
> -------------------------------------------------------------------
> echo "+p" > control 39,712 282 CAL 141x
> echo "-p" > control 39,674 255 CAL 156x
> "+p ; -p" (1x) 79,256 424 CAL 187x
> "+p ; -p" (4x) 316,773 756 CAL 419x
>
> Bare-metal desktops (vmlinux + loaded modules incl. amdgpu):
>
> Host / CPUs / Sites "module amdgpu +p" "+p ; -p" (4x)
> ----------------------------------------------------------------
> Ryzen 9 6900HX, 16c, 8,484 615 CAL 12,524 CAL
> Ryzen 9 3900X, 24c, 8,197 909 CAL 18,243 CAL
Jim, is 491x the best you can do? c'mon, man...
What I'm not finding in here is the actual user-visible effect. How
much user-visible wall time are we talking about here? When they
do what??
(And this info should be in the first paragraph of the [0/N]! When
busy maintainers&reviewers look at a thing, their uppermost question is
"why should I care". Tell them!)
Sashiko said a bunch of stuff, fwtw:
https://sashiko.dev/#/patchset/20261204-jump-batch-261128-v3-0-2adda55a5d71@gmail.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-04 20:29 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 1/5] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 2/5] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 3/5] jump_label: Add static_key_batch API Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 4/5] dyndbg: Batch static branch updates across queries Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 5/5] dyndbg: Batch classmap param bit-changes into multi-query Jim Cromie via B4 Relay
2026-12-04 20:29 ` [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®