Re: [PATCH v9 04/20] KVM: x86: Support APX state for XSAVE ABI
From: Jim Mattson
Date: Wed Nov 18 2026 - 17:40:34 EST
On Tue, Nov 17, 2026 at 10:31 AM Chang S. Bae <chang.seok.bae@xxxxxxxxx> wrote:
>
> Introduce a facility to copy APX state between the VCPU cache and the
> userspace buffer as the VCPU cache is the single source of truth.
>
> The existing fpstate copy functions historically sync all XSTATEs in
> between userspace and kernel buffers [1]. In this regard, any additional
> state handling logic should be consistent with them -- i.e. validation of
> XSTATE_BV against the supported XCR0 mask.
>
> Now the two copy paths requires to take care of these facts:
s/requires to take care of/must account for/
>
> * When exporting to userspace, copy fpstate first, since it zeros the
> area of any component that is absent or inactive. Then copy the APX
> state from the VCPU cache.
>
> * When importing from userspace, the APX state is copied to both
> fpstate and the VCPU cache. Avoiding the duplicate copy would need
> special care in the common code or in XSTATE_BV/XCR0 which isn't
> worth that much for a slow path, so keep it simple.
>
> [1] Except for PKRU state, as stored in struct thread_struct.
>
> Tested-by: Zhao Liu <zhao1.liu@xxxxxxxxx>
> Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
> ---
> V8 -> V9:
> * Fix XSTATE_BV[APX]=0 behavior (Jim Mattson)
> * On the way to the kernel buffer, switch the copy ordering (Jim Mattson)
> ---
[ ... ]
> +#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.)
[ ... ]
> + /*
> + * The APX state was also copied into fpstate above, but the VCPU cache
> + * is the single source of truth. Skipping the redundant copy tends to
> + * introduce special-case handling. Since this lies in a slow path, keep
> + * the implementation simple.
> + */
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.