Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2020, Google LLC.
0004  */
0005 #include <inttypes.h>
0006 
0007 #include "kvm_util.h"
0008 #include "perf_test_util.h"
0009 #include "processor.h"
0010 
0011 struct perf_test_args perf_test_args;
0012 
0013 /*
0014  * Guest virtual memory offset of the testing memory slot.
0015  * Must not conflict with identity mapped test code.
0016  */
0017 static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
0018 
0019 struct vcpu_thread {
0020     /* The index of the vCPU. */
0021     int vcpu_idx;
0022 
0023     /* The pthread backing the vCPU. */
0024     pthread_t thread;
0025 
0026     /* Set to true once the vCPU thread is up and running. */
0027     bool running;
0028 };
0029 
0030 /* The vCPU threads involved in this test. */
0031 static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
0032 
0033 /* The function run by each vCPU thread, as provided by the test. */
0034 static void (*vcpu_thread_fn)(struct perf_test_vcpu_args *);
0035 
0036 /* Set to true once all vCPU threads are up and running. */
0037 static bool all_vcpu_threads_running;
0038 
0039 static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
0040 
0041 /*
0042  * Continuously write to the first 8 bytes of each page in the
0043  * specified region.
0044  */
0045 void perf_test_guest_code(uint32_t vcpu_idx)
0046 {
0047     struct perf_test_args *pta = &perf_test_args;
0048     struct perf_test_vcpu_args *vcpu_args = &pta->vcpu_args[vcpu_idx];
0049     uint64_t gva;
0050     uint64_t pages;
0051     int i;
0052 
0053     gva = vcpu_args->gva;
0054     pages = vcpu_args->pages;
0055 
0056     /* Make sure vCPU args data structure is not corrupt. */
0057     GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx);
0058 
0059     while (true) {
0060         for (i = 0; i < pages; i++) {
0061             uint64_t addr = gva + (i * pta->guest_page_size);
0062 
0063             if (i % pta->wr_fract == 0)
0064                 *(uint64_t *)addr = 0x0123456789ABCDEF;
0065             else
0066                 READ_ONCE(*(uint64_t *)addr);
0067         }
0068 
0069         GUEST_SYNC(1);
0070     }
0071 }
0072 
0073 void perf_test_setup_vcpus(struct kvm_vm *vm, int nr_vcpus,
0074                struct kvm_vcpu *vcpus[],
0075                uint64_t vcpu_memory_bytes,
0076                bool partition_vcpu_memory_access)
0077 {
0078     struct perf_test_args *pta = &perf_test_args;
0079     struct perf_test_vcpu_args *vcpu_args;
0080     int i;
0081 
0082     for (i = 0; i < nr_vcpus; i++) {
0083         vcpu_args = &pta->vcpu_args[i];
0084 
0085         vcpu_args->vcpu = vcpus[i];
0086         vcpu_args->vcpu_idx = i;
0087 
0088         if (partition_vcpu_memory_access) {
0089             vcpu_args->gva = guest_test_virt_mem +
0090                      (i * vcpu_memory_bytes);
0091             vcpu_args->pages = vcpu_memory_bytes /
0092                        pta->guest_page_size;
0093             vcpu_args->gpa = pta->gpa + (i * vcpu_memory_bytes);
0094         } else {
0095             vcpu_args->gva = guest_test_virt_mem;
0096             vcpu_args->pages = (nr_vcpus * vcpu_memory_bytes) /
0097                        pta->guest_page_size;
0098             vcpu_args->gpa = pta->gpa;
0099         }
0100 
0101         vcpu_args_set(vcpus[i], 1, i);
0102 
0103         pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
0104              i, vcpu_args->gpa, vcpu_args->gpa +
0105              (vcpu_args->pages * pta->guest_page_size));
0106     }
0107 }
0108 
0109 struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus,
0110                    uint64_t vcpu_memory_bytes, int slots,
0111                    enum vm_mem_backing_src_type backing_src,
0112                    bool partition_vcpu_memory_access)
0113 {
0114     struct perf_test_args *pta = &perf_test_args;
0115     struct kvm_vm *vm;
0116     uint64_t guest_num_pages, slot0_pages = 0;
0117     uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
0118     uint64_t region_end_gfn;
0119     int i;
0120 
0121     pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
0122 
0123     /* By default vCPUs will write to memory. */
0124     pta->wr_fract = 1;
0125 
0126     /*
0127      * Snapshot the non-huge page size.  This is used by the guest code to
0128      * access/dirty pages at the logging granularity.
0129      */
0130     pta->guest_page_size = vm_guest_mode_params[mode].page_size;
0131 
0132     guest_num_pages = vm_adjust_num_guest_pages(mode,
0133                 (nr_vcpus * vcpu_memory_bytes) / pta->guest_page_size);
0134 
0135     TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,
0136             "Guest memory size is not host page size aligned.");
0137     TEST_ASSERT(vcpu_memory_bytes % pta->guest_page_size == 0,
0138             "Guest memory size is not guest page size aligned.");
0139     TEST_ASSERT(guest_num_pages % slots == 0,
0140             "Guest memory cannot be evenly divided into %d slots.",
0141             slots);
0142 
0143     /*
0144      * If using nested, allocate extra pages for the nested page tables and
0145      * in-memory data structures.
0146      */
0147     if (pta->nested)
0148         slot0_pages += perf_test_nested_pages(nr_vcpus);
0149 
0150     /*
0151      * Pass guest_num_pages to populate the page tables for test memory.
0152      * The memory is also added to memslot 0, but that's a benign side
0153      * effect as KVM allows aliasing HVAs in meslots.
0154      */
0155     vm = __vm_create_with_vcpus(mode, nr_vcpus, slot0_pages + guest_num_pages,
0156                     perf_test_guest_code, vcpus);
0157 
0158     pta->vm = vm;
0159 
0160     /* Put the test region at the top guest physical memory. */
0161     region_end_gfn = vm->max_gfn + 1;
0162 
0163 #ifdef __x86_64__
0164     /*
0165      * When running vCPUs in L2, restrict the test region to 48 bits to
0166      * avoid needing 5-level page tables to identity map L2.
0167      */
0168     if (pta->nested)
0169         region_end_gfn = min(region_end_gfn, (1UL << 48) / pta->guest_page_size);
0170 #endif
0171     /*
0172      * If there should be more memory in the guest test region than there
0173      * can be pages in the guest, it will definitely cause problems.
0174      */
0175     TEST_ASSERT(guest_num_pages < region_end_gfn,
0176             "Requested more guest memory than address space allows.\n"
0177             "    guest pages: %" PRIx64 " max gfn: %" PRIx64
0178             " nr_vcpus: %d wss: %" PRIx64 "]\n",
0179             guest_num_pages, region_end_gfn - 1, nr_vcpus, vcpu_memory_bytes);
0180 
0181     pta->gpa = (region_end_gfn - guest_num_pages - 1) * pta->guest_page_size;
0182     pta->gpa = align_down(pta->gpa, backing_src_pagesz);
0183 #ifdef __s390x__
0184     /* Align to 1M (segment size) */
0185     pta->gpa = align_down(pta->gpa, 1 << 20);
0186 #endif
0187     pta->size = guest_num_pages * pta->guest_page_size;
0188     pr_info("guest physical test memory: [0x%lx, 0x%lx)\n",
0189         pta->gpa, pta->gpa + pta->size);
0190 
0191     /* Add extra memory slots for testing */
0192     for (i = 0; i < slots; i++) {
0193         uint64_t region_pages = guest_num_pages / slots;
0194         vm_paddr_t region_start = pta->gpa + region_pages * pta->guest_page_size * i;
0195 
0196         vm_userspace_mem_region_add(vm, backing_src, region_start,
0197                         PERF_TEST_MEM_SLOT_INDEX + i,
0198                         region_pages, 0);
0199     }
0200 
0201     /* Do mapping for the demand paging memory slot */
0202     virt_map(vm, guest_test_virt_mem, pta->gpa, guest_num_pages);
0203 
0204     perf_test_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes,
0205                   partition_vcpu_memory_access);
0206 
0207     if (pta->nested) {
0208         pr_info("Configuring vCPUs to run in L2 (nested).\n");
0209         perf_test_setup_nested(vm, nr_vcpus, vcpus);
0210     }
0211 
0212     ucall_init(vm, NULL);
0213 
0214     /* Export the shared variables to the guest. */
0215     sync_global_to_guest(vm, perf_test_args);
0216 
0217     return vm;
0218 }
0219 
0220 void perf_test_destroy_vm(struct kvm_vm *vm)
0221 {
0222     ucall_uninit(vm);
0223     kvm_vm_free(vm);
0224 }
0225 
0226 void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract)
0227 {
0228     perf_test_args.wr_fract = wr_fract;
0229     sync_global_to_guest(vm, perf_test_args);
0230 }
0231 
0232 uint64_t __weak perf_test_nested_pages(int nr_vcpus)
0233 {
0234     return 0;
0235 }
0236 
0237 void __weak perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus)
0238 {
0239     pr_info("%s() not support on this architecture, skipping.\n", __func__);
0240     exit(KSFT_SKIP);
0241 }
0242 
0243 static void *vcpu_thread_main(void *data)
0244 {
0245     struct vcpu_thread *vcpu = data;
0246 
0247     WRITE_ONCE(vcpu->running, true);
0248 
0249     /*
0250      * Wait for all vCPU threads to be up and running before calling the test-
0251      * provided vCPU thread function. This prevents thread creation (which
0252      * requires taking the mmap_sem in write mode) from interfering with the
0253      * guest faulting in its memory.
0254      */
0255     while (!READ_ONCE(all_vcpu_threads_running))
0256         ;
0257 
0258     vcpu_thread_fn(&perf_test_args.vcpu_args[vcpu->vcpu_idx]);
0259 
0260     return NULL;
0261 }
0262 
0263 void perf_test_start_vcpu_threads(int nr_vcpus,
0264                   void (*vcpu_fn)(struct perf_test_vcpu_args *))
0265 {
0266     int i;
0267 
0268     vcpu_thread_fn = vcpu_fn;
0269     WRITE_ONCE(all_vcpu_threads_running, false);
0270 
0271     for (i = 0; i < nr_vcpus; i++) {
0272         struct vcpu_thread *vcpu = &vcpu_threads[i];
0273 
0274         vcpu->vcpu_idx = i;
0275         WRITE_ONCE(vcpu->running, false);
0276 
0277         pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);
0278     }
0279 
0280     for (i = 0; i < nr_vcpus; i++) {
0281         while (!READ_ONCE(vcpu_threads[i].running))
0282             ;
0283     }
0284 
0285     WRITE_ONCE(all_vcpu_threads_running, true);
0286 }
0287 
0288 void perf_test_join_vcpu_threads(int nr_vcpus)
0289 {
0290     int i;
0291 
0292     for (i = 0; i < nr_vcpus; i++)
0293         pthread_join(vcpu_threads[i].thread, NULL);
0294 }