2 * Strictly speaking, this is not a test. But it can report during test
3 * runs so relative performace can be measured.
14 #include <linux/filter.h>
15 #include <linux/seccomp.h>
16 #include <sys/param.h>
17 #include <sys/prctl.h>
18 #include <sys/syscall.h>
19 #include <sys/types.h>
21 #include "../kselftest.h"
23 unsigned long long timing(clockid_t clk_id, unsigned long long samples)
25 struct timespec start, finish;
30 assert(clock_gettime(clk_id, &start) == 0);
31 for (i = 0; i < samples; i++) {
32 ret = syscall(__NR_getpid);
35 assert(clock_gettime(clk_id, &finish) == 0);
37 i = finish.tv_sec - start.tv_sec;
39 i += finish.tv_nsec - start.tv_nsec;
41 ksft_print_msg("%lu.%09lu - %lu.%09lu = %llu (%.1fs)\n",
42 finish.tv_sec, finish.tv_nsec,
43 start.tv_sec, start.tv_nsec,
44 i, (double)i / 1000000000.0);
49 unsigned long long calibrate(void)
51 struct timespec start, finish;
52 unsigned long long i, samples, step = 9973;
56 ksft_print_msg("Calibrating sample size for %d seconds worth of syscalls ...\n", seconds);
60 assert(clock_gettime(CLOCK_MONOTONIC, &start) == 0);
62 for (i = 0; i < step; i++) {
63 ret = syscall(__NR_getpid);
66 assert(clock_gettime(CLOCK_MONOTONIC, &finish) == 0);
69 i = finish.tv_sec - start.tv_sec;
71 i += finish.tv_nsec - start.tv_nsec;
72 } while (i < 1000000000ULL);
74 return samples * seconds;
77 bool approx(int i_one, int i_two)
79 double one = i_one, one_bump = one * 0.01;
80 double two = i_two, two_bump = two * 0.01;
82 one_bump = one + MAX(one_bump, 2.0);
83 two_bump = two + MAX(two_bump, 2.0);
85 /* Equal to, or within 1% or 2 digits */
87 (one > two && one <= two_bump) ||
88 (two > one && two <= one_bump))
93 bool le(int i_one, int i_two)
100 long compare(const char *name_one, const char *name_eval, const char *name_two,
101 unsigned long long one, bool (*eval)(int, int), unsigned long long two)
105 ksft_print_msg("\t%s %s %s (%lld %s %lld): ", name_one, name_eval, name_two,
106 (long long)one, name_eval, (long long)two);
108 ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)one);
112 ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)two);
116 good = eval(one, two);
117 printf("%s\n", good ? "✔️" : "❌");
122 int main(int argc, char *argv[])
124 struct sock_filter bitmap_filter[] = {
125 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, nr)),
126 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
128 struct sock_fprog bitmap_prog = {
129 .len = (unsigned short)ARRAY_SIZE(bitmap_filter),
130 .filter = bitmap_filter,
132 struct sock_filter filter[] = {
133 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, args[0])),
134 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
136 struct sock_fprog prog = {
137 .len = (unsigned short)ARRAY_SIZE(filter),
142 unsigned long long samples, calc;
143 unsigned long long native, filter1, filter2, bitmap1, bitmap2;
144 unsigned long long entry, per_filter1, per_filter2;
146 setbuf(stdout, NULL);
148 ksft_print_msg("Running on:\n");
152 ksft_print_msg("Current BPF sysctl settings:\n");
153 /* Avoid using "sysctl" which may not be installed. */
155 system("grep -H . /proc/sys/net/core/bpf_jit_enable");
157 system("grep -H . /proc/sys/net/core/bpf_jit_harden");
160 samples = strtoull(argv[1], NULL, 0);
162 samples = calibrate();
164 ksft_print_msg("Benchmarking %llu syscalls...\n", samples);
167 native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
168 ksft_print_msg("getpid native: %llu ns\n", native);
170 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
173 /* One filter resulting in a bitmap */
174 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
177 bitmap1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
178 ksft_print_msg("getpid RET_ALLOW 1 filter (bitmap): %llu ns\n", bitmap1);
180 /* Second filter resulting in a bitmap */
181 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
184 bitmap2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
185 ksft_print_msg("getpid RET_ALLOW 2 filters (bitmap): %llu ns\n", bitmap2);
187 /* Third filter, can no longer be converted to bitmap */
188 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
191 filter1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
192 ksft_print_msg("getpid RET_ALLOW 3 filters (full): %llu ns\n", filter1);
194 /* Fourth filter, can not be converted to bitmap because of filter 3 */
195 ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
198 filter2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
199 ksft_print_msg("getpid RET_ALLOW 4 filters (full): %llu ns\n", filter2);
202 #define ESTIMATE(fmt, var, what) do { \
204 ksft_print_msg("Estimated " fmt ": %llu ns\n", var); \
209 ESTIMATE("total seccomp overhead for 1 bitmapped filter", calc,
211 ESTIMATE("total seccomp overhead for 2 bitmapped filters", calc,
213 ESTIMATE("total seccomp overhead for 3 full filters", calc,
215 ESTIMATE("total seccomp overhead for 4 full filters", calc,
217 ESTIMATE("seccomp entry overhead", entry,
218 bitmap1 - native - (bitmap2 - bitmap1));
219 ESTIMATE("seccomp per-filter overhead (last 2 diff)", per_filter1,
221 ESTIMATE("seccomp per-filter overhead (filters / 4)", per_filter2,
222 (filter2 - native - entry) / 4);
224 ksft_print_msg("Expectations:\n");
225 ret |= compare("native", "≤", "1 bitmap", native, le, bitmap1);
226 bits = compare("native", "≤", "1 filter", native, le, filter1);
230 ret |= compare("per-filter (last 2 diff)", "≈", "per-filter (filters / 4)",
231 per_filter1, approx, per_filter2);
233 bits = compare("1 bitmapped", "≈", "2 bitmapped",
234 bitmap1 - native, approx, bitmap2 - native);
236 ksft_print_msg("Skipping constant action bitmap expectations: they appear unsupported.\n");
240 ret |= compare("entry", "≈", "1 bitmapped", entry, approx, bitmap1 - native);
241 ret |= compare("entry", "≈", "2 bitmapped", entry, approx, bitmap2 - native);
242 ret |= compare("native + entry + (per filter * 4)", "≈", "4 filters total",
243 entry + (per_filter1 * 4) + native, approx, filter2);
248 ksft_print_msg("Saw unexpected benchmark result. Try running again with more samples?\n");