Skip to contentSkip to conversation

KVM: Make the maximum number of user memslots configurable and raise the default

PATCH In review Vitaly Kuznetsov 4 participants 15 messages Jan 27, 2021 – Feb 8, 2021

Subsystems (13)
Documentation x86 KVM PowerPC +9 more subsystems
lore
Share & tools
lore
#0 lore raw

cover KVM: Make the maximum number of user memslots configurable and raise the default

This is a successor of previously sent "[PATCH RFC 0/4] KVM: x86:
Drastically raise KVM_USER_MEM_SLOTS limit".

Changes since RFC:
- Re-wrote everything [Sean]. The maximum number of slots is now a per-VM thing controlled by an ioctl.

Original description:

Current KVM_USER_MEM_SLOTS limit on x86 (509) can be a limiting factor for some configurations. In particular, when QEMU tries to start a Windows guest with Hyper-V SynIC enabled and e.g. 256 vCPUs the limit is hit as SynIC requires two pages per vCPU and the guest is free to pick any GFN for each of them, this fragments memslots as QEMU wants to have a separate memslot for each of these pages (which are supposed to act as 'overlay' pages).

Memory slots are allocated dynamically in KVM when added so the only real limitation is 'id_to_index' array which is 'short'. We don't seem to have any other KVM_MEM_SLOTS_NUM/KVM_USER_MEM_SLOTS-sized statically defined structures.

We could've just raised the limit to e.g. '1021' (we have 3 private memslots on x86) and this should be enough for now as KVM_MAX_VCPUS is '288' but AFAIK there are plans to raise this limit as well. Raise the default value to 32768 - KVM_PRIVATE_MEM_SLOTS and introduce a new ioctl to set the limit per-VM.

Vitaly Kuznetsov (5):
KVM: Make the maximum number of user memslots a per-VM thing
KVM: Raise the maximum number of user memslots
KVM: Make the maximum number of user memslots configurable
selftests: kvm: Test the newly introduced KVM_CAP_MEMSLOTS_LIMIT
selftests: kvm: Raise the default timeout to 120 seconds

 Documentation/virt/kvm/api.rst                | 16 +++++++
 arch/arm64/include/asm/kvm_host.h             |  1 -
 arch/mips/include/asm/kvm_host.h              |  1 -
 arch/powerpc/include/asm/kvm_host.h           |  1 -
 arch/powerpc/kvm/book3s_hv.c                  |  2 +-
 arch/s390/include/asm/kvm_host.h              |  1 -
 arch/s390/kvm/kvm-s390.c                      |  2 +-
 arch/x86/include/asm/kvm_host.h               |  2 -
 include/linux/kvm_host.h                      |  6 +--
 include/uapi/linux/kvm.h                      |  1 +
 .../testing/selftests/kvm/include/kvm_util.h  |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 30 ++++++++++++-
 .../selftests/kvm/set_memory_region_test.c    | 43 ++++++++++++++++---
 tools/testing/selftests/kvm/settings          |  1 +
 virt/kvm/dirty_ring.c                         |  2 +-
 virt/kvm/kvm_main.c                           | 42 +++++++++++++++---
 16 files changed, 128 insertions(+), 24 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/settings
Signature · 1 line
2.29.2
#1 lore raw

PATCH 1/5 KVM: Make the maximum number of user memslots a per-VM thing

Limiting the maximum number of user memslots globally can be undesirable as different VMs may have different needs. Generally, a relatively small number should suffice and a VMM may want to enforce the limitation so a VM won't accidentally eat too much memory. On the other hand, the number of required memslots can depend on the number of assigned vCPUs, e.g. each Hyper-V SynIC may require up to two additional slots per vCPU.

Prepare to limit the maximum number of user memslots per-VM. No real functional change in this patch as the limit is still hard-coded to KVM_USER_MEM_SLOTS.

---
 arch/powerpc/kvm/book3s_hv.c |  2 +-
 arch/s390/kvm/kvm-s390.c     |  2 +-
 include/linux/kvm_host.h     |  1 +
 virt/kvm/dirty_ring.c        |  2 +-
 virt/kvm/kvm_main.c          | 11 ++++++-----
 5 files changed, 10 insertions(+), 8 deletions(-)
