1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
5 #include <linux/jhash.h>
6 #include <linux/filter.h>
7 #include <linux/kernel.h>
8 #include <linux/stacktrace.h>
9 #include <linux/perf_event.h>
10 #include <linux/elf.h>
11 #include <linux/pagemap.h>
12 #include <linux/irq_work.h>
13 #include <linux/btf_ids.h>
14 #include "percpu_freelist.h"
16 #define STACK_CREATE_FLAG_MASK \
17 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY | \
20 struct stack_map_bucket {
21 struct pcpu_freelist_node fnode;
27 struct bpf_stack_map {
30 struct pcpu_freelist freelist;
32 struct stack_map_bucket *buckets[];
35 /* irq_work to run up_read() for build_id lookup in nmi context */
36 struct stack_map_irq_work {
37 struct irq_work irq_work;
41 static void do_up_read(struct irq_work *entry)
43 struct stack_map_irq_work *work;
45 if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
48 work = container_of(entry, struct stack_map_irq_work, irq_work);
49 mmap_read_unlock_non_owner(work->mm);
52 static DEFINE_PER_CPU(struct stack_map_irq_work, up_read_work);
54 static inline bool stack_map_use_build_id(struct bpf_map *map)
56 return (map->map_flags & BPF_F_STACK_BUILD_ID);
59 static inline int stack_map_data_size(struct bpf_map *map)
61 return stack_map_use_build_id(map) ?
62 sizeof(struct bpf_stack_build_id) : sizeof(u64);
65 static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
67 u64 elem_size = sizeof(struct stack_map_bucket) +
68 (u64)smap->map.value_size;
71 smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
76 err = pcpu_freelist_init(&smap->freelist);
80 pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
81 smap->map.max_entries);
85 bpf_map_area_free(smap->elems);
89 /* Called from syscall */
90 static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
92 u32 value_size = attr->value_size;
93 struct bpf_stack_map *smap;
94 struct bpf_map_memory mem;
99 return ERR_PTR(-EPERM);
101 if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
102 return ERR_PTR(-EINVAL);
104 /* check sanity of attributes */
105 if (attr->max_entries == 0 || attr->key_size != 4 ||
106 value_size < 8 || value_size % 8)
107 return ERR_PTR(-EINVAL);
109 BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
110 if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
111 if (value_size % sizeof(struct bpf_stack_build_id) ||
112 value_size / sizeof(struct bpf_stack_build_id)
113 > sysctl_perf_event_max_stack)
114 return ERR_PTR(-EINVAL);
115 } else if (value_size / 8 > sysctl_perf_event_max_stack)
116 return ERR_PTR(-EINVAL);
118 /* hash table size must be power of 2; roundup_pow_of_two() can overflow
119 * into UB on 32-bit arches, so check that first
121 if (attr->max_entries > 1UL << 31)
122 return ERR_PTR(-E2BIG);
124 n_buckets = roundup_pow_of_two(attr->max_entries);
126 cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
127 err = bpf_map_charge_init(&mem, cost + attr->max_entries *
128 (sizeof(struct stack_map_bucket) + (u64)value_size));
132 smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
134 bpf_map_charge_finish(&mem);
135 return ERR_PTR(-ENOMEM);
138 bpf_map_init_from_attr(&smap->map, attr);
139 smap->map.value_size = value_size;
140 smap->n_buckets = n_buckets;
142 err = get_callchain_buffers(sysctl_perf_event_max_stack);
146 err = prealloc_elems_and_freelist(smap);
150 bpf_map_charge_move(&smap->map.memory, &mem);
155 put_callchain_buffers();
157 bpf_map_charge_finish(&mem);
158 bpf_map_area_free(smap);
162 #define BPF_BUILD_ID 3
164 * Parse build id from the note segment. This logic can be shared between
165 * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
168 static inline int stack_map_parse_build_id(void *page_addr,
169 unsigned char *build_id,
171 Elf32_Word note_size)
173 Elf32_Word note_offs = 0, new_offs;
175 /* check for overflow */
176 if (note_start < page_addr || note_start + note_size < note_start)
179 /* only supports note that fits in the first page */
180 if (note_start + note_size > page_addr + PAGE_SIZE)
183 while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
184 Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
186 if (nhdr->n_type == BPF_BUILD_ID &&
187 nhdr->n_namesz == sizeof("GNU") &&
188 nhdr->n_descsz > 0 &&
189 nhdr->n_descsz <= BPF_BUILD_ID_SIZE) {
191 note_start + note_offs +
192 ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
194 memset(build_id + nhdr->n_descsz, 0,
195 BPF_BUILD_ID_SIZE - nhdr->n_descsz);
198 new_offs = note_offs + sizeof(Elf32_Nhdr) +
199 ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
200 if (new_offs <= note_offs) /* overflow */
202 note_offs = new_offs;
207 /* Parse build ID from 32-bit ELF */
208 static int stack_map_get_build_id_32(void *page_addr,
209 unsigned char *build_id)
211 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
215 /* only supports phdr that fits in one page */
217 (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
220 phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
222 for (i = 0; i < ehdr->e_phnum; ++i) {
223 if (phdr[i].p_type == PT_NOTE &&
224 !stack_map_parse_build_id(page_addr, build_id,
225 page_addr + phdr[i].p_offset,
232 /* Parse build ID from 64-bit ELF */
233 static int stack_map_get_build_id_64(void *page_addr,
234 unsigned char *build_id)
236 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
240 /* only supports phdr that fits in one page */
242 (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
245 phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
247 for (i = 0; i < ehdr->e_phnum; ++i) {
248 if (phdr[i].p_type == PT_NOTE &&
249 !stack_map_parse_build_id(page_addr, build_id,
250 page_addr + phdr[i].p_offset,
257 /* Parse build ID of ELF file mapped to vma */
258 static int stack_map_get_build_id(struct vm_area_struct *vma,
259 unsigned char *build_id)
266 /* only works for page backed storage */
270 page = find_get_page(vma->vm_file->f_mapping, 0);
272 return -EFAULT; /* page not mapped */
275 page_addr = kmap_atomic(page);
276 ehdr = (Elf32_Ehdr *)page_addr;
278 /* compare magic x7f "ELF" */
279 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
282 /* only support executable file and shared object file */
283 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
286 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
287 ret = stack_map_get_build_id_32(page_addr, build_id);
288 else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
289 ret = stack_map_get_build_id_64(page_addr, build_id);
291 kunmap_atomic(page_addr);
296 static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
297 u64 *ips, u32 trace_nr, bool user)
300 struct vm_area_struct *vma;
301 bool irq_work_busy = false;
302 struct stack_map_irq_work *work = NULL;
304 if (irqs_disabled()) {
305 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
306 work = this_cpu_ptr(&up_read_work);
307 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY) {
308 /* cannot queue more up_read, fallback */
309 irq_work_busy = true;
313 * PREEMPT_RT does not allow to trylock mmap sem in
314 * interrupt disabled context. Force the fallback code.
316 irq_work_busy = true;
321 * We cannot do up_read() when the irq is disabled, because of
322 * risk to deadlock with rq_lock. To do build_id lookup when the
323 * irqs are disabled, we need to run up_read() in irq_work. We use
324 * a percpu variable to do the irq_work. If the irq_work is
325 * already used by another lookup, we fall back to report ips.
327 * Same fallback is used for kernel stack (!user) on a stackmap
330 if (!user || !current || !current->mm || irq_work_busy ||
331 !mmap_read_trylock_non_owner(current->mm)) {
332 /* cannot access current->mm, fall back to ips */
333 for (i = 0; i < trace_nr; i++) {
334 id_offs[i].status = BPF_STACK_BUILD_ID_IP;
335 id_offs[i].ip = ips[i];
336 memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE);
341 for (i = 0; i < trace_nr; i++) {
342 vma = find_vma(current->mm, ips[i]);
343 if (!vma || stack_map_get_build_id(vma, id_offs[i].build_id)) {
344 /* per entry fall back to ips */
345 id_offs[i].status = BPF_STACK_BUILD_ID_IP;
346 id_offs[i].ip = ips[i];
347 memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE);
350 id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
352 id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
356 mmap_read_unlock_non_owner(current->mm);
358 work->mm = current->mm;
359 irq_work_queue(&work->irq_work);
363 static struct perf_callchain_entry *
364 get_callchain_entry_for_task(struct task_struct *task, u32 max_depth)
366 #ifdef CONFIG_STACKTRACE
367 struct perf_callchain_entry *entry;
370 entry = get_callchain_entry(&rctx);
375 entry->nr = stack_trace_save_tsk(task, (unsigned long *)entry->ip,
378 /* stack_trace_save_tsk() works on unsigned long array, while
379 * perf_callchain_entry uses u64 array. For 32-bit systems, it is
380 * necessary to fix this mismatch.
382 if (__BITS_PER_LONG != 64) {
383 unsigned long *from = (unsigned long *) entry->ip;
387 /* copy data from the end to avoid using extra buffer */
388 for (i = entry->nr - 1; i >= 0; i--)
389 to[i] = (u64)(from[i]);
392 put_callchain_entry(rctx);
395 #else /* CONFIG_STACKTRACE */
400 static long __bpf_get_stackid(struct bpf_map *map,
401 struct perf_callchain_entry *trace, u64 flags)
403 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
404 struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
405 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
406 u32 hash, id, trace_nr, trace_len;
407 bool user = flags & BPF_F_USER_STACK;
411 if (trace->nr <= skip)
412 /* skipping more than usable stack trace */
415 trace_nr = trace->nr - skip;
416 trace_len = trace_nr * sizeof(u64);
417 ips = trace->ip + skip;
418 hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
419 id = hash & (smap->n_buckets - 1);
420 bucket = READ_ONCE(smap->buckets[id]);
422 hash_matches = bucket && bucket->hash == hash;
424 if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
427 if (stack_map_use_build_id(map)) {
428 /* for build_id+offset, pop a bucket before slow cmp */
429 new_bucket = (struct stack_map_bucket *)
430 pcpu_freelist_pop(&smap->freelist);
431 if (unlikely(!new_bucket))
433 new_bucket->nr = trace_nr;
434 stack_map_get_build_id_offset(
435 (struct bpf_stack_build_id *)new_bucket->data,
436 ips, trace_nr, user);
437 trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
438 if (hash_matches && bucket->nr == trace_nr &&
439 memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
440 pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
443 if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
444 pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
448 if (hash_matches && bucket->nr == trace_nr &&
449 memcmp(bucket->data, ips, trace_len) == 0)
451 if (bucket && !(flags & BPF_F_REUSE_STACKID))
454 new_bucket = (struct stack_map_bucket *)
455 pcpu_freelist_pop(&smap->freelist);
456 if (unlikely(!new_bucket))
458 memcpy(new_bucket->data, ips, trace_len);
461 new_bucket->hash = hash;
462 new_bucket->nr = trace_nr;
464 old_bucket = xchg(&smap->buckets[id], new_bucket);
466 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
470 BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
473 u32 max_depth = map->value_size / stack_map_data_size(map);
474 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
475 bool user = flags & BPF_F_USER_STACK;
476 struct perf_callchain_entry *trace;
479 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
480 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
484 if (max_depth > sysctl_perf_event_max_stack)
485 max_depth = sysctl_perf_event_max_stack;
487 trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
490 if (unlikely(!trace))
491 /* couldn't fetch the stack trace */
494 return __bpf_get_stackid(map, trace, flags);
497 const struct bpf_func_proto bpf_get_stackid_proto = {
498 .func = bpf_get_stackid,
500 .ret_type = RET_INTEGER,
501 .arg1_type = ARG_PTR_TO_CTX,
502 .arg2_type = ARG_CONST_MAP_PTR,
503 .arg3_type = ARG_ANYTHING,
506 static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
510 while (nr_kernel < trace->nr) {
511 if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
518 BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
519 struct bpf_map *, map, u64, flags)
521 struct perf_event *event = ctx->event;
522 struct perf_callchain_entry *trace;
527 /* perf_sample_data doesn't have callchain, use bpf_get_stackid */
528 if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
529 return bpf_get_stackid((unsigned long)(ctx->regs),
530 (unsigned long) map, flags, 0, 0);
532 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
533 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
536 user = flags & BPF_F_USER_STACK;
539 trace = ctx->data->callchain;
540 if (unlikely(!trace))
543 nr_kernel = count_kernel_ip(trace);
546 __u64 nr = trace->nr;
548 trace->nr = nr_kernel;
549 ret = __bpf_get_stackid(map, trace, flags);
554 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
557 if (skip > BPF_F_SKIP_FIELD_MASK)
560 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
561 ret = __bpf_get_stackid(map, trace, flags);
566 const struct bpf_func_proto bpf_get_stackid_proto_pe = {
567 .func = bpf_get_stackid_pe,
569 .ret_type = RET_INTEGER,
570 .arg1_type = ARG_PTR_TO_CTX,
571 .arg2_type = ARG_CONST_MAP_PTR,
572 .arg3_type = ARG_ANYTHING,
575 static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
576 struct perf_callchain_entry *trace_in,
577 void *buf, u32 size, u64 flags)
579 u32 trace_nr, copy_len, elem_size, num_elem, max_depth;
580 bool user_build_id = flags & BPF_F_USER_BUILD_ID;
581 bool crosstask = task && task != current;
582 u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
583 bool user = flags & BPF_F_USER_STACK;
584 struct perf_callchain_entry *trace;
589 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
590 BPF_F_USER_BUILD_ID)))
592 if (kernel && user_build_id)
595 elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
597 if (unlikely(size % elem_size))
600 /* cannot get valid user stack for task without user_mode regs */
601 if (task && user && !user_mode(regs))
604 /* get_perf_callchain does not support crosstask user stack walking
605 * but returns an empty stack instead of NULL.
607 if (crosstask && user) {
612 num_elem = size / elem_size;
613 max_depth = num_elem + skip;
614 if (sysctl_perf_event_max_stack < max_depth)
615 max_depth = sysctl_perf_event_max_stack;
619 else if (kernel && task)
620 trace = get_callchain_entry_for_task(task, max_depth);
622 trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
624 if (unlikely(!trace))
627 if (trace->nr < skip)
630 trace_nr = trace->nr - skip;
631 trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
632 copy_len = trace_nr * elem_size;
634 ips = trace->ip + skip;
635 if (user && user_build_id)
636 stack_map_get_build_id_offset(buf, ips, trace_nr, user);
638 memcpy(buf, ips, copy_len);
641 memset(buf + copy_len, 0, size - copy_len);
647 memset(buf, 0, size);
651 BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
654 return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
657 const struct bpf_func_proto bpf_get_stack_proto = {
658 .func = bpf_get_stack,
660 .ret_type = RET_INTEGER,
661 .arg1_type = ARG_PTR_TO_CTX,
662 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
663 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
664 .arg4_type = ARG_ANYTHING,
667 BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
668 u32, size, u64, flags)
670 struct pt_regs *regs;
673 if (!try_get_task_stack(task))
676 regs = task_pt_regs(task);
678 res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
679 put_task_stack(task);
684 BTF_ID_LIST_SINGLE(bpf_get_task_stack_btf_ids, struct, task_struct)
686 const struct bpf_func_proto bpf_get_task_stack_proto = {
687 .func = bpf_get_task_stack,
689 .ret_type = RET_INTEGER,
690 .arg1_type = ARG_PTR_TO_BTF_ID,
691 .arg1_btf_id = &bpf_get_task_stack_btf_ids[0],
692 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
693 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
694 .arg4_type = ARG_ANYTHING,
697 BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
698 void *, buf, u32, size, u64, flags)
700 struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
701 struct perf_event *event = ctx->event;
702 struct perf_callchain_entry *trace;
707 if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
708 return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
710 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
711 BPF_F_USER_BUILD_ID)))
714 user = flags & BPF_F_USER_STACK;
718 trace = ctx->data->callchain;
719 if (unlikely(!trace))
722 nr_kernel = count_kernel_ip(trace);
725 __u64 nr = trace->nr;
727 trace->nr = nr_kernel;
728 err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
733 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
736 if (skip > BPF_F_SKIP_FIELD_MASK)
739 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
740 err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
745 memset(buf, 0, size);
750 const struct bpf_func_proto bpf_get_stack_proto_pe = {
751 .func = bpf_get_stack_pe,
753 .ret_type = RET_INTEGER,
754 .arg1_type = ARG_PTR_TO_CTX,
755 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
756 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
757 .arg4_type = ARG_ANYTHING,
760 /* Called from eBPF program */
761 static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
763 return ERR_PTR(-EOPNOTSUPP);
766 /* Called from syscall */
767 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
769 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
770 struct stack_map_bucket *bucket, *old_bucket;
771 u32 id = *(u32 *)key, trace_len;
773 if (unlikely(id >= smap->n_buckets))
776 bucket = xchg(&smap->buckets[id], NULL);
780 trace_len = bucket->nr * stack_map_data_size(map);
781 memcpy(value, bucket->data, trace_len);
782 memset(value + trace_len, 0, map->value_size - trace_len);
784 old_bucket = xchg(&smap->buckets[id], bucket);
786 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
790 static int stack_map_get_next_key(struct bpf_map *map, void *key,
793 struct bpf_stack_map *smap = container_of(map,
794 struct bpf_stack_map, map);
797 WARN_ON_ONCE(!rcu_read_lock_held());
803 if (id >= smap->n_buckets || !smap->buckets[id])
809 while (id < smap->n_buckets && !smap->buckets[id])
812 if (id >= smap->n_buckets)
815 *(u32 *)next_key = id;
819 static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
825 /* Called from syscall or from eBPF program */
826 static int stack_map_delete_elem(struct bpf_map *map, void *key)
828 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
829 struct stack_map_bucket *old_bucket;
830 u32 id = *(u32 *)key;
832 if (unlikely(id >= smap->n_buckets))
835 old_bucket = xchg(&smap->buckets[id], NULL);
837 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
844 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
845 static void stack_map_free(struct bpf_map *map)
847 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
849 bpf_map_area_free(smap->elems);
850 pcpu_freelist_destroy(&smap->freelist);
851 bpf_map_area_free(smap);
852 put_callchain_buffers();
855 static int stack_trace_map_btf_id;
856 const struct bpf_map_ops stack_trace_map_ops = {
857 .map_meta_equal = bpf_map_meta_equal,
858 .map_alloc = stack_map_alloc,
859 .map_free = stack_map_free,
860 .map_get_next_key = stack_map_get_next_key,
861 .map_lookup_elem = stack_map_lookup_elem,
862 .map_update_elem = stack_map_update_elem,
863 .map_delete_elem = stack_map_delete_elem,
864 .map_check_btf = map_check_no_btf,
865 .map_btf_name = "bpf_stack_map",
866 .map_btf_id = &stack_trace_map_btf_id,
869 static int __init stack_map_init(void)
872 struct stack_map_irq_work *work;
874 for_each_possible_cpu(cpu) {
875 work = per_cpu_ptr(&up_read_work, cpu);
876 init_irq_work(&work->irq_work, do_up_read);
880 subsys_initcall(stack_map_init);