[PATCH v4 09/10] KVM: selftests: Stop memslot creation in KVM internal memslot region

From: Ben Gardon
Date: Thu Jan 23 2020 - 13:05:08 EST


KVM creates internal memslots covering the region between 3G and 4G in
the guest physical address space, when the first vCPU is created.
Mapping this region before creation of the first vCPU causes vCPU
creation to fail. Prohibit tests from creating such a memslot and fail
with a helpful warning when they try to.

Signed-off-by: Ben Gardon <bgardon@xxxxxxxxxx>
---
tools/testing/selftests/kvm/lib/kvm_util.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 41cf45416060f..5b971c04f1643 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -113,6 +113,8 @@ const char * const vm_guest_mode_string[] = {
_Static_assert(sizeof(vm_guest_mode_string)/sizeof(char *) == NUM_VM_MODES,
"Missing new mode strings?");

+#define KVM_INTERNAL_MEMSLOTS_START_PADDR (3UL << 30)
+#define KVM_INTERNAL_MEMSLOTS_END_PADDR (4UL << 30)
/*
* VM Create
*
@@ -593,6 +595,20 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
" vm->max_gfn: 0x%lx vm->page_size: 0x%x",
guest_paddr, npages, vm->max_gfn, vm->page_size);

+ /*
+ * Check that this region does not overlap with KVM internal memslots
+ * which are created when the first vCPU is created.
+ */
+ TEST_ASSERT(guest_paddr >= KVM_INTERNAL_MEMSLOTS_END_PADDR ||
+ guest_paddr + npages < KVM_INTERNAL_MEMSLOTS_START_PADDR,
+ "Memslot overlapps with region mapped by internal KVM\n"
+ "memslots:\n"
+ " Requested paddr range: [0x%lx, 0x%lx)\n"
+ " KVM internal memslot range: [0x%lx, 0x%lx)\n",
+ guest_paddr, guest_paddr + npages,
+ KVM_INTERNAL_MEMSLOTS_START_PADDR,
+ KVM_INTERNAL_MEMSLOTS_END_PADDR);
+
/*
* Confirm a mem region with an overlapping address doesn't
* already exist.
--
2.25.0.341.g760bfbb309-goog