stackmap.c 17 KB

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