1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * A memslot-related performance benchmark. 4 * 5 * Copyright (C) 2021 Oracle and/or its affiliates. 6 * 7 * Basic guest setup / host vCPU thread code lifted from set_memory_region_test. 8 */ 9 #include <pthread.h> 10 #include <sched.h> 11 #include <semaphore.h> 12 #include <stdatomic.h> 13 #include <stdbool.h> 14 #include <stdint.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <time.h> 19 #include <unistd.h> 20 21 #include <linux/compiler.h> 22 #include <linux/sizes.h> 23 24 #include <test_util.h> 25 #include <kvm_syscalls.h> 26 #include <kvm_util.h> 27 #include <processor.h> 28 #include <ucall_common.h> 29 30 #define MEM_EXTRA_SIZE SZ_64K 31 32 #define MEM_SIZE (SZ_512M + MEM_EXTRA_SIZE) 33 #define MEM_GPA SZ_512M 34 #define MEM_AUX_GPA MEM_GPA 35 #define MEM_SYNC_GPA MEM_AUX_GPA 36 #define MEM_TEST_GPA (MEM_AUX_GPA + MEM_EXTRA_SIZE) 37 #define MEM_TEST_SIZE (MEM_SIZE - MEM_EXTRA_SIZE) 38 39 /* 40 * 32 MiB is max size that gets well over 100 iterations on 509 slots. 41 * Considering that each slot needs to have at least one page up to 42 * 8194 slots in use can then be tested (although with slightly 43 * limited resolution). 44 */ 45 #define MEM_SIZE_MAP (SZ_32M + MEM_EXTRA_SIZE) 46 #define MEM_TEST_MAP_SIZE (MEM_SIZE_MAP - MEM_EXTRA_SIZE) 47 48 /* 49 * 128 MiB is min size that fills 32k slots with at least one page in each 50 * while at the same time gets 100+ iterations in such test 51 * 52 * 2 MiB chunk size like a typical huge page 53 */ 54 #define MEM_TEST_UNMAP_SIZE SZ_128M 55 #define MEM_TEST_UNMAP_CHUNK_SIZE SZ_2M 56 57 /* 58 * For the move active test the middle of the test area is placed on 59 * a memslot boundary: half lies in the memslot being moved, half in 60 * other memslot(s). 61 * 62 * We have different number of memory slots, excluding the reserved 63 * memory slot 0, on various architectures and configurations. The 64 * memory size in this test is calculated by picking the maximal 65 * last memory slot's memory size, with alignment to the largest 66 * supported page size (64KB). In this way, the selected memory 67 * size for this test is compatible with test_memslot_move_prepare(). 68 * 69 * architecture slots memory-per-slot memory-on-last-slot 70 * -------------------------------------------------------------- 71 * x86-4KB 32763 16KB 160KB 72 * arm64-4KB 32766 16KB 112KB 73 * arm64-16KB 32766 16KB 112KB 74 * arm64-64KB 8192 64KB 128KB 75 */ 76 #define MEM_TEST_MOVE_SIZE (3 * SZ_64K) 77 #define MEM_TEST_MOVE_GPA_DEST (MEM_GPA + MEM_SIZE) 78 static_assert(MEM_TEST_MOVE_SIZE <= MEM_TEST_SIZE, 79 "invalid move test region size"); 80 81 #define MEM_TEST_VAL_1 0x1122334455667788 82 #define MEM_TEST_VAL_2 0x99AABBCCDDEEFF00 83 84 struct vm_data { 85 struct kvm_vm *vm; 86 struct kvm_vcpu *vcpu; 87 pthread_t vcpu_thread; 88 u32 nslots; 89 u64 npages; 90 u64 pages_per_slot; 91 void **hva_slots; 92 bool mmio_ok; 93 u64 mmio_gpa_min; 94 u64 mmio_gpa_max; 95 }; 96 97 struct sync_area { 98 u32 guest_page_size; 99 atomic_bool start_flag; 100 atomic_bool exit_flag; 101 atomic_bool sync_flag; 102 void *move_area_ptr; 103 }; 104 105 /* 106 * Technically, we need also for the atomic bool to be address-free, which 107 * is recommended, but not strictly required, by C11 for lockless 108 * implementations. 109 * However, in practice both GCC and Clang fulfill this requirement on 110 * all KVM-supported platforms. 111 */ 112 static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "atomic bool is not lockless"); 113 114 static int wait_timeout = 10; 115 static sem_t vcpu_ready; 116 117 static bool map_unmap_verify; 118 #ifdef __x86_64__ 119 static bool disable_slot_zap_quirk; 120 #endif 121 122 static bool verbose; 123 #define pr_info_v(...) \ 124 do { \ 125 if (verbose) \ 126 pr_info(__VA_ARGS__); \ 127 } while (0) 128 129 static void check_mmio_access(struct vm_data *data, struct kvm_run *run) 130 { 131 TEST_ASSERT(data->mmio_ok, "Unexpected mmio exit"); 132 TEST_ASSERT(run->mmio.is_write, "Unexpected mmio read"); 133 TEST_ASSERT(run->mmio.len == 8, 134 "Unexpected exit mmio size = %u", run->mmio.len); 135 TEST_ASSERT(run->mmio.phys_addr >= data->mmio_gpa_min && 136 run->mmio.phys_addr <= data->mmio_gpa_max, 137 "Unexpected exit mmio address = 0x%llx", 138 run->mmio.phys_addr); 139 } 140 141 static void *vcpu_worker(void *__data) 142 { 143 struct vm_data *data = __data; 144 struct kvm_vcpu *vcpu = data->vcpu; 145 struct kvm_run *run = vcpu->run; 146 struct ucall uc; 147 148 while (1) { 149 vcpu_run(vcpu); 150 151 switch (get_ucall(vcpu, &uc)) { 152 case UCALL_SYNC: 153 TEST_ASSERT(uc.args[1] == 0, 154 "Unexpected sync ucall, got %lx", 155 (unsigned long)uc.args[1]); 156 sem_post(&vcpu_ready); 157 continue; 158 case UCALL_NONE: 159 if (run->exit_reason == KVM_EXIT_MMIO) 160 check_mmio_access(data, run); 161 else 162 goto done; 163 break; 164 case UCALL_ABORT: 165 REPORT_GUEST_ASSERT(uc); 166 break; 167 case UCALL_DONE: 168 goto done; 169 default: 170 TEST_FAIL("Unknown ucall %lu", uc.cmd); 171 } 172 } 173 174 done: 175 return NULL; 176 } 177 178 static void wait_for_vcpu(void) 179 { 180 struct timespec ts; 181 182 TEST_ASSERT(!clock_gettime(CLOCK_REALTIME, &ts), 183 "clock_gettime() failed: %d", errno); 184 185 ts.tv_sec += 2; 186 TEST_ASSERT(!sem_timedwait(&vcpu_ready, &ts), 187 "sem_timedwait() failed: %d", errno); 188 } 189 190 static void *vm_gpa2hva(struct vm_data *data, gpa_t gpa, u64 *rempages) 191 { 192 gpa_t gpage, pgoffs; 193 u32 slot, slotoffs; 194 void *base; 195 u32 guest_page_size = data->vm->page_size; 196 197 TEST_ASSERT(gpa >= MEM_GPA, "Too low gpa to translate"); 198 TEST_ASSERT(gpa < MEM_GPA + data->npages * guest_page_size, 199 "Too high gpa to translate"); 200 gpa -= MEM_GPA; 201 202 gpage = gpa / guest_page_size; 203 pgoffs = gpa % guest_page_size; 204 slot = min(gpage / data->pages_per_slot, (u64)data->nslots - 1); 205 slotoffs = gpage - (slot * data->pages_per_slot); 206 207 if (rempages) { 208 u64 slotpages; 209 210 if (slot == data->nslots - 1) 211 slotpages = data->npages - slot * data->pages_per_slot; 212 else 213 slotpages = data->pages_per_slot; 214 215 TEST_ASSERT(!pgoffs, 216 "Asking for remaining pages in slot but gpa not page aligned"); 217 *rempages = slotpages - slotoffs; 218 } 219 220 base = data->hva_slots[slot]; 221 return (u8 *)base + slotoffs * guest_page_size + pgoffs; 222 } 223 224 static u64 vm_slot2gpa(struct vm_data *data, u32 slot) 225 { 226 u32 guest_page_size = data->vm->page_size; 227 228 TEST_ASSERT(slot < data->nslots, "Too high slot number"); 229 230 return MEM_GPA + slot * data->pages_per_slot * guest_page_size; 231 } 232 233 static struct vm_data *alloc_vm(void) 234 { 235 struct vm_data *data; 236 237 data = malloc(sizeof(*data)); 238 TEST_ASSERT(data, "malloc(vmdata) failed"); 239 240 data->vm = NULL; 241 data->vcpu = NULL; 242 data->hva_slots = NULL; 243 244 return data; 245 } 246 247 static bool check_slot_pages(u32 host_page_size, u32 guest_page_size, 248 u64 pages_per_slot, u64 rempages) 249 { 250 if (!pages_per_slot) 251 return false; 252 253 if ((pages_per_slot * guest_page_size) % host_page_size) 254 return false; 255 256 if ((rempages * guest_page_size) % host_page_size) 257 return false; 258 259 return true; 260 } 261 262 263 static u64 get_max_slots(struct vm_data *data, u32 host_page_size) 264 { 265 u32 guest_page_size = data->vm->page_size; 266 u64 mempages, pages_per_slot, rempages; 267 u64 slots; 268 269 mempages = data->npages; 270 slots = data->nslots; 271 while (--slots > 1) { 272 pages_per_slot = mempages / slots; 273 if (!pages_per_slot) 274 continue; 275 276 rempages = mempages % pages_per_slot; 277 if (check_slot_pages(host_page_size, guest_page_size, 278 pages_per_slot, rempages)) 279 return slots + 1; /* slot 0 is reserved */ 280 } 281 282 return 0; 283 } 284 285 static bool prepare_vm(struct vm_data *data, int nslots, u64 *maxslots, 286 void *guest_code, u64 mem_size, 287 struct timespec *slot_runtime) 288 { 289 u64 mempages, rempages; 290 u64 guest_addr; 291 u32 slot, host_page_size, guest_page_size; 292 struct timespec tstart; 293 struct sync_area *sync; 294 295 host_page_size = getpagesize(); 296 guest_page_size = vm_guest_mode_params[VM_MODE_DEFAULT].page_size; 297 mempages = mem_size / guest_page_size; 298 299 data->vm = __vm_create_with_one_vcpu(&data->vcpu, mempages, guest_code); 300 TEST_ASSERT(data->vm->page_size == guest_page_size, "Invalid VM page size"); 301 302 data->npages = mempages; 303 TEST_ASSERT(data->npages > 1, "Can't test without any memory"); 304 data->nslots = nslots; 305 data->pages_per_slot = data->npages / data->nslots; 306 rempages = data->npages % data->nslots; 307 if (!check_slot_pages(host_page_size, guest_page_size, 308 data->pages_per_slot, rempages)) { 309 *maxslots = get_max_slots(data, host_page_size); 310 return false; 311 } 312 313 data->hva_slots = malloc(sizeof(*data->hva_slots) * data->nslots); 314 TEST_ASSERT(data->hva_slots, "malloc() fail"); 315 316 pr_info_v("Adding slots 1..%i, each slot with %"PRIu64" pages + %"PRIu64" extra pages last\n", 317 data->nslots, data->pages_per_slot, rempages); 318 319 clock_gettime(CLOCK_MONOTONIC, &tstart); 320 for (slot = 1, guest_addr = MEM_GPA; slot <= data->nslots; slot++) { 321 u64 npages; 322 323 npages = data->pages_per_slot; 324 if (slot == data->nslots) 325 npages += rempages; 326 327 vm_userspace_mem_region_add(data->vm, VM_MEM_SRC_ANONYMOUS, 328 guest_addr, slot, npages, 329 0); 330 guest_addr += npages * guest_page_size; 331 } 332 *slot_runtime = timespec_elapsed(tstart); 333 334 for (slot = 1, guest_addr = MEM_GPA; slot <= data->nslots; slot++) { 335 u64 npages; 336 gpa_t gpa; 337 338 npages = data->pages_per_slot; 339 if (slot == data->nslots) 340 npages += rempages; 341 342 gpa = ____vm_phy_pages_alloc(data->vm, npages, guest_addr, slot, 343 false, false); 344 TEST_ASSERT(gpa == guest_addr, 345 "vm_phy_pages_alloc() failed"); 346 347 data->hva_slots[slot - 1] = addr_gpa2hva(data->vm, guest_addr); 348 memset(data->hva_slots[slot - 1], 0, npages * guest_page_size); 349 350 guest_addr += npages * guest_page_size; 351 } 352 353 virt_map(data->vm, MEM_GPA, MEM_GPA, data->npages); 354 355 sync = (typeof(sync))vm_gpa2hva(data, MEM_SYNC_GPA, NULL); 356 sync->guest_page_size = data->vm->page_size; 357 atomic_init(&sync->start_flag, false); 358 atomic_init(&sync->exit_flag, false); 359 atomic_init(&sync->sync_flag, false); 360 361 data->mmio_ok = false; 362 363 return true; 364 } 365 366 static void launch_vm(struct vm_data *data) 367 { 368 pr_info_v("Launching the test VM\n"); 369 370 kvm_pthread_create(&data->vcpu_thread, NULL, vcpu_worker, data); 371 372 /* Ensure the guest thread is spun up. */ 373 wait_for_vcpu(); 374 } 375 376 static void free_vm(struct vm_data *data) 377 { 378 kvm_vm_free(data->vm); 379 free(data->hva_slots); 380 free(data); 381 } 382 383 static void wait_guest_exit(struct vm_data *data) 384 { 385 kvm_pthread_join(data->vcpu_thread, NULL); 386 } 387 388 static void let_guest_run(struct sync_area *sync) 389 { 390 atomic_store_explicit(&sync->start_flag, true, memory_order_release); 391 } 392 393 static void guest_spin_until_start(void) 394 { 395 struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA; 396 397 while (!atomic_load_explicit(&sync->start_flag, memory_order_acquire)) 398 ; 399 } 400 401 static void make_guest_exit(struct sync_area *sync) 402 { 403 atomic_store_explicit(&sync->exit_flag, true, memory_order_release); 404 } 405 406 static bool _guest_should_exit(void) 407 { 408 struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA; 409 410 return atomic_load_explicit(&sync->exit_flag, memory_order_acquire); 411 } 412 413 #define guest_should_exit() unlikely(_guest_should_exit()) 414 415 /* 416 * noinline so we can easily see how much time the host spends waiting 417 * for the guest. 418 * For the same reason use alarm() instead of polling clock_gettime() 419 * to implement a wait timeout. 420 */ 421 static noinline void host_perform_sync(struct sync_area *sync) 422 { 423 alarm(wait_timeout); 424 425 atomic_store_explicit(&sync->sync_flag, true, memory_order_release); 426 while (atomic_load_explicit(&sync->sync_flag, memory_order_acquire)) 427 ; 428 429 alarm(0); 430 } 431 432 static bool guest_perform_sync(void) 433 { 434 struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA; 435 bool expected; 436 437 do { 438 if (guest_should_exit()) 439 return false; 440 441 expected = true; 442 } while (!atomic_compare_exchange_weak_explicit(&sync->sync_flag, 443 &expected, false, 444 memory_order_acq_rel, 445 memory_order_relaxed)); 446 447 return true; 448 } 449 450 static void guest_code_test_memslot_move(void) 451 { 452 struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA; 453 u32 page_size = (typeof(page_size))READ_ONCE(sync->guest_page_size); 454 uintptr_t base = (typeof(base))READ_ONCE(sync->move_area_ptr); 455 456 GUEST_SYNC(0); 457 458 guest_spin_until_start(); 459 460 while (!guest_should_exit()) { 461 uintptr_t ptr; 462 463 for (ptr = base; ptr < base + MEM_TEST_MOVE_SIZE; 464 ptr += page_size) 465 *(u64 *)ptr = MEM_TEST_VAL_1; 466 467 /* 468 * No host sync here since the MMIO exits are so expensive 469 * that the host would spend most of its time waiting for 470 * the guest and so instead of measuring memslot move 471 * performance we would measure the performance and 472 * likelihood of MMIO exits 473 */ 474 } 475 476 GUEST_DONE(); 477 } 478 479 static void guest_code_test_memslot_map(void) 480 { 481 struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA; 482 u32 page_size = (typeof(page_size))READ_ONCE(sync->guest_page_size); 483 484 GUEST_SYNC(0); 485 486 guest_spin_until_start(); 487 488 while (1) { 489 uintptr_t ptr; 490 491 for (ptr = MEM_TEST_GPA; 492 ptr < MEM_TEST_GPA + MEM_TEST_MAP_SIZE / 2; 493 ptr += page_size) 494 *(u64 *)ptr = MEM_TEST_VAL_1; 495 496 if (!guest_perform_sync()) 497 break; 498 499 for (ptr = MEM_TEST_GPA + MEM_TEST_MAP_SIZE / 2; 500 ptr < MEM_TEST_GPA + MEM_TEST_MAP_SIZE; 501 ptr += page_size) 502 *(u64 *)ptr = MEM_TEST_VAL_2; 503 504 if (!guest_perform_sync()) 505 break; 506 } 507 508 GUEST_DONE(); 509 } 510 511 static void guest_code_test_memslot_unmap(void) 512 { 513 struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA; 514 515 GUEST_SYNC(0); 516 517 guest_spin_until_start(); 518 519 while (1) { 520 uintptr_t ptr = MEM_TEST_GPA; 521 522 /* 523 * We can afford to access (map) just a small number of pages 524 * per host sync as otherwise the host will spend 525 * a significant amount of its time waiting for the guest 526 * (instead of doing unmap operations), so this will 527 * effectively turn this test into a map performance test. 528 * 529 * Just access a single page to be on the safe side. 530 */ 531 *(u64 *)ptr = MEM_TEST_VAL_1; 532 533 if (!guest_perform_sync()) 534 break; 535 536 ptr += MEM_TEST_UNMAP_SIZE / 2; 537 *(u64 *)ptr = MEM_TEST_VAL_2; 538 539 if (!guest_perform_sync()) 540 break; 541 } 542 543 GUEST_DONE(); 544 } 545 546 static void guest_code_test_memslot_rw(void) 547 { 548 struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA; 549 u32 page_size = (typeof(page_size))READ_ONCE(sync->guest_page_size); 550 551 GUEST_SYNC(0); 552 553 guest_spin_until_start(); 554 555 while (1) { 556 uintptr_t ptr; 557 558 for (ptr = MEM_TEST_GPA; 559 ptr < MEM_TEST_GPA + MEM_TEST_SIZE; ptr += page_size) 560 *(u64 *)ptr = MEM_TEST_VAL_1; 561 562 if (!guest_perform_sync()) 563 break; 564 565 for (ptr = MEM_TEST_GPA + page_size / 2; 566 ptr < MEM_TEST_GPA + MEM_TEST_SIZE; ptr += page_size) { 567 u64 val = *(u64 *)ptr; 568 569 GUEST_ASSERT_EQ(val, MEM_TEST_VAL_2); 570 *(u64 *)ptr = 0; 571 } 572 573 if (!guest_perform_sync()) 574 break; 575 } 576 577 GUEST_DONE(); 578 } 579 580 static bool test_memslot_move_prepare(struct vm_data *data, 581 struct sync_area *sync, 582 u64 *maxslots, bool isactive) 583 { 584 u32 guest_page_size = data->vm->page_size; 585 u64 movesrcgpa, movetestgpa; 586 587 #ifdef __x86_64__ 588 if (disable_slot_zap_quirk) 589 vm_enable_cap(data->vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL); 590 #endif 591 592 movesrcgpa = vm_slot2gpa(data, data->nslots - 1); 593 594 if (isactive) { 595 u64 lastpages; 596 597 vm_gpa2hva(data, movesrcgpa, &lastpages); 598 if (lastpages * guest_page_size < MEM_TEST_MOVE_SIZE / 2) { 599 *maxslots = 0; 600 return false; 601 } 602 } 603 604 movetestgpa = movesrcgpa - (MEM_TEST_MOVE_SIZE / (isactive ? 2 : 1)); 605 sync->move_area_ptr = (void *)movetestgpa; 606 607 if (isactive) { 608 data->mmio_ok = true; 609 data->mmio_gpa_min = movesrcgpa; 610 data->mmio_gpa_max = movesrcgpa + MEM_TEST_MOVE_SIZE / 2 - 1; 611 } 612 613 return true; 614 } 615 616 static bool test_memslot_move_prepare_active(struct vm_data *data, 617 struct sync_area *sync, 618 u64 *maxslots) 619 { 620 return test_memslot_move_prepare(data, sync, maxslots, true); 621 } 622 623 static bool test_memslot_move_prepare_inactive(struct vm_data *data, 624 struct sync_area *sync, 625 u64 *maxslots) 626 { 627 return test_memslot_move_prepare(data, sync, maxslots, false); 628 } 629 630 static void test_memslot_move_loop(struct vm_data *data, struct sync_area *sync) 631 { 632 u64 movesrcgpa; 633 634 movesrcgpa = vm_slot2gpa(data, data->nslots - 1); 635 vm_mem_region_move(data->vm, data->nslots - 1 + 1, 636 MEM_TEST_MOVE_GPA_DEST); 637 vm_mem_region_move(data->vm, data->nslots - 1 + 1, movesrcgpa); 638 } 639 640 static void test_memslot_do_unmap(struct vm_data *data, 641 u64 offsp, u64 count) 642 { 643 gpa_t gpa, ctr; 644 u32 guest_page_size = data->vm->page_size; 645 646 for (gpa = MEM_TEST_GPA + offsp * guest_page_size, ctr = 0; ctr < count; ) { 647 u64 npages; 648 void *hva; 649 int ret; 650 651 hva = vm_gpa2hva(data, gpa, &npages); 652 TEST_ASSERT(npages, "Empty memory slot at gptr 0x%"PRIx64, gpa); 653 npages = min(npages, count - ctr); 654 ret = madvise(hva, npages * guest_page_size, MADV_DONTNEED); 655 TEST_ASSERT(!ret, 656 "madvise(%p, MADV_DONTNEED) on VM memory should not fail for gptr 0x%"PRIx64, 657 hva, gpa); 658 ctr += npages; 659 gpa += npages * guest_page_size; 660 } 661 TEST_ASSERT(ctr == count, 662 "madvise(MADV_DONTNEED) should exactly cover all of the requested area"); 663 } 664 665 static void test_memslot_map_unmap_check(struct vm_data *data, 666 u64 offsp, u64 valexp) 667 { 668 gpa_t gpa; 669 u64 *val; 670 u32 guest_page_size = data->vm->page_size; 671 672 if (!map_unmap_verify) 673 return; 674 675 gpa = MEM_TEST_GPA + offsp * guest_page_size; 676 val = (typeof(val))vm_gpa2hva(data, gpa, NULL); 677 TEST_ASSERT(*val == valexp, 678 "Guest written values should read back correctly before unmap (%"PRIu64" vs %"PRIu64" @ %"PRIx64")", 679 *val, valexp, gpa); 680 *val = 0; 681 } 682 683 static void test_memslot_map_loop(struct vm_data *data, struct sync_area *sync) 684 { 685 u32 guest_page_size = data->vm->page_size; 686 u64 guest_pages = MEM_TEST_MAP_SIZE / guest_page_size; 687 688 /* 689 * Unmap the second half of the test area while guest writes to (maps) 690 * the first half. 691 */ 692 test_memslot_do_unmap(data, guest_pages / 2, guest_pages / 2); 693 694 /* 695 * Wait for the guest to finish writing the first half of the test 696 * area, verify the written value on the first and the last page of 697 * this area and then unmap it. 698 * Meanwhile, the guest is writing to (mapping) the second half of 699 * the test area. 700 */ 701 host_perform_sync(sync); 702 test_memslot_map_unmap_check(data, 0, MEM_TEST_VAL_1); 703 test_memslot_map_unmap_check(data, guest_pages / 2 - 1, MEM_TEST_VAL_1); 704 test_memslot_do_unmap(data, 0, guest_pages / 2); 705 706 707 /* 708 * Wait for the guest to finish writing the second half of the test 709 * area and verify the written value on the first and the last page 710 * of this area. 711 * The area will be unmapped at the beginning of the next loop 712 * iteration. 713 * Meanwhile, the guest is writing to (mapping) the first half of 714 * the test area. 715 */ 716 host_perform_sync(sync); 717 test_memslot_map_unmap_check(data, guest_pages / 2, MEM_TEST_VAL_2); 718 test_memslot_map_unmap_check(data, guest_pages - 1, MEM_TEST_VAL_2); 719 } 720 721 static void test_memslot_unmap_loop_common(struct vm_data *data, 722 struct sync_area *sync, 723 u64 chunk) 724 { 725 u32 guest_page_size = data->vm->page_size; 726 u64 guest_pages = MEM_TEST_UNMAP_SIZE / guest_page_size; 727 u64 ctr; 728 729 /* 730 * Wait for the guest to finish mapping page(s) in the first half 731 * of the test area, verify the written value and then perform unmap 732 * of this area. 733 * Meanwhile, the guest is writing to (mapping) page(s) in the second 734 * half of the test area. 735 */ 736 host_perform_sync(sync); 737 test_memslot_map_unmap_check(data, 0, MEM_TEST_VAL_1); 738 for (ctr = 0; ctr < guest_pages / 2; ctr += chunk) 739 test_memslot_do_unmap(data, ctr, chunk); 740 741 /* Likewise, but for the opposite host / guest areas */ 742 host_perform_sync(sync); 743 test_memslot_map_unmap_check(data, guest_pages / 2, MEM_TEST_VAL_2); 744 for (ctr = guest_pages / 2; ctr < guest_pages; ctr += chunk) 745 test_memslot_do_unmap(data, ctr, chunk); 746 } 747 748 static void test_memslot_unmap_loop(struct vm_data *data, 749 struct sync_area *sync) 750 { 751 u32 host_page_size = getpagesize(); 752 u32 guest_page_size = data->vm->page_size; 753 u64 guest_chunk_pages = guest_page_size >= host_page_size ? 754 1 : host_page_size / guest_page_size; 755 756 test_memslot_unmap_loop_common(data, sync, guest_chunk_pages); 757 } 758 759 static void test_memslot_unmap_loop_chunked(struct vm_data *data, 760 struct sync_area *sync) 761 { 762 u32 guest_page_size = data->vm->page_size; 763 u64 guest_chunk_pages = MEM_TEST_UNMAP_CHUNK_SIZE / guest_page_size; 764 765 test_memslot_unmap_loop_common(data, sync, guest_chunk_pages); 766 } 767 768 static void test_memslot_rw_loop(struct vm_data *data, struct sync_area *sync) 769 { 770 u64 gptr; 771 u32 guest_page_size = data->vm->page_size; 772 773 for (gptr = MEM_TEST_GPA + guest_page_size / 2; 774 gptr < MEM_TEST_GPA + MEM_TEST_SIZE; gptr += guest_page_size) 775 *(u64 *)vm_gpa2hva(data, gptr, NULL) = MEM_TEST_VAL_2; 776 777 host_perform_sync(sync); 778 779 for (gptr = MEM_TEST_GPA; 780 gptr < MEM_TEST_GPA + MEM_TEST_SIZE; gptr += guest_page_size) { 781 u64 *vptr = (typeof(vptr))vm_gpa2hva(data, gptr, NULL); 782 u64 val = *vptr; 783 784 TEST_ASSERT(val == MEM_TEST_VAL_1, 785 "Guest written values should read back correctly (is %"PRIu64" @ %"PRIx64")", 786 val, gptr); 787 *vptr = 0; 788 } 789 790 host_perform_sync(sync); 791 } 792 793 struct test_data { 794 const char *name; 795 u64 mem_size; 796 void (*guest_code)(void); 797 bool (*prepare)(struct vm_data *data, struct sync_area *sync, 798 u64 *maxslots); 799 void (*loop)(struct vm_data *data, struct sync_area *sync); 800 }; 801 802 static bool test_execute(int nslots, u64 *maxslots, 803 unsigned int maxtime, 804 const struct test_data *tdata, 805 u64 *nloops, 806 struct timespec *slot_runtime, 807 struct timespec *guest_runtime) 808 { 809 u64 mem_size = tdata->mem_size ? : MEM_SIZE; 810 struct vm_data *data; 811 struct sync_area *sync; 812 struct timespec tstart; 813 bool ret = true; 814 815 data = alloc_vm(); 816 if (!prepare_vm(data, nslots, maxslots, tdata->guest_code, 817 mem_size, slot_runtime)) { 818 ret = false; 819 goto exit_free; 820 } 821 822 sync = (typeof(sync))vm_gpa2hva(data, MEM_SYNC_GPA, NULL); 823 if (tdata->prepare && 824 !tdata->prepare(data, sync, maxslots)) { 825 ret = false; 826 goto exit_free; 827 } 828 829 launch_vm(data); 830 831 clock_gettime(CLOCK_MONOTONIC, &tstart); 832 let_guest_run(sync); 833 834 while (1) { 835 *guest_runtime = timespec_elapsed(tstart); 836 if (guest_runtime->tv_sec >= maxtime) 837 break; 838 839 tdata->loop(data, sync); 840 841 (*nloops)++; 842 } 843 844 make_guest_exit(sync); 845 wait_guest_exit(data); 846 847 exit_free: 848 free_vm(data); 849 850 return ret; 851 } 852 853 static const struct test_data tests[] = { 854 { 855 .name = "map", 856 .mem_size = MEM_SIZE_MAP, 857 .guest_code = guest_code_test_memslot_map, 858 .loop = test_memslot_map_loop, 859 }, 860 { 861 .name = "unmap", 862 .mem_size = MEM_TEST_UNMAP_SIZE + MEM_EXTRA_SIZE, 863 .guest_code = guest_code_test_memslot_unmap, 864 .loop = test_memslot_unmap_loop, 865 }, 866 { 867 .name = "unmap chunked", 868 .mem_size = MEM_TEST_UNMAP_SIZE + MEM_EXTRA_SIZE, 869 .guest_code = guest_code_test_memslot_unmap, 870 .loop = test_memslot_unmap_loop_chunked, 871 }, 872 { 873 .name = "move active area", 874 .guest_code = guest_code_test_memslot_move, 875 .prepare = test_memslot_move_prepare_active, 876 .loop = test_memslot_move_loop, 877 }, 878 { 879 .name = "move inactive area", 880 .guest_code = guest_code_test_memslot_move, 881 .prepare = test_memslot_move_prepare_inactive, 882 .loop = test_memslot_move_loop, 883 }, 884 { 885 .name = "RW", 886 .guest_code = guest_code_test_memslot_rw, 887 .loop = test_memslot_rw_loop 888 }, 889 }; 890 891 #define NTESTS ARRAY_SIZE(tests) 892 893 struct test_args { 894 int tfirst; 895 int tlast; 896 int nslots; 897 int seconds; 898 int runs; 899 }; 900 901 static void help(char *name, struct test_args *targs) 902 { 903 int ctr; 904 905 pr_info("usage: %s [-h] [-v] [-d] [-s slots] [-f first_test] [-e last_test] [-l test_length] [-r run_count] [-t wait_timeout]\n", 906 name); 907 pr_info(" -h: print this help screen.\n"); 908 pr_info(" -v: enable verbose mode (not for benchmarking).\n"); 909 pr_info(" -d: enable extra debug checks.\n"); 910 pr_info(" -q: Disable memslot zap quirk during memslot move.\n"); 911 pr_info(" -s: specify memslot count cap (-1 means no cap; currently: %i)\n", 912 targs->nslots); 913 pr_info(" -f: specify the first test to run (currently: %i; max %zu)\n", 914 targs->tfirst, NTESTS - 1); 915 pr_info(" -e: specify the last test to run (currently: %i; max %zu)\n", 916 targs->tlast, NTESTS - 1); 917 pr_info(" -l: specify the test length in seconds (currently: %i)\n", 918 targs->seconds); 919 pr_info(" -r: specify the number of runs per test (currently: %i)\n", 920 targs->runs); 921 pr_info(" -t: specify the number of seconds for host wait timeout (currently: %i)\n", 922 wait_timeout); 923 924 pr_info("\nAvailable tests:\n"); 925 for (ctr = 0; ctr < NTESTS; ctr++) 926 pr_info("%d: %s\n", ctr, tests[ctr].name); 927 } 928 929 static bool check_memory_sizes(void) 930 { 931 u32 host_page_size = getpagesize(); 932 u32 guest_page_size = vm_guest_mode_params[VM_MODE_DEFAULT].page_size; 933 934 if (host_page_size > SZ_64K || guest_page_size > SZ_64K) { 935 pr_info("Unsupported page size on host (0x%x) or guest (0x%x)\n", 936 host_page_size, guest_page_size); 937 return false; 938 } 939 940 if (MEM_SIZE % guest_page_size || 941 MEM_TEST_SIZE % guest_page_size) { 942 pr_info("invalid MEM_SIZE or MEM_TEST_SIZE\n"); 943 return false; 944 } 945 946 if (MEM_SIZE_MAP % guest_page_size || 947 MEM_TEST_MAP_SIZE % guest_page_size || 948 (MEM_TEST_MAP_SIZE / guest_page_size) <= 2 || 949 (MEM_TEST_MAP_SIZE / guest_page_size) % 2) { 950 pr_info("invalid MEM_SIZE_MAP or MEM_TEST_MAP_SIZE\n"); 951 return false; 952 } 953 954 if (MEM_TEST_UNMAP_SIZE > MEM_TEST_SIZE || 955 MEM_TEST_UNMAP_SIZE % guest_page_size || 956 (MEM_TEST_UNMAP_SIZE / guest_page_size) % 957 (2 * MEM_TEST_UNMAP_CHUNK_SIZE / guest_page_size)) { 958 pr_info("invalid MEM_TEST_UNMAP_SIZE or MEM_TEST_UNMAP_CHUNK_SIZE\n"); 959 return false; 960 } 961 962 return true; 963 } 964 965 static bool parse_args(int argc, char *argv[], 966 struct test_args *targs) 967 { 968 u32 max_mem_slots; 969 int opt; 970 971 while ((opt = getopt(argc, argv, "hvdqs:f:e:l:r:t:")) != -1) { 972 switch (opt) { 973 case 'h': 974 default: 975 help(argv[0], targs); 976 return false; 977 case 'v': 978 verbose = true; 979 break; 980 case 'd': 981 map_unmap_verify = true; 982 break; 983 #ifdef __x86_64__ 984 case 'q': 985 disable_slot_zap_quirk = true; 986 TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & 987 KVM_X86_QUIRK_SLOT_ZAP_ALL); 988 break; 989 #endif 990 case 's': 991 targs->nslots = atoi_paranoid(optarg); 992 if (targs->nslots <= 1 && targs->nslots != -1) { 993 pr_info("Slot count cap must be larger than 1 or -1 for no cap\n"); 994 return false; 995 } 996 break; 997 case 'f': 998 targs->tfirst = atoi_non_negative("First test", optarg); 999 break; 1000 case 'e': 1001 targs->tlast = atoi_non_negative("Last test", optarg); 1002 if (targs->tlast >= NTESTS) { 1003 pr_info("Last test to run has to be non-negative and less than %zu\n", 1004 NTESTS); 1005 return false; 1006 } 1007 break; 1008 case 'l': 1009 targs->seconds = atoi_non_negative("Test length", optarg); 1010 break; 1011 case 'r': 1012 targs->runs = atoi_positive("Runs per test", optarg); 1013 break; 1014 case 't': 1015 wait_timeout = atoi_positive("Host wait timeout", optarg); 1016 break; 1017 } 1018 } 1019 1020 if (optind < argc) { 1021 help(argv[0], targs); 1022 return false; 1023 } 1024 1025 if (targs->tfirst > targs->tlast) { 1026 pr_info("First test to run cannot be greater than the last test to run\n"); 1027 return false; 1028 } 1029 1030 max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS); 1031 if (max_mem_slots <= 1) { 1032 pr_info("KVM_CAP_NR_MEMSLOTS should be greater than 1\n"); 1033 return false; 1034 } 1035 1036 /* Memory slot 0 is reserved */ 1037 if (targs->nslots == -1) 1038 targs->nslots = max_mem_slots - 1; 1039 else 1040 targs->nslots = min_t(int, targs->nslots, max_mem_slots) - 1; 1041 1042 pr_info_v("Allowed Number of memory slots: %"PRIu32"\n", 1043 targs->nslots + 1); 1044 1045 return true; 1046 } 1047 1048 struct test_result { 1049 struct timespec slot_runtime, guest_runtime, iter_runtime; 1050 s64 slottimens, runtimens; 1051 u64 nloops; 1052 }; 1053 1054 static bool test_loop(const struct test_data *data, 1055 const struct test_args *targs, 1056 struct test_result *rbestslottime, 1057 struct test_result *rbestruntime) 1058 { 1059 u64 maxslots; 1060 struct test_result result = {}; 1061 1062 if (!test_execute(targs->nslots, &maxslots, targs->seconds, data, 1063 &result.nloops, 1064 &result.slot_runtime, &result.guest_runtime)) { 1065 if (maxslots) 1066 pr_info("Memslot count too high for this test, decrease the cap (max is %"PRIu64")\n", 1067 maxslots); 1068 else 1069 pr_info("Memslot count may be too high for this test, try adjusting the cap\n"); 1070 1071 return false; 1072 } 1073 1074 pr_info("Test took %ld.%.9lds for slot setup + %ld.%.9lds all iterations\n", 1075 result.slot_runtime.tv_sec, result.slot_runtime.tv_nsec, 1076 result.guest_runtime.tv_sec, result.guest_runtime.tv_nsec); 1077 if (!result.nloops) { 1078 pr_info("No full loops done - too short test time or system too loaded?\n"); 1079 return true; 1080 } 1081 1082 result.iter_runtime = timespec_div(result.guest_runtime, 1083 result.nloops); 1084 pr_info("Done %"PRIu64" iterations, avg %ld.%.9lds each\n", 1085 result.nloops, 1086 result.iter_runtime.tv_sec, 1087 result.iter_runtime.tv_nsec); 1088 result.slottimens = timespec_to_ns(result.slot_runtime); 1089 result.runtimens = timespec_to_ns(result.iter_runtime); 1090 1091 /* 1092 * Only rank the slot setup time for tests using the whole test memory 1093 * area so they are comparable 1094 */ 1095 if (!data->mem_size && 1096 (!rbestslottime->slottimens || 1097 result.slottimens < rbestslottime->slottimens)) 1098 *rbestslottime = result; 1099 if (!rbestruntime->runtimens || 1100 result.runtimens < rbestruntime->runtimens) 1101 *rbestruntime = result; 1102 1103 return true; 1104 } 1105 1106 int main(int argc, char *argv[]) 1107 { 1108 struct test_args targs = { 1109 .tfirst = 0, 1110 .tlast = NTESTS - 1, 1111 .nslots = -1, 1112 .seconds = 5, 1113 .runs = 1, 1114 }; 1115 struct test_result rbestslottime = {}; 1116 int tctr; 1117 1118 if (!check_memory_sizes()) 1119 return -1; 1120 1121 if (!parse_args(argc, argv, &targs)) 1122 return -1; 1123 1124 for (tctr = targs.tfirst; tctr <= targs.tlast; tctr++) { 1125 const struct test_data *data = &tests[tctr]; 1126 unsigned int runctr; 1127 struct test_result rbestruntime = {}; 1128 1129 if (tctr > targs.tfirst) 1130 pr_info("\n"); 1131 1132 pr_info("Testing %s performance with %i runs, %d seconds each\n", 1133 data->name, targs.runs, targs.seconds); 1134 1135 for (runctr = 0; runctr < targs.runs; runctr++) 1136 if (!test_loop(data, &targs, 1137 &rbestslottime, &rbestruntime)) 1138 break; 1139 1140 if (rbestruntime.runtimens) 1141 pr_info("Best runtime result was %ld.%.9lds per iteration (with %"PRIu64" iterations)\n", 1142 rbestruntime.iter_runtime.tv_sec, 1143 rbestruntime.iter_runtime.tv_nsec, 1144 rbestruntime.nloops); 1145 } 1146 1147 if (rbestslottime.slottimens) 1148 pr_info("Best slot setup time for the whole test area was %ld.%.9lds\n", 1149 rbestslottime.slot_runtime.tv_sec, 1150 rbestslottime.slot_runtime.tv_nsec); 1151 1152 return 0; 1153 } 1154