From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
"Jean-Christophe Guillain" <jean-christophe@guillain.net>,
"Paweł S" <spawel523@gmail.com>
Subject: [PATCH 3/5] KVM: Move check for existing vCPU ID to the top of vCPU creation
Date: Mon, 14 Sep 2026 11:12:21 -0700 [thread overview]
Message-ID: <20260914181223.289061-4-seanjc@google.com> (raw)
In-Reply-To: <20260914181223.289061-1-seanjc@google.com>
Now that kvm->lock is held for the entirety of vCPU creation, check for a
conflicting vCPU ID at the begnning of vCPU creation, before the arch
precreate() hook is invoked. This will allow reverting commit 97d65b544f48
("KVM: Check for duplicate vcpu_id as early as possible"). For now, keep
the redundant vcpu_ids tracking as a sanity check.
No functional change intended (absent KVM bugs, checking vcpu_ids and
walking kvm_get_vcpu_by_id() should yield the same result).
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
virt/kvm/kvm_main.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index c17cc8dd371b..d5524ac8c5cf 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4182,7 +4182,10 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
if (kvm->created_vcpus >= kvm->max_vcpus)
return -EINVAL;
- if (test_bit(id, kvm->vcpu_ids))
+ if (kvm_get_vcpu_by_id(kvm, id))
+ return -EEXIST;
+
+ if (WARN_ON_ONCE(test_bit(id, kvm->vcpu_ids)))
return -EEXIST;
r = kvm_arch_vcpu_precreate(kvm, id);
@@ -4221,11 +4224,6 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
goto arch_vcpu_destroy;
}
- if (WARN_ON_ONCE(kvm_get_vcpu_by_id(kvm, id))) {
- r = -EEXIST;
- goto unlock_vcpu_destroy;
- }
-
/*
* Set the vCPU's index *before* the vCPU is reachable by other tasks.
* Unwind the index back to -1 on failure so that KVM can use the index
--
2.55.0.1032.g73a4cd73de-goog
next prev parent reply other threads:[~2026-09-14 18:12 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-14 18:12 [PATCH 0/5] KVM: Serialize vCPU creation and revert vcpu_ids tracking Sean Christopherson
2026-09-14 18:12 ` [PATCH 1/5] KVM: Reject attempts to lock all vCPUs if vCPU creation is in-progress Sean Christopherson
2026-09-14 18:24 ` sashiko-bot
2026-09-14 18:12 ` [PATCH 2/5] KVM: Protect all of kvm_vm_ioctl_create_vcpu() with kvm->lock Sean Christopherson
2026-09-14 18:37 ` sashiko-bot
2026-09-14 18:12 ` Sean Christopherson [this message]
2026-09-14 18:12 ` [PATCH 4/5] Revert "KVM: Check for duplicate vcpu_id as early as possible" Sean Christopherson
2026-09-14 18:12 ` [PATCH 5/5] KVM: WARN if vCPU creation is in-progress when locking all vCPUs Sean Christopherson
2026-09-14 18:39 ` [PATCH 0/5] KVM: Serialize vCPU creation and revert vcpu_ids tracking Christian Borntraeger
2026-09-15 13:13 ` Jean-Christophe Guillain
2026-09-21 13:57 ` Thorsten Leemhuis
2026-09-21 14:00 ` Paolo Bonzini
2026-09-21 14:11 ` 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=20260914181223.289061-4-seanjc@google.com \
--to=seanjc@google.com \
--cc=jean-christophe@guillain.net \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=spawel523@gmail.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 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.