Re: [Patch v8 07/23] x86/fpu/xstate: Add xsaves_nmi() helper

From: Peter Zijlstra

Date: Fri May 29 2026 - 07:33:10 EST


On Fri, May 29, 2026 at 03:56:29PM +0800, Dapeng Mi wrote:
> From: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>
>
> Add xsaves_nmi() to save supported xsave states in NMI handler.
>
> This function is similar to xsaves(), but should only be called within
> a NMI handler. This function returns the actual register contents at
> the moment the NMI occurs.
>
> Currently the perf subsystem is the sole user of this helper. It uses
> this function to snapshot SIMD (XMM/YMM/ZMM) and APX eGPRs registers
> which would be added in subsequent patches.
>
> Signed-off-by: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>
> Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
> ---
> arch/x86/include/asm/fpu/xstate.h | 1 +
> arch/x86/kernel/fpu/xstate.c | 23 +++++++++++++++++++++++
> 2 files changed, 24 insertions(+)
>
> diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/xstate.h
> index 7a7dc9d56027..38fa8ff26559 100644
> --- a/arch/x86/include/asm/fpu/xstate.h
> +++ b/arch/x86/include/asm/fpu/xstate.h
> @@ -110,6 +110,7 @@ int xfeature_size(int xfeature_nr);
>
> void xsaves(struct xregs_state *xsave, u64 mask);
> void xrstors(struct xregs_state *xsave, u64 mask);
> +void xsaves_nmi(struct xregs_state *xsave, u64 mask);
>
> int xfd_enable_feature(u64 xfd_err);
>
> diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
> index a7b6524a9dea..4394091c4791 100644
> --- a/arch/x86/kernel/fpu/xstate.c
> +++ b/arch/x86/kernel/fpu/xstate.c
> @@ -1474,6 +1474,29 @@ void xrstors(struct xregs_state *xstate, u64 mask)
> WARN_ON_ONCE(err);
> }
>
> +/**
> + * xsaves_nmi - Save selected components to a kernel xstate buffer in NMI
> + * @xstate: Pointer to the buffer
> + * @mask: Feature mask to select the components to save
> + *
> + * This function is similar to xsaves(), but should only be called within
> + * a NMI handler. This function returns the actual register contents at
> + * the moment the NMI occurs.
> + *
> + * Currently, the perf subsystem is the sole user of this helper. It uses
> + * the function to snapshot SIMD (XMM/YMM/ZMM) and APX eGPRs registers.
> + */
> +void xsaves_nmi(struct xregs_state *xstate, u64 mask)
> +{
> + int err;
> +
> + if (!in_nmi())
> + return;
> +
> + XSTATE_OP(XSAVES, xstate, (u32)mask, (u32)(mask >> 32), err);
> + WARN_ON_ONCE(err);
> +}

Sashiko raises a fun point vs skid; if an exclude_kernel=1 event trips
inside the kernel this can potentially leak a whole pile of kernel regs.

But of course the same thing is true for the existing setup. So perhaps
that doesn't need to concern us now.

There used to be discussions about this case, and I think we had generic
code to sanitize such boundary events, but I can't seem to find that in
the current tree.

Mark, ISTR you were involved at some point, any idea what happened?