Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kvm: introduce KVM_MAX_VCPU_ID

The KVM_MAX_VCPUS define provides the maximum number of vCPUs per guest, and
also the upper limit for vCPU ids. This is okay for all archs except PowerPC
which can have higher ids, depending on the cpu/core/thread topology. In the
worst case (single threaded guest, host with 8 threads per core), it limits
the maximum number of vCPUS to KVM_MAX_VCPUS / 8.

This patch separates the vCPU numbering from the total number of vCPUs, with
the introduction of KVM_MAX_VCPU_ID, as the maximal valid value for vCPU ids
plus one.

The corresponding KVM_CAP_MAX_VCPU_ID allows userspace to validate vCPU ids
before passing them to KVM_CREATE_VCPU.

This patch only implements KVM_MAX_VCPU_ID with a specific value for PowerPC.
Other archs continue to return KVM_MAX_VCPUS instead.

Suggested-by: Radim Krcmar <rkrcmar@redhat.com>
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
Reviewed-by: Cornelia Huck <cornelia.huck@de.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Greg Kurz and committed by
Paolo Bonzini
0b1b1dfd 9b9e3fc4

+19 -3
+8 -2
Documentation/virtual/kvm/api.txt
··· 199 199 Parameters: vcpu id (apic id on x86) 200 200 Returns: vcpu fd on success, -1 on error 201 201 202 - This API adds a vcpu to a virtual machine. The vcpu id is a small integer 203 - in the range [0, max_vcpus). 202 + This API adds a vcpu to a virtual machine. No more than max_vcpus may be added. 203 + The vcpu id is an integer in the range [0, max_vcpu_id). 204 204 205 205 The recommended max_vcpus value can be retrieved using the KVM_CAP_NR_VCPUS of 206 206 the KVM_CHECK_EXTENSION ioctl() at run-time. ··· 211 211 cpus max. 212 212 If the KVM_CAP_MAX_VCPUS does not exist, you should assume that max_vcpus is 213 213 same as the value returned from KVM_CAP_NR_VCPUS. 214 + 215 + The maximum possible value for max_vcpu_id can be retrieved using the 216 + KVM_CAP_MAX_VCPU_ID of the KVM_CHECK_EXTENSION ioctl() at run-time. 217 + 218 + If the KVM_CAP_MAX_VCPU_ID does not exist, you should assume that max_vcpu_id 219 + is the same as the value returned from KVM_CAP_MAX_VCPUS. 214 220 215 221 On powerpc using book3s_hv mode, the vcpus are mapped onto virtual 216 222 threads in one or more virtual CPU cores. (This is because the
+3
arch/powerpc/include/asm/kvm_host.h
··· 40 40 #define KVM_MAX_VCORES NR_CPUS 41 41 #define KVM_USER_MEM_SLOTS 512 42 42 43 + #include <asm/cputhreads.h> 44 + #define KVM_MAX_VCPU_ID (threads_per_subcore * KVM_MAX_VCORES) 45 + 43 46 #ifdef CONFIG_KVM_MMIO 44 47 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1 45 48 #endif
+4
include/linux/kvm_host.h
··· 35 35 36 36 #include <asm/kvm_host.h> 37 37 38 + #ifndef KVM_MAX_VCPU_ID 39 + #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS 40 + #endif 41 + 38 42 /* 39 43 * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used 40 44 * in kvm, other bits are visible for userspace which are defined in
+1
include/uapi/linux/kvm.h
··· 865 865 #define KVM_CAP_SPAPR_TCE_64 125 866 866 #define KVM_CAP_ARM_PMU_V3 126 867 867 #define KVM_CAP_VCPU_ATTRIBUTES 127 868 + #define KVM_CAP_MAX_VCPU_ID 128 868 869 869 870 #ifdef KVM_CAP_IRQ_ROUTING 870 871
+3 -1
virt/kvm/kvm_main.c
··· 2272 2272 int r; 2273 2273 struct kvm_vcpu *vcpu; 2274 2274 2275 - if (id >= KVM_MAX_VCPUS) 2275 + if (id >= KVM_MAX_VCPU_ID) 2276 2276 return -EINVAL; 2277 2277 2278 2278 vcpu = kvm_arch_vcpu_create(kvm, id); ··· 2746 2746 case KVM_CAP_MULTI_ADDRESS_SPACE: 2747 2747 return KVM_ADDRESS_SPACE_NUM; 2748 2748 #endif 2749 + case KVM_CAP_MAX_VCPU_ID: 2750 + return KVM_MAX_VCPU_ID; 2749 2751 default: 2750 2752 break; 2751 2753 }