Skip to content

Commit fa7f17d

Browse files
Hou Taoanakryiko
authored andcommitted
bpf/selftests: Add test for writable bare tracepoint
Add a writable bare tracepoint in bpf_testmod module, and trigger its calling when reading /sys/kernel/bpf_testmod with a specific buffer length. The reading will return the value in writable context if the early return flag is enabled in writable context. Signed-off-by: Hou Tao <houtao1@huawei.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20211004094857.30868-4-hotforest@gmail.com
1 parent ccaf12d commit fa7f17d

File tree

7 files changed

+83
-2
lines changed

7 files changed

+83
-2
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ DECLARE_TRACE(bpf_testmod_test_write_bare,
3434
TP_ARGS(task, ctx)
3535
);
3636

37+
#undef BPF_TESTMOD_DECLARE_TRACE
38+
#ifdef DECLARE_TRACE_WRITABLE
39+
#define BPF_TESTMOD_DECLARE_TRACE(call, proto, args, size) \
40+
DECLARE_TRACE_WRITABLE(call, PARAMS(proto), PARAMS(args), size)
41+
#else
42+
#define BPF_TESTMOD_DECLARE_TRACE(call, proto, args, size) \
43+
DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
44+
#endif
45+
46+
BPF_TESTMOD_DECLARE_TRACE(bpf_testmod_test_writable_bare,
47+
TP_PROTO(struct bpf_testmod_test_writable_ctx *ctx),
48+
TP_ARGS(ctx),
49+
sizeof(struct bpf_testmod_test_writable_ctx)
50+
);
51+
3752
#endif /* _BPF_TESTMOD_EVENTS_H */
3853

3954
#undef TRACE_INCLUDE_PATH

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
5050
if (bpf_testmod_loop_test(101) > 100)
5151
trace_bpf_testmod_test_read(current, &ctx);
5252

53+
/* Magic number to enable writable tp */
54+
if (len == 64) {
55+
struct bpf_testmod_test_writable_ctx writable = {
56+
.val = 1024,
57+
};
58+
trace_bpf_testmod_test_writable_bare(&writable);
59+
if (writable.early_ret)
60+
return snprintf(buf, len, "%d\n", writable.val);
61+
}
62+
5363
return -EIO; /* always fail */
5464
}
5565
EXPORT_SYMBOL(bpf_testmod_test_read);

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ struct bpf_testmod_test_write_ctx {
1717
size_t len;
1818
};
1919

20+
struct bpf_testmod_test_writable_ctx {
21+
bool early_ret;
22+
int val;
23+
};
24+
2025
#endif /* _BPF_TESTMOD_H */

tools/testing/selftests/bpf/prog_tests/module_attach.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,36 @@
22
/* Copyright (c) 2020 Facebook */
33

44
#include <test_progs.h>
5+
#include <stdbool.h>
56
#include "test_module_attach.skel.h"
67

78
static int duration;
89

10+
static int trigger_module_test_writable(int *val)
11+
{
12+
int fd, err;
13+
char buf[65];
14+
ssize_t rd;
15+
16+
fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
17+
err = -errno;
18+
if (!ASSERT_GE(fd, 0, "testmode_file_open"))
19+
return err;
20+
21+
rd = read(fd, buf, sizeof(buf) - 1);
22+
err = -errno;
23+
if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) {
24+
close(fd);
25+
return err;
26+
}
27+
28+
buf[rd] = '\0';
29+
*val = strtol(buf, NULL, 0);
30+
close(fd);
31+
32+
return 0;
33+
}
34+
935
static int delete_module(const char *name, int flags)
1036
{
1137
return syscall(__NR_delete_module, name, flags);
@@ -19,6 +45,7 @@ void test_module_attach(void)
1945
struct test_module_attach__bss *bss;
2046
struct bpf_link *link;
2147
int err;
48+
int writable_val = 0;
2249

2350
skel = test_module_attach__open();
2451
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
@@ -51,6 +78,14 @@ void test_module_attach(void)
5178
ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet");
5279
ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret");
5380

81+
bss->raw_tp_writable_bare_early_ret = true;
82+
bss->raw_tp_writable_bare_out_val = 0xf1f2f3f4;
83+
ASSERT_OK(trigger_module_test_writable(&writable_val),
84+
"trigger_writable");
85+
ASSERT_EQ(bss->raw_tp_writable_bare_in_val, 1024, "writable_test_in");
86+
ASSERT_EQ(bss->raw_tp_writable_bare_out_val, writable_val,
87+
"writable_test_out");
88+
5489
test_module_attach__detach(skel);
5590

5691
/* attach fentry/fexit and make sure it get's module reference */

tools/testing/selftests/bpf/progs/test_module_attach.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ int BPF_PROG(handle_raw_tp_bare,
2727
return 0;
2828
}
2929

30+
int raw_tp_writable_bare_in_val = 0;
31+
int raw_tp_writable_bare_early_ret = 0;
32+
int raw_tp_writable_bare_out_val = 0;
33+
34+
SEC("raw_tp.w/bpf_testmod_test_writable_bare")
35+
int BPF_PROG(handle_raw_tp_writable_bare,
36+
struct bpf_testmod_test_writable_ctx *writable)
37+
{
38+
raw_tp_writable_bare_in_val = writable->val;
39+
writable->early_ret = raw_tp_writable_bare_early_ret;
40+
writable->val = raw_tp_writable_bare_out_val;
41+
return 0;
42+
}
43+
3044
__u32 tp_btf_read_sz = 0;
3145

3246
SEC("tp_btf/bpf_testmod_test_read")

tools/testing/selftests/bpf/test_progs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ int trigger_module_test_read(int read_sz)
747747
{
748748
int fd, err;
749749

750-
fd = open("/sys/kernel/bpf_testmod", O_RDONLY);
750+
fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
751751
err = -errno;
752752
if (!ASSERT_GE(fd, 0, "testmod_file_open"))
753753
return err;
@@ -769,7 +769,7 @@ int trigger_module_test_write(int write_sz)
769769
memset(buf, 'a', write_sz);
770770
buf[write_sz-1] = '\0';
771771

772-
fd = open("/sys/kernel/bpf_testmod", O_WRONLY);
772+
fd = open(BPF_TESTMOD_TEST_FILE, O_WRONLY);
773773
err = -errno;
774774
if (!ASSERT_GE(fd, 0, "testmod_file_open")) {
775775
free(buf);

tools/testing/selftests/bpf/test_progs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,5 @@ int trigger_module_test_write(int write_sz);
301301
#else
302302
#define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
303303
#endif
304+
305+
#define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"

0 commit comments

Comments
 (0)