]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86: Zero XSTATE components on INIT by iterating over supported features
authorChao Gao <chao.gao@intel.com>
Tue, 12 Aug 2025 02:55:13 +0000 (19:55 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 19 Aug 2025 18:59:51 +0000 (11:59 -0700)
Tweak the code a bit to facilitate resetting more xstate components in
the future, e.g., CET's xstate-managed MSRs.

No functional change intended.

Suggested-by: Sean Christopherson <seanjc@google.com>
Tested-by: Mathias Krause <minipli@grsecurity.net>
Tested-by: John Allen <john.allen@amd.com>
Signed-off-by: Chao Gao <chao.gao@intel.com>
Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Link: https://lore.kernel.org/r/20250812025606.74625-6-chao.gao@intel.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/x86.c

index d398ee84c8f3e878d9686b6eff9dc1c8f0fdc32e..8bfba7d8f7509e06008a3c3bc3a1e52bdc9af690 100644 (file)
@@ -12401,6 +12401,8 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 static void kvm_xstate_reset(struct kvm_vcpu *vcpu, bool init_event)
 {
        struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate;
+       u64 xfeatures_mask;
+       int i;
 
        /*
         * Guest FPU state is zero allocated and so doesn't need to be manually
@@ -12414,16 +12416,20 @@ static void kvm_xstate_reset(struct kvm_vcpu *vcpu, bool init_event)
         * are unchanged.  Currently, the only components that are zeroed and
         * supported by KVM are MPX related.
         */
-       if (!kvm_mpx_supported())
+       xfeatures_mask = (kvm_caps.supported_xcr0 | kvm_caps.supported_xss) &
+                        (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
+       if (!xfeatures_mask)
                return;
 
+       BUILD_BUG_ON(sizeof(xfeatures_mask) * BITS_PER_BYTE <= XFEATURE_MAX);
+
        /*
         * All paths that lead to INIT are required to load the guest's FPU
         * state (because most paths are buried in KVM_RUN).
         */
        kvm_put_guest_fpu(vcpu);
-       fpstate_clear_xstate_component(fpstate, XFEATURE_BNDREGS);
-       fpstate_clear_xstate_component(fpstate, XFEATURE_BNDCSR);
+       for_each_set_bit(i, (unsigned long *)&xfeatures_mask, XFEATURE_MAX)
+               fpstate_clear_xstate_component(fpstate, i);
        kvm_load_guest_fpu(vcpu);
 }