~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/bench/sched-seccomp-notify.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 <uapi/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 synchronous 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 }
179 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php