Re: [PATCH v9 8/9] KVM: x86: Allow userspace set maximum VCPU id for VM

From: Paolo Bonzini
Date: Mon May 02 2022 - 12:07:23 EST


On 4/19/22 17:44, Zeng Guang wrote:
+Userspace is able to calculate the limit to APIC ID values from designated CPU
+topology. This capability allows userspace to specify maximum possible APIC ID
+assigned for current VM session prior to the creation of vCPUs. By design, it
+can set only once and doesn't accept change any more. KVM will manage memory
+allocation of VM-scope structures which depends on the value of APIC ID.
+
+Calling KVM_CHECK_EXTENSION for this capability returns the value of maximum APIC
+ID that KVM supports at runtime. It sets as KVM_MAX_VCPU_IDS by default.

Better:

This capability allows userspace to specify maximum possible APIC ID
assigned for current VM session prior to the creation of vCPUs, saving
memory for data structures indexed by the APIC ID. Userspace is able
to calculate the limit to APIC ID values from designated
CPU topology.

The value can be changed only until KVM_ENABLE_CAP is set to a nonzero
value or until a vCPU is created. Upon creation of the first vCPU,
if the value was set to zero or KVM_ENABLE_CAP was not invoked, KVM
uses the return value of KVM_CHECK_EXTENSION(KVM_CAP_MAX_VCPU_ID) as
the maximum APIC ID.

case KVM_CAP_MAX_VCPU_ID:
- r = KVM_MAX_VCPU_IDS;
+ if (!kvm->arch.max_vcpu_ids)
+ r = KVM_MAX_VCPU_IDS;
+ else
+ r = kvm->arch.max_vcpu_ids;

I think returning the constant KVM_CAP_MAX_VCPU_IDS is better.

Paolo