* [PATCH] x86/fred: Add this_cpu_fred_sp() and export it for KVM
@ 2026-11-24 1:58 Sohil Mehta
2026-11-24 15:52 ` Dave Hansen
0 siblings, 1 reply; 5+ messages in thread
From: Sohil Mehta @ 2026-11-24 1:58 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 adds fields to the VMCS host-state area for the stack pointers of
stack levels 1 to 3. The CPU loads these pointers on every VM exit to
ensure a valid FRED event delivery context is established immediately.
These values are per CPU, so KVM has to refresh them each time a vCPU is
loaded onto a CPU.
The stack for each level is assigned implicitly, when the FRED RSP MSRs
are programmed during CPU initialization. KVM could read the MSRs back,
but that costs three RDMSRs on every vCPU load, while the kernel already
has the values at hand. Add a helper that returns the corresponding
stack pointer for each FRED RSP MSR. Use it to program the MSRs during
init, and export it for KVM.
Key the helper on the MSR rather than on a stack level, so that KVM
never needs to know which exception stack backs which level. Eventually,
the same interface can be extended for the FRED shadow stack MSRs as
well.
Originally-by: Xin Li <xin@zytor.com>
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
I have been working on splitting the FRED KVM series to make it easier
to review and merge. I am posting this separately because the patch has
a minor dependency on the ESTACK_ prefix patch merged in tip today. The
main goal is to collect acks from the x86 maintainers.
The first exported user for the helper is going to be in the FRED KVM
series, so I assume this would likely be merged via the KVM tree. You
can consider merging it via the x86 tree if you are feeling generous :)
Note, based on a quick check on a CWF system, reading the three FRED RSP
MSRs takes about ~6x longer than reading the per-CPU cached values.
v1:
- Key the helper on the FRED RSP MSR rather than on a stack level.
https://lore.kernel.org/lkml/82dc24e3-f574-4696-99cb-87c0c04eab93@intel.com/
- Warn and return 0 on an unexpected MSR rather than calling BUG().
- Add a comment saying why stack level 2 is special.
- Drop Binbin's Reviewed-by and Xin's author tag, since the interface
has changed.
FRED KVM v10:
- https://lore.kernel.org/lkml/20260911213659.2025974-7-sohil.mehta@intel.com/
---
arch/x86/include/asm/fred.h | 2 ++
arch/x86/kernel/fred.c | 29 ++++++++++++++++++++++++++---
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/arch/x86/include/asm/fred.h b/arch/x86/include/asm/fred.h
index 18a2f811c358..eb0b231f24ae 100644
--- a/arch/x86/include/asm/fred.h
+++ b/arch/x86/include/asm/fred.h
@@ -88,6 +88,7 @@ static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int
void cpu_init_fred_exceptions(void);
void cpu_init_fred_rsps(void);
void fred_complete_exception_setup(void);
+unsigned long this_cpu_fred_sp(u32 msr);
DECLARE_PER_CPU(unsigned long, fred_rsp0);
@@ -110,6 +111,7 @@ static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { ret
static inline void cpu_init_fred_exceptions(void) { }
static inline void cpu_init_fred_rsps(void) { }
static inline void fred_complete_exception_setup(void) { }
+static inline unsigned long this_cpu_fred_sp(u32 msr) { return 0; }
static inline void fred_sync_rsp0(unsigned long rsp0) { }
static inline void fred_update_rsp0(void) { }
#endif /* CONFIG_X86_FRED */
diff --git a/arch/x86/kernel/fred.c b/arch/x86/kernel/fred.c
index 1c452a389a70..12d2fda60e69 100644
--- a/arch/x86/kernel/fred.c
+++ b/arch/x86/kernel/fred.c
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/kernel.h>
+#include <linux/kvm_types.h>
#include <asm/desc.h>
#include <asm/fred.h>
@@ -25,6 +26,28 @@
DEFINE_PER_CPU(unsigned long, fred_rsp0);
EXPORT_PER_CPU_SYMBOL(fred_rsp0);
+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;
+ }
+}
+EXPORT_SYMBOL_FOR_KVM(this_cpu_fred_sp);
+
void cpu_init_fred_exceptions(void)
{
/*
@@ -84,7 +107,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] 5+ messages in thread
* Re: [PATCH] x86/fred: Add this_cpu_fred_sp() and export it for KVM
2026-11-24 1:58 [PATCH] x86/fred: Add this_cpu_fred_sp() and export it for KVM Sohil Mehta
@ 2026-11-24 15:52 ` Dave Hansen
2026-11-24 17:00 ` Sohil Mehta
0 siblings, 1 reply; 5+ messages in thread
From: Dave Hansen @ 2026-11-24 15:52 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/23/26 17:58, Sohil Mehta wrote:
> FRED adds fields to the VMCS host-state area for the stack pointers of
> stack levels 1 to 3. The CPU loads these pointers on every VM exit to
> ensure a valid FRED event delivery context is established immediately.
> These values are per CPU, so KVM has to refresh them each time a vCPU is
> loaded onto a CPU.
Sohil,
We can take cleanups that both make sense for non-KVM FRED code and are
_described_ that way. We can't add dead code, or take things that have
changelogs for code that's not in the tree.
In other words, please write a changelog and send code that has nothing
immediate to do with KVM. If that's not possible for a given patch,
that's a pretty good clue it's not a cleanup and can't be merged early.
Anything going in early has to stand on its own, please.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] x86/fred: Add this_cpu_fred_sp() and export it for KVM
2026-11-24 15:52 ` Dave Hansen
@ 2026-11-24 17:00 ` Sohil Mehta
2026-11-24 17:13 ` Dave Hansen
0 siblings, 1 reply; 5+ messages in thread
From: Sohil Mehta @ 2026-11-24 17:00 UTC (permalink / raw)
To: Dave Hansen, 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/2026 7:52 AM, Dave Hansen wrote:
> Sohil,
>
> We can take cleanups that both make sense for non-KVM FRED code and are
> _described_ that way. We can't add dead code, or take things that have
> changelogs for code that's not in the tree.
>
> In other words, please write a changelog and send code that has nothing
> immediate to do with KVM. If that's not possible for a given patch,
> that's a pretty good clue it's not a cleanup and can't be merged early.
>
> Anything going in early has to stand on its own, please.
Hi Dave,
My primary motivation with this patch was only to get an ACK from the
x86 side. I believe you may have missed my note under the '---' section.
Maybe I should have included a small cover letter.
"... I am posting this separately because the patch has a minor
dependency on the ESTACK_ prefix patch merged in tip today. The main
goal is to collect acks from the x86 maintainers.
The first exported user for the helper is going to be in the FRED KVM
series, so I assume this would likely be merged via the KVM tree ..."
This patch only exists due to KVM and should be merged with the FRED KVM
series. It is the last remaining core x86 patch, so I just wanted to
make it easier for you to review it (instead of the scanning through the
large KVM series).
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] x86/fred: Add this_cpu_fred_sp() and export it for KVM
2026-11-24 17:00 ` Sohil Mehta
@ 2026-11-24 17:13 ` Dave Hansen
2026-11-24 20:22 ` Sohil Mehta
0 siblings, 1 reply; 5+ messages in thread
From: Dave Hansen @ 2026-11-24 17:13 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 09:00, Sohil Mehta wrote:
> My primary motivation with this patch was only to get an ACK from the
> x86 side. I believe you may have missed my note under the '---' section.
Sohil,
Please just reframe this patch as an x86 cleanup patch. The new comments
actually do explain a thing or two. Then, the FRED/KVM patch can add the
export as a one-liner.
The cleanup can be merged now if you do it that way.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] x86/fred: Add this_cpu_fred_sp() and export it for KVM
2026-11-24 17:13 ` Dave Hansen
@ 2026-11-24 20:22 ` Sohil Mehta
0 siblings, 0 replies; 5+ messages in thread
From: Sohil Mehta @ 2026-11-24 20:22 UTC (permalink / raw)
To: Dave Hansen, 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/2026 9:13 AM, Dave Hansen wrote:
> On 11/24/26 09:00, Sohil Mehta wrote:
>> My primary motivation with this patch was only to get an ACK from the
>> x86 side. I believe you may have missed my note under the '---' section.
>
> Sohil,
>
> Please just reframe this patch as an x86 cleanup patch. The new comments
> actually do explain a thing or two. Then, the FRED/KVM patch can add the
> export as a one-liner.
>
> The cleanup can be merged now if you do it that way.
Thanks! Posted the cleanup patch at
https://lore.kernel.org/lkml/20261124200659.471685-1-sohil.mehta@intel.com/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 20:23 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 1:58 [PATCH] x86/fred: Add this_cpu_fred_sp() and export it for KVM Sohil Mehta
2026-11-24 15:52 ` Dave Hansen
2026-11-24 17:00 ` Sohil Mehta
2026-11-24 17:13 ` Dave Hansen
2026-11-24 20:22 ` Sohil Mehta
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®