step_after_suspend_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Google, Inc.
  4. */
  5. #define _GNU_SOURCE
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <sched.h>
  9. #include <signal.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/ptrace.h>
  15. #include <sys/stat.h>
  16. #include <sys/timerfd.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include "kselftest.h"
  20. void child(int cpu)
  21. {
  22. cpu_set_t set;
  23. CPU_ZERO(&set);
  24. CPU_SET(cpu, &set);
  25. if (sched_setaffinity(0, sizeof(set), &set) != 0) {
  26. ksft_print_msg("sched_setaffinity() failed: %s\n",
  27. strerror(errno));
  28. _exit(1);
  29. }
  30. if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
  31. ksft_print_msg("ptrace(PTRACE_TRACEME) failed: %s\n",
  32. strerror(errno));
  33. _exit(1);
  34. }
  35. if (raise(SIGSTOP) != 0) {
  36. ksft_print_msg("raise(SIGSTOP) failed: %s\n", strerror(errno));
  37. _exit(1);
  38. }
  39. _exit(0);
  40. }
  41. int run_test(int cpu)
  42. {
  43. int status;
  44. pid_t pid = fork();
  45. pid_t wpid;
  46. if (pid < 0) {
  47. ksft_print_msg("fork() failed: %s\n", strerror(errno));
  48. return KSFT_FAIL;
  49. }
  50. if (pid == 0)
  51. child(cpu);
  52. wpid = waitpid(pid, &status, __WALL);
  53. if (wpid != pid) {
  54. ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
  55. return KSFT_FAIL;
  56. }
  57. if (!WIFSTOPPED(status)) {
  58. ksft_print_msg("child did not stop: %s\n", strerror(errno));
  59. return KSFT_FAIL;
  60. }
  61. if (WSTOPSIG(status) != SIGSTOP) {
  62. ksft_print_msg("child did not stop with SIGSTOP: %s\n",
  63. strerror(errno));
  64. return KSFT_FAIL;
  65. }
  66. if (ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0) {
  67. if (errno == EIO) {
  68. ksft_print_msg(
  69. "ptrace(PTRACE_SINGLESTEP) not supported on this architecture: %s\n",
  70. strerror(errno));
  71. return KSFT_SKIP;
  72. }
  73. ksft_print_msg("ptrace(PTRACE_SINGLESTEP) failed: %s\n",
  74. strerror(errno));
  75. return KSFT_FAIL;
  76. }
  77. wpid = waitpid(pid, &status, __WALL);
  78. if (wpid != pid) {
  79. ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
  80. return KSFT_FAIL;
  81. }
  82. if (WIFEXITED(status)) {
  83. ksft_print_msg("child did not single-step: %s\n",
  84. strerror(errno));
  85. return KSFT_FAIL;
  86. }
  87. if (!WIFSTOPPED(status)) {
  88. ksft_print_msg("child did not stop: %s\n", strerror(errno));
  89. return KSFT_FAIL;
  90. }
  91. if (WSTOPSIG(status) != SIGTRAP) {
  92. ksft_print_msg("child did not stop with SIGTRAP: %s\n",
  93. strerror(errno));
  94. return KSFT_FAIL;
  95. }
  96. if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) {
  97. ksft_print_msg("ptrace(PTRACE_CONT) failed: %s\n",
  98. strerror(errno));
  99. return KSFT_FAIL;
  100. }
  101. wpid = waitpid(pid, &status, __WALL);
  102. if (wpid != pid) {
  103. ksft_print_msg("waitpid() failed: %s\n", strerror(errno));
  104. return KSFT_FAIL;
  105. }
  106. if (!WIFEXITED(status)) {
  107. ksft_print_msg("child did not exit after PTRACE_CONT: %s\n",
  108. strerror(errno));
  109. return KSFT_FAIL;
  110. }
  111. return KSFT_PASS;
  112. }
  113. /*
  114. * Reads the suspend success count from sysfs.
  115. * Returns the count on success or exits on failure.
  116. */
  117. static int get_suspend_success_count_or_fail(void)
  118. {
  119. FILE *fp;
  120. int val;
  121. fp = fopen("/sys/power/suspend_stats/success", "r");
  122. if (!fp)
  123. ksft_exit_fail_msg(
  124. "Failed to open suspend_stats/success: %s\n",
  125. strerror(errno));
  126. if (fscanf(fp, "%d", &val) != 1) {
  127. fclose(fp);
  128. ksft_exit_fail_msg(
  129. "Failed to read suspend success count\n");
  130. }
  131. fclose(fp);
  132. return val;
  133. }
  134. void suspend(void)
  135. {
  136. int timerfd;
  137. int err;
  138. int count_before;
  139. int count_after;
  140. struct itimerspec spec = {};
  141. if (getuid() != 0)
  142. ksft_exit_skip("Please run the test as root - Exiting.\n");
  143. timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
  144. if (timerfd < 0)
  145. ksft_exit_fail_msg("timerfd_create() failed\n");
  146. spec.it_value.tv_sec = 5;
  147. err = timerfd_settime(timerfd, 0, &spec, NULL);
  148. if (err < 0)
  149. ksft_exit_fail_msg("timerfd_settime() failed\n");
  150. count_before = get_suspend_success_count_or_fail();
  151. system("(echo mem > /sys/power/state) 2> /dev/null");
  152. count_after = get_suspend_success_count_or_fail();
  153. if (count_after <= count_before)
  154. ksft_exit_fail_msg("Failed to enter Suspend state\n");
  155. close(timerfd);
  156. }
  157. int main(int argc, char **argv)
  158. {
  159. int opt;
  160. bool do_suspend = true;
  161. bool succeeded = true;
  162. unsigned int tests = 0;
  163. cpu_set_t available_cpus;
  164. int err;
  165. int cpu;
  166. ksft_print_header();
  167. while ((opt = getopt(argc, argv, "n")) != -1) {
  168. switch (opt) {
  169. case 'n':
  170. do_suspend = false;
  171. break;
  172. default:
  173. printf("Usage: %s [-n]\n", argv[0]);
  174. printf(" -n: do not trigger a suspend/resume cycle before the test\n");
  175. return -1;
  176. }
  177. }
  178. err = sched_getaffinity(0, sizeof(available_cpus), &available_cpus);
  179. if (err < 0)
  180. ksft_exit_fail_msg("sched_getaffinity() failed\n");
  181. for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
  182. if (!CPU_ISSET(cpu, &available_cpus))
  183. continue;
  184. tests++;
  185. }
  186. if (do_suspend)
  187. suspend();
  188. ksft_set_plan(tests);
  189. for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
  190. int test_success;
  191. if (!CPU_ISSET(cpu, &available_cpus))
  192. continue;
  193. test_success = run_test(cpu);
  194. switch (test_success) {
  195. case KSFT_PASS:
  196. ksft_test_result_pass("CPU %d\n", cpu);
  197. break;
  198. case KSFT_SKIP:
  199. ksft_test_result_skip("CPU %d\n", cpu);
  200. break;
  201. case KSFT_FAIL:
  202. ksft_test_result_fail("CPU %d\n", cpu);
  203. succeeded = false;
  204. break;
  205. }
  206. }
  207. if (succeeded)
  208. ksft_exit_pass();
  209. else
  210. ksft_exit_fail();
  211. }