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 1/5] x86/alternatives: Keep text-poke batch sorted on insert
Date: Fri, 04 Dec 2026 11:27:52 -0700	[thread overview]
Message-ID: <20261204-jump-batch-261128-v3-1-2adda55a5d71@gmail.com> (raw)
In-Reply-To: <20261204-jump-batch-261128-v3-0-2adda55a5d71@gmail.com>

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 poke sites in
INT3 trap context, requiring vec[] to stay strictly sorted by poke
address. Currently smp_text_poke_batch_add() enforces ordering by
flushing the batch whenever a new address is lower than the tail entry.

ftrace submits its poke addresses in ascending order, so the queue fills
to capacity before flushing. But batching across multiple static keys
(such as dyndbg walking descriptors) submits poke addresses out of code
order, triggering a flush and IPIs after ~2 entries on average.

Because text_poke_array.vec[] is private under text_mutex until
smp_text_poke_batch_finish() publishes text_poke_array_refs,
out-of-order entries can be inserted into sorted position instead of
flushing early.

Inserting in sorted order also allows detecting duplicate addresses in
a submitted stream (which binary search also forbids), so when an
address is already queued we can flush the current batch to apply that
pending state first and start the next batch with the new update (e.g.
a "+p;-p" toggle by dyndbg, or a re-target by ftrace) without warning.

1. Test new pokes for a tail append first in text_poke_find_slot(),
   which replaces text_poke_addr_ordered(): when vec[] is empty or addr
   exceeds the tail entry, return nr_entries in one comparison (the
   ftrace fast path). Requiring addr to exceed the tail also catches
   adjacent duplicate addresses that the old check let through.

2. Otherwise, use binary search on vec[] to find the sorted insertion
   slot for addr; if addr is already queued, return false so
   smp_text_poke_batch_add() flushes the pending batch and starts the
   next batch with the new poke.

3. Pass the slot to __smp_text_poke_batch_add(), using memmove() to
   shift trailing entries up when inserting ahead of the tail (skipped
   on tail appends; ~1 KB moved on average within the 4 KB vec[] page).

Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
 arch/x86/kernel/alternative.c | 57 +++++++++++++++++++++++++++++++------------
 1 file changed, 41 insertions(+), 16 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index efbc11f8c336..c837678f379f 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 insn insn;
 	int ret, i = 0;
 
-	tpl = &text_poke_array.vec[text_poke_array.nr_entries++];
+	tpl = &text_poke_array.vec[pos];
+	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,42 @@ 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)
+	*pos = nr;
+	if (!nr || addr > text_poke_addr(&text_poke_array.vec[nr - 1]))
 		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;
 }
 
@@ -3070,9 +3090,14 @@ static bool text_poke_addr_ordered(void *addr)
  */
 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



  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 ` Jim Cromie via B4 Relay [this message]
2026-12-04 18:27 ` [PATCH v3 2/5] x86/jump_label: Sync arch_jump_label_transform_queue() with batch limit Jim Cromie via B4 Relay
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-1-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®