Path: blob/master/tools/testing/selftests/kvm/memslot_perf_test.c
38189 views
// SPDX-License-Identifier: GPL-2.01/*2* A memslot-related performance benchmark.3*4* Copyright (C) 2021 Oracle and/or its affiliates.5*6* Basic guest setup / host vCPU thread code lifted from set_memory_region_test.7*/8#include <pthread.h>9#include <sched.h>10#include <semaphore.h>11#include <stdatomic.h>12#include <stdbool.h>13#include <stdint.h>14#include <stdio.h>15#include <stdlib.h>16#include <string.h>17#include <sys/mman.h>18#include <time.h>19#include <unistd.h>2021#include <linux/compiler.h>22#include <linux/sizes.h>2324#include <test_util.h>25#include <kvm_util.h>26#include <processor.h>27#include <ucall_common.h>2829#define MEM_EXTRA_SIZE SZ_64K3031#define MEM_SIZE (SZ_512M + MEM_EXTRA_SIZE)32#define MEM_GPA SZ_256M33#define MEM_AUX_GPA MEM_GPA34#define MEM_SYNC_GPA MEM_AUX_GPA35#define MEM_TEST_GPA (MEM_AUX_GPA + MEM_EXTRA_SIZE)36#define MEM_TEST_SIZE (MEM_SIZE - MEM_EXTRA_SIZE)3738/*39* 32 MiB is max size that gets well over 100 iterations on 509 slots.40* Considering that each slot needs to have at least one page up to41* 8194 slots in use can then be tested (although with slightly42* limited resolution).43*/44#define MEM_SIZE_MAP (SZ_32M + MEM_EXTRA_SIZE)45#define MEM_TEST_MAP_SIZE (MEM_SIZE_MAP - MEM_EXTRA_SIZE)4647/*48* 128 MiB is min size that fills 32k slots with at least one page in each49* while at the same time gets 100+ iterations in such test50*51* 2 MiB chunk size like a typical huge page52*/53#define MEM_TEST_UNMAP_SIZE SZ_128M54#define MEM_TEST_UNMAP_CHUNK_SIZE SZ_2M5556/*57* For the move active test the middle of the test area is placed on58* a memslot boundary: half lies in the memslot being moved, half in59* other memslot(s).60*61* We have different number of memory slots, excluding the reserved62* memory slot 0, on various architectures and configurations. The63* memory size in this test is calculated by picking the maximal64* last memory slot's memory size, with alignment to the largest65* supported page size (64KB). In this way, the selected memory66* size for this test is compatible with test_memslot_move_prepare().67*68* architecture slots memory-per-slot memory-on-last-slot69* --------------------------------------------------------------70* x86-4KB 32763 16KB 160KB71* arm64-4KB 32766 16KB 112KB72* arm64-16KB 32766 16KB 112KB73* arm64-64KB 8192 64KB 128KB74*/75#define MEM_TEST_MOVE_SIZE (3 * SZ_64K)76#define MEM_TEST_MOVE_GPA_DEST (MEM_GPA + MEM_SIZE)77static_assert(MEM_TEST_MOVE_SIZE <= MEM_TEST_SIZE,78"invalid move test region size");7980#define MEM_TEST_VAL_1 0x112233445566778881#define MEM_TEST_VAL_2 0x99AABBCCDDEEFF008283struct vm_data {84struct kvm_vm *vm;85struct kvm_vcpu *vcpu;86pthread_t vcpu_thread;87uint32_t nslots;88uint64_t npages;89uint64_t pages_per_slot;90void **hva_slots;91bool mmio_ok;92uint64_t mmio_gpa_min;93uint64_t mmio_gpa_max;94};9596struct sync_area {97uint32_t guest_page_size;98atomic_bool start_flag;99atomic_bool exit_flag;100atomic_bool sync_flag;101void *move_area_ptr;102};103104/*105* Technically, we need also for the atomic bool to be address-free, which106* is recommended, but not strictly required, by C11 for lockless107* implementations.108* However, in practice both GCC and Clang fulfill this requirement on109* all KVM-supported platforms.110*/111static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "atomic bool is not lockless");112113static sem_t vcpu_ready;114115static bool map_unmap_verify;116#ifdef __x86_64__117static bool disable_slot_zap_quirk;118#endif119120static bool verbose;121#define pr_info_v(...) \122do { \123if (verbose) \124pr_info(__VA_ARGS__); \125} while (0)126127static void check_mmio_access(struct vm_data *data, struct kvm_run *run)128{129TEST_ASSERT(data->mmio_ok, "Unexpected mmio exit");130TEST_ASSERT(run->mmio.is_write, "Unexpected mmio read");131TEST_ASSERT(run->mmio.len == 8,132"Unexpected exit mmio size = %u", run->mmio.len);133TEST_ASSERT(run->mmio.phys_addr >= data->mmio_gpa_min &&134run->mmio.phys_addr <= data->mmio_gpa_max,135"Unexpected exit mmio address = 0x%llx",136run->mmio.phys_addr);137}138139static void *vcpu_worker(void *__data)140{141struct vm_data *data = __data;142struct kvm_vcpu *vcpu = data->vcpu;143struct kvm_run *run = vcpu->run;144struct ucall uc;145146while (1) {147vcpu_run(vcpu);148149switch (get_ucall(vcpu, &uc)) {150case UCALL_SYNC:151TEST_ASSERT(uc.args[1] == 0,152"Unexpected sync ucall, got %lx",153(ulong)uc.args[1]);154sem_post(&vcpu_ready);155continue;156case UCALL_NONE:157if (run->exit_reason == KVM_EXIT_MMIO)158check_mmio_access(data, run);159else160goto done;161break;162case UCALL_ABORT:163REPORT_GUEST_ASSERT(uc);164break;165case UCALL_DONE:166goto done;167default:168TEST_FAIL("Unknown ucall %lu", uc.cmd);169}170}171172done:173return NULL;174}175176static void wait_for_vcpu(void)177{178struct timespec ts;179180TEST_ASSERT(!clock_gettime(CLOCK_REALTIME, &ts),181"clock_gettime() failed: %d", errno);182183ts.tv_sec += 2;184TEST_ASSERT(!sem_timedwait(&vcpu_ready, &ts),185"sem_timedwait() failed: %d", errno);186}187188static void *vm_gpa2hva(struct vm_data *data, uint64_t gpa, uint64_t *rempages)189{190uint64_t gpage, pgoffs;191uint32_t slot, slotoffs;192void *base;193uint32_t guest_page_size = data->vm->page_size;194195TEST_ASSERT(gpa >= MEM_GPA, "Too low gpa to translate");196TEST_ASSERT(gpa < MEM_GPA + data->npages * guest_page_size,197"Too high gpa to translate");198gpa -= MEM_GPA;199200gpage = gpa / guest_page_size;201pgoffs = gpa % guest_page_size;202slot = min(gpage / data->pages_per_slot, (uint64_t)data->nslots - 1);203slotoffs = gpage - (slot * data->pages_per_slot);204205if (rempages) {206uint64_t slotpages;207208if (slot == data->nslots - 1)209slotpages = data->npages - slot * data->pages_per_slot;210else211slotpages = data->pages_per_slot;212213TEST_ASSERT(!pgoffs,214"Asking for remaining pages in slot but gpa not page aligned");215*rempages = slotpages - slotoffs;216}217218base = data->hva_slots[slot];219return (uint8_t *)base + slotoffs * guest_page_size + pgoffs;220}221222static uint64_t vm_slot2gpa(struct vm_data *data, uint32_t slot)223{224uint32_t guest_page_size = data->vm->page_size;225226TEST_ASSERT(slot < data->nslots, "Too high slot number");227228return MEM_GPA + slot * data->pages_per_slot * guest_page_size;229}230231static struct vm_data *alloc_vm(void)232{233struct vm_data *data;234235data = malloc(sizeof(*data));236TEST_ASSERT(data, "malloc(vmdata) failed");237238data->vm = NULL;239data->vcpu = NULL;240data->hva_slots = NULL;241242return data;243}244245static bool check_slot_pages(uint32_t host_page_size, uint32_t guest_page_size,246uint64_t pages_per_slot, uint64_t rempages)247{248if (!pages_per_slot)249return false;250251if ((pages_per_slot * guest_page_size) % host_page_size)252return false;253254if ((rempages * guest_page_size) % host_page_size)255return false;256257return true;258}259260261static uint64_t get_max_slots(struct vm_data *data, uint32_t host_page_size)262{263uint32_t guest_page_size = data->vm->page_size;264uint64_t mempages, pages_per_slot, rempages;265uint64_t slots;266267mempages = data->npages;268slots = data->nslots;269while (--slots > 1) {270pages_per_slot = mempages / slots;271if (!pages_per_slot)272continue;273274rempages = mempages % pages_per_slot;275if (check_slot_pages(host_page_size, guest_page_size,276pages_per_slot, rempages))277return slots + 1; /* slot 0 is reserved */278}279280return 0;281}282283static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots,284void *guest_code, uint64_t mem_size,285struct timespec *slot_runtime)286{287uint64_t mempages, rempages;288uint64_t guest_addr;289uint32_t slot, host_page_size, guest_page_size;290struct timespec tstart;291struct sync_area *sync;292293host_page_size = getpagesize();294guest_page_size = vm_guest_mode_params[VM_MODE_DEFAULT].page_size;295mempages = mem_size / guest_page_size;296297data->vm = __vm_create_with_one_vcpu(&data->vcpu, mempages, guest_code);298TEST_ASSERT(data->vm->page_size == guest_page_size, "Invalid VM page size");299300data->npages = mempages;301TEST_ASSERT(data->npages > 1, "Can't test without any memory");302data->nslots = nslots;303data->pages_per_slot = data->npages / data->nslots;304rempages = data->npages % data->nslots;305if (!check_slot_pages(host_page_size, guest_page_size,306data->pages_per_slot, rempages)) {307*maxslots = get_max_slots(data, host_page_size);308return false;309}310311data->hva_slots = malloc(sizeof(*data->hva_slots) * data->nslots);312TEST_ASSERT(data->hva_slots, "malloc() fail");313314pr_info_v("Adding slots 1..%i, each slot with %"PRIu64" pages + %"PRIu64" extra pages last\n",315data->nslots, data->pages_per_slot, rempages);316317clock_gettime(CLOCK_MONOTONIC, &tstart);318for (slot = 1, guest_addr = MEM_GPA; slot <= data->nslots; slot++) {319uint64_t npages;320321npages = data->pages_per_slot;322if (slot == data->nslots)323npages += rempages;324325vm_userspace_mem_region_add(data->vm, VM_MEM_SRC_ANONYMOUS,326guest_addr, slot, npages,3270);328guest_addr += npages * guest_page_size;329}330*slot_runtime = timespec_elapsed(tstart);331332for (slot = 1, guest_addr = MEM_GPA; slot <= data->nslots; slot++) {333uint64_t npages;334uint64_t gpa;335336npages = data->pages_per_slot;337if (slot == data->nslots)338npages += rempages;339340gpa = vm_phy_pages_alloc(data->vm, npages, guest_addr, slot);341TEST_ASSERT(gpa == guest_addr,342"vm_phy_pages_alloc() failed");343344data->hva_slots[slot - 1] = addr_gpa2hva(data->vm, guest_addr);345memset(data->hva_slots[slot - 1], 0, npages * guest_page_size);346347guest_addr += npages * guest_page_size;348}349350virt_map(data->vm, MEM_GPA, MEM_GPA, data->npages);351352sync = (typeof(sync))vm_gpa2hva(data, MEM_SYNC_GPA, NULL);353sync->guest_page_size = data->vm->page_size;354atomic_init(&sync->start_flag, false);355atomic_init(&sync->exit_flag, false);356atomic_init(&sync->sync_flag, false);357358data->mmio_ok = false;359360return true;361}362363static void launch_vm(struct vm_data *data)364{365pr_info_v("Launching the test VM\n");366367pthread_create(&data->vcpu_thread, NULL, vcpu_worker, data);368369/* Ensure the guest thread is spun up. */370wait_for_vcpu();371}372373static void free_vm(struct vm_data *data)374{375kvm_vm_free(data->vm);376free(data->hva_slots);377free(data);378}379380static void wait_guest_exit(struct vm_data *data)381{382pthread_join(data->vcpu_thread, NULL);383}384385static void let_guest_run(struct sync_area *sync)386{387atomic_store_explicit(&sync->start_flag, true, memory_order_release);388}389390static void guest_spin_until_start(void)391{392struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA;393394while (!atomic_load_explicit(&sync->start_flag, memory_order_acquire))395;396}397398static void make_guest_exit(struct sync_area *sync)399{400atomic_store_explicit(&sync->exit_flag, true, memory_order_release);401}402403static bool _guest_should_exit(void)404{405struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA;406407return atomic_load_explicit(&sync->exit_flag, memory_order_acquire);408}409410#define guest_should_exit() unlikely(_guest_should_exit())411412/*413* noinline so we can easily see how much time the host spends waiting414* for the guest.415* For the same reason use alarm() instead of polling clock_gettime()416* to implement a wait timeout.417*/418static noinline void host_perform_sync(struct sync_area *sync)419{420alarm(10);421422atomic_store_explicit(&sync->sync_flag, true, memory_order_release);423while (atomic_load_explicit(&sync->sync_flag, memory_order_acquire))424;425426alarm(0);427}428429static bool guest_perform_sync(void)430{431struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA;432bool expected;433434do {435if (guest_should_exit())436return false;437438expected = true;439} while (!atomic_compare_exchange_weak_explicit(&sync->sync_flag,440&expected, false,441memory_order_acq_rel,442memory_order_relaxed));443444return true;445}446447static void guest_code_test_memslot_move(void)448{449struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA;450uint32_t page_size = (typeof(page_size))READ_ONCE(sync->guest_page_size);451uintptr_t base = (typeof(base))READ_ONCE(sync->move_area_ptr);452453GUEST_SYNC(0);454455guest_spin_until_start();456457while (!guest_should_exit()) {458uintptr_t ptr;459460for (ptr = base; ptr < base + MEM_TEST_MOVE_SIZE;461ptr += page_size)462*(uint64_t *)ptr = MEM_TEST_VAL_1;463464/*465* No host sync here since the MMIO exits are so expensive466* that the host would spend most of its time waiting for467* the guest and so instead of measuring memslot move468* performance we would measure the performance and469* likelihood of MMIO exits470*/471}472473GUEST_DONE();474}475476static void guest_code_test_memslot_map(void)477{478struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA;479uint32_t page_size = (typeof(page_size))READ_ONCE(sync->guest_page_size);480481GUEST_SYNC(0);482483guest_spin_until_start();484485while (1) {486uintptr_t ptr;487488for (ptr = MEM_TEST_GPA;489ptr < MEM_TEST_GPA + MEM_TEST_MAP_SIZE / 2;490ptr += page_size)491*(uint64_t *)ptr = MEM_TEST_VAL_1;492493if (!guest_perform_sync())494break;495496for (ptr = MEM_TEST_GPA + MEM_TEST_MAP_SIZE / 2;497ptr < MEM_TEST_GPA + MEM_TEST_MAP_SIZE;498ptr += page_size)499*(uint64_t *)ptr = MEM_TEST_VAL_2;500501if (!guest_perform_sync())502break;503}504505GUEST_DONE();506}507508static void guest_code_test_memslot_unmap(void)509{510struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA;511512GUEST_SYNC(0);513514guest_spin_until_start();515516while (1) {517uintptr_t ptr = MEM_TEST_GPA;518519/*520* We can afford to access (map) just a small number of pages521* per host sync as otherwise the host will spend522* a significant amount of its time waiting for the guest523* (instead of doing unmap operations), so this will524* effectively turn this test into a map performance test.525*526* Just access a single page to be on the safe side.527*/528*(uint64_t *)ptr = MEM_TEST_VAL_1;529530if (!guest_perform_sync())531break;532533ptr += MEM_TEST_UNMAP_SIZE / 2;534*(uint64_t *)ptr = MEM_TEST_VAL_2;535536if (!guest_perform_sync())537break;538}539540GUEST_DONE();541}542543static void guest_code_test_memslot_rw(void)544{545struct sync_area *sync = (typeof(sync))MEM_SYNC_GPA;546uint32_t page_size = (typeof(page_size))READ_ONCE(sync->guest_page_size);547548GUEST_SYNC(0);549550guest_spin_until_start();551552while (1) {553uintptr_t ptr;554555for (ptr = MEM_TEST_GPA;556ptr < MEM_TEST_GPA + MEM_TEST_SIZE; ptr += page_size)557*(uint64_t *)ptr = MEM_TEST_VAL_1;558559if (!guest_perform_sync())560break;561562for (ptr = MEM_TEST_GPA + page_size / 2;563ptr < MEM_TEST_GPA + MEM_TEST_SIZE; ptr += page_size) {564uint64_t val = *(uint64_t *)ptr;565566GUEST_ASSERT_EQ(val, MEM_TEST_VAL_2);567*(uint64_t *)ptr = 0;568}569570if (!guest_perform_sync())571break;572}573574GUEST_DONE();575}576577static bool test_memslot_move_prepare(struct vm_data *data,578struct sync_area *sync,579uint64_t *maxslots, bool isactive)580{581uint32_t guest_page_size = data->vm->page_size;582uint64_t movesrcgpa, movetestgpa;583584#ifdef __x86_64__585if (disable_slot_zap_quirk)586vm_enable_cap(data->vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);587#endif588589movesrcgpa = vm_slot2gpa(data, data->nslots - 1);590591if (isactive) {592uint64_t lastpages;593594vm_gpa2hva(data, movesrcgpa, &lastpages);595if (lastpages * guest_page_size < MEM_TEST_MOVE_SIZE / 2) {596*maxslots = 0;597return false;598}599}600601movetestgpa = movesrcgpa - (MEM_TEST_MOVE_SIZE / (isactive ? 2 : 1));602sync->move_area_ptr = (void *)movetestgpa;603604if (isactive) {605data->mmio_ok = true;606data->mmio_gpa_min = movesrcgpa;607data->mmio_gpa_max = movesrcgpa + MEM_TEST_MOVE_SIZE / 2 - 1;608}609610return true;611}612613static bool test_memslot_move_prepare_active(struct vm_data *data,614struct sync_area *sync,615uint64_t *maxslots)616{617return test_memslot_move_prepare(data, sync, maxslots, true);618}619620static bool test_memslot_move_prepare_inactive(struct vm_data *data,621struct sync_area *sync,622uint64_t *maxslots)623{624return test_memslot_move_prepare(data, sync, maxslots, false);625}626627static void test_memslot_move_loop(struct vm_data *data, struct sync_area *sync)628{629uint64_t movesrcgpa;630631movesrcgpa = vm_slot2gpa(data, data->nslots - 1);632vm_mem_region_move(data->vm, data->nslots - 1 + 1,633MEM_TEST_MOVE_GPA_DEST);634vm_mem_region_move(data->vm, data->nslots - 1 + 1, movesrcgpa);635}636637static void test_memslot_do_unmap(struct vm_data *data,638uint64_t offsp, uint64_t count)639{640uint64_t gpa, ctr;641uint32_t guest_page_size = data->vm->page_size;642643for (gpa = MEM_TEST_GPA + offsp * guest_page_size, ctr = 0; ctr < count; ) {644uint64_t npages;645void *hva;646int ret;647648hva = vm_gpa2hva(data, gpa, &npages);649TEST_ASSERT(npages, "Empty memory slot at gptr 0x%"PRIx64, gpa);650npages = min(npages, count - ctr);651ret = madvise(hva, npages * guest_page_size, MADV_DONTNEED);652TEST_ASSERT(!ret,653"madvise(%p, MADV_DONTNEED) on VM memory should not fail for gptr 0x%"PRIx64,654hva, gpa);655ctr += npages;656gpa += npages * guest_page_size;657}658TEST_ASSERT(ctr == count,659"madvise(MADV_DONTNEED) should exactly cover all of the requested area");660}661662static void test_memslot_map_unmap_check(struct vm_data *data,663uint64_t offsp, uint64_t valexp)664{665uint64_t gpa;666uint64_t *val;667uint32_t guest_page_size = data->vm->page_size;668669if (!map_unmap_verify)670return;671672gpa = MEM_TEST_GPA + offsp * guest_page_size;673val = (typeof(val))vm_gpa2hva(data, gpa, NULL);674TEST_ASSERT(*val == valexp,675"Guest written values should read back correctly before unmap (%"PRIu64" vs %"PRIu64" @ %"PRIx64")",676*val, valexp, gpa);677*val = 0;678}679680static void test_memslot_map_loop(struct vm_data *data, struct sync_area *sync)681{682uint32_t guest_page_size = data->vm->page_size;683uint64_t guest_pages = MEM_TEST_MAP_SIZE / guest_page_size;684685/*686* Unmap the second half of the test area while guest writes to (maps)687* the first half.688*/689test_memslot_do_unmap(data, guest_pages / 2, guest_pages / 2);690691/*692* Wait for the guest to finish writing the first half of the test693* area, verify the written value on the first and the last page of694* this area and then unmap it.695* Meanwhile, the guest is writing to (mapping) the second half of696* the test area.697*/698host_perform_sync(sync);699test_memslot_map_unmap_check(data, 0, MEM_TEST_VAL_1);700test_memslot_map_unmap_check(data, guest_pages / 2 - 1, MEM_TEST_VAL_1);701test_memslot_do_unmap(data, 0, guest_pages / 2);702703704/*705* Wait for the guest to finish writing the second half of the test706* area and verify the written value on the first and the last page707* of this area.708* The area will be unmapped at the beginning of the next loop709* iteration.710* Meanwhile, the guest is writing to (mapping) the first half of711* the test area.712*/713host_perform_sync(sync);714test_memslot_map_unmap_check(data, guest_pages / 2, MEM_TEST_VAL_2);715test_memslot_map_unmap_check(data, guest_pages - 1, MEM_TEST_VAL_2);716}717718static void test_memslot_unmap_loop_common(struct vm_data *data,719struct sync_area *sync,720uint64_t chunk)721{722uint32_t guest_page_size = data->vm->page_size;723uint64_t guest_pages = MEM_TEST_UNMAP_SIZE / guest_page_size;724uint64_t ctr;725726/*727* Wait for the guest to finish mapping page(s) in the first half728* of the test area, verify the written value and then perform unmap729* of this area.730* Meanwhile, the guest is writing to (mapping) page(s) in the second731* half of the test area.732*/733host_perform_sync(sync);734test_memslot_map_unmap_check(data, 0, MEM_TEST_VAL_1);735for (ctr = 0; ctr < guest_pages / 2; ctr += chunk)736test_memslot_do_unmap(data, ctr, chunk);737738/* Likewise, but for the opposite host / guest areas */739host_perform_sync(sync);740test_memslot_map_unmap_check(data, guest_pages / 2, MEM_TEST_VAL_2);741for (ctr = guest_pages / 2; ctr < guest_pages; ctr += chunk)742test_memslot_do_unmap(data, ctr, chunk);743}744745static void test_memslot_unmap_loop(struct vm_data *data,746struct sync_area *sync)747{748uint32_t host_page_size = getpagesize();749uint32_t guest_page_size = data->vm->page_size;750uint64_t guest_chunk_pages = guest_page_size >= host_page_size ?7511 : host_page_size / guest_page_size;752753test_memslot_unmap_loop_common(data, sync, guest_chunk_pages);754}755756static void test_memslot_unmap_loop_chunked(struct vm_data *data,757struct sync_area *sync)758{759uint32_t guest_page_size = data->vm->page_size;760uint64_t guest_chunk_pages = MEM_TEST_UNMAP_CHUNK_SIZE / guest_page_size;761762test_memslot_unmap_loop_common(data, sync, guest_chunk_pages);763}764765static void test_memslot_rw_loop(struct vm_data *data, struct sync_area *sync)766{767uint64_t gptr;768uint32_t guest_page_size = data->vm->page_size;769770for (gptr = MEM_TEST_GPA + guest_page_size / 2;771gptr < MEM_TEST_GPA + MEM_TEST_SIZE; gptr += guest_page_size)772*(uint64_t *)vm_gpa2hva(data, gptr, NULL) = MEM_TEST_VAL_2;773774host_perform_sync(sync);775776for (gptr = MEM_TEST_GPA;777gptr < MEM_TEST_GPA + MEM_TEST_SIZE; gptr += guest_page_size) {778uint64_t *vptr = (typeof(vptr))vm_gpa2hva(data, gptr, NULL);779uint64_t val = *vptr;780781TEST_ASSERT(val == MEM_TEST_VAL_1,782"Guest written values should read back correctly (is %"PRIu64" @ %"PRIx64")",783val, gptr);784*vptr = 0;785}786787host_perform_sync(sync);788}789790struct test_data {791const char *name;792uint64_t mem_size;793void (*guest_code)(void);794bool (*prepare)(struct vm_data *data, struct sync_area *sync,795uint64_t *maxslots);796void (*loop)(struct vm_data *data, struct sync_area *sync);797};798799static bool test_execute(int nslots, uint64_t *maxslots,800unsigned int maxtime,801const struct test_data *tdata,802uint64_t *nloops,803struct timespec *slot_runtime,804struct timespec *guest_runtime)805{806uint64_t mem_size = tdata->mem_size ? : MEM_SIZE;807struct vm_data *data;808struct sync_area *sync;809struct timespec tstart;810bool ret = true;811812data = alloc_vm();813if (!prepare_vm(data, nslots, maxslots, tdata->guest_code,814mem_size, slot_runtime)) {815ret = false;816goto exit_free;817}818819sync = (typeof(sync))vm_gpa2hva(data, MEM_SYNC_GPA, NULL);820if (tdata->prepare &&821!tdata->prepare(data, sync, maxslots)) {822ret = false;823goto exit_free;824}825826launch_vm(data);827828clock_gettime(CLOCK_MONOTONIC, &tstart);829let_guest_run(sync);830831while (1) {832*guest_runtime = timespec_elapsed(tstart);833if (guest_runtime->tv_sec >= maxtime)834break;835836tdata->loop(data, sync);837838(*nloops)++;839}840841make_guest_exit(sync);842wait_guest_exit(data);843844exit_free:845free_vm(data);846847return ret;848}849850static const struct test_data tests[] = {851{852.name = "map",853.mem_size = MEM_SIZE_MAP,854.guest_code = guest_code_test_memslot_map,855.loop = test_memslot_map_loop,856},857{858.name = "unmap",859.mem_size = MEM_TEST_UNMAP_SIZE + MEM_EXTRA_SIZE,860.guest_code = guest_code_test_memslot_unmap,861.loop = test_memslot_unmap_loop,862},863{864.name = "unmap chunked",865.mem_size = MEM_TEST_UNMAP_SIZE + MEM_EXTRA_SIZE,866.guest_code = guest_code_test_memslot_unmap,867.loop = test_memslot_unmap_loop_chunked,868},869{870.name = "move active area",871.guest_code = guest_code_test_memslot_move,872.prepare = test_memslot_move_prepare_active,873.loop = test_memslot_move_loop,874},875{876.name = "move inactive area",877.guest_code = guest_code_test_memslot_move,878.prepare = test_memslot_move_prepare_inactive,879.loop = test_memslot_move_loop,880},881{882.name = "RW",883.guest_code = guest_code_test_memslot_rw,884.loop = test_memslot_rw_loop885},886};887888#define NTESTS ARRAY_SIZE(tests)889890struct test_args {891int tfirst;892int tlast;893int nslots;894int seconds;895int runs;896};897898static void help(char *name, struct test_args *targs)899{900int ctr;901902pr_info("usage: %s [-h] [-v] [-d] [-s slots] [-f first_test] [-e last_test] [-l test_length] [-r run_count]\n",903name);904pr_info(" -h: print this help screen.\n");905pr_info(" -v: enable verbose mode (not for benchmarking).\n");906pr_info(" -d: enable extra debug checks.\n");907pr_info(" -q: Disable memslot zap quirk during memslot move.\n");908pr_info(" -s: specify memslot count cap (-1 means no cap; currently: %i)\n",909targs->nslots);910pr_info(" -f: specify the first test to run (currently: %i; max %zu)\n",911targs->tfirst, NTESTS - 1);912pr_info(" -e: specify the last test to run (currently: %i; max %zu)\n",913targs->tlast, NTESTS - 1);914pr_info(" -l: specify the test length in seconds (currently: %i)\n",915targs->seconds);916pr_info(" -r: specify the number of runs per test (currently: %i)\n",917targs->runs);918919pr_info("\nAvailable tests:\n");920for (ctr = 0; ctr < NTESTS; ctr++)921pr_info("%d: %s\n", ctr, tests[ctr].name);922}923924static bool check_memory_sizes(void)925{926uint32_t host_page_size = getpagesize();927uint32_t guest_page_size = vm_guest_mode_params[VM_MODE_DEFAULT].page_size;928929if (host_page_size > SZ_64K || guest_page_size > SZ_64K) {930pr_info("Unsupported page size on host (0x%x) or guest (0x%x)\n",931host_page_size, guest_page_size);932return false;933}934935if (MEM_SIZE % guest_page_size ||936MEM_TEST_SIZE % guest_page_size) {937pr_info("invalid MEM_SIZE or MEM_TEST_SIZE\n");938return false;939}940941if (MEM_SIZE_MAP % guest_page_size ||942MEM_TEST_MAP_SIZE % guest_page_size ||943(MEM_TEST_MAP_SIZE / guest_page_size) <= 2 ||944(MEM_TEST_MAP_SIZE / guest_page_size) % 2) {945pr_info("invalid MEM_SIZE_MAP or MEM_TEST_MAP_SIZE\n");946return false;947}948949if (MEM_TEST_UNMAP_SIZE > MEM_TEST_SIZE ||950MEM_TEST_UNMAP_SIZE % guest_page_size ||951(MEM_TEST_UNMAP_SIZE / guest_page_size) %952(2 * MEM_TEST_UNMAP_CHUNK_SIZE / guest_page_size)) {953pr_info("invalid MEM_TEST_UNMAP_SIZE or MEM_TEST_UNMAP_CHUNK_SIZE\n");954return false;955}956957return true;958}959960static bool parse_args(int argc, char *argv[],961struct test_args *targs)962{963uint32_t max_mem_slots;964int opt;965966while ((opt = getopt(argc, argv, "hvdqs:f:e:l:r:")) != -1) {967switch (opt) {968case 'h':969default:970help(argv[0], targs);971return false;972case 'v':973verbose = true;974break;975case 'd':976map_unmap_verify = true;977break;978#ifdef __x86_64__979case 'q':980disable_slot_zap_quirk = true;981TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &982KVM_X86_QUIRK_SLOT_ZAP_ALL);983break;984#endif985case 's':986targs->nslots = atoi_paranoid(optarg);987if (targs->nslots <= 1 && targs->nslots != -1) {988pr_info("Slot count cap must be larger than 1 or -1 for no cap\n");989return false;990}991break;992case 'f':993targs->tfirst = atoi_non_negative("First test", optarg);994break;995case 'e':996targs->tlast = atoi_non_negative("Last test", optarg);997if (targs->tlast >= NTESTS) {998pr_info("Last test to run has to be non-negative and less than %zu\n",999NTESTS);1000return false;1001}1002break;1003case 'l':1004targs->seconds = atoi_non_negative("Test length", optarg);1005break;1006case 'r':1007targs->runs = atoi_positive("Runs per test", optarg);1008break;1009}1010}10111012if (optind < argc) {1013help(argv[0], targs);1014return false;1015}10161017if (targs->tfirst > targs->tlast) {1018pr_info("First test to run cannot be greater than the last test to run\n");1019return false;1020}10211022max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS);1023if (max_mem_slots <= 1) {1024pr_info("KVM_CAP_NR_MEMSLOTS should be greater than 1\n");1025return false;1026}10271028/* Memory slot 0 is reserved */1029if (targs->nslots == -1)1030targs->nslots = max_mem_slots - 1;1031else1032targs->nslots = min_t(int, targs->nslots, max_mem_slots) - 1;10331034pr_info_v("Allowed Number of memory slots: %"PRIu32"\n",1035targs->nslots + 1);10361037return true;1038}10391040struct test_result {1041struct timespec slot_runtime, guest_runtime, iter_runtime;1042int64_t slottimens, runtimens;1043uint64_t nloops;1044};10451046static bool test_loop(const struct test_data *data,1047const struct test_args *targs,1048struct test_result *rbestslottime,1049struct test_result *rbestruntime)1050{1051uint64_t maxslots;1052struct test_result result = {};10531054if (!test_execute(targs->nslots, &maxslots, targs->seconds, data,1055&result.nloops,1056&result.slot_runtime, &result.guest_runtime)) {1057if (maxslots)1058pr_info("Memslot count too high for this test, decrease the cap (max is %"PRIu64")\n",1059maxslots);1060else1061pr_info("Memslot count may be too high for this test, try adjusting the cap\n");10621063return false;1064}10651066pr_info("Test took %ld.%.9lds for slot setup + %ld.%.9lds all iterations\n",1067result.slot_runtime.tv_sec, result.slot_runtime.tv_nsec,1068result.guest_runtime.tv_sec, result.guest_runtime.tv_nsec);1069if (!result.nloops) {1070pr_info("No full loops done - too short test time or system too loaded?\n");1071return true;1072}10731074result.iter_runtime = timespec_div(result.guest_runtime,1075result.nloops);1076pr_info("Done %"PRIu64" iterations, avg %ld.%.9lds each\n",1077result.nloops,1078result.iter_runtime.tv_sec,1079result.iter_runtime.tv_nsec);1080result.slottimens = timespec_to_ns(result.slot_runtime);1081result.runtimens = timespec_to_ns(result.iter_runtime);10821083/*1084* Only rank the slot setup time for tests using the whole test memory1085* area so they are comparable1086*/1087if (!data->mem_size &&1088(!rbestslottime->slottimens ||1089result.slottimens < rbestslottime->slottimens))1090*rbestslottime = result;1091if (!rbestruntime->runtimens ||1092result.runtimens < rbestruntime->runtimens)1093*rbestruntime = result;10941095return true;1096}10971098int main(int argc, char *argv[])1099{1100struct test_args targs = {1101.tfirst = 0,1102.tlast = NTESTS - 1,1103.nslots = -1,1104.seconds = 5,1105.runs = 1,1106};1107struct test_result rbestslottime = {};1108int tctr;11091110if (!check_memory_sizes())1111return -1;11121113if (!parse_args(argc, argv, &targs))1114return -1;11151116for (tctr = targs.tfirst; tctr <= targs.tlast; tctr++) {1117const struct test_data *data = &tests[tctr];1118unsigned int runctr;1119struct test_result rbestruntime = {};11201121if (tctr > targs.tfirst)1122pr_info("\n");11231124pr_info("Testing %s performance with %i runs, %d seconds each\n",1125data->name, targs.runs, targs.seconds);11261127for (runctr = 0; runctr < targs.runs; runctr++)1128if (!test_loop(data, &targs,1129&rbestslottime, &rbestruntime))1130break;11311132if (rbestruntime.runtimens)1133pr_info("Best runtime result was %ld.%.9lds per iteration (with %"PRIu64" iterations)\n",1134rbestruntime.iter_runtime.tv_sec,1135rbestruntime.iter_runtime.tv_nsec,1136rbestruntime.nloops);1137}11381139if (rbestslottime.slottimens)1140pr_info("Best slot setup time for the whole test area was %ld.%.9lds\n",1141rbestslottime.slot_runtime.tv_sec,1142rbestslottime.slot_runtime.tv_nsec);11431144return 0;1145}114611471148