arch/powerpc/kvm/book3s_hv.c +1 -1
index 6f612d240392..bea2f34e3662 100644
@@ -4472,7 +4472,7 @@ static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
 	mutex_lock(&kvm->slots_lock);
 
 	r = -EINVAL;
-	if (log->slot >= KVM_USER_MEM_SLOTS)
+	if (log->slot >= kvm->memslots_max)
 		goto out;
 
 	slots = kvm_memslots(kvm);
arch/s390/kvm/kvm-s390.c +1 -1
index dbafd057ca6a..b8c49105f40c 100644
@@ -640,7 +640,7 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
 	mutex_lock(&kvm->slots_lock);
 
 	r = -EINVAL;
-	if (log->slot >= KVM_USER_MEM_SLOTS)
+	if (log->slot >= kvm->memslots_max)
 		goto out;
 
 	r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
include/linux/kvm_host.h +1 -0
index f3b1013fb22c..0033ccffe617 100644
@@ -513,6 +513,7 @@ struct kvm {
 	pid_t userspace_pid;
 	unsigned int max_halt_poll_ns;
 	u32 dirty_ring_size;
+	short int memslots_max;
 };
 
 #define kvm_err(fmt, ...) \
virt/kvm/dirty_ring.c +1 -1
index 9d01299563ee..40d0a749a55d 100644
@@ -52,7 +52,7 @@ static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask)
 	as_id = slot >> 16;
 	id = (u16)slot;
 
-	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
+	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= kvm->memslots_max)
 		return;
 
 	memslot = id_to_memslot(__kvm_memslots(kvm, as_id), id);
virt/kvm/kvm_main.c +6 -5
index 8367d88ce39b..a78e982e7107 100644
@@ -755,6 +755,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
 	INIT_LIST_HEAD(&kvm->devices);
 
 	BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
+	kvm->memslots_max = KVM_USER_MEM_SLOTS;
 
 	if (init_srcu_struct(&kvm->srcu))
 		goto out_err_no_srcu;
@@ -1404,7 +1405,7 @@ EXPORT_SYMBOL_GPL(kvm_set_memory_region);
 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
 					  struct kvm_userspace_memory_region *mem)
 {
-	if ((u16)mem->slot >= KVM_USER_MEM_SLOTS)
+	if ((u16)mem->slot >= kvm->memslots_max)
 		return -EINVAL;
 
 	return kvm_set_memory_region(kvm, mem);
@@ -1435,7 +1436,7 @@ int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log,
 
 	as_id = log->slot >> 16;
 	id = (u16)log->slot;
-	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
+	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= kvm->memslots_max)
 		return -EINVAL;
 
 	slots = __kvm_memslots(kvm, as_id);
@@ -1497,7 +1498,7 @@ static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log)
 
 	as_id = log->slot >> 16;
 	id = (u16)log->slot;
-	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
+	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= kvm->memslots_max)
 		return -EINVAL;
 
 	slots = __kvm_memslots(kvm, as_id);
@@ -1609,7 +1610,7 @@ static int kvm_clear_dirty_log_protect(struct kvm *kvm,
 
 	as_id = log->slot >> 16;
 	id = (u16)log->slot;
-	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
+	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= kvm->memslots_max)
 		return -EINVAL;
 
 	if (log->first_page & 63)
@@ -3682,7 +3683,7 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 		return KVM_ADDRESS_SPACE_NUM;
 #endif
 	case KVM_CAP_NR_MEMSLOTS:
-		return KVM_USER_MEM_SLOTS;
+		return kvm ? kvm->memslots_max : KVM_USER_MEM_SLOTS;
 	case KVM_CAP_DIRTY_LOG_RING:
 #if KVM_DIRTY_LOG_PAGE_OFFSET > 0
 		return KVM_DIRTY_RING_MAX_ENTRIES * sizeof(struct kvm_dirty_gfn);
Signature · 1 line
2.29.2
#2 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 7 lines · Vitaly Kuznetsov

Limiting the maximum number of user memslots globally can be undesirable as different VMs may have different needs. Generally, a relatively small number should suffice and a VMM may want to enforce the limitation so a VM won't accidentally eat too much memory. On the other hand, the number of required memslots can depend on the number of assigned vCPUs, e.g. each Hyper-V SynIC may require up to two additional slots per vCPU.

