Skip to contentSkip to conversation

x86/fpu: Fix guest FPU state buffer allocation size

PATCH Applied Stanislav Spassov 2 participants 2 messages Feb 18, 2025 – Feb 21, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/fpu: Fix guest FPU state buffer allocation size

Ongoing work on an optimization to batch-preallocate vCPU state buffers for KVM revealed a mismatch between the allocation sizes used in fpu_alloc_guest_fpstate() and fpstate_realloc(). While the former allocates a buffer sized to fit the default set of XSAVE features in UABI form (as per fpu_user_cfg), the latter uses its ksize argument derived (for the requested set of features) in the same way as the sizes found in fpu_kernel_cfg, i.e. using the compacted in-kernel representation.

The correct size to use for guest FPU state should indeed be the kernel one as seen in fpstate_realloc(). The original issue likely went unnoticed through a combination of UABI size typically being larger than or equal to kernel size, and/or both amounting to the same number of allocated 4K pages.

Fixes 69f6ed1d14c6 ("x86/fpu: Provide infrastructure for KVM FPU cleanup")
---
 arch/x86/kernel/fpu/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/core.c +1 -1
index 1209c7aebb21..36df548acc40 100644
@@ -220,7 +220,7 @@ bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
 	struct fpstate *fpstate;
 	unsigned int size;
 
-	size = fpu_user_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
+	size = fpu_kernel_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
 	fpstate = vzalloc(size);
 	if (!fpstate)
 		return false;
Signature · 1 line
2.47.1
#1 lore raw
The following commit has been merged into the x86/fpu branch of tip:

The following commit has been merged into the x86/fpu branch of tip:

Commit-ID:     1937e18cc3cf27e2b3ef70e8c161437051ab7608
Gitweb:        https://git.kernel.org/tip/1937e18cc3cf27e2b3ef70e8c161437051ab7608
Author:        Stanislav Spassov <[email protected]>
AuthorDate:    Tue, 18 Feb 2025 14:10:45 
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 21 Feb 2025 14:47:26 +01:00

x86/fpu: Fix guest FPU state buffer allocation size

Ongoing work on an optimization to batch-preallocate vCPU state buffers for KVM revealed a mismatch between the allocation sizes used in fpu_alloc_guest_fpstate() and fpstate_realloc(). While the former allocates a buffer sized to fit the default set of XSAVE features in UABI form (as per fpu_user_cfg), the latter uses its ksize argument derived (for the requested set of features) in the same way as the sizes found in fpu_kernel_cfg, i.e. using the compacted in-kernel representation.

The correct size to use for guest FPU state should indeed be the kernel one as seen in fpstate_realloc(). The original issue likely went unnoticed through a combination of UABI size typically being larger than or equal to kernel size, and/or both amounting to the same number of allocated 4K pages.

Fixes 69f6ed1d14c6 ("x86/fpu: Provide infrastructure for KVM FPU cleanup")
---
 arch/x86/kernel/fpu/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/core.c +1 -1
index 1209c7a..36df548 100644
@@ -220,7 +220,7 @@ bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
 	struct fpstate *fpstate;
 	unsigned int size;
 
-	size = fpu_user_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
+	size = fpu_kernel_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
 	fpstate = vzalloc(size);
 	if (!fpstate)
 		return false;