mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Chang S. Bae" <chang.seok.bae@intel.com>
To: Jim Mattson <jmattson@google.com>,
	"Chang S. Bae" <chang.seok.bae@intel.com>
Cc: <pbonzini@redhat.com>, <seanjc@google.com>, <kvm@vger.kernel.org>,
	<x86@kernel.org>, <linux-kernel@vger.kernel.org>,
	<zhao1.liu@intel.com>
Subject: Re: [PATCH v8 03/20] KVM: x86: Support APX state for XSAVE ABI
Date: Thu, 5 Nov 2026 21:33:29 -0800	[thread overview]
Message-ID: <24be20bc-4089-4cf9-899b-8da1b2ee2825@intel.com> (raw)
In-Reply-To: <CALMp9eT1Wza=7s53Pu7ODcBRkYSbpv4A_y58xJNEhQeaJ6r1Rg@mail.gmail.com>

On 11/5/26 16:13, Jim Mattson wrote:
> On Wed, Sep 30, 2026 at 4:02 PM Chang S. Bae <chang.seok.bae@intel.com> wrote:
>>
> 
>> +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;
>> +
>> +       if (!(xstate->xsave.header.xfeatures & XFEATURE_MASK_APX))
>> +               return 0;
>> +
>> +       if (!(supported_xcr0 & XFEATURE_MASK_APX))
>> +               return -EINVAL;
> 
> Sashiko is right that this early return leaves stale values in R16-R31 when
> XSTATE_BV[19] is 0.
> 
> In your reply to Sashiko, you wrote:
> 
>    This matches the existing XSAVE uABI semantics. XSTATE_BV specifies
>    which state components are being updated. If userspace wants to zero
>    the APX state, it must include the APX bit in XSTATE_BV; omitting the
>    bit means "leave this component unchanged", just like the generic
>    XSAVE copy path.
> 
> That is not how XRSTOR or KVM_SET_XSAVE works. In the XSAVE architecture,
> XSTATE_BV[i] = 0 indicates that state component i is in its initial
> configuration (typically zeros), not that it should be left unchanged. In
> copy_uabi_to_xstate(), clearing bit i in xsave->header.xfeatures 

But copy_uabi_to_xstate() does not overwrite XSTATE_BV. Instead,

	/*
	 * Add back in the features that came in from userspace:
	 */
	xsave->header.xfeatures |= hdr.xfeatures;

This means that even if userspace clears a bit, the bit in the kernel 
XSAVE buffer remains set if it was already set. Consequently, clearing 
XSTATE_BV[i] in the userspace buffer does not necessarily reset the 
state component.

I'd follow up separately with update the wording, if needed, to match 
with this behavior.

>> @@ -3189,6 +3240,16 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
>>           */
>>          xstate->xsave.header.xfeatures &= ~vcpu->arch.guest_fpu.fpstate->xfd;
>>
>> +       /*
>> +        * This and the following copy functions copy APX state into each
>> +        * storage. The VCPU cache is the single source of truth but avoiding
>> +        * this redundant copy tends to introduce special-case handling. Since
>> +        * this lies in a slow path, keep the implementation simple.
>> +        */
>> +       err = kvm_copy_uabi_to_vcpu_regs(vcpu, guest_xsave->region, kvm_caps.supported_xcr0);
>> +       if (err)
>> +               return err;
>> +
>>          return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu,
>>                                                guest_xsave->region,
>>                                                kvm_caps.supported_xcr0,
> 
> If fpu_copy_uabi_to_guest_fpstate() fails, vcpu->arch.regs[R16..R31] will
> have already been clobbered.
> 
> Moreover, if you call kvm_copy_uabi_to_vcpu_regs() *after*
> fpu_copy_uabi_to_guest_fpstate() succeeds, you don't need to check
> supported_xcr0 in kvm_copy_uabi_to_vcpu_regs(), since
> fpu_copy_uabi_to_guest_fpstate() rejects any xfeatures bits not in
> kvm_caps.supported_xcr0.

I think this is a good point. Since it was decided to let APX state go 
through the fpstate anyway, it looks rather simplifying if switching the 
order.

Thanks,
Chang

  reply	other threads:[~2026-11-06  5:33 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-30 21:07 [PATCH v8 00/20] KVM: x86: Enable APX for guests Chang S. Bae
2026-09-30 21:07 ` [PATCH v8 01/20] KVM: x86: Extend VCPU registers for EGPRs Chang S. Bae
2026-11-05 23:42   ` Jim Mattson
2026-11-06  5:32     ` Chang S. Bae
2026-09-30 21:07 ` [PATCH v8 02/20] KVM: VMX: Save guest EGPRs in VCPU cache Chang S. Bae
2026-11-05 23:52   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 03/20] KVM: x86: Support APX state for XSAVE ABI Chang S. Bae
2026-11-06  0:13   ` Jim Mattson
2026-11-06  5:33     ` Chang S. Bae [this message]
2026-11-06  5:46       ` Jim Mattson
2026-11-06  5:57         ` Chang S. Bae
2026-09-30 21:07 ` [PATCH v8 04/20] KVM: VMX: Refactor VMX instruction information access Chang S. Bae
2026-11-06  0:17   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 05/20] KVM: VMX: Refactor instruction information decoding Chang S. Bae
2026-11-06  0:19   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 06/20] KVM: VMX: Remove unused control-register access defines Chang S. Bae
2026-11-06  0:20   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 07/20] KVM: VMX: Refactor register index retrieval from exit qualification Chang S. Bae
2026-11-06  0:22   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 08/20] KVM: VMX: Support instruction information extension Chang S. Bae
2026-11-06  0:40   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 09/20] KVM: nVMX: Propagate extended instruction information Chang S. Bae
2026-11-06  0:53   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 10/20] KVM: x86: Support EGPR accessing and tracking for emulator Chang S. Bae
2026-11-06  0:55   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 11/20] KVM: x86: Handle EGPR index and REX2-incompatible opcodes Chang S. Bae
2026-11-06  4:34   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 12/20] KVM: x86: Support REX2-prefixed opcode decode Chang S. Bae
2026-11-06  4:48   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 13/20] KVM: x86: Reject EVEX-prefixed instructions Chang S. Bae
2026-11-06  4:56   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 14/20] KVM: x86: Move KVM_SUPPORTED_{XCR0,XSS} into kvm_x86_vendor_init() Chang S. Bae
2026-11-06  4:58   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 15/20] KVM: x86: Guard valid XCR0.APX settings Chang S. Bae
2026-11-06  5:00   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 16/20] KVM: x86: Add APX to supported XCR0 Chang S. Bae
2026-11-06  5:01   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 17/20] KVM: x86: Expose APX foundation feature to userspace Chang S. Bae
2026-11-06  5:05   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 18/20] KVM: x86: Expose APX sub-features " Chang S. Bae
2026-09-30 21:07 ` [PATCH v8 19/20] KVM: x86: selftests: Add APX state and ABI test Chang S. Bae
2026-11-06  5:31   ` Jim Mattson
2026-09-30 21:07 ` [PATCH v8 20/20] KVM: x86: selftests: Add APX state handling and XCR0 sanity checks Chang S. Bae
2026-11-06  5:36   ` Jim Mattson
2026-10-01 20:29 ` [PATCH v8 00/20] KVM: x86: Enable APX for guests Chang S. Bae
2026-11-04  0:45   ` Chang S. Bae

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=24be20bc-4089-4cf9-899b-8da1b2ee2825@intel.com \
    --to=chang.seok.bae@intel.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=x86@kernel.org \
    --cc=zhao1.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®