Alexei Starovoitov | 9d8b612 | 2016-03-08 15:07:52 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2016, Facebook |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | */ |
| 7 | #include <linux/skbuff.h> |
| 8 | #include <linux/netdevice.h> |
| 9 | #include <linux/version.h> |
| 10 | #include <uapi/linux/bpf.h> |
Alexei Starovoitov | 7dcc42b | 2016-03-08 15:07:53 -0800 | [diff] [blame] | 11 | #include <uapi/linux/perf_event.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 12 | #include <bpf/bpf_helpers.h> |
| 13 | #include <bpf/bpf_tracing.h> |
Alexei Starovoitov | 9d8b612 | 2016-03-08 15:07:52 -0800 | [diff] [blame] | 14 | |
Daniel T. Lee | 3677d0a | 2020-08-23 17:53:33 +0900 | [diff] [blame] | 15 | struct { |
| 16 | __uint(type, BPF_MAP_TYPE_HASH); |
| 17 | __type(key, long); |
| 18 | __type(value, long); |
| 19 | __uint(max_entries, 1024); |
| 20 | } my_map SEC(".maps"); |
| 21 | struct { |
| 22 | __uint(type, BPF_MAP_TYPE_PERCPU_HASH); |
| 23 | __uint(key_size, sizeof(long)); |
| 24 | __uint(value_size, sizeof(long)); |
| 25 | __uint(max_entries, 1024); |
| 26 | } my_map2 SEC(".maps"); |
Alexei Starovoitov | 9d8b612 | 2016-03-08 15:07:52 -0800 | [diff] [blame] | 27 | |
Daniel T. Lee | 3677d0a | 2020-08-23 17:53:33 +0900 | [diff] [blame] | 28 | struct { |
| 29 | __uint(type, BPF_MAP_TYPE_STACK_TRACE); |
| 30 | __uint(key_size, sizeof(u32)); |
| 31 | __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64)); |
| 32 | __uint(max_entries, 10000); |
| 33 | } stackmap SEC(".maps"); |
Alexei Starovoitov | 7dcc42b | 2016-03-08 15:07:53 -0800 | [diff] [blame] | 34 | |
Alexei Starovoitov | 9d8b612 | 2016-03-08 15:07:52 -0800 | [diff] [blame] | 35 | #define PROG(foo) \ |
| 36 | int foo(struct pt_regs *ctx) \ |
| 37 | { \ |
Naveen N. Rao | 138d615 | 2016-04-04 22:31:34 +0530 | [diff] [blame] | 38 | long v = PT_REGS_IP(ctx), *val; \ |
Alexei Starovoitov | 9d8b612 | 2016-03-08 15:07:52 -0800 | [diff] [blame] | 39 | \ |
| 40 | val = bpf_map_lookup_elem(&my_map, &v); \ |
| 41 | bpf_map_update_elem(&my_map, &v, &v, BPF_ANY); \ |
| 42 | bpf_map_update_elem(&my_map2, &v, &v, BPF_ANY); \ |
| 43 | bpf_map_delete_elem(&my_map2, &v); \ |
Alexei Starovoitov | 7dcc42b | 2016-03-08 15:07:53 -0800 | [diff] [blame] | 44 | bpf_get_stackid(ctx, &stackmap, BPF_F_REUSE_STACKID); \ |
Alexei Starovoitov | 9d8b612 | 2016-03-08 15:07:52 -0800 | [diff] [blame] | 45 | return 0; \ |
| 46 | } |
| 47 | |
| 48 | /* add kprobes to all possible *spin* functions */ |
| 49 | SEC("kprobe/spin_unlock")PROG(p1) |
| 50 | SEC("kprobe/spin_lock")PROG(p2) |
| 51 | SEC("kprobe/mutex_spin_on_owner")PROG(p3) |
| 52 | SEC("kprobe/rwsem_spin_on_owner")PROG(p4) |
| 53 | SEC("kprobe/spin_unlock_irqrestore")PROG(p5) |
| 54 | SEC("kprobe/_raw_spin_unlock_irqrestore")PROG(p6) |
| 55 | SEC("kprobe/_raw_spin_unlock_bh")PROG(p7) |
| 56 | SEC("kprobe/_raw_spin_unlock")PROG(p8) |
| 57 | SEC("kprobe/_raw_spin_lock_irqsave")PROG(p9) |
| 58 | SEC("kprobe/_raw_spin_trylock_bh")PROG(p10) |
| 59 | SEC("kprobe/_raw_spin_lock_irq")PROG(p11) |
| 60 | SEC("kprobe/_raw_spin_trylock")PROG(p12) |
| 61 | SEC("kprobe/_raw_spin_lock")PROG(p13) |
| 62 | SEC("kprobe/_raw_spin_lock_bh")PROG(p14) |
| 63 | /* and to inner bpf helpers */ |
| 64 | SEC("kprobe/htab_map_update_elem")PROG(p15) |
| 65 | SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16) |
| 66 | SEC("kprobe/htab_map_alloc")PROG(p17) |
| 67 | |
| 68 | char _license[] SEC("license") = "GPL"; |
| 69 | u32 _version SEC("version") = LINUX_VERSION_CODE; |