Skip to content

Commit 8edc0c6

Browse files
author
Alexei Starovoitov
committed
Merge branch 'Allow attaching to bare tracepoints'
Qais Yousef says: ==================== Changes in v3: * Fix not returning error value correctly in trigger_module_test_write() (Yonghong) * Add Yonghong acked-by to patch 1. Changes in v2: * Fix compilation error. (Andrii) * Make the new test use write() instead of read() (Andrii) Add some missing glue logic to teach bpf about bare tracepoints - tracepoints without any trace event associated with them. Bare tracepoints are declare with DECLARE_TRACE(). Full tracepoints are declare with TRACE_EVENT(). BPF can attach to these tracepoints as RAW_TRACEPOINT() only as there're no events in tracefs created with them. ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 3a6984e + 1053cbb commit 8edc0c6

File tree

7 files changed

+85
-3
lines changed

7 files changed

+85
-3
lines changed

Documentation/bpf/bpf_design_QA.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ data structures and compile with kernel internal headers. Both of these
208208
kernel internals are subject to change and can break with newer kernels
209209
such that the program needs to be adapted accordingly.
210210

211+
Q: Are tracepoints part of the stable ABI?
212+
------------------------------------------
213+
A: NO. Tracepoints are tied to internal implementation details hence they are
214+
subject to change and can break with newer kernels. BPF programs need to change
215+
accordingly when this happens.
216+
211217
Q: How much stack space a BPF program uses?
212218
-------------------------------------------
213219
A: Currently all program types are limited to 512 bytes of stack

include/trace/bpf_probe.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,18 @@
5555
/* tracepoints with more than 12 arguments will hit build error */
5656
#define CAST_TO_U64(...) CONCATENATE(__CAST, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)
5757

58-
#undef DECLARE_EVENT_CLASS
59-
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
58+
#define __BPF_DECLARE_TRACE(call, proto, args) \
6059
static notrace void \
6160
__bpf_trace_##call(void *__data, proto) \
6261
{ \
6362
struct bpf_prog *prog = __data; \
6463
CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(prog, CAST_TO_U64(args)); \
6564
}
6665

66+
#undef DECLARE_EVENT_CLASS
67+
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
68+
__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
69+
6770
/*
6871
* This part is compiled out, it is only here as a build time check
6972
* to make sure that if the tracepoint handling changes, the
@@ -111,6 +114,11 @@ __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
111114
#define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
112115
DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
113116

117+
#undef DECLARE_TRACE
118+
#define DECLARE_TRACE(call, proto, args) \
119+
__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
120+
__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
121+
114122
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
115123

116124
#undef DEFINE_EVENT_WRITABLE

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+
free(buf);
39+
return err;
40+
}
41+
42+
write(fd, buf, write_sz);
43+
close(fd);
44+
free(buf);
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)