Quoted 7 lines

Prepare to limit the maximum number of user memslots per-VM. No real functional change in this patch as the limit is still hard-coded to KVM_USER_MEM_SLOTS.

---

Perhaps I didn't understand the idea clearly but I thought it was to protect the kernel from a rogue userspace VMM allocating many memslots and so consuming a lot of memory in kernel?

But then what's the difference between allocating 32k memslots for one VM and allocating 509 slots for 64 VMs?

A guest can't add a memslot on its own, only the host software (like QEMU) can, right?

Thanks,
Maciej

#3 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 16 lines · Maciej S. Szmigiero
Quoted 7 lines · Vitaly Kuznetsov

Limiting the maximum number of user memslots globally can be undesirable as different VMs may have different needs. Generally, a relatively small number should suffice and a VMM may want to enforce the limitation so a VM won't accidentally eat too much memory. On the other hand, the number of required memslots can depend on the number of assigned vCPUs, e.g. each Hyper-V SynIC may require up to two additional slots per vCPU.

Quoted 7 lines

Prepare to limit the maximum number of user memslots per-VM. No real functional change in this patch as the limit is still hard-coded to KVM_USER_MEM_SLOTS.

---

Quoted 7 lines

Perhaps I didn't understand the idea clearly but I thought it was to protect the kernel from a rogue userspace VMM allocating many memslots and so consuming a lot of memory in kernel?

But then what's the difference between allocating 32k memslots for one VM and allocating 509 slots for 64 VMs?

It was Sean's idea :-) Initially, I had the exact same thoughts but now I agree with

"I see it as an easy way to mitigate the damage. E.g. if a containers use case is spinning up hundreds of VMs and something goes awry in the config, it would be the difference between consuming tens of MBs and hundreds of MBs. Cgroup limits should also be in play, but defense in depth and all that. "

https://lore.kernel.org/kvm/[email protected]/

That said it is not really a security feature, VMM still stays in control.

Quoted 3 lines

A guest can't add a memslot on its own, only the host software (like QEMU) can, right?

VMMs (especially big ones like QEMU) are complex and e.g. each driver can cause memory regions (-> memslots in KVM) to change. With this feature it becomes possible to set a limit upfront (based on VM configuration) so it'll be more obvious when it's hit.

Signature · 1 line
Vitaly
#4 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 39 lines · Vitaly Kuznetsov
Quoted 16 lines · Maciej S. Szmigiero
Quoted 7 lines · Vitaly Kuznetsov

Limiting the maximum number of user memslots globally can be undesirable as different VMs may have different needs. Generally, a relatively small number should suffice and a VMM may want to enforce the limitation so a VM won't accidentally eat too much memory. On the other hand, the number of required memslots can depend on the number of assigned vCPUs, e.g. each Hyper-V SynIC may require up to two additional slots per vCPU.

Quoted 7 lines

Prepare to limit the maximum number of user memslots per-VM. No real functional change in this patch as the limit is still hard-coded to KVM_USER_MEM_SLOTS.

---

Quoted 7 lines

Perhaps I didn't understand the idea clearly but I thought it was to protect the kernel from a rogue userspace VMM allocating many memslots and so consuming a lot of memory in kernel?

But then what's the difference between allocating 32k memslots for one VM and allocating 509 slots for 64 VMs?

It was Sean's idea :-) Initially, I had the exact same thoughts but now I agree with

"I see it as an easy way to mitigate the damage. E.g. if a containers use case is spinning up hundreds of VMs and something goes awry in the config, it would be the difference between consuming tens of MBs and hundreds of MBs. Cgroup limits should also be in play, but defense in depth and all that. "

https://urldefense.com/v3/__https://lore.kernel.org/kvm/[email protected]/__;!!GqivPVa7Brio!MEvJAWTpdPwU7jynHog2X5g4AHX7YCbRlNvTC9x4xdmk3aiSMjwT_rMpvZM6g8TkoJfvcw$

That said it is not really a security feature, VMM still stays in control.

Quoted 9 lines
Quoted 3 lines

