* [PATCH v2] x86/fred: Consolidate the FRED stack pointer assignments
@ 2026-11-24 20:06 Sohil Mehta
2026-11-30 22:43 ` Dave Hansen
0 siblings, 1 reply; 2+ messages in thread
From: Sohil Mehta @ 2026-11-24 20:06 UTC (permalink / raw)
To: Dave Hansen, Borislav Petkov
Cc: Thomas Gleixner, Ingo Molnar, x86, H . Peter Anvin, Xin Li,
Andy Lutomirski, Peter Zijlstra, Binbin Wu, Tom Lendacky,
Dave Hansen, Chang S . Bae, Chao Gao, Sean Christopherson,
Paolo Bonzini, Sohil Mehta, kvm, linux-kernel
FRED delivers events on one of four stack levels, and each level has its
own stack pointer MSR. The mapping of stacks to levels is only implied
by the MSR writes. Nothing in the code explicitly states it, or notes
that #MC uses the NMI stack.
Add a helper that makes the connection obvious and returns the
corresponding stack pointer for each RSP MSR. Use it to program all four
FRED RSP MSRs during CPU initialization.
Key the helper on the MSR index rather than the stack level to keep it
simple. Later, the same interface can easily be exported to KVM or
extended for FRED shadow stack MSRs as well.
No functional change intended.
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
This is a standalone cleanup that was part of the FRED KVM series. Dave
asked me to send things like this up separately for consideration:
https://lore.kernel.org/lkml/1712c0cd-747b-4dce-8231-0c8d75225648@intel.com/
v2:
- Reframe as an x86 cleanup. Drop the export, the KVM heavy changelog
and the unused !CONFIG_X86_FRED stub (Dave).
- Program the level 0 stack pointer MSR through the helper too, so that
all four of them go through it.
- Keep the helper static for now, since all the callers are file-local.
v1: https://lore.kernel.org/lkml/20261124015840.2673975-1-sohil.mehta@intel.com/
---
arch/x86/kernel/fred.c | 29 +++++++++++++++++++++++++----
1 file changed, 25 insertions(+), 4 deletions(-)
diff --git a/arch/x86/kernel/fred.c b/arch/x86/kernel/fred.c
index 1c452a389a70..35f5c27c7198 100644
--- a/arch/x86/kernel/fred.c
+++ b/arch/x86/kernel/fred.c
@@ -25,6 +25,27 @@
DEFINE_PER_CPU(unsigned long, fred_rsp0);
EXPORT_PER_CPU_SYMBOL(fred_rsp0);
+static unsigned long this_cpu_fred_sp(u32 msr)
+{
+ switch (msr) {
+ case MSR_IA32_FRED_RSP0:
+ return __this_cpu_read(fred_rsp0);
+ case MSR_IA32_FRED_RSP1:
+ return __this_cpu_ist_top_va(ESTACK_DB);
+ case MSR_IA32_FRED_RSP2:
+ /*
+ * NMI and #MC both use the NMI stack so ESTACK_MCE is left
+ * unused on a FRED system.
+ */
+ return __this_cpu_ist_top_va(ESTACK_NMI);
+ case MSR_IA32_FRED_RSP3:
+ return __this_cpu_ist_top_va(ESTACK_DF);
+ default:
+ WARN_ONCE(1, "Unexpected FRED stack pointer MSR 0x%x\n", msr);
+ return 0;
+ }
+}
+
void cpu_init_fred_exceptions(void)
{
/*
@@ -53,7 +74,7 @@ void cpu_init_fred_exceptions(void)
* Ater a CPU offline/online cycle, the FRED RSP0 MSR should be
* resynchronized with its per-CPU cache.
*/
- wrmsrq(MSR_IA32_FRED_RSP0, __this_cpu_read(fred_rsp0));
+ wrmsrq(MSR_IA32_FRED_RSP0, this_cpu_fred_sp(MSR_IA32_FRED_RSP0));
wrmsrq(MSR_IA32_FRED_RSP1, 0);
wrmsrq(MSR_IA32_FRED_RSP2, 0);
@@ -84,7 +105,7 @@ void cpu_init_fred_rsps(void)
FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL));
/* The FRED equivalents to IST stacks... */
- wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(ESTACK_DB));
- wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(ESTACK_NMI));
- wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(ESTACK_DF));
+ wrmsrq(MSR_IA32_FRED_RSP1, this_cpu_fred_sp(MSR_IA32_FRED_RSP1));
+ wrmsrq(MSR_IA32_FRED_RSP2, this_cpu_fred_sp(MSR_IA32_FRED_RSP2));
+ wrmsrq(MSR_IA32_FRED_RSP3, this_cpu_fred_sp(MSR_IA32_FRED_RSP3));
}
base-commit: 135dbd0519cf7f72af120801ec7dbb79275a362d
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH v2] x86/fred: Consolidate the FRED stack pointer assignments
2026-11-24 20:06 [PATCH v2] x86/fred: Consolidate the FRED stack pointer assignments Sohil Mehta
@ 2026-11-30 22:43 ` Dave Hansen
0 siblings, 0 replies; 2+ messages in thread
From: Dave Hansen @ 2026-11-30 22:43 UTC (permalink / raw)
To: Sohil Mehta, Dave Hansen, Borislav Petkov
Cc: Thomas Gleixner, Ingo Molnar, x86, H . Peter Anvin, Xin Li,
Andy Lutomirski, Peter Zijlstra, Binbin Wu, Tom Lendacky,
Chang S . Bae, Chao Gao, Sean Christopherson, Paolo Bonzini, kvm,
linux-kernel
On 11/24/26 12:06, Sohil Mehta wrote:
> +static unsigned long this_cpu_fred_sp(u32 msr)
> +{
I don't super like where this is going.
The previous code kinda showed in one place how the FRED stack levels
map to the exception stacks:
> /* The FRED equivalents to IST stacks... */
> - wrmsrq(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(ESTACK_DB));
> - wrmsrq(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(ESTACK_NMI));
> - wrmsrq(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(ESTACK_DF));
But the new abstraction hides it completely:
> + wrmsrq(MSR_IA32_FRED_RSP1, this_cpu_fred_sp(MSR_IA32_FRED_RSP1));
> + wrmsrq(MSR_IA32_FRED_RSP2, this_cpu_fred_sp(MSR_IA32_FRED_RSP2));
> + wrmsrq(MSR_IA32_FRED_RSP3, this_cpu_fred_sp(MSR_IA32_FRED_RSP3));
> }
It also says MSR_IA32_FRED_RSPX in a redundant way.
Can this be done without the redundancy? Could it also be done with a
build warning instead of a runtime one? I'd honestly be OK with this
thing going in a header even. It's *tiny*.
BTW, does the KVM code need the MSR value or does it also need to do a
wrmsr()?
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 22:43 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 20:06 [PATCH v2] x86/fred: Consolidate the FRED stack pointer assignments Sohil Mehta
2026-11-30 22:43 ` Dave Hansen
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®