stackmap.c 17 KB

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