mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jim Cromie via B4 Relay <devnull+jim.cromie.gmail.com@kernel.org>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	 Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	 x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	 Peter Zijlstra <peterz@infradead.org>,
	Josh Poimboeuf <jpoimboe@kernel.org>,
	 Jason Baron <jbaron@akamai.com>,
	Alice Ryhl <aliceryhl@google.com>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Ard Biesheuvel <ardb@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, Jim Cromie <jim.cromie@gmail.com>
Subject: [PATCH v3 2/5] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit
Date: Fri, 04 Dec 2026 11:27:53 -0700	[thread overview]
Message-ID: <20261204-jump-batch-261128-v3-2-2adda55a5d71@gmail.com> (raw)
In-Reply-To: <20261204-jump-batch-261128-v3-0-2adda55a5d71@gmail.com>

From: Jim Cromie <jim.cromie@gmail.com>

__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@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 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



  parent reply	other threads:[~2026-12-04 18:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:27 [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 1/5] x86/alternatives: Keep text-poke batch sorted on insert Jim Cromie via B4 Relay
2026-12-04 18:27 ` Jim Cromie via B4 Relay [this message]
2026-12-04 18:27 ` [PATCH v3 3/5] jump_label: Add static_key_batch API Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 4/5] dyndbg: Batch static branch updates across queries Jim Cromie via B4 Relay
2026-12-04 18:27 ` [PATCH v3 5/5] dyndbg: Batch classmap param bit-changes into multi-query Jim Cromie via B4 Relay
2026-12-04 20:29 ` [PATCH v3 0/5] jump_label, x86, dyndbg: Batch multi-key static branch updates Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204-jump-batch-261128-v3-2-2adda55a5d71@gmail.com \
    --to=devnull+jim.cromie.gmail.com@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=aliceryhl@google.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jbaron@akamai.com \
    --cc=jim.cromie@gmail.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®