Skip to contentSkip to conversation

KVM: selftests: Ensure gmem file sizes are multiple of host page size

PATCH Applied Sean Christopherson 2 participants 2 messages May 12

Subsystems (2)
lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

PATCH 1/1 KVM: selftests: Ensure gmem file sizes are multiple of host page size

When creating a guest_memfd file and associated memslot to validate shared guest memory, size the file+memslot to the maximum of the host or guest page size. Attempting to allocate a single guest page will fail if the host page size is greater than the guest page size, as KVM requires that the size of memslots and guest_memfd files are a multiple of the host page size.

For simplicity, verify the entire file can be shared between guest and host, e.g. instead of trying to validate "partial" mappings.

Fixes 42188667be38 ("KVM: selftests: Add guest_memfd testcase to fault-in on !mmap()'d memory")
---
 tools/testing/selftests/kvm/guest_memfd_test.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/guest_memfd_test.c +7 -2
index d6528c6f5e03..253e748c1d4a 100644
@@ -510,7 +510,12 @@ static void test_guest_memfd_guest(void)
 		    "Default VM type should support INIT_SHARED, supported flags = 0x%x",
 		    vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS));
 
-	size = vm->page_size;
+	/*
+	 * Use the max of the host or guest page size for all operations, as
+	 * KVM requires guest_memfd files and memslots to be sized to multiples
+	 * of the host page size.
+	 */
+	size = max_t(size_t, vm->page_size, page_size);
 	fd = vm_create_guest_memfd(vm, size, GUEST_MEMFD_FLAG_MMAP |
 					     GUEST_MEMFD_FLAG_INIT_SHARED);
 	vm_set_user_memory_region2(vm, slot, KVM_MEM_GUEST_MEMFD, gpa, size, NULL, fd, 0);
@@ -519,7 +524,7 @@ static void test_guest_memfd_guest(void)
 	memset(mem, 0xaa, size);
 	kvm_munmap(mem, size);
 
-	virt_pg_map(vm, gpa, gpa);
+	virt_map(vm, gpa, gpa, size / vm->page_size);
 	vcpu_args_set(vcpu, 2, gpa, size);
 	vcpu_run(vcpu);
 

base-commit: 6d35786de28116ecf78797a62b84e6bf3c45aa5a

Signature · 1 line
2.54.0.563.g4f69b47b94-goog
maintainer Applied
#1 lore raw
Quoted 7 lines · Sean Christopherson

When creating a guest_memfd file and associated memslot to validate shared guest memory, size the file+memslot to the maximum of the host or guest page size. Attempting to allocate a single guest page will fail if the host page size is greater than the guest page size, as KVM requires that the size of memslots and guest_memfd files are a multiple of the host page size.

Quoted 7 lines

For simplicity, verify the entire file can be shared between guest and host, e.g. instead of trying to validate "partial" mappings.

Fixes 42188667be38 ("KVM: selftests: Add guest_memfd testcase to fault-in on !mmap()'d memory")

Queued, thanks.

Paolo

Quoted 33 lines
---
  tools/testing/selftests/kvm/guest_memfd_test.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/guest_memfd_test.c +7 -2
index d6528c6f5e03..253e748c1d4a 100644
@@ -510,7 +510,12 @@ static void test_guest_memfd_guest(void)
  		    "Default VM type should support INIT_SHARED, supported flags = 0x%x",
  		    vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS));
 
-	size = vm->page_size;
+	/*
+	 * Use the max of the host or guest page size for all operations, as
+	 * KVM requires guest_memfd files and memslots to be sized to multiples
+	 * of the host page size.
+	 */
+	size = max_t(size_t, vm->page_size, page_size);
  	fd = vm_create_guest_memfd(vm, size, GUEST_MEMFD_FLAG_MMAP |
  					     GUEST_MEMFD_FLAG_INIT_SHARED);
  	vm_set_user_memory_region2(vm, slot, KVM_MEM_GUEST_MEMFD, gpa, size, NULL, fd, 0);
@@ -519,7 +524,7 @@ static void test_guest_memfd_guest(void)
  	memset(mem, 0xaa, size);
  	kvm_munmap(mem, size);
 
-	virt_pg_map(vm, gpa, gpa);
+	virt_map(vm, gpa, gpa, size / vm->page_size);
  	vcpu_args_set(vcpu, 2, gpa, size);
  	vcpu_run(vcpu);
 

base-commit: 6d35786de28116ecf78797a62b84e6bf3c45aa5a