Re: [PATCH v10 06/28] x86/fred: Export this_cpu_fred_rsp() for KVM usage

From: Sohil Mehta

Date: Sat Nov 21 2026 - 15:42:23 EST


On 9/11/2026 2:36 PM, Sohil Mehta wrote:
> diff --git a/arch/x86/include/asm/fred.h b/arch/x86/include/asm/fred.h
> index 18a2f811c358..85b851f16bae 100644
> --- a/arch/x86/include/asm/fred.h
> +++ b/arch/x86/include/asm/fred.h
> @@ -35,6 +35,13 @@
>
> #ifndef __ASSEMBLER__
>
> +enum fred_stack_level {
> + FRED_STACK_LEVEL_0,
> + FRED_STACK_LEVEL_1,
> + FRED_STACK_LEVEL_2,
> + FRED_STACK_LEVEL_3
> +};
> +

...

> +unsigned long this_cpu_fred_rsp(enum fred_stack_level lvl)
> +{
> + switch (lvl) {
> + case FRED_STACK_LEVEL_0:
> + return __this_cpu_read(fred_rsp0);
> + case FRED_STACK_LEVEL_1:
> + return __this_cpu_ist_top_va(ESTACK_DB);
> + case FRED_STACK_LEVEL_2:
> + return __this_cpu_ist_top_va(ESTACK_NMI);
> + case FRED_STACK_LEVEL_3:
> + return __this_cpu_ist_top_va(ESTACK_DF);
> + default:
> + BUG();
> + }
> +}
> +EXPORT_SYMBOL_FOR_KVM(this_cpu_fred_rsp);
> +

I am not sure if the FRED_STACK_LEVEL_* abstraction is really helpful.
It makes the KVM interface a bit convoluted I think. So right now we
need to expose this_cpu_fred_rsp() as well as enum fred_stack_level for
KVM to get the kernel stack information.

Sean suggested in v9 keying based on the MSR itself which makes more
sense. https://lore.kernel.org/lkml/aa7mILKnhX9N4228@xxxxxxxxxx/

It avoids exposing the enum and makes it future-proof with supervisor
shadow stacks. KVM will need similar information for
MSR_IA32_FRED_SSP1..3. With the current approach we would have to add
another export for the SSP:

unsigned long this_cpu_fred_ssp(enum fred_stack_level lvl);


Keying on the MSR index makes this simpler and can be easily expanded to
SSP MSRs. We can name is this_cpu_fred_sp() to prevent Xin's rename
concern in the above thread.

unsigned long this_cpu_fred_sp(u32 msr);

So, the below would become

wrmsrq(MSR_IA32_FRED_RSP1, this_cpu_fred_sp(MSR_IA32_FRED_RSP1));

Any concerns?

> /* Must be called after setup_cpu_entry_areas() */
> void cpu_init_fred_rsps(void)
> {
> @@ -84,7 +102,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_rsp(FRED_STACK_LEVEL_1));
> + wrmsrq(MSR_IA32_FRED_RSP2, this_cpu_fred_rsp(FRED_STACK_LEVEL_2));
> + wrmsrq(MSR_IA32_FRED_RSP3, this_cpu_fred_rsp(FRED_STACK_LEVEL_3));
> }