jump_label, x86, dyndbg: Batch multi-key static branch updates
Subsystems (4)
Jim Cromie
maintainerv2 cover jump_label, x86, dyndbg: Batch multi-key static branch updates
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.
---
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/[email protected]/
---
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,
Signature · 1 line
Jim Cromie <[email protected]>
Jim Cromie
maintainerPATCH v2 1/7 x86/alternatives: Keep text-poke batch sorted on insert
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.
--- arch/x86/kernel/alternative.c | 63 ++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 18 deletions(-)
arch/x86/kernel/alternative.c +45 -18
@@ -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); }/**
Signature · 1 line
2.56.1
Jim Cromie
maintainerPATCH v2 2/7 x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit
In reply to Jim Cromie__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().
--- 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(-)
arch/x86/include/asm/text-patching.h +1 -0
@@ -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 );
arch/x86/kernel/alternative.c +28 -6
@@ -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); }/**
arch/x86/kernel/jump_label.c +4 -2
@@ -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 )
Signature · 1 line
2.56.1
Jim Cromie
maintainerPATCH v2 3/7 jump_label: Plumb bool apply through jump_label_update_key()
In reply to Jim Cromie__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.
--- kernel/jump_label.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-)
kernel/jump_label.c +20 -22
@@ -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
Signature · 1 line
2.56.1
Jim Cromie
maintainerPATCH v2 4/7 jump_label: Add batching API for queued static key updates
In reply to Jim CromieBuilt-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.
--- include/linux/jump_label.h | 22 +++++ kernel/jump_label.c | 240 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 246 insertions(+), 16 deletions(-)
include/linux/jump_label.h +22 -0
@@ -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)
kernel/jump_label.c +224 -16
313 lines not on this page · raw message
Signature · 1 line
2.56.1
Jim Cromie
maintainerPATCH v2 5/7 dyndbg: Use queued static-key batching in ddebug_change()
In reply to Jim CromieToggling "+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.
--- lib/dynamic_debug.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
lib/dynamic_debug.c +5 -2
@@ -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" );
Signature · 1 line
2.56.1
Jim Cromie
maintainerPATCH v2 6/7 dyndbg: Batch classmap param bit-changes into multi-query
In reply to Jim Cromieddebug_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.
--- lib/dynamic_debug.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)
lib/dynamic_debug.c +23 -11
@@ -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; }
Signature · 1 line
2.56.1
Jim Cromie
maintainerPATCH v2 7/7 dyndbg: Hoist lock and static_key_batch across queries
In reply to Jim Cromieddebug_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().
--- lib/dynamic_debug.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-)
lib/dynamic_debug.c +23 -21
@@ -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);
Signature · 1 line
2.56.1