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

KVM: selftests: Fix 32-bit truncation of vm_get_max_gfn()

vm_get_max_gfn() casts vm->max_gfn from a uint64_t to an unsigned int,
which causes the upper 32-bits of the max_gfn to get truncated.

Nobody noticed until now likely because vm_get_max_gfn() is only used
as a mechanism to create a memslot in an unused region of the guest
physical address space (the top), and the top of the 32-bit physical
address space was always good enough.

This fix reveals a bug in memslot_modification_stress_test which was
trying to create a dummy memslot past the end of guest physical memory.
Fix that by moving the dummy memslot lower.

Fixes: 52200d0d944e ("KVM: selftests: Remove duplicate guest mode handling")
Reviewed-by: Venkatesh Srinivas <venkateshs@chromium.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Message-Id: <20210521173828.1180619-1-dmatlack@google.com>
Reviewed-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

David Matlack and committed by
Paolo Bonzini
ef4c9f4f cad347fa

+16 -10
+1 -1
tools/testing/selftests/kvm/include/kvm_util.h
··· 302 302 303 303 unsigned int vm_get_page_size(struct kvm_vm *vm); 304 304 unsigned int vm_get_page_shift(struct kvm_vm *vm); 305 - unsigned int vm_get_max_gfn(struct kvm_vm *vm); 305 + uint64_t vm_get_max_gfn(struct kvm_vm *vm); 306 306 int vm_get_fd(struct kvm_vm *vm); 307 307 308 308 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size);
+1 -1
tools/testing/selftests/kvm/lib/kvm_util.c
··· 2117 2117 return vm->page_shift; 2118 2118 } 2119 2119 2120 - unsigned int vm_get_max_gfn(struct kvm_vm *vm) 2120 + uint64_t vm_get_max_gfn(struct kvm_vm *vm) 2121 2121 { 2122 2122 return vm->max_gfn; 2123 2123 }
+3 -1
tools/testing/selftests/kvm/lib/perf_test_util.c
··· 2 2 /* 3 3 * Copyright (C) 2020, Google LLC. 4 4 */ 5 + #include <inttypes.h> 5 6 6 7 #include "kvm_util.h" 7 8 #include "perf_test_util.h" ··· 81 80 */ 82 81 TEST_ASSERT(guest_num_pages < vm_get_max_gfn(vm), 83 82 "Requested more guest memory than address space allows.\n" 84 - " guest pages: %lx max gfn: %x vcpus: %d wss: %lx]\n", 83 + " guest pages: %" PRIx64 " max gfn: %" PRIx64 84 + " vcpus: %d wss: %" PRIx64 "]\n", 85 85 guest_num_pages, vm_get_max_gfn(vm), vcpus, 86 86 vcpu_memory_bytes); 87 87
+11 -7
tools/testing/selftests/kvm/memslot_modification_stress_test.c
··· 71 71 }; 72 72 73 73 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay, 74 - uint64_t nr_modifications, uint64_t gpa) 74 + uint64_t nr_modifications) 75 75 { 76 + const uint64_t pages = 1; 77 + uint64_t gpa; 76 78 int i; 79 + 80 + /* 81 + * Add the dummy memslot just below the perf_test_util memslot, which is 82 + * at the top of the guest physical address space. 83 + */ 84 + gpa = guest_test_phys_mem - pages * vm_get_page_size(vm); 77 85 78 86 for (i = 0; i < nr_modifications; i++) { 79 87 usleep(delay); 80 88 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa, 81 - DUMMY_MEMSLOT_INDEX, 1, 0); 89 + DUMMY_MEMSLOT_INDEX, pages, 0); 82 90 83 91 vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX); 84 92 } ··· 128 120 pr_info("Started all vCPUs\n"); 129 121 130 122 add_remove_memslot(vm, p->memslot_modification_delay, 131 - p->nr_memslot_modifications, 132 - guest_test_phys_mem + 133 - (guest_percpu_mem_size * nr_vcpus) + 134 - perf_test_args.host_page_size + 135 - perf_test_args.guest_page_size); 123 + p->nr_memslot_modifications); 136 124 137 125 run_vcpus = false; 138 126