A guest can't add a memslot on its own, only the host software (like QEMU) can, right?

VMMs (especially big ones like QEMU) are complex and e.g. each driver can cause memory regions (-> memslots in KVM) to change. With this feature it becomes possible to set a limit upfront (based on VM configuration) so it'll be more obvious when it's hit.

I see: it's a kind of a "big switch", so every VMM doesn't have to be modified or audited.
Thanks for the explanation.

Maciej

maintainer
#5 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 10 lines · Maciej S. Szmigiero
Quoted 6 lines

VMMs (especially big ones like QEMU) are complex and e.g. each driver can cause memory regions (-> memslots in KVM) to change. With this feature it becomes possible to set a limit upfront (based on VM configuration) so it'll be more obvious when it's hit.

I see: it's a kind of a "big switch", so every VMM doesn't have to be modified or audited.
Thanks for the explanation.

Not really, it's the opposite: the VMM needs to opt into a smaller number of memslots.

I don't know... I understand it would be defense in depth, however between dynamic allocation of memslots arrays and GFP_KERNEL_ACCOUNT, it seems to be a bit of a solution in search of a problem. For now I applied patches 1-2-5.

Thanks,

Paolo

#6 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Maciej S. Szmigiero
Quoted 6 lines

VMMs (especially big ones like QEMU) are complex and e.g. each driver can cause memory regions (-> memslots in KVM) to change. With this feature it becomes possible to set a limit upfront (based on VM configuration) so it'll be more obvious when it's hit.

I see: it's a kind of a "big switch", so every VMM doesn't have to be modified or audited.
Thanks for the explanation.

Quoted 7 lines

Not really, it's the opposite: the VMM needs to opt into a smaller number of memslots.

I don't know... I understand it would be defense in depth, however between dynamic allocation of memslots arrays and GFP_KERNEL_ACCOUNT, it seems to be a bit of a solution in search of a problem. For now I applied patches 1-2-5.

An alternative with a new module parameter was also suggested, that would make it possible to protect against buggy/malicious VMMs but again, the attack is not any different from just creating many VMs. Module parameter will most like end up being unused 99,9% of the time (if not 100). I don't seem to have a strong opinion.

Signature · 1 line
Vitaly
#7 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Maciej S. Szmigiero
Quoted 6 lines

VMMs (especially big ones like QEMU) are complex and e.g. each driver can cause memory regions (-> memslots in KVM) to change. With this feature it becomes possible to set a limit upfront (based on VM configuration) so it'll be more obvious when it's hit.

Quoted 6 lines
Quoted 3 lines

I see: it's a kind of a "big switch", so every VMM doesn't have to be modified or audited.
Thanks for the explanation.

Not really, it's the opposite: the VMM needs to opt into a smaller number of memslots.

Yep, my thinking is that it would be similar to using seccomp to prevent doing something that should never happen.

Quoted 3 lines

I don't know... I understand it would be defense in depth, however between dynamic allocation of memslots arrays and GFP_KERNEL_ACCOUNT, it seems to be a bit of a solution in search of a problem.

I'm a-ok waiting to add a capability until there's a VMM that actually wants to use it.

Quoted 1 line

For now I applied patches 1-2-5.

Why keep patch 1? Simply raising the limit in patch 2 shouldn't require per-VM tracking. The 'memslots_max' name is also ambiguous. In my head, the new capability would restrict the _number_ of memslots, but as implemented in patches 1+3 it restrists the max _ID_ of a memslot. Limiting the max ID also effectively limits that max number of memslots, but that approach confuses things since the IDs themselves do not affect memory consumption. Limiting the IDs bleeds the old implementation details into the ABI.

maintainer
#8 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 4 lines · Sean Christopherson
Quoted 2 lines

For now I applied patches 1-2-5.

Why keep patch 1? Simply raising the limit in patch 2 shouldn't require per-VM tracking.

Well, right you are.

Paolo

#9 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 8 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson
Quoted 2 lines

For now I applied patches 1-2-5.

Why keep patch 1? Simply raising the limit in patch 2 shouldn't require per-VM tracking.

Well, right you are.

... so are we going to raise it after all? I don't see anything in kvm/queue so just wanted to double-check.

