* [PATCH v2 1/7] x86/alternatives: Keep text-poke batch sorted on insert
2026-11-29 7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
@ 2026-11-29 7:44 ` Jim Cromie via B4 Relay
2026-11-30 16:13 ` Steven Rostedt
2026-11-29 7:44 ` [PATCH v2 2/7] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit Jim Cromie via B4 Relay
` (5 subsequent siblings)
6 siblings, 1 reply; 11+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-11-29 7:44 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 patch sites in
INT3 trap context, requiring vec[0 .. nr_entries - 1] to be strictly
sorted by patch address with unique entries. Currently
smp_text_poke_batch_add() enforces that invariant by calling
smp_text_poke_batch_finish() whenever a new address is lower than the
tail entry.
For ftrace and single-key jump_label updates, patch addresses arrive in
ascending order, so the queue fills to capacity before finish()ing.
Multi-key callers such as dyndbg, however, walk descriptors rather than
code order; compiler and linker layout scramble patch addresses enough
to trigger finish()ing after ~2 entries on average.
Since vec[] is private under text_mutex until finish() publishes
text_poke_array_refs, out-of-order entries can be inserted into sorted
position instead of finish()ing early. Sorting on insert also detects
duplicate addresses immediately, so add() can finish() before queueing
a second poke to the same @addr.
0. Replace text_poke_addr_ordered() with
text_poke_find_slot(addr, &pos). Fast-path '!nr || addr > tail' in
one comparison (*pos = nr), which also catches adjacent addr == tail
duplicates missed by the old '>' check.
1. On out-of-order arrivals, binary-search vec[] (<= 8 steps for 256
entries) using 64-bit text_poke_addr() pointers to match patch_cmp()
across core and module text. If @addr is already present, return
false so smp_text_poke_batch_add() finish()es before queueing it.
2. In __smp_text_poke_batch_add(), shift vec[pos .. nr_entries - 1] up
one slot via memmove() when pos < nr_entries; vec[] fits in one 4 KB
page, moving ~1 KB on average.
What's Unchanged:
0. Pre-sorted callers -- ftrace_replace_code() and single-key
jump_label updates -- take the 1-comparison 'addr > tail' fastpath
with zero binary searches and zero memmove() calls.
1. smp_text_poke_batch_finish(), smp_text_poke_int3_handler(), and
struct smp_text_poke_array are unchanged.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
arch/x86/kernel/alternative.c | 63 ++++++++++++++++++++++++++++++-------------
1 file changed, 45 insertions(+), 18 deletions(-)
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index efbc11f8c336..d924c3e891b6 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 smp_text_poke_loc *tpl = &text_poke_array.vec[pos];
struct insn insn;
int ret, i = 0;
- tpl = &text_poke_array.vec[text_poke_array.nr_entries++];
+ 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,43 @@ 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)
+ if (!nr || addr > text_poke_addr(&text_poke_array.vec[nr - 1])) {
+ *pos = nr;
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;
}
@@ -3068,11 +3089,17 @@ static bool text_poke_addr_ordered(void *addr)
* execution. All pending patching requests will be executed on the next
* smp_text_poke_batch_finish() call.
*/
-void __ref smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate)
+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] 11+ messages in thread* Re: [PATCH v2 1/7] x86/alternatives: Keep text-poke batch sorted on insert
2026-11-29 7:44 ` [PATCH v2 1/7] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
@ 2026-11-30 16:13 ` Steven Rostedt
0 siblings, 0 replies; 11+ messages in thread
From: Steven Rostedt @ 2026-11-30 16:13 UTC (permalink / raw)
To: Jim Cromie via B4 Relay
Cc: jim.cromie, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Peter Zijlstra, Josh Poimboeuf,
Jason Baron, Alice Ryhl, Ard Biesheuvel, Andrew Morton,
linux-kernel
On Sun, 29 Nov 2026 00:44:14 -0700
Jim Cromie via B4 Relay <devnull+jim.cromie.gmail.com@kernel.org> wrote:
Please clean up the AI output because AI sucks at writing control flow in a
human readable way. The below is absolute garbage for understanding what is
going on.
> 0. Replace text_poke_addr_ordered() with
> text_poke_find_slot(addr, &pos). Fast-path '!nr || addr > tail' in
> one comparison (*pos = nr), which also catches adjacent addr == tail
> duplicates missed by the old '>' check.
I mean mentioning "(*pos = nr)" after saying "in one comparison" is very
confusing. It makes no sense without reading the code. The change log is to
understand the code, not the other way around. I'm really hating the AI
change logs because I have to read the code to reverse engineer what the
change log is talking about.
> 1. On out-of-order arrivals, binary-search vec[] (<= 8 steps for 256
> entries) using 64-bit text_poke_addr() pointers to match patch_cmp()
> across core and module text. If @addr is already present, return
> false so smp_text_poke_batch_add() finish()es before queueing it.
You really understand what the above is saying?
> 2. In __smp_text_poke_batch_add(), shift vec[pos .. nr_entries - 1] up
> one slot via memmove() when pos < nr_entries; vec[] fits in one 4 KB
> page, moving ~1 KB on average.
When submitting AI created patches, update the wording better. For example
the above should have been:
[ and start with 1. not 0. as this isn't a C array, as well as space them ]
1. Rename text_poke_addr_order() to text_poke_find_slot().
Update the index to the nr_entries immediately and return if either
there are no current entries or the addr is greater than the tail.
This catches adjacent addr == tail duplicates missed by just updating if
the tail is greater than the addr.
2. For out-of-order entries, binary search the vec[] using the
text_poke_addr() pointer. If found, return false so that
smp_text_poke_batch_add() calls finish() before queuing it.
3. In __smp_text_poke_batch_add() use memmove to shift the vec[] array to
add the new entry. This moves ~1 KB on average.
See how much easier it is to read. The above translation between AI slop
output to human readable messages is up to the submitter. Not the
maintainer or reviewer.
>
> What's Unchanged:
>
> 0. Pre-sorted callers -- ftrace_replace_code() and single-key
> jump_label updates -- take the 1-comparison 'addr > tail' fastpath
> with zero binary searches and zero memmove() calls.
> 1. smp_text_poke_batch_finish(), smp_text_poke_int3_handler(), and
> struct smp_text_poke_array are unchanged.
I personally do not think the "unchanged" section is useful. I have no idea
why AI wants to tell us this.
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index efbc11f8c336..d924c3e891b6 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 smp_text_poke_loc *tpl = &text_poke_array.vec[pos];
Why make the assignment here and not just replace the assignment below but
without the nr_entries update?
> struct insn insn;
> int ret, i = 0;
>
> - tpl = &text_poke_array.vec[text_poke_array.nr_entries++];
> + 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,43 @@ 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)
> + if (!nr || addr > text_poke_addr(&text_poke_array.vec[nr - 1])) {
> + *pos = nr;
> 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;
> }
>
> @@ -3068,11 +3089,17 @@ static bool text_poke_addr_ordered(void *addr)
> * execution. All pending patching requests will be executed on the next
> * smp_text_poke_batch_finish() call.
> */
> -void __ref smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate)
> +void __ref smp_text_poke_batch_add(void *addr, const void *opcode,
> + size_t len, const void *emulate)
Please do not add line breaks to code you do not touch. If the maintainer
had it that way, it may be what they prefer.
-- Steve
> {
> - 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);
> }
>
> /**
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 2/7] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit
2026-11-29 7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 1/7] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
@ 2026-11-29 7:44 ` Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 3/7] jump_label: Plumb bool apply through jump_label_update_key() Jim Cromie via B4 Relay
` (4 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-11-29 7:44 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 architecture patch queue is full so the caller can
apply the current batch and retry. On x86, however,
arch_jump_label_transform_queue() calls smp_text_poke_batch_add(), which
silently finish()es text_poke_array at TEXT_POKE_ARRAY_MAX (256), and
unconditionally returns true.
That hides batch boundaries from kernel/jump_label.c -- harmless for a
single static_key, where key->enabled stays fixed across internal
flushes, but upcoming multi-key static_key_batch queueing needs the
queue-full signal whenever multi-site (e.g. inlined) keys fill the
256-site text_poke_array ahead of the 256-key queue, avoiding extra
runt flushes every cycle.
Wire the bool queue-full return back up to __jump_label_update():
0. Extract smp_text_poke_batch_try_add() in alternative.c, returning
false without finish()ing when text_poke_array is full or @addr is
already queued in vec[], and keep smp_text_poke_batch_add() as an
auto-finishing wrapper for callers such as ftrace_replace_code().
1. Call smp_text_poke_batch_try_add() in x86's
arch_jump_label_transform_queue() and return its bool result so
__jump_label_update() flushes and retries at TEXT_POKE_ARRAY_MAX.
What's Unchanged:
0. smp_text_poke_batch_add() callers -- ftrace_replace_code() and
smp_text_poke_single() -- keep identical auto-finishing behavior.
1. Single-key jump_label_update() behavior is unchanged: entry 256 now
triggers arch_jump_label_transform_apply() from __jump_label_update()
rather than inside smp_text_poke_batch_add().
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 d924c3e891b6..046b7da6a3f9 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -3076,6 +3076,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
@@ -3092,14 +3118,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] 11+ messages in thread* [PATCH v2 3/7] jump_label: Plumb bool apply through jump_label_update_key()
2026-11-29 7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 1/7] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 2/7] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit Jim Cromie via B4 Relay
@ 2026-11-29 7:44 ` Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 4/7] jump_label: Add batching API for queued static key updates Jim Cromie via B4 Relay
` (3 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-11-29 7:44 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() under HAVE_JUMP_LABEL_BATCH unconditionally calls
arch_jump_label_transform_apply() after queueing all jump entries for a
single static_key, which prevents callers from coalescing multiple keys
into one architecture batch flush.
Prepare for multi-key batching without changing current behavior:
0. Unify the #ifndef / #else HAVE_JUMP_LABEL_BATCH definitions of
__jump_label_update() and gate the trailing
arch_jump_label_transform_apply() on a new @apply parameter.
1. Thread @apply through __jump_label_mod_update() and
jump_label_update_key(), keeping jump_label_update() as a wrapper
passing apply = true.
No functional change.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
kernel/jump_label.c | 42 ++++++++++++++++++++----------------------
1 file changed, 20 insertions(+), 22 deletions(-)
diff --git a/kernel/jump_label.c b/kernel/jump_label.c
index ab6b774bcfd7..d20b5b3ffbd3 100644
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -488,28 +488,16 @@ static bool jump_label_can_update(struct jump_entry *entry, bool init)
return true;
}
-#ifndef HAVE_JUMP_LABEL_BATCH
static void __jump_label_update(struct static_key *key,
struct jump_entry *entry,
struct jump_entry *stop,
- bool init)
+ bool init, bool apply)
{
for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
- if (jump_label_can_update(entry, init))
- arch_jump_label_transform(entry, jump_label_type(entry));
- }
-}
-#else
-static void __jump_label_update(struct static_key *key,
- struct jump_entry *entry,
- struct jump_entry *stop,
- bool init)
-{
- for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
-
if (!jump_label_can_update(entry, init))
continue;
+#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.
@@ -517,10 +505,15 @@ static void __jump_label_update(struct static_key *key,
arch_jump_label_transform_apply();
BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry)));
}
+#else
+ arch_jump_label_transform(entry, jump_label_type(entry));
+#endif
}
- arch_jump_label_transform_apply();
-}
+#ifdef HAVE_JUMP_LABEL_BATCH
+ if (apply)
+ arch_jump_label_transform_apply();
#endif
+}
void __init jump_label_init(void)
{
@@ -662,7 +655,7 @@ static int __jump_label_mod_text_reserved(void *start, void *end)
return ret;
}
-static void __jump_label_mod_update(struct static_key *key)
+static void __jump_label_mod_update(struct static_key *key, bool apply)
{
struct static_key_mod *mod;
@@ -683,7 +676,7 @@ static void __jump_label_mod_update(struct static_key *key)
else
stop = m->jump_entries + m->num_jump_entries;
__jump_label_update(key, mod->entries, stop,
- m && m->state == MODULE_STATE_COMING);
+ m && m->state == MODULE_STATE_COMING, apply);
}
}
@@ -752,7 +745,7 @@ static int jump_label_add_module(struct module *mod)
/* Only update if we've changed from our initial state */
do_poke:
if (jump_label_type(iter) != jump_label_init_type(iter))
- __jump_label_update(key, iter, iter_stop, true);
+ __jump_label_update(key, iter, iter_stop, true, true);
}
return 0;
@@ -882,7 +875,7 @@ int jump_label_text_reserved(void *start, void *end)
return ret;
}
-static void jump_label_update(struct static_key *key)
+static void jump_label_update_key(struct static_key *key, bool apply)
{
struct jump_entry *stop = __stop___jump_table;
bool init = system_state < SYSTEM_RUNNING;
@@ -891,7 +884,7 @@ static void jump_label_update(struct static_key *key)
struct module *mod;
if (static_key_linked(key)) {
- __jump_label_mod_update(key);
+ __jump_label_mod_update(key, apply);
return;
}
@@ -906,7 +899,12 @@ static void jump_label_update(struct static_key *key)
entry = static_key_entries(key);
/* if there are no users, entry can be NULL */
if (entry)
- __jump_label_update(key, entry, stop, init);
+ __jump_label_update(key, entry, stop, init, apply);
+}
+
+static void jump_label_update(struct static_key *key)
+{
+ jump_label_update_key(key, true);
}
#ifdef CONFIG_STATIC_KEYS_SELFTEST
--
2.56.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v2 4/7] jump_label: Add batching API for queued static key updates
2026-11-29 7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
` (2 preceding siblings ...)
2026-11-29 7:44 ` [PATCH v2 3/7] jump_label: Plumb bool apply through jump_label_update_key() Jim Cromie via B4 Relay
@ 2026-11-29 7:44 ` Jim Cromie via B4 Relay
2026-11-30 11:55 ` Peter Zijlstra
2026-11-29 7:44 ` [PATCH v2 5/7] dyndbg: Use queued static-key batching in ddebug_change() Jim Cromie via B4 Relay
` (2 subsequent siblings)
6 siblings, 1 reply; 11+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-11-29 7:44 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>
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 <jim.cromie@gmail.com>
---
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
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v2 4/7] jump_label: Add batching API for queued static key updates
2026-11-29 7:44 ` [PATCH v2 4/7] jump_label: Add batching API for queued static key updates Jim Cromie via B4 Relay
@ 2026-11-30 11:55 ` Peter Zijlstra
2026-11-30 13:51 ` Peter Zijlstra
0 siblings, 1 reply; 11+ messages in thread
From: Peter Zijlstra @ 2026-11-30 11:55 UTC (permalink / raw)
To: jim.cromie
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Josh Poimboeuf, Jason Baron, Alice Ryhl,
Steven Rostedt, Ard Biesheuvel, Andrew Morton, linux-kernel
On Sun, Nov 29, 2026 at 12:44:17AM -0700, Jim Cromie via B4 Relay wrote:
> From: Jim Cromie <jim.cromie@gmail.com>
>
> 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
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v2 4/7] jump_label: Add batching API for queued static key updates
2026-11-30 11:55 ` Peter Zijlstra
@ 2026-11-30 13:51 ` Peter Zijlstra
0 siblings, 0 replies; 11+ messages in thread
From: Peter Zijlstra @ 2026-11-30 13:51 UTC (permalink / raw)
To: jim.cromie
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Josh Poimboeuf, Jason Baron, Alice Ryhl,
Steven Rostedt, Ard Biesheuvel, Andrew Morton, linux-kernel
On Mon, Nov 30, 2026 at 12:55:09PM +0100, Peter Zijlstra wrote:
> +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_end_cpuslocked();
> + cpus_read_unlock();
> +}
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 5/7] dyndbg: Use queued static-key batching in ddebug_change()
2026-11-29 7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
` (3 preceding siblings ...)
2026-11-29 7:44 ` [PATCH v2 4/7] jump_label: Add batching API for queued static key updates Jim Cromie via B4 Relay
@ 2026-11-29 7:44 ` Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 6/7] dyndbg: Batch classmap param bit-changes into multi-query Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 7/7] dyndbg: Hoist lock and static_key_batch across queries Jim Cromie via B4 Relay
6 siblings, 0 replies; 11+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-11-29 7:44 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 "+p" or "-p" across a broad query in ddebug_change() currently
patches each callsite's static branch synchronously, triggering one
cross-CPU text-poke IPI per matched descriptor.
Switch ddebug_change() to the batched static-key queueing API:
0. Bracket the ddebug_tables scan in static_branch_batch_begin() and
static_branch_batch_end() under ddebug_lock.
1. Replace static_branch_enable() and static_branch_disable() with
static_branch_enable_queued() and static_branch_disable_queued().
2. Log total matched sites after flushing the batch via v2pr_info().
Instead of one text-poke IPI sequence per matched pr_debug() site,
synchronization is amortized per batch flush (~39.7k -> ~280 CAL IPIs
toggling 1,997 sites across 8 CPUs on x86).
What's Unchanged:
0. Lock ordering remains ddebug_lock -> cpus_read_lock ->
jump_label_mutex -> text_mutex.
1. Query matching and descriptor flag updates in ddebug_change() are
unchanged.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
lib/dynamic_debug.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 18a71a9108d3..48f520a57b22 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -185,6 +185,7 @@ static int ddebug_change(const struct ddebug_query *query,
/* search for matching ddebugs */
mutex_lock(&ddebug_lock);
+ static_branch_batch_begin();
list_for_each_entry(dt, &ddebug_tables, link) {
/* match against the module name */
@@ -250,9 +251,9 @@ static int ddebug_change(const struct ddebug_query *query,
#ifdef CONFIG_JUMP_LABEL
if (dp->flags & _DPRINTK_FLAGS_PRINT) {
if (!(newflags & _DPRINTK_FLAGS_PRINT))
- static_branch_disable(&dp->key.dd_key_true);
+ static_branch_disable_queued(&dp->key.dd_key_true);
} else if (newflags & _DPRINTK_FLAGS_PRINT) {
- static_branch_enable(&dp->key.dd_key_true);
+ static_branch_enable_queued(&dp->key.dd_key_true);
}
#endif
v4pr_info("changed %s:%d [%s]%s %s => %s\n",
@@ -263,7 +264,9 @@ static int ddebug_change(const struct ddebug_query *query,
dp->flags = newflags;
}
}
+ static_branch_batch_end();
mutex_unlock(&ddebug_lock);
+ v2pr_info("applied %d queued updates to sites in total\n", nfound);
if (!nfound && verbose)
pr_info("no matches for query\n");
--
2.56.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v2 6/7] dyndbg: Batch classmap param bit-changes into multi-query
2026-11-29 7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
` (4 preceding siblings ...)
2026-11-29 7:44 ` [PATCH v2 5/7] dyndbg: Use queued static-key batching in ddebug_change() Jim Cromie via B4 Relay
@ 2026-11-29 7:44 ` Jim Cromie via B4 Relay
2026-11-29 7:44 ` [PATCH v2 7/7] dyndbg: Hoist lock and static_key_batch across queries Jim Cromie via B4 Relay
6 siblings, 0 replies; 11+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-11-29 7:44 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 runs ddebug_exec_queries() once
per changed bit in a classmap parameter, re-acquiring locks and
re-scanning ddebug_tables for every toggled class in the classmap on a
single sysfs write.
Assemble all changed class bits into one semicolon-separated multi-query
buffer and submit them in a single ddebug_exec_queries() call:
0. Return early when *new_bits == *old_bits or ddebug_tables is empty.
Built-in module parameters (e.g. drm.debug=0x1f with CONFIG_DRM=y)
invoke param_set_dyndbg_classes() from parse_args() in start_kernel()
before mm_core_init() initializes SLUB and before
early_initcall(dynamic_debug_init) populates ddebug_tables; returning
0 preserves the existing empty-table no-op while avoiding pre-SLUB
kmalloc().
1. Allocate a PAGE_SIZE buffer via kmalloc(GFP_KERNEL) and format each
toggled bit as "class %s %c%s;" via scnprintf(), trimming the final
';' before calling ddebug_exec_queries(). Each entry uses
strlen(name) + strlen(flags) + 9 bytes (<= 15 bytes syntax overhead),
so PAGE_SIZE (4096) fits up to 50-char names across 63 classes (and
113-char names across 32 classes) without chunked flushing, guarded
by WARN_ON_ONCE(pos == PAGE_SIZE - 1).
What's Unchanged:
0. Unchanged bitmap bits are skipped without generating queries.
1. Return value still reports total matched callsites or a negative
errno on allocation or query failure.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
lib/dynamic_debug.c | 34 +++++++++++++++++++++++-----------
1 file changed, 23 insertions(+), 11 deletions(-)
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 48f520a57b22..02dfe37aa834 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -609,11 +609,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);
@@ -621,15 +630,18 @@ 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);
+ WARN_ON_ONCE(pos == PAGE_SIZE - 1);
+ if (pos) {
+ buf[pos - 1] = '\0'; /* trim trailing ';' */
+ matches = ddebug_exec_queries(buf, NULL);
}
+
+ kfree(buf);
return matches;
}
--
2.56.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v2 7/7] dyndbg: Hoist lock and static_key_batch across queries
2026-11-29 7:44 [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
` (5 preceding siblings ...)
2026-11-29 7:44 ` [PATCH v2 6/7] dyndbg: Batch classmap param bit-changes into multi-query Jim Cromie via B4 Relay
@ 2026-11-29 7:44 ` Jim Cromie via B4 Relay
6 siblings, 0 replies; 11+ messages in thread
From: Jim Cromie via B4 Relay @ 2026-11-29 7:44 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_change() acquires and releases ddebug_lock and flushes the
static-key batch on every single query, forcing semicolon-separated
multi-queries (such as ddebug_apply_class_bitmap() updates or
echo "class A +p; class B +p" > control) to send a separate text-poke
IPI per sub-query.
Hoist both ddebug_lock and static_key_batch up to ddebug_exec_queries():
0. Replace mutex_lock(&ddebug_lock) and static_branch_batch_begin/end()
in ddebug_change() with lockdep_assert_held(&ddebug_lock).
1. Acquire ddebug_lock once around ddebug_exec_queries() and wrap the
sub-query loop in scoped_guard(static_key_batch), coalescing static
branch updates across all ';' and '\n' delimited sub-queries into a
single batched flush before releasing ddebug_lock.
What's Unchanged:
0. Same-batch key reversals (e.g. "module foo +p; func bar -p") remain
safe: __static_key_set_queued() flushes the prior batch when a key is
already in-flight in the opposite direction.
1. Lock hierarchy (ddebug_lock -> cpus_read_lock -> jump_label_mutex ->
text_mutex) is unchanged, and queries that toggle no static keys skip
text_mutex in static_key_batch_end().
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
lib/dynamic_debug.c | 44 +++++++++++++++++++++++---------------------
1 file changed, 23 insertions(+), 21 deletions(-)
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 02dfe37aa834..9aedffe4938e 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,9 +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);
- static_branch_batch_begin();
list_for_each_entry(dt, &ddebug_tables, link) {
/* match against the module name */
@@ -264,8 +264,6 @@ static int ddebug_change(const struct ddebug_query *query,
dp->flags = newflags;
}
}
- static_branch_batch_end();
- mutex_unlock(&ddebug_lock);
v2pr_info("applied %d queued updates to sites in total\n", nfound);
if (!nfound && verbose)
@@ -576,26 +574,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] 11+ messages in thread