stackmap.c 7.3 KB

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