Skip to content

Commit 3bf0fcd

Browse files
vittyvkbonzini
authored andcommitted
KVM: selftests: Speed up set_memory_region_test
After commit 4fc096a ("KVM: Raise the maximum number of user memslots") set_memory_region_test may take too long, reports are that the default timeout value we have (120s) may not be enough even on a physical host. Speed things up a bit by throwing away vm_userspace_mem_region_add() usage from test_add_max_memory_regions(), we don't really need to do the majority of the stuff it does for the sake of this test. On my AMD EPYC 7401P, # time ./set_memory_region_test pre-patch: Testing KVM_RUN with zero added memory regions Allowed number of memory slots: 32764 Adding slots 0..32763, each memory region with 2048K size Testing MOVE of in-use region, 10 loops Testing DELETE of in-use region, 10 loops real 0m44.917s user 0m7.416s sys 0m34.601s post-patch: Testing KVM_RUN with zero added memory regions Allowed number of memory slots: 32764 Adding slots 0..32763, each memory region with 2048K size Testing MOVE of in-use region, 10 loops Testing DELETE of in-use region, 10 loops real 0m20.714s user 0m0.109s sys 0m18.359s Reported-by: kernel test robot <[email protected]> Signed-off-by: Vitaly Kuznetsov <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d478757 commit 3bf0fcd

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

tools/testing/selftests/kvm/set_memory_region_test.c

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ static void test_zero_memory_regions(void)
329329
}
330330
#endif /* __x86_64__ */
331331

332+
static int test_memory_region_add(struct kvm_vm *vm, void *mem, uint32_t slot,
333+
uint32_t size, uint64_t guest_addr)
334+
{
335+
struct kvm_userspace_memory_region region;
336+
int ret;
337+
338+
region.slot = slot;
339+
region.flags = 0;
340+
region.guest_phys_addr = guest_addr;
341+
region.memory_size = size;
342+
region.userspace_addr = (uintptr_t) mem;
343+
ret = ioctl(vm_get_fd(vm), KVM_SET_USER_MEMORY_REGION, &region);
344+
345+
return ret;
346+
}
347+
332348
/*
333349
* Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
334350
* tentative to add further slots should fail.
@@ -339,9 +355,15 @@ static void test_add_max_memory_regions(void)
339355
struct kvm_vm *vm;
340356
uint32_t max_mem_slots;
341357
uint32_t slot;
342-
uint64_t guest_addr = 0x0;
343-
uint64_t mem_reg_npages;
344-
void *mem;
358+
void *mem, *mem_aligned, *mem_extra;
359+
size_t alignment;
360+
361+
#ifdef __s390x__
362+
/* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
363+
alignment = 0x100000;
364+
#else
365+
alignment = 1;
366+
#endif
345367

346368
max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS);
347369
TEST_ASSERT(max_mem_slots > 0,
@@ -350,30 +372,37 @@ static void test_add_max_memory_regions(void)
350372

351373
vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);
352374

353-
mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, MEM_REGION_SIZE);
354-
355375
/* Check it can be added memory slots up to the maximum allowed */
356376
pr_info("Adding slots 0..%i, each memory region with %dK size\n",
357377
(max_mem_slots - 1), MEM_REGION_SIZE >> 10);
378+
379+
mem = mmap(NULL, MEM_REGION_SIZE * max_mem_slots + alignment,
380+
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
381+
TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host");
382+
mem_aligned = (void *)(((size_t) mem + alignment - 1) & ~(alignment - 1));
383+
358384
for (slot = 0; slot < max_mem_slots; slot++) {
359-
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
360-
guest_addr, slot, mem_reg_npages,
361-
0);
362-
guest_addr += MEM_REGION_SIZE;
385+
ret = test_memory_region_add(vm, mem_aligned +
386+
((uint64_t)slot * MEM_REGION_SIZE),
387+
slot, MEM_REGION_SIZE,
388+
(uint64_t)slot * MEM_REGION_SIZE);
389+
TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
390+
" rc: %i errno: %i slot: %i\n",
391+
ret, errno, slot);
363392
}
364393

365394
/* Check it cannot be added memory slots beyond the limit */
366-
mem = mmap(NULL, MEM_REGION_SIZE, PROT_READ | PROT_WRITE,
367-
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
368-
TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host");
395+
mem_extra = mmap(NULL, MEM_REGION_SIZE, PROT_READ | PROT_WRITE,
396+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
397+
TEST_ASSERT(mem_extra != MAP_FAILED, "Failed to mmap() host");
369398

370-
ret = ioctl(vm_get_fd(vm), KVM_SET_USER_MEMORY_REGION,
371-
&(struct kvm_userspace_memory_region) {slot, 0, guest_addr,
372-
MEM_REGION_SIZE, (uint64_t) mem});
399+
ret = test_memory_region_add(vm, mem_extra, max_mem_slots, MEM_REGION_SIZE,
400+
(uint64_t)max_mem_slots * MEM_REGION_SIZE);
373401
TEST_ASSERT(ret == -1 && errno == EINVAL,
374402
"Adding one more memory slot should fail with EINVAL");
375403

376-
munmap(mem, MEM_REGION_SIZE);
404+
munmap(mem, MEM_REGION_SIZE * max_mem_slots + alignment);
405+
munmap(mem_extra, MEM_REGION_SIZE);
377406
kvm_vm_free(vm);
378407
}
379408

0 commit comments

Comments
 (0)