spintest_kern.c 2.1 KB

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