Skip to contentSkip to conversation

x86/fred: Add this_cpu_fred_sp() and export it for KVM

PATCH New Sohil Mehta 1 participant 1 message Nov 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/fred: Add this_cpu_fred_sp() and export it for KVM

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.

---
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/[email protected]/
- 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/[email protected]/

---
 arch/x86/include/asm/fred.h |  2 ++
 arch/x86/kernel/fred.c      | 29 ++++++++++++++++++++++++++---
 2 files changed, 28 insertions(+), 3 deletions(-)
arch/x86/include/asm/fred.h +2 -0
index 18a2f811c358..eb0b231f24ae 100644
@@ -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 */
arch/x86/kernel/fred.c +26 -3
index 1c452a389a70..12d2fda60e69 100644
@@ -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

Signature · 1 line
2.43.0