stackmap.c 7.7 KB

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