sched-seccomp-notify.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <subcmd/parse-options.h>
  3. #include "bench.h"
  4. #include <uapi/linux/filter.h>
  5. #include <sys/types.h>
  6. #include <sys/time.h>
  7. #include <linux/unistd.h>
  8. #include <sys/syscall.h>
  9. #include <sys/ioctl.h>
  10. #include <linux/time64.h>
  11. #include <uapi/linux/seccomp.h>
  12. #include <sys/prctl.h>
  13. #include <unistd.h>
  14. #include <limits.h>
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <signal.h>
  20. #include <sys/wait.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <err.h>
  24. #include <inttypes.h>
  25. #define LOOPS_DEFAULT 1000000UL
  26. static uint64_t loops = LOOPS_DEFAULT;
  27. static bool sync_mode;
  28. static const struct option options[] = {
  29. OPT_U64('l', "loop", &loops, "Specify number of loops"),
  30. OPT_BOOLEAN('s', "sync-mode", &sync_mode,
  31. "Enable the synchronous mode for seccomp notifications"),
  32. OPT_END()
  33. };
  34. static const char * const bench_seccomp_usage[] = {
  35. "perf bench sched secccomp-notify <options>",
  36. NULL
  37. };
  38. static int seccomp(unsigned int op, unsigned int flags, void *args)
  39. {
  40. return syscall(__NR_seccomp, op, flags, args);
  41. }
  42. static int user_notif_syscall(int nr, unsigned int flags)
  43. {
  44. struct sock_filter filter[] = {
  45. BPF_STMT(BPF_LD|BPF_W|BPF_ABS,
  46. offsetof(struct seccomp_data, nr)),
  47. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, nr, 0, 1),
  48. BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_USER_NOTIF),
  49. BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
  50. };
  51. struct sock_fprog prog = {
  52. .len = (unsigned short)ARRAY_SIZE(filter),
  53. .filter = filter,
  54. };
  55. return seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog);
  56. }
  57. #define USER_NOTIF_MAGIC INT_MAX
  58. static void user_notification_sync_loop(int listener)
  59. {
  60. struct seccomp_notif_resp resp;
  61. struct seccomp_notif req;
  62. uint64_t nr;
  63. for (nr = 0; nr < loops; nr++) {
  64. memset(&req, 0, sizeof(req));
  65. if (ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req))
  66. err(EXIT_FAILURE, "SECCOMP_IOCTL_NOTIF_RECV failed");
  67. if (req.data.nr != __NR_gettid)
  68. errx(EXIT_FAILURE, "unexpected syscall: %d", req.data.nr);
  69. resp.id = req.id;
  70. resp.error = 0;
  71. resp.val = USER_NOTIF_MAGIC;
  72. resp.flags = 0;
  73. if (ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp))
  74. err(EXIT_FAILURE, "SECCOMP_IOCTL_NOTIF_SEND failed");
  75. }
  76. }
  77. #ifndef SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP
  78. #define SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP (1UL << 0)
  79. #define SECCOMP_IOCTL_NOTIF_SET_FLAGS SECCOMP_IOW(4, __u64)
  80. #endif
  81. int bench_sched_seccomp_notify(int argc, const char **argv)
  82. {
  83. struct timeval start, stop, diff;
  84. unsigned long long result_usec = 0;
  85. int status, listener;
  86. pid_t pid;
  87. long ret;
  88. argc = parse_options(argc, argv, options, bench_seccomp_usage, 0);
  89. gettimeofday(&start, NULL);
  90. prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
  91. listener = user_notif_syscall(__NR_gettid,
  92. SECCOMP_FILTER_FLAG_NEW_LISTENER);
  93. if (listener < 0)
  94. err(EXIT_FAILURE, "can't create a notification descriptor");
  95. pid = fork();
  96. if (pid < 0)
  97. err(EXIT_FAILURE, "fork");
  98. if (pid == 0) {
  99. if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0))
  100. err(EXIT_FAILURE, "can't set the parent death signal");
  101. while (1) {
  102. ret = syscall(__NR_gettid);
  103. if (ret == USER_NOTIF_MAGIC)
  104. continue;
  105. break;
  106. }
  107. _exit(1);
  108. }
  109. if (sync_mode) {
  110. if (ioctl(listener, SECCOMP_IOCTL_NOTIF_SET_FLAGS,
  111. SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP, 0))
  112. err(EXIT_FAILURE,
  113. "can't set SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP");
  114. }
  115. user_notification_sync_loop(listener);
  116. kill(pid, SIGKILL);
  117. if (waitpid(pid, &status, 0) != pid)
  118. err(EXIT_FAILURE, "waitpid(%d) failed", pid);
  119. if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
  120. errx(EXIT_FAILURE, "unexpected exit code: %d", status);
  121. gettimeofday(&stop, NULL);
  122. timersub(&stop, &start, &diff);
  123. switch (bench_format) {
  124. case BENCH_FORMAT_DEFAULT:
  125. printf("# Executed %" PRIu64 " system calls\n\n",
  126. loops);
  127. result_usec = diff.tv_sec * USEC_PER_SEC;
  128. result_usec += diff.tv_usec;
  129. printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
  130. (unsigned long) diff.tv_sec,
  131. (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
  132. printf(" %14lf usecs/op\n",
  133. (double)result_usec / (double)loops);
  134. printf(" %14d ops/sec\n",
  135. (int)((double)loops /
  136. ((double)result_usec / (double)USEC_PER_SEC)));
  137. break;
  138. case BENCH_FORMAT_SIMPLE:
  139. printf("%lu.%03lu\n",
  140. (unsigned long) diff.tv_sec,
  141. (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
  142. break;
  143. default:
  144. /* reaching here is something disaster */
  145. fprintf(stderr, "Unknown format:%d\n", bench_format);
  146. exit(1);
  147. break;
  148. }
  149. return 0;
  150. }