Skip to content

Commit 456d535

Browse files
DanielTimLeeAlexei Starovoitov
authored andcommitted
samples/bpf: simplify spintest with kprobe.multi
With the introduction of kprobe.multi, it is now possible to attach multiple kprobes to a single BPF program without the need for multiple definitions. Additionally, this method supports wildcard-based matching, allowing for further simplification of BPF programs. In here, an asterisk (*) wildcard is used to map to all symbols relevant to spin_{lock|unlock}. Furthermore, since kprobe.multi handles symbol matching, this commit eliminates the need for the previous logic of reading the ksym table to verify the existence of symbols. Signed-off-by: Daniel T. Lee <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 8dc8055 commit 456d535

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

samples/bpf/spintest.bpf.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,9 @@ int foo(struct pt_regs *ctx) \
4747
}
4848

4949
/* add kprobes to all possible *spin* functions */
50-
SEC("kprobe/spin_unlock")PROG(p1)
51-
SEC("kprobe/spin_lock")PROG(p2)
52-
SEC("kprobe/mutex_spin_on_owner")PROG(p3)
53-
SEC("kprobe/rwsem_spin_on_owner")PROG(p4)
54-
SEC("kprobe/spin_unlock_irqrestore")PROG(p5)
55-
SEC("kprobe/_raw_spin_unlock_irqrestore")PROG(p6)
56-
SEC("kprobe/_raw_spin_unlock_bh")PROG(p7)
57-
SEC("kprobe/_raw_spin_unlock")PROG(p8)
58-
SEC("kprobe/_raw_spin_lock_irqsave")PROG(p9)
59-
SEC("kprobe/_raw_spin_trylock_bh")PROG(p10)
60-
SEC("kprobe/_raw_spin_lock_irq")PROG(p11)
61-
SEC("kprobe/_raw_spin_trylock")PROG(p12)
62-
SEC("kprobe/_raw_spin_lock")PROG(p13)
63-
SEC("kprobe/_raw_spin_lock_bh")PROG(p14)
50+
SEC("kprobe.multi/spin_*lock*")PROG(spin_lock)
51+
SEC("kprobe.multi/*_spin_on_owner")PROG(spin_on_owner)
52+
SEC("kprobe.multi/_raw_spin_*lock*")PROG(raw_spin_lock)
6453
6554
/* and to inner bpf helpers */
6655
SEC("kprobe/htab_map_update_elem")PROG(p15)

samples/bpf/spintest_user.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99

1010
int main(int ac, char **argv)
1111
{
12-
char filename[256], symbol[256];
1312
struct bpf_object *obj = NULL;
1413
struct bpf_link *links[20];
1514
long key, next_key, value;
1615
struct bpf_program *prog;
1716
int map_fd, i, j = 0;
18-
const char *section;
17+
char filename[256];
1918
struct ksym *sym;
2019

2120
if (load_kallsyms()) {
@@ -44,20 +43,13 @@ int main(int ac, char **argv)
4443
}
4544

4645
bpf_object__for_each_program(prog, obj) {
47-
section = bpf_program__section_name(prog);
48-
if (sscanf(section, "kprobe/%s", symbol) != 1)
49-
continue;
50-
51-
/* Attach prog only when symbol exists */
52-
if (ksym_get_addr(symbol)) {
53-
links[j] = bpf_program__attach(prog);
54-
if (libbpf_get_error(links[j])) {
55-
fprintf(stderr, "bpf_program__attach failed\n");
56-
links[j] = NULL;
57-
goto cleanup;
58-
}
59-
j++;
46+
links[j] = bpf_program__attach(prog);
47+
if (libbpf_get_error(links[j])) {
48+
fprintf(stderr, "bpf_program__attach failed\n");
49+
links[j] = NULL;
50+
goto cleanup;
6051
}
52+
j++;
6153
}
6254

6355
for (i = 0; i < 5; i++) {

0 commit comments

Comments
 (0)