Skip to content

Commit d633d57

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
selftest/bpf: Add test for allowed trampolines count
There's limit of 40 programs tht can be attached to trampoline for one function. Adding test that tries to attach that many plus one extra that needs to fail. Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent e9b4e60 commit d633d57

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#define _GNU_SOURCE
3+
#include <sched.h>
4+
#include <sys/prctl.h>
5+
#include <test_progs.h>
6+
7+
#define MAX_TRAMP_PROGS 40
8+
9+
struct inst {
10+
struct bpf_object *obj;
11+
struct bpf_link *link_fentry;
12+
struct bpf_link *link_fexit;
13+
};
14+
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)
34+
{
35+
struct bpf_program *prog;
36+
int duration = 0;
37+
38+
prog = bpf_object__find_program_by_title(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);
42+
}
43+
44+
void test_trampoline_count(void)
45+
{
46+
const char *fentry_name = "fentry/__set_task_comm";
47+
const char *fexit_name = "fexit/__set_task_comm";
48+
const char *object = "test_trampoline_count.o";
49+
struct inst inst[MAX_TRAMP_PROGS] = { 0 };
50+
int err, i = 0, duration = 0;
51+
struct bpf_object *obj;
52+
struct bpf_link *link;
53+
char comm[16] = {};
54+
55+
/* attach 'allowed' 40 trampoline programs */
56+
for (i = 0; i < MAX_TRAMP_PROGS; i++) {
57+
obj = bpf_object__open_file(object, NULL);
58+
if (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj)))
59+
goto cleanup;
60+
61+
err = bpf_object__load(obj);
62+
if (CHECK(err, "obj_load", "err %d\n", err))
63+
goto cleanup;
64+
inst[i].obj = obj;
65+
66+
if (rand() % 2) {
67+
link = load(obj, fentry_name);
68+
if (CHECK(IS_ERR(link), "attach prog", "err %ld\n", PTR_ERR(link)))
69+
goto cleanup;
70+
inst[i].link_fentry = link;
71+
} else {
72+
link = load(obj, fexit_name);
73+
if (CHECK(IS_ERR(link), "attach prog", "err %ld\n", PTR_ERR(link)))
74+
goto cleanup;
75+
inst[i].link_fexit = link;
76+
}
77+
}
78+
79+
/* and try 1 extra.. */
80+
obj = bpf_object__open_file(object, NULL);
81+
if (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj)))
82+
goto cleanup;
83+
84+
err = bpf_object__load(obj);
85+
if (CHECK(err, "obj_load", "err %d\n", err))
86+
goto cleanup_extra;
87+
88+
/* ..that needs to fail */
89+
link = load(obj, fentry_name);
90+
if (CHECK(!IS_ERR(link), "cannot attach over the limit", "err %ld\n", PTR_ERR(link))) {
91+
bpf_link__destroy(link);
92+
goto cleanup_extra;
93+
}
94+
95+
/* with E2BIG error */
96+
CHECK(PTR_ERR(link) != -E2BIG, "proper error check", "err %ld\n", PTR_ERR(link));
97+
98+
/* and finaly execute the probe */
99+
if (CHECK_FAIL(prctl(PR_GET_NAME, comm, 0L, 0L, 0L)))
100+
goto cleanup_extra;
101+
CHECK_FAIL(test_task_rename());
102+
CHECK_FAIL(prctl(PR_SET_NAME, comm, 0L, 0L, 0L));
103+
104+
cleanup_extra:
105+
bpf_object__close(obj);
106+
cleanup:
107+
while (--i) {
108+
bpf_link__destroy(inst[i].link_fentry);
109+
bpf_link__destroy(inst[i].link_fexit);
110+
bpf_object__close(inst[i].obj);
111+
}
112+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stdbool.h>
3+
#include <stddef.h>
4+
#include <linux/bpf.h>
5+
#include "bpf_trace_helpers.h"
6+
7+
struct task_struct;
8+
9+
SEC("fentry/__set_task_comm")
10+
int BPF_PROG(prog1, struct task_struct *tsk, const char *buf, bool exec)
11+
{
12+
return 0;
13+
}
14+
15+
SEC("fexit/__set_task_comm")
16+
int BPF_PROG(prog2, struct task_struct *tsk, const char *buf, bool exec)
17+
{
18+
return 0;
19+
}
20+
21+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)