[PATCH v3 2/5] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit
From: Jim Cromie via B4 Relay
Date: Fri Dec 04 2026 - 13:28:30 EST
From: Jim Cromie <jim.cromie@xxxxxxxxx>
__jump_label_update() expects arch_jump_label_transform_queue() to
return false when the current architecture batch cannot accept another
poke, so the caller can apply the pending batch and queue that entry at
the start of a fresh batch. On x86, however,
arch_jump_label_transform_queue() calls smp_text_poke_batch_add(),
which silently flushes text_poke_array when it fills
(TEXT_POKE_ARRAY_MAX = 256) or hits an already-queued address, and
unconditionally returns true.
Hiding those flush boundaries is harmless when updating one static key
at a time, where key->enabled stays fixed across internal flushes.
Multi-key static_key_batch updates, however, defer publishing
key->enabled = 1 until the batch flushes: because multi-site (e.g.
inlined) keys fill the 256-entry text_poke_array ahead of the 256-key
queue, jump_label.c needs the false return so it can flush the pending
batch, publish completed key states, and start the next batch in
lockstep without extra runt flushes.
1. Extract smp_text_poke_batch_try_add() in alternative.c, returning
false without flushing when text_poke_array is full or addr is
already queued, and keep smp_text_poke_batch_add() as an
auto-flushing wrapper for callers such as ftrace_replace_code().
2. Call smp_text_poke_batch_try_add() in x86's
arch_jump_label_transform_queue() and return its result so
__jump_label_update() flushes and starts a fresh batch on boundary
hits.
Signed-off-by: Jim Cromie <jim.cromie@xxxxxxxxx>
---
arch/x86/include/asm/text-patching.h | 1 +
arch/x86/kernel/alternative.c | 34 ++++++++++++++++++++++++++++------
arch/x86/kernel/jump_label.c | 6 ++++--
3 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h
index ea09381070e8..de43b5266867 100644
--- a/arch/x86/include/asm/text-patching.h
+++ b/arch/x86/include/asm/text-patching.h
@@ -41,6 +41,7 @@ extern void *text_poke_set(void *addr, int c, size_t len);
extern int smp_text_poke_int3_handler(struct pt_regs *regs);
extern void smp_text_poke_single(void *addr, const void *opcode, size_t len, const void *emulate);
+bool smp_text_poke_batch_try_add(void *addr, const void *opcode, size_t len, const void *emulate);
extern void smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate);
extern void smp_text_poke_batch_finish(void);
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index c837678f379f..034a9f1d8568 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -3075,6 +3075,32 @@ static bool text_poke_find_slot(void *addr, int *pos)
return true;
}
+/**
+ * smp_text_poke_batch_try_add() -- queue a batched live-kernel text poke
+ * @addr: address to patch
+ * @opcode: opcode of new instruction
+ * @len: length to copy
+ * @emulate: instruction to be emulated
+ *
+ * Queue a patching request in sorted position for execution on the next
+ * smp_text_poke_batch_finish() call without finish()ing an existing batch.
+ *
+ * Return: true if queued, or false if the batch is full or @addr is already
+ * queued.
+ */
+bool __ref smp_text_poke_batch_try_add(void *addr, const void *opcode,
+ size_t len, const void *emulate)
+{
+ int pos;
+
+ if (text_poke_array.nr_entries == TEXT_POKE_ARRAY_MAX ||
+ !text_poke_find_slot(addr, &pos))
+ return false;
+
+ __smp_text_poke_batch_add(addr, opcode, len, emulate, pos);
+ return true;
+}
+
/**
* smp_text_poke_batch_add() -- update instruction on live kernel on SMP, batched
* @addr: address to patch
@@ -3090,14 +3116,10 @@ static bool text_poke_find_slot(void *addr, int *pos)
*/
void __ref smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate)
{
- int pos = 0;
-
- if (text_poke_array.nr_entries == TEXT_POKE_ARRAY_MAX ||
- !text_poke_find_slot(addr, &pos)) {
+ if (!smp_text_poke_batch_try_add(addr, opcode, len, emulate)) {
smp_text_poke_batch_finish();
- pos = 0;
+ __smp_text_poke_batch_add(addr, opcode, len, emulate, 0);
}
- __smp_text_poke_batch_add(addr, opcode, len, emulate, pos);
}
/**
diff --git a/arch/x86/kernel/jump_label.c b/arch/x86/kernel/jump_label.c
index a7949a54a0ff..4a7daea5ce6e 100644
--- a/arch/x86/kernel/jump_label.c
+++ b/arch/x86/kernel/jump_label.c
@@ -124,6 +124,7 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
enum jump_label_type type)
{
struct jump_label_patch jlp;
+ bool queued;
if (system_state == SYSTEM_BOOTING) {
/*
@@ -135,9 +136,10 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
mutex_lock(&text_mutex);
jlp = __jump_label_patch(entry, type);
- smp_text_poke_batch_add((void *)jump_entry_code(entry), jlp.code, jlp.size, NULL);
+ queued = smp_text_poke_batch_try_add((void *)jump_entry_code(entry),
+ jlp.code, jlp.size, NULL);
mutex_unlock(&text_mutex);
- return true;
+ return queued;
}
void arch_jump_label_transform_apply(void)
--
2.56.1