vmm: Allocate vCPUs on first use of a vCPU.
authorJohn Baldwin <jhb@FreeBSD.org>
Fri, 18 Nov 2022 18:05:35 +0000 (10:05 -0800)
committerJohn Baldwin <jhb@FreeBSD.org>
Fri, 18 Nov 2022 18:25:38 +0000 (10:25 -0800)
commit98568a005a193ce2c37702a8377ddd10c570e452
tree87468108d3ed2cd1d766590a5a5830e435deeea5
parentc0f35dbf19c3c8825bd2b321d8efd582807d1940
vmm: Allocate vCPUs on first use of a vCPU.

Convert the vcpu[] array in struct vm to an array of pointers and
allocate vCPUs on first use.  This avoids always allocating VM_MAXCPU
vCPUs for each VM, but instead only allocates the vCPUs in use.  A new
per-VM sx lock is added to serialize attempts to allocate vCPUs on
first use.  However, a given vCPU is never freed while the VM is
active, so the pointer is read via an unlocked read first to avoid the
need for the lock in the common case once the vCPU has been created.

Some ioctls need to lock all vCPUs.  To prevent races with ioctls that
want to allocate a new vCPU, these ioctls also lock the sx lock that
protects vCPU creation.

Reviewed by: corvink, markj
Differential Revision: https://reviews.freebsd.org/D37174
sys/amd64/include/vmm.h
sys/amd64/vmm/io/vlapic.c
sys/amd64/vmm/vmm.c
sys/amd64/vmm/vmm_dev.c