blob: ce0167d09cdc6f1432e687933bf38ebef59bad21 [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>
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080012#include "bpf_helpers.h"
13
14struct bpf_map_def SEC("maps") my_map = {
15 .type = BPF_MAP_TYPE_HASH,
16 .key_size = sizeof(long),
17 .value_size = sizeof(long),
18 .max_entries = 1024,
19};
20struct bpf_map_def SEC("maps") my_map2 = {
21 .type = BPF_MAP_TYPE_PERCPU_HASH,
22 .key_size = sizeof(long),
23 .value_size = sizeof(long),
24 .max_entries = 1024,
25};
26
Alexei Starovoitov7dcc42b2016-03-08 15:07:53 -080027struct bpf_map_def SEC("maps") stackmap = {
28 .type = BPF_MAP_TYPE_STACK_TRACE,
29 .key_size = sizeof(u32),
30 .value_size = PERF_MAX_STACK_DEPTH * sizeof(u64),
31 .max_entries = 10000,
32};
33
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080034#define PROG(foo) \
35int foo(struct pt_regs *ctx) \
36{ \
Naveen N. Rao138d6152016-04-04 22:31:34 +053037 long v = PT_REGS_IP(ctx), *val; \
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080038\
39 val = bpf_map_lookup_elem(&my_map, &v); \
40 bpf_map_update_elem(&my_map, &v, &v, BPF_ANY); \
41 bpf_map_update_elem(&my_map2, &v, &v, BPF_ANY); \
42 bpf_map_delete_elem(&my_map2, &v); \
Alexei Starovoitov7dcc42b2016-03-08 15:07:53 -080043 bpf_get_stackid(ctx, &stackmap, BPF_F_REUSE_STACKID); \
Alexei Starovoitov9d8b6122016-03-08 15:07:52 -080044 return 0; \
45}
46
47/* add kprobes to all possible *spin* functions */
48SEC("kprobe/spin_unlock")PROG(p1)
49SEC("kprobe/spin_lock")PROG(p2)
50SEC("kprobe/mutex_spin_on_owner")PROG(p3)
51SEC("kprobe/rwsem_spin_on_owner")PROG(p4)
52SEC("kprobe/spin_unlock_irqrestore")PROG(p5)
53SEC("kprobe/_raw_spin_unlock_irqrestore")PROG(p6)
54SEC("kprobe/_raw_spin_unlock_bh")PROG(p7)
55SEC("kprobe/_raw_spin_unlock")PROG(p8)
56SEC("kprobe/_raw_spin_lock_irqsave")PROG(p9)
57SEC("kprobe/_raw_spin_trylock_bh")PROG(p10)
58SEC("kprobe/_raw_spin_lock_irq")PROG(p11)
59SEC("kprobe/_raw_spin_trylock")PROG(p12)
60SEC("kprobe/_raw_spin_lock")PROG(p13)
61SEC("kprobe/_raw_spin_lock_bh")PROG(p14)
62/* and to inner bpf helpers */
63SEC("kprobe/htab_map_update_elem")PROG(p15)
64SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16)
65SEC("kprobe/htab_map_alloc")PROG(p17)
66
67char _license[] SEC("license") = "GPL";
68u32 _version SEC("version") = LINUX_VERSION_CODE;