Path: blob/master/tools/testing/selftests/kvm/arch_timer.c
38189 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* arch_timer.c - Tests the arch timer IRQ functionality3*4* The guest's main thread configures the timer interrupt and waits5* for it to fire, with a timeout equal to the timer period.6* It asserts that the timeout doesn't exceed the timer period plus7* a user configurable error margin(default to 100us)8*9* On the other hand, upon receipt of an interrupt, the guest's interrupt10* handler validates the interrupt by checking if the architectural state11* is in compliance with the specifications.12*13* The test provides command-line options to configure the timer's14* period (-p), number of vCPUs (-n), iterations per stage (-i) and timer15* interrupt arrival error margin (-e). To stress-test the timer stack16* even more, an option to migrate the vCPUs across pCPUs (-m), at a17* particular rate, is also provided.18*19* Copyright (c) 2021, Google LLC.20*/21#include <stdlib.h>22#include <pthread.h>23#include <linux/sizes.h>24#include <linux/bitmap.h>25#include <sys/sysinfo.h>2627#include "timer_test.h"28#include "ucall_common.h"2930struct test_args test_args = {31.nr_vcpus = NR_VCPUS_DEF,32.nr_iter = NR_TEST_ITERS_DEF,33.timer_period_ms = TIMER_TEST_PERIOD_MS_DEF,34.migration_freq_ms = TIMER_TEST_MIGRATION_FREQ_MS,35.timer_err_margin_us = TIMER_TEST_ERR_MARGIN_US,36.reserved = 1,37};3839struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];40struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS];4142static pthread_t pt_vcpu_run[KVM_MAX_VCPUS];43static unsigned long *vcpu_done_map;44static pthread_mutex_t vcpu_done_map_lock;4546static void *test_vcpu_run(void *arg)47{48unsigned int vcpu_idx = (unsigned long)arg;49struct ucall uc;50struct kvm_vcpu *vcpu = vcpus[vcpu_idx];51struct kvm_vm *vm = vcpu->vm;52struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[vcpu_idx];5354vcpu_run(vcpu);5556/* Currently, any exit from guest is an indication of completion */57pthread_mutex_lock(&vcpu_done_map_lock);58__set_bit(vcpu_idx, vcpu_done_map);59pthread_mutex_unlock(&vcpu_done_map_lock);6061switch (get_ucall(vcpu, &uc)) {62case UCALL_SYNC:63case UCALL_DONE:64break;65case UCALL_ABORT:66sync_global_from_guest(vm, *shared_data);67fprintf(stderr, "Guest assert failed, vcpu %u; stage; %u; iter: %u\n",68vcpu_idx, shared_data->guest_stage, shared_data->nr_iter);69REPORT_GUEST_ASSERT(uc);70break;71default:72TEST_FAIL("Unexpected guest exit");73}7475pr_info("PASS(vCPU-%d).\n", vcpu_idx);7677return NULL;78}7980static uint32_t test_get_pcpu(void)81{82uint32_t pcpu;83unsigned int nproc_conf;84cpu_set_t online_cpuset;8586nproc_conf = get_nprocs_conf();87sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset);8889/* Randomly find an available pCPU to place a vCPU on */90do {91pcpu = rand() % nproc_conf;92} while (!CPU_ISSET(pcpu, &online_cpuset));9394return pcpu;95}9697static int test_migrate_vcpu(unsigned int vcpu_idx)98{99int ret;100uint32_t new_pcpu = test_get_pcpu();101102pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu);103104ret = __pin_task_to_cpu(pt_vcpu_run[vcpu_idx], new_pcpu);105106/* Allow the error where the vCPU thread is already finished */107TEST_ASSERT(ret == 0 || ret == ESRCH,108"Failed to migrate the vCPU:%u to pCPU: %u; ret: %d",109vcpu_idx, new_pcpu, ret);110111return ret;112}113114static void *test_vcpu_migration(void *arg)115{116unsigned int i, n_done;117bool vcpu_done;118119do {120usleep(msecs_to_usecs(test_args.migration_freq_ms));121122for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {123pthread_mutex_lock(&vcpu_done_map_lock);124vcpu_done = test_bit(i, vcpu_done_map);125pthread_mutex_unlock(&vcpu_done_map_lock);126127if (vcpu_done) {128n_done++;129continue;130}131132test_migrate_vcpu(i);133}134} while (test_args.nr_vcpus != n_done);135136return NULL;137}138139static void test_run(struct kvm_vm *vm)140{141pthread_t pt_vcpu_migration;142unsigned int i;143int ret;144145pthread_mutex_init(&vcpu_done_map_lock, NULL);146vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);147TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap");148149for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++) {150ret = pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run,151(void *)(unsigned long)i);152TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread", i);153}154155/* Spawn a thread to control the vCPU migrations */156if (test_args.migration_freq_ms) {157srand(time(NULL));158159ret = pthread_create(&pt_vcpu_migration, NULL,160test_vcpu_migration, NULL);161TEST_ASSERT(!ret, "Failed to create the migration pthread");162}163164165for (i = 0; i < test_args.nr_vcpus; i++)166pthread_join(pt_vcpu_run[i], NULL);167168if (test_args.migration_freq_ms)169pthread_join(pt_vcpu_migration, NULL);170171bitmap_free(vcpu_done_map);172}173174static void test_print_help(char *name)175{176pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n"177"\t\t [-m migration_freq_ms] [-o counter_offset]\n"178"\t\t [-e timer_err_margin_us]\n", name);179pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n",180NR_VCPUS_DEF, KVM_MAX_VCPUS);181pr_info("\t-i: Number of iterations per stage (default: %u)\n",182NR_TEST_ITERS_DEF);183pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",184TIMER_TEST_PERIOD_MS_DEF);185pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",186TIMER_TEST_MIGRATION_FREQ_MS);187pr_info("\t-o: Counter offset (in counter cycles, default: 0) [aarch64-only]\n");188pr_info("\t-e: Interrupt arrival error margin (in us) of the guest timer (default: %u)\n",189TIMER_TEST_ERR_MARGIN_US);190pr_info("\t-h: print this help screen\n");191}192193static bool parse_args(int argc, char *argv[])194{195int opt;196197while ((opt = getopt(argc, argv, "hn:i:p:m:o:e:")) != -1) {198switch (opt) {199case 'n':200test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);201if (test_args.nr_vcpus > KVM_MAX_VCPUS) {202pr_info("Max allowed vCPUs: %u\n",203KVM_MAX_VCPUS);204goto err;205}206break;207case 'i':208test_args.nr_iter = atoi_positive("Number of iterations", optarg);209break;210case 'p':211test_args.timer_period_ms = atoi_positive("Periodicity", optarg);212break;213case 'm':214test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);215break;216case 'e':217test_args.timer_err_margin_us = atoi_non_negative("Error Margin", optarg);218break;219case 'o':220test_args.counter_offset = strtol(optarg, NULL, 0);221test_args.reserved = 0;222break;223case 'h':224default:225goto err;226}227}228229return true;230231err:232test_print_help(argv[0]);233return false;234}235236int main(int argc, char *argv[])237{238struct kvm_vm *vm;239240if (!parse_args(argc, argv))241exit(KSFT_SKIP);242243__TEST_REQUIRE(!test_args.migration_freq_ms || get_nprocs() >= 2,244"At least two physical CPUs needed for vCPU migration");245246vm = test_vm_create();247test_run(vm);248test_vm_cleanup(vm);249250return 0;251}252253254