seccomp_benchmark.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Strictly speaking, this is not a test. But it can report during test
  3. * runs so relative performance can be measured.
  4. */
  5. #define _GNU_SOURCE
  6. #include <assert.h>
  7. #include <err.h>
  8. #include <limits.h>
  9. #include <sched.h>
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include <unistd.h>
  16. #include <linux/filter.h>
  17. #include <linux/seccomp.h>
  18. #include <sys/param.h>
  19. #include <sys/prctl.h>
  20. #include <sys/syscall.h>
  21. #include <sys/types.h>
  22. #include "kselftest.h"
  23. unsigned long long timing(clockid_t clk_id, unsigned long long samples)
  24. {
  25. struct timespec start, finish;
  26. unsigned long long i;
  27. pid_t pid, ret;
  28. pid = getpid();
  29. assert(clock_gettime(clk_id, &start) == 0);
  30. for (i = 0; i < samples; i++) {
  31. ret = syscall(__NR_getpid);
  32. assert(pid == ret);
  33. }
  34. assert(clock_gettime(clk_id, &finish) == 0);
  35. i = finish.tv_sec - start.tv_sec;
  36. i *= 1000000000ULL;
  37. i += finish.tv_nsec - start.tv_nsec;
  38. ksft_print_msg("%lu.%09lu - %lu.%09lu = %llu (%.1fs)\n",
  39. finish.tv_sec, finish.tv_nsec,
  40. start.tv_sec, start.tv_nsec,
  41. i, (double)i / 1000000000.0);
  42. return i;
  43. }
  44. unsigned long long calibrate(void)
  45. {
  46. struct timespec start, finish;
  47. unsigned long long i, samples, step = 9973;
  48. pid_t pid, ret;
  49. int seconds = 15;
  50. ksft_print_msg("Calibrating sample size for %d seconds worth of syscalls ...\n", seconds);
  51. samples = 0;
  52. pid = getpid();
  53. assert(clock_gettime(CLOCK_MONOTONIC, &start) == 0);
  54. do {
  55. for (i = 0; i < step; i++) {
  56. ret = syscall(__NR_getpid);
  57. assert(pid == ret);
  58. }
  59. assert(clock_gettime(CLOCK_MONOTONIC, &finish) == 0);
  60. samples += step;
  61. i = finish.tv_sec - start.tv_sec;
  62. i *= 1000000000ULL;
  63. i += finish.tv_nsec - start.tv_nsec;
  64. } while (i < 1000000000ULL);
  65. return samples * seconds;
  66. }
  67. bool approx(int i_one, int i_two)
  68. {
  69. /*
  70. * This continues to be a noisy test. Instead of a 1% comparison
  71. * go with 10%.
  72. */
  73. double one = i_one, one_bump = one * 0.1;
  74. double two = i_two, two_bump = two * 0.1;
  75. one_bump = one + MAX(one_bump, 2.0);
  76. two_bump = two + MAX(two_bump, 2.0);
  77. /* Equal to, or within 1% or 2 digits */
  78. if (one == two ||
  79. (one > two && one <= two_bump) ||
  80. (two > one && two <= one_bump))
  81. return true;
  82. return false;
  83. }
  84. bool le(int i_one, int i_two)
  85. {
  86. if (i_one <= i_two)
  87. return true;
  88. return false;
  89. }
  90. long compare(const char *name_one, const char *name_eval, const char *name_two,
  91. unsigned long long one, bool (*eval)(int, int), unsigned long long two,
  92. bool skip)
  93. {
  94. bool good;
  95. if (skip) {
  96. ksft_test_result_skip("%s %s %s\n", name_one, name_eval,
  97. name_two);
  98. return 0;
  99. }
  100. ksft_print_msg("\t%s %s %s (%lld %s %lld): ", name_one, name_eval, name_two,
  101. (long long)one, name_eval, (long long)two);
  102. if (one > INT_MAX) {
  103. ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)one);
  104. good = false;
  105. goto out;
  106. }
  107. if (two > INT_MAX) {
  108. ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)two);
  109. good = false;
  110. goto out;
  111. }
  112. good = eval(one, two);
  113. printf("%s\n", good ? "✔️" : "❌");
  114. out:
  115. ksft_test_result(good, "%s %s %s\n", name_one, name_eval, name_two);
  116. return good ? 0 : 1;
  117. }
  118. /* Pin to a single CPU so the benchmark won't bounce around the system. */
  119. void affinity(void)
  120. {
  121. long cpu;
  122. ulong ncores = sysconf(_SC_NPROCESSORS_CONF);
  123. cpu_set_t *setp = CPU_ALLOC(ncores);
  124. ulong setsz = CPU_ALLOC_SIZE(ncores);
  125. /*
  126. * Totally unscientific way to avoid CPUs that might be busier:
  127. * choose the highest CPU instead of the lowest.
  128. */
  129. for (cpu = ncores - 1; cpu >= 0; cpu--) {
  130. CPU_ZERO_S(setsz, setp);
  131. CPU_SET_S(cpu, setsz, setp);
  132. if (sched_setaffinity(getpid(), setsz, setp) == -1)
  133. continue;
  134. printf("Pinned to CPU %lu of %lu\n", cpu + 1, ncores);
  135. goto out;
  136. }
  137. fprintf(stderr, "Could not set CPU affinity -- calibration may not work well");
  138. out:
  139. CPU_FREE(setp);
  140. }
  141. int main(int argc, char *argv[])
  142. {
  143. struct sock_filter bitmap_filter[] = {
  144. BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, nr)),
  145. BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
  146. };
  147. struct sock_fprog bitmap_prog = {
  148. .len = (unsigned short)ARRAY_SIZE(bitmap_filter),
  149. .filter = bitmap_filter,
  150. };
  151. struct sock_filter filter[] = {
  152. BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, args[0])),
  153. BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
  154. };
  155. struct sock_fprog prog = {
  156. .len = (unsigned short)ARRAY_SIZE(filter),
  157. .filter = filter,
  158. };
  159. long ret, bits;
  160. unsigned long long samples, calc;
  161. unsigned long long native, filter1, filter2, bitmap1, bitmap2;
  162. unsigned long long entry, per_filter1, per_filter2;
  163. bool skip = false;
  164. setbuf(stdout, NULL);
  165. ksft_print_header();
  166. ksft_set_plan(7);
  167. ksft_print_msg("Running on:\n");
  168. ksft_print_msg("%s", "");
  169. system("uname -a");
  170. ksft_print_msg("Current BPF sysctl settings:\n");
  171. /* Avoid using "sysctl" which may not be installed. */
  172. ksft_print_msg("%s", "");
  173. system("grep -H . /proc/sys/net/core/bpf_jit_enable");
  174. ksft_print_msg("%s", "");
  175. system("grep -H . /proc/sys/net/core/bpf_jit_harden");
  176. affinity();
  177. if (argc > 1)
  178. samples = strtoull(argv[1], NULL, 0);
  179. else
  180. samples = calibrate();
  181. ksft_print_msg("Benchmarking %llu syscalls...\n", samples);
  182. /* Native call */
  183. native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  184. ksft_print_msg("getpid native: %llu ns\n", native);
  185. ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
  186. assert(ret == 0);
  187. /* One filter resulting in a bitmap */
  188. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
  189. assert(ret == 0);
  190. bitmap1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  191. ksft_print_msg("getpid RET_ALLOW 1 filter (bitmap): %llu ns\n", bitmap1);
  192. /* Second filter resulting in a bitmap */
  193. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
  194. assert(ret == 0);
  195. bitmap2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  196. ksft_print_msg("getpid RET_ALLOW 2 filters (bitmap): %llu ns\n", bitmap2);
  197. /* Third filter, can no longer be converted to bitmap */
  198. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
  199. assert(ret == 0);
  200. filter1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  201. ksft_print_msg("getpid RET_ALLOW 3 filters (full): %llu ns\n", filter1);
  202. /* Fourth filter, can not be converted to bitmap because of filter 3 */
  203. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
  204. assert(ret == 0);
  205. filter2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  206. ksft_print_msg("getpid RET_ALLOW 4 filters (full): %llu ns\n", filter2);
  207. /* Estimations */
  208. #define ESTIMATE(fmt, var, what) do { \
  209. var = (what); \
  210. ksft_print_msg("Estimated " fmt ": %llu ns\n", var); \
  211. if (var > INT_MAX) { \
  212. skip = true; \
  213. ret |= 1; \
  214. } \
  215. } while (0)
  216. ESTIMATE("total seccomp overhead for 1 bitmapped filter", calc,
  217. bitmap1 - native);
  218. ESTIMATE("total seccomp overhead for 2 bitmapped filters", calc,
  219. bitmap2 - native);
  220. ESTIMATE("total seccomp overhead for 3 full filters", calc,
  221. filter1 - native);
  222. ESTIMATE("total seccomp overhead for 4 full filters", calc,
  223. filter2 - native);
  224. ESTIMATE("seccomp entry overhead", entry,
  225. bitmap1 - native - (bitmap2 - bitmap1));
  226. ESTIMATE("seccomp per-filter overhead (last 2 diff)", per_filter1,
  227. filter2 - filter1);
  228. ESTIMATE("seccomp per-filter overhead (filters / 4)", per_filter2,
  229. (filter2 - native - entry) / 4);
  230. ksft_print_msg("Expectations:\n");
  231. ret |= compare("native", "≤", "1 bitmap", native, le, bitmap1,
  232. skip);
  233. bits = compare("native", "≤", "1 filter", native, le, filter1,
  234. skip);
  235. if (bits)
  236. skip = true;
  237. ret |= compare("per-filter (last 2 diff)", "≈", "per-filter (filters / 4)",
  238. per_filter1, approx, per_filter2, skip);
  239. bits = compare("1 bitmapped", "≈", "2 bitmapped",
  240. bitmap1 - native, approx, bitmap2 - native, skip);
  241. if (bits) {
  242. ksft_print_msg("Skipping constant action bitmap expectations: they appear unsupported.\n");
  243. skip = true;
  244. }
  245. ret |= compare("entry", "≈", "1 bitmapped", entry, approx,
  246. bitmap1 - native, skip);
  247. ret |= compare("entry", "≈", "2 bitmapped", entry, approx,
  248. bitmap2 - native, skip);
  249. ret |= compare("native + entry + (per filter * 4)", "≈", "4 filters total",
  250. entry + (per_filter1 * 4) + native, approx, filter2,
  251. skip);
  252. if (ret)
  253. ksft_print_msg("Saw unexpected benchmark result. Try running again with more samples?\n");
  254. ksft_finished();
  255. }