Skip to content

KVM: Serialize vCPU creation and revert vcpu_ids tracking

Sean Christopherson kvm kvm-riscv linux-coco linux-riscv linuxppc-dev lkml 2 versions – Applied

Status from this message: Queued, thanks.

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest7 patches 3 repliesApplied
  2. v15 patches 5 repliesSuperseded

Changes in v2 from the cover letter

- Tweak patch 1's changelog to clarify that that only x86's manual checks are dropped. [Sashiko]
- Add patches to convert additional arm64 and RISC-V usage to kvm_is_vcpu_creation_in_progress(). [Sashiko]
- Remove acquisition of kvm->lock from s390 and PPC vCPU creation flows. [Christian, Sashiko]
- Add Jean-Christophe's Tested-by to the revert.

Patches in v2 Applied

#Subject and filesSizeRepliesApplied
1/7KVM: Reject attempts to lock all vCPUs if vCPU creation is in-progressarch/x86/kvm/svm/sev.c arch/x86/kvm/vmx/tdx.c virt/kvm/kvm_main.c+6 -150 replies
2/7KVM: arm64: vgic: Rely on vCPU creation check in "trylock all vCPUs"arch/arm64/kvm/vgic/vgic-init.c+4 -80 replies
3/7KVM: RISC-V: Use kvm_is_vcpu_creation_in_progress() instead of open-coded equivalentarch/riscv/kvm/aia_device.c+1 -10 replies
4/7KVM: Protect all of kvm_vm_ioctl_create_vcpu() with kvm->lockarch/powerpc/kvm/book3s_hv.c arch/s390/kvm/s390/s390.c virt/kvm/kvm_main.c+6 -230 replies
5/7KVM: Move check for existing vCPU ID to the top of vCPU creationvirt/kvm/kvm_main.c+4 -60 replies
6/7Revert "KVM: Check for duplicate vcpu_id as early as possible"include/linux/kvm_host.h virt/kvm/kvm_main.c+0 -60 replies
7/7KVM: WARN if vCPU creation is in-progress when locking all vCPUsvirt/kvm/kvm_main.c+2 -20 replies

Reviews trailers per patch

None of the 7 patches has a Reviewed-by or Acked-by.

PatchJean-Christophe GuillainNaveen N Rao (AMD)
SeriesT-b T-b
1/7
2/7
3/7
4/7
5/7
6/7T-b
7/7

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/7] KVM: Serialize vCPU creation and revert vcpu_ids tracking

Serialize vCPU creation by holding kvm->lock for the entirety of kvm_vm_ioctl_create_vcpu(), and then revert the now-redundant tracking adding by commit 97d65b544f48 ("KVM: Check for duplicate vcpu_id as early as possible"). I botched the math when justifying the vcpu_ids tracking; it's not an extra 256 bytes, it's an extra 2048 bytes. Roughly doubling the size of "struct kvm" tripped x86's KVM_SANITY_CHECK_VM_STRUCT_SIZE, and obviously isn't something we want to do in general.

The TL;DR of why it's a-ok to serialize vCPU creation is that no VMM actually does parallel vCPU creation. As with so many things, KVM's current behavior is the result of decades-old cruft, not intentional, deliberate design.

Patches 1-3 are a tangentially related cleanups and bug fixes; I included them here because holding kvm->lock for all of vCPU creation allows WARNing if KVM attempts to lock all vCPUs if vCPU creation is in-progress (the caller must hold kvm->lock).

v2:
- Tweak patch 1's changelog to clarify that that only x86's manual checks are dropped. [Sashiko]
- Add patches to convert additional arm64 and RISC-V usage to kvm_is_vcpu_creation_in_progress(). [Sashiko]
- Remove acquisition of kvm->lock from s390 and PPC vCPU creation flows. [Christian, Sashiko]
- Add Jean-Christophe's Tested-by to the revert.

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (7):
KVM: Reject attempts to lock all vCPUs if vCPU creation is in-progress
KVM: arm64: vgic: Rely on vCPU creation check in "trylock all vCPUs"
KVM: RISC-V: Use kvm_is_vcpu_creation_in_progress() instead of
open-coded equivalent
KVM: Protect all of kvm_vm_ioctl_create_vcpu() with kvm->lock
KVM: Move check for existing vCPU ID to the top of vCPU creation Revert "KVM: Check for duplicate vcpu_id as early as possible"
KVM: WARN if vCPU creation is in-progress when locking all vCPUs

 arch/arm64/kvm/vgic/vgic-init.c | 12 ++++-------
 arch/powerpc/kvm/book3s_hv.c    |  2 --
 arch/riscv/kvm/aia_device.c     |  2 +-
 arch/s390/kvm/s390/s390.c       |  5 +----
 arch/x86/kvm/svm/sev.c          | 10 ----------
 arch/x86/kvm/vmx/tdx.c          |  5 -----
 include/linux/kvm_host.h        |  1 -
 virt/kvm/kvm_main.c             | 35 +++++++++++----------------------
 8 files changed, 17 insertions(+), 55 deletions(-)

base-commit: d599822bdb66aeec5ec76297b0fc6efaaeefe07c

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog