Re: [PATCH v9 04/20] KVM: x86: Support APX state for XSAVE ABI
From: Chang S. Bae
Date: Wed Nov 18 2026 - 20:55:30 EST
On 11/18/2026 2:39 PM, Jim Mattson wrote:
+#ifdef CONFIG_KVM_APX
+static void kvm_copy_vcpu_regs_to_uabi(struct kvm_vcpu *vcpu, void *buf, u64 supported_xcr0)
+{
+ union fpregs_state *xstate = (union fpregs_state *)buf;
+
+ BUILD_BUG_ON(NR_VCPU_GENERAL_PURPOSE_REGS <= VCPU_REGS_R31);
+
+ if (!(supported_xcr0 & XFEATURE_MASK_APX))
+ return;
Now that this is called only after fpu_copy_uabi_to_guest_fpstate()
succeeds, this condition cannot be true. fpu_copy_uabi_to_guest_fpstate()
has already rejected any XSTATE_BV bits that aren't in
kvm_caps.supported_xcr0. (That was the second half of my comment on v8.)
I assume you meant kvm_copy_uabi_to_vcpu_regs() since that's the function after kvm_copy_uabi_to_vcpu_regs().
Then, the check can be skipped when XSTATE_BV[APX] is set, as fpu_copy_uabi_to_guest_fpstate() has already validated the XCR0 support. But when XSTATE_BV[APX] is clear, still need to check supported_xcr0 before zeroing the VCPU cache of EGPRs.
While the sanity check is redundant, the rational for the removal has to be documented, So,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 84d9a71e14e3..c559ae29b916 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3169,6 +3169,10 @@ static void kvm_copy_vcpu_regs_to_uabi(struct kvm_vcpu *vcpu, void *buf, u64 sup
xstate->xsave.header.xfeatures |= XFEATURE_MASK_APX;
}
+/*
+ * Must call this after successful fpu_copy_uabi_to_guest_fpstate(),
+ * which means APX is XCR0-supported if XSTATE_BV[APX] is set.
+ */
static int kvm_copy_uabi_to_vcpu_regs(struct kvm_vcpu *vcpu, void *buf, u64 supported_xcr0)
{
union fpregs_state *xstate = (union fpregs_state *)buf;
@@ -3182,9 +3186,6 @@ static int kvm_copy_uabi_to_vcpu_regs(struct kvm_vcpu *vcpu, void *buf, u64 supp
return 0;
}
- if (!(supported_xcr0 & XFEATURE_MASK_APX))
- return -EINVAL;
-
memcpy(&vcpu->arch.regs[VCPU_REGS_R16],
buf + xstate_offset(XFEATURE_APX),
xstate_size(XFEATURE_APX));
I think I ended up leaving the checker here rather than adding the code comment, but I'm fine with removing the redundant check.
With vcpu->arch.regs[] being the single source of truth, how hard would it
be to keep the APX state out of guest_fpu altogether? It seems like that
would be cleaner.
So, the the guest fpstate has to stay in sync with the VCPU cache.
On VM entry/exit, the guest XSTATE remains in the physical registers. When returning to userspace, or preempted, it is saved to the guest fpstate storage. This part is already handled, so straightforward.
But the complication is with emulator and VMX exit handling, where those registers need to be updated through accessors, similar to how {X|Y}MM registers are handled in fpu.h. It thus requires bringing back the ugly switch statement [*] that previously was dropped out, or adding something similar that is unlikely to be any simpler.
[*] https://lore.kernel.org/kvm/20260112235408.168200-3-chang.seok.bae@xxxxxxxxx/
Thanks,
Chang