From: Jim Cromie Date: Sun, 29 Nov 2026 00:44:15 -0700 Subject: [PATCH v2 2/7] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-jump-batch-261128-v2-2-b1829d2429f0@gmail.com> References: <20261129-jump-batch-261128-v2-0-b1829d2429f0@gmail.com> In-Reply-To: <20261129-jump-batch-261128-v2-0-b1829d2429f0@gmail.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Alice Ryhl , Steven Rostedt , Ard Biesheuvel , Andrew Morton Cc: linux-kernel@vger.kernel.org, Jim Cromie X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795938258; l=5354; i=jim.cromie@gmail.com; s=20260203; h=from:subject:message-id; bh=4QCUCvL2QYZb/Z25ur1ddc/dZp66WsPKkxTfYhNKlM4=; b=PpjxTvNFHqAkOHi5Ksr3wJKpbOpJfsYbgFCImah/rDHhS9R2lMfziKVBIqdIZRn/SYDB0PAxA 2CWIEK/XNOHAbcYIuVKBBfaXx3W+wEg8eKwjBl5orQrNlKN1aT2E0oJ X-Developer-Key: i=jim.cromie@gmail.com; a=ed25519; pk=C6E5ODlPQo7ZBynATXH9wg7K6HxP0pIXyf4s38Qw0XE= X-Endpoint-Received: by B4 Relay for jim.cromie@gmail.com/20260203 with auth_id=958 List-Id: B4 Relay Submissions __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 --- 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