Skip to content

Commit c5f8860

Browse files
Qais Yousefkernel-patches-bot
authored andcommitted
selftests: bpf: Add a new test for bare tracepoints
Reuse module_attach infrastructure to add a new bare tracepoint to check we can attach to it as a raw tracepoint. Signed-off-by: Qais Yousef <qais.yousef@arm.com>
1 parent 457107d commit c5f8860

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ TRACE_EVENT(bpf_testmod_test_read,
2828
__entry->pid, __entry->comm, __entry->off, __entry->len)
2929
);
3030

31+
/* A bare tracepoint with no event associated with it */
32+
DECLARE_TRACE(bpf_testmod_test_write_bare,
33+
TP_PROTO(struct task_struct *task, struct bpf_testmod_test_write_ctx *ctx),
34+
TP_ARGS(task, ctx)
35+
);
36+
3137
#endif /* _BPF_TESTMOD_EVENTS_H */
3238

3339
#undef TRACE_INCLUDE_PATH

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,28 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
3131
EXPORT_SYMBOL(bpf_testmod_test_read);
3232
ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
3333

34+
noinline ssize_t
35+
bpf_testmod_test_write(struct file *file, struct kobject *kobj,
36+
struct bin_attribute *bin_attr,
37+
char *buf, loff_t off, size_t len)
38+
{
39+
struct bpf_testmod_test_write_ctx ctx = {
40+
.buf = buf,
41+
.off = off,
42+
.len = len,
43+
};
44+
45+
trace_bpf_testmod_test_write_bare(current, &ctx);
46+
47+
return -EIO; /* always fail */
48+
}
49+
EXPORT_SYMBOL(bpf_testmod_test_write);
50+
ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
51+
3452
static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
35-
.attr = { .name = "bpf_testmod", .mode = 0444, },
53+
.attr = { .name = "bpf_testmod", .mode = 0666, },
3654
.read = bpf_testmod_test_read,
55+
.write = bpf_testmod_test_write,
3756
};
3857

3958
static int bpf_testmod_init(void)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ struct bpf_testmod_test_read_ctx {
1111
size_t len;
1212
};
1313

14+
struct bpf_testmod_test_write_ctx {
15+
char *buf;
16+
loff_t off;
17+
size_t len;
18+
};
19+
1420
#endif /* _BPF_TESTMOD_H */

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,34 @@ static int trigger_module_test_read(int read_sz)
2121
return 0;
2222
}
2323

24+
static int trigger_module_test_write(int write_sz)
25+
{
26+
int fd, err;
27+
char *buf = malloc(write_sz);
28+
29+
if (!buf)
30+
return -ENOMEM;
31+
32+
memset(buf, 'a', write_sz);
33+
buf[write_sz-1] = '\0';
34+
35+
fd = open("/sys/kernel/bpf_testmod", O_WRONLY);
36+
err = -errno;
37+
if (CHECK(fd < 0, "testmod_file_open", "failed: %d\n", err))
38+
goto out;
39+
40+
write(fd, buf, write_sz);
41+
close(fd);
42+
out:
43+
free(buf);
44+
45+
return 0;
46+
}
47+
2448
void test_module_attach(void)
2549
{
2650
const int READ_SZ = 456;
51+
const int WRITE_SZ = 457;
2752
struct test_module_attach* skel;
2853
struct test_module_attach__bss *bss;
2954
int err;
@@ -48,8 +73,10 @@ void test_module_attach(void)
4873

4974
/* trigger tracepoint */
5075
ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read");
76+
ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write");
5177

5278
ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
79+
ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare");
5380
ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
5481
ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
5582
ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ int BPF_PROG(handle_raw_tp,
1717
return 0;
1818
}
1919

20+
__u32 raw_tp_bare_write_sz = 0;
21+
22+
SEC("raw_tp/bpf_testmod_test_write_bare")
23+
int BPF_PROG(handle_raw_tp_bare,
24+
struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx)
25+
{
26+
raw_tp_bare_write_sz = BPF_CORE_READ(write_ctx, len);
27+
return 0;
28+
}
29+
2030
__u32 tp_btf_read_sz = 0;
2131

2232
SEC("tp_btf/bpf_testmod_test_read")

0 commit comments

Comments
 (0)