Skip to content

Commit 7d5cb68

Browse files
avaginkees
authored andcommitted
perf/benchmark: add a new benchmark for seccom_unotify
The benchmark is similar to the pipe benchmark. It creates two processes, one is calling syscalls, and another process is handling them via seccomp user notifications. It measures the time required to run a specified number of interations. $ ./perf bench sched seccomp-notify --sync-mode --loop 1000000 # Running 'sched/seccomp-notify' benchmark: # Executed 1000000 system calls Total time: 2.769 [sec] 2.769629 usecs/op 361059 ops/sec $ ./perf bench sched seccomp-notify # Running 'sched/seccomp-notify' benchmark: # Executed 1000000 system calls Total time: 8.571 [sec] 8.571119 usecs/op 116670 ops/sec Signed-off-by: Andrei Vagin <[email protected]> Acked-by: "Peter Zijlstra (Intel)" <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] [kees: Added PRIu64 format string] Signed-off-by: Kees Cook <[email protected]>
1 parent 8feae5a commit 7d5cb68

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed

tools/arch/x86/include/uapi/asm/unistd_32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@
2626
#ifndef __NR_setns
2727
#define __NR_setns 346
2828
#endif
29+
#ifdef __NR_seccomp
30+
#define __NR_seccomp 354
31+
#endif

tools/arch/x86/include/uapi/asm/unistd_64.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@
2626
#ifndef __NR_getcpu
2727
#define __NR_getcpu 309
2828
#endif
29+
#ifndef __NR_seccomp
30+
#define __NR_seccomp 317
31+
#endif

tools/perf/bench/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
perf-y += sched-messaging.o
22
perf-y += sched-pipe.o
3+
perf-y += sched-seccomp-notify.o
34
perf-y += syscall.o
45
perf-y += mem-functions.o
56
perf-y += futex-hash.o

tools/perf/bench/bench.h

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

tools/perf/builtin-bench.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static struct bench numa_benchmarks[] = {
4747
static struct bench sched_benchmarks[] = {
4848
{ "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging },
4949
{ "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe },
50+
{ "seccomp-notify", "Benchmark for seccomp user notify", bench_sched_seccomp_notify},
5051
{ "all", "Run all scheduler benchmarks", NULL },
5152
{ NULL, NULL, NULL }
5253
};

0 commit comments

Comments
 (0)