xref: /linux-6.15/kernel/bpf/stackmap.c (revision 2f1aaf3e)
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>
12bd7525daSJiri 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);
1176183f4d3SBui Quang Minh 	if (!n_buckets)
1186183f4d3SBui Quang Minh 		return ERR_PTR(-E2BIG);
119d5a3b1f6SAlexei Starovoitov 
120d5a3b1f6SAlexei Starovoitov 	cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
121d5a3b1f6SAlexei Starovoitov 	cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
122b936ca64SRoman Gushchin 	smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
12337086810SRoman Gushchin 	if (!smap)
124b936ca64SRoman Gushchin 		return ERR_PTR(-ENOMEM);
125d5a3b1f6SAlexei Starovoitov 
126bd475643SJakub Kicinski 	bpf_map_init_from_attr(&smap->map, attr);
127d5a3b1f6SAlexei Starovoitov 	smap->map.value_size = value_size;
128d5a3b1f6SAlexei Starovoitov 	smap->n_buckets = n_buckets;
129557c0c6eSAlexei Starovoitov 
13097c79a38SArnaldo Carvalho de Melo 	err = get_callchain_buffers(sysctl_perf_event_max_stack);
131d5a3b1f6SAlexei Starovoitov 	if (err)
13237086810SRoman Gushchin 		goto free_smap;
133d5a3b1f6SAlexei Starovoitov 
134557c0c6eSAlexei Starovoitov 	err = prealloc_elems_and_freelist(smap);
135557c0c6eSAlexei Starovoitov 	if (err)
136557c0c6eSAlexei Starovoitov 		goto put_buffers;
137557c0c6eSAlexei Starovoitov 
138d5a3b1f6SAlexei Starovoitov 	return &smap->map;
139d5a3b1f6SAlexei Starovoitov 
140557c0c6eSAlexei Starovoitov put_buffers:
141557c0c6eSAlexei Starovoitov 	put_callchain_buffers();
14237086810SRoman Gushchin free_smap:
143d407bd25SDaniel Borkmann 	bpf_map_area_free(smap);
144d5a3b1f6SAlexei Starovoitov 	return ERR_PTR(err);
145d5a3b1f6SAlexei Starovoitov }
146d5a3b1f6SAlexei Starovoitov 
1475f412632SYonghong Song static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
148615755a7SSong Liu 					  u64 *ips, u32 trace_nr, bool user)
149615755a7SSong Liu {
150615755a7SSong Liu 	int i;
151615755a7SSong Liu 	struct vm_area_struct *vma;
152bae77c5eSSong Liu 	bool irq_work_busy = false;
153dc3b8ae9SArnd Bergmann 	struct stack_map_irq_work *work = NULL;
154bae77c5eSSong Liu 
155eac9153fSSong Liu 	if (irqs_disabled()) {
156099bfaa7SDavid Miller 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
157bae77c5eSSong Liu 			work = this_cpu_ptr(&up_read_work);
1587a9f50a0SPeter Zijlstra 			if (irq_work_is_busy(&work->irq_work)) {
159bae77c5eSSong Liu 				/* cannot queue more up_read, fallback */
160bae77c5eSSong Liu 				irq_work_busy = true;
161bae77c5eSSong Liu 			}
162099bfaa7SDavid Miller 		} else {
163099bfaa7SDavid Miller 			/*
164099bfaa7SDavid Miller 			 * PREEMPT_RT does not allow to trylock mmap sem in
165099bfaa7SDavid Miller 			 * interrupt disabled context. Force the fallback code.
166099bfaa7SDavid Miller 			 */
167099bfaa7SDavid Miller 			irq_work_busy = true;
168099bfaa7SDavid Miller 		}
169099bfaa7SDavid Miller 	}
170615755a7SSong Liu 
171615755a7SSong Liu 	/*
172eac9153fSSong Liu 	 * We cannot do up_read() when the irq is disabled, because of
173eac9153fSSong Liu 	 * risk to deadlock with rq_lock. To do build_id lookup when the
174eac9153fSSong Liu 	 * irqs are disabled, we need to run up_read() in irq_work. We use
175bae77c5eSSong Liu 	 * a percpu variable to do the irq_work. If the irq_work is
176bae77c5eSSong Liu 	 * already used by another lookup, we fall back to report ips.
177615755a7SSong Liu 	 *
178615755a7SSong Liu 	 * Same fallback is used for kernel stack (!user) on a stackmap
179615755a7SSong Liu 	 * with build_id.
180615755a7SSong Liu 	 */
181bae77c5eSSong Liu 	if (!user || !current || !current->mm || irq_work_busy ||
182*2f1aaf3eSYonghong Song 	    !mmap_read_trylock(current->mm)) {
183615755a7SSong Liu 		/* cannot access current->mm, fall back to ips */
184615755a7SSong Liu 		for (i = 0; i < trace_nr; i++) {
185615755a7SSong Liu 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
186615755a7SSong Liu 			id_offs[i].ip = ips[i];
187bd7525daSJiri Olsa 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
188615755a7SSong Liu 		}
189615755a7SSong Liu 		return;
190615755a7SSong Liu 	}
191615755a7SSong Liu 
192615755a7SSong Liu 	for (i = 0; i < trace_nr; i++) {
193615755a7SSong Liu 		vma = find_vma(current->mm, ips[i]);
194921f88fcSJiri Olsa 		if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) {
195615755a7SSong Liu 			/* per entry fall back to ips */
196615755a7SSong Liu 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
197615755a7SSong Liu 			id_offs[i].ip = ips[i];
198bd7525daSJiri Olsa 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
199615755a7SSong Liu 			continue;
200615755a7SSong Liu 		}
201615755a7SSong Liu 		id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
202615755a7SSong Liu 			- vma->vm_start;
203615755a7SSong Liu 		id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
204615755a7SSong Liu 	}
205bae77c5eSSong Liu 
206dc3b8ae9SArnd Bergmann 	if (!work) {
207*2f1aaf3eSYonghong Song 		mmap_read_unlock(current->mm);
208bae77c5eSSong Liu 	} else {
2090cc55a02SMichel Lespinasse 		work->mm = current->mm;
210*2f1aaf3eSYonghong Song 
211*2f1aaf3eSYonghong Song 		/* The lock will be released once we're out of interrupt
212*2f1aaf3eSYonghong Song 		 * context. Tell lockdep that we've released it now so
213*2f1aaf3eSYonghong Song 		 * it doesn't complain that we forgot to release it.
214*2f1aaf3eSYonghong Song 		 */
215*2f1aaf3eSYonghong Song 		rwsem_release(&current->mm->mmap_lock.dep_map, _RET_IP_);
216bae77c5eSSong Liu 		irq_work_queue(&work->irq_work);
217bae77c5eSSong Liu 	}
218615755a7SSong Liu }
219615755a7SSong Liu 
220fa28dcb8SSong Liu static struct perf_callchain_entry *
221fa28dcb8SSong Liu get_callchain_entry_for_task(struct task_struct *task, u32 init_nr)
222fa28dcb8SSong Liu {
223046cc3ddSSong Liu #ifdef CONFIG_STACKTRACE
224fa28dcb8SSong Liu 	struct perf_callchain_entry *entry;
225fa28dcb8SSong Liu 	int rctx;
226fa28dcb8SSong Liu 
227fa28dcb8SSong Liu 	entry = get_callchain_entry(&rctx);
228fa28dcb8SSong Liu 
229fa28dcb8SSong Liu 	if (!entry)
230fa28dcb8SSong Liu 		return NULL;
231fa28dcb8SSong Liu 
232fa28dcb8SSong Liu 	entry->nr = init_nr +
233fa28dcb8SSong Liu 		stack_trace_save_tsk(task, (unsigned long *)(entry->ip + init_nr),
234fa28dcb8SSong Liu 				     sysctl_perf_event_max_stack - init_nr, 0);
235fa28dcb8SSong Liu 
236fa28dcb8SSong Liu 	/* stack_trace_save_tsk() works on unsigned long array, while
237fa28dcb8SSong Liu 	 * perf_callchain_entry uses u64 array. For 32-bit systems, it is
238fa28dcb8SSong Liu 	 * necessary to fix this mismatch.
239fa28dcb8SSong Liu 	 */
240fa28dcb8SSong Liu 	if (__BITS_PER_LONG != 64) {
241fa28dcb8SSong Liu 		unsigned long *from = (unsigned long *) entry->ip;
242fa28dcb8SSong Liu 		u64 *to = entry->ip;
243fa28dcb8SSong Liu 		int i;
244fa28dcb8SSong Liu 
245fa28dcb8SSong Liu 		/* copy data from the end to avoid using extra buffer */
246fa28dcb8SSong Liu 		for (i = entry->nr - 1; i >= (int)init_nr; i--)
247fa28dcb8SSong Liu 			to[i] = (u64)(from[i]);
248fa28dcb8SSong Liu 	}
249fa28dcb8SSong Liu 
250fa28dcb8SSong Liu 	put_callchain_entry(rctx);
251fa28dcb8SSong Liu 
252fa28dcb8SSong Liu 	return entry;
253046cc3ddSSong Liu #else /* CONFIG_STACKTRACE */
254046cc3ddSSong Liu 	return NULL;
255046cc3ddSSong Liu #endif
256fa28dcb8SSong Liu }
257fa28dcb8SSong Liu 
2587b04d6d6SSong Liu static long __bpf_get_stackid(struct bpf_map *map,
2597b04d6d6SSong Liu 			      struct perf_callchain_entry *trace, u64 flags)
260d5a3b1f6SAlexei Starovoitov {
261d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
262d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
263615755a7SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
264c5dfd78eSArnaldo Carvalho de Melo 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
265c5dfd78eSArnaldo Carvalho de Melo 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
266d5a3b1f6SAlexei Starovoitov 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
267d5a3b1f6SAlexei Starovoitov 	u32 hash, id, trace_nr, trace_len;
268d5a3b1f6SAlexei Starovoitov 	bool user = flags & BPF_F_USER_STACK;
269d5a3b1f6SAlexei Starovoitov 	u64 *ips;
270615755a7SSong Liu 	bool hash_matches;
271d5a3b1f6SAlexei Starovoitov 
272d5a3b1f6SAlexei Starovoitov 	/* get_perf_callchain() guarantees that trace->nr >= init_nr
273c5dfd78eSArnaldo Carvalho de Melo 	 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
274d5a3b1f6SAlexei Starovoitov 	 */
275d5a3b1f6SAlexei Starovoitov 	trace_nr = trace->nr - init_nr;
276d5a3b1f6SAlexei Starovoitov 
277d5a3b1f6SAlexei Starovoitov 	if (trace_nr <= skip)
278d5a3b1f6SAlexei Starovoitov 		/* skipping more than usable stack trace */
279d5a3b1f6SAlexei Starovoitov 		return -EFAULT;
280d5a3b1f6SAlexei Starovoitov 
281d5a3b1f6SAlexei Starovoitov 	trace_nr -= skip;
282d5a3b1f6SAlexei Starovoitov 	trace_len = trace_nr * sizeof(u64);
283d5a3b1f6SAlexei Starovoitov 	ips = trace->ip + skip + init_nr;
284d5a3b1f6SAlexei Starovoitov 	hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
285d5a3b1f6SAlexei Starovoitov 	id = hash & (smap->n_buckets - 1);
286557c0c6eSAlexei Starovoitov 	bucket = READ_ONCE(smap->buckets[id]);
287d5a3b1f6SAlexei Starovoitov 
288615755a7SSong Liu 	hash_matches = bucket && bucket->hash == hash;
289615755a7SSong Liu 	/* fast cmp */
290615755a7SSong Liu 	if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
291d5a3b1f6SAlexei Starovoitov 		return id;
292615755a7SSong Liu 
293615755a7SSong Liu 	if (stack_map_use_build_id(map)) {
294615755a7SSong Liu 		/* for build_id+offset, pop a bucket before slow cmp */
295615755a7SSong Liu 		new_bucket = (struct stack_map_bucket *)
296615755a7SSong Liu 			pcpu_freelist_pop(&smap->freelist);
297615755a7SSong Liu 		if (unlikely(!new_bucket))
298615755a7SSong Liu 			return -ENOMEM;
2995f412632SYonghong Song 		new_bucket->nr = trace_nr;
3005f412632SYonghong Song 		stack_map_get_build_id_offset(
3015f412632SYonghong Song 			(struct bpf_stack_build_id *)new_bucket->data,
3025f412632SYonghong Song 			ips, trace_nr, user);
303615755a7SSong Liu 		trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
304615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
305615755a7SSong Liu 		    memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
306615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
307d5a3b1f6SAlexei Starovoitov 			return id;
308d5a3b1f6SAlexei Starovoitov 		}
309615755a7SSong Liu 		if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
310615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
311615755a7SSong Liu 			return -EEXIST;
312615755a7SSong Liu 		}
313615755a7SSong Liu 	} else {
314615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
315615755a7SSong Liu 		    memcmp(bucket->data, ips, trace_len) == 0)
316615755a7SSong Liu 			return id;
317d5a3b1f6SAlexei Starovoitov 		if (bucket && !(flags & BPF_F_REUSE_STACKID))
318d5a3b1f6SAlexei Starovoitov 			return -EEXIST;
319d5a3b1f6SAlexei Starovoitov 
320557c0c6eSAlexei Starovoitov 		new_bucket = (struct stack_map_bucket *)
321557c0c6eSAlexei Starovoitov 			pcpu_freelist_pop(&smap->freelist);
322d5a3b1f6SAlexei Starovoitov 		if (unlikely(!new_bucket))
323d5a3b1f6SAlexei Starovoitov 			return -ENOMEM;
324615755a7SSong Liu 		memcpy(new_bucket->data, ips, trace_len);
325615755a7SSong Liu 	}
326d5a3b1f6SAlexei Starovoitov 
327d5a3b1f6SAlexei Starovoitov 	new_bucket->hash = hash;
328d5a3b1f6SAlexei Starovoitov 	new_bucket->nr = trace_nr;
329d5a3b1f6SAlexei Starovoitov 
330d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], new_bucket);
331d5a3b1f6SAlexei Starovoitov 	if (old_bucket)
332557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
333d5a3b1f6SAlexei Starovoitov 	return id;
334d5a3b1f6SAlexei Starovoitov }
335d5a3b1f6SAlexei Starovoitov 
3367b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
3377b04d6d6SSong Liu 	   u64, flags)
3387b04d6d6SSong Liu {
3397b04d6d6SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
3407b04d6d6SSong Liu 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
3417b04d6d6SSong Liu 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
3427b04d6d6SSong Liu 	bool user = flags & BPF_F_USER_STACK;
3437b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
3447b04d6d6SSong Liu 	bool kernel = !user;
3457b04d6d6SSong Liu 
3467b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
3477b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
3487b04d6d6SSong Liu 		return -EINVAL;
3497b04d6d6SSong Liu 
3507b04d6d6SSong Liu 	trace = get_perf_callchain(regs, init_nr, kernel, user,
3517b04d6d6SSong Liu 				   sysctl_perf_event_max_stack, false, false);
3527b04d6d6SSong Liu 
3537b04d6d6SSong Liu 	if (unlikely(!trace))
3547b04d6d6SSong Liu 		/* couldn't fetch the stack trace */
3557b04d6d6SSong Liu 		return -EFAULT;
3567b04d6d6SSong Liu 
3577b04d6d6SSong Liu 	return __bpf_get_stackid(map, trace, flags);
3587b04d6d6SSong Liu }
3597b04d6d6SSong Liu 
360d5a3b1f6SAlexei Starovoitov const struct bpf_func_proto bpf_get_stackid_proto = {
361d5a3b1f6SAlexei Starovoitov 	.func		= bpf_get_stackid,
362d5a3b1f6SAlexei Starovoitov 	.gpl_only	= true,
363d5a3b1f6SAlexei Starovoitov 	.ret_type	= RET_INTEGER,
364d5a3b1f6SAlexei Starovoitov 	.arg1_type	= ARG_PTR_TO_CTX,
365d5a3b1f6SAlexei Starovoitov 	.arg2_type	= ARG_CONST_MAP_PTR,
366d5a3b1f6SAlexei Starovoitov 	.arg3_type	= ARG_ANYTHING,
367d5a3b1f6SAlexei Starovoitov };
368d5a3b1f6SAlexei Starovoitov 
3697b04d6d6SSong Liu static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
3707b04d6d6SSong Liu {
3717b04d6d6SSong Liu 	__u64 nr_kernel = 0;
3727b04d6d6SSong Liu 
3737b04d6d6SSong Liu 	while (nr_kernel < trace->nr) {
3747b04d6d6SSong Liu 		if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
3757b04d6d6SSong Liu 			break;
3767b04d6d6SSong Liu 		nr_kernel++;
3777b04d6d6SSong Liu 	}
3787b04d6d6SSong Liu 	return nr_kernel;
3797b04d6d6SSong Liu }
3807b04d6d6SSong Liu 
3817b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
3827b04d6d6SSong Liu 	   struct bpf_map *, map, u64, flags)
3837b04d6d6SSong Liu {
3847b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
3857b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
3867b04d6d6SSong Liu 	bool kernel, user;
3877b04d6d6SSong Liu 	__u64 nr_kernel;
3887b04d6d6SSong Liu 	int ret;
3897b04d6d6SSong Liu 
3907b04d6d6SSong Liu 	/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
3917b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
3927b04d6d6SSong Liu 		return bpf_get_stackid((unsigned long)(ctx->regs),
3937b04d6d6SSong Liu 				       (unsigned long) map, flags, 0, 0);
3947b04d6d6SSong Liu 
3957b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
3967b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
3977b04d6d6SSong Liu 		return -EINVAL;
3987b04d6d6SSong Liu 
3997b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
4007b04d6d6SSong Liu 	kernel = !user;
4017b04d6d6SSong Liu 
4027b04d6d6SSong Liu 	trace = ctx->data->callchain;
4037b04d6d6SSong Liu 	if (unlikely(!trace))
4047b04d6d6SSong Liu 		return -EFAULT;
4057b04d6d6SSong Liu 
4067b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
4077b04d6d6SSong Liu 
4087b04d6d6SSong Liu 	if (kernel) {
4097b04d6d6SSong Liu 		__u64 nr = trace->nr;
4107b04d6d6SSong Liu 
4117b04d6d6SSong Liu 		trace->nr = nr_kernel;
4127b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
4137b04d6d6SSong Liu 
4147b04d6d6SSong Liu 		/* restore nr */
4157b04d6d6SSong Liu 		trace->nr = nr;
4167b04d6d6SSong Liu 	} else { /* user */
4177b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
4187b04d6d6SSong Liu 
4197b04d6d6SSong Liu 		skip += nr_kernel;
4207b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
4217b04d6d6SSong Liu 			return -EFAULT;
4227b04d6d6SSong Liu 
4237b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
4247b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
4257b04d6d6SSong Liu 	}
4267b04d6d6SSong Liu 	return ret;
4277b04d6d6SSong Liu }
4287b04d6d6SSong Liu 
4297b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stackid_proto_pe = {
4307b04d6d6SSong Liu 	.func		= bpf_get_stackid_pe,
4317b04d6d6SSong Liu 	.gpl_only	= false,
4327b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
4337b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
4347b04d6d6SSong Liu 	.arg2_type	= ARG_CONST_MAP_PTR,
4357b04d6d6SSong Liu 	.arg3_type	= ARG_ANYTHING,
4367b04d6d6SSong Liu };
4377b04d6d6SSong Liu 
438fa28dcb8SSong Liu static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
4397b04d6d6SSong Liu 			    struct perf_callchain_entry *trace_in,
440fa28dcb8SSong Liu 			    void *buf, u32 size, u64 flags)
441c195651eSYonghong Song {
442c195651eSYonghong Song 	u32 init_nr, trace_nr, copy_len, elem_size, num_elem;
443c195651eSYonghong Song 	bool user_build_id = flags & BPF_F_USER_BUILD_ID;
444c195651eSYonghong Song 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
445c195651eSYonghong Song 	bool user = flags & BPF_F_USER_STACK;
446c195651eSYonghong Song 	struct perf_callchain_entry *trace;
447c195651eSYonghong Song 	bool kernel = !user;
448c195651eSYonghong Song 	int err = -EINVAL;
449c195651eSYonghong Song 	u64 *ips;
450c195651eSYonghong Song 
451c195651eSYonghong Song 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
452c195651eSYonghong Song 			       BPF_F_USER_BUILD_ID)))
453c195651eSYonghong Song 		goto clear;
454c195651eSYonghong Song 	if (kernel && user_build_id)
455c195651eSYonghong Song 		goto clear;
456c195651eSYonghong Song 
457c195651eSYonghong Song 	elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
458c195651eSYonghong Song 					    : sizeof(u64);
459c195651eSYonghong Song 	if (unlikely(size % elem_size))
460c195651eSYonghong Song 		goto clear;
461c195651eSYonghong Song 
462fa28dcb8SSong Liu 	/* cannot get valid user stack for task without user_mode regs */
463fa28dcb8SSong Liu 	if (task && user && !user_mode(regs))
464fa28dcb8SSong Liu 		goto err_fault;
465fa28dcb8SSong Liu 
466c195651eSYonghong Song 	num_elem = size / elem_size;
467c195651eSYonghong Song 	if (sysctl_perf_event_max_stack < num_elem)
468c195651eSYonghong Song 		init_nr = 0;
469c195651eSYonghong Song 	else
470c195651eSYonghong Song 		init_nr = sysctl_perf_event_max_stack - num_elem;
471fa28dcb8SSong Liu 
4727b04d6d6SSong Liu 	if (trace_in)
4737b04d6d6SSong Liu 		trace = trace_in;
4747b04d6d6SSong Liu 	else if (kernel && task)
475fa28dcb8SSong Liu 		trace = get_callchain_entry_for_task(task, init_nr);
476fa28dcb8SSong Liu 	else
477c195651eSYonghong Song 		trace = get_perf_callchain(regs, init_nr, kernel, user,
478fa28dcb8SSong Liu 					   sysctl_perf_event_max_stack,
479fa28dcb8SSong Liu 					   false, false);
480c195651eSYonghong Song 	if (unlikely(!trace))
481c195651eSYonghong Song 		goto err_fault;
482c195651eSYonghong Song 
483c195651eSYonghong Song 	trace_nr = trace->nr - init_nr;
484c195651eSYonghong Song 	if (trace_nr < skip)
485c195651eSYonghong Song 		goto err_fault;
486c195651eSYonghong Song 
487c195651eSYonghong Song 	trace_nr -= skip;
488c195651eSYonghong Song 	trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
489c195651eSYonghong Song 	copy_len = trace_nr * elem_size;
490c195651eSYonghong Song 	ips = trace->ip + skip + init_nr;
491c195651eSYonghong Song 	if (user && user_build_id)
492c195651eSYonghong Song 		stack_map_get_build_id_offset(buf, ips, trace_nr, user);
493c195651eSYonghong Song 	else
494c195651eSYonghong Song 		memcpy(buf, ips, copy_len);
495c195651eSYonghong Song 
496c195651eSYonghong Song 	if (size > copy_len)
497c195651eSYonghong Song 		memset(buf + copy_len, 0, size - copy_len);
498c195651eSYonghong Song 	return copy_len;
499c195651eSYonghong Song 
500c195651eSYonghong Song err_fault:
501c195651eSYonghong Song 	err = -EFAULT;
502c195651eSYonghong Song clear:
503c195651eSYonghong Song 	memset(buf, 0, size);
504c195651eSYonghong Song 	return err;
505c195651eSYonghong Song }
506c195651eSYonghong Song 
507fa28dcb8SSong Liu BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
508fa28dcb8SSong Liu 	   u64, flags)
509fa28dcb8SSong Liu {
5107b04d6d6SSong Liu 	return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
511fa28dcb8SSong Liu }
512fa28dcb8SSong Liu 
513c195651eSYonghong Song const struct bpf_func_proto bpf_get_stack_proto = {
514c195651eSYonghong Song 	.func		= bpf_get_stack,
515c195651eSYonghong Song 	.gpl_only	= true,
516c195651eSYonghong Song 	.ret_type	= RET_INTEGER,
517c195651eSYonghong Song 	.arg1_type	= ARG_PTR_TO_CTX,
518c195651eSYonghong Song 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
519c195651eSYonghong Song 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
520c195651eSYonghong Song 	.arg4_type	= ARG_ANYTHING,
521c195651eSYonghong Song };
522c195651eSYonghong Song 
523fa28dcb8SSong Liu BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
524fa28dcb8SSong Liu 	   u32, size, u64, flags)
525fa28dcb8SSong Liu {
52606ab134cSDave Marchevsky 	struct pt_regs *regs;
52706ab134cSDave Marchevsky 	long res;
528fa28dcb8SSong Liu 
52906ab134cSDave Marchevsky 	if (!try_get_task_stack(task))
53006ab134cSDave Marchevsky 		return -EFAULT;
53106ab134cSDave Marchevsky 
53206ab134cSDave Marchevsky 	regs = task_pt_regs(task);
53306ab134cSDave Marchevsky 	res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
53406ab134cSDave Marchevsky 	put_task_stack(task);
53506ab134cSDave Marchevsky 
53606ab134cSDave Marchevsky 	return res;
537fa28dcb8SSong Liu }
538fa28dcb8SSong Liu 
539fa28dcb8SSong Liu const struct bpf_func_proto bpf_get_task_stack_proto = {
540fa28dcb8SSong Liu 	.func		= bpf_get_task_stack,
541fa28dcb8SSong Liu 	.gpl_only	= false,
542fa28dcb8SSong Liu 	.ret_type	= RET_INTEGER,
543fa28dcb8SSong Liu 	.arg1_type	= ARG_PTR_TO_BTF_ID,
54433c5cb36SDaniel Xu 	.arg1_btf_id	= &btf_task_struct_ids[0],
545fa28dcb8SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
546fa28dcb8SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
547fa28dcb8SSong Liu 	.arg4_type	= ARG_ANYTHING,
548fa28dcb8SSong Liu };
549fa28dcb8SSong Liu 
5507b04d6d6SSong Liu BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
5517b04d6d6SSong Liu 	   void *, buf, u32, size, u64, flags)
5527b04d6d6SSong Liu {
5532b9b305fSSong Liu 	struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
5547b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
5557b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
5567b04d6d6SSong Liu 	bool kernel, user;
5577b04d6d6SSong Liu 	int err = -EINVAL;
5587b04d6d6SSong Liu 	__u64 nr_kernel;
5597b04d6d6SSong Liu 
5607b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
5612b9b305fSSong Liu 		return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
5627b04d6d6SSong Liu 
5637b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
5647b04d6d6SSong Liu 			       BPF_F_USER_BUILD_ID)))
5657b04d6d6SSong Liu 		goto clear;
5667b04d6d6SSong Liu 
5677b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
5687b04d6d6SSong Liu 	kernel = !user;
5697b04d6d6SSong Liu 
5707b04d6d6SSong Liu 	err = -EFAULT;
5717b04d6d6SSong Liu 	trace = ctx->data->callchain;
5727b04d6d6SSong Liu 	if (unlikely(!trace))
5737b04d6d6SSong Liu 		goto clear;
5747b04d6d6SSong Liu 
5757b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
5767b04d6d6SSong Liu 
5777b04d6d6SSong Liu 	if (kernel) {
5787b04d6d6SSong Liu 		__u64 nr = trace->nr;
5797b04d6d6SSong Liu 
5807b04d6d6SSong Liu 		trace->nr = nr_kernel;
5812b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5827b04d6d6SSong Liu 
5837b04d6d6SSong Liu 		/* restore nr */
5847b04d6d6SSong Liu 		trace->nr = nr;
5857b04d6d6SSong Liu 	} else { /* user */
5867b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
5877b04d6d6SSong Liu 
5887b04d6d6SSong Liu 		skip += nr_kernel;
5897b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
5907b04d6d6SSong Liu 			goto clear;
5917b04d6d6SSong Liu 
5927b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
5932b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5947b04d6d6SSong Liu 	}
5957b04d6d6SSong Liu 	return err;
5967b04d6d6SSong Liu 
5977b04d6d6SSong Liu clear:
5987b04d6d6SSong Liu 	memset(buf, 0, size);
5997b04d6d6SSong Liu 	return err;
6007b04d6d6SSong Liu 
6017b04d6d6SSong Liu }
6027b04d6d6SSong Liu 
6037b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stack_proto_pe = {
6047b04d6d6SSong Liu 	.func		= bpf_get_stack_pe,
6057b04d6d6SSong Liu 	.gpl_only	= true,
6067b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
6077b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
6087b04d6d6SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
6097b04d6d6SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
6107b04d6d6SSong Liu 	.arg4_type	= ARG_ANYTHING,
6117b04d6d6SSong Liu };
6127b04d6d6SSong Liu 
613557c0c6eSAlexei Starovoitov /* Called from eBPF program */
614d5a3b1f6SAlexei Starovoitov static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
615d5a3b1f6SAlexei Starovoitov {
6163b4a63f6SPrashant Bhole 	return ERR_PTR(-EOPNOTSUPP);
617557c0c6eSAlexei Starovoitov }
618557c0c6eSAlexei Starovoitov 
619557c0c6eSAlexei Starovoitov /* Called from syscall */
620557c0c6eSAlexei Starovoitov int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
621557c0c6eSAlexei Starovoitov {
622d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
623557c0c6eSAlexei Starovoitov 	struct stack_map_bucket *bucket, *old_bucket;
624557c0c6eSAlexei Starovoitov 	u32 id = *(u32 *)key, trace_len;
625d5a3b1f6SAlexei Starovoitov 
626d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
627557c0c6eSAlexei Starovoitov 		return -ENOENT;
628557c0c6eSAlexei Starovoitov 
629557c0c6eSAlexei Starovoitov 	bucket = xchg(&smap->buckets[id], NULL);
630557c0c6eSAlexei Starovoitov 	if (!bucket)
631557c0c6eSAlexei Starovoitov 		return -ENOENT;
632557c0c6eSAlexei Starovoitov 
633615755a7SSong Liu 	trace_len = bucket->nr * stack_map_data_size(map);
634615755a7SSong Liu 	memcpy(value, bucket->data, trace_len);
635557c0c6eSAlexei Starovoitov 	memset(value + trace_len, 0, map->value_size - trace_len);
636557c0c6eSAlexei Starovoitov 
637557c0c6eSAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], bucket);
638557c0c6eSAlexei Starovoitov 	if (old_bucket)
639557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
640557c0c6eSAlexei Starovoitov 	return 0;
641d5a3b1f6SAlexei Starovoitov }
642d5a3b1f6SAlexei Starovoitov 
64316f07c55SYonghong Song static int stack_map_get_next_key(struct bpf_map *map, void *key,
64416f07c55SYonghong Song 				  void *next_key)
645d5a3b1f6SAlexei Starovoitov {
64616f07c55SYonghong Song 	struct bpf_stack_map *smap = container_of(map,
64716f07c55SYonghong Song 						  struct bpf_stack_map, map);
64816f07c55SYonghong Song 	u32 id;
64916f07c55SYonghong Song 
65016f07c55SYonghong Song 	WARN_ON_ONCE(!rcu_read_lock_held());
65116f07c55SYonghong Song 
65216f07c55SYonghong Song 	if (!key) {
65316f07c55SYonghong Song 		id = 0;
65416f07c55SYonghong Song 	} else {
65516f07c55SYonghong Song 		id = *(u32 *)key;
65616f07c55SYonghong Song 		if (id >= smap->n_buckets || !smap->buckets[id])
65716f07c55SYonghong Song 			id = 0;
65816f07c55SYonghong Song 		else
65916f07c55SYonghong Song 			id++;
66016f07c55SYonghong Song 	}
66116f07c55SYonghong Song 
66216f07c55SYonghong Song 	while (id < smap->n_buckets && !smap->buckets[id])
66316f07c55SYonghong Song 		id++;
66416f07c55SYonghong Song 
66516f07c55SYonghong Song 	if (id >= smap->n_buckets)
66616f07c55SYonghong Song 		return -ENOENT;
66716f07c55SYonghong Song 
66816f07c55SYonghong Song 	*(u32 *)next_key = id;
66916f07c55SYonghong Song 	return 0;
670d5a3b1f6SAlexei Starovoitov }
671d5a3b1f6SAlexei Starovoitov 
672d5a3b1f6SAlexei Starovoitov static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
673d5a3b1f6SAlexei Starovoitov 				 u64 map_flags)
674d5a3b1f6SAlexei Starovoitov {
675d5a3b1f6SAlexei Starovoitov 	return -EINVAL;
676d5a3b1f6SAlexei Starovoitov }
677d5a3b1f6SAlexei Starovoitov 
678d5a3b1f6SAlexei Starovoitov /* Called from syscall or from eBPF program */
679d5a3b1f6SAlexei Starovoitov static int stack_map_delete_elem(struct bpf_map *map, void *key)
680d5a3b1f6SAlexei Starovoitov {
681d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
682d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *old_bucket;
683d5a3b1f6SAlexei Starovoitov 	u32 id = *(u32 *)key;
684d5a3b1f6SAlexei Starovoitov 
685d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
686d5a3b1f6SAlexei Starovoitov 		return -E2BIG;
687d5a3b1f6SAlexei Starovoitov 
688d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], NULL);
689d5a3b1f6SAlexei Starovoitov 	if (old_bucket) {
690557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
691d5a3b1f6SAlexei Starovoitov 		return 0;
692d5a3b1f6SAlexei Starovoitov 	} else {
693d5a3b1f6SAlexei Starovoitov 		return -ENOENT;
694d5a3b1f6SAlexei Starovoitov 	}
695d5a3b1f6SAlexei Starovoitov }
696d5a3b1f6SAlexei Starovoitov 
697d5a3b1f6SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
698d5a3b1f6SAlexei Starovoitov static void stack_map_free(struct bpf_map *map)
699d5a3b1f6SAlexei Starovoitov {
700d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
701d5a3b1f6SAlexei Starovoitov 
702d407bd25SDaniel Borkmann 	bpf_map_area_free(smap->elems);
703557c0c6eSAlexei Starovoitov 	pcpu_freelist_destroy(&smap->freelist);
704d407bd25SDaniel Borkmann 	bpf_map_area_free(smap);
705d5a3b1f6SAlexei Starovoitov 	put_callchain_buffers();
706d5a3b1f6SAlexei Starovoitov }
707d5a3b1f6SAlexei Starovoitov 
7082872e9acSAndrey Ignatov static int stack_trace_map_btf_id;
70914499160SMauricio Vasquez B const struct bpf_map_ops stack_trace_map_ops = {
710f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
711d5a3b1f6SAlexei Starovoitov 	.map_alloc = stack_map_alloc,
712d5a3b1f6SAlexei Starovoitov 	.map_free = stack_map_free,
713d5a3b1f6SAlexei Starovoitov 	.map_get_next_key = stack_map_get_next_key,
714d5a3b1f6SAlexei Starovoitov 	.map_lookup_elem = stack_map_lookup_elem,
715d5a3b1f6SAlexei Starovoitov 	.map_update_elem = stack_map_update_elem,
716d5a3b1f6SAlexei Starovoitov 	.map_delete_elem = stack_map_delete_elem,
717e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
7182872e9acSAndrey Ignatov 	.map_btf_name = "bpf_stack_map",
7192872e9acSAndrey Ignatov 	.map_btf_id = &stack_trace_map_btf_id,
720d5a3b1f6SAlexei Starovoitov };
721bae77c5eSSong Liu 
722bae77c5eSSong Liu static int __init stack_map_init(void)
723bae77c5eSSong Liu {
724bae77c5eSSong Liu 	int cpu;
725bae77c5eSSong Liu 	struct stack_map_irq_work *work;
726bae77c5eSSong Liu 
727bae77c5eSSong Liu 	for_each_possible_cpu(cpu) {
728bae77c5eSSong Liu 		work = per_cpu_ptr(&up_read_work, cpu);
729bae77c5eSSong Liu 		init_irq_work(&work->irq_work, do_up_read);
730bae77c5eSSong Liu 	}
731bae77c5eSSong Liu 	return 0;
732bae77c5eSSong Liu }
733bae77c5eSSong Liu subsys_initcall(stack_map_init);
734