stackmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 <linux/elf.h>
  13. #include <linux/pagemap.h>
  14. #include "percpu_freelist.h"
  15. #define STACK_CREATE_FLAG_MASK \
  16. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY | \
  17. BPF_F_STACK_BUILD_ID)
  18. struct stack_map_bucket {
  19. struct pcpu_freelist_node fnode;
  20. u32 hash;
  21. u32 nr;
  22. u64 data[];
  23. };
  24. struct bpf_stack_map {
  25. struct bpf_map map;
  26. void *elems;
  27. struct pcpu_freelist freelist;
  28. u32 n_buckets;
  29. struct stack_map_bucket *buckets[];
  30. };
  31. static inline bool stack_map_use_build_id(struct bpf_map *map)
  32. {
  33. return (map->map_flags & BPF_F_STACK_BUILD_ID);
  34. }
  35. static inline int stack_map_data_size(struct bpf_map *map)
  36. {
  37. return stack_map_use_build_id(map) ?
  38. sizeof(struct bpf_stack_build_id) : sizeof(u64);
  39. }
  40. static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
  41. {
  42. u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
  43. int err;
  44. smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
  45. smap->map.numa_node);
  46. if (!smap->elems)
  47. return -ENOMEM;
  48. err = pcpu_freelist_init(&smap->freelist);
  49. if (err)
  50. goto free_elems;
  51. pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
  52. smap->map.max_entries);
  53. return 0;
  54. free_elems:
  55. bpf_map_area_free(smap->elems);
  56. return err;
  57. }
  58. /* Called from syscall */
  59. static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
  60. {
  61. u32 value_size = attr->value_size;
  62. struct bpf_stack_map *smap;
  63. u64 cost, n_buckets;
  64. int err;
  65. if (!capable(CAP_SYS_ADMIN))
  66. return ERR_PTR(-EPERM);
  67. if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
  68. return ERR_PTR(-EINVAL);
  69. /* check sanity of attributes */
  70. if (attr->max_entries == 0 || attr->key_size != 4 ||
  71. value_size < 8 || value_size % 8)
  72. return ERR_PTR(-EINVAL);
  73. BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
  74. if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
  75. if (value_size % sizeof(struct bpf_stack_build_id) ||
  76. value_size / sizeof(struct bpf_stack_build_id)
  77. > sysctl_perf_event_max_stack)
  78. return ERR_PTR(-EINVAL);
  79. } else if (value_size / 8 > sysctl_perf_event_max_stack)
  80. return ERR_PTR(-EINVAL);
  81. /* hash table size must be power of 2 */
  82. n_buckets = roundup_pow_of_two(attr->max_entries);
  83. cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
  84. if (cost >= U32_MAX - PAGE_SIZE)
  85. return ERR_PTR(-E2BIG);
  86. smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
  87. if (!smap)
  88. return ERR_PTR(-ENOMEM);
  89. err = -E2BIG;
  90. cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
  91. if (cost >= U32_MAX - PAGE_SIZE)
  92. goto free_smap;
  93. bpf_map_init_from_attr(&smap->map, attr);
  94. smap->map.value_size = value_size;
  95. smap->n_buckets = n_buckets;
  96. smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  97. err = bpf_map_precharge_memlock(smap->map.pages);
  98. if (err)
  99. goto free_smap;
  100. err = get_callchain_buffers(sysctl_perf_event_max_stack);
  101. if (err)
  102. goto free_smap;
  103. err = prealloc_elems_and_freelist(smap);
  104. if (err)
  105. goto put_buffers;
  106. return &smap->map;
  107. put_buffers:
  108. put_callchain_buffers();
  109. free_smap:
  110. bpf_map_area_free(smap);
  111. return ERR_PTR(err);
  112. }
  113. #define BPF_BUILD_ID 3
  114. /*
  115. * Parse build id from the note segment. This logic can be shared between
  116. * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
  117. * identical.
  118. */
  119. static inline int stack_map_parse_build_id(void *page_addr,
  120. unsigned char *build_id,
  121. void *note_start,
  122. Elf32_Word note_size)
  123. {
  124. Elf32_Word note_offs = 0, new_offs;
  125. /* check for overflow */
  126. if (note_start < page_addr || note_start + note_size < note_start)
  127. return -EINVAL;
  128. /* only supports note that fits in the first page */
  129. if (note_start + note_size > page_addr + PAGE_SIZE)
  130. return -EINVAL;
  131. while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
  132. Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
  133. if (nhdr->n_type == BPF_BUILD_ID &&
  134. nhdr->n_namesz == sizeof("GNU") &&
  135. nhdr->n_descsz == BPF_BUILD_ID_SIZE) {
  136. memcpy(build_id,
  137. note_start + note_offs +
  138. ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
  139. BPF_BUILD_ID_SIZE);
  140. return 0;
  141. }
  142. new_offs = note_offs + sizeof(Elf32_Nhdr) +
  143. ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
  144. if (new_offs <= note_offs) /* overflow */
  145. break;
  146. note_offs = new_offs;
  147. }
  148. return -EINVAL;
  149. }
  150. /* Parse build ID from 32-bit ELF */
  151. static int stack_map_get_build_id_32(void *page_addr,
  152. unsigned char *build_id)
  153. {
  154. Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
  155. Elf32_Phdr *phdr;
  156. int i;
  157. /* only supports phdr that fits in one page */
  158. if (ehdr->e_phnum >
  159. (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
  160. return -EINVAL;
  161. phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
  162. for (i = 0; i < ehdr->e_phnum; ++i)
  163. if (phdr[i].p_type == PT_NOTE)
  164. return stack_map_parse_build_id(page_addr, build_id,
  165. page_addr + phdr[i].p_offset,
  166. phdr[i].p_filesz);
  167. return -EINVAL;
  168. }
  169. /* Parse build ID from 64-bit ELF */
  170. static int stack_map_get_build_id_64(void *page_addr,
  171. unsigned char *build_id)
  172. {
  173. Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
  174. Elf64_Phdr *phdr;
  175. int i;
  176. /* only supports phdr that fits in one page */
  177. if (ehdr->e_phnum >
  178. (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
  179. return -EINVAL;
  180. phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
  181. for (i = 0; i < ehdr->e_phnum; ++i)
  182. if (phdr[i].p_type == PT_NOTE)
  183. return stack_map_parse_build_id(page_addr, build_id,
  184. page_addr + phdr[i].p_offset,
  185. phdr[i].p_filesz);
  186. return -EINVAL;
  187. }
  188. /* Parse build ID of ELF file mapped to vma */
  189. static int stack_map_get_build_id(struct vm_area_struct *vma,
  190. unsigned char *build_id)
  191. {
  192. Elf32_Ehdr *ehdr;
  193. struct page *page;
  194. void *page_addr;
  195. int ret;
  196. /* only works for page backed storage */
  197. if (!vma->vm_file)
  198. return -EINVAL;
  199. page = find_get_page(vma->vm_file->f_mapping, 0);
  200. if (!page)
  201. return -EFAULT; /* page not mapped */
  202. ret = -EINVAL;
  203. page_addr = page_address(page);
  204. ehdr = (Elf32_Ehdr *)page_addr;
  205. /* compare magic x7f "ELF" */
  206. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
  207. goto out;
  208. /* only support executable file and shared object file */
  209. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
  210. goto out;
  211. if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
  212. ret = stack_map_get_build_id_32(page_addr, build_id);
  213. else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  214. ret = stack_map_get_build_id_64(page_addr, build_id);
  215. out:
  216. put_page(page);
  217. return ret;
  218. }
  219. static void stack_map_get_build_id_offset(struct bpf_map *map,
  220. struct stack_map_bucket *bucket,
  221. u64 *ips, u32 trace_nr, bool user)
  222. {
  223. int i;
  224. struct vm_area_struct *vma;
  225. struct bpf_stack_build_id *id_offs;
  226. bucket->nr = trace_nr;
  227. id_offs = (struct bpf_stack_build_id *)bucket->data;
  228. /*
  229. * We cannot do up_read() in nmi context, so build_id lookup is
  230. * only supported for non-nmi events. If at some point, it is
  231. * possible to run find_vma() without taking the semaphore, we
  232. * would like to allow build_id lookup in nmi context.
  233. *
  234. * Same fallback is used for kernel stack (!user) on a stackmap
  235. * with build_id.
  236. */
  237. if (!user || !current || !current->mm || in_nmi() ||
  238. down_read_trylock(&current->mm->mmap_sem) == 0) {
  239. /* cannot access current->mm, fall back to ips */
  240. for (i = 0; i < trace_nr; i++) {
  241. id_offs[i].status = BPF_STACK_BUILD_ID_IP;
  242. id_offs[i].ip = ips[i];
  243. }
  244. return;
  245. }
  246. for (i = 0; i < trace_nr; i++) {
  247. vma = find_vma(current->mm, ips[i]);
  248. if (!vma || stack_map_get_build_id(vma, id_offs[i].build_id)) {
  249. /* per entry fall back to ips */
  250. id_offs[i].status = BPF_STACK_BUILD_ID_IP;
  251. id_offs[i].ip = ips[i];
  252. continue;
  253. }
  254. id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
  255. - vma->vm_start;
  256. id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
  257. }
  258. up_read(&current->mm->mmap_sem);
  259. }
  260. BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
  261. u64, flags)
  262. {
  263. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  264. struct perf_callchain_entry *trace;
  265. struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
  266. u32 max_depth = map->value_size / stack_map_data_size(map);
  267. /* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
  268. u32 init_nr = sysctl_perf_event_max_stack - max_depth;
  269. u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
  270. u32 hash, id, trace_nr, trace_len;
  271. bool user = flags & BPF_F_USER_STACK;
  272. bool kernel = !user;
  273. u64 *ips;
  274. bool hash_matches;
  275. if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
  276. BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
  277. return -EINVAL;
  278. trace = get_perf_callchain(regs, init_nr, kernel, user,
  279. sysctl_perf_event_max_stack, false, false);
  280. if (unlikely(!trace))
  281. /* couldn't fetch the stack trace */
  282. return -EFAULT;
  283. /* get_perf_callchain() guarantees that trace->nr >= init_nr
  284. * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
  285. */
  286. trace_nr = trace->nr - init_nr;
  287. if (trace_nr <= skip)
  288. /* skipping more than usable stack trace */
  289. return -EFAULT;
  290. trace_nr -= skip;
  291. trace_len = trace_nr * sizeof(u64);
  292. ips = trace->ip + skip + init_nr;
  293. hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
  294. id = hash & (smap->n_buckets - 1);
  295. bucket = READ_ONCE(smap->buckets[id]);
  296. hash_matches = bucket && bucket->hash == hash;
  297. /* fast cmp */
  298. if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
  299. return id;
  300. if (stack_map_use_build_id(map)) {
  301. /* for build_id+offset, pop a bucket before slow cmp */
  302. new_bucket = (struct stack_map_bucket *)
  303. pcpu_freelist_pop(&smap->freelist);
  304. if (unlikely(!new_bucket))
  305. return -ENOMEM;
  306. stack_map_get_build_id_offset(map, new_bucket, ips,
  307. trace_nr, user);
  308. trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
  309. if (hash_matches && bucket->nr == trace_nr &&
  310. memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
  311. pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
  312. return id;
  313. }
  314. if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
  315. pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
  316. return -EEXIST;
  317. }
  318. } else {
  319. if (hash_matches && bucket->nr == trace_nr &&
  320. memcmp(bucket->data, ips, trace_len) == 0)
  321. return id;
  322. if (bucket && !(flags & BPF_F_REUSE_STACKID))
  323. return -EEXIST;
  324. new_bucket = (struct stack_map_bucket *)
  325. pcpu_freelist_pop(&smap->freelist);
  326. if (unlikely(!new_bucket))
  327. return -ENOMEM;
  328. memcpy(new_bucket->data, ips, trace_len);
  329. }
  330. new_bucket->hash = hash;
  331. new_bucket->nr = trace_nr;
  332. old_bucket = xchg(&smap->buckets[id], new_bucket);
  333. if (old_bucket)
  334. pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
  335. return id;
  336. }
  337. const struct bpf_func_proto bpf_get_stackid_proto = {
  338. .func = bpf_get_stackid,
  339. .gpl_only = true,
  340. .ret_type = RET_INTEGER,
  341. .arg1_type = ARG_PTR_TO_CTX,
  342. .arg2_type = ARG_CONST_MAP_PTR,
  343. .arg3_type = ARG_ANYTHING,
  344. };
  345. /* Called from eBPF program */
  346. static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
  347. {
  348. return NULL;
  349. }
  350. /* Called from syscall */
  351. int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
  352. {
  353. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  354. struct stack_map_bucket *bucket, *old_bucket;
  355. u32 id = *(u32 *)key, trace_len;
  356. if (unlikely(id >= smap->n_buckets))
  357. return -ENOENT;
  358. bucket = xchg(&smap->buckets[id], NULL);
  359. if (!bucket)
  360. return -ENOENT;
  361. trace_len = bucket->nr * stack_map_data_size(map);
  362. memcpy(value, bucket->data, trace_len);
  363. memset(value + trace_len, 0, map->value_size - trace_len);
  364. old_bucket = xchg(&smap->buckets[id], bucket);
  365. if (old_bucket)
  366. pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
  367. return 0;
  368. }
  369. static int stack_map_get_next_key(struct bpf_map *map, void *key,
  370. void *next_key)
  371. {
  372. struct bpf_stack_map *smap = container_of(map,
  373. struct bpf_stack_map, map);
  374. u32 id;
  375. WARN_ON_ONCE(!rcu_read_lock_held());
  376. if (!key) {
  377. id = 0;
  378. } else {
  379. id = *(u32 *)key;
  380. if (id >= smap->n_buckets || !smap->buckets[id])
  381. id = 0;
  382. else
  383. id++;
  384. }
  385. while (id < smap->n_buckets && !smap->buckets[id])
  386. id++;
  387. if (id >= smap->n_buckets)
  388. return -ENOENT;
  389. *(u32 *)next_key = id;
  390. return 0;
  391. }
  392. static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
  393. u64 map_flags)
  394. {
  395. return -EINVAL;
  396. }
  397. /* Called from syscall or from eBPF program */
  398. static int stack_map_delete_elem(struct bpf_map *map, void *key)
  399. {
  400. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  401. struct stack_map_bucket *old_bucket;
  402. u32 id = *(u32 *)key;
  403. if (unlikely(id >= smap->n_buckets))
  404. return -E2BIG;
  405. old_bucket = xchg(&smap->buckets[id], NULL);
  406. if (old_bucket) {
  407. pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
  408. return 0;
  409. } else {
  410. return -ENOENT;
  411. }
  412. }
  413. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  414. static void stack_map_free(struct bpf_map *map)
  415. {
  416. struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
  417. /* wait for bpf programs to complete before freeing stack map */
  418. synchronize_rcu();
  419. bpf_map_area_free(smap->elems);
  420. pcpu_freelist_destroy(&smap->freelist);
  421. bpf_map_area_free(smap);
  422. put_callchain_buffers();
  423. }
  424. const struct bpf_map_ops stack_map_ops = {
  425. .map_alloc = stack_map_alloc,
  426. .map_free = stack_map_free,
  427. .map_get_next_key = stack_map_get_next_key,
  428. .map_lookup_elem = stack_map_lookup_elem,
  429. .map_update_elem = stack_map_update_elem,
  430. .map_delete_elem = stack_map_delete_elem,
  431. };