arch_timer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. cpu_set_t cpuset;
  87. uint32_t new_pcpu = test_get_pcpu();
  88. CPU_ZERO(&cpuset);
  89. CPU_SET(new_pcpu, &cpuset);
  90. pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu);
  91. ret = pthread_setaffinity_np(pt_vcpu_run[vcpu_idx],
  92. sizeof(cpuset), &cpuset);
  93. /* Allow the error where the vCPU thread is already finished */
  94. TEST_ASSERT(ret == 0 || ret == ESRCH,
  95. "Failed to migrate the vCPU:%u to pCPU: %u; ret: %d",
  96. vcpu_idx, new_pcpu, ret);
  97. return ret;
  98. }
  99. static void *test_vcpu_migration(void *arg)
  100. {
  101. unsigned int i, n_done;
  102. bool vcpu_done;
  103. do {
  104. usleep(msecs_to_usecs(test_args.migration_freq_ms));
  105. for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {
  106. pthread_mutex_lock(&vcpu_done_map_lock);
  107. vcpu_done = test_bit(i, vcpu_done_map);
  108. pthread_mutex_unlock(&vcpu_done_map_lock);
  109. if (vcpu_done) {
  110. n_done++;
  111. continue;
  112. }
  113. test_migrate_vcpu(i);
  114. }
  115. } while (test_args.nr_vcpus != n_done);
  116. return NULL;
  117. }
  118. static void test_run(struct kvm_vm *vm)
  119. {
  120. pthread_t pt_vcpu_migration;
  121. unsigned int i;
  122. int ret;
  123. pthread_mutex_init(&vcpu_done_map_lock, NULL);
  124. vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);
  125. TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap");
  126. for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++) {
  127. ret = pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run,
  128. (void *)(unsigned long)i);
  129. TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread", i);
  130. }
  131. /* Spawn a thread to control the vCPU migrations */
  132. if (test_args.migration_freq_ms) {
  133. srand(time(NULL));
  134. ret = pthread_create(&pt_vcpu_migration, NULL,
  135. test_vcpu_migration, NULL);
  136. TEST_ASSERT(!ret, "Failed to create the migration pthread");
  137. }
  138. for (i = 0; i < test_args.nr_vcpus; i++)
  139. pthread_join(pt_vcpu_run[i], NULL);
  140. if (test_args.migration_freq_ms)
  141. pthread_join(pt_vcpu_migration, NULL);
  142. bitmap_free(vcpu_done_map);
  143. }
  144. static void test_print_help(char *name)
  145. {
  146. pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n"
  147. "\t\t [-m migration_freq_ms] [-o counter_offset]\n"
  148. "\t\t [-e timer_err_margin_us]\n", name);
  149. pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n",
  150. NR_VCPUS_DEF, KVM_MAX_VCPUS);
  151. pr_info("\t-i: Number of iterations per stage (default: %u)\n",
  152. NR_TEST_ITERS_DEF);
  153. pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",
  154. TIMER_TEST_PERIOD_MS_DEF);
  155. pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",
  156. TIMER_TEST_MIGRATION_FREQ_MS);
  157. pr_info("\t-o: Counter offset (in counter cycles, default: 0) [aarch64-only]\n");
  158. pr_info("\t-e: Interrupt arrival error margin (in us) of the guest timer (default: %u)\n",
  159. TIMER_TEST_ERR_MARGIN_US);
  160. pr_info("\t-h: print this help screen\n");
  161. }
  162. static bool parse_args(int argc, char *argv[])
  163. {
  164. int opt;
  165. while ((opt = getopt(argc, argv, "hn:i:p:m:o:e:")) != -1) {
  166. switch (opt) {
  167. case 'n':
  168. test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
  169. if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
  170. pr_info("Max allowed vCPUs: %u\n",
  171. KVM_MAX_VCPUS);
  172. goto err;
  173. }
  174. break;
  175. case 'i':
  176. test_args.nr_iter = atoi_positive("Number of iterations", optarg);
  177. break;
  178. case 'p':
  179. test_args.timer_period_ms = atoi_positive("Periodicity", optarg);
  180. break;
  181. case 'm':
  182. test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);
  183. break;
  184. case 'e':
  185. test_args.timer_err_margin_us = atoi_non_negative("Error Margin", optarg);
  186. break;
  187. case 'o':
  188. test_args.counter_offset = strtol(optarg, NULL, 0);
  189. test_args.reserved = 0;
  190. break;
  191. case 'h':
  192. default:
  193. goto err;
  194. }
  195. }
  196. return true;
  197. err:
  198. test_print_help(argv[0]);
  199. return false;
  200. }
  201. int main(int argc, char *argv[])
  202. {
  203. struct kvm_vm *vm;
  204. if (!parse_args(argc, argv))
  205. exit(KSFT_SKIP);
  206. __TEST_REQUIRE(!test_args.migration_freq_ms || get_nprocs() >= 2,
  207. "At least two physical CPUs needed for vCPU migration");
  208. vm = test_vm_create();
  209. test_run(vm);
  210. test_vm_cleanup(vm);
  211. return 0;
  212. }