Thanks!

Signature · 1 line
Vitaly
maintainer
#10 lore raw

Re: [PATCH 1/5] KVM: Make the maximum number of user memslots a per-VM thing

Quoted 5 lines · Vitaly Kuznetsov
Quoted 3 lines · Paolo Bonzini
Quoted 2 lines · Sean Christopherson
Quoted 2 lines

For now I applied patches 1-2-5.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Why keep patch 1? Simply raising the limit in patch 2 shouldn't require per-VM tracking.

Well, right you are.

... so are we going to raise it after all? I don't see anything in kvm/queue so just wanted to double-check.

Added back, thanks.

Paolo

#11 lore raw

PATCH 2/5 KVM: Raise the maximum number of user memslots

In reply to Vitaly Kuznetsov

Current KVM_USER_MEM_SLOTS limits are arch specific (512 on Power, 509 on x86, 32 on s390, 16 on MIPS) but they don't really need to be. Memory slots are allocated dynamically in KVM when added so the only real limitation is 'id_to_index' array which is 'short'. We don't have any other KVM_MEM_SLOTS_NUM/KVM_USER_MEM_SLOTS-sized statically defined structures.

Low KVM_USER_MEM_SLOTS can be a limiting factor for some configurations. In particular, when QEMU tries to start a Windows guest with Hyper-V SynIC enabled and e.g. 256 vCPUs the limit is hit as SynIC requires two pages per vCPU and the guest is free to pick any GFN for each of them, this fragments memslots as QEMU wants to have a separate memslot for each of these pages (which are supposed to act as 'overlay' pages).

---
 arch/arm64/include/asm/kvm_host.h   | 1 -
 arch/mips/include/asm/kvm_host.h    | 1 -
 arch/powerpc/include/asm/kvm_host.h | 1 -
 arch/s390/include/asm/kvm_host.h    | 1 -
 arch/x86/include/asm/kvm_host.h     | 2 --
 include/linux/kvm_host.h            | 5 ++---
 virt/kvm/kvm_main.c                 | 1 +
 7 files changed, 3 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -1
index 8fcfab0c2567..1b8a3d825276 100644
@@ -30,7 +30,6 @@
 
 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
 
-#define KVM_USER_MEM_SLOTS 512
 #define KVM_HALT_POLL_NS_DEFAULT 500000
 
 #include <kvm/arm_vgic.h>
arch/mips/include/asm/kvm_host.h +0 -1
index 24f3d0f9996b..3a5612e7304c 100644
@@ -83,7 +83,6 @@
 
 
 #define KVM_MAX_VCPUS		16
-#define KVM_USER_MEM_SLOTS	16
 /* memory slots that does not exposed to userspace */
 #define KVM_PRIVATE_MEM_SLOTS	0
 
arch/powerpc/include/asm/kvm_host.h +0 -1
index d67a470e95a3..2b9b6855ec86 100644
@@ -28,7 +28,6 @@
 
 #define KVM_MAX_VCPUS		NR_CPUS
 #define KVM_MAX_VCORES		NR_CPUS
-#define KVM_USER_MEM_SLOTS	512
 
 #include <asm/cputhreads.h>
 
arch/s390/include/asm/kvm_host.h +0 -1
index 74f9a036bab2..6bcfc5614bbc 100644
@@ -28,7 +28,6 @@
 #define KVM_S390_BSCA_CPU_SLOTS 64
 #define KVM_S390_ESCA_CPU_SLOTS 248
 #define KVM_MAX_VCPUS 255
-#define KVM_USER_MEM_SLOTS 32
 
 /*
  * These seem to be used for allocating ->chip in the routing table, which we
arch/x86/include/asm/kvm_host.h +0 -2
index 3d6616f6f6ef..a6547dc9191a 100644
@@ -40,10 +40,8 @@
 #define KVM_MAX_VCPUS 288
 #define KVM_SOFT_MAX_VCPUS 240
 #define KVM_MAX_VCPU_ID 1023
-#define KVM_USER_MEM_SLOTS 509
 /* memory slots that are not exposed to userspace */
 #define KVM_PRIVATE_MEM_SLOTS 3
-#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
 
 #define KVM_HALT_POLL_NS_DEFAULT 200000
 
