Skip to content

Commit a38d110

Browse files
mokomullAlexei Starovoitov
authored andcommitted
bpf: support raw tracepoints in modules
Distributions build drivers as modules, including network and filesystem drivers which export numerous tracepoints. This enables bpf(BPF_RAW_TRACEPOINT_OPEN) to attach to those tracepoints. Signed-off-by: Matt Mullins <mmullins@fb.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent a137401 commit a38d110

File tree

5 files changed

+120
-7
lines changed

5 files changed

+120
-7
lines changed

include/linux/module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ struct module {
432432
unsigned int num_tracepoints;
433433
tracepoint_ptr_t *tracepoints_ptrs;
434434
#endif
435+
#ifdef CONFIG_BPF_EVENTS
436+
unsigned int num_bpf_raw_events;
437+
struct bpf_raw_event_map *bpf_raw_events;
438+
#endif
435439
#ifdef HAVE_JUMP_LABEL
436440
struct jump_entry *jump_entries;
437441
unsigned int num_jump_entries;

include/linux/trace_events.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ void perf_event_detach_bpf_prog(struct perf_event *event);
471471
int perf_event_query_prog_array(struct perf_event *event, void __user *info);
472472
int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog);
473473
int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog);
474-
struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name);
474+
struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name);
475+
void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp);
475476
int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
476477
u32 *fd_type, const char **buf,
477478
u64 *probe_offset, u64 *probe_addr);
@@ -502,10 +503,13 @@ static inline int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf
502503
{
503504
return -EOPNOTSUPP;
504505
}
505-
static inline struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
506+
static inline struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
506507
{
507508
return NULL;
508509
}
510+
static inline void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
511+
{
512+
}
509513
static inline int bpf_get_perf_event_info(const struct perf_event *event,
510514
u32 *prog_id, u32 *fd_type,
511515
const char **buf, u64 *probe_offset,

kernel/bpf/syscall.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,7 @@ static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
16041604
bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
16051605
bpf_prog_put(raw_tp->prog);
16061606
}
1607+
bpf_put_raw_tracepoint(raw_tp->btp);
16071608
kfree(raw_tp);
16081609
return 0;
16091610
}
@@ -1629,13 +1630,15 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
16291630
return -EFAULT;
16301631
tp_name[sizeof(tp_name) - 1] = 0;
16311632

1632-
btp = bpf_find_raw_tracepoint(tp_name);
1633+
btp = bpf_get_raw_tracepoint(tp_name);
16331634
if (!btp)
16341635
return -ENOENT;
16351636

16361637
raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1637-
if (!raw_tp)
1638-
return -ENOMEM;
1638+
if (!raw_tp) {
1639+
err = -ENOMEM;
1640+
goto out_put_btp;
1641+
}
16391642
raw_tp->btp = btp;
16401643

16411644
prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
@@ -1663,6 +1666,8 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
16631666
bpf_prog_put(prog);
16641667
out_free_tp:
16651668
kfree(raw_tp);
1669+
out_put_btp:
1670+
bpf_put_raw_tracepoint(btp);
16661671
return err;
16671672
}
16681673

kernel/module.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,6 +3093,11 @@ static int find_module_sections(struct module *mod, struct load_info *info)
30933093
sizeof(*mod->tracepoints_ptrs),
30943094
&mod->num_tracepoints);
30953095
#endif
3096+
#ifdef CONFIG_BPF_EVENTS
3097+
mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
3098+
sizeof(*mod->bpf_raw_events),
3099+
&mod->num_bpf_raw_events);
3100+
#endif
30963101
#ifdef HAVE_JUMP_LABEL
30973102
mod->jump_entries = section_objs(info, "__jump_table",
30983103
sizeof(*mod->jump_entries),

kernel/trace/bpf_trace.c

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,43 @@
1717
#include "trace_probe.h"
1818
#include "trace.h"
1919

20+
#ifdef CONFIG_MODULES
21+
struct bpf_trace_module {
22+
struct module *module;
23+
struct list_head list;
24+
};
25+
26+
static LIST_HEAD(bpf_trace_modules);
27+
static DEFINE_MUTEX(bpf_module_mutex);
28+
29+
static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
30+
{
31+
struct bpf_raw_event_map *btp, *ret = NULL;
32+
struct bpf_trace_module *btm;
33+
unsigned int i;
34+
35+
mutex_lock(&bpf_module_mutex);
36+
list_for_each_entry(btm, &bpf_trace_modules, list) {
37+
for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
38+
btp = &btm->module->bpf_raw_events[i];
39+
if (!strcmp(btp->tp->name, name)) {
40+
if (try_module_get(btm->module))
41+
ret = btp;
42+
goto out;
43+
}
44+
}
45+
}
46+
out:
47+
mutex_unlock(&bpf_module_mutex);
48+
return ret;
49+
}
50+
#else
51+
static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
52+
{
53+
return NULL;
54+
}
55+
#endif /* CONFIG_MODULES */
56+
2057
u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2158
u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2259

@@ -1076,15 +1113,24 @@ int perf_event_query_prog_array(struct perf_event *event, void __user *info)
10761113
extern struct bpf_raw_event_map __start__bpf_raw_tp[];
10771114
extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
10781115

1079-
struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
1116+
struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
10801117
{
10811118
struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
10821119

10831120
for (; btp < __stop__bpf_raw_tp; btp++) {
10841121
if (!strcmp(btp->tp->name, name))
10851122
return btp;
10861123
}
1087-
return NULL;
1124+
1125+
return bpf_get_raw_tracepoint_module(name);
1126+
}
1127+
1128+
void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
1129+
{
1130+
struct module *mod = __module_address((unsigned long)btp);
1131+
1132+
if (mod)
1133+
module_put(mod);
10881134
}
10891135

10901136
static __always_inline
@@ -1222,3 +1268,52 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
12221268

12231269
return err;
12241270
}
1271+
1272+
#ifdef CONFIG_MODULES
1273+
int bpf_event_notify(struct notifier_block *nb, unsigned long op, void *module)
1274+
{
1275+
struct bpf_trace_module *btm, *tmp;
1276+
struct module *mod = module;
1277+
1278+
if (mod->num_bpf_raw_events == 0 ||
1279+
(op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
1280+
return 0;
1281+
1282+
mutex_lock(&bpf_module_mutex);
1283+
1284+
switch (op) {
1285+
case MODULE_STATE_COMING:
1286+
btm = kzalloc(sizeof(*btm), GFP_KERNEL);
1287+
if (btm) {
1288+
btm->module = module;
1289+
list_add(&btm->list, &bpf_trace_modules);
1290+
}
1291+
break;
1292+
case MODULE_STATE_GOING:
1293+
list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
1294+
if (btm->module == module) {
1295+
list_del(&btm->list);
1296+
kfree(btm);
1297+
break;
1298+
}
1299+
}
1300+
break;
1301+
}
1302+
1303+
mutex_unlock(&bpf_module_mutex);
1304+
1305+
return 0;
1306+
}
1307+
1308+
static struct notifier_block bpf_module_nb = {
1309+
.notifier_call = bpf_event_notify,
1310+
};
1311+
1312+
int __init bpf_event_init(void)
1313+
{
1314+
register_module_notifier(&bpf_module_nb);
1315+
return 0;
1316+
}
1317+
1318+
fs_initcall(bpf_event_init);
1319+
#endif /* CONFIG_MODULES */

0 commit comments

Comments
 (0)