xref: /linux-6.15/kernel/bpf/stackmap.c (revision bd7525da)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d5a3b1f6SAlexei Starovoitov /* Copyright (c) 2016 Facebook
3d5a3b1f6SAlexei Starovoitov  */
4d5a3b1f6SAlexei Starovoitov #include <linux/bpf.h>
5d5a3b1f6SAlexei Starovoitov #include <linux/jhash.h>
6d5a3b1f6SAlexei Starovoitov #include <linux/filter.h>
77b04d6d6SSong Liu #include <linux/kernel.h>
8d5a3b1f6SAlexei Starovoitov #include <linux/stacktrace.h>
9d5a3b1f6SAlexei Starovoitov #include <linux/perf_event.h>
10bae77c5eSSong Liu #include <linux/irq_work.h>
11c9a0f3b8SJiri Olsa #include <linux/btf_ids.h>
12*bd7525daSJiri Olsa #include <linux/buildid.h>
13557c0c6eSAlexei Starovoitov #include "percpu_freelist.h"
14d5a3b1f6SAlexei Starovoitov 
156e71b04aSChenbo Feng #define STACK_CREATE_FLAG_MASK					\
16615755a7SSong Liu 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY |	\
17615755a7SSong Liu 	 BPF_F_STACK_BUILD_ID)
186e71b04aSChenbo Feng 
19d5a3b1f6SAlexei Starovoitov struct stack_map_bucket {
20557c0c6eSAlexei Starovoitov 	struct pcpu_freelist_node fnode;
21d5a3b1f6SAlexei Starovoitov 	u32 hash;
22d5a3b1f6SAlexei Starovoitov 	u32 nr;
23615755a7SSong Liu 	u64 data[];
24d5a3b1f6SAlexei Starovoitov };
25d5a3b1f6SAlexei Starovoitov 
26d5a3b1f6SAlexei Starovoitov struct bpf_stack_map {
27d5a3b1f6SAlexei Starovoitov 	struct bpf_map map;
28557c0c6eSAlexei Starovoitov 	void *elems;
29557c0c6eSAlexei Starovoitov 	struct pcpu_freelist freelist;
30d5a3b1f6SAlexei Starovoitov 	u32 n_buckets;
31557c0c6eSAlexei Starovoitov 	struct stack_map_bucket *buckets[];
32d5a3b1f6SAlexei Starovoitov };
33d5a3b1f6SAlexei Starovoitov 
34bae77c5eSSong Liu /* irq_work to run up_read() for build_id lookup in nmi context */
35bae77c5eSSong Liu struct stack_map_irq_work {
36bae77c5eSSong Liu 	struct irq_work irq_work;
370cc55a02SMichel Lespinasse 	struct mm_struct *mm;
38bae77c5eSSong Liu };
39bae77c5eSSong Liu 
40bae77c5eSSong Liu static void do_up_read(struct irq_work *entry)
41bae77c5eSSong Liu {
42bae77c5eSSong Liu 	struct stack_map_irq_work *work;
43bae77c5eSSong Liu 
44099bfaa7SDavid Miller 	if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
45099bfaa7SDavid Miller 		return;
46099bfaa7SDavid Miller 
47bae77c5eSSong Liu 	work = container_of(entry, struct stack_map_irq_work, irq_work);
480cc55a02SMichel Lespinasse 	mmap_read_unlock_non_owner(work->mm);
49bae77c5eSSong Liu }
50bae77c5eSSong Liu 
51bae77c5eSSong Liu static DEFINE_PER_CPU(struct stack_map_irq_work, up_read_work);
52bae77c5eSSong Liu 
53615755a7SSong Liu static inline bool stack_map_use_build_id(struct bpf_map *map)
54615755a7SSong Liu {
55615755a7SSong Liu 	return (map->map_flags & BPF_F_STACK_BUILD_ID);
56615755a7SSong Liu }
57615755a7SSong Liu 
58615755a7SSong Liu static inline int stack_map_data_size(struct bpf_map *map)
59615755a7SSong Liu {
60615755a7SSong Liu 	return stack_map_use_build_id(map) ?
61615755a7SSong Liu 		sizeof(struct bpf_stack_build_id) : sizeof(u64);
62615755a7SSong Liu }
63615755a7SSong Liu 
64557c0c6eSAlexei Starovoitov static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
65557c0c6eSAlexei Starovoitov {
66557c0c6eSAlexei Starovoitov 	u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
67557c0c6eSAlexei Starovoitov 	int err;
68557c0c6eSAlexei Starovoitov 
6996eabe7aSMartin KaFai Lau 	smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
7096eabe7aSMartin KaFai Lau 					 smap->map.numa_node);
71557c0c6eSAlexei Starovoitov 	if (!smap->elems)
72557c0c6eSAlexei Starovoitov 		return -ENOMEM;
73557c0c6eSAlexei Starovoitov 
74557c0c6eSAlexei Starovoitov 	err = pcpu_freelist_init(&smap->freelist);
75557c0c6eSAlexei Starovoitov 	if (err)
76557c0c6eSAlexei Starovoitov 		goto free_elems;
77557c0c6eSAlexei Starovoitov 
78557c0c6eSAlexei Starovoitov 	pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
79557c0c6eSAlexei Starovoitov 			       smap->map.max_entries);
80557c0c6eSAlexei Starovoitov 	return 0;
81557c0c6eSAlexei Starovoitov 
82557c0c6eSAlexei Starovoitov free_elems:
83d407bd25SDaniel Borkmann 	bpf_map_area_free(smap->elems);
84557c0c6eSAlexei Starovoitov 	return err;
85557c0c6eSAlexei Starovoitov }
86557c0c6eSAlexei Starovoitov 
87d5a3b1f6SAlexei Starovoitov /* Called from syscall */
88d5a3b1f6SAlexei Starovoitov static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
89d5a3b1f6SAlexei Starovoitov {
90d5a3b1f6SAlexei Starovoitov 	u32 value_size = attr->value_size;
91d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap;
92d5a3b1f6SAlexei Starovoitov 	u64 cost, n_buckets;
93d5a3b1f6SAlexei Starovoitov 	int err;
94d5a3b1f6SAlexei Starovoitov 
952c78ee89SAlexei Starovoitov 	if (!bpf_capable())
96d5a3b1f6SAlexei Starovoitov 		return ERR_PTR(-EPERM);
97d5a3b1f6SAlexei Starovoitov 
986e71b04aSChenbo Feng 	if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
99823707b6SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
100823707b6SAlexei Starovoitov 
101d5a3b1f6SAlexei Starovoitov 	/* check sanity of attributes */
102d5a3b1f6SAlexei Starovoitov 	if (attr->max_entries == 0 || attr->key_size != 4 ||
103615755a7SSong Liu 	    value_size < 8 || value_size % 8)
104615755a7SSong Liu 		return ERR_PTR(-EINVAL);
105615755a7SSong Liu 
106615755a7SSong Liu 	BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
107615755a7SSong Liu 	if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
108615755a7SSong Liu 		if (value_size % sizeof(struct bpf_stack_build_id) ||
109615755a7SSong Liu 		    value_size / sizeof(struct bpf_stack_build_id)
110615755a7SSong Liu 		    > sysctl_perf_event_max_stack)
111615755a7SSong Liu 			return ERR_PTR(-EINVAL);
112615755a7SSong Liu 	} else if (value_size / 8 > sysctl_perf_event_max_stack)
113d5a3b1f6SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
114d5a3b1f6SAlexei Starovoitov 
115d5a3b1f6SAlexei Starovoitov 	/* hash table size must be power of 2 */
116d5a3b1f6SAlexei Starovoitov 	n_buckets = roundup_pow_of_two(attr->max_entries);
117d5a3b1f6SAlexei Starovoitov 
118d5a3b1f6SAlexei Starovoitov 	cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
119d5a3b1f6SAlexei Starovoitov 	cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
120b936ca64SRoman Gushchin 	smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
12137086810SRoman Gushchin 	if (!smap)
122b936ca64SRoman Gushchin 		return ERR_PTR(-ENOMEM);
123d5a3b1f6SAlexei Starovoitov 
124bd475643SJakub Kicinski 	bpf_map_init_from_attr(&smap->map, attr);
125d5a3b1f6SAlexei Starovoitov 	smap->map.value_size = value_size;
126d5a3b1f6SAlexei Starovoitov 	smap->n_buckets = n_buckets;
127557c0c6eSAlexei Starovoitov 
12897c79a38SArnaldo Carvalho de Melo 	err = get_callchain_buffers(sysctl_perf_event_max_stack);
129d5a3b1f6SAlexei Starovoitov 	if (err)
13037086810SRoman Gushchin 		goto free_smap;
131d5a3b1f6SAlexei Starovoitov 
132557c0c6eSAlexei Starovoitov 	err = prealloc_elems_and_freelist(smap);
133557c0c6eSAlexei Starovoitov 	if (err)
134557c0c6eSAlexei Starovoitov 		goto put_buffers;
135557c0c6eSAlexei Starovoitov 
136d5a3b1f6SAlexei Starovoitov 	return &smap->map;
137d5a3b1f6SAlexei Starovoitov 
138557c0c6eSAlexei Starovoitov put_buffers:
139557c0c6eSAlexei Starovoitov 	put_callchain_buffers();
14037086810SRoman Gushchin free_smap:
141d407bd25SDaniel Borkmann 	bpf_map_area_free(smap);
142d5a3b1f6SAlexei Starovoitov 	return ERR_PTR(err);
143d5a3b1f6SAlexei Starovoitov }
144d5a3b1f6SAlexei Starovoitov 
1455f412632SYonghong Song static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
146615755a7SSong Liu 					  u64 *ips, u32 trace_nr, bool user)
147615755a7SSong Liu {
148615755a7SSong Liu 	int i;
149615755a7SSong Liu 	struct vm_area_struct *vma;
150bae77c5eSSong Liu 	bool irq_work_busy = false;
151dc3b8ae9SArnd Bergmann 	struct stack_map_irq_work *work = NULL;
152bae77c5eSSong Liu 
153eac9153fSSong Liu 	if (irqs_disabled()) {
154099bfaa7SDavid Miller 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
155bae77c5eSSong Liu 			work = this_cpu_ptr(&up_read_work);
1567a9f50a0SPeter Zijlstra 			if (irq_work_is_busy(&work->irq_work)) {
157bae77c5eSSong Liu 				/* cannot queue more up_read, fallback */
158bae77c5eSSong Liu 				irq_work_busy = true;
159bae77c5eSSong Liu 			}
160099bfaa7SDavid Miller 		} else {
161099bfaa7SDavid Miller 			/*
162099bfaa7SDavid Miller 			 * PREEMPT_RT does not allow to trylock mmap sem in
163099bfaa7SDavid Miller 			 * interrupt disabled context. Force the fallback code.
164099bfaa7SDavid Miller 			 */
165099bfaa7SDavid Miller 			irq_work_busy = true;
166099bfaa7SDavid Miller 		}
167099bfaa7SDavid Miller 	}
168615755a7SSong Liu 
169615755a7SSong Liu 	/*
170eac9153fSSong Liu 	 * We cannot do up_read() when the irq is disabled, because of
171eac9153fSSong Liu 	 * risk to deadlock with rq_lock. To do build_id lookup when the
172eac9153fSSong Liu 	 * irqs are disabled, we need to run up_read() in irq_work. We use
173bae77c5eSSong Liu 	 * a percpu variable to do the irq_work. If the irq_work is
174bae77c5eSSong Liu 	 * already used by another lookup, we fall back to report ips.
175615755a7SSong Liu 	 *
176615755a7SSong Liu 	 * Same fallback is used for kernel stack (!user) on a stackmap
177615755a7SSong Liu 	 * with build_id.
178615755a7SSong Liu 	 */
179bae77c5eSSong Liu 	if (!user || !current || !current->mm || irq_work_busy ||
1800cc55a02SMichel Lespinasse 	    !mmap_read_trylock_non_owner(current->mm)) {
181615755a7SSong Liu 		/* cannot access current->mm, fall back to ips */
182615755a7SSong Liu 		for (i = 0; i < trace_nr; i++) {
183615755a7SSong Liu 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
184615755a7SSong Liu 			id_offs[i].ip = ips[i];
185*bd7525daSJiri Olsa 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
186615755a7SSong Liu 		}
187615755a7SSong Liu 		return;
188615755a7SSong Liu 	}
189615755a7SSong Liu 
190615755a7SSong Liu 	for (i = 0; i < trace_nr; i++) {
191615755a7SSong Liu 		vma = find_vma(current->mm, ips[i]);
192*bd7525daSJiri Olsa 		if (!vma || build_id_parse(vma, id_offs[i].build_id)) {
193615755a7SSong Liu 			/* per entry fall back to ips */
194615755a7SSong Liu 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
195615755a7SSong Liu 			id_offs[i].ip = ips[i];
196*bd7525daSJiri Olsa 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
197615755a7SSong Liu 			continue;
198615755a7SSong Liu 		}
199615755a7SSong Liu 		id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
200615755a7SSong Liu 			- vma->vm_start;
201615755a7SSong Liu 		id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
202615755a7SSong Liu 	}
203bae77c5eSSong Liu 
204dc3b8ae9SArnd Bergmann 	if (!work) {
2050cc55a02SMichel Lespinasse 		mmap_read_unlock_non_owner(current->mm);
206bae77c5eSSong Liu 	} else {
2070cc55a02SMichel Lespinasse 		work->mm = current->mm;
208bae77c5eSSong Liu 		irq_work_queue(&work->irq_work);
209bae77c5eSSong Liu 	}
210615755a7SSong Liu }
211615755a7SSong Liu 
212fa28dcb8SSong Liu static struct perf_callchain_entry *
213fa28dcb8SSong Liu get_callchain_entry_for_task(struct task_struct *task, u32 init_nr)
214fa28dcb8SSong Liu {
215046cc3ddSSong Liu #ifdef CONFIG_STACKTRACE
216fa28dcb8SSong Liu 	struct perf_callchain_entry *entry;
217fa28dcb8SSong Liu 	int rctx;
218fa28dcb8SSong Liu 
219fa28dcb8SSong Liu 	entry = get_callchain_entry(&rctx);
220fa28dcb8SSong Liu 
221fa28dcb8SSong Liu 	if (!entry)
222fa28dcb8SSong Liu 		return NULL;
223fa28dcb8SSong Liu 
224fa28dcb8SSong Liu 	entry->nr = init_nr +
225fa28dcb8SSong Liu 		stack_trace_save_tsk(task, (unsigned long *)(entry->ip + init_nr),
226fa28dcb8SSong Liu 				     sysctl_perf_event_max_stack - init_nr, 0);
227fa28dcb8SSong Liu 
228fa28dcb8SSong Liu 	/* stack_trace_save_tsk() works on unsigned long array, while
229fa28dcb8SSong Liu 	 * perf_callchain_entry uses u64 array. For 32-bit systems, it is
230fa28dcb8SSong Liu 	 * necessary to fix this mismatch.
231fa28dcb8SSong Liu 	 */
232fa28dcb8SSong Liu 	if (__BITS_PER_LONG != 64) {
233fa28dcb8SSong Liu 		unsigned long *from = (unsigned long *) entry->ip;
234fa28dcb8SSong Liu 		u64 *to = entry->ip;
235fa28dcb8SSong Liu 		int i;
236fa28dcb8SSong Liu 
237fa28dcb8SSong Liu 		/* copy data from the end to avoid using extra buffer */
238fa28dcb8SSong Liu 		for (i = entry->nr - 1; i >= (int)init_nr; i--)
239fa28dcb8SSong Liu 			to[i] = (u64)(from[i]);
240fa28dcb8SSong Liu 	}
241fa28dcb8SSong Liu 
242fa28dcb8SSong Liu 	put_callchain_entry(rctx);
243fa28dcb8SSong Liu 
244fa28dcb8SSong Liu 	return entry;
245046cc3ddSSong Liu #else /* CONFIG_STACKTRACE */
246046cc3ddSSong Liu 	return NULL;
247046cc3ddSSong Liu #endif
248fa28dcb8SSong Liu }
249fa28dcb8SSong Liu 
2507b04d6d6SSong Liu static long __bpf_get_stackid(struct bpf_map *map,
2517b04d6d6SSong Liu 			      struct perf_callchain_entry *trace, u64 flags)
252d5a3b1f6SAlexei Starovoitov {
253d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
254d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
255615755a7SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
256c5dfd78eSArnaldo Carvalho de Melo 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
257c5dfd78eSArnaldo Carvalho de Melo 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
258d5a3b1f6SAlexei Starovoitov 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
259d5a3b1f6SAlexei Starovoitov 	u32 hash, id, trace_nr, trace_len;
260d5a3b1f6SAlexei Starovoitov 	bool user = flags & BPF_F_USER_STACK;
261d5a3b1f6SAlexei Starovoitov 	u64 *ips;
262615755a7SSong Liu 	bool hash_matches;
263d5a3b1f6SAlexei Starovoitov 
264d5a3b1f6SAlexei Starovoitov 	/* get_perf_callchain() guarantees that trace->nr >= init_nr
265c5dfd78eSArnaldo Carvalho de Melo 	 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
266d5a3b1f6SAlexei Starovoitov 	 */
267d5a3b1f6SAlexei Starovoitov 	trace_nr = trace->nr - init_nr;
268d5a3b1f6SAlexei Starovoitov 
269d5a3b1f6SAlexei Starovoitov 	if (trace_nr <= skip)
270d5a3b1f6SAlexei Starovoitov 		/* skipping more than usable stack trace */
271d5a3b1f6SAlexei Starovoitov 		return -EFAULT;
272d5a3b1f6SAlexei Starovoitov 
273d5a3b1f6SAlexei Starovoitov 	trace_nr -= skip;
274d5a3b1f6SAlexei Starovoitov 	trace_len = trace_nr * sizeof(u64);
275d5a3b1f6SAlexei Starovoitov 	ips = trace->ip + skip + init_nr;
276d5a3b1f6SAlexei Starovoitov 	hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
277d5a3b1f6SAlexei Starovoitov 	id = hash & (smap->n_buckets - 1);
278557c0c6eSAlexei Starovoitov 	bucket = READ_ONCE(smap->buckets[id]);
279d5a3b1f6SAlexei Starovoitov 
280615755a7SSong Liu 	hash_matches = bucket && bucket->hash == hash;
281615755a7SSong Liu 	/* fast cmp */
282615755a7SSong Liu 	if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
283d5a3b1f6SAlexei Starovoitov 		return id;
284615755a7SSong Liu 
285615755a7SSong Liu 	if (stack_map_use_build_id(map)) {
286615755a7SSong Liu 		/* for build_id+offset, pop a bucket before slow cmp */
287615755a7SSong Liu 		new_bucket = (struct stack_map_bucket *)
288615755a7SSong Liu 			pcpu_freelist_pop(&smap->freelist);
289615755a7SSong Liu 		if (unlikely(!new_bucket))
290615755a7SSong Liu 			return -ENOMEM;
2915f412632SYonghong Song 		new_bucket->nr = trace_nr;
2925f412632SYonghong Song 		stack_map_get_build_id_offset(
2935f412632SYonghong Song 			(struct bpf_stack_build_id *)new_bucket->data,
2945f412632SYonghong Song 			ips, trace_nr, user);
295615755a7SSong Liu 		trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
296615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
297615755a7SSong Liu 		    memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
298615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
299d5a3b1f6SAlexei Starovoitov 			return id;
300d5a3b1f6SAlexei Starovoitov 		}
301615755a7SSong Liu 		if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
302615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
303615755a7SSong Liu 			return -EEXIST;
304615755a7SSong Liu 		}
305615755a7SSong Liu 	} else {
306615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
307615755a7SSong Liu 		    memcmp(bucket->data, ips, trace_len) == 0)
308615755a7SSong Liu 			return id;
309d5a3b1f6SAlexei Starovoitov 		if (bucket && !(flags & BPF_F_REUSE_STACKID))
310d5a3b1f6SAlexei Starovoitov 			return -EEXIST;
311d5a3b1f6SAlexei Starovoitov 
312557c0c6eSAlexei Starovoitov 		new_bucket = (struct stack_map_bucket *)
313557c0c6eSAlexei Starovoitov 			pcpu_freelist_pop(&smap->freelist);
314d5a3b1f6SAlexei Starovoitov 		if (unlikely(!new_bucket))
315d5a3b1f6SAlexei Starovoitov 			return -ENOMEM;
316615755a7SSong Liu 		memcpy(new_bucket->data, ips, trace_len);
317615755a7SSong Liu 	}
318d5a3b1f6SAlexei Starovoitov 
319d5a3b1f6SAlexei Starovoitov 	new_bucket->hash = hash;
320d5a3b1f6SAlexei Starovoitov 	new_bucket->nr = trace_nr;
321d5a3b1f6SAlexei Starovoitov 
322d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], new_bucket);
323d5a3b1f6SAlexei Starovoitov 	if (old_bucket)
324557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
325d5a3b1f6SAlexei Starovoitov 	return id;
326d5a3b1f6SAlexei Starovoitov }
327d5a3b1f6SAlexei Starovoitov 
3287b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
3297b04d6d6SSong Liu 	   u64, flags)
3307b04d6d6SSong Liu {
3317b04d6d6SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
3327b04d6d6SSong Liu 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
3337b04d6d6SSong Liu 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
3347b04d6d6SSong Liu 	bool user = flags & BPF_F_USER_STACK;
3357b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
3367b04d6d6SSong Liu 	bool kernel = !user;
3377b04d6d6SSong Liu 
3387b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
3397b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
3407b04d6d6SSong Liu 		return -EINVAL;
3417b04d6d6SSong Liu 
3427b04d6d6SSong Liu 	trace = get_perf_callchain(regs, init_nr, kernel, user,
3437b04d6d6SSong Liu 				   sysctl_perf_event_max_stack, false, false);
3447b04d6d6SSong Liu 
3457b04d6d6SSong Liu 	if (unlikely(!trace))
3467b04d6d6SSong Liu 		/* couldn't fetch the stack trace */
3477b04d6d6SSong Liu 		return -EFAULT;
3487b04d6d6SSong Liu 
3497b04d6d6SSong Liu 	return __bpf_get_stackid(map, trace, flags);
3507b04d6d6SSong Liu }
3517b04d6d6SSong Liu 
352d5a3b1f6SAlexei Starovoitov const struct bpf_func_proto bpf_get_stackid_proto = {
353d5a3b1f6SAlexei Starovoitov 	.func		= bpf_get_stackid,
354d5a3b1f6SAlexei Starovoitov 	.gpl_only	= true,
355d5a3b1f6SAlexei Starovoitov 	.ret_type	= RET_INTEGER,
356d5a3b1f6SAlexei Starovoitov 	.arg1_type	= ARG_PTR_TO_CTX,
357d5a3b1f6SAlexei Starovoitov 	.arg2_type	= ARG_CONST_MAP_PTR,
358d5a3b1f6SAlexei Starovoitov 	.arg3_type	= ARG_ANYTHING,
359d5a3b1f6SAlexei Starovoitov };
360d5a3b1f6SAlexei Starovoitov 
3617b04d6d6SSong Liu static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
3627b04d6d6SSong Liu {
3637b04d6d6SSong Liu 	__u64 nr_kernel = 0;
3647b04d6d6SSong Liu 
3657b04d6d6SSong Liu 	while (nr_kernel < trace->nr) {
3667b04d6d6SSong Liu 		if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
3677b04d6d6SSong Liu 			break;
3687b04d6d6SSong Liu 		nr_kernel++;
3697b04d6d6SSong Liu 	}
3707b04d6d6SSong Liu 	return nr_kernel;
3717b04d6d6SSong Liu }
3727b04d6d6SSong Liu 
3737b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
3747b04d6d6SSong Liu 	   struct bpf_map *, map, u64, flags)
3757b04d6d6SSong Liu {
3767b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
3777b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
3787b04d6d6SSong Liu 	bool kernel, user;
3797b04d6d6SSong Liu 	__u64 nr_kernel;
3807b04d6d6SSong Liu 	int ret;
3817b04d6d6SSong Liu 
3827b04d6d6SSong Liu 	/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
3837b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
3847b04d6d6SSong Liu 		return bpf_get_stackid((unsigned long)(ctx->regs),
3857b04d6d6SSong Liu 				       (unsigned long) map, flags, 0, 0);
3867b04d6d6SSong Liu 
3877b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
3887b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
3897b04d6d6SSong Liu 		return -EINVAL;
3907b04d6d6SSong Liu 
3917b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
3927b04d6d6SSong Liu 	kernel = !user;
3937b04d6d6SSong Liu 
3947b04d6d6SSong Liu 	trace = ctx->data->callchain;
3957b04d6d6SSong Liu 	if (unlikely(!trace))
3967b04d6d6SSong Liu 		return -EFAULT;
3977b04d6d6SSong Liu 
3987b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
3997b04d6d6SSong Liu 
4007b04d6d6SSong Liu 	if (kernel) {
4017b04d6d6SSong Liu 		__u64 nr = trace->nr;
4027b04d6d6SSong Liu 
4037b04d6d6SSong Liu 		trace->nr = nr_kernel;
4047b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
4057b04d6d6SSong Liu 
4067b04d6d6SSong Liu 		/* restore nr */
4077b04d6d6SSong Liu 		trace->nr = nr;
4087b04d6d6SSong Liu 	} else { /* user */
4097b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
4107b04d6d6SSong Liu 
4117b04d6d6SSong Liu 		skip += nr_kernel;
4127b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
4137b04d6d6SSong Liu 			return -EFAULT;
4147b04d6d6SSong Liu 
4157b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
4167b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
4177b04d6d6SSong Liu 	}
4187b04d6d6SSong Liu 	return ret;
4197b04d6d6SSong Liu }
4207b04d6d6SSong Liu 
4217b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stackid_proto_pe = {
4227b04d6d6SSong Liu 	.func		= bpf_get_stackid_pe,
4237b04d6d6SSong Liu 	.gpl_only	= false,
4247b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
4257b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
4267b04d6d6SSong Liu 	.arg2_type	= ARG_CONST_MAP_PTR,
4277b04d6d6SSong Liu 	.arg3_type	= ARG_ANYTHING,
4287b04d6d6SSong Liu };
4297b04d6d6SSong Liu 
430fa28dcb8SSong Liu static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
4317b04d6d6SSong Liu 			    struct perf_callchain_entry *trace_in,
432fa28dcb8SSong Liu 			    void *buf, u32 size, u64 flags)
433c195651eSYonghong Song {
434c195651eSYonghong Song 	u32 init_nr, trace_nr, copy_len, elem_size, num_elem;
435c195651eSYonghong Song 	bool user_build_id = flags & BPF_F_USER_BUILD_ID;
436c195651eSYonghong Song 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
437c195651eSYonghong Song 	bool user = flags & BPF_F_USER_STACK;
438c195651eSYonghong Song 	struct perf_callchain_entry *trace;
439c195651eSYonghong Song 	bool kernel = !user;
440c195651eSYonghong Song 	int err = -EINVAL;
441c195651eSYonghong Song 	u64 *ips;
442c195651eSYonghong Song 
443c195651eSYonghong Song 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
444c195651eSYonghong Song 			       BPF_F_USER_BUILD_ID)))
445c195651eSYonghong Song 		goto clear;
446c195651eSYonghong Song 	if (kernel && user_build_id)
447c195651eSYonghong Song 		goto clear;
448c195651eSYonghong Song 
449c195651eSYonghong Song 	elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
450c195651eSYonghong Song 					    : sizeof(u64);
451c195651eSYonghong Song 	if (unlikely(size % elem_size))
452c195651eSYonghong Song 		goto clear;
453c195651eSYonghong Song 
454fa28dcb8SSong Liu 	/* cannot get valid user stack for task without user_mode regs */
455fa28dcb8SSong Liu 	if (task && user && !user_mode(regs))
456fa28dcb8SSong Liu 		goto err_fault;
457fa28dcb8SSong Liu 
458c195651eSYonghong Song 	num_elem = size / elem_size;
459c195651eSYonghong Song 	if (sysctl_perf_event_max_stack < num_elem)
460c195651eSYonghong Song 		init_nr = 0;
461c195651eSYonghong Song 	else
462c195651eSYonghong Song 		init_nr = sysctl_perf_event_max_stack - num_elem;
463fa28dcb8SSong Liu 
4647b04d6d6SSong Liu 	if (trace_in)
4657b04d6d6SSong Liu 		trace = trace_in;
4667b04d6d6SSong Liu 	else if (kernel && task)
467fa28dcb8SSong Liu 		trace = get_callchain_entry_for_task(task, init_nr);
468fa28dcb8SSong Liu 	else
469c195651eSYonghong Song 		trace = get_perf_callchain(regs, init_nr, kernel, user,
470fa28dcb8SSong Liu 					   sysctl_perf_event_max_stack,
471fa28dcb8SSong Liu 					   false, false);
472c195651eSYonghong Song 	if (unlikely(!trace))
473c195651eSYonghong Song 		goto err_fault;
474c195651eSYonghong Song 
475c195651eSYonghong Song 	trace_nr = trace->nr - init_nr;
476c195651eSYonghong Song 	if (trace_nr < skip)
477c195651eSYonghong Song 		goto err_fault;
478c195651eSYonghong Song 
479c195651eSYonghong Song 	trace_nr -= skip;
480c195651eSYonghong Song 	trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
481c195651eSYonghong Song 	copy_len = trace_nr * elem_size;
482c195651eSYonghong Song 	ips = trace->ip + skip + init_nr;
483c195651eSYonghong Song 	if (user && user_build_id)
484c195651eSYonghong Song 		stack_map_get_build_id_offset(buf, ips, trace_nr, user);
485c195651eSYonghong Song 	else
486c195651eSYonghong Song 		memcpy(buf, ips, copy_len);
487c195651eSYonghong Song 
488c195651eSYonghong Song 	if (size > copy_len)
489c195651eSYonghong Song 		memset(buf + copy_len, 0, size - copy_len);
490c195651eSYonghong Song 	return copy_len;
491c195651eSYonghong Song 
492c195651eSYonghong Song err_fault:
493c195651eSYonghong Song 	err = -EFAULT;
494c195651eSYonghong Song clear:
495c195651eSYonghong Song 	memset(buf, 0, size);
496c195651eSYonghong Song 	return err;
497c195651eSYonghong Song }
498c195651eSYonghong Song 
499fa28dcb8SSong Liu BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
500fa28dcb8SSong Liu 	   u64, flags)
501fa28dcb8SSong Liu {
5027b04d6d6SSong Liu 	return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
503fa28dcb8SSong Liu }
504fa28dcb8SSong Liu 
505c195651eSYonghong Song const struct bpf_func_proto bpf_get_stack_proto = {
506c195651eSYonghong Song 	.func		= bpf_get_stack,
507c195651eSYonghong Song 	.gpl_only	= true,
508c195651eSYonghong Song 	.ret_type	= RET_INTEGER,
509c195651eSYonghong Song 	.arg1_type	= ARG_PTR_TO_CTX,
510c195651eSYonghong Song 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
511c195651eSYonghong Song 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
512c195651eSYonghong Song 	.arg4_type	= ARG_ANYTHING,
513c195651eSYonghong Song };
514c195651eSYonghong Song 
515fa28dcb8SSong Liu BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
516fa28dcb8SSong Liu 	   u32, size, u64, flags)
517fa28dcb8SSong Liu {
518fa28dcb8SSong Liu 	struct pt_regs *regs = task_pt_regs(task);
519fa28dcb8SSong Liu 
5207b04d6d6SSong Liu 	return __bpf_get_stack(regs, task, NULL, buf, size, flags);
521fa28dcb8SSong Liu }
522fa28dcb8SSong Liu 
5239436ef6eSLorenz Bauer BTF_ID_LIST_SINGLE(bpf_get_task_stack_btf_ids, struct, task_struct)
524c9a0f3b8SJiri Olsa 
525fa28dcb8SSong Liu const struct bpf_func_proto bpf_get_task_stack_proto = {
526fa28dcb8SSong Liu 	.func		= bpf_get_task_stack,
527fa28dcb8SSong Liu 	.gpl_only	= false,
528fa28dcb8SSong Liu 	.ret_type	= RET_INTEGER,
529fa28dcb8SSong Liu 	.arg1_type	= ARG_PTR_TO_BTF_ID,
5309436ef6eSLorenz Bauer 	.arg1_btf_id	= &bpf_get_task_stack_btf_ids[0],
531fa28dcb8SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
532fa28dcb8SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
533fa28dcb8SSong Liu 	.arg4_type	= ARG_ANYTHING,
534fa28dcb8SSong Liu };
535fa28dcb8SSong Liu 
5367b04d6d6SSong Liu BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
5377b04d6d6SSong Liu 	   void *, buf, u32, size, u64, flags)
5387b04d6d6SSong Liu {
5392b9b305fSSong Liu 	struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
5407b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
5417b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
5427b04d6d6SSong Liu 	bool kernel, user;
5437b04d6d6SSong Liu 	int err = -EINVAL;
5447b04d6d6SSong Liu 	__u64 nr_kernel;
5457b04d6d6SSong Liu 
5467b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
5472b9b305fSSong Liu 		return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
5487b04d6d6SSong Liu 
5497b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
5507b04d6d6SSong Liu 			       BPF_F_USER_BUILD_ID)))
5517b04d6d6SSong Liu 		goto clear;
5527b04d6d6SSong Liu 
5537b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
5547b04d6d6SSong Liu 	kernel = !user;
5557b04d6d6SSong Liu 
5567b04d6d6SSong Liu 	err = -EFAULT;
5577b04d6d6SSong Liu 	trace = ctx->data->callchain;
5587b04d6d6SSong Liu 	if (unlikely(!trace))
5597b04d6d6SSong Liu 		goto clear;
5607b04d6d6SSong Liu 
5617b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
5627b04d6d6SSong Liu 
5637b04d6d6SSong Liu 	if (kernel) {
5647b04d6d6SSong Liu 		__u64 nr = trace->nr;
5657b04d6d6SSong Liu 
5667b04d6d6SSong Liu 		trace->nr = nr_kernel;
5672b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5687b04d6d6SSong Liu 
5697b04d6d6SSong Liu 		/* restore nr */
5707b04d6d6SSong Liu 		trace->nr = nr;
5717b04d6d6SSong Liu 	} else { /* user */
5727b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
5737b04d6d6SSong Liu 
5747b04d6d6SSong Liu 		skip += nr_kernel;
5757b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
5767b04d6d6SSong Liu 			goto clear;
5777b04d6d6SSong Liu 
5787b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
5792b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5807b04d6d6SSong Liu 	}
5817b04d6d6SSong Liu 	return err;
5827b04d6d6SSong Liu 
5837b04d6d6SSong Liu clear:
5847b04d6d6SSong Liu 	memset(buf, 0, size);
5857b04d6d6SSong Liu 	return err;
5867b04d6d6SSong Liu 
5877b04d6d6SSong Liu }
5887b04d6d6SSong Liu 
5897b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stack_proto_pe = {
5907b04d6d6SSong Liu 	.func		= bpf_get_stack_pe,
5917b04d6d6SSong Liu 	.gpl_only	= true,
5927b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
5937b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
5947b04d6d6SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
5957b04d6d6SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
5967b04d6d6SSong Liu 	.arg4_type	= ARG_ANYTHING,
5977b04d6d6SSong Liu };
5987b04d6d6SSong Liu 
599557c0c6eSAlexei Starovoitov /* Called from eBPF program */
600d5a3b1f6SAlexei Starovoitov static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
601d5a3b1f6SAlexei Starovoitov {
6023b4a63f6SPrashant Bhole 	return ERR_PTR(-EOPNOTSUPP);
603557c0c6eSAlexei Starovoitov }
604557c0c6eSAlexei Starovoitov 
605557c0c6eSAlexei Starovoitov /* Called from syscall */
606557c0c6eSAlexei Starovoitov int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
607557c0c6eSAlexei Starovoitov {
608d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
609557c0c6eSAlexei Starovoitov 	struct stack_map_bucket *bucket, *old_bucket;
610557c0c6eSAlexei Starovoitov 	u32 id = *(u32 *)key, trace_len;
611d5a3b1f6SAlexei Starovoitov 
612d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
613557c0c6eSAlexei Starovoitov 		return -ENOENT;
614557c0c6eSAlexei Starovoitov 
615557c0c6eSAlexei Starovoitov 	bucket = xchg(&smap->buckets[id], NULL);
616557c0c6eSAlexei Starovoitov 	if (!bucket)
617557c0c6eSAlexei Starovoitov 		return -ENOENT;
618557c0c6eSAlexei Starovoitov 
619615755a7SSong Liu 	trace_len = bucket->nr * stack_map_data_size(map);
620615755a7SSong Liu 	memcpy(value, bucket->data, trace_len);
621557c0c6eSAlexei Starovoitov 	memset(value + trace_len, 0, map->value_size - trace_len);
622557c0c6eSAlexei Starovoitov 
623557c0c6eSAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], bucket);
624557c0c6eSAlexei Starovoitov 	if (old_bucket)
625557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
626557c0c6eSAlexei Starovoitov 	return 0;
627d5a3b1f6SAlexei Starovoitov }
628d5a3b1f6SAlexei Starovoitov 
62916f07c55SYonghong Song static int stack_map_get_next_key(struct bpf_map *map, void *key,
63016f07c55SYonghong Song 				  void *next_key)
631d5a3b1f6SAlexei Starovoitov {
63216f07c55SYonghong Song 	struct bpf_stack_map *smap = container_of(map,
63316f07c55SYonghong Song 						  struct bpf_stack_map, map);
63416f07c55SYonghong Song 	u32 id;
63516f07c55SYonghong Song 
63616f07c55SYonghong Song 	WARN_ON_ONCE(!rcu_read_lock_held());
63716f07c55SYonghong Song 
63816f07c55SYonghong Song 	if (!key) {
63916f07c55SYonghong Song 		id = 0;
64016f07c55SYonghong Song 	} else {
64116f07c55SYonghong Song 		id = *(u32 *)key;
64216f07c55SYonghong Song 		if (id >= smap->n_buckets || !smap->buckets[id])
64316f07c55SYonghong Song 			id = 0;
64416f07c55SYonghong Song 		else
64516f07c55SYonghong Song 			id++;
64616f07c55SYonghong Song 	}
64716f07c55SYonghong Song 
64816f07c55SYonghong Song 	while (id < smap->n_buckets && !smap->buckets[id])
64916f07c55SYonghong Song 		id++;
65016f07c55SYonghong Song 
65116f07c55SYonghong Song 	if (id >= smap->n_buckets)
65216f07c55SYonghong Song 		return -ENOENT;
65316f07c55SYonghong Song 
65416f07c55SYonghong Song 	*(u32 *)next_key = id;
65516f07c55SYonghong Song 	return 0;
656d5a3b1f6SAlexei Starovoitov }
657d5a3b1f6SAlexei Starovoitov 
658d5a3b1f6SAlexei Starovoitov static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
659d5a3b1f6SAlexei Starovoitov 				 u64 map_flags)
660d5a3b1f6SAlexei Starovoitov {
661d5a3b1f6SAlexei Starovoitov 	return -EINVAL;
662d5a3b1f6SAlexei Starovoitov }
663d5a3b1f6SAlexei Starovoitov 
664d5a3b1f6SAlexei Starovoitov /* Called from syscall or from eBPF program */
665d5a3b1f6SAlexei Starovoitov static int stack_map_delete_elem(struct bpf_map *map, void *key)
666d5a3b1f6SAlexei Starovoitov {
667d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
668d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *old_bucket;
669d5a3b1f6SAlexei Starovoitov 	u32 id = *(u32 *)key;
670d5a3b1f6SAlexei Starovoitov 
671d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
672d5a3b1f6SAlexei Starovoitov 		return -E2BIG;
673d5a3b1f6SAlexei Starovoitov 
674d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], NULL);
675d5a3b1f6SAlexei Starovoitov 	if (old_bucket) {
676557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
677d5a3b1f6SAlexei Starovoitov 		return 0;
678d5a3b1f6SAlexei Starovoitov 	} else {
679d5a3b1f6SAlexei Starovoitov 		return -ENOENT;
680d5a3b1f6SAlexei Starovoitov 	}
681d5a3b1f6SAlexei Starovoitov }
682d5a3b1f6SAlexei Starovoitov 
683d5a3b1f6SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
684d5a3b1f6SAlexei Starovoitov static void stack_map_free(struct bpf_map *map)
685d5a3b1f6SAlexei Starovoitov {
686d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
687d5a3b1f6SAlexei Starovoitov 
688d407bd25SDaniel Borkmann 	bpf_map_area_free(smap->elems);
689557c0c6eSAlexei Starovoitov 	pcpu_freelist_destroy(&smap->freelist);
690d407bd25SDaniel Borkmann 	bpf_map_area_free(smap);
691d5a3b1f6SAlexei Starovoitov 	put_callchain_buffers();
692d5a3b1f6SAlexei Starovoitov }
693d5a3b1f6SAlexei Starovoitov 
6942872e9acSAndrey Ignatov static int stack_trace_map_btf_id;
69514499160SMauricio Vasquez B const struct bpf_map_ops stack_trace_map_ops = {
696f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
697d5a3b1f6SAlexei Starovoitov 	.map_alloc = stack_map_alloc,
698d5a3b1f6SAlexei Starovoitov 	.map_free = stack_map_free,
699d5a3b1f6SAlexei Starovoitov 	.map_get_next_key = stack_map_get_next_key,
700d5a3b1f6SAlexei Starovoitov 	.map_lookup_elem = stack_map_lookup_elem,
701d5a3b1f6SAlexei Starovoitov 	.map_update_elem = stack_map_update_elem,
702d5a3b1f6SAlexei Starovoitov 	.map_delete_elem = stack_map_delete_elem,
703e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
7042872e9acSAndrey Ignatov 	.map_btf_name = "bpf_stack_map",
7052872e9acSAndrey Ignatov 	.map_btf_id = &stack_trace_map_btf_id,
706d5a3b1f6SAlexei Starovoitov };
707bae77c5eSSong Liu 
708bae77c5eSSong Liu static int __init stack_map_init(void)
709bae77c5eSSong Liu {
710bae77c5eSSong Liu 	int cpu;
711bae77c5eSSong Liu 	struct stack_map_irq_work *work;
712bae77c5eSSong Liu 
713bae77c5eSSong Liu 	for_each_possible_cpu(cpu) {
714bae77c5eSSong Liu 		work = per_cpu_ptr(&up_read_work, cpu);
715bae77c5eSSong Liu 		init_irq_work(&work->irq_work, do_up_read);
716bae77c5eSSong Liu 	}
717bae77c5eSSong Liu 	return 0;
718bae77c5eSSong Liu }
719bae77c5eSSong Liu subsys_initcall(stack_map_init);
720