Skip to content

Commit d3a37ea

Browse files
committed
selftests/seccomp: Expand benchmark to per-filter measurements
It's useful to see how much (at a minimum) each filter adds to the syscall overhead. Add additional calculations. Signed-off-by: Kees Cook <[email protected]>
1 parent ad56821 commit d3a37ea

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

tools/testing/selftests/seccomp/seccomp_benchmark.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,54 @@ int main(int argc, char *argv[])
6868
};
6969
long ret;
7070
unsigned long long samples;
71-
unsigned long long native, filtered;
71+
unsigned long long native, filter1, filter2;
7272

7373
if (argc > 1)
7474
samples = strtoull(argv[1], NULL, 0);
7575
else
7676
samples = calibrate();
7777

78+
printf("Current BPF sysctl settings:\n");
79+
system("sysctl net.core.bpf_jit_enable");
80+
system("sysctl net.core.bpf_jit_harden");
7881
printf("Benchmarking %llu samples...\n", samples);
7982

83+
/* Native call */
8084
native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
8185
printf("getpid native: %llu ns\n", native);
8286

8387
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
8488
assert(ret == 0);
8589

90+
/* One filter */
8691
ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
8792
assert(ret == 0);
8893

89-
filtered = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
90-
printf("getpid RET_ALLOW: %llu ns\n", filtered);
94+
filter1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
95+
printf("getpid RET_ALLOW 1 filter: %llu ns\n", filter1);
9196

92-
printf("Estimated seccomp overhead per syscall: %llu ns\n",
93-
filtered - native);
97+
if (filter1 == native)
98+
printf("No overhead measured!? Try running again with more samples.\n");
9499

95-
if (filtered == native)
96-
printf("Trying running again with more samples.\n");
100+
/* Two filters */
101+
ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
102+
assert(ret == 0);
103+
104+
filter2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
105+
printf("getpid RET_ALLOW 2 filters: %llu ns\n", filter2);
106+
107+
/* Calculations */
108+
printf("Estimated total seccomp overhead for 1 filter: %llu ns\n",
109+
filter1 - native);
110+
111+
printf("Estimated total seccomp overhead for 2 filters: %llu ns\n",
112+
filter2 - native);
113+
114+
printf("Estimated seccomp per-filter overhead: %llu ns\n",
115+
filter2 - filter1);
116+
117+
printf("Estimated seccomp entry overhead: %llu ns\n",
118+
filter1 - native - (filter2 - filter1));
97119

98120
return 0;
99121
}

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3840,15 +3840,13 @@ TEST(user_notification_filter_empty_threaded)
38403840

38413841
/*
38423842
* TODO:
3843-
* - add microbenchmarks
38443843
* - expand NNP testing
38453844
* - better arch-specific TRACE and TRAP handlers.
38463845
* - endianness checking when appropriate
38473846
* - 64-bit arg prodding
38483847
* - arch value testing (x86 modes especially)
38493848
* - verify that FILTER_FLAG_LOG filters generate log messages
38503849
* - verify that RET_LOG generates log messages
3851-
* - ...
38523850
*/
38533851

38543852
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)