* [PATCH v5 2/2] x86/alternative: Patch a single alternative location only once
2026-01-05 8:04 [PATCH v5 0/2] " Juergen Gross
@ 2026-01-05 8:04 ` Juergen Gross
0 siblings, 0 replies; 2+ messages in thread
From: Juergen Gross @ 2026-01-05 8: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
V5:
- small cosmetic changes (Boris Petkov)
- rebase due to changes in patch
---
arch/x86/kernel/alternative.c | 49 +++++++++++++++++++----------------
1 file changed, 26 insertions(+), 23 deletions(-)
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 6e3eec048d19..693b59b2f7d0 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -593,39 +593,38 @@ 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 *alt = 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 (; alt < end && instr_va(alt) == ps->instr; alt++) {
+ ps->len = max(ps->len, alt->instrlen);
+
+ BUG_ON(alt->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(alt->cpuid) != !(alt->flags & ALT_FLAG_NOT))
+ ps->alt = alt;
}
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 alt;
}
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,
* 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);
}
--
2.51.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v5 2/2] x86/alternative: Patch a single alternative location only once
@ 2026-01-05 12:44 kernel test robot
0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2026-01-05 12:44 UTC (permalink / raw)
To: oe-kbuild
::::::
:::::: Manual check reason: "high confidence checkpatch report"
::::::
BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20260105080452.5064-3-jgross@suse.com>
References: <20260105080452.5064-3-jgross@suse.com>
TO: Juergen Gross <jgross@suse.com>
Hi Juergen,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/x86/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Juergen-Gross/x86-alternative-Use-helper-functions-for-patching-alternatives/20260105-161434
base: tip/x86/core
patch link: https://lore.kernel.org/r/20260105080452.5064-3-jgross%40suse.com
patch subject: [PATCH v5 2/2] x86/alternative: Patch a single alternative location only once
:::::: branch date: 5 hours ago
:::::: commit date: 5 hours ago
reproduce: (https://download.01.org/0day-ci/archive/20260105/202601051306.KfFREWM9-lkp@intel.com/reproduce)
# many are suggestions rather than must-fix
ERROR:POINTER_LOCATION: "foo * bar" should be "foo *bar"
#48: FILE: arch/x86/kernel/alternative.c:614:
+static struct alt_instr * __init_or_module analyze_patch_site(struct patch_site *ps,
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-01-05 12:45 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-05 12:44 [PATCH v5 2/2] x86/alternative: Patch a single alternative location only once kernel test robot
-- strict thread matches above, loose matches on Subject: below --
2026-01-05 8:04 [PATCH v5 0/2] " Juergen Gross
2026-01-05 8:04 ` [PATCH v5 2/2] " Juergen Gross
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.