History

selftests/bpf: Add missing trampoline program type to trampoline_count test

Currently the trampoline_count test doesn't include any fmod_ret bpf programs, fix it to make the test cover all possible trampoline program types. Since fmod_ret bpf programs can't be attached to __set_task_comm function, as it's neither whitelisted for error injection nor a security hook, change it to bpf_modify_return_test. This patch also does some other cleanups such as removing duplicate code, dropping inconsistent comments, etc.

Yuntao Wang committed 4 years ago

Commit b23316a1 parent 96af42c3 files · +61 −91Browse files

Unified · Split

3 files
  1. include/linux/bpf.h +1 −1
  2. tools/testing/selftests/bpf/prog_tests/trampoline_count.c +51 −83
  3. tools/testing/selftests/bpf/progs/test_trampoline_count.c +9 −7
include/linux/bpf.h+1−1
@@ -724,7 +724,7 @@ struct btf_func_model { · 723 unmodified lines
724724 #define BPF_TRAMP_F_RET_FENTRY_RET BIT(4)
725725
726726 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
727− * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2
727+ * bytes on x86.
728728 */
729729 #define BPF_MAX_TRAMP_LINKS 38
730730
tools/testing/selftests/bpf/prog_tests/trampoline_count.c+51−83
@@ -1,126 +1,94 @@
11 // SPDX-License-Identifier: GPL-2.0-only
22 #define _GNU_SOURCE
3−#include <sched.h>
4−#include <sys/prctl.h>
53 #include <test_progs.h>
64
75 #define MAX_TRAMP_PROGS 38
86
97 struct inst {
108 struct bpf_object *obj;
11− struct bpf_link *link_fentry;
12− struct bpf_link *link_fexit;
9+ struct bpf_link *link;
1310 };
1411
15−static int test_task_rename(void)
16−{
17− int fd, duration = 0, err;
18− char buf[] = "test_overhead";
19−
20− fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
21− if (CHECK(fd < 0, "open /proc", "err %d", errno))
22− return -1;
23− err = write(fd, buf, sizeof(buf));
24− if (err < 0) {
25− CHECK(err < 0, "task rename", "err %d", errno);
26− close(fd);
27− return -1;
28− }
29− close(fd);
30− return 0;
31−}
32−
33−static struct bpf_link *load(struct bpf_object *obj, const char *name)
12+static struct bpf_program *load_prog(char *file, char *name, struct inst *inst)
3413 {
14+ struct bpf_object *obj;
3515 struct bpf_program *prog;
36− int duration = 0;
16+ int err;
17+
18+ obj = bpf_object__open_file(file, NULL);
19+ if (!ASSERT_OK_PTR(obj, "obj_open_file"))
20+ return NULL;
21+
22+ inst->obj = obj;
23+
24+ err = bpf_object__load(obj);
25+ if (!ASSERT_OK(err, "obj_load"))
26+ return NULL;
3727
3828 prog = bpf_object__find_program_by_name(obj, name);
39− if (CHECK(!prog, "find_probe", "prog '%s' not found\n", name))
40− return ERR_PTR(-EINVAL);
41− return bpf_program__attach_trace(prog);
29+ if (!ASSERT_OK_PTR(prog, "obj_find_prog"))
30+ return NULL;
31+
32+ return prog;
4233 }
4334
4435 /* TODO: use different target function to run in concurrent mode */
4536 void serial_test_trampoline_count(void)
4637 {
47− const char *fentry_name = "prog1";
48− const char *fexit_name = "prog2";
49− const char *object = "test_trampoline_count.o";
50− struct inst inst[MAX_TRAMP_PROGS] = {};
51− int err, i = 0, duration = 0;
52− struct bpf_object *obj;
38+ char *file = "test_trampoline_count.o";
39+ char *const progs[] = { "fentry_test", "fmod_ret_test", "fexit_test" };
40+ struct inst inst[MAX_TRAMP_PROGS + 1] = {};
41+ struct bpf_program *prog;
5342 struct bpf_link *link;
54− char comm[16] = {};
43+ int prog_fd, err, i;
44+ LIBBPF_OPTS(bpf_test_run_opts, opts);
5545
5646 /* attach 'allowed' trampoline programs */
5747 for (i = 0; i < MAX_TRAMP_PROGS; i++) {
58− obj = bpf_object__open_file(object, NULL);
59− if (!ASSERT_OK_PTR(obj, "obj_open_file")) {
60− obj = NULL;
48+ prog = load_prog(file, progs[i % ARRAY_SIZE(progs)], &inst[i]);
49+ if (!prog)
6150 goto cleanup;
62− }
6351
64− err = bpf_object__load(obj);
65− if (CHECK(err, "obj_load", "err %d\n", err))
52+ link = bpf_program__attach(prog);
53+ if (!ASSERT_OK_PTR(link, "attach_prog"))
6654 goto cleanup;
67− inst[i].obj = obj;
68− obj = NULL;
69−
70− if (rand() % 2) {
71− link = load(inst[i].obj, fentry_name);
72− if (!ASSERT_OK_PTR(link, "attach_prog")) {
73− link = NULL;
74− goto cleanup;
75− }
76− inst[i].link_fentry = link;
77− } else {
78− link = load(inst[i].obj, fexit_name);
79− if (!ASSERT_OK_PTR(link, "attach_prog")) {
80− link = NULL;
81− goto cleanup;
82− }
83− inst[i].link_fexit = link;
84− }
55+
56+ inst[i].link = link;
8557 }
8658
8759 /* and try 1 extra.. */
88− obj = bpf_object__open_file(object, NULL);
89− if (!ASSERT_OK_PTR(obj, "obj_open_file")) {
90− obj = NULL;
60+ prog = load_prog(file, "fmod_ret_test", &inst[i]);
61+ if (!prog)
9162 goto cleanup;
92− }
93−
94− err = bpf_object__load(obj);
95− if (CHECK(err, "obj_load", "err %d\n", err))
96− goto cleanup_extra;
9763
9864 /* ..that needs to fail */
99− link = load(obj, fentry_name);
100− err = libbpf_get_error(link);
101− if (!ASSERT_ERR_PTR(link, "cannot attach over the limit")) {
102− bpf_link__destroy(link);
103− goto cleanup_extra;
65+ link = bpf_program__attach(prog);
66+ if (!ASSERT_ERR_PTR(link, "attach_prog")) {
67+ inst[i].link = link;
68+ goto cleanup;
10469 }
10570
10671 /* with E2BIG error */
107− ASSERT_EQ(err, -E2BIG, "proper error check");
108− ASSERT_EQ(link, NULL, "ptr_is_null");
72+ if (!ASSERT_EQ(libbpf_get_error(link), -E2BIG, "E2BIG"))
73+ goto cleanup;
74+ if (!ASSERT_EQ(link, NULL, "ptr_is_null"))
75+ goto cleanup;
10976
11077 /* and finaly execute the probe */
111− if (CHECK_FAIL(prctl(PR_GET_NAME, comm, 0L, 0L, 0L)))
112− goto cleanup_extra;
113− CHECK_FAIL(test_task_rename());
114− CHECK_FAIL(prctl(PR_SET_NAME, comm, 0L, 0L, 0L));
78+ prog_fd = bpf_program__fd(prog);
79+ if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd"))
80+ goto cleanup;
81+
82+ err = bpf_prog_test_run_opts(prog_fd, &opts);
83+ if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
84+ goto cleanup;
85+
86+ ASSERT_EQ(opts.retval & 0xffff, 4, "bpf_modify_return_test.result");
87+ ASSERT_EQ(opts.retval >> 16, 1, "bpf_modify_return_test.side_effect");
11588
116−cleanup_extra:
117− bpf_object__close(obj);
11889 cleanup:
119− if (i >= MAX_TRAMP_PROGS)
120− i = MAX_TRAMP_PROGS - 1;
12190 for (; i >= 0; i--) {
122− bpf_link__destroy(inst[i].link_fentry);
123− bpf_link__destroy(inst[i].link_fexit);
91+ bpf_link__destroy(inst[i].link);
12492 bpf_object__close(inst[i].obj);
12593 }
12694 }
tools/testing/selftests/bpf/progs/test_trampoline_count.c+9−7
@@ -1,20 +1,22 @@
11 // SPDX-License-Identifier: GPL-2.0
2−#include <stdbool.h>
3−#include <stddef.h>
42 #include <linux/bpf.h>
53 #include <bpf/bpf_helpers.h>
64 #include <bpf/bpf_tracing.h>
75
8−struct task_struct;
6+SEC("fentry/bpf_modify_return_test")
7+int BPF_PROG(fentry_test, int a, int *b)
8+{
9+ return 0;
10+}
911
10−SEC("fentry/__set_task_comm")
11−int BPF_PROG(prog1, struct task_struct *tsk, const char *buf, bool exec)
12+SEC("fmod_ret/bpf_modify_return_test")
13+int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
1214 {
1315 return 0;
1416 }
1517
16−SEC("fexit/__set_task_comm")
17−int BPF_PROG(prog2, struct task_struct *tsk, const char *buf, bool exec)
18+SEC("fexit/bpf_modify_return_test")
19+int BPF_PROG(fexit_test, int a, int *b, int ret)
1820 {
1921 return 0;
2022 }