Skip to content

Commit

Permalink
perf test: Add landlock workload
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Howard Chu <[email protected]>
Tested-by: Arnaldo Carvalho de Melo <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Ian Rogers <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Kan Liang <[email protected]>
Cc: Namhyung Kim <[email protected]>
Link: https://lore.kernel.org/lkml/CAH0uvohaypdTV6Z7O5QSK+va_qnhZ6BP6oSJ89s1c1E0CjgxDA@mail.gmail.com
Link: https://lore.kernel.org/r/[email protected]
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
Sberm authored and acmel committed Jul 31, 2024
1 parent 9558658 commit 3656e56
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/perf/tests/builtin-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static struct test_workload *workloads[] = {
&workload__sqrtloop,
&workload__brstack,
&workload__datasym,
&workload__landlock,
};

static int num_subtests(const struct test_suite *t)
Expand Down
1 change: 1 addition & 0 deletions tools/perf/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ DECLARE_WORKLOAD(leafloop);
DECLARE_WORKLOAD(sqrtloop);
DECLARE_WORKLOAD(brstack);
DECLARE_WORKLOAD(datasym);
DECLARE_WORKLOAD(landlock);

extern const char *dso_to_test;
extern const char *test_objdump_path;
Expand Down
1 change: 1 addition & 0 deletions tools/perf/tests/workloads/Build
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ perf-test-y += leafloop.o
perf-test-y += sqrtloop.o
perf-test-y += brstack.o
perf-test-y += datasym.o
perf-test-y += landlock.o

CFLAGS_sqrtloop.o = -g -O0 -fno-inline -U_FORTIFY_SOURCE
CFLAGS_leafloop.o = -g -O0 -fno-inline -fno-omit-frame-pointer -U_FORTIFY_SOURCE
Expand Down
66 changes: 66 additions & 0 deletions tools/perf/tests/workloads/landlock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/compiler.h>
#include <linux/types.h>
#include <unistd.h>
#include "../tests.h"

/* This workload was initially added to test enum augmentation with BTF in perf
* trace because its the only syscall that has an enum argument. Since it is
* a recent addition to the Linux kernel (at the time of the introduction of this
* 'perf test' workload) we just add the required types and defines here instead
* of including linux/landlock, that isn't available in older systems.
*
* We are not interested in the the result of the syscall, just in intercepting
* its arguments.
*/

#ifndef __NR_landlock_add_rule
#define __NR_landlock_add_rule 445
#endif

#ifndef LANDLOCK_ACCESS_FS_READ_FILE
#define LANDLOCK_ACCESS_FS_READ_FILE (1ULL << 2)

#define LANDLOCK_RULE_PATH_BENEATH 1

struct landlock_path_beneath_attr {
__u64 allowed_access;
__s32 parent_fd;
};
#endif

#ifndef LANDLOCK_ACCESS_NET_CONNECT_TCP
#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1)

#define LANDLOCK_RULE_NET_PORT 2

struct landlock_net_port_attr {
__u64 allowed_access;
__u64 port;
};
#endif

static int landlock(int argc __maybe_unused, const char **argv __maybe_unused)
{
int fd = 11, flags = 45;

struct landlock_path_beneath_attr path_beneath_attr = {
.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE,
.parent_fd = 14,
};

struct landlock_net_port_attr net_port_attr = {
.port = 19,
.allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
};

syscall(__NR_landlock_add_rule, fd, LANDLOCK_RULE_PATH_BENEATH,
&path_beneath_attr, flags);

syscall(__NR_landlock_add_rule, fd, LANDLOCK_RULE_NET_PORT,
&net_port_attr, flags);

return 0;
}

DEFINE_WORKLOAD(landlock);

0 comments on commit 3656e56

Please sign in to comment.