include/linux/kvm_host.h +2 -3
index 0033ccffe617..754020140f37 100644
@@ -425,9 +425,8 @@ struct kvm_irq_routing_table {
 #define KVM_PRIVATE_MEM_SLOTS 0
 #endif
 
-#ifndef KVM_MEM_SLOTS_NUM
-#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
-#endif
+#define KVM_MEM_SLOTS_NUM SHRT_MAX
+#define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_PRIVATE_MEM_SLOTS)
 
 #ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE
 static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu)
virt/kvm/kvm_main.c +1 -0
index a78e982e7107..5adb1b694304 100644
@@ -755,6 +755,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
 	INIT_LIST_HEAD(&kvm->devices);
 
 	BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
+	BUILD_BUG_ON(KVM_PRIVATE_MEM_SLOTS >= KVM_MEM_SLOTS_NUM);
 	kvm->memslots_max = KVM_USER_MEM_SLOTS;
 
 	if (init_srcu_struct(&kvm->srcu))
Signature · 1 line
2.29.2
#12 lore raw

PATCH 3/5 KVM: Make the maximum number of user memslots configurable

In reply to Vitaly Kuznetsov

The maximum number of user memslots is now a per-VM setting but there is no way to change it. Intoduce KVM_CAP_MEMSLOTS_LIMIT per-VM capability to set the limit.

When the limit is set, it becomes impossible to manage memslots whose id is greater or equal so make sure there are no such slots.

---
 Documentation/virt/kvm/api.rst | 16 ++++++++++++++++
 include/uapi/linux/kvm.h       |  1 +
 virt/kvm/kvm_main.c            | 30 ++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+)
Documentation/virt/kvm/api.rst +16 -0
index 99ceb978c8b0..551236fc1261 100644
@@ -6038,6 +6038,22 @@ KVM_EXIT_X86_RDMSR and KVM_EXIT_X86_WRMSR exit notifications which user space
 can then handle to implement model specific MSR handling and/or user notifications
 to inform a user that an MSR was not handled.
 
+7.22 KVM_CAP_MEMSLOTS_LIMIT
+----------------------
+
+:Architectures: all
+:Target: VM
+:Parameters: args[0] is the maximum number of memory slots
+:Returns: 0 on success; E2BIG when set above global KVM_CAP_NR_MEMSLOTS; EINVAL
+          when there is an existing slot with id >= limit
+
+This capability overrides the default maximum number of memory slots, available
+per target VM. The limit can be changed at any time, however, when lowered, no
+memory slots with id ('slot' in 'struct kvm_userspace_memory_region') greater
+than the requested value should exist or EINVAL is returned. The maximum allowed
+value can be queried by checking system KVM_CAP_NR_MEMSLOTS capability. Per-VM
+KVM_CAP_NR_MEMSLOTS capability represents the currently set limit.
+
 8. Other capabilities.
 ======================
 
include/uapi/linux/kvm.h +1 -0
index 374c67875cdb..f68b0cde801a 100644
@@ -1058,6 +1058,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190
 #define KVM_CAP_SYS_HYPERV_CPUID 191
 #define KVM_CAP_DIRTY_LOG_RING 192
+#define KVM_CAP_MEMSLOTS_LIMIT 193
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
virt/kvm/kvm_main.c +30 -0
index 5adb1b694304..da2cbfe9c9ee 100644
@@ -1412,6 +1412,31 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
 	return kvm_set_memory_region(kvm, mem);
 }
 
