Skip to content

Commit a4233c2

Browse files
jgross1bp3tk0v
authored andcommitted
x86/alternative: Patch a single alternative location only once
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> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://patch.msgid.link/20260105080452.5064-3-jgross@suse.com
1 parent 544b4e1 commit a4233c2

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

arch/x86/kernel/alternative.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -593,39 +593,38 @@ struct patch_site {
593593
u8 len;
594594
};
595595

596-
static void __init_or_module analyze_patch_site(struct patch_site *ps,
597-
struct alt_instr *start,
598-
struct alt_instr *end)
596+
static struct alt_instr * __init_or_module analyze_patch_site(struct patch_site *ps,
597+
struct alt_instr *start,
598+
struct alt_instr *end)
599599
{
600-
struct alt_instr *r;
600+
struct alt_instr *alt = start;
601601

602602
ps->instr = instr_va(start);
603-
ps->len = start->instrlen;
604603

605604
/*
606605
* In case of nested ALTERNATIVE()s the outer alternative might add
607606
* more padding. To ensure consistent patching find the max padding for
608607
* all alt_instr entries for this site (nested alternatives result in
609608
* consecutive entries).
609+
* Find the last alt_instr eligible for patching at the site.
610610
*/
611-
for (r = start+1; r < end && instr_va(r) == ps->instr; r++) {
612-
ps->len = max(ps->len, r->instrlen);
613-
start->instrlen = r->instrlen = ps->len;
611+
for (; alt < end && instr_va(alt) == ps->instr; alt++) {
612+
ps->len = max(ps->len, alt->instrlen);
613+
614+
BUG_ON(alt->cpuid >= (NCAPINTS + NBUGINTS) * 32);
615+
/*
616+
* Patch if either:
617+
* - feature is present
618+
* - feature not present but ALT_FLAG_NOT is set to mean,
619+
* patch if feature is *NOT* present.
620+
*/
621+
if (!boot_cpu_has(alt->cpuid) != !(alt->flags & ALT_FLAG_NOT))
622+
ps->alt = alt;
614623
}
615624

616625
BUG_ON(ps->len > sizeof(ps->buff));
617-
BUG_ON(start->cpuid >= (NCAPINTS + NBUGINTS) * 32);
618626

619-
/*
620-
* Patch if either:
621-
* - feature is present
622-
* - feature not present but ALT_FLAG_NOT is set to mean,
623-
* patch if feature is *NOT* present.
624-
*/
625-
if (!boot_cpu_has(start->cpuid) == !(start->flags & ALT_FLAG_NOT))
626-
ps->alt = NULL;
627-
else
628-
ps->alt = start;
627+
return alt;
629628
}
630629

631630
static void __init_or_module prep_patch_site(struct patch_site *ps)
@@ -704,10 +703,14 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
704703
* So be careful if you want to change the scan order to any other
705704
* order.
706705
*/
707-
for (a = start; a < end; a++) {
708-
struct patch_site ps;
709-
710-
analyze_patch_site(&ps, a, end);
706+
a = start;
707+
while (a < end) {
708+
struct patch_site ps = {
709+
.alt = NULL,
710+
.len = 0
711+
};
712+
713+
a = analyze_patch_site(&ps, a, end);
711714
prep_patch_site(&ps);
712715
patch_site(&ps);
713716
}

0 commit comments

Comments
 (0)