Commit 456d5355 authored by Daniel T. Lee's avatar Daniel T. Lee Committed by Alexei Starovoitov

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: default avatarDaniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230818090119.477441-10-danieltimlee@gmail.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 8dc80551
...@@ -47,20 +47,9 @@ int foo(struct pt_regs *ctx) \ ...@@ -47,20 +47,9 @@ int foo(struct pt_regs *ctx) \
} }
/* add kprobes to all possible *spin* functions */ /* add kprobes to all possible *spin* functions */
SEC("kprobe/spin_unlock")PROG(p1) SEC("kprobe.multi/spin_*lock*")PROG(spin_lock)
SEC("kprobe/spin_lock")PROG(p2) SEC("kprobe.multi/*_spin_on_owner")PROG(spin_on_owner)
SEC("kprobe/mutex_spin_on_owner")PROG(p3) SEC("kprobe.multi/_raw_spin_*lock*")PROG(raw_spin_lock)
SEC("kprobe/rwsem_spin_on_owner")PROG(p4)
SEC("kprobe/spin_unlock_irqrestore")PROG(p5)
SEC("kprobe/_raw_spin_unlock_irqrestore")PROG(p6)
SEC("kprobe/_raw_spin_unlock_bh")PROG(p7)
SEC("kprobe/_raw_spin_unlock")PROG(p8)
SEC("kprobe/_raw_spin_lock_irqsave")PROG(p9)
SEC("kprobe/_raw_spin_trylock_bh")PROG(p10)
SEC("kprobe/_raw_spin_lock_irq")PROG(p11)
SEC("kprobe/_raw_spin_trylock")PROG(p12)
SEC("kprobe/_raw_spin_lock")PROG(p13)
SEC("kprobe/_raw_spin_lock_bh")PROG(p14)
/* and to inner bpf helpers */ /* and to inner bpf helpers */
SEC("kprobe/htab_map_update_elem")PROG(p15) SEC("kprobe/htab_map_update_elem")PROG(p15)
......
...@@ -9,13 +9,12 @@ ...@@ -9,13 +9,12 @@
int main(int ac, char **argv) int main(int ac, char **argv)
{ {
char filename[256], symbol[256];
struct bpf_object *obj = NULL; struct bpf_object *obj = NULL;
struct bpf_link *links[20]; struct bpf_link *links[20];
long key, next_key, value; long key, next_key, value;
struct bpf_program *prog; struct bpf_program *prog;
int map_fd, i, j = 0; int map_fd, i, j = 0;
const char *section; char filename[256];
struct ksym *sym; struct ksym *sym;
if (load_kallsyms()) { if (load_kallsyms()) {
...@@ -44,20 +43,13 @@ int main(int ac, char **argv) ...@@ -44,20 +43,13 @@ int main(int ac, char **argv)
} }
bpf_object__for_each_program(prog, obj) { bpf_object__for_each_program(prog, obj) {
section = bpf_program__section_name(prog); links[j] = bpf_program__attach(prog);
if (sscanf(section, "kprobe/%s", symbol) != 1) if (libbpf_get_error(links[j])) {
continue; fprintf(stderr, "bpf_program__attach failed\n");
links[j] = NULL;
/* Attach prog only when symbol exists */ goto cleanup;
if (ksym_get_addr(symbol)) {
links[j] = bpf_program__attach(prog);
if (libbpf_get_error(links[j])) {
fprintf(stderr, "bpf_program__attach failed\n");
links[j] = NULL;
goto cleanup;
}
j++;
} }
j++;
} }
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment