From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796408903; b=0q7pqsFSidv0acGAqP/GU0PbfLUVcUCezuUUGCTgalbGMMcZnagq0OJ5YjjnGHn9y1ne uY3guh/8LL8gAGxJm8NE6bjn9HPWWIYAesbVnzmuWBBM8WfORUBXAhZbf78DjzW0KjpKR Q/InP5qYQDrRuO4aOrPMQgZ6HbkhSXmmggam7JgqyWshb6CDUNT3wI96W1lTTOC2ZbA+Q GopZ0RiF+HYfXwYHwRC/kYQCy1n40BtBM/ckzi+zNhEdvx4iTpm3s3qN8fEOulAL6j48r OiP+GupH23MOcqCgUQSRp1RBIGMwRFJB4XRcsctiF2RblxBh0Uys3Hknu+lB9gBNO8w== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796408903; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=q+xaRZFQ3M3wtzYY7eHFOPkD5F6Pfep6UrP1BAyYWjo=; b=Z2K12yIJIyvPVTLQktYmBduD0R1LRYx4ZrqExMJRWmV3UWB3PyqM+RJvdWOOLo6codcQ 6+CGo3zrSq7QXpq2pIoSk9YXSEyf1nC21uIY/r3armfJCnPb8xzxBmdv+WWO0aRP6HhpG eqVVAsK6qW5hozafRHym/3DfQo5OxJG4gqI8VBH1d256bIL2C6Jwki5UvAJd2IC/W33d6 e219/nguSF6+SDzHIr2VwQTw4J2rRwZXhgGWnw0crcH0aW1jWLTetQeWJEB+uEnHqXGJr SbSYjjqC952y1hSi+6al3K3/MmlLnyw772R7+lOD6ZFJZp/6B7Lx+6GsNMxcKNEXJ+A== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mIm7GhFE; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2AE8606A330 for ; Fri, 4 Dec 2026 18:28:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mIm7GhFE Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796408903; b=UdPEmaPUpSpOLSGhl/aMpoIc3JCc3k6Gmq1vQqJ9csQZ5h6HT46lB6dJCI4rNKThESLx uekSUpFklEVukGNMg1S5Eisti65BOtYiRHvL+TzXo2CTEkWqfgmqSG2JCL9niLNW6VhIh qjQGRWAC7yoPZx9QnprglhA6106+d070tpk2Em7j2vCgO9TMBGMnBg4Iu172YN3GysEVn HAidvRZT9mGVQxWitpxoHbbdmvblI46p3smuBlMRr/Dicgz+Wh5VwYUWT9lieJG2EiNAi SGv6OxMb08V8I4h4uVKee+teuyHQJ4TL/AJ2jti2Vv4RXqFGdRAGpoTwTBNOHUNswNw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796408903; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=q+xaRZFQ3M3wtzYY7eHFOPkD5F6Pfep6UrP1BAyYWjo=; b=VmqNcYVr4duqY8BqdyN+L0nuj2qc7gelnKEHgtaQ2G8hf97AjZ6fzgueSJPdTIf/YvqE pqB49RPToh+E2lvLabRURyNCJrVOpEN6LLNmL1LeWGVsyUYKZB/yJpASgS7+dvth9WAUZ e2b2ISuJvxepLjIrN8+YZ/xYuyXzzWH8+T7BxbpxvWQiMXB9te8YK4lE46OBAeGdSsQes C+IO7KDj+wk8Ax+7wDyXhrPfRtTWnGFULL7/PeG9GO7Jgjgd9JiAOTSiBp0u5ZPQPxpct 8MAnjjOhClSmtBKEQxJ14FV68D169W1bEkGts36pf2zLSXnkjJJpqdW5UsltvP+vLjA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id DA6AA1F00936; Fri, 4 Dec 2026 18:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796408903; bh=q+xaRZFQ3M3wtzYY7eHFOPkD5F6Pfep6UrP1BAyYWjo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=mIm7GhFEPox9atTKyYhox1zhCHmpWqmlkztzdMTMTuiDxXfjLbhowd0V0Dq4eT1n5 /977jAXuTXbXpFtq+pO5WRUcszfcL79M/sy6pPxNrSM2D4p/2N5u82Ivof1Eh7wH2e ggrLZ83oZa0SFwrFc3F5NJMvGQbsoJq3hYzBDKw8MTW/VXWC7JdshnW9qWTBf5VH24 rebSBTqr04qLrFRfLgE0qx0mraF7osjoPij4ZDVNqatshZzsBCW/CFA0OT8R7AFuKV WmRXkcnTE0kGnsYpmozFCpbRZscvrzw9B3Y1EXBf/IQRk12mf54cIejQ+Rs4vX7RRa 1qpYTgIqkmuaw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 67DD14DBEA; Fri, 4 Dec 2026 18:28:22 +0000 (UTC) From: Jim Cromie via B4 Relay Date: Fri, 04 Dec 2026 11:27:52 -0700 Subject: [PATCH v3 1/5] x86/alternatives: Keep text-poke batch sorted on insert X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261204-jump-batch-261128-v3-1-2adda55a5d71@gmail.com> References: <20261204-jump-batch-261128-v3-0-2adda55a5d71@gmail.com> In-Reply-To: <20261204-jump-batch-261128-v3-0-2adda55a5d71@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=1796408901; l=5381; i=jim.cromie@gmail.com; s=20260203; h=from:subject:message-id; bh=TZ8HKn3UVuaE6kj3AkuTTHHiQ+sb4n1neYei6BdCFzQ=; b=az0uPz7ngZOzVLFQAZcodOxeY312NfF2KDbpYF+01HCWmgZ14fa2/ctzhMzDXpMnnkN3/vAkC nG7uFyuo5Q6CIb9gZl2oLvMs5pj4BquGN8XubOD/ouZ5Vk5UfwUvOeq 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 X-Original-From: Jim Cromie Reply-To: jim.cromie@gmail.com Content-Transfer-Encoding: 7bit From: Jim Cromie 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 --- 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