]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
x86/fpu: Initialize guest fpstate and FPU pseudo container from guest defaults
authorChao Gao <chao.gao@intel.com>
Thu, 22 May 2025 15:10:06 +0000 (08:10 -0700)
committerDave Hansen <dave.hansen@linux.intel.com>
Tue, 24 Jun 2025 20:46:32 +0000 (13:46 -0700)
fpu_alloc_guest_fpstate() currently uses host defaults to initialize guest
fpstate and pseudo containers. Guest defaults were introduced to
differentiate the features and sizes of host and guest FPUs. Switch to
using guest defaults instead.

Adjust __fpstate_reset() to handle different defaults for host and guest
FPUs. And to distinguish between the types of FPUs, move the initialization
of indicators (is_guest and is_valloc) before the reset.

Suggested-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Chao Gao <chao.gao@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: John Allen <john.allen@amd.com>
Link: https://lore.kernel.org/all/20250522151031.426788-4-chao.gao%40intel.com
arch/x86/kernel/fpu/core.c

index 94706a596aaa9b203d9c5cfc4c09ed1d18423f61..e02705187bbf48679d94709dca374040012cf962 100644 (file)
@@ -243,19 +243,22 @@ bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
        struct fpstate *fpstate;
        unsigned int size;
 
-       size = fpu_kernel_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
+       size = guest_default_cfg.size + ALIGN(offsetof(struct fpstate, regs), 64);
+
        fpstate = vzalloc(size);
        if (!fpstate)
                return false;
 
+       /* Initialize indicators to reflect properties of the fpstate */
+       fpstate->is_valloc      = true;
+       fpstate->is_guest       = true;
+
        /* Leave xfd to 0 (the reset value defined by spec) */
        __fpstate_reset(fpstate, 0);
        fpstate_init_user(fpstate);
-       fpstate->is_valloc      = true;
-       fpstate->is_guest       = true;
 
        gfpu->fpstate           = fpstate;
-       gfpu->xfeatures         = fpu_kernel_cfg.default_features;
+       gfpu->xfeatures         = guest_default_cfg.features;
 
        /*
         * KVM sets the FP+SSE bits in the XSAVE header when copying FPU state
@@ -544,10 +547,22 @@ void fpstate_init_user(struct fpstate *fpstate)
 
 static void __fpstate_reset(struct fpstate *fpstate, u64 xfd)
 {
-       /* Initialize sizes and feature masks */
-       fpstate->size           = fpu_kernel_cfg.default_size;
+       /*
+        * Supervisor features (and thus sizes) may diverge between guest
+        * FPUs and host FPUs, as some supervisor features are supported
+        * for guests despite not being utilized by the host. User
+        * features and sizes are always identical, which allows for
+        * common guest and userspace ABI.
+        */
+       if (fpstate->is_guest) {
+               fpstate->size           = guest_default_cfg.size;
+               fpstate->xfeatures      = guest_default_cfg.features;
+       } else {
+               fpstate->size           = fpu_kernel_cfg.default_size;
+               fpstate->xfeatures      = fpu_kernel_cfg.default_features;
+       }
+
        fpstate->user_size      = fpu_user_cfg.default_size;
-       fpstate->xfeatures      = fpu_kernel_cfg.default_features;
        fpstate->user_xfeatures = fpu_user_cfg.default_features;
        fpstate->xfd            = xfd;
 }