x86/kvm: add boot parameters for max vcpu configs
Subsystems (7)
v2 cover x86/kvm: add boot parameters for max vcpu configs
In order to be able to have a single kernel for supporting even huge numbers of vcpus per guest some arrays should be sized dynamically.
The easiest way to do that is to add boot parameters for the maximum number of vcpus and to calculate the maximum vcpu-id from that using either the host topology or a topology hint via another boot parameter.
This patch series is doing that for x86. The same scheme can be easily adapted to other architectures, but I don't want to do that in the first iteration.
In the long term I'd suggest to have a per-guest setting of the two parameters allowing to spare some memory for smaller guests. OTOH this would require new ioctl()s and respective qemu modifications, so I let those away for now.
I've tested the series not to break normal guest operation and the new parameters to be effective on x86. For Arm64 I did a compile test only.
Changes in V2:
- removed old patch 1, as already applied
- patch 1 (old patch 2) only for reference, as the patch is already in the kvm tree
- switch patch 2 (old patch 3) to calculate vcpu-id
- added patch 4
Juergen Gross (6):
x86/kvm: remove non-x86 stuff from arch/x86/kvm/ioapic.h x86/kvm: add boot parameter for adding vcpu-id bits x86/kvm: introduce per cpu vcpu masks
kvm: use kvfree() in kvm_arch_free_vm()
kvm: allocate vcpu pointer array separately
x86/kvm: add boot parameter for setting max number of vcpus per guest
.../admin-guide/kernel-parameters.txt | 25 ++++++ arch/arm64/include/asm/kvm_host.h | 1 - arch/arm64/kvm/arm.c | 23 ++++-- arch/x86/include/asm/kvm_host.h | 26 +++++-- arch/x86/kvm/hyperv.c | 25 ++++-- arch/x86/kvm/ioapic.c | 12 ++- arch/x86/kvm/ioapic.h | 8 +- arch/x86/kvm/irq_comm.c | 9 ++- arch/x86/kvm/x86.c | 78 ++++++++++++++++++- include/linux/kvm_host.h | 26 ++++++- 10 files changed, 198 insertions(+), 35 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/6 x86/kvm: remove non-x86 stuff from arch/x86/kvm/ioapic.h
The file has been moved to arch/x86 long time ago. Time to get rid of non-x86 stuff.
---
V2:
- patch only kept for reference, has already been added to kvm.git
--- arch/x86/kvm/ioapic.h | 4 ---- 1 file changed, 4 deletions(-)
arch/x86/kvm/ioapic.h +0 -4
@@ -35,11 +35,7 @@ struct kvm_vcpu;#define IOAPIC_INIT 0x5#define IOAPIC_EXTINT 0x7 -#ifdef CONFIG_X86 #define RTC_GSI 8 -#else -#define RTC_GSI -1U -#endif struct dest_map {/* vcpu bitmap where IRQ has been sent */
Signature · 1 line
2.26.2
PATCH v2 2/6 x86/kvm: add boot parameter for adding vcpu-id bits
In reply to Juergen GrossToday the maximum vcpu-id of a kvm guest's vcpu on x86 systems is set via a #define in a header file.
In order to support higher vcpu-ids without generally increasing the memory consumption of guests on the host (some guest structures contain arrays sized by KVM_MAX_VCPU_ID) add a boot parameter for adding some bits to the vcpu-id. Additional bits are needed as the vcpu-id is constructed via bit-wise concatenation of socket-id, core-id, etc. As those ids maximum values are not always a power of 2, the vcpu-ids are sparse.
The additional number of bits needed is basically the number of topology levels with a non-power-of-2 maximum value, excluding the top most level.
The default value of the new parameter will be to take the correct setting from the host's topology.
Calculating the maximum vcpu-id dynamically requires to allocate the arrays using KVM_MAX_VCPU_ID as the size dynamically.
---
V2:
- switch to specifying additional bits (based on comment by Vitaly Kuznetsov)
--- .../admin-guide/kernel-parameters.txt | 18 ++++++++++++ arch/x86/include/asm/kvm_host.h | 4 ++- arch/x86/kvm/ioapic.c | 12 +++++++- arch/x86/kvm/ioapic.h | 4 +-- arch/x86/kvm/x86.c | 29 +++++++++++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +18 -0
@@ -2435,6 +2435,24 @@ feature (tagged TLBs) on capable Intel chips. Default is 1 (enabled) + kvm.vcpu_id_add_bits= + [KVM,X86] The vcpu-ids of guests are sparse, as they + are constructed by bit-wise concatenation of the ids of + the different topology levels (sockets, cores, threads). + + This parameter specifies how many additional bits the + maximum vcpu-id needs compared to the maximum number of + vcpus. + + Normally this value is the number of topology levels + without the threads level and without the highest + level. + + The special value -1 can be used to support guests + with the same topology is the host. + + Default: -1 + l1d_flush= [X86,INTEL] Control mitigation for L1D based snooping vulnerability.
arch/x86/include/asm/kvm_host.h +3 -1
@@ -39,7 +39,7 @@#define KVM_MAX_VCPUS 288#define KVM_SOFT_MAX_VCPUS 240 -#define KVM_MAX_VCPU_ID 1023 +#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3
@@ -1588,6 +1588,8 @@ extern u64 kvm_max_tsc_scaling_ratio;extern u64 kvm_default_tsc_scaling_ratio;/* bus lock detection supported? */ extern bool kvm_has_bus_lock_exit; +/* maximum vcpu-id */ +unsigned int kvm_max_vcpu_id (void );extern u64 kvm_mce_cap_supported;
arch/x86/kvm/ioapic.c +11 -1
@@ -685,11 +685,21 @@ static const struct kvm_io_device_ops ioapic_mmio_ops = {int kvm_ioapic_init (struct kvm *kvm) {struct kvm_ioapic *ioapic; +size_t sz;int ret; - ioapic =kzalloc (sizeof (struct kvm_ioapic ),GFP_KERNEL_ACCOUNT ); + sz =sizeof (struct kvm_ioapic ) + +sizeof (*ioapic->rtc_status.dest_map.map) * +BITS_TO_LONGS (KVM_MAX_VCPU_ID +1 ) + +sizeof (*ioapic->rtc_status.dest_map.vectors) * + (KVM_MAX_VCPU_ID +1 ); + ioapic =kzalloc (sz,GFP_KERNEL_ACCOUNT );if (!ioapic)return -ENOMEM ; + ioapic->rtc_status.dest_map.map = (void *)(ioapic +1 ); + ioapic->rtc_status.dest_map.vectors = + (void *)(ioapic->rtc_status.dest_map.map + +BITS_TO_LONGS (KVM_MAX_VCPU_ID +1 ));spin_lock_init (&ioapic->lock);INIT_DELAYED_WORK (&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work); kvm->arch.vioapic = ioapic;
arch/x86/kvm/ioapic.h +2 -2
@@ -39,13 +39,13 @@ struct kvm_vcpu;struct dest_map {/* vcpu bitmap where IRQ has been sent */ - DECLARE_BITMAP(map, KVM_MAX_VCPU_ID + 1); +unsigned long *map;/* * Vector sent to a given vcpu, only valid when * the vcpu's bit in map is set */ -u8 vectors[KVM_MAX_VCPU_ID +1 ]; +u8 *vectors; };
arch/x86/kvm/x86.c +29 -0
@@ -78,6 +78,7 @@#include <asm/intel_pt.h> #include <asm/emulate_prefix.h> #include <asm/sgx.h> +#include <asm/topology.h> #include <clocksource/hyperv_timer.h> #define CREATE_TRACE_POINTS
@@ -184,6 +185,34 @@ module_param(force_emulation_prefix, bool, S_IRUGO);int __read_mostly pi_inject_timer =-1 ;module_param (pi_inject_timer, bint,S_IRUGO |S_IWUSR ); +static int __read_mostly vcpu_id_add_bits =-1 ; +module_param (vcpu_id_add_bits, int,S_IRUGO ); + +unsigned int kvm_max_vcpu_id (void ) +{ +int n_bits =fls (KVM_MAX_VCPUS -1 ); + +if (vcpu_id_add_bits <-1 || vcpu_id_add_bits > (32 - n_bits)) { +pr_err ("Invalid value of vcpu_id_add_bits=%d parameter!\n" , + vcpu_id_add_bits); + vcpu_id_add_bits =-1 ; + } + +if (vcpu_id_add_bits >=0 ) { + n_bits += vcpu_id_add_bits; + }else { + n_bits++;/* One additional bit for core level. */ +if (topology_max_die_per_package () >1 ) + n_bits++;/* One additional bit for die level. */ + } + +if (!n_bits) + n_bits =1 ; + +return (1U << n_bits) -1 ; +} +EXPORT_SYMBOL_GPL (kvm_max_vcpu_id); +/* * Restoring the host value for MSRs that are only consumed when running in * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
Signature · 1 line
2.26.2
Vitaly Kuznetsov
maintainerRe: [PATCH v2 2/6] x86/kvm: add boot parameter for adding vcpu-id bits
Quoted 159 lines · Juergen Gross
Today the maximum vcpu-id of a kvm guest's vcpu on x86 systems is set via a #define in a header file.
In order to support higher vcpu-ids without generally increasing the memory consumption of guests on the host (some guest structures contain arrays sized by
KVM_MAX_VCPU_ID) add a boot parameter for adding some bits to the vcpu-id. Additional bits are needed as the vcpu-id is constructed via bit-wise concatenation of socket-id, core-id, etc. As those ids maximum values are not always a power of 2, the vcpu-ids are sparse.The additional number of bits needed is basically the number of topology levels with a non-power-of-2 maximum value, excluding the top most level.
The default value of the new parameter will be to take the correct setting from the host's topology.
Calculating the maximum vcpu-id dynamically requires to allocate the arrays using
KVM_MAX_VCPU_IDas the size dynamically.Signed-of-by Juergen Gross <[email protected]>---
V2:
- switch to specifying additional bits (based on comment by Vitaly Kuznetsov)Signed-off-by Juergen Gross <[email protected]>--- .../admin-guide/kernel-parameters.txt | 18 ++++++++++++ arch/x86/include/asm/kvm_host.h | 4 ++- arch/x86/kvm/ioapic.c | 12 +++++++- arch/x86/kvm/ioapic.h | 4 +-- arch/x86/kvm/x86.c | 29 +++++++++++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-)Documentation/admin-guide/kernel-parameters.txt +18
-0@@ -2435,6 +2435,24 @@ feature (tagged TLBs) on capable Intel chips. Default is 1 (enabled) + kvm.vcpu_id_add_bits= + [KVM,X86] The vcpu-ids of guests are sparse, as they + are constructed by bit-wise concatenation of the ids of + the different topology levels (sockets, cores, threads). + + This parameter specifies how many additional bits the + maximum vcpu-id needs compared to the maximum number of + vcpus. + + Normally this value is the number of topology levels + without the threads level and without the highest + level. + + The special value -1 can be used to support guests + with the same topology is the host. + + Default: -1 + l1d_flush= [X86,INTEL] Control mitigation for L1D based snooping vulnerability.arch/x86/include/asm/kvm_host.h +3
-1@@ -39,7 +39,7 @@#define KVM_MAX_VCPUS 288#define KVM_SOFT_MAX_VCPUS 240 -#define KVM_MAX_VCPU_ID 1023 +#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3@@ -1588,6 +1588,8 @@ extern u64 kvm_max_tsc_scaling_ratio;extern u64 kvm_default_tsc_scaling_ratio;/* bus lock detection supported? */ extern bool kvm_has_bus_lock_exit; +/* maximum vcpu-id */ +unsigned int kvm_max_vcpu_id (void );extern u64 kvm_mce_cap_supported;arch/x86/kvm/ioapic.c +11
-1@@ -685,11 +685,21 @@ static const struct kvm_io_device_ops ioapic_mmio_ops = {int kvm_ioapic_init (struct kvm *kvm) {struct kvm_ioapic *ioapic; +size_t sz;int ret; - ioapic =kzalloc (sizeof (struct kvm_ioapic ),GFP_KERNEL_ACCOUNT ); + sz =sizeof (struct kvm_ioapic ) + +sizeof (*ioapic->rtc_status.dest_map.map) * +BITS_TO_LONGS (KVM_MAX_VCPU_ID +1 ) + +sizeof (*ioapic->rtc_status.dest_map.vectors) * + (KVM_MAX_VCPU_ID +1 ); + ioapic =kzalloc (sz,GFP_KERNEL_ACCOUNT );if (!ioapic)return -ENOMEM ; + ioapic->rtc_status.dest_map.map = (void *)(ioapic +1 ); + ioapic->rtc_status.dest_map.vectors = + (void *)(ioapic->rtc_status.dest_map.map + +BITS_TO_LONGS (KVM_MAX_VCPU_ID +1 ));spin_lock_init (&ioapic->lock);INIT_DELAYED_WORK (&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work); kvm->arch.vioapic = ioapic;arch/x86/kvm/ioapic.h +2
-2@@ -39,13 +39,13 @@ struct kvm_vcpu;struct dest_map {/* vcpu bitmap where IRQ has been sent */ - DECLARE_BITMAP(map, KVM_MAX_VCPU_ID + 1); +unsigned long *map;/* * Vector sent to a given vcpu, only valid when * the vcpu's bit in map is set */ -u8 vectors[KVM_MAX_VCPU_ID +1 ]; +u8 *vectors; };arch/x86/kvm/x86.c +10
-0@@ -78,6 +78,7 @@#include <asm/intel_pt.h> #include <asm/emulate_prefix.h> #include <asm/sgx.h> +#include <asm/topology.h> #include <clocksource/hyperv_timer.h> #define CREATE_TRACE_POINTS @@ -184,6 +185,34 @@ module_param(force_emulation_prefix, bool, S_IRUGO);int __read_mostly pi_inject_timer =-1 ;module_param (pi_inject_timer, bint,S_IRUGO |S_IWUSR ); +static int __read_mostly vcpu_id_add_bits =-1 ; +module_param (vcpu_id_add_bits, int,S_IRUGO ); + +unsigned int kvm_max_vcpu_id (void ) +{ +int n_bits =fls (KVM_MAX_VCPUS -1 ); + +if (vcpu_id_add_bits <-1 || vcpu_id_add_bits > (32 - n_bits)) { + pr_err("Invalid value of vcpu_id_add_bits=%d parameter!\n",
arch/x86/kvm/x86.c module_param(force_emulation_prefix, bool, S_IRUGO); · line 206
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ vcpu_id_add_bits); + vcpu_id_add_bits =-1 ; + } + +if (vcpu_id_add_bits >=0 ) { + n_bits += vcpu_id_add_bits; + }else { + n_bits++;/* One additional bit for core level. */ +if (topology_max_die_per_package () >1 ) + n_bits++;/* One additional bit for die level. */
This assumes topology_max_die_per_package() can not be greater than 2, or 1 additional bit may not suffice, right?
arch/x86/kvm/x86.c module_param(force_emulation_prefix, bool, S_IRUGO); · line 210
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+ } + +if (!n_bits) + n_bits =1 ;
Nitpick: AFAIU n_bits can't be zero here as KVM_MAX_VCPUS is still static. The last patch of the series, however, makes it possible when max_vcpus = 1 and vcpu_id_add_bits = 0. With this, I'd suggest to move the check to the last patch.
Quoted 8 lines
arch/x86/kvm/x86.c +5
-0+ +return (1U << n_bits) -1 ; +} +EXPORT_SYMBOL_GPL (kvm_max_vcpu_id); +/* * Restoring the host value for MSRs that are only consumed when running in * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
Signature · 1 line
Vitaly
Re: [PATCH v2 2/6] x86/kvm: add boot parameter for adding vcpu-id bits
Quoted 166 lines · Vitaly Kuznetsov
Quoted 164 lines · Juergen Gross
Today the maximum vcpu-id of a kvm guest's vcpu on x86 systems is set via a #define in a header file.
In order to support higher vcpu-ids without generally increasing the memory consumption of guests on the host (some guest structures contain arrays sized by
KVM_MAX_VCPU_ID) add a boot parameter for adding some bits to the vcpu-id. Additional bits are needed as the vcpu-id is constructed via bit-wise concatenation of socket-id, core-id, etc. As those ids maximum values are not always a power of 2, the vcpu-ids are sparse.The additional number of bits needed is basically the number of topology levels with a non-power-of-2 maximum value, excluding the top most level.
The default value of the new parameter will be to take the correct setting from the host's topology.
Calculating the maximum vcpu-id dynamically requires to allocate the arrays using
KVM_MAX_VCPU_IDas the size dynamically.Signed-of-by Juergen Gross <[email protected]>---
V2:
- switch to specifying additional bits (based on comment by Vitaly Kuznetsov)Signed-off-by Juergen Gross <[email protected]>--- .../admin-guide/kernel-parameters.txt | 18 ++++++++++++ arch/x86/include/asm/kvm_host.h | 4 ++- arch/x86/kvm/ioapic.c | 12 +++++++- arch/x86/kvm/ioapic.h | 4 +-- arch/x86/kvm/x86.c | 29 +++++++++++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-)Documentation/admin-guide/kernel-parameters.txt +18
-0@@ -2435,6 +2435,24 @@ feature (tagged TLBs) on capable Intel chips. Default is 1 (enabled) + kvm.vcpu_id_add_bits= + [KVM,X86] The vcpu-ids of guests are sparse, as they + are constructed by bit-wise concatenation of the ids of + the different topology levels (sockets, cores, threads). + + This parameter specifies how many additional bits the + maximum vcpu-id needs compared to the maximum number of + vcpus. + + Normally this value is the number of topology levels + without the threads level and without the highest + level. + + The special value -1 can be used to support guests + with the same topology is the host. + + Default: -1 + l1d_flush= [X86,INTEL] Control mitigation for L1D based snooping vulnerability.arch/x86/include/asm/kvm_host.h +3
-1@@ -39,7 +39,7 @@#define KVM_MAX_VCPUS 288#define KVM_SOFT_MAX_VCPUS 240 -#define KVM_MAX_VCPU_ID 1023 +#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3@@ -1588,6 +1588,8 @@ extern u64 kvm_max_tsc_scaling_ratio;extern u64 kvm_default_tsc_scaling_ratio;/* bus lock detection supported? */ extern bool kvm_has_bus_lock_exit; +/* maximum vcpu-id */ +unsigned int kvm_max_vcpu_id (void );extern u64 kvm_mce_cap_supported;arch/x86/kvm/ioapic.c +11
-1@@ -685,11 +685,21 @@ static const struct kvm_io_device_ops ioapic_mmio_ops = {int kvm_ioapic_init (struct kvm *kvm) {struct kvm_ioapic *ioapic; +size_t sz;int ret; - ioapic =kzalloc (sizeof (struct kvm_ioapic ),GFP_KERNEL_ACCOUNT ); + sz =sizeof (struct kvm_ioapic ) + +sizeof (*ioapic->rtc_status.dest_map.map) * +BITS_TO_LONGS (KVM_MAX_VCPU_ID +1 ) + +sizeof (*ioapic->rtc_status.dest_map.vectors) * + (KVM_MAX_VCPU_ID +1 ); + ioapic =kzalloc (sz,GFP_KERNEL_ACCOUNT );if (!ioapic)return -ENOMEM ; + ioapic->rtc_status.dest_map.map = (void *)(ioapic +1 ); + ioapic->rtc_status.dest_map.vectors = + (void *)(ioapic->rtc_status.dest_map.map + +BITS_TO_LONGS (KVM_MAX_VCPU_ID +1 ));spin_lock_init (&ioapic->lock);INIT_DELAYED_WORK (&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work); kvm->arch.vioapic = ioapic;arch/x86/kvm/ioapic.h +2
-2@@ -39,13 +39,13 @@ struct kvm_vcpu;struct dest_map {/* vcpu bitmap where IRQ has been sent */ - DECLARE_BITMAP(map, KVM_MAX_VCPU_ID + 1); +unsigned long *map;/* * Vector sent to a given vcpu, only valid when * the vcpu's bit in map is set */ -u8 vectors[KVM_MAX_VCPU_ID +1 ]; +u8 *vectors; };arch/x86/kvm/x86.c +15
-0@@ -78,6 +78,7 @@#include <asm/intel_pt.h> #include <asm/emulate_prefix.h> #include <asm/sgx.h> +#include <asm/topology.h> #include <clocksource/hyperv_timer.h> #define CREATE_TRACE_POINTS @@ -184,6 +185,34 @@ module_param(force_emulation_prefix, bool, S_IRUGO);int __read_mostly pi_inject_timer =-1 ;module_param (pi_inject_timer, bint,S_IRUGO |S_IWUSR ); +static int __read_mostly vcpu_id_add_bits =-1 ; +module_param (vcpu_id_add_bits, int,S_IRUGO ); + +unsigned int kvm_max_vcpu_id (void ) +{ +int n_bits =fls (KVM_MAX_VCPUS -1 ); + +if (vcpu_id_add_bits <-1 || vcpu_id_add_bits > (32 - n_bits)) { +pr_err ("Invalid value of vcpu_id_add_bits=%d parameter!\n" , + vcpu_id_add_bits); + vcpu_id_add_bits =-1 ; + } + +if (vcpu_id_add_bits >=0 ) {
Quoted 8 lines
arch/x86/kvm/x86.c module_param(force_emulation_prefix, bool, S_IRUGO); · line 206
Quoted 5 lines
arch/x86/kvm/x86.c +5
-0+ n_bits += vcpu_id_add_bits; + }else { + n_bits++;/* One additional bit for core level. */ +if (topology_max_die_per_package () >1 ) + n_bits++;/* One additional bit for die level. */ This assumes
topology_max_die_per_package()can not be greater than 2, or 1 additional bit may not suffice, right?
No. Each topology level can at least add one additional bit. This mechanism assumes that each level consumes not more bits as necessary, so with e.g. a core count of 18 per die 5 bits are used, and not more.
Quoted 10 lines
arch/x86/kvm/x86.c module_param(force_emulation_prefix, bool, S_IRUGO); · line 210
Quoted 5 lines
arch/x86/kvm/x86.c +4
-0+ } + +if (!n_bits) + n_bits =1 ;Nitpick: AFAIU
n_bitscan't be zero here asKVM_MAX_VCPUSis still static. The last patch of the series, however, makes it possible whenmax_vcpus= 1 andvcpu_id_add_bits= 0. With this, I'd suggest to move the check to the last patch.
This is true only if no downstream has a patch setting KVM_MAX_VCPUS to
1. I'd rather be safe than sorry here, especially as it would be very easy to miss this dependency.
Juergen
Re: [PATCH v2 2/6] x86/kvm: add boot parameter for adding vcpu-id bits
In reply to Juergen GrossQuoted 11 lines · Juergen Gross
Today the maximum vcpu-id of a kvm guest's vcpu on x86 systems is set via a #define in a header file.
In order to support higher vcpu-ids without generally increasing the memory consumption of guests on the host (some guest structures contain arrays sized by
KVM_MAX_VCPU_ID) add a boot parameter for adding some bits to the vcpu-id. Additional bits are needed as the vcpu-id is constructed via bit-wise concatenation of socket-id, core-id, etc. As those ids maximum values are not always a power of 2, the vcpu-ids are sparse.
Quoted 6 lines
The additional number of bits needed is basically the number of topology levels with a non-power-of-2 maximum value, excluding the top most level.
The default value of the new parameter will be to take the correct setting from the host's topology.
Having the default depend on the host topology makes the host behaviour unpredictable (which might be a problem when migrating VMs from another host with a different topology). Can't we just default to 2?
Quoted 12 lines
Calculating the maximum vcpu-id dynamically requires to allocate the arrays using
KVM_MAX_VCPU_IDas the size dynamically.Signed-of-by Juergen Gross <[email protected]>---
V2:
- switch to specifying additional bits (based on comment by Vitaly Kuznetsov)Signed-off-by Juergen Gross <[email protected]>---
[...]
Quoted 4 lines
#define
KVM_MAX_VCPUS288
#defineKVM_SOFT_MAX_VCPUS240arch/x86/include/asm/kvm_host.h +1
-1-#define KVM_MAX_VCPU_ID 1023 +#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()
[...]
Quoted 12 lines
arch/x86/kvm/x86.c +12
-0+unsigned int kvm_max_vcpu_id (void ) +{ +int n_bits =fls (KVM_MAX_VCPUS -1 ); + +if (vcpu_id_add_bits <-1 || vcpu_id_add_bits > (32 - n_bits)) { +pr_err ("Invalid value of vcpu_id_add_bits=%d parameter!\n" , + vcpu_id_add_bits); + vcpu_id_add_bits =-1 ; + } + +if (vcpu_id_add_bits >=0 ) { + n_bits += vcpu_id_add_bits;
arch/x86/kvm/x86.c module_param(force_emulation_prefix, bool, S_IRUGO); · line 212
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ }else { + n_bits++;/* One additional bit for core level. */ +if (topology_max_die_per_package () >1 ) + n_bits++;/* One additional bit for die level. */ + } + +if (!n_bits) + n_bits =1 ; + +return (1U << n_bits) -1 ;
The largest possible VCPU ID is not KVM_MAX_VCPU_ID, it's (KVM_MAX_VCPU_ID - 1). This is enforced by kvm_vm_ioctl_create_vcpu().
That would mean KVM_MAX_VCPU_ID should be (1 << n_bits) instead of ((1 << n_bits) - 1), wouldn't it?
Quoted 9 lines
arch/x86/kvm/x86.c +3
-0+} +EXPORT_SYMBOL_GPL (kvm_max_vcpu_id); +/* * Restoring the host value for MSRs that are only consumed when running in * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU Signature · 1 line
2.26.2
Signature · 1 line
Eduardo
Re: [PATCH v2 2/6] x86/kvm: add boot parameter for adding vcpu-id bits
Quoted 16 lines · Eduardo Habkost
Quoted 15 lines · Juergen Gross
Today the maximum vcpu-id of a kvm guest's vcpu on x86 systems is set via a #define in a header file.
In order to support higher vcpu-ids without generally increasing the memory consumption of guests on the host (some guest structures contain arrays sized by
KVM_MAX_VCPU_ID) add a boot parameter for adding some bits to the vcpu-id. Additional bits are needed as the vcpu-id is constructed via bit-wise concatenation of socket-id, core-id, etc. As those ids maximum values are not always a power of 2, the vcpu-ids are sparse.The additional number of bits needed is basically the number of topology levels with a non-power-of-2 maximum value, excluding the top most level.
Quoted 7 lines
Quoted 2 lines
The default value of the new parameter will be to take the correct setting from the host's topology.
Having the default depend on the host topology makes the host behaviour unpredictable (which might be a problem when migrating VMs from another host with a different topology). Can't we just default to 2?
Okay, fine with me.
Quoted 46 lines
Quoted 13 lines
Calculating the maximum vcpu-id dynamically requires to allocate the arrays using
KVM_MAX_VCPU_IDas the size dynamically.Signed-of-by Juergen Gross <[email protected]>---
V2:
- switch to specifying additional bits (based on comment by Vitaly Kuznetsov)Signed-off-by Juergen Gross <[email protected]>---
[...]
Quoted 4 lines
#define KVM_MAX_VCPUS 288 #define KVM_SOFT_MAX_VCPUS 240arch/x86/include/asm/kvm_host.h +1
-1-#define KVM_MAX_VCPU_ID 1023 +#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()[...]
Quoted 12 lines
arch/x86/kvm/x86.c +12
-0+unsigned int kvm_max_vcpu_id (void ) +{ +int n_bits =fls (KVM_MAX_VCPUS -1 ); + +if (vcpu_id_add_bits <-1 || vcpu_id_add_bits > (32 - n_bits)) { +pr_err ("Invalid value of vcpu_id_add_bits=%d parameter!\n" , + vcpu_id_add_bits); + vcpu_id_add_bits =-1 ; + } + +if (vcpu_id_add_bits >=0 ) { + n_bits += vcpu_id_add_bits;arch/x86/kvm/x86.c module_param(force_emulation_prefix, bool, S_IRUGO); · line 212
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ }else { + n_bits++;/* One additional bit for core level. */ +if (topology_max_die_per_package () >1 ) + n_bits++;/* One additional bit for die level. */ + } + +if (!n_bits) + n_bits =1 ; + +return (1U << n_bits) -1 ;The largest possible VCPU ID is not
KVM_MAX_VCPU_ID, it's (KVM_MAX_VCPU_ID- 1). This is enforced bykvm_vm_ioctl_create_vcpu().
Quoted 2 lines
That would mean
KVM_MAX_VCPU_IDshould be (1 <<n_bits) instead of ((1 <<n_bits) - 1), wouldn't it?
Oh, indeed. I have been fooled by the IMO bad naming of this macro.
The current value 1023 suggests it is not only me having been fooled.
Shouldn't it be named "KVM_MAX_VCPU_IDS" instead?
Juergen
Paolo Bonzini
maintainerRe: [PATCH v2 2/6] x86/kvm: add boot parameter for adding vcpu-id bits
In reply to Juergen Grossarch/x86/kvm/x86.c module_param(force_emulation_prefix, bool, S_IRUGO); · line 206
Quoted 6 lines · Juergen Gross
arch/x86/kvm/x86.c +6
-0+if (vcpu_id_add_bits >=0 ) { + n_bits += vcpu_id_add_bits; + }else { + n_bits++;/* One additional bit for core level. */ +if (topology_max_die_per_package () >1 ) + n_bits++;/* One additional bit for die level. */
This needs to be unconditional since it is always possible to emulate a multiple-die-per-package topology for a guest, even if the host has just one.
Paolo
Quoted 3 lines
arch/x86/kvm/x86.c +3
-0+ } + +if (!n_bits)
PATCH v2 3/6 x86/kvm: introduce per cpu vcpu masks
In reply to Juergen GrossIn order to support high vcpu numbers per guest don't use on stack vcpu bitmasks. As all those currently used bitmasks are not used in functions subject to recursion it is fairly easy to replace them with percpu bitmasks.
Disable preemption while such a bitmask is being used in order to avoid double usage in case we'd switch cpus.
---
V2:
- use local_lock() instead of preempt_disable() (Paolo Bonzini)
--- arch/x86/include/asm/kvm_host.h | 10 ++++++++++ arch/x86/kvm/hyperv.c | 25 ++++++++++++++++++------- arch/x86/kvm/irq_comm.c | 9 +++++++-- arch/x86/kvm/x86.c | 22 +++++++++++++++++++++- 4 files changed, 56 insertions(+), 10 deletions(-)
arch/x86/include/asm/kvm_host.h +10 -0
@@ -15,6 +15,7 @@#include <linux/cpumask.h> #include <linux/irq_work.h> #include <linux/irq.h> +#include <linux/local_lock.h> #include <linux/kvm.h> #include <linux/kvm_para.h>
@@ -1591,6 +1592,15 @@ extern bool kvm_has_bus_lock_exit;/* maximum vcpu-id */ unsigned int kvm_max_vcpu_id (void ); +/* per cpu vcpu bitmasks, protected by kvm_pcpu_mask_lock */ +DECLARE_PER_CPU (local_lock_t, kvm_pcpu_mask_lock); +extern unsigned long __percpu *kvm_pcpu_vcpu_mask; +#define KVM_VCPU_MASK_SZ \ + (sizeof(*kvm_pcpu_vcpu_mask) * BITS_TO_LONGS(KVM_MAX_VCPUS)) +extern u64 __percpu *kvm_hv_vp_bitmap; +#define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) +#define KVM_HV_VPMAP_SZ (sizeof(u64) * KVM_HV_MAX_SPARSE_VCPU_SET_BITS) +extern u64 kvm_mce_cap_supported;/*
arch/x86/kvm/hyperv.c +18 -7
@@ -41,7 +41,7 @@/* "Hv#1" signature */ #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648 -#define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) +u64 __percpu *kvm_hv_vp_bitmap;static void stimer_mark_pending (struct kvm_vcpu_hv_stimer *stimer,bool vcpu_kick);
@@ -1701,8 +1701,7 @@ static __always_inline unsigned long *sparse_set_to_vcpu_mask(struct kvm_vcpu *vcpu;int i, bank, sbank =0 ; -memset (vp_bitmap,0 , -KVM_HV_MAX_SPARSE_VCPU_SET_BITS *sizeof (*vp_bitmap)); +memset (vp_bitmap,0 ,KVM_HV_VPMAP_SZ );for_each_set_bit (bank, (unsigned long *)&valid_bank_mask,KVM_HV_MAX_SPARSE_VCPU_SET_BITS ) vp_bitmap[bank] = sparse_banks[sbank++];
@@ -1740,8 +1739,8 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, boolstruct kvm_vcpu_hv *hv_vcpu =to_hv_vcpu (vcpu);struct hv_tlb_flush_ex flush_ex;struct hv_tlb_flush flush; -u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS ]; -DECLARE_BITMAP (vcpu_bitmap,KVM_MAX_VCPUS ); +u64 *vp_bitmap; +unsigned long *vcpu_bitmap;unsigned long *vcpu_mask;u64 valid_bank_mask;u64 sparse_banks[64 ];
@@ -1821,6 +1820,10 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, boolcpumask_clear (&hv_vcpu->tlb_flush); +local_lock (&kvm_pcpu_mask_lock); + vcpu_bitmap =this_cpu_ptr (kvm_pcpu_vcpu_mask); + vp_bitmap =this_cpu_ptr (kvm_hv_vp_bitmap); + vcpu_mask = all_cpus ?NULL :sparse_set_to_vcpu_mask (kvm, sparse_banks, valid_bank_mask, vp_bitmap, vcpu_bitmap);
@@ -1832,6 +1835,8 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, boolkvm_make_vcpus_request_mask (kvm,KVM_REQ_TLB_FLUSH_GUEST ,NULL , vcpu_mask, &hv_vcpu->tlb_flush); +local_unlock (&kvm_pcpu_mask_lock); + ret_success:/* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */ return (u64)HV_STATUS_SUCCESS |
@@ -1862,8 +1867,8 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, boolstruct kvm *kvm = vcpu->kvm;struct hv_send_ipi_ex send_ipi_ex;struct hv_send_ipi send_ipi; -u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS ]; -DECLARE_BITMAP (vcpu_bitmap,KVM_MAX_VCPUS ); +u64 *vp_bitmap; +unsigned long *vcpu_bitmap;unsigned long *vcpu_mask;unsigned long valid_bank_mask;u64 sparse_banks[64 ];
@@ -1920,12 +1925,18 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, boolif ((vector <HV_IPI_LOW_VECTOR ) || (vector >HV_IPI_HIGH_VECTOR ))return HV_STATUS_INVALID_HYPERCALL_INPUT ; +local_lock (&kvm_pcpu_mask_lock); + vcpu_bitmap =this_cpu_ptr (kvm_pcpu_vcpu_mask); + vp_bitmap =this_cpu_ptr (kvm_hv_vp_bitmap); + vcpu_mask = all_cpus ?NULL :sparse_set_to_vcpu_mask (kvm, sparse_banks, valid_bank_mask, vp_bitmap, vcpu_bitmap);kvm_send_ipi_to_many (kvm, vector, vcpu_mask); +local_unlock (&kvm_pcpu_mask_lock); + ret_success:return HV_STATUS_SUCCESS ; }
arch/x86/kvm/irq_comm.c +7 -2
@@ -47,7 +47,7 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, {int i, r =-1 ;struct kvm_vcpu *vcpu, *lowest =NULL ; -unsigned long dest_vcpu_bitmap[BITS_TO_LONGS (KVM_MAX_VCPUS )]; +unsigned long *dest_vcpu_bitmap;unsigned int dest_vcpus =0 ;if (kvm_irq_delivery_to_apic_fast (kvm, src, irq, &r, dest_map))
@@ -59,7 +59,10 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, irq->delivery_mode =APIC_DM_FIXED ; } -memset (dest_vcpu_bitmap,0 ,sizeof (dest_vcpu_bitmap)); +local_lock (&kvm_pcpu_mask_lock); + dest_vcpu_bitmap =this_cpu_ptr (kvm_pcpu_vcpu_mask); + +memset (dest_vcpu_bitmap,0 ,KVM_VCPU_MASK_SZ );kvm_for_each_vcpu (i, vcpu, kvm) {if (!kvm_apic_present (vcpu))
@@ -93,6 +96,8 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, lowest =kvm_get_vcpu (kvm, idx); } +local_unlock (&kvm_pcpu_mask_lock); +if (lowest) r =kvm_apic_set_irq (lowest, irq, dest_map);
arch/x86/kvm/x86.c +21 -1
@@ -213,6 +213,10 @@ unsigned int kvm_max_vcpu_id(void) }EXPORT_SYMBOL_GPL (kvm_max_vcpu_id); +DEFINE_PER_CPU (local_lock_t, kvm_pcpu_mask_lock) = +INIT_LOCAL_LOCK (kvm_pcpu_mask_lock); +unsigned long __percpu *kvm_pcpu_vcpu_mask; +/* * Restoring the host value for MSRs that are only consumed when running in * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
@@ -11029,9 +11033,18 @@ int kvm_arch_hardware_setup(void *opaque)if (boot_cpu_has (X86_FEATURE_XSAVES ))rdmsrl (MSR_IA32_XSS , host_xss); + kvm_pcpu_vcpu_mask =__alloc_percpu (KVM_VCPU_MASK_SZ , +sizeof (unsigned long )); + kvm_hv_vp_bitmap =__alloc_percpu (KVM_HV_VPMAP_SZ ,sizeof (u64)); + +if (!kvm_pcpu_vcpu_mask || !kvm_hv_vp_bitmap) { + r = -ENOMEM ; + goto err; + } + r = ops->hardware_setup ();if (r !=0 ) -return r; + goto err;memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));kvm_ops_static_call_update ();
@@ -11059,11 +11072,18 @@ int kvm_arch_hardware_setup(void *opaque)kvm_init_msr_list ();return 0 ; + + err: +free_percpu (kvm_pcpu_vcpu_mask); +free_percpu (kvm_hv_vp_bitmap); +return r; }void kvm_arch_hardware_unsetup (void ) {static_call (kvm_x86_hardware_unsetup)(); +free_percpu (kvm_pcpu_vcpu_mask); +free_percpu (kvm_hv_vp_bitmap); }int kvm_arch_check_processor_compat (void *opaque)
Signature · 1 line
2.26.2
Re: [PATCH v2 3/6] x86/kvm: introduce per cpu vcpu masks
Quoted 9 lines · Juergen Gross
In order to support high vcpu numbers per guest don't use on stack vcpu bitmasks. As all those currently used bitmasks are not used in functions subject to recursion it is fairly easy to replace them with percpu bitmasks.
Disable preemption while such a bitmask is being used in order to avoid double usage in case we'd switch cpus.
Signed-off-by Juergen Gross <[email protected]>
Note that there's another patch that will introduce another KVM_MAX_VCPUS bitmap variable on the stack:
https://lore.kernel.org/lkml/[email protected]/
Considering that the patch is a bug fix, should this series be rebased on top of that?
Signature · 1 line
Eduardo
Re: [PATCH v2 3/6] x86/kvm: introduce per cpu vcpu masks
Quoted 9 lines · Eduardo Habkost
Quoted 8 lines · Juergen Gross
In order to support high vcpu numbers per guest don't use on stack vcpu bitmasks. As all those currently used bitmasks are not used in functions subject to recursion it is fairly easy to replace them with percpu bitmasks.
Disable preemption while such a bitmask is being used in order to avoid double usage in case we'd switch cpus.
Quoted 9 lines
Quoted 1 line
Signed-off-by Juergen Gross <[email protected]>Note that there's another patch that will introduce another
KVM_MAX_VCPUSbitmap variable on the stack:
https://lore.kernel.org/lkml/[email protected]/Considering that the patch is a bug fix, should this series be rebased on top of that?
Yes, I can do that.
Juergen
Re: [PATCH v2 3/6] x86/kvm: introduce per cpu vcpu masks
In reply to Juergen GrossQuoted 33 lines · Juergen Gross
In order to support high vcpu numbers per guest don't use on stack vcpu bitmasks. As all those currently used bitmasks are not used in functions subject to recursion it is fairly easy to replace them with percpu bitmasks.
Disable preemption while such a bitmask is being used in order to avoid double usage in case we'd switch cpus.
Signed-off-by Juergen Gross <[email protected]>---
V2:
- uselocal_lock()instead ofpreempt_disable()(Paolo Bonzini)--- arch/x86/include/asm/kvm_host.h | 10 ++++++++++ arch/x86/kvm/hyperv.c | 25 ++++++++++++++++++------- arch/x86/kvm/irq_comm.c | 9 +++++++-- arch/x86/kvm/x86.c | 22 +++++++++++++++++++++- 4 files changed, 56 insertions(+), 10 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -15,6 +15,7 @@#include <linux/cpumask.h> #include <linux/irq_work.h> #include <linux/irq.h> +#include <linux/local_lock.h> #include <linux/kvm.h> #include <linux/kvm_para.h> @@ -1591,6 +1592,15 @@ extern bool kvm_has_bus_lock_exit;/* maximum vcpu-id */
arch/x86/include/asm/kvm_host.h extern bool kvm_has_bus_lock_exit; · line 1602
Quoted 10 lines
arch/x86/include/asm/kvm_host.h +8
-0unsigned int kvm_max_vcpu_id (void ); +/* per cpu vcpu bitmasks, protected by kvm_pcpu_mask_lock */ +DECLARE_PER_CPU (local_lock_t, kvm_pcpu_mask_lock); +extern unsigned long __percpu *kvm_pcpu_vcpu_mask; +#define KVM_VCPU_MASK_SZ \ + (sizeof(*kvm_pcpu_vcpu_mask) * BITS_TO_LONGS(KVM_MAX_VCPUS)) +extern u64 __percpu *kvm_hv_vp_bitmap; +#define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) +#define KVM_HV_VPMAP_SZ (sizeof(u64) * KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
I have just realized that the Hyper-V sparse bitmap format can support only up to 4096 CPUs, and the current implementation of sparse_set_to_vcpu_mask() won't even work correctly if KVM_MAX_VCPUS is larger than 4096.
This means vp_bitmap can't and will never be larger than 512 bytes. Isn't a per-CPU variable for vp_bitmap overkill in this case?
Quoted 1 line
[...]
Signature · 1 line
Eduardo
Vitaly Kuznetsov
maintainerRe: [PATCH v2 3/6] x86/kvm: introduce per cpu vcpu masks
Quoted 36 lines · Eduardo Habkost
Quoted 35 lines · Juergen Gross
In order to support high vcpu numbers per guest don't use on stack vcpu bitmasks. As all those currently used bitmasks are not used in functions subject to recursion it is fairly easy to replace them with percpu bitmasks.
Disable preemption while such a bitmask is being used in order to avoid double usage in case we'd switch cpus.
Signed-off-by Juergen Gross <[email protected]>---
V2:
- uselocal_lock()instead ofpreempt_disable()(Paolo Bonzini)--- arch/x86/include/asm/kvm_host.h | 10 ++++++++++ arch/x86/kvm/hyperv.c | 25 ++++++++++++++++++------- arch/x86/kvm/irq_comm.c | 9 +++++++-- arch/x86/kvm/x86.c | 22 +++++++++++++++++++++- 4 files changed, 56 insertions(+), 10 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -15,6 +15,7 @@#include <linux/cpumask.h> #include <linux/irq_work.h> #include <linux/irq.h> +#include <linux/local_lock.h> #include <linux/kvm.h> #include <linux/kvm_para.h> @@ -1591,6 +1592,15 @@ extern bool kvm_has_bus_lock_exit;/* maximum vcpu-id */ unsigned int kvm_max_vcpu_id (void );
Quoted 13 lines
arch/x86/include/asm/kvm_host.h extern bool kvm_has_bus_lock_exit; · line 1602
Quoted 8 lines
arch/x86/include/asm/kvm_host.h +8
-0+/* per cpu vcpu bitmasks, protected by kvm_pcpu_mask_lock */ +DECLARE_PER_CPU (local_lock_t, kvm_pcpu_mask_lock); +extern unsigned long __percpu *kvm_pcpu_vcpu_mask; +#define KVM_VCPU_MASK_SZ \ + (sizeof(*kvm_pcpu_vcpu_mask) * BITS_TO_LONGS(KVM_MAX_VCPUS)) +extern u64 __percpu *kvm_hv_vp_bitmap; +#define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) +#define KVM_HV_VPMAP_SZ (sizeof(u64) * KVM_HV_MAX_SPARSE_VCPU_SET_BITS)I have just realized that the Hyper-V sparse bitmap format can support only up to 4096 CPUs, and the current implementation of
sparse_set_to_vcpu_mask()won't even work correctly ifKVM_MAX_VCPUSis larger than 4096.
Nice catch! Indeed, we can only encode 64 'banks' with 64 vCPUs each. We need to enforce the limit somehow. As a big hammer, I can suggest to fail kvm_hv_vcpu_init() and write to HV_X64_MSR_VP_INDEX for vCPUs above 4095 for now (I seriously doubt there's real need to run such big Windows guests anywhere but who knows).
Quoted 4 lines
This means
vp_bitmapcan't and will never be larger than 512 bytes. Isn't a per-CPU variable forvp_bitmapoverkill in this case?
Yes, it's OK to allocate 512 bytes on stack.
Signature · 1 line
Vitaly
PATCH v2 4/6 kvm: use kvfree() in kvm_arch_free_vm()
In reply to Juergen GrossBy switching from kfree() to kvfree() in kvm_arch_free_vm() Arm64 can use the common variant. This can be accomplished by adding another macro __KVM_HAVE_ARCH_VM_FREE, which will be used only by x86 for now.
Further simplification can be achieved by adding __kvm_arch_free_vm() doing the common part.
---
V2:
- new patch
--- arch/arm64/include/asm/kvm_host.h | 1 - arch/arm64/kvm/arm.c | 8 -------- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 9 ++++++++- 5 files changed, 11 insertions(+), 11 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -1
@@ -767,7 +767,6 @@ int kvm_set_ipa_limit(void);#define __KVM_HAVE_ARCH_VM_ALLOCstruct kvm *kvm_arch_alloc_vm (void ); -void kvm_arch_free_vm (struct kvm *kvm);int kvm_arm_setup_stage2 (struct kvm *kvm,unsigned long type);
arch/arm64/kvm/arm.c +0 -8
@@ -299,14 +299,6 @@ struct kvm *kvm_arch_alloc_vm(void)return vzalloc (sizeof (struct kvm )); } -void kvm_arch_free_vm (struct kvm *kvm) -{ -if (!has_vhe ()) -kfree (kvm); -else -vfree (kvm); -} -int kvm_arch_vcpu_precreate (struct kvm *kvm,unsigned int id) {if (irqchip_in_kernel (kvm) &&vgic_initialized (kvm))
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1521,6 +1521,8 @@ static inline struct kvm *kvm_arch_alloc_vm(void) {return __vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); } + +#define __KVM_HAVE_ARCH_VM_FREEvoid kvm_arch_free_vm (struct kvm *kvm);#define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB
arch/x86/kvm/x86.c +1 -1
@@ -11129,7 +11129,7 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)void kvm_arch_free_vm (struct kvm *kvm) {kfree (to_kvm_hv (kvm)->hv_pa_pg); -vfree (kvm); +__kvm_arch_free_vm (kvm); }
include/linux/kvm_host.h +8 -1
@@ -1044,10 +1044,17 @@ static inline struct kvm *kvm_arch_alloc_vm(void) {return kzalloc (sizeof (struct kvm ),GFP_KERNEL ); } +#endif + +static inline void __kvm_arch_free_vm (struct kvm *kvm) +{ +kvfree (kvm); +} +#ifndef __KVM_HAVE_ARCH_VM_FREEstatic inline void kvm_arch_free_vm (struct kvm *kvm) { -kfree (kvm); +__kvm_arch_free_vm (kvm); }#endif
Signature · 1 line
2.26.2
Re: [PATCH v2 4/6] kvm: use kvfree() in kvm_arch_free_vm()
Quoted 9 lines · Juergen Gross
By switching from
kfree()tokvfree()inkvm_arch_free_vm()Arm64 can use the common variant. This can be accomplished by adding another macro__KVM_HAVE_ARCH_VM_FREE, which will be used only by x86 for now.Further simplification can be achieved by adding
__kvm_arch_free_vm()doing the common part.Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Juergen Gross <[email protected]>
Queued this one already, thanks.
Paolo
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v2 5/6 kvm: allocate vcpu pointer array separately
In reply to Juergen GrossPrepare support of very large vcpu numbers per guest by moving the vcpu pointer array out of struct kvm.
---
V2:
- rebase to new kvm_arch_free_vm() implementation
--- arch/arm64/kvm/arm.c | 21 +++++++++++++++++++-- arch/x86/include/asm/kvm_host.h | 5 +---- arch/x86/kvm/x86.c | 18 ++++++++++++++++++ include/linux/kvm_host.h | 17 +++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)
arch/arm64/kvm/arm.c +19 -2
@@ -293,10 +293,27 @@ long kvm_arch_dev_ioctl(struct file *filp,struct kvm *kvm_arch_alloc_vm (void ) { +struct kvm *kvm; + +if (!has_vhe ()) + kvm =kzalloc (sizeof (struct kvm ),GFP_KERNEL ); +else + kvm =vzalloc (sizeof (struct kvm )); + +if (!kvm) +return NULL ; +if (!has_vhe ()) -return kzalloc (sizeof (struct kvm ),GFP_KERNEL ); + kvm->vcpus =kcalloc (KVM_MAX_VCPUS ,sizeof (void *),GFP_KERNEL ); +else + kvm->vcpus =vzalloc (KVM_MAX_VCPUS *sizeof (void *)); + +if (!kvm->vcpus) { +kvm_arch_free_vm (kvm); + kvm =NULL ; + } -return vzalloc (sizeof (struct kvm )); +return kvm; }int kvm_arch_vcpu_precreate (struct kvm *kvm,unsigned int id)
arch/x86/include/asm/kvm_host.h +1 -4
@@ -1517,10 +1517,7 @@ static inline void kvm_ops_static_call_update(void) }#define __KVM_HAVE_ARCH_VM_ALLOC -static inline struct kvm *kvm_arch_alloc_vm (void ) -{ -return __vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); -} +struct kvm *kvm_arch_alloc_vm (void );#define __KVM_HAVE_ARCH_VM_FREEvoid kvm_arch_free_vm (struct kvm *kvm);
arch/x86/kvm/x86.c +18 -0
@@ -11126,6 +11126,24 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)static_call (kvm_x86_sched_in)(vcpu, cpu); } +struct kvm *kvm_arch_alloc_vm (void ) +{ +struct kvm *kvm; + + kvm =__vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); +if (!kvm) +return NULL ; + + kvm->vcpus =__vmalloc (KVM_MAX_VCPUS *sizeof (void *), +GFP_KERNEL_ACCOUNT | __GFP_ZERO); +if (!kvm->vcpus) { +vfree (kvm); + kvm =NULL ; + } + +return kvm; +} +void kvm_arch_free_vm (struct kvm *kvm) {kfree (to_kvm_hv (kvm)->hv_pa_pg);
include/linux/kvm_host.h +15 -2
@@ -536,7 +536,7 @@ struct kvm {struct mutex slots_arch_lock;struct mm_struct *mm;/* userspace tied to this vm */ struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM ]; -struct kvm_vcpu *vcpus[KVM_MAX_VCPUS ]; +struct kvm_vcpu **vcpus;/* * created_vcpus is protected by kvm->lock, and is incremented
@@ -1042,12 +1042,25 @@ void kvm_arch_pre_destroy_vm(struct kvm *kvm);*/ static inline struct kvm *kvm_arch_alloc_vm (void ) { -return kzalloc (sizeof (struct kvm ),GFP_KERNEL ); +struct kvm *kvm =kzalloc (sizeof (struct kvm ),GFP_KERNEL ); + +if (!kvm) +return NULL ; + + kvm->vcpus =kcalloc (KVM_MAX_VCPUS ,sizeof (void *),GFP_KERNEL ); +if (!kvm->vcpus) { +kfree (kvm); + kvm =NULL ; + } + +return kvm; }#endif static inline void __kvm_arch_free_vm (struct kvm *kvm) { +if (kvm) +kvfree (kvm->vcpus);kvfree (kvm); }
Signature · 1 line
2.26.2
Marc Zyngier
maintainerRe: [PATCH v2 5/6] kvm: allocate vcpu pointer array separately
Quoted 99 lines · Juergen Gross
Prepare support of very large vcpu numbers per guest by moving the vcpu pointer array out of struct kvm.
Signed-off-by Juergen Gross <[email protected]>---
V2:
- rebase to newkvm_arch_free_vm()implementation--- arch/arm64/kvm/arm.c | 21 +++++++++++++++++++-- arch/x86/include/asm/kvm_host.h | 5 +---- arch/x86/kvm/x86.c | 18 ++++++++++++++++++ include/linux/kvm_host.h | 17 +++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)arch/arm64/kvm/arm.c +19
-2@@ -293,10 +293,27 @@ long kvm_arch_dev_ioctl(struct file *filp,struct kvm *kvm_arch_alloc_vm (void ) { +struct kvm *kvm; + +if (!has_vhe ()) + kvm =kzalloc (sizeof (struct kvm ),GFP_KERNEL ); +else + kvm =vzalloc (sizeof (struct kvm )); + +if (!kvm) +return NULL ; +if (!has_vhe ()) -return kzalloc (sizeof (struct kvm ),GFP_KERNEL ); + kvm->vcpus =kcalloc (KVM_MAX_VCPUS ,sizeof (void *),GFP_KERNEL ); +else + kvm->vcpus =vzalloc (KVM_MAX_VCPUS *sizeof (void *)); + +if (!kvm->vcpus) { +kvm_arch_free_vm (kvm); + kvm =NULL ; + } -return vzalloc (sizeof (struct kvm )); +return kvm; }int kvm_arch_vcpu_precreate (struct kvm *kvm,unsigned int id)arch/x86/include/asm/kvm_host.h +1
-4@@ -1517,10 +1517,7 @@ static inline void kvm_ops_static_call_update(void) }#define __KVM_HAVE_ARCH_VM_ALLOC -static inline struct kvm *kvm_arch_alloc_vm (void ) -{ -return __vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); -} +struct kvm *kvm_arch_alloc_vm (void );#define __KVM_HAVE_ARCH_VM_FREEvoid kvm_arch_free_vm (struct kvm *kvm);arch/x86/kvm/x86.c +18
-0@@ -11126,6 +11126,24 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)static_call (kvm_x86_sched_in)(vcpu, cpu); } +struct kvm *kvm_arch_alloc_vm (void ) +{ +struct kvm *kvm; + + kvm =__vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); +if (!kvm) +return NULL ; + + kvm->vcpus =__vmalloc (KVM_MAX_VCPUS *sizeof (void *), +GFP_KERNEL_ACCOUNT | __GFP_ZERO); +if (!kvm->vcpus) { +vfree (kvm); + kvm =NULL ; + } + +return kvm; +} +void kvm_arch_free_vm (struct kvm *kvm) {kfree (to_kvm_hv (kvm)->hv_pa_pg);include/linux/kvm_host.h +0
-0
include/linux/kvm_host.h struct kvm { · line 539
Quoted 5 lines
include/linux/kvm_host.h +1
-1@@ -536,7 +536,7 @@ struct kvm {struct mutex slots_arch_lock;struct mm_struct *mm;/* userspace tied to this vm */ struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM ]; -struct kvm_vcpu *vcpus[KVM_MAX_VCPUS ]; +struct kvm_vcpu **vcpus;
At this stage, I really wonder why we are not using an xarray instead.
I wrote this [1] a while ago, and nothing caught fire. It was also a net deletion of code...
M.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git/log/?h=kvm-arm64/vcpu-xarray
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v2 5/6] kvm: allocate vcpu pointer array separately
Quoted 109 lines · Marc Zyngier
Quoted 99 lines · Juergen Gross
Prepare support of very large vcpu numbers per guest by moving the vcpu pointer array out of struct kvm.
Signed-off-by Juergen Gross <[email protected]>---
V2:
- rebase to newkvm_arch_free_vm()implementation--- arch/arm64/kvm/arm.c | 21 +++++++++++++++++++-- arch/x86/include/asm/kvm_host.h | 5 +---- arch/x86/kvm/x86.c | 18 ++++++++++++++++++ include/linux/kvm_host.h | 17 +++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)arch/arm64/kvm/arm.c +19
-2@@ -293,10 +293,27 @@ long kvm_arch_dev_ioctl(struct file *filp,struct kvm *kvm_arch_alloc_vm (void ) { +struct kvm *kvm; + +if (!has_vhe ()) + kvm =kzalloc (sizeof (struct kvm ),GFP_KERNEL ); +else + kvm =vzalloc (sizeof (struct kvm )); + +if (!kvm) +return NULL ; +if (!has_vhe ()) -return kzalloc (sizeof (struct kvm ),GFP_KERNEL ); + kvm->vcpus =kcalloc (KVM_MAX_VCPUS ,sizeof (void *),GFP_KERNEL ); +else + kvm->vcpus =vzalloc (KVM_MAX_VCPUS *sizeof (void *)); + +if (!kvm->vcpus) { +kvm_arch_free_vm (kvm); + kvm =NULL ; + } -return vzalloc (sizeof (struct kvm )); +return kvm; }int kvm_arch_vcpu_precreate (struct kvm *kvm,unsigned int id)arch/x86/include/asm/kvm_host.h +1
-4@@ -1517,10 +1517,7 @@ static inline void kvm_ops_static_call_update(void) }#define __KVM_HAVE_ARCH_VM_ALLOC -static inline struct kvm *kvm_arch_alloc_vm (void ) -{ -return __vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); -} +struct kvm *kvm_arch_alloc_vm (void );#define __KVM_HAVE_ARCH_VM_FREEvoid kvm_arch_free_vm (struct kvm *kvm);arch/x86/kvm/x86.c +18
-0@@ -11126,6 +11126,24 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)static_call (kvm_x86_sched_in)(vcpu, cpu); } +struct kvm *kvm_arch_alloc_vm (void ) +{ +struct kvm *kvm; + + kvm =__vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); +if (!kvm) +return NULL ; + + kvm->vcpus =__vmalloc (KVM_MAX_VCPUS *sizeof (void *), +GFP_KERNEL_ACCOUNT | __GFP_ZERO); +if (!kvm->vcpus) { +vfree (kvm); + kvm =NULL ; + } + +return kvm; +} +void kvm_arch_free_vm (struct kvm *kvm) {kfree (to_kvm_hv (kvm)->hv_pa_pg);include/linux/kvm_host.h +0
-0include/linux/kvm_host.h struct kvm { · line 539
Quoted 5 lines
include/linux/kvm_host.h +1
-1@@ -536,7 +536,7 @@ struct kvm {struct mutex slots_arch_lock;struct mm_struct *mm;/* userspace tied to this vm */ struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM ]; -struct kvm_vcpu *vcpus[KVM_MAX_VCPUS ]; +struct kvm_vcpu **vcpus;At this stage, I really wonder why we are not using an xarray instead.
Quoted 2 lines
I wrote this [1] a while ago, and nothing caught fire. It was also a net deletion of code...
Indeed, I'd prefer that solution!
Are you fine with me swapping my patch with yours in the series?
Juergen
Marc Zyngier
maintainerRe: [PATCH v2 5/6] kvm: allocate vcpu pointer array separately
Quoted 11 lines · Juergen Gross
Quoted 4 lines · Marc Zyngier
At this stage, I really wonder why we are not using an xarray instead.
I wrote this [1] a while ago, and nothing caught fire. It was also a net deletion of code...
Indeed, I'd prefer that solution!
Are you fine with me swapping my patch with yours in the series?
Of course, feel free to grab the whole series. You'll probably need the initial patches to set the scene for this. On their own, they are a nice cleanup, and I trust you can write a decent commit message for the three patches affecting mips/s390/x86.
I would normally do that myself, but my network connectivity is reduced to almost nothing at the moment.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Sean Christopherson
maintainerRe: [PATCH v2 5/6] kvm: allocate vcpu pointer array separately
Quoted 10 lines · Marc Zyngier
Quoted 8 lines · Juergen Gross
Quoted 4 lines · Marc Zyngier
At this stage, I really wonder why we are not using an xarray instead.
I wrote this [1] a while ago, and nothing caught fire. It was also a net deletion of code...
Quoted 8 lines
Quoted 3 lines
Indeed, I'd prefer that solution!
Are you fine with me swapping my patch with yours in the series?
Of course, feel free to grab the whole series. You'll probably need the initial patches to set the scene for this. On their own, they are a nice cleanup, and I trust you can write a decent commit message for the three patches affecting mips/s390/x86.
It would also be a good idea to convert kvm_for_each_vcpu() to use xa_for_each(), I assume that's more performant than 2x atomic_read() + xa_load(). Unless I'm misreading the code, xa_for_each() is guaranteed to iterate in ascending index order, i.e. preserves the current vcpu0..vcpuN iteration order.
PATCH v2 6/6 x86/kvm: add boot parameter for setting max number of vcpus per guest
In reply to Juergen GrossToday the maximum number of vcpus of a kvm guest is set via a #define in a header file.
In order to support higher vcpu numbers for guests without generally increasing the memory consumption of guests on the host especially on very large systems add a boot parameter for specifying the number of allowed vcpus for guests.
The default will still be the current setting of 288. The value 0 has the special meaning to limit the number of possible vcpus to the number of possible cpus of the host.
--- Documentation/admin-guide/kernel-parameters.txt | 7 +++++++ arch/x86/include/asm/kvm_host.h | 5 ++++- arch/x86/kvm/x86.c | 9 ++++++++- 3 files changed, 19 insertions(+), 2 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +7 -0
@@ -2435,6 +2435,13 @@ feature (tagged TLBs) on capable Intel chips. Default is 1 (enabled) + kvm.max_vcpus= [KVM,X86] Set the maximum allowed numbers of vcpus per + guest. The special value 0 sets the limit to the number + of physical cpus possible on the host (including not + yet hotplugged cpus). Higher values will result in + slightly higher memory consumption per guest. + Default: 288 + kvm.vcpu_id_add_bits= [KVM,X86] The vcpu-ids of guests are sparse, as they are constructed by bit-wise concatenation of the ids of
arch/x86/include/asm/kvm_host.h +4 -1
@@ -38,7 +38,8 @@#define __KVM_HAVE_ARCH_VCPU_DEBUGFS -#define KVM_MAX_VCPUS 288 +#define KVM_DEFAULT_MAX_VCPUS 288 +#define KVM_MAX_VCPUS max_vcpus#define KVM_SOFT_MAX_VCPUS 240#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()/* memory slots that are not exposed to userspace */
@@ -1588,6 +1589,8 @@ extern u64 kvm_max_tsc_scaling_ratio;extern u64 kvm_default_tsc_scaling_ratio;/* bus lock detection supported? */ extern bool kvm_has_bus_lock_exit; +/* maximum number of vcpus per guest */ +extern unsigned int max_vcpus;/* maximum vcpu-id */ unsigned int kvm_max_vcpu_id (void );
arch/x86/kvm/x86.c +8 -1
@@ -188,9 +188,13 @@ module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);static int __read_mostly vcpu_id_add_bits =-1 ;module_param (vcpu_id_add_bits, int,S_IRUGO ); +unsigned int __read_mostly max_vcpus =KVM_DEFAULT_MAX_VCPUS ; +module_param (max_vcpus, uint,S_IRUGO ); +EXPORT_SYMBOL_GPL (max_vcpus); +unsigned int kvm_max_vcpu_id (void ) { -int n_bits =fls (KVM_MAX_VCPUS -1 ); +int n_bits =fls (max_vcpus -1 );if (vcpu_id_add_bits <-1 || vcpu_id_add_bits > (32 - n_bits)) { pr_err("Invalid value of vcpu_id_add_bits=%d parameter!\n",
@@ -11033,6 +11037,9 @@ int kvm_arch_hardware_setup(void *opaque)if (boot_cpu_has (X86_FEATURE_XSAVES ))rdmsrl (MSR_IA32_XSS , host_xss); +if (max_vcpus ==0 ) + max_vcpus =num_possible_cpus (); + kvm_pcpu_vcpu_mask =__alloc_percpu (KVM_VCPU_MASK_SZ ,sizeof (unsigned long )); kvm_hv_vp_bitmap =__alloc_percpu (KVM_HV_VPMAP_SZ ,sizeof (u64));
Signature · 1 line
2.26.2
Re: [PATCH v2 6/6] x86/kvm: add boot parameter for setting max number of vcpus per guest
Quoted 66 lines · Juergen Gross
Today the maximum number of vcpus of a kvm guest is set via a #define in a header file.
In order to support higher vcpu numbers for guests without generally increasing the memory consumption of guests on the host especially on very large systems add a boot parameter for specifying the number of allowed vcpus for guests.
The default will still be the current setting of 288. The value 0 has the special meaning to limit the number of possible vcpus to the number of possible cpus of the host.
Signed-off-by Juergen Gross <[email protected]>--- Documentation/admin-guide/kernel-parameters.txt | 7 +++++++ arch/x86/include/asm/kvm_host.h | 5 ++++- arch/x86/kvm/x86.c | 9 ++++++++- 3 files changed, 19 insertions(+), 2 deletions(-)Documentation/admin-guide/kernel-parameters.txt +7
-0@@ -2435,6 +2435,13 @@ feature (tagged TLBs) on capable Intel chips. Default is 1 (enabled) + kvm.max_vcpus= [KVM,X86] Set the maximum allowed numbers of vcpus per + guest. The special value 0 sets the limit to the number + of physical cpus possible on the host (including not + yet hotplugged cpus). Higher values will result in + slightly higher memory consumption per guest. + Default: 288 + kvm.vcpu_id_add_bits= [KVM,X86] The vcpu-ids of guests are sparse, as they are constructed by bit-wise concatenation of the ids ofarch/x86/include/asm/kvm_host.h +4
-1@@ -38,7 +38,8 @@#define __KVM_HAVE_ARCH_VCPU_DEBUGFS -#define KVM_MAX_VCPUS 288 +#define KVM_DEFAULT_MAX_VCPUS 288 +#define KVM_MAX_VCPUS max_vcpus#define KVM_SOFT_MAX_VCPUS 240#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()/* memory slots that are not exposed to userspace */ @@ -1588,6 +1589,8 @@ extern u64 kvm_max_tsc_scaling_ratio;extern u64 kvm_default_tsc_scaling_ratio;/* bus lock detection supported? */ extern bool kvm_has_bus_lock_exit; +/* maximum number of vcpus per guest */ +extern unsigned int max_vcpus;/* maximum vcpu-id */ unsigned int kvm_max_vcpu_id (void );arch/x86/kvm/x86.c +0
-0@@ -188,9 +188,13 @@ module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);static int __read_mostly vcpu_id_add_bits =-1 ;
arch/x86/kvm/x86.c module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR); · line 197
Quoted 10 lines
arch/x86/kvm/x86.c +5
-1module_param (vcpu_id_add_bits, int,S_IRUGO ); +unsigned int __read_mostly max_vcpus =KVM_DEFAULT_MAX_VCPUS ; +module_param (max_vcpus, uint,S_IRUGO ); +EXPORT_SYMBOL_GPL (max_vcpus); +unsigned int kvm_max_vcpu_id (void ) { -int n_bits =fls (KVM_MAX_VCPUS -1 ); +int n_bits =fls (max_vcpus -1 );
A quesintion here: the parameter "vcpu_id_add_bits" also depends on the "max_vcpus", we can't calculate the "vcpu_id_add_bits" from "max_vcpus" because KVM has no topologically knowledge to determine bits needed for each socket/core/thread level, right?
Quoted 16 lines
arch/x86/kvm/x86.c +3
-1if (vcpu_id_add_bits <-1 || vcpu_id_add_bits > (32 - n_bits)) { pr_err("Invalid value of vcpu_id_add_bits=%d parameter!\n",@@ -11033,6 +11037,9 @@ int kvm_arch_hardware_setup(void *opaque)if (boot_cpu_has (X86_FEATURE_XSAVES ))rdmsrl (MSR_IA32_XSS , host_xss); +if (max_vcpus ==0 ) + max_vcpus =num_possible_cpus (); + kvm_pcpu_vcpu_mask =__alloc_percpu (KVM_VCPU_MASK_SZ ,sizeof (unsigned long )); kvm_hv_vp_bitmap =__alloc_percpu (KVM_HV_VPMAP_SZ ,sizeof (u64)); --2.26.2
Re: [PATCH v2 6/6] x86/kvm: add boot parameter for setting max number of vcpus per guest
Quoted 69 lines · Yao Yuan
Quoted 68 lines · Juergen Gross
Today the maximum number of vcpus of a kvm guest is set via a #define in a header file.
In order to support higher vcpu numbers for guests without generally increasing the memory consumption of guests on the host especially on very large systems add a boot parameter for specifying the number of allowed vcpus for guests.
The default will still be the current setting of 288. The value 0 has the special meaning to limit the number of possible vcpus to the number of possible cpus of the host.
Signed-off-by Juergen Gross <[email protected]>--- Documentation/admin-guide/kernel-parameters.txt | 7 +++++++ arch/x86/include/asm/kvm_host.h | 5 ++++- arch/x86/kvm/x86.c | 9 ++++++++- 3 files changed, 19 insertions(+), 2 deletions(-)Documentation/admin-guide/kernel-parameters.txt +7
-0@@ -2435,6 +2435,13 @@ feature (tagged TLBs) on capable Intel chips. Default is 1 (enabled) + kvm.max_vcpus= [KVM,X86] Set the maximum allowed numbers of vcpus per + guest. The special value 0 sets the limit to the number + of physical cpus possible on the host (including not + yet hotplugged cpus). Higher values will result in + slightly higher memory consumption per guest. + Default: 288 + kvm.vcpu_id_add_bits= [KVM,X86] The vcpu-ids of guests are sparse, as they are constructed by bit-wise concatenation of the ids ofarch/x86/include/asm/kvm_host.h +4
-1@@ -38,7 +38,8 @@#define __KVM_HAVE_ARCH_VCPU_DEBUGFS -#define KVM_MAX_VCPUS 288 +#define KVM_DEFAULT_MAX_VCPUS 288 +#define KVM_MAX_VCPUS max_vcpus#define KVM_SOFT_MAX_VCPUS 240#define KVM_MAX_VCPU_ID kvm_max_vcpu_id()/* memory slots that are not exposed to userspace */ @@ -1588,6 +1589,8 @@ extern u64 kvm_max_tsc_scaling_ratio;extern u64 kvm_default_tsc_scaling_ratio;/* bus lock detection supported? */ extern bool kvm_has_bus_lock_exit; +/* maximum number of vcpus per guest */ +extern unsigned int max_vcpus;/* maximum vcpu-id */ unsigned int kvm_max_vcpu_id (void );arch/x86/kvm/x86.c +0
-0@@ -188,9 +188,13 @@ module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);static int __read_mostly vcpu_id_add_bits =-1 ;module_param (vcpu_id_add_bits, int,S_IRUGO );
Quoted 13 lines
arch/x86/kvm/x86.c module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR); · line 197
Quoted 8 lines
arch/x86/kvm/x86.c +5
-1+unsigned int __read_mostly max_vcpus =KVM_DEFAULT_MAX_VCPUS ; +module_param (max_vcpus, uint,S_IRUGO ); +EXPORT_SYMBOL_GPL (max_vcpus); +unsigned int kvm_max_vcpu_id (void ) { -int n_bits =fls (KVM_MAX_VCPUS -1 ); +int n_bits =fls (max_vcpus -1 );A quesintion here: the parameter "
vcpu_id_add_bits" also depends on the "max_vcpus", we can't calculate the "vcpu_id_add_bits" from "max_vcpus" because KVM has no topologically knowledge to determine bits needed for each socket/core/thread level, right?
Correct.
Juergen