cs_prctl_test.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Use the core scheduling prctl() to test core scheduling cookies control.
  4. *
  5. * Copyright (c) 2021 Oracle and/or its affiliates.
  6. * Author: Chris Hyser <chris.hyser@oracle.com>
  7. *
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of version 2.1 of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this library; if not, see <http://www.gnu.org/licenses>.
  20. */
  21. #define _GNU_SOURCE
  22. #include <sys/eventfd.h>
  23. #include <sys/wait.h>
  24. #include <sys/types.h>
  25. #include <sched.h>
  26. #include <sys/prctl.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <errno.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #if __GLIBC_PREREQ(2, 30) == 0
  34. #include <sys/syscall.h>
  35. static pid_t gettid(void)
  36. {
  37. return syscall(SYS_gettid);
  38. }
  39. #endif
  40. #ifndef PR_SCHED_CORE
  41. #define PR_SCHED_CORE 62
  42. #define PR_SCHED_CORE_GET 0
  43. #define PR_SCHED_CORE_CREATE 1 /* create unique core_sched cookie */
  44. #define PR_SCHED_CORE_SHARE_TO 2 /* push core_sched cookie to pid */
  45. #define PR_SCHED_CORE_SHARE_FROM 3 /* pull core_sched cookie to pid */
  46. #define PR_SCHED_CORE_MAX 4
  47. #endif
  48. #define MAX_PROCESSES 128
  49. #define MAX_THREADS 128
  50. static const char USAGE[] = "cs_prctl_test [options]\n"
  51. " options:\n"
  52. " -P : number of processes to create.\n"
  53. " -T : number of threads per process to create.\n"
  54. " -d : delay time to keep tasks alive.\n"
  55. " -k : keep tasks alive until keypress.\n";
  56. enum pid_type {PIDTYPE_PID = 0, PIDTYPE_TGID, PIDTYPE_PGID};
  57. const int THREAD_CLONE_FLAGS = CLONE_THREAD | CLONE_SIGHAND | CLONE_FS | CLONE_VM | CLONE_FILES;
  58. struct child_args {
  59. int num_threads;
  60. int pfd[2];
  61. int cpid;
  62. int thr_tids[MAX_THREADS];
  63. };
  64. static struct child_args procs[MAX_PROCESSES];
  65. static int num_processes = 2;
  66. static int need_cleanup;
  67. static int _prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4,
  68. unsigned long arg5)
  69. {
  70. int res;
  71. res = prctl(option, arg2, arg3, arg4, arg5);
  72. printf("%d = prctl(%d, %ld, %ld, %ld, %lx)\n", res, option, (long)arg2, (long)arg3,
  73. (long)arg4, arg5);
  74. return res;
  75. }
  76. #define STACK_SIZE (1024 * 1024)
  77. #define handle_error(msg) __handle_error(__FILE__, __LINE__, msg)
  78. static void __handle_error(char *fn, int ln, char *msg)
  79. {
  80. int pidx;
  81. printf("(%s:%d) - ", fn, ln);
  82. perror(msg);
  83. if (need_cleanup) {
  84. for (pidx = 0; pidx < num_processes; ++pidx)
  85. kill(procs[pidx].cpid, 15);
  86. need_cleanup = 0;
  87. }
  88. exit(EXIT_FAILURE);
  89. }
  90. static void handle_usage(int rc, char *msg)
  91. {
  92. puts(USAGE);
  93. puts(msg);
  94. putchar('\n');
  95. exit(rc);
  96. }
  97. static unsigned long get_cs_cookie(int pid)
  98. {
  99. unsigned long long cookie;
  100. int ret;
  101. ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, pid, PIDTYPE_PID,
  102. (unsigned long)&cookie);
  103. if (ret) {
  104. printf("Not a core sched system\n");
  105. return -1UL;
  106. }
  107. return cookie;
  108. }
  109. static int child_func_thread(void __attribute__((unused))*arg)
  110. {
  111. while (1)
  112. usleep(20000);
  113. return 0;
  114. }
  115. static void create_threads(int num_threads, int thr_tids[])
  116. {
  117. void *child_stack;
  118. pid_t tid;
  119. int i;
  120. for (i = 0; i < num_threads; ++i) {
  121. child_stack = malloc(STACK_SIZE);
  122. if (!child_stack)
  123. handle_error("child stack allocate");
  124. tid = clone(child_func_thread, child_stack + STACK_SIZE, THREAD_CLONE_FLAGS, NULL);
  125. if (tid == -1)
  126. handle_error("clone thread");
  127. thr_tids[i] = tid;
  128. }
  129. }
  130. static int child_func_process(void *arg)
  131. {
  132. struct child_args *ca = (struct child_args *)arg;
  133. int ret;
  134. close(ca->pfd[0]);
  135. create_threads(ca->num_threads, ca->thr_tids);
  136. ret = write(ca->pfd[1], &ca->thr_tids, sizeof(int) * ca->num_threads);
  137. if (ret == -1)
  138. printf("write failed on pfd[%d] - error (%s)\n",
  139. ca->pfd[1], strerror(errno));
  140. close(ca->pfd[1]);
  141. while (1)
  142. usleep(20000);
  143. return 0;
  144. }
  145. static unsigned char child_func_process_stack[STACK_SIZE];
  146. void create_processes(int num_processes, int num_threads, struct child_args proc[])
  147. {
  148. pid_t cpid;
  149. int i, ret;
  150. for (i = 0; i < num_processes; ++i) {
  151. proc[i].num_threads = num_threads;
  152. if (pipe(proc[i].pfd) == -1)
  153. handle_error("pipe() failed");
  154. cpid = clone(child_func_process, child_func_process_stack + STACK_SIZE,
  155. SIGCHLD, &proc[i]);
  156. proc[i].cpid = cpid;
  157. close(proc[i].pfd[1]);
  158. }
  159. for (i = 0; i < num_processes; ++i) {
  160. ret = read(proc[i].pfd[0], &proc[i].thr_tids, sizeof(int) * proc[i].num_threads);
  161. if (ret == -1)
  162. printf("read failed on proc[%d].pfd[0] error (%s)\n",
  163. i, strerror(errno));
  164. close(proc[i].pfd[0]);
  165. }
  166. }
  167. void disp_processes(int num_processes, struct child_args proc[])
  168. {
  169. int i, j;
  170. printf("tid=%d, / tgid=%d / pgid=%d: %lx\n", gettid(), getpid(), getpgid(0),
  171. get_cs_cookie(getpid()));
  172. for (i = 0; i < num_processes; ++i) {
  173. printf(" tid=%d, / tgid=%d / pgid=%d: %lx\n", proc[i].cpid, proc[i].cpid,
  174. getpgid(proc[i].cpid), get_cs_cookie(proc[i].cpid));
  175. for (j = 0; j < proc[i].num_threads; ++j) {
  176. printf(" tid=%d, / tgid=%d / pgid=%d: %lx\n", proc[i].thr_tids[j],
  177. proc[i].cpid, getpgid(0), get_cs_cookie(proc[i].thr_tids[j]));
  178. }
  179. }
  180. puts("\n");
  181. }
  182. static int errors;
  183. #define validate(v) _validate(__LINE__, v, #v)
  184. void _validate(int line, int val, char *msg)
  185. {
  186. if (!val) {
  187. ++errors;
  188. printf("(%d) FAILED: %s\n", line, msg);
  189. } else {
  190. printf("(%d) PASSED: %s\n", line, msg);
  191. }
  192. }
  193. int main(int argc, char *argv[])
  194. {
  195. int keypress = 0;
  196. int num_threads = 3;
  197. int delay = 0;
  198. int res = 0;
  199. int pidx;
  200. int pid;
  201. int opt;
  202. while ((opt = getopt(argc, argv, ":hkT:P:d:")) != -1) {
  203. switch (opt) {
  204. case 'P':
  205. num_processes = (int)strtol(optarg, NULL, 10);
  206. break;
  207. case 'T':
  208. num_threads = (int)strtoul(optarg, NULL, 10);
  209. break;
  210. case 'd':
  211. delay = (int)strtol(optarg, NULL, 10);
  212. break;
  213. case 'k':
  214. keypress = 1;
  215. break;
  216. case 'h':
  217. printf(USAGE);
  218. exit(EXIT_SUCCESS);
  219. default:
  220. handle_usage(20, "unknown option");
  221. }
  222. }
  223. if (num_processes < 1 || num_processes > MAX_PROCESSES)
  224. handle_usage(1, "Bad processes value");
  225. if (num_threads < 1 || num_threads > MAX_THREADS)
  226. handle_usage(2, "Bad thread value");
  227. if (keypress)
  228. delay = -1;
  229. srand(time(NULL));
  230. /* put into separate process group */
  231. if (setpgid(0, 0) != 0)
  232. handle_error("process group");
  233. printf("\n## Create a thread/process/process group hierarchy\n");
  234. create_processes(num_processes, num_threads, procs);
  235. need_cleanup = 1;
  236. disp_processes(num_processes, procs);
  237. validate(get_cs_cookie(0) == 0);
  238. printf("\n## Set a cookie on entire process group\n");
  239. if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_PGID, 0) < 0)
  240. handle_error("core_sched create failed -- PGID");
  241. disp_processes(num_processes, procs);
  242. validate(get_cs_cookie(0) != 0);
  243. /* get a random process pid */
  244. pidx = rand() % num_processes;
  245. pid = procs[pidx].cpid;
  246. validate(get_cs_cookie(0) == get_cs_cookie(pid));
  247. validate(get_cs_cookie(0) == get_cs_cookie(procs[pidx].thr_tids[0]));
  248. printf("\n## Set a new cookie on entire process/TGID [%d]\n", pid);
  249. if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, pid, PIDTYPE_TGID, 0) < 0)
  250. handle_error("core_sched create failed -- TGID");
  251. disp_processes(num_processes, procs);
  252. validate(get_cs_cookie(0) != get_cs_cookie(pid));
  253. validate(get_cs_cookie(pid) != 0);
  254. validate(get_cs_cookie(pid) == get_cs_cookie(procs[pidx].thr_tids[0]));
  255. printf("\n## Copy the cookie of current/PGID[%d], to pid [%d] as PIDTYPE_PID\n",
  256. getpid(), pid);
  257. if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, pid, PIDTYPE_PID, 0) < 0)
  258. handle_error("core_sched share to itself failed -- PID");
  259. disp_processes(num_processes, procs);
  260. validate(get_cs_cookie(0) == get_cs_cookie(pid));
  261. validate(get_cs_cookie(pid) != 0);
  262. validate(get_cs_cookie(pid) != get_cs_cookie(procs[pidx].thr_tids[0]));
  263. printf("\n## Copy cookie from a thread [%d] to current/PGID [%d] as PIDTYPE_PID\n",
  264. procs[pidx].thr_tids[0], getpid());
  265. if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, procs[pidx].thr_tids[0],
  266. PIDTYPE_PID, 0) < 0)
  267. handle_error("core_sched share from thread failed -- PID");
  268. disp_processes(num_processes, procs);
  269. validate(get_cs_cookie(0) == get_cs_cookie(procs[pidx].thr_tids[0]));
  270. validate(get_cs_cookie(pid) != get_cs_cookie(procs[pidx].thr_tids[0]));
  271. printf("\n## Copy cookie from current [%d] to current as pidtype PGID\n", getpid());
  272. if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, 0, PIDTYPE_PGID, 0) < 0)
  273. handle_error("core_sched share to self failed -- PGID");
  274. disp_processes(num_processes, procs);
  275. validate(get_cs_cookie(0) == get_cs_cookie(pid));
  276. validate(get_cs_cookie(pid) != 0);
  277. validate(get_cs_cookie(pid) == get_cs_cookie(procs[pidx].thr_tids[0]));
  278. validate(_prctl(PR_SCHED_CORE, PR_SCHED_CORE_MAX, 0, PIDTYPE_PGID, 0) < 0
  279. && errno == EINVAL);
  280. validate(_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, 0, PIDTYPE_PGID, 1) < 0
  281. && errno == EINVAL);
  282. if (errors) {
  283. printf("TESTS FAILED. errors: %d\n", errors);
  284. res = 10;
  285. } else {
  286. printf("SUCCESS !!!\n");
  287. }
  288. if (keypress)
  289. getchar();
  290. else
  291. sleep(delay);
  292. for (pidx = 0; pidx < num_processes; ++pidx)
  293. kill(procs[pidx].cpid, 15);
  294. return res;
  295. }