hardware_disable_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This test is intended to reproduce a crash that happens when
  4. * kvm_arch_hardware_disable is called and it attempts to unregister the user
  5. * return notifiers.
  6. */
  7. #include <fcntl.h>
  8. #include <pthread.h>
  9. #include <semaphore.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/wait.h>
  14. #include <test_util.h>
  15. #include "kvm_util.h"
  16. #define VCPU_NUM 4
  17. #define SLEEPING_THREAD_NUM (1 << 4)
  18. #define FORK_NUM (1ULL << 9)
  19. #define DELAY_US_MAX 2000
  20. sem_t *sem;
  21. static void guest_code(void)
  22. {
  23. for (;;)
  24. ; /* Some busy work */
  25. printf("Should not be reached.\n");
  26. }
  27. static void *run_vcpu(void *arg)
  28. {
  29. struct kvm_vcpu *vcpu = arg;
  30. struct kvm_run *run = vcpu->run;
  31. vcpu_run(vcpu);
  32. TEST_ASSERT(false, "%s: exited with reason %d: %s",
  33. __func__, run->exit_reason,
  34. exit_reason_str(run->exit_reason));
  35. pthread_exit(NULL);
  36. }
  37. static void *sleeping_thread(void *arg)
  38. {
  39. int fd;
  40. while (true) {
  41. fd = open("/dev/null", O_RDWR);
  42. close(fd);
  43. }
  44. TEST_ASSERT(false, "%s: exited", __func__);
  45. pthread_exit(NULL);
  46. }
  47. static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
  48. void *(*f)(void *), void *arg)
  49. {
  50. int r;
  51. r = pthread_create(thread, attr, f, arg);
  52. TEST_ASSERT(r == 0, "%s: failed to create thread", __func__);
  53. }
  54. static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set)
  55. {
  56. int r;
  57. r = pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpu_set);
  58. TEST_ASSERT(r == 0, "%s: failed set affinity", __func__);
  59. }
  60. static inline void check_join(pthread_t thread, void **retval)
  61. {
  62. int r;
  63. r = pthread_join(thread, retval);
  64. TEST_ASSERT(r == 0, "%s: failed to join thread", __func__);
  65. }
  66. static void run_test(uint32_t run)
  67. {
  68. struct kvm_vcpu *vcpu;
  69. struct kvm_vm *vm;
  70. cpu_set_t cpu_set;
  71. pthread_t threads[VCPU_NUM];
  72. pthread_t throw_away;
  73. void *b;
  74. uint32_t i, j;
  75. CPU_ZERO(&cpu_set);
  76. for (i = 0; i < VCPU_NUM; i++)
  77. CPU_SET(i, &cpu_set);
  78. vm = vm_create(VCPU_NUM);
  79. pr_debug("%s: [%d] start vcpus\n", __func__, run);
  80. for (i = 0; i < VCPU_NUM; ++i) {
  81. vcpu = vm_vcpu_add(vm, i, guest_code);
  82. check_create_thread(&threads[i], NULL, run_vcpu, vcpu);
  83. check_set_affinity(threads[i], &cpu_set);
  84. for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
  85. check_create_thread(&throw_away, NULL, sleeping_thread,
  86. (void *)NULL);
  87. check_set_affinity(throw_away, &cpu_set);
  88. }
  89. }
  90. pr_debug("%s: [%d] all threads launched\n", __func__, run);
  91. sem_post(sem);
  92. for (i = 0; i < VCPU_NUM; ++i)
  93. check_join(threads[i], &b);
  94. /* Should not be reached */
  95. TEST_ASSERT(false, "%s: [%d] child escaped the ninja", __func__, run);
  96. }
  97. void wait_for_child_setup(pid_t pid)
  98. {
  99. /*
  100. * Wait for the child to post to the semaphore, but wake up periodically
  101. * to check if the child exited prematurely.
  102. */
  103. for (;;) {
  104. const struct timespec wait_period = { .tv_sec = 1 };
  105. int status;
  106. if (!sem_timedwait(sem, &wait_period))
  107. return;
  108. /* Child is still running, keep waiting. */
  109. if (pid != waitpid(pid, &status, WNOHANG))
  110. continue;
  111. /*
  112. * Child is no longer running, which is not expected.
  113. *
  114. * If it exited with a non-zero status, we explicitly forward
  115. * the child's status in case it exited with KSFT_SKIP.
  116. */
  117. if (WIFEXITED(status))
  118. exit(WEXITSTATUS(status));
  119. else
  120. TEST_ASSERT(false, "Child exited unexpectedly");
  121. }
  122. }
  123. int main(int argc, char **argv)
  124. {
  125. uint32_t i;
  126. int s, r;
  127. pid_t pid;
  128. sem = sem_open("vm_sem", O_CREAT | O_EXCL, 0644, 0);
  129. sem_unlink("vm_sem");
  130. for (i = 0; i < FORK_NUM; ++i) {
  131. pid = fork();
  132. TEST_ASSERT(pid >= 0, "%s: unable to fork", __func__);
  133. if (pid == 0)
  134. run_test(i); /* This function always exits */
  135. pr_debug("%s: [%d] waiting semaphore\n", __func__, i);
  136. wait_for_child_setup(pid);
  137. r = (rand() % DELAY_US_MAX) + 1;
  138. pr_debug("%s: [%d] waiting %dus\n", __func__, i, r);
  139. usleep(r);
  140. r = waitpid(pid, &s, WNOHANG);
  141. TEST_ASSERT(r != pid,
  142. "%s: [%d] child exited unexpectedly status: [%d]",
  143. __func__, i, s);
  144. pr_debug("%s: [%d] killing child\n", __func__, i);
  145. kill(pid, SIGKILL);
  146. }
  147. sem_destroy(sem);
  148. exit(0);
  149. }