step_after_suspend_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. perror("sched_setaffinity() failed");
  36. _exit(1);
  37. }
  38. if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
  39. perror("ptrace(PTRACE_TRACEME) failed");
  40. _exit(1);
  41. }
  42. if (raise(SIGSTOP) != 0) {
  43. perror("raise(SIGSTOP) failed");
  44. _exit(1);
  45. }
  46. _exit(0);
  47. }
  48. bool run_test(int cpu)
  49. {
  50. int status;
  51. pid_t pid = fork();
  52. pid_t wpid;
  53. if (pid < 0) {
  54. perror("fork() failed");
  55. return false;
  56. }
  57. if (pid == 0)
  58. child(cpu);
  59. wpid = waitpid(pid, &status, __WALL);
  60. if (wpid != pid) {
  61. perror("waitpid() failed");
  62. return false;
  63. }
  64. if (!WIFSTOPPED(status)) {
  65. printf("child did not stop\n");
  66. return false;
  67. }
  68. if (WSTOPSIG(status) != SIGSTOP) {
  69. printf("child did not stop with SIGSTOP\n");
  70. return false;
  71. }
  72. if (ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0) {
  73. if (errno == EIO) {
  74. printf("ptrace(PTRACE_SINGLESTEP) not supported on this architecture\n");
  75. ksft_exit_skip();
  76. }
  77. perror("ptrace(PTRACE_SINGLESTEP) failed");
  78. return false;
  79. }
  80. wpid = waitpid(pid, &status, __WALL);
  81. if (wpid != pid) {
  82. perror("waitpid() failed");
  83. return false;
  84. }
  85. if (WIFEXITED(status)) {
  86. printf("child did not single-step\n");
  87. return false;
  88. }
  89. if (!WIFSTOPPED(status)) {
  90. printf("child did not stop\n");
  91. return false;
  92. }
  93. if (WSTOPSIG(status) != SIGTRAP) {
  94. printf("child did not stop with SIGTRAP\n");
  95. return false;
  96. }
  97. if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) {
  98. perror("ptrace(PTRACE_CONT) failed");
  99. return false;
  100. }
  101. wpid = waitpid(pid, &status, __WALL);
  102. if (wpid != pid) {
  103. perror("waitpid() failed");
  104. return false;
  105. }
  106. if (!WIFEXITED(status)) {
  107. printf("child did not exit after PTRACE_CONT\n");
  108. return false;
  109. }
  110. return true;
  111. }
  112. void suspend(void)
  113. {
  114. int power_state_fd;
  115. struct sigevent event = {};
  116. int timerfd;
  117. int err;
  118. struct itimerspec spec = {};
  119. power_state_fd = open("/sys/power/state", O_RDWR);
  120. if (power_state_fd < 0) {
  121. perror("open(\"/sys/power/state\") failed (is this test running as root?)");
  122. ksft_exit_fail();
  123. }
  124. timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
  125. if (timerfd < 0) {
  126. perror("timerfd_create() failed");
  127. ksft_exit_fail();
  128. }
  129. spec.it_value.tv_sec = 5;
  130. err = timerfd_settime(timerfd, 0, &spec, NULL);
  131. if (err < 0) {
  132. perror("timerfd_settime() failed");
  133. ksft_exit_fail();
  134. }
  135. if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem")) {
  136. perror("entering suspend failed");
  137. ksft_exit_fail();
  138. }
  139. close(timerfd);
  140. close(power_state_fd);
  141. }
  142. int main(int argc, char **argv)
  143. {
  144. int opt;
  145. bool do_suspend = true;
  146. bool succeeded = true;
  147. cpu_set_t available_cpus;
  148. int err;
  149. int cpu;
  150. while ((opt = getopt(argc, argv, "n")) != -1) {
  151. switch (opt) {
  152. case 'n':
  153. do_suspend = false;
  154. break;
  155. default:
  156. printf("Usage: %s [-n]\n", argv[0]);
  157. printf(" -n: do not trigger a suspend/resume cycle before the test\n");
  158. return -1;
  159. }
  160. }
  161. if (do_suspend)
  162. suspend();
  163. err = sched_getaffinity(0, sizeof(available_cpus), &available_cpus);
  164. if (err < 0) {
  165. perror("sched_getaffinity() failed");
  166. ksft_exit_fail();
  167. }
  168. for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
  169. bool test_success;
  170. if (!CPU_ISSET(cpu, &available_cpus))
  171. continue;
  172. test_success = run_test(cpu);
  173. printf("CPU %d: ", cpu);
  174. if (test_success) {
  175. printf("[OK]\n");
  176. ksft_inc_pass_cnt();
  177. } else {
  178. printf("[FAILED]\n");
  179. ksft_inc_fail_cnt();
  180. succeeded = false;
  181. }
  182. }
  183. ksft_print_cnts();
  184. if (succeeded)
  185. ksft_exit_pass();
  186. else
  187. ksft_exit_fail();
  188. }