stackmap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/bpf.h>
  8. #include <linux/jhash.h>
  9. #include <linux/filter.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/stacktrace.h>
  12. #include <linux/perf_event.h>
  13. struct stack_map_bucket {
  14. struct rcu_head rcu;
  15. u32 hash;
  16. u32 nr;
  17. u64 ip[];
  18. };
  19. struct bpf_stack_map {
  20. struct bpf_map map;
  21. u32 n_buckets;
  22. struct stack_map_bucket __rcu *buckets[];
  23. };
  24. /* Called from syscall */
  25. static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
  26. {
  27. u32 value_size = attr->value_size;
  28. struct bpf_stack_map *smap;
  29. u64 cost, n_buckets;
  30. int err;
  31. if (!capable(CAP_SYS_ADMIN))
  32. return ERR_PTR(-EPERM);
  33. /* check sanity of attributes */
  34. if (attr->max_entries == 0 || attr->key_size != 4 ||
  35. value_size < 8 || value_size % 8 ||
  36. value_size / 8 > PERF_MAX_STACK_DEPTH)
  37. return ERR_PTR(-EINVAL);
  38. /* hash table size must be power of 2 */
  39. n_buckets = roundup_pow_of_two(attr->max_entries);
  40. cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
  41. if (cost >= U32_MAX - PAGE_SIZE)
  42. return ERR_PTR(-E2BIG);
  43. smap = kzalloc(cost, GFP_USER | __GFP_NOWARN);
  44. if (!smap) {
  45. smap = vzalloc(cost);
  46. if (!smap)
  47. return ERR_PTR(-ENOMEM);
  48. }
  49. err = -E2BIG;
  50. cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
  51. if (cost >= U32_MAX - PAGE_SIZE)
  52. goto free_smap;
  53. smap->map.map_type = attr->map_type;
  54. smap->map.key_size = attr->key_size;
  55. smap->map.value_size = value_size;
  56. smap->map.max_entries = attr->max_entries;
  57. smap->n_buckets = n_buckets;
  58. smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  59. err = get_callchain_buffers();
  60. if (err)
  61. goto free_smap;
  62. return &smap->map;
  63. free_smap:
  64. kvfree(smap);
  65. return ERR_PTR(err);
  66. }
  67. static u64 bpf_get_stackid(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5)
  68. {
  69. struct pt_regs *regs = (struct pt_regs *) (long) r1;
  70. struct bpf_map *map = (struct bpf_map *) (long) r2;
  71. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  72. struct perf_callchain_entry *trace;
  73. struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
  74. u32 max_depth = map->value_size / 8;
  75. /* stack_map_alloc() checks that max_depth <= PERF_MAX_STACK_DEPTH */
  76. u32 init_nr = PERF_MAX_STACK_DEPTH - max_depth;
  77. u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
  78. u32 hash, id, trace_nr, trace_len;
  79. bool user = flags & BPF_F_USER_STACK;
  80. bool kernel = !user;
  81. u64 *ips;
  82. if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
  83. BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
  84. return -EINVAL;
  85. trace = get_perf_callchain(regs, init_nr, kernel, user, false, false);
  86. if (unlikely(!trace))
  87. /* couldn't fetch the stack trace */
  88. return -EFAULT;
  89. /* get_perf_callchain() guarantees that trace->nr >= init_nr
  90. * and trace-nr <= PERF_MAX_STACK_DEPTH, so trace_nr <= max_depth
  91. */
  92. trace_nr = trace->nr - init_nr;
  93. if (trace_nr <= skip)
  94. /* skipping more than usable stack trace */
  95. return -EFAULT;
  96. trace_nr -= skip;
  97. trace_len = trace_nr * sizeof(u64);
  98. ips = trace->ip + skip + init_nr;
  99. hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
  100. id = hash & (smap->n_buckets - 1);
  101. bucket = rcu_dereference(smap->buckets[id]);
  102. if (bucket && bucket->hash == hash) {
  103. if (flags & BPF_F_FAST_STACK_CMP)
  104. return id;
  105. if (bucket->nr == trace_nr &&
  106. memcmp(bucket->ip, ips, trace_len) == 0)
  107. return id;
  108. }
  109. /* this call stack is not in the map, try to add it */
  110. if (bucket && !(flags & BPF_F_REUSE_STACKID))
  111. return -EEXIST;
  112. new_bucket = kmalloc(sizeof(struct stack_map_bucket) + map->value_size,
  113. GFP_ATOMIC | __GFP_NOWARN);
  114. if (unlikely(!new_bucket))
  115. return -ENOMEM;
  116. memcpy(new_bucket->ip, ips, trace_len);
  117. memset(new_bucket->ip + trace_len / 8, 0, map->value_size - trace_len);
  118. new_bucket->hash = hash;
  119. new_bucket->nr = trace_nr;
  120. old_bucket = xchg(&smap->buckets[id], new_bucket);
  121. if (old_bucket)
  122. kfree_rcu(old_bucket, rcu);
  123. return id;
  124. }
  125. const struct bpf_func_proto bpf_get_stackid_proto = {
  126. .func = bpf_get_stackid,
  127. .gpl_only = true,
  128. .ret_type = RET_INTEGER,
  129. .arg1_type = ARG_PTR_TO_CTX,
  130. .arg2_type = ARG_CONST_MAP_PTR,
  131. .arg3_type = ARG_ANYTHING,
  132. };
  133. /* Called from syscall or from eBPF program */
  134. static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
  135. {
  136. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  137. struct stack_map_bucket *bucket;
  138. u32 id = *(u32 *)key;
  139. if (unlikely(id >= smap->n_buckets))
  140. return NULL;
  141. bucket = rcu_dereference(smap->buckets[id]);
  142. return bucket ? bucket->ip : NULL;
  143. }
  144. static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  145. {
  146. return -EINVAL;
  147. }
  148. static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
  149. u64 map_flags)
  150. {
  151. return -EINVAL;
  152. }
  153. /* Called from syscall or from eBPF program */
  154. static int stack_map_delete_elem(struct bpf_map *map, void *key)
  155. {
  156. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  157. struct stack_map_bucket *old_bucket;
  158. u32 id = *(u32 *)key;
  159. if (unlikely(id >= smap->n_buckets))
  160. return -E2BIG;
  161. old_bucket = xchg(&smap->buckets[id], NULL);
  162. if (old_bucket) {
  163. kfree_rcu(old_bucket, rcu);
  164. return 0;
  165. } else {
  166. return -ENOENT;
  167. }
  168. }
  169. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  170. static void stack_map_free(struct bpf_map *map)
  171. {
  172. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  173. int i;
  174. synchronize_rcu();
  175. for (i = 0; i < smap->n_buckets; i++)
  176. if (smap->buckets[i])
  177. kfree_rcu(smap->buckets[i], rcu);
  178. kvfree(smap);
  179. put_callchain_buffers();
  180. }
  181. static const struct bpf_map_ops stack_map_ops = {
  182. .map_alloc = stack_map_alloc,
  183. .map_free = stack_map_free,
  184. .map_get_next_key = stack_map_get_next_key,
  185. .map_lookup_elem = stack_map_lookup_elem,
  186. .map_update_elem = stack_map_update_elem,
  187. .map_delete_elem = stack_map_delete_elem,
  188. };
  189. static struct bpf_map_type_list stack_map_type __read_mostly = {
  190. .ops = &stack_map_ops,
  191. .type = BPF_MAP_TYPE_STACK_TRACE,
  192. };
  193. static int __init register_stack_map(void)
  194. {
  195. bpf_register_map_type(&stack_map_type);
  196. return 0;
  197. }
  198. late_initcall(register_stack_map);