From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=216.40.44.12 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796055208; b=Dqrwe+dhkUDq5P9B7mkIAFKMx7+W/iZOhF342t4MtwFw7AYoT7qI4ob7I4zkBsLQ9Sue 7Xlp7ZYbBzjzoIpW2W7RJvbUhq49HVsR+nANY0kXOoU1oLLueeUtAsIqjslfo9o6QMvX8 ndJbjaY3zEKPO2HMuhr8X3MtZkbYeqXvfVAs80TRGJlZEVo6zqdotN8VJnH3yc7herQ7x uP01EZTmCgTGmlFEcBrUsfbjmUt3pndqS8RTZTTUIAuQgmQeMrdCoUPS+Xp+Z/wnKjebr H6PfwAx+xbfzsm7M1T/posLiAETh+PNShQrE3RwxFS/js3X1/Il7bWdwokD4tGFWwJQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796055208; h=DMARC-Filter:Received:Received:Date:From:To:Cc:Subject:Message-ID: In-Reply-To:References:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding:X-Rspamd-Server:X-Rspamd-Queue-Id: X-Stat-Signature:X-Spam-Status:X-Session-Marker:X-Session-ID: DKIM-Signature:X-HE-Tag:X-HE-Meta; bh=a3BVLon2gJBjhkQ/JTwsW+W3C32UrjpyZoEBkKb7nr4=; b=k+qiPO6yWLUfWb/ARONvGpecVFZDSCdTXJeLS16ZXdwWJtvWqVUmSc2dt/1Mkq+h5Hsy i7TYtQmxBzudQZKzZmwCHgRPSdQ1JVG8J3BEuCdb7XZTExdJ0Y0NGXRtCza5HVwZtCUY6 JGrx/DkHeth1fp1w/ryqLRV2WMRGVtA7XJGK1PLuucw5S/gKJnYcwM8gfGmxBjPCOi886 9tmMpPiqmo27rGgl3v9wQoQY8mKMtHQMJEPw9bopd4JWkoROrvokVI6NlMivNuzHKMBWV lO9c3L42osAWYRf4ufti/ZGCgRdCY/akYwFsTRsgi0j9Lnys5O7OAAhevHyVAbMJT9A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=goodmis.org; spf=pass smtp.mailfrom=goodmis.org; dkim=pass header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=xoGsi684; arc=none smtp.remote-ip=216.40.44.12 Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) (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.subspace.kernel.org (Postfix) with ESMTPS id DF40F606A31C for ; Mon, 30 Nov 2026 16:13:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DF40F606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=goodmis.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=goodmis.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=xoGsi684 Received: from omf19.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 14D16A0993; Mon, 30 Nov 2026 16:13:20 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: rostedt@goodmis.org) by omf19.hostedemail.com (Postfix) with ESMTPA id E955A20028; Mon, 30 Nov 2026 16:13:16 +0000 (UTC) Date: Mon, 30 Nov 2026 11:13:15 -0500 From: Steven Rostedt To: Jim Cromie via B4 Relay Cc: jim.cromie@gmail.com, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Alice Ryhl , Ard Biesheuvel , Andrew Morton , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/7] x86/alternatives: Keep text-poke batch sorted on insert Message-ID: <20261130111315.1cbcd11c@gandalf.local.home> In-Reply-To: <20261129-jump-batch-261128-v2-1-b1829d2429f0@gmail.com> References: <20261129-jump-batch-261128-v2-0-b1829d2429f0@gmail.com> <20261129-jump-batch-261128-v2-1-b1829d2429f0@gmail.com> X-Mailer: Claws Mail 3.20.0git84 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspamout04 X-Rspamd-Queue-Id: E955A20028 X-Stat-Signature: 5bfyomdssdcces7uz37gxgfn6s5cox9y X-Session-Marker: 726F737465647440676F6F646D69732E6F7267 X-Session-ID: U2FsdGVkX1/15KK1YvtLsFO1viBjGTZ+vum4XPtSGu8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=goodmis.org; h=date:from:to:cc:subject:message-id:in-reply-to:references:mime-version:content-type:content-transfer-encoding; s=dkim1; bh=a3BVLon2gJBjhkQ/JTwsW+W3C32UrjpyZoEBkKb7nr4=; b=xoGsi6848EKepfEr8sdOAOsVVvG2wt7XwvVY/TgxpOdJevaX5SshnXbQh+wljyKVShQw74o/Jc4Q/oyVe+skOT/8W3QL+NyzxSd8aZNmG9hOikmlKrjEV3hsg0p0zAhbx39WEBCi+fq2wcqZi8g+scl1Qxcy+Xg/I8h1t4vIUXY= X-HE-Tag: 1796055196-26830 X-HE-Meta: U2FsdGVkX1/RN0vR4JVXJbX/uPOdGR5H+43/hdygHUR5TBY6Mzbkr4jufl7CqWb13+oWz60DpQ2X9IFSXGNu+Y+vIBr9/b7MvpSQXZVptC5g74GtyfFUr7PBASAbUJr7LmDaLytcKtA0YnmOP5DdodPfkoeedPO2PPxn+eCb8BJQnZUJ7Ptp7G7udsVlawUAWQjwio9VciLQjogaeWPRLY1baqxuFy9/RqO/uVMBUP3kDZL5RMtKYyBgLA5h1sn15gUcAYVuCSoKW4ygpd6PgqMqDurh/8xR5wjGwiTt51K+GWn8XPPMjKEAoOUahCivwjXuYDwHuYwF/RTWfT3Oiz70HvQyNXb3UqvRi9xeB9FeQXabHY+rBvK3JZco5EMH On Sun, 29 Nov 2026 00:44:14 -0700 Jim Cromie via B4 Relay wrote: Please clean up the AI output because AI sucks at writing control flow in a human readable way. The below is absolute garbage for understanding what is going on. > 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. I mean mentioning "(*pos = nr)" after saying "in one comparison" is very confusing. It makes no sense without reading the code. The change log is to understand the code, not the other way around. I'm really hating the AI change logs because I have to read the code to reverse engineer what the change log is talking about. > 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. You really understand what the above is saying? > 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. When submitting AI created patches, update the wording better. For example the above should have been: [ and start with 1. not 0. as this isn't a C array, as well as space them ] 1. Rename text_poke_addr_order() to text_poke_find_slot(). Update the index to the nr_entries immediately and return if either there are no current entries or the addr is greater than the tail. This catches adjacent addr == tail duplicates missed by just updating if the tail is greater than the addr. 2. For out-of-order entries, binary search the vec[] using the text_poke_addr() pointer. If found, return false so that smp_text_poke_batch_add() calls finish() before queuing it. 3. In __smp_text_poke_batch_add() use memmove to shift the vec[] array to add the new entry. This moves ~1 KB on average. See how much easier it is to read. The above translation between AI slop output to human readable messages is up to the submitter. Not the maintainer or reviewer. > > 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. I personally do not think the "unchanged" section is useful. I have no idea why AI wants to tell us this. > 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]; Why make the assignment here and not just replace the assignment below but without the nr_entries update? > 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) Please do not add line breaks to code you do not touch. If the maintainer had it that way, it may be what they prefer. -- Steve > { > - 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); > } > > /**