Skip to content

Commit 309505d

Browse files
Zenghui Yubonzini
authored andcommitted
KVM: selftests: Fix mapping length truncation in m{,un}map()
max_mem_slots is now declared as uint32_t. The result of (0x200000 * 32767) is unexpectedly truncated to be 0xffe00000, whilst we actually need to allocate about, 63GB. Cast max_mem_slots to size_t in both mmap() and munmap() to fix the length truncation. We'll otherwise see the failure on arm64 thanks to the access_ok() checking in __kvm_set_memory_region(), as the unmapped VA happen to go beyond the task's allowed address space. # ./set_memory_region_test Allowed number of memory slots: 32767 Adding slots 0..32766, each memory region with 2048K size ==== Test Assertion Failure ==== set_memory_region_test.c:391: ret == 0 pid=94861 tid=94861 errno=22 - Invalid argument 1 0x00000000004015a7: test_add_max_memory_regions at set_memory_region_test.c:389 2 (inlined by) main at set_memory_region_test.c:426 3 0x0000ffffb8e67bdf: ?? ??:0 4 0x00000000004016db: _start at :? KVM_SET_USER_MEMORY_REGION IOCTL failed, rc: -1 errno: 22 slot: 2615 Fixes: 3bf0fcd ("KVM: selftests: Speed up set_memory_region_test") Signed-off-by: Zenghui Yu <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 13311e7 commit 309505d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

tools/testing/selftests/kvm/set_memory_region_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static void test_add_max_memory_regions(void)
376376
pr_info("Adding slots 0..%i, each memory region with %dK size\n",
377377
(max_mem_slots - 1), MEM_REGION_SIZE >> 10);
378378

379-
mem = mmap(NULL, MEM_REGION_SIZE * max_mem_slots + alignment,
379+
mem = mmap(NULL, (size_t)max_mem_slots * MEM_REGION_SIZE + alignment,
380380
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
381381
TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host");
382382
mem_aligned = (void *)(((size_t) mem + alignment - 1) & ~(alignment - 1));
@@ -401,7 +401,7 @@ static void test_add_max_memory_regions(void)
401401
TEST_ASSERT(ret == -1 && errno == EINVAL,
402402
"Adding one more memory slot should fail with EINVAL");
403403

404-
munmap(mem, MEM_REGION_SIZE * max_mem_slots + alignment);
404+
munmap(mem, (size_t)max_mem_slots * MEM_REGION_SIZE + alignment);
405405
munmap(mem_extra, MEM_REGION_SIZE);
406406
kvm_vm_free(vm);
407407
}

0 commit comments

Comments
 (0)