[SRU][J][PATCH 1/3] kvm: x86: Fix xstate_required_size() to follow XSTATE alignment rule

Matthew Ruffell matthew.ruffell at canonical.com
Wed Jan 8 03:53:31 UTC 2025


From: Jing Liu <jing2.liu at intel.com>

BugLink: https://bugs.launchpad.net/bugs/2093146

CPUID.0xD.1.EBX enumerates the size of the XSAVE area (in compacted
format) required by XSAVES. If CPUID.0xD.i.ECX[1] is set for a state
component (i), this state component should be located on the next
64-bytes boundary following the preceding state component in the
compacted layout.

Fix xstate_required_size() to follow the alignment rule. AMX is the
first state component with 64-bytes alignment to catch this bug.

Signed-off-by: Jing Liu <jing2.liu at intel.com>
Signed-off-by: Yang Zhong <yang.zhong at intel.com>
Message-Id: <20220105123532.12586-4-yang.zhong at intel.com>
Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
(cherry picked from commit cc04b6a21d431359eceeec0d812b492088b04af5)
Signed-off-by: Matthew Ruffell <matthew.ruffell at canonical.com>
---
 arch/x86/kvm/cpuid.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 786584a9904c..23a35b68476c 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -42,7 +42,11 @@ static u32 xstate_required_size(u64 xstate_bv, bool compacted)
 		if (xstate_bv & 0x1) {
 		        u32 eax, ebx, ecx, edx, offset;
 		        cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
-			offset = compacted ? ret : ebx;
+			/* ECX[1]: 64B alignment in compacted form */
+			if (compacted)
+				offset = (ecx & 0x2) ? ALIGN(ret, 64) : ret;
+			else
+				offset = ebx;
 			ret = max(ret, offset + eax);
 		}
 
-- 
2.45.2




More information about the kernel-team mailing list