* [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once
@ 2025-11-19 16:04 Juergen Gross
2025-11-19 16:04 ` [PATCH v4 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Juergen Gross
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Juergen Gross @ 2025-11-19 16:04 UTC (permalink / raw)
To: linux-kernel, x86
Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin
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.
Changes in V2:
- complete rework (Boris Petkov)
Changes in V3:
- split former V2 patch into 2 by introducing a helper function (Boris Petkov)
- repost the small cleanup patch 1 which was taken before, but has somehow
vanished from the tip x86/alternative branch (it is still in the tip
master branch, but I couldn't find it in any other tip branch).
Changes in V4:
- use 3 helpers instead of 1 (Boris Petkov)
Juergen Gross (3):
x86/alternative: Drop not needed test after call of alt_replace_call()
x86/alternative: Use helper functions for patching alternatives
x86/alternative: Patch a single alternative location only once
arch/x86/kernel/alternative.c | 147 ++++++++++++++++++++--------------
1 file changed, 87 insertions(+), 60 deletions(-)
--
2.51.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v4 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() 2025-11-19 16:04 [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once Juergen Gross @ 2025-11-19 16:04 ` Juergen Gross 2025-11-19 16:04 ` [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives Juergen Gross ` (2 subsequent siblings) 3 siblings, 0 replies; 8+ messages in thread From: Juergen Gross @ 2025-11-19 16:04 UTC (permalink / raw) To: linux-kernel, x86 Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin alt_replace_call() will never return a negative value, so testing the return value to be less than zero can be dropped. This makes it possible to switch the return type of alt_replace_call() and the type of insn_buff_sz to unsigned int. Signed-off-by: Juergen Gross <jgross@suse.com> --- V2: - new patch --- arch/x86/kernel/alternative.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 8ee5ff547357..4f3ea50e41e8 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -559,7 +559,7 @@ EXPORT_SYMBOL(BUG_func); * Rewrite the "call BUG_func" replacement to point to the target of the * indirect pv_ops call "call *disp(%ip)". */ -static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) +static unsigned int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) { void *target, *bug = &BUG_func; s32 disp; @@ -643,7 +643,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, * order. */ for (a = start; a < end; a++) { - int insn_buff_sz = 0; + unsigned int insn_buff_sz = 0; /* * In case of nested ALTERNATIVE()s the outer alternative might @@ -683,11 +683,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, memcpy(insn_buff, replacement, a->replacementlen); insn_buff_sz = a->replacementlen; - if (a->flags & ALT_FLAG_DIRECT_CALL) { + if (a->flags & ALT_FLAG_DIRECT_CALL) insn_buff_sz = alt_replace_call(instr, insn_buff, a); - if (insn_buff_sz < 0) - continue; - } for (; insn_buff_sz < a->instrlen; insn_buff_sz++) insn_buff[insn_buff_sz] = 0x90; -- 2.51.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives 2025-11-19 16:04 [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once Juergen Gross 2025-11-19 16:04 ` [PATCH v4 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Juergen Gross @ 2025-11-19 16:04 ` Juergen Gross 2026-01-02 13:00 ` Borislav Petkov 2025-11-19 16:04 ` [PATCH v4 3/3] x86/alternative: Patch a single alternative location only once Juergen Gross 2025-12-15 8:25 ` [PATCH v4 0/3] " Juergen Gross 3 siblings, 1 reply; 8+ messages in thread From: Juergen Gross @ 2025-11-19 16:04 UTC (permalink / raw) To: linux-kernel, x86 Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin Tidy up apply_alternatives() by moving the main patching action of a single alternative instance into 3 helper functions: - analyze_patch_site() for selection whether patching should occur or not and to handle nested alternatives. - prep_patch_site() for applying any needed relocations and issuing debug prints for the site. - patch_site() doing the real patching action, including optimization of any padding NOPs. In prep_patch_site() use __apply_relocation() instead of text_poke_apply_relocation(), as the NOP optimization is now done in patch_site() for all cases. Suggested-by: Borislav Petkov <bp@alien8.de> Signed-off-by: Juergen Gross <jgross@suse.com> --- V3: - new patch V4: - further split coding in more helpers (Borislav Petkov) --- arch/x86/kernel/alternative.c | 140 +++++++++++++++++++++------------- 1 file changed, 85 insertions(+), 55 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 4f3ea50e41e8..afcc681ff3bd 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -604,6 +604,86 @@ static inline u8 * instr_va(struct alt_instr *i) return (u8 *)&i->instr_offset + i->instr_offset; } +struct patch_site { + u8 *instr; + struct alt_instr *alt; + u8 buff[MAX_PATCH_LEN]; + u8 len; +}; + +static void __init_or_module analyze_patch_site(struct patch_site *ps, + struct alt_instr *p, struct alt_instr *end) +{ + struct alt_instr *r; + + /* + * In case of nested ALTERNATIVE()s the outer alternative might add + * more padding. To ensure consistent patching find the max padding for + * all alt_instr entries for this site (nested alternatives result in + * consecutive entries). + */ + ps->instr = instr_va(p); + ps->len = p->instrlen; + for (r = p+1; r < end && instr_va(r) == ps->instr; r++) { + ps->len = max(ps->len, r->instrlen); + p->instrlen = r->instrlen = ps->len; + } + + BUG_ON(ps->len > sizeof(ps->buff)); + BUG_ON(p->cpuid >= (NCAPINTS + NBUGINTS) * 32); + + /* + * Patch if either: + * - feature is present + * - feature not present but ALT_FLAG_NOT is set to mean, + * patch if feature is *NOT* present. + */ + if (!boot_cpu_has(p->cpuid) == !(p->flags & ALT_FLAG_NOT)) + ps->alt = NULL; + else + ps->alt = p; +} + +static void __init_or_module prep_patch_site(struct patch_site *ps) +{ + struct alt_instr *p = ps->alt; + u8 buff_sz; + u8 *repl; + + if (!p) { + /* Nothing to patch, use original instruction. */ + memcpy(ps->buff, ps->instr, ps->len); + return; + } + + repl = (u8 *)&p->repl_offset + p->repl_offset; + DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x", + p->cpuid >> 5, p->cpuid & 0x1f, + ps->instr, ps->instr, ps->len, + repl, p->replacementlen, p->flags); + + memcpy(ps->buff, repl, p->replacementlen); + buff_sz = p->replacementlen; + + if (p->flags & ALT_FLAG_DIRECT_CALL) + buff_sz = alt_replace_call(ps->instr, ps->buff, p); + + for (; buff_sz < ps->len; buff_sz++) + ps->buff[buff_sz] = 0x90; + + __apply_relocation(ps->buff, ps->instr, ps->len, repl, p->replacementlen); + + DUMP_BYTES(ALT, ps->instr, ps->len, "%px: old_insn: ", ps->instr); + DUMP_BYTES(ALT, repl, p->replacementlen, "%px: rpl_insn: ", repl); + DUMP_BYTES(ALT, ps->buff, ps->len, "%px: final_insn: ", ps->instr); +} + +static void __init_or_module patch_site(struct patch_site *ps) +{ + optimize_nops(ps->instr, ps->buff, ps->len); + text_poke_early(ps->instr, ps->buff, ps->len); +} + /* * Replace instructions with better alternatives for this CPU type. This runs * before SMP is initialized to avoid SMP problems with self modifying code. @@ -617,9 +697,7 @@ static inline u8 * instr_va(struct alt_instr *i) void __init_or_module noinline apply_alternatives(struct alt_instr *start, struct alt_instr *end) { - u8 insn_buff[MAX_PATCH_LEN]; - u8 *instr, *replacement; - struct alt_instr *a, *b; + struct alt_instr *a; DPRINTK(ALT, "alt table %px, -> %px", start, end); @@ -643,59 +721,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, * order. */ for (a = start; a < end; a++) { - unsigned int insn_buff_sz = 0; - - /* - * In case of nested ALTERNATIVE()s the outer alternative might - * add more padding. To ensure consistent patching find the max - * 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; - } - - instr = instr_va(a); - replacement = (u8 *)&a->repl_offset + a->repl_offset; - BUG_ON(a->instrlen > sizeof(insn_buff)); - BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32); - - /* - * Patch if either: - * - feature is present - * - 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); - text_poke_early(instr, insn_buff, a->instrlen); - continue; - } - - DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x", - a->cpuid >> 5, - a->cpuid & 0x1f, - instr, instr, a->instrlen, - replacement, a->replacementlen, a->flags); - - memcpy(insn_buff, replacement, a->replacementlen); - insn_buff_sz = a->replacementlen; - - if (a->flags & ALT_FLAG_DIRECT_CALL) - insn_buff_sz = alt_replace_call(instr, insn_buff, a); - - for (; insn_buff_sz < a->instrlen; insn_buff_sz++) - insn_buff[insn_buff_sz] = 0x90; - - text_poke_apply_relocation(insn_buff, instr, a->instrlen, replacement, a->replacementlen); - - DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); - DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement); - DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr); + struct patch_site ps; - text_poke_early(instr, insn_buff, insn_buff_sz); + analyze_patch_site(&ps, a, end); + prep_patch_site(&ps); + patch_site(&ps); } kasan_enable_current(); -- 2.51.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives 2025-11-19 16:04 ` [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives Juergen Gross @ 2026-01-02 13:00 ` Borislav Petkov 0 siblings, 0 replies; 8+ messages in thread From: Borislav Petkov @ 2026-01-02 13:00 UTC (permalink / raw) To: Juergen Gross Cc: linux-kernel, x86, Thomas Gleixner, Ingo Molnar, Dave Hansen, H. Peter Anvin On Wed, Nov 19, 2025 at 05:04:19PM +0100, Juergen Gross wrote: > Tidy up apply_alternatives() by moving the main patching action of a > single alternative instance into 3 helper functions: > > - analyze_patch_site() for selection whether patching should occur or > not and to handle nested alternatives. > > - prep_patch_site() for applying any needed relocations and issuing > debug prints for the site. > > - patch_site() doing the real patching action, including optimization > of any padding NOPs. > > In prep_patch_site() use __apply_relocation() instead of > text_poke_apply_relocation(), as the NOP optimization is now done > in patch_site() for all cases. > > Suggested-by: Borislav Petkov <bp@alien8.de> > Signed-off-by: Juergen Gross <jgross@suse.com> > --- > V3: > - new patch > V4: > - further split coding in more helpers (Borislav Petkov) > --- > arch/x86/kernel/alternative.c | 140 +++++++++++++++++++++------------- > 1 file changed, 85 insertions(+), 55 deletions(-) Better... Some additional changes ontop: - put the comment over the loop where it belongs - name variables into something more descriptive than just a single letter. With that I think it is starting to look better/more readable. diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 49a9eed24fdd..66868ecdebd2 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -594,25 +594,27 @@ struct patch_site { }; static void __init_or_module analyze_patch_site(struct patch_site *ps, - struct alt_instr *p, struct alt_instr *end) + struct alt_instr *start, + struct alt_instr *end) { struct alt_instr *r; + ps->instr = instr_va(start); + ps->len = start->instrlen; + /* * In case of nested ALTERNATIVE()s the outer alternative might add * more padding. To ensure consistent patching find the max padding for * all alt_instr entries for this site (nested alternatives result in * consecutive entries). */ - ps->instr = instr_va(p); - ps->len = p->instrlen; - for (r = p+1; r < end && instr_va(r) == ps->instr; r++) { + for (r = start+1; r < end && instr_va(r) == ps->instr; r++) { ps->len = max(ps->len, r->instrlen); - p->instrlen = r->instrlen = ps->len; + start->instrlen = r->instrlen = ps->len; } BUG_ON(ps->len > sizeof(ps->buff)); - BUG_ON(p->cpuid >= (NCAPINTS + NBUGINTS) * 32); + BUG_ON(start->cpuid >= (NCAPINTS + NBUGINTS) * 32); /* * Patch if either: @@ -620,43 +622,43 @@ static void __init_or_module analyze_patch_site(struct patch_site *ps, * - feature not present but ALT_FLAG_NOT is set to mean, * patch if feature is *NOT* present. */ - if (!boot_cpu_has(p->cpuid) == !(p->flags & ALT_FLAG_NOT)) + if (!boot_cpu_has(start->cpuid) == !(start->flags & ALT_FLAG_NOT)) ps->alt = NULL; else - ps->alt = p; + ps->alt = start; } static void __init_or_module prep_patch_site(struct patch_site *ps) { - struct alt_instr *p = ps->alt; + struct alt_instr *alt = ps->alt; u8 buff_sz; u8 *repl; - if (!p) { + if (!alt) { /* Nothing to patch, use original instruction. */ memcpy(ps->buff, ps->instr, ps->len); return; } - repl = (u8 *)&p->repl_offset + p->repl_offset; + repl = (u8 *)&alt->repl_offset + alt->repl_offset; DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x", - p->cpuid >> 5, p->cpuid & 0x1f, + alt->cpuid >> 5, alt->cpuid & 0x1f, ps->instr, ps->instr, ps->len, - repl, p->replacementlen, p->flags); + repl, alt->replacementlen, alt->flags); - memcpy(ps->buff, repl, p->replacementlen); - buff_sz = p->replacementlen; + memcpy(ps->buff, repl, alt->replacementlen); + buff_sz = alt->replacementlen; - if (p->flags & ALT_FLAG_DIRECT_CALL) - buff_sz = alt_replace_call(ps->instr, ps->buff, p); + if (alt->flags & ALT_FLAG_DIRECT_CALL) + buff_sz = alt_replace_call(ps->instr, ps->buff, alt); for (; buff_sz < ps->len; buff_sz++) ps->buff[buff_sz] = 0x90; - __apply_relocation(ps->buff, ps->instr, ps->len, repl, p->replacementlen); + __apply_relocation(ps->buff, ps->instr, ps->len, repl, alt->replacementlen); DUMP_BYTES(ALT, ps->instr, ps->len, "%px: old_insn: ", ps->instr); - DUMP_BYTES(ALT, repl, p->replacementlen, "%px: rpl_insn: ", repl); + DUMP_BYTES(ALT, repl, alt->replacementlen, "%px: rpl_insn: ", repl); DUMP_BYTES(ALT, ps->buff, ps->len, "%px: final_insn: ", ps->instr); } -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 3/3] x86/alternative: Patch a single alternative location only once 2025-11-19 16:04 [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once Juergen Gross 2025-11-19 16:04 ` [PATCH v4 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Juergen Gross 2025-11-19 16:04 ` [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives Juergen Gross @ 2025-11-19 16:04 ` Juergen Gross 2026-01-02 16:05 ` Borislav Petkov 2025-12-15 8:25 ` [PATCH v4 0/3] " Juergen Gross 3 siblings, 1 reply; 8+ messages in thread From: Juergen Gross @ 2025-11-19 16:04 UTC (permalink / raw) To: linux-kernel, x86 Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin 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). This issue has been hit when trying to do paravirt patching similar to the following: ALTERNATIVE_2(PARAVIRT_CALL, // indirect call instr, feature, // native instruction ALT_CALL_INSTR, X86_FEATURE_XENPV) // Xen function In case "feature" was true, "instr" replaced the indirect call. Under Xen PV the patching to have a direct call failed, as the original indirect call was no longer there to find the call target. - 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: - complete rework (Boris Petkov) V3: - rebase to added patch 2 --- arch/x86/kernel/alternative.c | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index afcc681ff3bd..c1032840df4e 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -611,37 +611,36 @@ struct patch_site { u8 len; }; -static void __init_or_module analyze_patch_site(struct patch_site *ps, - struct alt_instr *p, struct alt_instr *end) +static struct alt_instr * __init_or_module analyze_patch_site( + struct patch_site *ps, struct alt_instr *p, struct alt_instr *end) { - struct alt_instr *r; - /* * In case of nested ALTERNATIVE()s the outer alternative might add * more padding. To ensure consistent patching find the max padding for * all alt_instr entries for this site (nested alternatives result in * consecutive entries). + * Find the last alt_instr eligible for patching at the site. */ ps->instr = instr_va(p); - ps->len = p->instrlen; - for (r = p+1; r < end && instr_va(r) == ps->instr; r++) { - ps->len = max(ps->len, r->instrlen); - p->instrlen = r->instrlen = ps->len; + ps->alt = NULL; + ps->len = 0; + for (; p < end && instr_va(p) == ps->instr; p++) { + ps->len = max(ps->len, p->instrlen); + + BUG_ON(p->cpuid >= (NCAPINTS + NBUGINTS) * 32); + /* + * Patch if either: + * - feature is present + * - feature not present but ALT_FLAG_NOT is set to mean, + * patch if feature is *NOT* present. + */ + if (!boot_cpu_has(p->cpuid) != !(p->flags & ALT_FLAG_NOT)) + ps->alt = p; } BUG_ON(ps->len > sizeof(ps->buff)); - BUG_ON(p->cpuid >= (NCAPINTS + NBUGINTS) * 32); - /* - * Patch if either: - * - feature is present - * - feature not present but ALT_FLAG_NOT is set to mean, - * patch if feature is *NOT* present. - */ - if (!boot_cpu_has(p->cpuid) == !(p->flags & ALT_FLAG_NOT)) - ps->alt = NULL; - else - ps->alt = p; + return p; } static void __init_or_module prep_patch_site(struct patch_site *ps) @@ -720,10 +719,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, * So be careful if you want to change the scan order to any other * order. */ - for (a = start; a < end; a++) { + a = start; + while (a < end) { struct patch_site ps; - analyze_patch_site(&ps, a, end); + a = analyze_patch_site(&ps, a, end); prep_patch_site(&ps); patch_site(&ps); } -- 2.51.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v4 3/3] x86/alternative: Patch a single alternative location only once 2025-11-19 16:04 ` [PATCH v4 3/3] x86/alternative: Patch a single alternative location only once Juergen Gross @ 2026-01-02 16:05 ` Borislav Petkov 0 siblings, 0 replies; 8+ messages in thread From: Borislav Petkov @ 2026-01-02 16:05 UTC (permalink / raw) To: Juergen Gross Cc: linux-kernel, x86, Thomas Gleixner, Ingo Molnar, Dave Hansen, H. Peter Anvin On Wed, Nov 19, 2025 at 05:04:20PM +0100, 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). > This issue has been hit when trying to do paravirt patching similar > to the following: > ALTERNATIVE_2(PARAVIRT_CALL, // indirect call > instr, feature, // native instruction > ALT_CALL_INSTR, X86_FEATURE_XENPV) // Xen function > In case "feature" was true, "instr" replaced the indirect call. Under > Xen PV the patching to have a direct call failed, as the original > indirect call was no longer there to find the call target. > > - 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: > - complete rework (Boris Petkov) > V3: > - rebase to added patch 2 > --- > arch/x86/kernel/alternative.c | 42 +++++++++++++++++------------------ > 1 file changed, 21 insertions(+), 21 deletions(-) That one would then look like this as I've moved the struct patch_site ps at the beginning of the loop, where it belongs. Other than that, yap, looks ok, and it seems to work. Thx. --- diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 66868ecdebd2..6250c192bb59 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -593,39 +593,39 @@ struct patch_site { u8 len; }; -static void __init_or_module analyze_patch_site(struct patch_site *ps, - struct alt_instr *start, - struct alt_instr *end) +static struct alt_instr * __init_or_module analyze_patch_site(struct patch_site *ps, + struct alt_instr *start, + struct alt_instr *end) { - struct alt_instr *r; + struct alt_instr *p = start; ps->instr = instr_va(start); - ps->len = start->instrlen; /* * In case of nested ALTERNATIVE()s the outer alternative might add * more padding. To ensure consistent patching find the max padding for * all alt_instr entries for this site (nested alternatives result in * consecutive entries). + * Find the last alt_instr eligible for patching at the site. */ - for (r = start+1; r < end && instr_va(r) == ps->instr; r++) { - ps->len = max(ps->len, r->instrlen); - start->instrlen = r->instrlen = ps->len; + for (; p < end && instr_va(p) == ps->instr; p++) { + ps->len = max(ps->len, p->instrlen); + + BUG_ON(p->cpuid >= (NCAPINTS + NBUGINTS) * 32); + + /* + * Patch if either: + * - feature is present + * - feature not present but ALT_FLAG_NOT is set to mean, + * patch if feature is *NOT* present. + */ + if (!boot_cpu_has(p->cpuid) != !(p->flags & ALT_FLAG_NOT)) + ps->alt = p; } BUG_ON(ps->len > sizeof(ps->buff)); - BUG_ON(start->cpuid >= (NCAPINTS + NBUGINTS) * 32); - /* - * Patch if either: - * - feature is present - * - feature not present but ALT_FLAG_NOT is set to mean, - * patch if feature is *NOT* present. - */ - if (!boot_cpu_has(start->cpuid) == !(start->flags & ALT_FLAG_NOT)) - ps->alt = NULL; - else - ps->alt = start; + return p; } static void __init_or_module prep_patch_site(struct patch_site *ps) @@ -704,10 +704,14 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, * So be careful if you want to change the scan order to any other * order. */ - for (a = start; a < end; a++) { - struct patch_site ps; - - analyze_patch_site(&ps, a, end); + a = start; + while (a < end) { + struct patch_site ps = { + .alt = NULL, + .len = 0 + }; + + a = analyze_patch_site(&ps, a, end); prep_patch_site(&ps); patch_site(&ps); } -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once 2025-11-19 16:04 [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once Juergen Gross ` (2 preceding siblings ...) 2025-11-19 16:04 ` [PATCH v4 3/3] x86/alternative: Patch a single alternative location only once Juergen Gross @ 2025-12-15 8:25 ` Juergen Gross 2025-12-15 10:25 ` Borislav Petkov 3 siblings, 1 reply; 8+ messages in thread From: Juergen Gross @ 2025-12-15 8:25 UTC (permalink / raw) To: linux-kernel, x86 Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, H. Peter Anvin [-- Attachment #1.1.1: Type: text/plain, Size: 1082 bytes --] Ping? On 19.11.25 17:04, 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. > > Changes in V2: > - complete rework (Boris Petkov) > > Changes in V3: > - split former V2 patch into 2 by introducing a helper function (Boris Petkov) > - repost the small cleanup patch 1 which was taken before, but has somehow > vanished from the tip x86/alternative branch (it is still in the tip > master branch, but I couldn't find it in any other tip branch). > > Changes in V4: > - use 3 helpers instead of 1 (Boris Petkov) > > Juergen Gross (3): > x86/alternative: Drop not needed test after call of alt_replace_call() > x86/alternative: Use helper functions for patching alternatives > x86/alternative: Patch a single alternative location only once > > arch/x86/kernel/alternative.c | 147 ++++++++++++++++++++-------------- > 1 file changed, 87 insertions(+), 60 deletions(-) > [-- 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 --] ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once 2025-12-15 8:25 ` [PATCH v4 0/3] " Juergen Gross @ 2025-12-15 10:25 ` Borislav Petkov 0 siblings, 0 replies; 8+ messages in thread From: Borislav Petkov @ 2025-12-15 10:25 UTC (permalink / raw) To: Juergen Gross, linux-kernel, x86 Cc: Thomas Gleixner, Ingo Molnar, Dave Hansen, H. Peter Anvin On December 15, 2025 8:25:13 AM UTC, Juergen Gross <jgross@suse.com> wrote: >Ping? On vacation. Will take a look when I get back. Thx. Small device. Typos and formatting crap ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-01-02 16:05 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-11-19 16:04 [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once Juergen Gross 2025-11-19 16:04 ` [PATCH v4 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Juergen Gross 2025-11-19 16:04 ` [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives Juergen Gross 2026-01-02 13:00 ` Borislav Petkov 2025-11-19 16:04 ` [PATCH v4 3/3] x86/alternative: Patch a single alternative location only once Juergen Gross 2026-01-02 16:05 ` Borislav Petkov 2025-12-15 8:25 ` [PATCH v4 0/3] " Juergen Gross 2025-12-15 10:25 ` Borislav Petkov
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox