step_after_suspend_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2016 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #define _GNU_SOURCE
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <sched.h>
  18. #include <signal.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/ptrace.h>
  24. #include <sys/stat.h>
  25. #include <sys/timerfd.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include "../kselftest.h"
  29. void child(int cpu)
  30. {
  31. cpu_set_t set;
  32. CPU_ZERO(&set);
  33. CPU_SET(cpu, &set);
  34. if (sched_setaffinity(0, sizeof(set), &set) != 0) {
  35. ksft_print_msg("sched_setaffinity() failed: %s\n",
  36. strerror(errno));
  37. _exit(1);
  38. }
  39. if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
  40. ksft_print_msg("ptrace(PTRACE_TRACEME) failed: %s\n",
  41. strerror(errno));
  42. _exit(1);
  43. }
  44. if (raise(SIGSTOP) != 0) {
  45. ksft_print_msg("raise(SIGSTOP) failed: %s\n", strerror(errno));
  46. _exit(1);
  47. }
  48. _exit(0);
  49. }
  50. bool run_test(int cpu)
  51. {
  52. int status;
  53. pid_t pid = fork();
  54. pid_t wpid;
  55. if (pid < 0) {
  56. ksft_print_msg("fork() failed: %s\n", strerror(errno));
  57. return false;
  58. }
  59. if (pid == 0)
  60. child(cpu);
  61. wpid = waitpid(pid, &status, __WALL);
  62. if (wpid != pid) {
  63. ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
  64. return false;
  65. }
  66. if (!WIFSTOPPED(status)) {
  67. ksft_print_msg("child did not stop: %s\n", strerror(errno));
  68. return false;
  69. }
  70. if (WSTOPSIG(status) != SIGSTOP) {
  71. ksft_print_msg("child did not stop with SIGSTOP: %s\n",
  72. strerror(errno));
  73. return false;
  74. }
  75. if (ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0) {
  76. if (errno == EIO) {
  77. ksft_exit_skip(
  78. "ptrace(PTRACE_SINGLESTEP) not supported on this architecture: %s\n",
  79. strerror(errno));
  80. }
  81. ksft_print_msg("ptrace(PTRACE_SINGLESTEP) failed: %s\n",
  82. strerror(errno));
  83. return false;
  84. }
  85. wpid = waitpid(pid, &status, __WALL);
  86. if (wpid != pid) {
  87. ksft_print_msg("waitpid() failed: $s\n", strerror(errno));
  88. return false;
  89. }
  90. if (WIFEXITED(status)) {
  91. ksft_print_msg("child did not single-step: %s\n",
  92. strerror(errno));
  93. return false;
  94. }
  95. if (!WIFSTOPPED(status)) {
  96. ksft_print_msg("child did not stop: %s\n", strerror(errno));
  97. return false;
  98. }
  99. if (WSTOPSIG(status) != SIGTRAP) {
  100. ksft_print_msg("child did not stop with SIGTRAP: %s\n",
  101. strerror(errno));
  102. return false;
  103. }
  104. if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) {
  105. ksft_print_msg("ptrace(PTRACE_CONT) failed: %s\n",
  106. strerror(errno));
  107. return false;
  108. }
  109. wpid = waitpid(pid, &status, __WALL);
  110. if (wpid != pid) {
  111. ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
  112. return false;
  113. }
  114. if (!WIFEXITED(status)) {
  115. ksft_print_msg("child did not exit after PTRACE_CONT: %s\n",
  116. strerror(errno));
  117. return false;
  118. }
  119. return true;
  120. }
  121. void suspend(void)
  122. {
  123. int power_state_fd;
  124. struct sigevent event = {};
  125. int timerfd;
  126. int err;
  127. struct itimerspec spec = {};
  128. if (getuid() != 0)
  129. ksft_exit_skip("Please run the test as root - Exiting.\n");
  130. power_state_fd = open("/sys/power/state", O_RDWR);
  131. if (power_state_fd < 0)
  132. ksft_exit_fail_msg(
  133. "open(\"/sys/power/state\") failed %s)\n",
  134. strerror(errno));
  135. timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
  136. if (timerfd < 0)
  137. ksft_exit_fail_msg("timerfd_create() failed\n");
  138. spec.it_value.tv_sec = 5;
  139. err = timerfd_settime(timerfd, 0, &spec, NULL);
  140. if (err < 0)
  141. ksft_exit_fail_msg("timerfd_settime() failed\n");
  142. if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem"))
  143. ksft_exit_fail_msg("Failed to enter Suspend state\n");
  144. close(timerfd);
  145. close(power_state_fd);
  146. }
  147. int main(int argc, char **argv)
  148. {
  149. int opt;
  150. bool do_suspend = true;
  151. bool succeeded = true;
  152. cpu_set_t available_cpus;
  153. int err;
  154. int cpu;
  155. ksft_print_header();
  156. while ((opt = getopt(argc, argv, "n")) != -1) {
  157. switch (opt) {
  158. case 'n':
  159. do_suspend = false;
  160. break;
  161. default:
  162. printf("Usage: %s [-n]\n", argv[0]);
  163. printf(" -n: do not trigger a suspend/resume cycle before the test\n");
  164. return -1;
  165. }
  166. }
  167. if (do_suspend)
  168. suspend();
  169. err = sched_getaffinity(0, sizeof(available_cpus), &available_cpus);
  170. if (err < 0)
  171. ksft_exit_fail_msg("sched_getaffinity() failed\n");
  172. for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
  173. bool test_success;
  174. if (!CPU_ISSET(cpu, &available_cpus))
  175. continue;
  176. test_success = run_test(cpu);
  177. if (test_success) {
  178. ksft_test_result_pass("CPU %d\n", cpu);
  179. } else {
  180. ksft_test_result_fail("CPU %d\n", cpu);
  181. succeeded = false;
  182. }
  183. }
  184. if (succeeded)
  185. ksft_exit_pass();
  186. else
  187. ksft_exit_fail();
  188. }