Path: blob/master/tools/testing/selftests/kvm/lib/memstress.c
38237 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2020, Google LLC.3*/4#include <inttypes.h>5#include <linux/bitmap.h>67#include "kvm_util.h"8#include "memstress.h"9#include "processor.h"10#include "ucall_common.h"1112struct memstress_args memstress_args;1314/*15* Guest virtual memory offset of the testing memory slot.16* Must not conflict with identity mapped test code.17*/18static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;1920struct vcpu_thread {21/* The index of the vCPU. */22int vcpu_idx;2324/* The pthread backing the vCPU. */25pthread_t thread;2627/* Set to true once the vCPU thread is up and running. */28bool running;29};3031/* The vCPU threads involved in this test. */32static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];3334/* The function run by each vCPU thread, as provided by the test. */35static void (*vcpu_thread_fn)(struct memstress_vcpu_args *);3637/* Set to true once all vCPU threads are up and running. */38static bool all_vcpu_threads_running;3940static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];4142/*43* Continuously write to the first 8 bytes of each page in the44* specified region.45*/46void memstress_guest_code(uint32_t vcpu_idx)47{48struct memstress_args *args = &memstress_args;49struct memstress_vcpu_args *vcpu_args = &args->vcpu_args[vcpu_idx];50struct guest_random_state rand_state;51uint64_t gva;52uint64_t pages;53uint64_t addr;54uint64_t page;55int i;5657rand_state = new_guest_random_state(guest_random_seed + vcpu_idx);5859gva = vcpu_args->gva;60pages = vcpu_args->pages;6162/* Make sure vCPU args data structure is not corrupt. */63GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx);6465while (true) {66for (i = 0; i < sizeof(memstress_args); i += args->guest_page_size)67(void) *((volatile char *)args + i);6869for (i = 0; i < pages; i++) {70if (args->random_access)71page = guest_random_u32(&rand_state) % pages;72else73page = i;7475addr = gva + (page * args->guest_page_size);7677if (__guest_random_bool(&rand_state, args->write_percent))78*(uint64_t *)addr = 0x0123456789ABCDEF;79else80READ_ONCE(*(uint64_t *)addr);81}8283GUEST_SYNC(1);84}85}8687void memstress_setup_vcpus(struct kvm_vm *vm, int nr_vcpus,88struct kvm_vcpu *vcpus[],89uint64_t vcpu_memory_bytes,90bool partition_vcpu_memory_access)91{92struct memstress_args *args = &memstress_args;93struct memstress_vcpu_args *vcpu_args;94int i;9596for (i = 0; i < nr_vcpus; i++) {97vcpu_args = &args->vcpu_args[i];9899vcpu_args->vcpu = vcpus[i];100vcpu_args->vcpu_idx = i;101102if (partition_vcpu_memory_access) {103vcpu_args->gva = guest_test_virt_mem +104(i * vcpu_memory_bytes);105vcpu_args->pages = vcpu_memory_bytes /106args->guest_page_size;107vcpu_args->gpa = args->gpa + (i * vcpu_memory_bytes);108} else {109vcpu_args->gva = guest_test_virt_mem;110vcpu_args->pages = (nr_vcpus * vcpu_memory_bytes) /111args->guest_page_size;112vcpu_args->gpa = args->gpa;113}114115vcpu_args_set(vcpus[i], 1, i);116117pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",118i, vcpu_args->gpa, vcpu_args->gpa +119(vcpu_args->pages * args->guest_page_size));120}121}122123struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,124uint64_t vcpu_memory_bytes, int slots,125enum vm_mem_backing_src_type backing_src,126bool partition_vcpu_memory_access)127{128struct memstress_args *args = &memstress_args;129struct kvm_vm *vm;130uint64_t guest_num_pages, slot0_pages = 0;131uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);132uint64_t region_end_gfn;133int i;134135pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));136137/* By default vCPUs will write to memory. */138args->write_percent = 100;139140/*141* Snapshot the non-huge page size. This is used by the guest code to142* access/dirty pages at the logging granularity.143*/144args->guest_page_size = vm_guest_mode_params[mode].page_size;145146guest_num_pages = vm_adjust_num_guest_pages(mode,147(nr_vcpus * vcpu_memory_bytes) / args->guest_page_size);148149TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,150"Guest memory size is not host page size aligned.");151TEST_ASSERT(vcpu_memory_bytes % args->guest_page_size == 0,152"Guest memory size is not guest page size aligned.");153TEST_ASSERT(guest_num_pages % slots == 0,154"Guest memory cannot be evenly divided into %d slots.",155slots);156157/*158* If using nested, allocate extra pages for the nested page tables and159* in-memory data structures.160*/161if (args->nested)162slot0_pages += memstress_nested_pages(nr_vcpus);163164/*165* Pass guest_num_pages to populate the page tables for test memory.166* The memory is also added to memslot 0, but that's a benign side167* effect as KVM allows aliasing HVAs in meslots.168*/169vm = __vm_create_with_vcpus(VM_SHAPE(mode), nr_vcpus,170slot0_pages + guest_num_pages,171memstress_guest_code, vcpus);172173args->vm = vm;174175/* Put the test region at the top guest physical memory. */176region_end_gfn = vm->max_gfn + 1;177178#ifdef __x86_64__179/*180* When running vCPUs in L2, restrict the test region to 48 bits to181* avoid needing 5-level page tables to identity map L2.182*/183if (args->nested)184region_end_gfn = min(region_end_gfn, (1UL << 48) / args->guest_page_size);185#endif186/*187* If there should be more memory in the guest test region than there188* can be pages in the guest, it will definitely cause problems.189*/190TEST_ASSERT(guest_num_pages < region_end_gfn,191"Requested more guest memory than address space allows.\n"192" guest pages: %" PRIx64 " max gfn: %" PRIx64193" nr_vcpus: %d wss: %" PRIx64 "]",194guest_num_pages, region_end_gfn - 1, nr_vcpus, vcpu_memory_bytes);195196args->gpa = (region_end_gfn - guest_num_pages - 1) * args->guest_page_size;197args->gpa = align_down(args->gpa, backing_src_pagesz);198#ifdef __s390x__199/* Align to 1M (segment size) */200args->gpa = align_down(args->gpa, 1 << 20);201#endif202args->size = guest_num_pages * args->guest_page_size;203pr_info("guest physical test memory: [0x%lx, 0x%lx)\n",204args->gpa, args->gpa + args->size);205206/* Add extra memory slots for testing */207for (i = 0; i < slots; i++) {208uint64_t region_pages = guest_num_pages / slots;209vm_paddr_t region_start = args->gpa + region_pages * args->guest_page_size * i;210211vm_userspace_mem_region_add(vm, backing_src, region_start,212MEMSTRESS_MEM_SLOT_INDEX + i,213region_pages, 0);214}215216/* Do mapping for the demand paging memory slot */217virt_map(vm, guest_test_virt_mem, args->gpa, guest_num_pages);218219memstress_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes,220partition_vcpu_memory_access);221222if (args->nested) {223pr_info("Configuring vCPUs to run in L2 (nested).\n");224memstress_setup_nested(vm, nr_vcpus, vcpus);225}226227/* Export the shared variables to the guest. */228sync_global_to_guest(vm, memstress_args);229230return vm;231}232233void memstress_destroy_vm(struct kvm_vm *vm)234{235kvm_vm_free(vm);236}237238void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent)239{240memstress_args.write_percent = write_percent;241sync_global_to_guest(vm, memstress_args.write_percent);242}243244void memstress_set_random_access(struct kvm_vm *vm, bool random_access)245{246memstress_args.random_access = random_access;247sync_global_to_guest(vm, memstress_args.random_access);248}249250uint64_t __weak memstress_nested_pages(int nr_vcpus)251{252return 0;253}254255void __weak memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus)256{257pr_info("%s() not support on this architecture, skipping.\n", __func__);258exit(KSFT_SKIP);259}260261static void *vcpu_thread_main(void *data)262{263struct vcpu_thread *vcpu = data;264int vcpu_idx = vcpu->vcpu_idx;265266if (memstress_args.pin_vcpus)267pin_self_to_cpu(memstress_args.vcpu_to_pcpu[vcpu_idx]);268269WRITE_ONCE(vcpu->running, true);270271/*272* Wait for all vCPU threads to be up and running before calling the test-273* provided vCPU thread function. This prevents thread creation (which274* requires taking the mmap_sem in write mode) from interfering with the275* guest faulting in its memory.276*/277while (!READ_ONCE(all_vcpu_threads_running))278;279280vcpu_thread_fn(&memstress_args.vcpu_args[vcpu_idx]);281282return NULL;283}284285void memstress_start_vcpu_threads(int nr_vcpus,286void (*vcpu_fn)(struct memstress_vcpu_args *))287{288int i;289290vcpu_thread_fn = vcpu_fn;291WRITE_ONCE(all_vcpu_threads_running, false);292WRITE_ONCE(memstress_args.stop_vcpus, false);293294for (i = 0; i < nr_vcpus; i++) {295struct vcpu_thread *vcpu = &vcpu_threads[i];296297vcpu->vcpu_idx = i;298WRITE_ONCE(vcpu->running, false);299300pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);301}302303for (i = 0; i < nr_vcpus; i++) {304while (!READ_ONCE(vcpu_threads[i].running))305;306}307308WRITE_ONCE(all_vcpu_threads_running, true);309}310311void memstress_join_vcpu_threads(int nr_vcpus)312{313int i;314315WRITE_ONCE(memstress_args.stop_vcpus, true);316317for (i = 0; i < nr_vcpus; i++)318pthread_join(vcpu_threads[i].thread, NULL);319}320321static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable)322{323int i;324325for (i = 0; i < slots; i++) {326int slot = MEMSTRESS_MEM_SLOT_INDEX + i;327int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0;328329vm_mem_region_set_flags(vm, slot, flags);330}331}332333void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots)334{335toggle_dirty_logging(vm, slots, true);336}337338void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots)339{340toggle_dirty_logging(vm, slots, false);341}342343void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots)344{345int i;346347for (i = 0; i < slots; i++) {348int slot = MEMSTRESS_MEM_SLOT_INDEX + i;349350kvm_vm_get_dirty_log(vm, slot, bitmaps[i]);351}352}353354void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],355int slots, uint64_t pages_per_slot)356{357int i;358359for (i = 0; i < slots; i++) {360int slot = MEMSTRESS_MEM_SLOT_INDEX + i;361362kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot);363}364}365366unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot)367{368unsigned long **bitmaps;369int i;370371bitmaps = malloc(slots * sizeof(bitmaps[0]));372TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array.");373374for (i = 0; i < slots; i++) {375bitmaps[i] = bitmap_zalloc(pages_per_slot);376TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap.");377}378379return bitmaps;380}381382void memstress_free_bitmaps(unsigned long *bitmaps[], int slots)383{384int i;385386for (i = 0; i < slots; i++)387free(bitmaps[i]);388389free(bitmaps);390}391392393