19fda6753SDavid Matlack // SPDX-License-Identifier: GPL-2.0 29fda6753SDavid Matlack /* 39fda6753SDavid Matlack * Copyright (C) 2020, Google LLC. 49fda6753SDavid Matlack */ 59fda6753SDavid Matlack #define _GNU_SOURCE 69fda6753SDavid Matlack 79fda6753SDavid Matlack #include <inttypes.h> 89fda6753SDavid Matlack 99fda6753SDavid Matlack #include "kvm_util.h" 109fda6753SDavid Matlack #include "memstress.h" 119fda6753SDavid Matlack #include "processor.h" 129fda6753SDavid Matlack 137812d80cSDavid Matlack struct memstress_args memstress_args; 149fda6753SDavid Matlack 159fda6753SDavid Matlack /* 169fda6753SDavid Matlack * Guest virtual memory offset of the testing memory slot. 179fda6753SDavid Matlack * Must not conflict with identity mapped test code. 189fda6753SDavid Matlack */ 199fda6753SDavid Matlack static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM; 209fda6753SDavid Matlack 219fda6753SDavid Matlack struct vcpu_thread { 229fda6753SDavid Matlack /* The index of the vCPU. */ 239fda6753SDavid Matlack int vcpu_idx; 249fda6753SDavid Matlack 259fda6753SDavid Matlack /* The pthread backing the vCPU. */ 269fda6753SDavid Matlack pthread_t thread; 279fda6753SDavid Matlack 289fda6753SDavid Matlack /* Set to true once the vCPU thread is up and running. */ 299fda6753SDavid Matlack bool running; 309fda6753SDavid Matlack }; 319fda6753SDavid Matlack 329fda6753SDavid Matlack /* The vCPU threads involved in this test. */ 339fda6753SDavid Matlack static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS]; 349fda6753SDavid Matlack 359fda6753SDavid Matlack /* The function run by each vCPU thread, as provided by the test. */ 367812d80cSDavid Matlack static void (*vcpu_thread_fn)(struct memstress_vcpu_args *); 379fda6753SDavid Matlack 389fda6753SDavid Matlack /* Set to true once all vCPU threads are up and running. */ 399fda6753SDavid Matlack static bool all_vcpu_threads_running; 409fda6753SDavid Matlack 419fda6753SDavid Matlack static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; 429fda6753SDavid Matlack 439fda6753SDavid Matlack /* 449fda6753SDavid Matlack * Continuously write to the first 8 bytes of each page in the 459fda6753SDavid Matlack * specified region. 469fda6753SDavid Matlack */ 477812d80cSDavid Matlack void memstress_guest_code(uint32_t vcpu_idx) 489fda6753SDavid Matlack { 497812d80cSDavid Matlack struct memstress_args *args = &memstress_args; 507812d80cSDavid Matlack struct memstress_vcpu_args *vcpu_args = &args->vcpu_args[vcpu_idx]; 519fda6753SDavid Matlack struct guest_random_state rand_state; 529fda6753SDavid Matlack uint64_t gva; 539fda6753SDavid Matlack uint64_t pages; 549fda6753SDavid Matlack uint64_t addr; 559fda6753SDavid Matlack uint64_t page; 569fda6753SDavid Matlack int i; 579fda6753SDavid Matlack 58a008a335SDavid Matlack rand_state = new_guest_random_state(args->random_seed + vcpu_idx); 599fda6753SDavid Matlack 609fda6753SDavid Matlack gva = vcpu_args->gva; 619fda6753SDavid Matlack pages = vcpu_args->pages; 629fda6753SDavid Matlack 639fda6753SDavid Matlack /* Make sure vCPU args data structure is not corrupt. */ 649fda6753SDavid Matlack GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx); 659fda6753SDavid Matlack 669fda6753SDavid Matlack while (true) { 679fda6753SDavid Matlack for (i = 0; i < pages; i++) { 68a008a335SDavid Matlack if (args->random_access) 699fda6753SDavid Matlack page = guest_random_u32(&rand_state) % pages; 709fda6753SDavid Matlack else 719fda6753SDavid Matlack page = i; 729fda6753SDavid Matlack 73a008a335SDavid Matlack addr = gva + (page * args->guest_page_size); 749fda6753SDavid Matlack 75a008a335SDavid Matlack if (guest_random_u32(&rand_state) % 100 < args->write_percent) 769fda6753SDavid Matlack *(uint64_t *)addr = 0x0123456789ABCDEF; 779fda6753SDavid Matlack else 789fda6753SDavid Matlack READ_ONCE(*(uint64_t *)addr); 799fda6753SDavid Matlack } 809fda6753SDavid Matlack 819fda6753SDavid Matlack GUEST_SYNC(1); 829fda6753SDavid Matlack } 839fda6753SDavid Matlack } 849fda6753SDavid Matlack 857812d80cSDavid Matlack void memstress_setup_vcpus(struct kvm_vm *vm, int nr_vcpus, 869fda6753SDavid Matlack struct kvm_vcpu *vcpus[], 879fda6753SDavid Matlack uint64_t vcpu_memory_bytes, 889fda6753SDavid Matlack bool partition_vcpu_memory_access) 899fda6753SDavid Matlack { 907812d80cSDavid Matlack struct memstress_args *args = &memstress_args; 917812d80cSDavid Matlack struct memstress_vcpu_args *vcpu_args; 929fda6753SDavid Matlack int i; 939fda6753SDavid Matlack 949fda6753SDavid Matlack for (i = 0; i < nr_vcpus; i++) { 95a008a335SDavid Matlack vcpu_args = &args->vcpu_args[i]; 969fda6753SDavid Matlack 979fda6753SDavid Matlack vcpu_args->vcpu = vcpus[i]; 989fda6753SDavid Matlack vcpu_args->vcpu_idx = i; 999fda6753SDavid Matlack 1009fda6753SDavid Matlack if (partition_vcpu_memory_access) { 1019fda6753SDavid Matlack vcpu_args->gva = guest_test_virt_mem + 1029fda6753SDavid Matlack (i * vcpu_memory_bytes); 1039fda6753SDavid Matlack vcpu_args->pages = vcpu_memory_bytes / 104a008a335SDavid Matlack args->guest_page_size; 105a008a335SDavid Matlack vcpu_args->gpa = args->gpa + (i * vcpu_memory_bytes); 1069fda6753SDavid Matlack } else { 1079fda6753SDavid Matlack vcpu_args->gva = guest_test_virt_mem; 1089fda6753SDavid Matlack vcpu_args->pages = (nr_vcpus * vcpu_memory_bytes) / 109a008a335SDavid Matlack args->guest_page_size; 110a008a335SDavid Matlack vcpu_args->gpa = args->gpa; 1119fda6753SDavid Matlack } 1129fda6753SDavid Matlack 1139fda6753SDavid Matlack vcpu_args_set(vcpus[i], 1, i); 1149fda6753SDavid Matlack 1159fda6753SDavid Matlack pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n", 1169fda6753SDavid Matlack i, vcpu_args->gpa, vcpu_args->gpa + 117a008a335SDavid Matlack (vcpu_args->pages * args->guest_page_size)); 1189fda6753SDavid Matlack } 1199fda6753SDavid Matlack } 1209fda6753SDavid Matlack 1217812d80cSDavid Matlack struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus, 1229fda6753SDavid Matlack uint64_t vcpu_memory_bytes, int slots, 1239fda6753SDavid Matlack enum vm_mem_backing_src_type backing_src, 1249fda6753SDavid Matlack bool partition_vcpu_memory_access) 1259fda6753SDavid Matlack { 1267812d80cSDavid Matlack struct memstress_args *args = &memstress_args; 1279fda6753SDavid Matlack struct kvm_vm *vm; 1289fda6753SDavid Matlack uint64_t guest_num_pages, slot0_pages = 0; 1299fda6753SDavid Matlack uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src); 1309fda6753SDavid Matlack uint64_t region_end_gfn; 1319fda6753SDavid Matlack int i; 1329fda6753SDavid Matlack 1339fda6753SDavid Matlack pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode)); 1349fda6753SDavid Matlack 1359fda6753SDavid Matlack /* By default vCPUs will write to memory. */ 136a008a335SDavid Matlack args->write_percent = 100; 1379fda6753SDavid Matlack 1389fda6753SDavid Matlack /* 1399fda6753SDavid Matlack * Snapshot the non-huge page size. This is used by the guest code to 1409fda6753SDavid Matlack * access/dirty pages at the logging granularity. 1419fda6753SDavid Matlack */ 142a008a335SDavid Matlack args->guest_page_size = vm_guest_mode_params[mode].page_size; 1439fda6753SDavid Matlack 1449fda6753SDavid Matlack guest_num_pages = vm_adjust_num_guest_pages(mode, 145a008a335SDavid Matlack (nr_vcpus * vcpu_memory_bytes) / args->guest_page_size); 1469fda6753SDavid Matlack 1479fda6753SDavid Matlack TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0, 1489fda6753SDavid Matlack "Guest memory size is not host page size aligned."); 149a008a335SDavid Matlack TEST_ASSERT(vcpu_memory_bytes % args->guest_page_size == 0, 1509fda6753SDavid Matlack "Guest memory size is not guest page size aligned."); 1519fda6753SDavid Matlack TEST_ASSERT(guest_num_pages % slots == 0, 1529fda6753SDavid Matlack "Guest memory cannot be evenly divided into %d slots.", 1539fda6753SDavid Matlack slots); 1549fda6753SDavid Matlack 1559fda6753SDavid Matlack /* 1569fda6753SDavid Matlack * If using nested, allocate extra pages for the nested page tables and 1579fda6753SDavid Matlack * in-memory data structures. 1589fda6753SDavid Matlack */ 159a008a335SDavid Matlack if (args->nested) 1607812d80cSDavid Matlack slot0_pages += memstress_nested_pages(nr_vcpus); 1619fda6753SDavid Matlack 1629fda6753SDavid Matlack /* 1639fda6753SDavid Matlack * Pass guest_num_pages to populate the page tables for test memory. 1649fda6753SDavid Matlack * The memory is also added to memslot 0, but that's a benign side 1659fda6753SDavid Matlack * effect as KVM allows aliasing HVAs in meslots. 1669fda6753SDavid Matlack */ 1679fda6753SDavid Matlack vm = __vm_create_with_vcpus(mode, nr_vcpus, slot0_pages + guest_num_pages, 1687812d80cSDavid Matlack memstress_guest_code, vcpus); 1699fda6753SDavid Matlack 170a008a335SDavid Matlack args->vm = vm; 1719fda6753SDavid Matlack 1729fda6753SDavid Matlack /* Put the test region at the top guest physical memory. */ 1739fda6753SDavid Matlack region_end_gfn = vm->max_gfn + 1; 1749fda6753SDavid Matlack 1759fda6753SDavid Matlack #ifdef __x86_64__ 1769fda6753SDavid Matlack /* 1779fda6753SDavid Matlack * When running vCPUs in L2, restrict the test region to 48 bits to 1789fda6753SDavid Matlack * avoid needing 5-level page tables to identity map L2. 1799fda6753SDavid Matlack */ 180a008a335SDavid Matlack if (args->nested) 181a008a335SDavid Matlack region_end_gfn = min(region_end_gfn, (1UL << 48) / args->guest_page_size); 1829fda6753SDavid Matlack #endif 1839fda6753SDavid Matlack /* 1849fda6753SDavid Matlack * If there should be more memory in the guest test region than there 1859fda6753SDavid Matlack * can be pages in the guest, it will definitely cause problems. 1869fda6753SDavid Matlack */ 1879fda6753SDavid Matlack TEST_ASSERT(guest_num_pages < region_end_gfn, 1889fda6753SDavid Matlack "Requested more guest memory than address space allows.\n" 1899fda6753SDavid Matlack " guest pages: %" PRIx64 " max gfn: %" PRIx64 1909fda6753SDavid Matlack " nr_vcpus: %d wss: %" PRIx64 "]\n", 1919fda6753SDavid Matlack guest_num_pages, region_end_gfn - 1, nr_vcpus, vcpu_memory_bytes); 1929fda6753SDavid Matlack 193a008a335SDavid Matlack args->gpa = (region_end_gfn - guest_num_pages - 1) * args->guest_page_size; 194a008a335SDavid Matlack args->gpa = align_down(args->gpa, backing_src_pagesz); 1959fda6753SDavid Matlack #ifdef __s390x__ 1969fda6753SDavid Matlack /* Align to 1M (segment size) */ 197a008a335SDavid Matlack args->gpa = align_down(args->gpa, 1 << 20); 1989fda6753SDavid Matlack #endif 199a008a335SDavid Matlack args->size = guest_num_pages * args->guest_page_size; 2009fda6753SDavid Matlack pr_info("guest physical test memory: [0x%lx, 0x%lx)\n", 201a008a335SDavid Matlack args->gpa, args->gpa + args->size); 2029fda6753SDavid Matlack 2039fda6753SDavid Matlack /* Add extra memory slots for testing */ 2049fda6753SDavid Matlack for (i = 0; i < slots; i++) { 2059fda6753SDavid Matlack uint64_t region_pages = guest_num_pages / slots; 206a008a335SDavid Matlack vm_paddr_t region_start = args->gpa + region_pages * args->guest_page_size * i; 2079fda6753SDavid Matlack 2089fda6753SDavid Matlack vm_userspace_mem_region_add(vm, backing_src, region_start, 2097812d80cSDavid Matlack MEMSTRESS_MEM_SLOT_INDEX + i, 2109fda6753SDavid Matlack region_pages, 0); 2119fda6753SDavid Matlack } 2129fda6753SDavid Matlack 2139fda6753SDavid Matlack /* Do mapping for the demand paging memory slot */ 214a008a335SDavid Matlack virt_map(vm, guest_test_virt_mem, args->gpa, guest_num_pages); 2159fda6753SDavid Matlack 2167812d80cSDavid Matlack memstress_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes, 2179fda6753SDavid Matlack partition_vcpu_memory_access); 2189fda6753SDavid Matlack 219a008a335SDavid Matlack if (args->nested) { 2209fda6753SDavid Matlack pr_info("Configuring vCPUs to run in L2 (nested).\n"); 2217812d80cSDavid Matlack memstress_setup_nested(vm, nr_vcpus, vcpus); 2229fda6753SDavid Matlack } 2239fda6753SDavid Matlack 2249fda6753SDavid Matlack /* Export the shared variables to the guest. */ 2257812d80cSDavid Matlack sync_global_to_guest(vm, memstress_args); 2269fda6753SDavid Matlack 2279fda6753SDavid Matlack return vm; 2289fda6753SDavid Matlack } 2299fda6753SDavid Matlack 2307812d80cSDavid Matlack void memstress_destroy_vm(struct kvm_vm *vm) 2319fda6753SDavid Matlack { 2329fda6753SDavid Matlack kvm_vm_free(vm); 2339fda6753SDavid Matlack } 2349fda6753SDavid Matlack 2357812d80cSDavid Matlack void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent) 2369fda6753SDavid Matlack { 2377812d80cSDavid Matlack memstress_args.write_percent = write_percent; 2387812d80cSDavid Matlack sync_global_to_guest(vm, memstress_args.write_percent); 2399fda6753SDavid Matlack } 2409fda6753SDavid Matlack 2417812d80cSDavid Matlack void memstress_set_random_seed(struct kvm_vm *vm, uint32_t random_seed) 2429fda6753SDavid Matlack { 2437812d80cSDavid Matlack memstress_args.random_seed = random_seed; 2447812d80cSDavid Matlack sync_global_to_guest(vm, memstress_args.random_seed); 2459fda6753SDavid Matlack } 2469fda6753SDavid Matlack 2477812d80cSDavid Matlack void memstress_set_random_access(struct kvm_vm *vm, bool random_access) 2489fda6753SDavid Matlack { 2497812d80cSDavid Matlack memstress_args.random_access = random_access; 2507812d80cSDavid Matlack sync_global_to_guest(vm, memstress_args.random_access); 2519fda6753SDavid Matlack } 2529fda6753SDavid Matlack 2537812d80cSDavid Matlack uint64_t __weak memstress_nested_pages(int nr_vcpus) 2549fda6753SDavid Matlack { 2559fda6753SDavid Matlack return 0; 2569fda6753SDavid Matlack } 2579fda6753SDavid Matlack 2587812d80cSDavid Matlack void __weak memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus) 2599fda6753SDavid Matlack { 2609fda6753SDavid Matlack pr_info("%s() not support on this architecture, skipping.\n", __func__); 2619fda6753SDavid Matlack exit(KSFT_SKIP); 2629fda6753SDavid Matlack } 2639fda6753SDavid Matlack 2649fda6753SDavid Matlack static void *vcpu_thread_main(void *data) 2659fda6753SDavid Matlack { 2669fda6753SDavid Matlack struct vcpu_thread *vcpu = data; 2679fda6753SDavid Matlack int vcpu_idx = vcpu->vcpu_idx; 2689fda6753SDavid Matlack 2697812d80cSDavid Matlack if (memstress_args.pin_vcpus) 2707812d80cSDavid Matlack kvm_pin_this_task_to_pcpu(memstress_args.vcpu_to_pcpu[vcpu_idx]); 2719fda6753SDavid Matlack 2729fda6753SDavid Matlack WRITE_ONCE(vcpu->running, true); 2739fda6753SDavid Matlack 2749fda6753SDavid Matlack /* 2759fda6753SDavid Matlack * Wait for all vCPU threads to be up and running before calling the test- 2769fda6753SDavid Matlack * provided vCPU thread function. This prevents thread creation (which 2779fda6753SDavid Matlack * requires taking the mmap_sem in write mode) from interfering with the 2789fda6753SDavid Matlack * guest faulting in its memory. 2799fda6753SDavid Matlack */ 2809fda6753SDavid Matlack while (!READ_ONCE(all_vcpu_threads_running)) 2819fda6753SDavid Matlack ; 2829fda6753SDavid Matlack 2837812d80cSDavid Matlack vcpu_thread_fn(&memstress_args.vcpu_args[vcpu_idx]); 2849fda6753SDavid Matlack 2859fda6753SDavid Matlack return NULL; 2869fda6753SDavid Matlack } 2879fda6753SDavid Matlack 2887812d80cSDavid Matlack void memstress_start_vcpu_threads(int nr_vcpus, 2897812d80cSDavid Matlack void (*vcpu_fn)(struct memstress_vcpu_args *)) 2909fda6753SDavid Matlack { 2919fda6753SDavid Matlack int i; 2929fda6753SDavid Matlack 2939fda6753SDavid Matlack vcpu_thread_fn = vcpu_fn; 2949fda6753SDavid Matlack WRITE_ONCE(all_vcpu_threads_running, false); 295*eb561891SPaolo Bonzini WRITE_ONCE(memstress_args.stop_vcpus, false); 2969fda6753SDavid Matlack 2979fda6753SDavid Matlack for (i = 0; i < nr_vcpus; i++) { 2989fda6753SDavid Matlack struct vcpu_thread *vcpu = &vcpu_threads[i]; 2999fda6753SDavid Matlack 3009fda6753SDavid Matlack vcpu->vcpu_idx = i; 3019fda6753SDavid Matlack WRITE_ONCE(vcpu->running, false); 3029fda6753SDavid Matlack 3039fda6753SDavid Matlack pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu); 3049fda6753SDavid Matlack } 3059fda6753SDavid Matlack 3069fda6753SDavid Matlack for (i = 0; i < nr_vcpus; i++) { 3079fda6753SDavid Matlack while (!READ_ONCE(vcpu_threads[i].running)) 3089fda6753SDavid Matlack ; 3099fda6753SDavid Matlack } 3109fda6753SDavid Matlack 3119fda6753SDavid Matlack WRITE_ONCE(all_vcpu_threads_running, true); 3129fda6753SDavid Matlack } 3139fda6753SDavid Matlack 3147812d80cSDavid Matlack void memstress_join_vcpu_threads(int nr_vcpus) 3159fda6753SDavid Matlack { 3169fda6753SDavid Matlack int i; 3179fda6753SDavid Matlack 318*eb561891SPaolo Bonzini WRITE_ONCE(memstress_args.stop_vcpus, true); 319*eb561891SPaolo Bonzini 3209fda6753SDavid Matlack for (i = 0; i < nr_vcpus; i++) 3219fda6753SDavid Matlack pthread_join(vcpu_threads[i].thread, NULL); 3229fda6753SDavid Matlack } 323