Skip to content

Commit a1852ce

Browse files
committed
Merge branch 'add support for writable bare tracepoint'
Hou Tao says: ==================== From: Hou Tao <houtao1@huawei.com> Hi, The patchset series supports writable context for bare tracepoint. The main idea comes from patchset "writable contexts for bpf raw tracepoints" [1], but it only supports normal tracepoint with associated trace event under tracefs. Now we have one use case in which we add bare tracepoint in VFS layer, and update file::f_mode for specific files. The reason using bare tracepoint is that it doesn't form a ABI and we can change it freely. So add support for it in BPF. Comments are always welcome. [1]: https://lore.kernel.org/lkml/20190426184951.21812-1-mmullins@fb.com Change log: v5: * rebased on bpf-next * patch 1: add Acked-by tag * patch 2: handle invalid section name, make prefixes array being const v4: https://www.spinics.net/lists/bpf/msg47021.html * rebased on bpf-next * update patch 2 to add support for writable raw tracepoint attachment in attach_raw_tp(). * update patch 3 to add Acked-by tag v3: https://www.spinics.net/lists/bpf/msg46824.html * use raw_tp.w instead of raw_tp_writable as section name of writable tp * use ASSERT_XXX() instead of CHECK() * define a common macro for "/sys/kernel/bpf_testmod" v2: https://www.spinics.net/lists/bpf/msg46356.html * rebase on bpf-next tree * address comments from Yonghong Song * rename bpf_testmode_test_writable_ctx::ret as early_ret to reflect its purpose better. v1: https://www.spinics.net/lists/bpf/msg46221.html ==================== Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
2 parents 1c8dab7 + fa7f17d commit a1852ce

File tree

9 files changed

+119
-11
lines changed

9 files changed

+119
-11
lines changed

include/trace/bpf_probe.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ __section("__bpf_raw_tp_map") = { \
9393

9494
#define FIRST(x, ...) x
9595

96-
#undef DEFINE_EVENT_WRITABLE
97-
#define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \
96+
#define __CHECK_WRITABLE_BUF_SIZE(call, proto, args, size) \
9897
static inline void bpf_test_buffer_##call(void) \
9998
{ \
10099
/* BUILD_BUG_ON() is ignored if the code is completely eliminated, but \
@@ -103,8 +102,12 @@ static inline void bpf_test_buffer_##call(void) \
103102
*/ \
104103
FIRST(proto); \
105104
(void)BUILD_BUG_ON_ZERO(size != sizeof(*FIRST(args))); \
106-
} \
107-
__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
105+
}
106+
107+
#undef DEFINE_EVENT_WRITABLE
108+
#define DEFINE_EVENT_WRITABLE(template, call, proto, args, size) \
109+
__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
110+
__DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
108111

109112
#undef DEFINE_EVENT
110113
#define DEFINE_EVENT(template, call, proto, args) \
@@ -119,9 +122,17 @@ __DEFINE_EVENT(template, call, PARAMS(proto), PARAMS(args), size)
119122
__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
120123
__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), 0)
121124

125+
#undef DECLARE_TRACE_WRITABLE
126+
#define DECLARE_TRACE_WRITABLE(call, proto, args, size) \
127+
__CHECK_WRITABLE_BUF_SIZE(call, PARAMS(proto), PARAMS(args), size) \
128+
__BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) \
129+
__DEFINE_EVENT(call, call, PARAMS(proto), PARAMS(args), size)
130+
122131
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
123132

133+
#undef DECLARE_TRACE_WRITABLE
124134
#undef DEFINE_EVENT_WRITABLE
135+
#undef __CHECK_WRITABLE_BUF_SIZE
125136
#undef __DEFINE_EVENT
126137
#undef FIRST
127138

tools/lib/bpf/libbpf.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8077,6 +8077,8 @@ static const struct bpf_sec_def section_defs[] = {
80778077
SEC_DEF("tp/", TRACEPOINT, 0, SEC_NONE, attach_tp),
80788078
SEC_DEF("raw_tracepoint/", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
80798079
SEC_DEF("raw_tp/", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
8080+
SEC_DEF("raw_tracepoint.w/", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
8081+
SEC_DEF("raw_tp.w/", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
80808082
SEC_DEF("tp_btf/", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
80818083
SEC_DEF("fentry/", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
80828084
SEC_DEF("fmod_ret/", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
@@ -9846,12 +9848,26 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *pr
98469848

98479849
static struct bpf_link *attach_raw_tp(const struct bpf_program *prog, long cookie)
98489850
{
9849-
const char *tp_name;
9851+
static const char *const prefixes[] = {
9852+
"raw_tp/",
9853+
"raw_tracepoint/",
9854+
"raw_tp.w/",
9855+
"raw_tracepoint.w/",
9856+
};
9857+
size_t i;
9858+
const char *tp_name = NULL;
98509859

9851-
if (str_has_pfx(prog->sec_name, "raw_tp/"))
9852-
tp_name = prog->sec_name + sizeof("raw_tp/") - 1;
9853-
else
9854-
tp_name = prog->sec_name + sizeof("raw_tracepoint/") - 1;
9860+
for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
9861+
if (str_has_pfx(prog->sec_name, prefixes[i])) {
9862+
tp_name = prog->sec_name + strlen(prefixes[i]);
9863+
break;
9864+
}
9865+
}
9866+
if (!tp_name) {
9867+
pr_warn("prog '%s': invalid section name '%s'\n",
9868+
prog->name, prog->sec_name);
9869+
return libbpf_err_ptr(-EINVAL);
9870+
}
98559871

98569872
return bpf_program__attach_raw_tracepoint(prog, tp_name);
98579873
}

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)