All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: David Woodhouse <dwmw2@infradead.org>
Cc: sashiko-reviews@lists.linux.dev, kvm@vger.kernel.org,
	 Paul Durrant <paul@xen.org>
Subject: Re: [PATCH v2 4/8] KVM: Initialize a vCPU's index to '-1' while it's being created
Date: Mon, 15 Jun 2026 07:12:09 -0700	[thread overview]
Message-ID: <ajAIOVLzf6AriO5r@google.com> (raw)
In-Reply-To: <a8eefe40b2b0a0a3d4afd675ed4fea6762934fdc.camel@infradead.org>

On Sat, Jun 13, 2026, David Woodhouse wrote:
> On Fri, 2026-06-12 at 16:40 -0700, Sean Christopherson wrote:
> > > 
> > > arch/x86/kvm/xen.c:kvm_xen_init_vcpu() {
> > >     ...
> > >     vcpu->arch.xen.vcpu_id = vcpu->vcpu_idx;
> > 
> > I don't see how the existing code can be correct.  David/Paul, is this supposed
> > to be vcpu->vcpu_id?
> 
> No, vcpu->vcpu_id is the APIC ID. 
> 
> But xen.vcpu_id is the Xen/ACPI ID. The kernel needs to know it in
> order to accelerate timer hypercalls. The vcpu->vcpu_idx is a
> reasonable default for that.

Right, but isn't vcpu_id also a reasonable default?  Ugh, never mind.  Even if
it's conceptually a reasonable default, in practice using vcpu_id would lead to
creating an illegal xen.vcpu_id since KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID restricts
the ID to KVM_MAX_VCPUS, not KVM_MAX_VCPU_IDS (which is KVM_MAX_VCPUS * 4).

	case KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID:
		if (data->u.vcpu_id >= KVM_MAX_VCPUS)
			r = -EINVAL;
		else {
			vcpu->arch.xen.vcpu_id = data->u.vcpu_id;
			r = 0;
		}
		break;

> (As if this wasn't confusing enough, note ACPI != APIC and neither of
> those are typos)
> 
> > >     ...
> > > }
> > > 
> > > Since the actual vcpu_idx isn't assigned until later in
> > > kvm_vm_ioctl_create_vcpu(), this leaves vcpu->arch.xen.vcpu_id as -1 instead
> > > of its previous default of 0.
> > > 
> > > Could this cause guest hypercalls, such as VCPUOP_set_singleshot_timer,
> > > to fail with -EINVAL if a VMM relies on the previous default of 0 for vCPU 0
> > > and doesn't explicitly invoke KVM_XEN_VCPU_SET_ATTR?
> 
> Yeah, but it would be tolerable to just return !handled for the -1 case
> instead of "I handled that and it got -EINVAL". Then the hypercall gets
> punted to userspace.

Are you thinking this?

diff --git arch/x86/kvm/xen.c arch/x86/kvm/xen.c
index 694b31c1fcc9..7a94b84f6492 100644
--- arch/x86/kvm/xen.c
+++ arch/x86/kvm/xen.c
@@ -1607,7 +1607,7 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_vcpu *vcpu, bool longmode, int cmd,
        struct vcpu_set_singleshot_timer oneshot;
        struct x86_exception e;
 
-       if (!kvm_xen_timer_enabled(vcpu))
+       if (!kvm_xen_timer_enabled(vcpu) || vcpu->arch.xen.vcpu_id < 0)
                return false;
 
        switch (cmd) {

> I'd slightly prefer a deferred initialization, but I believe all known
> userspace explicitly *sets* the ID via KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID

Yeah, userspace pretty much has to, otherwise only vCPU0 could do
VCPUOP_set_singleshot_timer.  If we were implementing this for the first time,
I would be 100% in favor of deferred initialization, but since this will be an
ABI change, explicitly initializing xen.vcpu_id to -1 feels "safer", because at
least then there will be a fairly noisy failure (exit to userspace), versus the
guest silently getting different behavior.


  reply	other threads:[~2026-06-15 14:12 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-12 23:06 [PATCH v2 0/8] KVM: x86/hyperv: Fix racy usage of vcpu->arch.hyperv Sean Christopherson
2026-06-12 23:06 ` [PATCH v2 1/8] KVM: x86/hyperv: Get target FIFO in hv_tlb_flush_enqueue(), not caller Sean Christopherson
2026-06-12 23:06 ` [PATCH v2 2/8] KVM: x86/hyperv: Check for NULL vCPU Hyper-V object in kvm_hv_get_tlb_flush_fifo() Sean Christopherson
2026-06-12 23:06 ` [PATCH v2 3/8] KVM: x86/hyperv: Ensure vCPU's Hyper-V object is initialized on cross-vCPU accesses Sean Christopherson
2026-06-12 23:22   ` sashiko-bot
2026-06-13  0:20     ` Sean Christopherson
2026-06-12 23:06 ` [PATCH v2 4/8] KVM: Initialize a vCPU's index to '-1' while it's being created Sean Christopherson
2026-06-12 23:30   ` sashiko-bot
2026-06-12 23:40     ` Sean Christopherson
2026-06-13  0:49       ` David Woodhouse
2026-06-15 14:12         ` Sean Christopherson [this message]
2026-06-12 23:06 ` [PATCH v2 5/8] KVM: Move nVMX's lockdep logic for vcpu->mutex to a common helper Sean Christopherson
2026-06-12 23:06 ` [PATCH v2 6/8] KVM: x86: Treat a vCPU as unreachable if its index is invalid Sean Christopherson
2026-06-12 23:06 ` [PATCH v2 7/8] KVM: x86/hyperv: Assert vCPU's mutex is held in to_hv_vcpu() Sean Christopherson
2026-06-12 23:06 ` [PATCH v2 8/8] KVM: x86/hyperv: Use {READ,WRITE}_ONCE for cross-task synic->active accesses Sean Christopherson
2026-06-13 20:38 ` [syzbot ci] Re: KVM: x86/hyperv: Fix racy usage of vcpu->arch.hyperv syzbot ci
2026-06-15 14:28   ` Sean Christopherson

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=ajAIOVLzf6AriO5r@google.com \
    --to=seanjc@google.com \
    --cc=dwmw2@infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=paul@xen.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.