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

From: Binbin Wu

Date: Fri Oct 23 2026 - 02:51:00 EST


On 9/12/2026 5:36 AM, Sohil Mehta wrote:
> From: "Xin Li (Intel)" <xin@xxxxxxxxx>
>
> Introduce and export this_cpu_fred_rsp() to provide KVM with a self-
> explanatory interface for retrieving per-CPU FRED regular stacks for
> stack levels 1->3.
>
> FRED introduced new fields in the VMCS host-state area for stack levels
> 1–>3 (HOST_IA32_FRED_RSP[123]), which correspond to the per-CPU FRED
> regular stacks for stack levels 1->3. KVM must populate these fields
> each time a vCPU is loaded onto a CPU to ensure a complete valid FRED
> event delivery context immediately after any VM-Exits.
>
> Signed-off-by: Xin Li (Intel) <xin@xxxxxxxxx>
> Signed-off-by: Sohil Mehta <sohil.mehta@xxxxxxxxx>

Reviewed-by: Binbin Wu <binbin.wu@xxxxxxxxxxxxxxx>

Two nits below.

[...]

>
> +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);

Nit:
How about adding a comment to state NMI and MC share this one?


Also, currently, FRED_{DB,NMI,MC,DF}_STACK_LEVEL are defined as
number, may be it can be defined using the new added
FRED_STACK_LEVEL_{1,2,3}

> + case FRED_STACK_LEVEL_3:
> + return __this_cpu_ist_top_va(ESTACK_DF);
> + default:
> + BUG();
> + }
> +}
> +EXPORT_SYMBOL_FOR_KVM(this_cpu_fred_rsp);
> +
> /* 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));
> }