Skip to content

Commit a69170c

Browse files
Gavin ShanMarc Zyngier
authored andcommitted
KVM: selftests: memslot_perf_test: Report optimal memory slots
The memory area in each slot should be aligned to host page size. Otherwise, the test will fail. For example, the following command fails with the following messages with 64KB-page-size-host and 4KB-pae-size-guest. It's not user friendly to abort the test. Lets do something to report the optimal memory slots, instead of failing the test. # ./memslot_perf_test -v -s 1000 Number of memory slots: 999 Testing map performance with 1 runs, 5 seconds each Adding slots 1..999, each slot with 8 pages + 216 extra pages last ==== Test Assertion Failure ==== lib/kvm_util.c:824: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=19872 tid=19872 errno=0 - Success 1 0x00000000004065b3: vm_userspace_mem_region_add at kvm_util.c:822 2 0x0000000000401d6b: prepare_vm at memslot_perf_test.c:273 3 (inlined by) test_execute at memslot_perf_test.c:756 4 (inlined by) test_loop at memslot_perf_test.c:994 5 (inlined by) main at memslot_perf_test.c:1073 6 0x0000ffff7ebb4383: ?? ??:0 7 0x00000000004021ff: _start at :? Number of guest pages is not compatible with the host. Try npages=16 Report the optimal memory slots instead of failing the test when the memory area in each slot isn't aligned to host page size. With this applied, the optimal memory slots is reported. # ./memslot_perf_test -v -s 1000 Number of memory slots: 999 Testing map performance with 1 runs, 5 seconds each Memslot count too high for this test, decrease the cap (max is 514) Signed-off-by: Gavin Shan <[email protected]> Reviewed-by: Maciej S. Szmigiero <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 88a64e6 commit a69170c

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

tools/testing/selftests/kvm/memslot_perf_test.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,52 @@ static struct vm_data *alloc_vm(void)
239239
return data;
240240
}
241241

242+
static bool check_slot_pages(uint32_t host_page_size, uint32_t guest_page_size,
243+
uint64_t pages_per_slot, uint64_t rempages)
244+
{
245+
if (!pages_per_slot)
246+
return false;
247+
248+
if ((pages_per_slot * guest_page_size) % host_page_size)
249+
return false;
250+
251+
if ((rempages * guest_page_size) % host_page_size)
252+
return false;
253+
254+
return true;
255+
}
256+
257+
258+
static uint64_t get_max_slots(struct vm_data *data, uint32_t host_page_size)
259+
{
260+
uint32_t guest_page_size = data->vm->page_size;
261+
uint64_t mempages, pages_per_slot, rempages;
262+
uint64_t slots;
263+
264+
mempages = data->npages;
265+
slots = data->nslots;
266+
while (--slots > 1) {
267+
pages_per_slot = mempages / slots;
268+
rempages = mempages % pages_per_slot;
269+
if (check_slot_pages(host_page_size, guest_page_size,
270+
pages_per_slot, rempages))
271+
return slots + 1; /* slot 0 is reserved */
272+
}
273+
274+
return 0;
275+
}
276+
242277
static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots,
243278
void *guest_code, uint64_t mem_size,
244279
struct timespec *slot_runtime)
245280
{
246281
uint64_t mempages, rempages;
247282
uint64_t guest_addr;
248-
uint32_t slot, guest_page_size;
283+
uint32_t slot, host_page_size, guest_page_size;
249284
struct timespec tstart;
250285
struct sync_area *sync;
251286

287+
host_page_size = getpagesize();
252288
guest_page_size = vm_guest_mode_params[VM_MODE_DEFAULT].page_size;
253289
mempages = mem_size / guest_page_size;
254290

@@ -260,12 +296,13 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots,
260296
TEST_ASSERT(data->npages > 1, "Can't test without any memory");
261297
data->nslots = nslots;
262298
data->pages_per_slot = data->npages / data->nslots;
263-
if (!data->pages_per_slot) {
264-
*maxslots = data->npages + 1;
299+
rempages = data->npages % data->nslots;
300+
if (!check_slot_pages(host_page_size, guest_page_size,
301+
data->pages_per_slot, rempages)) {
302+
*maxslots = get_max_slots(data, host_page_size);
265303
return false;
266304
}
267305

268-
rempages = data->npages % data->nslots;
269306
data->hva_slots = malloc(sizeof(*data->hva_slots) * data->nslots);
270307
TEST_ASSERT(data->hva_slots, "malloc() fail");
271308

0 commit comments

Comments
 (0)