stackmap.c 7.3 KB

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