mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/7] jump_label, x86, dyndbg: Batch multi-key static branch updates
@ 2026-11-29  7:44 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
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ 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

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, sending cross-CPU text-poke
IPIs per site (~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 keys, x86's
smp_text_poke_batch_add() finish()es whenever a patch address arrives
below the tail entry (~2 entries per batch in dyndbg descriptor order)
and hides its 256-entry queue limit from jump_label.c.

This series fixes all three layers so multi-key static_key_batch
updates fill x86's 256-entry text_poke_array cleanly:

0. x86/alternatives (1/7): keep text_poke_array.vec[] sorted on insert
   via lower-bound binary search and memmove() inside its 4 KB page,
   preserving the 1-comparison fastpath for pre-sorted callers (ftrace
   and single-key jump_label) while letting unordered multi-key callers
   fill the batch and rejecting duplicate @addr entries before queueing.
1. x86/jump_label (2/7): add smp_text_poke_batch_try_add() and return
   its bool status from arch_jump_label_transform_queue() so
   __jump_label_update() flushes at TEXT_POKE_ARRAY_MAX (256) in
   lockstep with text_poke_array when multi-site (e.g. inlined) keys
   fill the 256-site array ahead of MAX_QUEUED_KEYS (256).
2. jump_label (3/7..4/7): plumb @apply through jump_label_update_key()
   (no functional change in 3/7), then add core-only (unexported)
   static_key_batch_begin/end(), DEFINE_LOCK_GUARD_0(static_key_batch),
   and static_key_{enable,disable}_queued() using negative transitional
   markers (-1/-2) finalized via release stores after IPI completion.
3. dyndbg (5/7..7/7): switch ddebug_change() to queued static-key
   batching, assemble changed classmap parameter bits into one
   multi-query buffer, and hoist ddebug_lock and
   scoped_guard(static_key_batch) to ddebug_exec_queries().

Measured on an 8-vCPU x86_64 guest (v7.4-rc4, 1,997 builtin pr_debug
descriptors / 1,885 non-init keys / 1,964 jump sites,
CONFIG_PROVE_LOCKING=y):

  Workload                        master     v2 (7 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        1,435 CAL      221x
  classmap bitmap 0x3ff (10 cls)     330          143 CAL      2.3x

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).

Verification & Tooling Disclosure:

0. Tested on 16-CPU (CONFIG_PROVE_LOCKING=y,
   CONFIG_FTRACE_STARTUP_TEST=y, CONFIG_EVENT_TRACE_STARTUP_TEST=y) and
   24-CPU AMD Ryzen hosts, plus 8-vCPU virtme-ng with
   CONFIG_STATIC_KEYS_SELFTEST=y (extended with batch + reversal cases),
   CONFIG_TEST_STATIC_KEYS=m, CONFIG_TEST_DYNAMIC_DEBUG=m, boot dyndbg=
   queries, and ftrace function tracer; 0 lockdep or dmesg warnings.
1. AI coding assistants (Gemini CLI / Antigravity) assisted with
   refactoring the commit split, designing the binary-search insertion
   sort in 1/7 and queue-limit sync in 2/7, and running virtme-ng and
   host IPI benchmarks; all code and commit messages were reviewed and
   tested by the author.

Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
Changes in v2:

0. CC x86, jump_label, ftrace, and dyndbg maintainers across the full
   series.
1. Split the combined v1 patch into single-purpose commits ordered
   x86 -> jump_label -> dyndbg, with mechanical @apply plumbing
   isolated in 3/7.
2. Drop all EXPORT_SYMBOL_GPL() exports on the queued static-key API
   (core-only).
3. Drop pr_debug() instrumentation from arch/x86/kernel/alternative.c.
4. 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().
5. 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 (7):
      x86/alternatives: Keep text-poke batch sorted on insert
      x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit
      jump_label: Plumb bool apply through jump_label_update_key()
      jump_label: Add batching API for queued static key updates
      dyndbg: Use queued static-key batching in ddebug_change()
      dyndbg: Batch classmap param bit-changes into multi-query
      dyndbg: Hoist lock and static_key_batch across queries

 arch/x86/include/asm/text-patching.h |   1 +
 arch/x86/kernel/alternative.c        |  85 ++++++++---
 arch/x86/kernel/jump_label.c         |   6 +-
 include/linux/jump_label.h           |  22 +++
 kernel/jump_label.c                  | 282 ++++++++++++++++++++++++++++++-----
 lib/dynamic_debug.c                  |  81 ++++++----
 6 files changed, 387 insertions(+), 90 deletions(-)
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261128-jump-batch-261128-1d89de6673f4

Best regards,
-- 
Jim Cromie <jim.cromie@gmail.com>



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [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-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, 0 replies; 8+ 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] 8+ 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; 8+ 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] 8+ 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; 8+ 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] 8+ 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-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, 0 replies; 8+ 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] 8+ 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; 8+ 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] 8+ 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; 8+ 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] 8+ 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; 8+ 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] 8+ messages in thread

end of thread, other threads:[~2026-11-29  7:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v2 3/7] jump_label: Plumb bool apply through jump_label_update_key() 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
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 ` [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

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®