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=1795938261; b=01bJ5ryR2OOYq3yZH8M/tBAUXU6CyM2XEf41tCRNWNSeOH9MukA2SFTlVMCkfMUVjVwZ U06xMAyql7D4KJUutTwBIM3oVtmtlfNAJlGjMO5OT+W8Tg+TBCmAKHdDpVQvSF/jXlk1/ rlgOca4+5Evgqqb3UvLfwDXxnYt1NCBJiGru7fLB+bWfU/rtaYVRSY1e5OOeon5FCUJU0 1hpipxM1EqmTTLwUeUYvRJbs0FiS+T5ciLFduw1s6H19nMb7xVJ/k3omo3pzspCShId1x rPZu3nky8J27FBJ0VcQaHgGvT/7eBMCQ/ObplQqTJHjvGrWqzsldRuN9ACeZLSUhKAQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795938261; 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=aqiIiHqsa/kkltpNO56i0+SBGZ48GD8/yeZK2OEDKxE=; b=EzCijSt3PlJcleKncjzioV5WdvxAsuLIffJ38Xyx06CFmnOnusFE3VYmmEGg5y2FjUZK Qd6ch46LVnklUJVVE0ss4xyudg7JbLeOQYxxjqn32xpl0lDUSlE5Do4QdSzqXnnwb8y9J HsQ3M8eDF/EyXyTz0JihV2aqSEyWk4HlFEpCZb1vOycN/0+zeai9KBo9PNNO1zI7eEvP2 N25+v7AnTWlLkbQExQ0yX0KZnMuRDZN55+L9kAYJ1J2hrOCLC4ncU1IovdtOEjz/j3Wcu z1klfcR0ab2NXO5HwvR0d8ABmtHEwsDEy4o+GRqxJE8JHPMNCXhv8s/i4TjK6FWmPzg== 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=aZzxnM2j; 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 06E72606A32D for ; Sun, 29 Nov 2026 07:44:20 +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=aZzxnM2j 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=1795938260; b=0h8yJfthwEsKA3xQkWn6Kv1WIMxRS89GkRiVSeZ17YaquDH/OWcf/C/vdU6+j+tsB9mZ kk3HOMI8WPxUAAKmniRw70NC4eW7VNtu9glBn841HiDNo4B+oWNkh7ao4gp6pF5KN8s54 1TLc5L5H0TZyUpRRhwzfT3oqjwpkmFKaQkRSLeLU+UHYtXrtYLjeeJAacIN8xOc8CTbMw GdXD0IJX3ZX/n56k6Sw1RySoAkwCnXu2E2AWdfIWqu3ElyxZEciY1zToykKGA6HKyAR5r PNajGvbsuvrjdmHUJvipIcSPFCnv4Ieso3Tby5TiOrdBXNnQbU5e4QpRmcmzK96D45Q== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795938260; 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=aqiIiHqsa/kkltpNO56i0+SBGZ48GD8/yeZK2OEDKxE=; b=fd8UANWU9rcLLDSYi8Nn24b5P7VPjpo+wLYhb2PaYiT1JJM819u/2h7qGYLpLf3maboI EM2WoF94DnhH9z6+xYJAhbPRlqsG0/PcHfGRjVXC3QAYFWcfbUovMJKoAs4bnYiyMYA0i KqrsHpOFoGi5GFHmoRh7mpu0o1EeEh4f5cChv8QDX0szmFA8bYuaIl62dQuePHf6bgE3x /pdub2IrsAgeXiPITxmAjZsi8BzoWTJMuNQiGtO5eWFgSx1fDIP2YLLhvJzWYPT4iiVMD co4BS3anXXsRJyl1FfMUDl1TD0ncxXlB1s6GEH0u9E1hD7fLGACvP4/Rzu2SynH9yjg== 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 337FF1F000C1; Sun, 29 Nov 2026 07:44:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795938260; bh=aqiIiHqsa/kkltpNO56i0+SBGZ48GD8/yeZK2OEDKxE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=aZzxnM2jEBe2hK8aQCUF4yvvrkAmYJeyNN3kIZC8Lmc5VD8c+ZDApEBxsZ0bcpaSc rkc1fKtGhyTOO8sQJeRenZg7YFYxZCw2/rWwCa59TOBsvflq3jH78fwWzLcP7/pJpS nxnoWb4CIinwkx+baFfWthgIfKi/G22tUCH33Vu8NVdYXN4E/JwH00cneSyiziQTMS KPwjAONlCopOtVGB1vip/KtULvUK7AUE3bbHq8yONMv54P78KDCprEZyfk/sBWi2jD B4mm3ixlIaLM/08n13CI0bqjREvRr8TlIHfCqH/Jp0bxlB/klG6XnHVS0mA9CQg494 tWuvDzzIo/Xeg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id AC2A64E3E0; Sun, 29 Nov 2026 07:44:18 +0000 (UTC) From: Jim Cromie via B4 Relay Date: Sun, 29 Nov 2026 00:44:14 -0700 Subject: [PATCH v2 1/7] 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: <20261129-jump-batch-261128-v2-1-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=5737; i=jim.cromie@gmail.com; s=20260203; h=from:subject:message-id; bh=8HTPl3UeIymFvjmeIWi78Ril9sQsMX0xjCUC4vvbNUE=; b=uWi88GNz2KeLh/grcqY3p1ltBP3k3AGI7buYs7LWhfHmRLUqqYmKuOTTzOj/OrDe58d9BZAXq aXYNrTj1zGaC0j8Pod83kWfJTJSIpLIm+HZICqU1ZUzmWejZ9cYz62F 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 patch sites in INT3 trap context, requiring vec[0 .. nr_entries - 1] to be strictly sorted by patch address with unique entries. Currently smp_text_poke_batch_add() enforces that invariant by calling smp_text_poke_batch_finish() whenever a new address is lower than the tail entry. For ftrace and single-key jump_label updates, patch addresses arrive in ascending order, so the queue fills to capacity before finish()ing. Multi-key callers such as dyndbg, however, walk descriptors rather than code order; compiler and linker layout scramble patch addresses enough to trigger finish()ing after ~2 entries on average. Since vec[] is private under text_mutex until finish() publishes text_poke_array_refs, out-of-order entries can be inserted into sorted position instead of finish()ing early. Sorting on insert also detects duplicate addresses immediately, so add() can finish() before queueing a second poke to the same @addr. 0. Replace text_poke_addr_ordered() with text_poke_find_slot(addr, &pos). Fast-path '!nr || addr > tail' in one comparison (*pos = nr), which also catches adjacent addr == tail duplicates missed by the old '>' check. 1. On out-of-order arrivals, binary-search vec[] (<= 8 steps for 256 entries) using 64-bit text_poke_addr() pointers to match patch_cmp() across core and module text. If @addr is already present, return false so smp_text_poke_batch_add() finish()es before queueing it. 2. In __smp_text_poke_batch_add(), shift vec[pos .. nr_entries - 1] up one slot via memmove() when pos < nr_entries; vec[] fits in one 4 KB page, moving ~1 KB on average. What's Unchanged: 0. Pre-sorted callers -- ftrace_replace_code() and single-key jump_label updates -- take the 1-comparison 'addr > tail' fastpath with zero binary searches and zero memmove() calls. 1. smp_text_poke_batch_finish(), smp_text_poke_int3_handler(), and struct smp_text_poke_array are unchanged. Signed-off-by: Jim Cromie --- arch/x86/kernel/alternative.c | 63 ++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index efbc11f8c336..d924c3e891b6 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 smp_text_poke_loc *tpl = &text_poke_array.vec[pos]; struct insn insn; int ret, i = 0; - tpl = &text_poke_array.vec[text_poke_array.nr_entries++]; + 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,43 @@ 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) + if (!nr || addr > text_poke_addr(&text_poke_array.vec[nr - 1])) { + *pos = nr; 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; } @@ -3068,11 +3089,17 @@ static bool text_poke_addr_ordered(void *addr) * execution. All pending patching requests will be executed on the next * smp_text_poke_batch_finish() call. */ -void __ref smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate) +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