+static int kvm_set_memslots_max(struct kvm *kvm, u64 max)
+{
+	struct kvm_memory_slot *memslot;
+	int r = 0, as_id;
+
+	if (max > KVM_USER_MEM_SLOTS)
+		return -E2BIG;
+
+	mutex_lock(&kvm->slots_lock);
+	for (as_id = 0; as_id < KVM_ADDRESS_SPACE_NUM; as_id++) {
+		kvm_for_each_memslot(memslot, __kvm_memslots(kvm, as_id)) {
+			if (memslot->id >= max) {
+				r = -EINVAL;
+				break;
+			}
+		}
+	}
+	if (!r)
+		kvm->memslots_max = max;
+
+	mutex_unlock(&kvm->slots_lock);
+
+	return r;
+}
+
 #ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
 /**
  * kvm_get_dirty_log - get a snapshot of dirty pages
@@ -3664,6 +3689,7 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_CHECK_EXTENSION_VM:
 	case KVM_CAP_ENABLE_CAP_VM:
 	case KVM_CAP_HALT_POLL:
+	case KVM_CAP_MEMSLOTS_LIMIT:
 		return 1;
 #ifdef CONFIG_KVM_MMIO
 	case KVM_CAP_COALESCED_MMIO:
@@ -3789,6 +3815,10 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
 	}
 	case KVM_CAP_DIRTY_LOG_RING:
 		return kvm_vm_ioctl_enable_dirty_log_ring(kvm, cap->args[0]);
+	case KVM_CAP_MEMSLOTS_LIMIT:
+		if (cap->flags)
+			return -EINVAL;
+		return kvm_set_memslots_max(kvm, cap->args[0]);
 	default:
 		return kvm_vm_ioctl_enable_cap(kvm, cap);
 	}
Signature · 1 line
2.29.2
#13 lore raw

PATCH 4/5 selftests: kvm: Test the newly introduced KVM_CAP_MEMSLOTS_LIMIT

In reply to Vitaly Kuznetsov

The number of user memslots can now be limited with KVM_CAP_MEMSLOTS_LIMIT and, when limited, per-VM KVM_CHECK_EXTENSION(KVM_CAP_NR_MEMSLOTS) should return the updated value.

---
 .../testing/selftests/kvm/include/kvm_util.h  |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 30 ++++++++++++-
 .../selftests/kvm/set_memory_region_test.c    | 43 ++++++++++++++++---
 3 files changed, 67 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
index 5cbb861525ed..eb759a54dfc6 100644
@@ -86,6 +86,7 @@ enum vm_mem_backing_src_type {
 };
 
 int kvm_check_cap(long cap);
+int vm_check_cap(struct kvm_vm *vm, long cap);
 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);
 int vcpu_enable_cap(struct kvm_vm *vm, uint32_t vcpu_id,
 		    struct kvm_enable_cap *cap);
tools/testing/selftests/kvm/lib/kvm_util.c +29 -1
index fa5a90e6c6f0..115947b77808 100644
@@ -31,7 +31,7 @@ static void *align(void *x, size_t size)
 }
 
 /*
- * Capability
+ * Check a global capability
  *
  * Input Args:
  *   cap - Capability
@@ -64,6 +64,34 @@ int kvm_check_cap(long cap)
 	return ret;
 }
 
+/*
+ * Check a per-VM capability
+ *
+ * Input Args:
+ *   vm - Virtual Machine
+ *   cap - Capability
+ *
+ * Output Args: None
+ *
+ * Return:
+ *   On success, the Value corresponding to the capability (KVM_CAP_*)
+ *   specified by the value of cap.  On failure a TEST_ASSERT failure
+ *   is produced.
+ *
+ * Looks up and returns the value corresponding to the capability
+ * (KVM_CAP_*) given by cap.
+ */
+int vm_check_cap(struct kvm_vm *vm, long cap)
+{
+	int ret;
+
+	ret = ioctl(vm->fd, KVM_CHECK_EXTENSION, cap);
+	TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n"
+		"  rc: %i errno: %i", ret, errno);
+
+	return ret;
+}
+
 /* VM Enable Capability
  *
  * Input Args:
tools/testing/selftests/kvm/set_memory_region_test.c +37 -6

68 lines not on this page · raw message

Signature · 1 line
2.29.2
#14 lore raw

PATCH 5/5 selftests: kvm: Raise the default timeout to 120 seconds

In reply to Vitaly Kuznetsov

With the updated maximum number of user memslots (32) set_memory_region_test sometimes takes longer than the default 45 seconds to finish. Raise the value to an arbitrary 120 seconds.

---
 tools/testing/selftests/kvm/settings | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 tools/testing/selftests/kvm/settings
tools/testing/selftests/kvm/settings new file +1 -0
new file mode 100644
index 000000000000..6091b45d226b
@@ -0,0 +1 @@
+timeout=120
Signature · 1 line
2.29.2