Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf test: Add landlock workload

We'll use it to add a regression test for the BTF augmentation of enum
arguments for tracepoints in 'perf trace':

root@x1:~# perf trace -e landlock_add_rule perf test -w landlock
0.000 ( 0.009 ms): perf/747160 landlock_add_rule(ruleset_fd: 11, rule_type: LANDLOCK_RULE_PATH_BENEATH, rule_attr: 0x7ffd8e258594, flags: 45) = -1 EINVAL (Invalid argument)
0.011 ( 0.002 ms): perf/747160 landlock_add_rule(ruleset_fd: 11, rule_type: LANDLOCK_RULE_NET_PORT, rule_attr: 0x7ffd8e2585a0, flags: 45) = -1 EINVAL (Invalid argument)
root@x1:~#

Committer notes:

It was agreed on the discussion (see Link below) to shorten then name of
the workload from 'landlock_add_rule' to 'landlock', and I moved it to a
separate patch.

Also, to address a build failure from Namhyung, I stopped loading
linux/landlock.h and instead added the used defines, enums and types to
make this build in older systems. All we want is to emit the syscall and
intercept it.

Suggested-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Signed-off-by: Howard Chu <howardchu95@gmail.com>
Tested-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/CAH0uvohaypdTV6Z7O5QSK+va_qnhZ6BP6oSJ89s1c1E0CjgxDA@mail.gmail.com
Link: https://lore.kernel.org/r/20240624181345.124764-1-howardchu95@gmail.com
Link: https://lore.kernel.org/r/20240624181345.124764-6-howardchu95@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Howard Chu and committed by
Arnaldo Carvalho de Melo
3656e566 95586588

+69
+1
tools/perf/tests/builtin-test.c
··· 152 152 &workload__sqrtloop, 153 153 &workload__brstack, 154 154 &workload__datasym, 155 + &workload__landlock, 155 156 }; 156 157 157 158 static int num_subtests(const struct test_suite *t)
+1
tools/perf/tests/tests.h
··· 205 205 DECLARE_WORKLOAD(sqrtloop); 206 206 DECLARE_WORKLOAD(brstack); 207 207 DECLARE_WORKLOAD(datasym); 208 + DECLARE_WORKLOAD(landlock); 208 209 209 210 extern const char *dso_to_test; 210 211 extern const char *test_objdump_path;
+1
tools/perf/tests/workloads/Build
··· 6 6 perf-test-y += sqrtloop.o 7 7 perf-test-y += brstack.o 8 8 perf-test-y += datasym.o 9 + perf-test-y += landlock.o 9 10 10 11 CFLAGS_sqrtloop.o = -g -O0 -fno-inline -U_FORTIFY_SOURCE 11 12 CFLAGS_leafloop.o = -g -O0 -fno-inline -fno-omit-frame-pointer -U_FORTIFY_SOURCE
+66
tools/perf/tests/workloads/landlock.c
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #include <linux/compiler.h> 3 + #include <linux/types.h> 4 + #include <unistd.h> 5 + #include "../tests.h" 6 + 7 + /* This workload was initially added to test enum augmentation with BTF in perf 8 + * trace because its the only syscall that has an enum argument. Since it is 9 + * a recent addition to the Linux kernel (at the time of the introduction of this 10 + * 'perf test' workload) we just add the required types and defines here instead 11 + * of including linux/landlock, that isn't available in older systems. 12 + * 13 + * We are not interested in the the result of the syscall, just in intercepting 14 + * its arguments. 15 + */ 16 + 17 + #ifndef __NR_landlock_add_rule 18 + #define __NR_landlock_add_rule 445 19 + #endif 20 + 21 + #ifndef LANDLOCK_ACCESS_FS_READ_FILE 22 + #define LANDLOCK_ACCESS_FS_READ_FILE (1ULL << 2) 23 + 24 + #define LANDLOCK_RULE_PATH_BENEATH 1 25 + 26 + struct landlock_path_beneath_attr { 27 + __u64 allowed_access; 28 + __s32 parent_fd; 29 + }; 30 + #endif 31 + 32 + #ifndef LANDLOCK_ACCESS_NET_CONNECT_TCP 33 + #define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1) 34 + 35 + #define LANDLOCK_RULE_NET_PORT 2 36 + 37 + struct landlock_net_port_attr { 38 + __u64 allowed_access; 39 + __u64 port; 40 + }; 41 + #endif 42 + 43 + static int landlock(int argc __maybe_unused, const char **argv __maybe_unused) 44 + { 45 + int fd = 11, flags = 45; 46 + 47 + struct landlock_path_beneath_attr path_beneath_attr = { 48 + .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE, 49 + .parent_fd = 14, 50 + }; 51 + 52 + struct landlock_net_port_attr net_port_attr = { 53 + .port = 19, 54 + .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP, 55 + }; 56 + 57 + syscall(__NR_landlock_add_rule, fd, LANDLOCK_RULE_PATH_BENEATH, 58 + &path_beneath_attr, flags); 59 + 60 + syscall(__NR_landlock_add_rule, fd, LANDLOCK_RULE_NET_PORT, 61 + &net_port_attr, flags); 62 + 63 + return 0; 64 + } 65 + 66 + DEFINE_WORKLOAD(landlock);