blob: f508af357251b5e180f8fabfbc6d4da603e6cff6 [file] [log] [blame]
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -08001/* 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 Starovoitov7dcc42b2016-03-08 15:07:53 -080011#include <uapi/linux/perf_event.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010012#include <bpf/bpf_helpers.h>
13#include <bpf/bpf_tracing.h>
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080014
15struct bpf_map_def SEC("maps") my_map = {
16 .type = BPF_MAP_TYPE_HASH,
17 .key_size = sizeof(long),
18 .value_size = sizeof(long),
19 .max_entries = 1024,
20};
21struct bpf_map_def SEC("maps") my_map2 = {
22 .type = BPF_MAP_TYPE_PERCPU_HASH,
23 .key_size = sizeof(long),
24 .value_size = sizeof(long),
25 .max_entries = 1024,
26};
27
Alexei Starovoitov7dcc42b2016-03-08 15:07:53 -080028struct bpf_map_def SEC("maps") stackmap = {
29 .type = BPF_MAP_TYPE_STACK_TRACE,
30 .key_size = sizeof(u32),
31 .value_size = PERF_MAX_STACK_DEPTH * sizeof(u64),
32 .max_entries = 10000,
33};
34
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080035#define PROG(foo) \
36int foo(struct pt_regs *ctx) \
37{ \
Naveen N. Rao138d6152016-04-04 22:31:34 +053038 long v = PT_REGS_IP(ctx), *val; \
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080039\
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 Starovoitov7dcc42b2016-03-08 15:07:53 -080044 bpf_get_stackid(ctx, &stackmap, BPF_F_REUSE_STACKID); \
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080045 return 0; \
46}
47
48/* add kprobes to all possible *spin* functions */
49SEC("kprobe/spin_unlock")PROG(p1)
50SEC("kprobe/spin_lock")PROG(p2)
51SEC("kprobe/mutex_spin_on_owner")PROG(p3)
52SEC("kprobe/rwsem_spin_on_owner")PROG(p4)
53SEC("kprobe/spin_unlock_irqrestore")PROG(p5)
54SEC("kprobe/_raw_spin_unlock_irqrestore")PROG(p6)
55SEC("kprobe/_raw_spin_unlock_bh")PROG(p7)
56SEC("kprobe/_raw_spin_unlock")PROG(p8)
57SEC("kprobe/_raw_spin_lock_irqsave")PROG(p9)
58SEC("kprobe/_raw_spin_trylock_bh")PROG(p10)
59SEC("kprobe/_raw_spin_lock_irq")PROG(p11)
60SEC("kprobe/_raw_spin_trylock")PROG(p12)
61SEC("kprobe/_raw_spin_lock")PROG(p13)
62SEC("kprobe/_raw_spin_lock_bh")PROG(p14)
63/* and to inner bpf helpers */
64SEC("kprobe/htab_map_update_elem")PROG(p15)
65SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16)
66SEC("kprobe/htab_map_alloc")PROG(p17)
67
68char _license[] SEC("license") = "GPL";
69u32 _version SEC("version") = LINUX_VERSION_CODE;