From: "Jürgen Groß" <jgross@suse.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [PATCH 3/3] x86/alternative: Patch a single alternative location only once
Date: Tue, 30 Sep 2025 09:33:25 +0200 [thread overview]
Message-ID: <bd595b07-5f00-420f-8bc0-4009237d040c@suse.com> (raw)
In-Reply-To: <20250930072643.GF3245006@noisy.programming.kicks-ass.net>
[-- Attachment #1.1.1: Type: text/plain, Size: 3585 bytes --]
On 30.09.25 09:26, Peter Zijlstra wrote:
> On Mon, Sep 29, 2025 at 01:29:47PM +0200, Juergen Gross wrote:
>> Instead of patching a single location potentially multiple times in
>> case of nested ALTERNATIVE()s, do the patching only after having
>> evaluated all alt_instr instances for that location.
>>
>> This has multiple advantages:
>>
>> - In case of replacing an indirect with a direct call using the
>> ALT_FLAG_DIRECT_CALL flag, there is no longer the need to have that
>> instance before any other instances at the same location (the
>> original instruction is needed for finding the target of the direct
>> call).
>>
>> - In case of nested ALTERNATIVE()s there is no intermediate replacement
>> visible. This avoids any problems in case e.g. an interrupt is
>> happening between the single instances and the patched location is
>> used during handling the interrupt.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>> V2:
>> - new patch
>> ---
>> arch/x86/kernel/alternative.c | 27 +++++++++++++++++++--------
>> 1 file changed, 19 insertions(+), 8 deletions(-)
>>
>> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
>> index 735cc017f2d3..ccf07131cd47 100644
>> --- a/arch/x86/kernel/alternative.c
>> +++ b/arch/x86/kernel/alternative.c
>> @@ -648,6 +648,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
>> u8 insn_buff[MAX_PATCH_LEN];
>> u8 *instr;
>> struct alt_instr *a, *b;
>> + unsigned int instances = 0;
>> + bool patched = false;
>>
>> DPRINTK(ALT, "alt table %px, -> %px", start, end);
>>
>> @@ -677,9 +679,13 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
>> * padding for all alt_instr entries for this site (nested
>> * alternatives result in consecutive entries).
>> */
>> - for (b = a+1; b < end && instr_va(b) == instr_va(a); b++) {
>> - u8 len = max(a->instrlen, b->instrlen);
>> - a->instrlen = b->instrlen = len;
>> + if (!instances) {
>> + for (b = a+1; b < end && instr_va(b) == instr_va(a); b++) {
>> + u8 len = max(a->instrlen, b->instrlen);
>> + a->instrlen = b->instrlen = len;
>> + }
>> + instances = b - a;
>> + patched = false;
>> }
>>
>> instr = instr_va(a);
>> @@ -692,14 +698,19 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
>> * - feature not present but ALT_FLAG_NOT is set to mean,
>> * patch if feature is *NOT* present.
>> */
>> - if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
>> - memcpy(insn_buff, instr, a->instrlen);
>> - optimize_nops(instr, insn_buff, a->instrlen);
>> - } else {
>> + if (!boot_cpu_has(a->cpuid) != !(a->flags & ALT_FLAG_NOT)) {
>> apply_one_alternative(instr, insn_buff, a);
>> + patched = true;
>> }
>>
>> - text_poke_early(instr, insn_buff, a->instrlen);
>> + instances--;
>> + if (!instances) {
>> + if (!patched) {
>> + memcpy(insn_buff, instr, a->instrlen);
>> + optimize_nops(instr, insn_buff, a->instrlen);
>> + }
>> + text_poke_early(instr, insn_buff, a->instrlen);
>> + }
>> }
>>
>> kasan_enable_current();
>
> I think you lost the optimize_nops() call for the patched case.
>
> That is, note how apply_one_alternative() does 0x90 padding, but then
> you only do optimize_nops() when !patched.
The call of optimize_nops() is part of text_poke_apply_relocation() when
patching, like without my series.
Juergen
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
next prev parent reply other threads:[~2025-09-30 7:33 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-29 11:29 [PATCH 0/3] x86/alternative: Make alternative patching more robust Juergen Gross
2025-09-29 11:29 ` [PATCH 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Juergen Gross
2025-10-14 8:42 ` [tip: x86/core] " tip-bot2 for Juergen Gross
2025-09-29 11:29 ` [PATCH 2/3] x86/alternative: Refactor apply_alternatives() Juergen Gross
2025-10-14 8:42 ` [tip: x86/core] " tip-bot2 for Juergen Gross
2025-09-29 11:29 ` [PATCH 3/3] x86/alternative: Patch a single alternative location only once Juergen Gross
2025-09-30 7:26 ` Peter Zijlstra
2025-09-30 7:33 ` Jürgen Groß [this message]
2025-09-30 7:39 ` Peter Zijlstra
2025-09-30 8:04 ` Jürgen Groß
2025-10-14 8:42 ` [tip: x86/core] " tip-bot2 for Juergen Gross
2025-10-14 12:59 ` Borislav Petkov
2025-10-14 13:25 ` Borislav Petkov
2025-10-14 14:13 ` Jürgen Groß
2025-10-14 14:17 ` Borislav Petkov
2025-10-14 14:18 ` Peter Zijlstra
2025-10-14 14:26 ` Juergen Gross
2025-10-14 14:29 ` Borislav Petkov
2025-10-14 14:08 ` Jürgen Groß
2025-10-14 14:12 ` Borislav Petkov
2025-10-14 14:22 ` Jürgen Groß
2025-10-14 14:26 ` Borislav Petkov
2025-10-14 14:31 ` Jürgen Groß
2025-10-14 14:39 ` Borislav Petkov
2025-10-14 14:46 ` Jürgen Groß
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=bd595b07-5f00-420f-8bc0-4009237d040c@suse.com \
--to=jgross@suse.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--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®