lib.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #define _GNU_SOURCE /* For CPU_ZERO etc. */
  6. #include <elf.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <link.h>
  10. #include <sched.h>
  11. #include <setjmp.h>
  12. #include <stdlib.h>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include "utils.h"
  17. #include "lib.h"
  18. int pick_online_cpu(void)
  19. {
  20. cpu_set_t mask;
  21. int cpu;
  22. CPU_ZERO(&mask);
  23. if (sched_getaffinity(0, sizeof(mask), &mask)) {
  24. perror("sched_getaffinity");
  25. return -1;
  26. }
  27. /* We prefer a primary thread, but skip 0 */
  28. for (cpu = 8; cpu < CPU_SETSIZE; cpu += 8)
  29. if (CPU_ISSET(cpu, &mask))
  30. return cpu;
  31. /* Search for anything, but in reverse */
  32. for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--)
  33. if (CPU_ISSET(cpu, &mask))
  34. return cpu;
  35. printf("No cpus in affinity mask?!\n");
  36. return -1;
  37. }
  38. int bind_to_cpu(int cpu)
  39. {
  40. cpu_set_t mask;
  41. printf("Binding to cpu %d\n", cpu);
  42. CPU_ZERO(&mask);
  43. CPU_SET(cpu, &mask);
  44. return sched_setaffinity(0, sizeof(mask), &mask);
  45. }
  46. #define PARENT_TOKEN 0xAA
  47. #define CHILD_TOKEN 0x55
  48. int sync_with_child(union pipe read_pipe, union pipe write_pipe)
  49. {
  50. char c = PARENT_TOKEN;
  51. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  52. FAIL_IF(read(read_pipe.read_fd, &c, 1) != 1);
  53. if (c != CHILD_TOKEN) /* sometimes expected */
  54. return 1;
  55. return 0;
  56. }
  57. int wait_for_parent(union pipe read_pipe)
  58. {
  59. char c;
  60. FAIL_IF(read(read_pipe.read_fd, &c, 1) != 1);
  61. FAIL_IF(c != PARENT_TOKEN);
  62. return 0;
  63. }
  64. int notify_parent(union pipe write_pipe)
  65. {
  66. char c = CHILD_TOKEN;
  67. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  68. return 0;
  69. }
  70. int notify_parent_of_error(union pipe write_pipe)
  71. {
  72. char c = ~CHILD_TOKEN;
  73. FAIL_IF(write(write_pipe.write_fd, &c, 1) != 1);
  74. return 0;
  75. }
  76. int wait_for_child(pid_t child_pid)
  77. {
  78. int rc;
  79. if (waitpid(child_pid, &rc, 0) == -1) {
  80. perror("waitpid");
  81. return 1;
  82. }
  83. if (WIFEXITED(rc))
  84. rc = WEXITSTATUS(rc);
  85. else
  86. rc = 1; /* Signal or other */
  87. return rc;
  88. }
  89. int kill_child_and_wait(pid_t child_pid)
  90. {
  91. kill(child_pid, SIGTERM);
  92. return wait_for_child(child_pid);
  93. }
  94. static int eat_cpu_child(union pipe read_pipe, union pipe write_pipe)
  95. {
  96. volatile int i = 0;
  97. /*
  98. * We are just here to eat cpu and die. So make sure we can be killed,
  99. * and also don't do any custom SIGTERM handling.
  100. */
  101. signal(SIGTERM, SIG_DFL);
  102. notify_parent(write_pipe);
  103. wait_for_parent(read_pipe);
  104. /* Soak up cpu forever */
  105. while (1) i++;
  106. return 0;
  107. }
  108. pid_t eat_cpu(int (test_function)(void))
  109. {
  110. union pipe read_pipe, write_pipe;
  111. int cpu, rc;
  112. pid_t pid;
  113. cpu = pick_online_cpu();
  114. FAIL_IF(cpu < 0);
  115. FAIL_IF(bind_to_cpu(cpu));
  116. if (pipe(read_pipe.fds) == -1)
  117. return -1;
  118. if (pipe(write_pipe.fds) == -1)
  119. return -1;
  120. pid = fork();
  121. if (pid == 0)
  122. exit(eat_cpu_child(write_pipe, read_pipe));
  123. if (sync_with_child(read_pipe, write_pipe)) {
  124. rc = -1;
  125. goto out;
  126. }
  127. printf("main test running as pid %d\n", getpid());
  128. rc = test_function();
  129. out:
  130. kill(pid, SIGKILL);
  131. return rc;
  132. }
  133. struct addr_range libc, vdso;
  134. int parse_proc_maps(void)
  135. {
  136. unsigned long start, end;
  137. char execute, name[128];
  138. FILE *f;
  139. int rc;
  140. f = fopen("/proc/self/maps", "r");
  141. if (!f) {
  142. perror("fopen");
  143. return -1;
  144. }
  145. do {
  146. /* This skips line with no executable which is what we want */
  147. rc = fscanf(f, "%lx-%lx %*c%*c%c%*c %*x %*d:%*d %*d %127s\n",
  148. &start, &end, &execute, name);
  149. if (rc <= 0)
  150. break;
  151. if (execute != 'x')
  152. continue;
  153. if (strstr(name, "libc")) {
  154. libc.first = start;
  155. libc.last = end - 1;
  156. } else if (strstr(name, "[vdso]")) {
  157. vdso.first = start;
  158. vdso.last = end - 1;
  159. }
  160. } while(1);
  161. fclose(f);
  162. return 0;
  163. }
  164. #define PARANOID_PATH "/proc/sys/kernel/perf_event_paranoid"
  165. bool require_paranoia_below(int level)
  166. {
  167. unsigned long current;
  168. char *end, buf[16];
  169. FILE *f;
  170. int rc;
  171. rc = -1;
  172. f = fopen(PARANOID_PATH, "r");
  173. if (!f) {
  174. perror("fopen");
  175. goto out;
  176. }
  177. if (!fgets(buf, sizeof(buf), f)) {
  178. printf("Couldn't read " PARANOID_PATH "?\n");
  179. goto out_close;
  180. }
  181. current = strtoul(buf, &end, 10);
  182. if (end == buf) {
  183. printf("Couldn't parse " PARANOID_PATH "?\n");
  184. goto out_close;
  185. }
  186. if (current >= level)
  187. goto out;
  188. rc = 0;
  189. out_close:
  190. fclose(f);
  191. out:
  192. return rc;
  193. }
  194. static char auxv[4096];
  195. void *get_auxv_entry(int type)
  196. {
  197. ElfW(auxv_t) *p;
  198. void *result;
  199. ssize_t num;
  200. int fd;
  201. fd = open("/proc/self/auxv", O_RDONLY);
  202. if (fd == -1) {
  203. perror("open");
  204. return NULL;
  205. }
  206. result = NULL;
  207. num = read(fd, auxv, sizeof(auxv));
  208. if (num < 0) {
  209. perror("read");
  210. goto out;
  211. }
  212. if (num > sizeof(auxv)) {
  213. printf("Overflowed auxv buffer\n");
  214. goto out;
  215. }
  216. p = (ElfW(auxv_t) *)auxv;
  217. while (p->a_type != AT_NULL) {
  218. if (p->a_type == type) {
  219. result = (void *)p->a_un.a_val;
  220. break;
  221. }
  222. p++;
  223. }
  224. out:
  225. close(fd);
  226. return result;
  227. }