arch_timer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch_timer.c - Tests the arch timer IRQ functionality
  4. *
  5. * The guest's main thread configures the timer interrupt and waits
  6. * for it to fire, with a timeout equal to the timer period.
  7. * It asserts that the timeout doesn't exceed the timer period plus
  8. * a user configurable error margin(default to 100us)
  9. *
  10. * On the other hand, upon receipt of an interrupt, the guest's interrupt
  11. * handler validates the interrupt by checking if the architectural state
  12. * is in compliance with the specifications.
  13. *
  14. * The test provides command-line options to configure the timer's
  15. * period (-p), number of vCPUs (-n), iterations per stage (-i) and timer
  16. * interrupt arrival error margin (-e). To stress-test the timer stack
  17. * even more, an option to migrate the vCPUs across pCPUs (-m), at a
  18. * particular rate, is also provided.
  19. *
  20. * Copyright (c) 2021, Google LLC.
  21. */
  22. #include <stdlib.h>
  23. #include <pthread.h>
  24. #include <linux/sizes.h>
  25. #include <linux/bitmap.h>
  26. #include <sys/sysinfo.h>
  27. #include "timer_test.h"
  28. #include "ucall_common.h"
  29. struct test_args test_args = {
  30. .nr_vcpus = NR_VCPUS_DEF,
  31. .nr_iter = NR_TEST_ITERS_DEF,
  32. .timer_period_ms = TIMER_TEST_PERIOD_MS_DEF,
  33. .migration_freq_ms = TIMER_TEST_MIGRATION_FREQ_MS,
  34. .timer_err_margin_us = TIMER_TEST_ERR_MARGIN_US,
  35. .reserved = 1,
  36. };
  37. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  38. struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS];
  39. static pthread_t pt_vcpu_run[KVM_MAX_VCPUS];
  40. static unsigned long *vcpu_done_map;
  41. static pthread_mutex_t vcpu_done_map_lock;
  42. static void *test_vcpu_run(void *arg)
  43. {
  44. unsigned int vcpu_idx = (unsigned long)arg;
  45. struct ucall uc;
  46. struct kvm_vcpu *vcpu = vcpus[vcpu_idx];
  47. struct kvm_vm *vm = vcpu->vm;
  48. struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[vcpu_idx];
  49. vcpu_run(vcpu);
  50. /* Currently, any exit from guest is an indication of completion */
  51. pthread_mutex_lock(&vcpu_done_map_lock);
  52. __set_bit(vcpu_idx, vcpu_done_map);
  53. pthread_mutex_unlock(&vcpu_done_map_lock);
  54. switch (get_ucall(vcpu, &uc)) {
  55. case UCALL_SYNC:
  56. case UCALL_DONE:
  57. break;
  58. case UCALL_ABORT:
  59. sync_global_from_guest(vm, *shared_data);
  60. fprintf(stderr, "Guest assert failed, vcpu %u; stage; %u; iter: %u\n",
  61. vcpu_idx, shared_data->guest_stage, shared_data->nr_iter);
  62. REPORT_GUEST_ASSERT(uc);
  63. break;
  64. default:
  65. TEST_FAIL("Unexpected guest exit");
  66. }
  67. pr_info("PASS(vCPU-%d).\n", vcpu_idx);
  68. return NULL;
  69. }
  70. static uint32_t test_get_pcpu(void)
  71. {
  72. uint32_t pcpu;
  73. unsigned int nproc_conf;
  74. cpu_set_t online_cpuset;
  75. nproc_conf = get_nprocs_conf();
  76. sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset);
  77. /* Randomly find an available pCPU to place a vCPU on */
  78. do {
  79. pcpu = rand() % nproc_conf;
  80. } while (!CPU_ISSET(pcpu, &online_cpuset));
  81. return pcpu;
  82. }
  83. static int test_migrate_vcpu(unsigned int vcpu_idx)
  84. {
  85. int ret;
  86. uint32_t new_pcpu = test_get_pcpu();
  87. pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu);
  88. ret = __pin_task_to_cpu(pt_vcpu_run[vcpu_idx], new_pcpu);
  89. /* Allow the error where the vCPU thread is already finished */
  90. TEST_ASSERT(ret == 0 || ret == ESRCH,
  91. "Failed to migrate the vCPU:%u to pCPU: %u; ret: %d",
  92. vcpu_idx, new_pcpu, ret);
  93. return ret;
  94. }
  95. static void *test_vcpu_migration(void *arg)
  96. {
  97. unsigned int i, n_done;
  98. bool vcpu_done;
  99. do {
  100. usleep(msecs_to_usecs(test_args.migration_freq_ms));
  101. for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {
  102. pthread_mutex_lock(&vcpu_done_map_lock);
  103. vcpu_done = test_bit(i, vcpu_done_map);
  104. pthread_mutex_unlock(&vcpu_done_map_lock);
  105. if (vcpu_done) {
  106. n_done++;
  107. continue;
  108. }
  109. test_migrate_vcpu(i);
  110. }
  111. } while (test_args.nr_vcpus != n_done);
  112. return NULL;
  113. }
  114. static void test_run(struct kvm_vm *vm)
  115. {
  116. pthread_t pt_vcpu_migration;
  117. unsigned int i;
  118. int ret;
  119. pthread_mutex_init(&vcpu_done_map_lock, NULL);
  120. vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);
  121. TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap");
  122. for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++) {
  123. ret = pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run,
  124. (void *)(unsigned long)i);
  125. TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread", i);
  126. }
  127. /* Spawn a thread to control the vCPU migrations */
  128. if (test_args.migration_freq_ms) {
  129. srand(time(NULL));
  130. ret = pthread_create(&pt_vcpu_migration, NULL,
  131. test_vcpu_migration, NULL);
  132. TEST_ASSERT(!ret, "Failed to create the migration pthread");
  133. }
  134. for (i = 0; i < test_args.nr_vcpus; i++)
  135. pthread_join(pt_vcpu_run[i], NULL);
  136. if (test_args.migration_freq_ms)
  137. pthread_join(pt_vcpu_migration, NULL);
  138. bitmap_free(vcpu_done_map);
  139. }
  140. static void test_print_help(char *name)
  141. {
  142. pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n"
  143. "\t\t [-m migration_freq_ms] [-o counter_offset]\n"
  144. "\t\t [-e timer_err_margin_us]\n", name);
  145. pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n",
  146. NR_VCPUS_DEF, KVM_MAX_VCPUS);
  147. pr_info("\t-i: Number of iterations per stage (default: %u)\n",
  148. NR_TEST_ITERS_DEF);
  149. pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",
  150. TIMER_TEST_PERIOD_MS_DEF);
  151. pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",
  152. TIMER_TEST_MIGRATION_FREQ_MS);
  153. pr_info("\t-o: Counter offset (in counter cycles, default: 0) [aarch64-only]\n");
  154. pr_info("\t-e: Interrupt arrival error margin (in us) of the guest timer (default: %u)\n",
  155. TIMER_TEST_ERR_MARGIN_US);
  156. pr_info("\t-h: print this help screen\n");
  157. }
  158. static bool parse_args(int argc, char *argv[])
  159. {
  160. int opt;
  161. while ((opt = getopt(argc, argv, "hn:i:p:m:o:e:")) != -1) {
  162. switch (opt) {
  163. case 'n':
  164. test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
  165. if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
  166. pr_info("Max allowed vCPUs: %u\n",
  167. KVM_MAX_VCPUS);
  168. goto err;
  169. }
  170. break;
  171. case 'i':
  172. test_args.nr_iter = atoi_positive("Number of iterations", optarg);
  173. break;
  174. case 'p':
  175. test_args.timer_period_ms = atoi_positive("Periodicity", optarg);
  176. break;
  177. case 'm':
  178. test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);
  179. break;
  180. case 'e':
  181. test_args.timer_err_margin_us = atoi_non_negative("Error Margin", optarg);
  182. break;
  183. case 'o':
  184. test_args.counter_offset = strtol(optarg, NULL, 0);
  185. test_args.reserved = 0;
  186. break;
  187. case 'h':
  188. default:
  189. goto err;
  190. }
  191. }
  192. return true;
  193. err:
  194. test_print_help(argv[0]);
  195. return false;
  196. }
  197. int main(int argc, char *argv[])
  198. {
  199. struct kvm_vm *vm;
  200. if (!parse_args(argc, argv))
  201. exit(KSFT_SKIP);
  202. __TEST_REQUIRE(!test_args.migration_freq_ms || get_nprocs() >= 2,
  203. "At least two physical CPUs needed for vCPU migration");
  204. vm = test_vm_create();
  205. test_run(vm);
  206. test_vm_cleanup(vm);
  207. return 0;
  208. }