xref: /linux-6.15/kernel/bpf/stackmap.c (revision 33c5cb36)
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 ||
1820cc55a02SMichel Lespinasse 	    !mmap_read_trylock_non_owner(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) {
2070cc55a02SMichel Lespinasse 		mmap_read_unlock_non_owner(current->mm);
208bae77c5eSSong Liu 	} else {
2090cc55a02SMichel Lespinasse 		work->mm = current->mm;
210bae77c5eSSong Liu 		irq_work_queue(&work->irq_work);
211bae77c5eSSong Liu 	}
212615755a7SSong Liu }
213615755a7SSong Liu 
214fa28dcb8SSong Liu static struct perf_callchain_entry *
215fa28dcb8SSong Liu get_callchain_entry_for_task(struct task_struct *task, u32 init_nr)
216fa28dcb8SSong Liu {
217046cc3ddSSong Liu #ifdef CONFIG_STACKTRACE
218fa28dcb8SSong Liu 	struct perf_callchain_entry *entry;
219fa28dcb8SSong Liu 	int rctx;
220fa28dcb8SSong Liu 
221fa28dcb8SSong Liu 	entry = get_callchain_entry(&rctx);
222fa28dcb8SSong Liu 
223fa28dcb8SSong Liu 	if (!entry)
224fa28dcb8SSong Liu 		return NULL;
225fa28dcb8SSong Liu 
226fa28dcb8SSong Liu 	entry->nr = init_nr +
227fa28dcb8SSong Liu 		stack_trace_save_tsk(task, (unsigned long *)(entry->ip + init_nr),
228fa28dcb8SSong Liu 				     sysctl_perf_event_max_stack - init_nr, 0);
229fa28dcb8SSong Liu 
230fa28dcb8SSong Liu 	/* stack_trace_save_tsk() works on unsigned long array, while
231fa28dcb8SSong Liu 	 * perf_callchain_entry uses u64 array. For 32-bit systems, it is
232fa28dcb8SSong Liu 	 * necessary to fix this mismatch.
233fa28dcb8SSong Liu 	 */
234fa28dcb8SSong Liu 	if (__BITS_PER_LONG != 64) {
235fa28dcb8SSong Liu 		unsigned long *from = (unsigned long *) entry->ip;
236fa28dcb8SSong Liu 		u64 *to = entry->ip;
237fa28dcb8SSong Liu 		int i;
238fa28dcb8SSong Liu 
239fa28dcb8SSong Liu 		/* copy data from the end to avoid using extra buffer */
240fa28dcb8SSong Liu 		for (i = entry->nr - 1; i >= (int)init_nr; i--)
241fa28dcb8SSong Liu 			to[i] = (u64)(from[i]);
242fa28dcb8SSong Liu 	}
243fa28dcb8SSong Liu 
244fa28dcb8SSong Liu 	put_callchain_entry(rctx);
245fa28dcb8SSong Liu 
246fa28dcb8SSong Liu 	return entry;
247046cc3ddSSong Liu #else /* CONFIG_STACKTRACE */
248046cc3ddSSong Liu 	return NULL;
249046cc3ddSSong Liu #endif
250fa28dcb8SSong Liu }
251fa28dcb8SSong Liu 
2527b04d6d6SSong Liu static long __bpf_get_stackid(struct bpf_map *map,
2537b04d6d6SSong Liu 			      struct perf_callchain_entry *trace, u64 flags)
254d5a3b1f6SAlexei Starovoitov {
255d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
256d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
257615755a7SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
258c5dfd78eSArnaldo Carvalho de Melo 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
259c5dfd78eSArnaldo Carvalho de Melo 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
260d5a3b1f6SAlexei Starovoitov 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
261d5a3b1f6SAlexei Starovoitov 	u32 hash, id, trace_nr, trace_len;
262d5a3b1f6SAlexei Starovoitov 	bool user = flags & BPF_F_USER_STACK;
263d5a3b1f6SAlexei Starovoitov 	u64 *ips;
264615755a7SSong Liu 	bool hash_matches;
265d5a3b1f6SAlexei Starovoitov 
266d5a3b1f6SAlexei Starovoitov 	/* get_perf_callchain() guarantees that trace->nr >= init_nr
267c5dfd78eSArnaldo Carvalho de Melo 	 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
268d5a3b1f6SAlexei Starovoitov 	 */
269d5a3b1f6SAlexei Starovoitov 	trace_nr = trace->nr - init_nr;
270d5a3b1f6SAlexei Starovoitov 
271d5a3b1f6SAlexei Starovoitov 	if (trace_nr <= skip)
272d5a3b1f6SAlexei Starovoitov 		/* skipping more than usable stack trace */
273d5a3b1f6SAlexei Starovoitov 		return -EFAULT;
274d5a3b1f6SAlexei Starovoitov 
275d5a3b1f6SAlexei Starovoitov 	trace_nr -= skip;
276d5a3b1f6SAlexei Starovoitov 	trace_len = trace_nr * sizeof(u64);
277d5a3b1f6SAlexei Starovoitov 	ips = trace->ip + skip + init_nr;
278d5a3b1f6SAlexei Starovoitov 	hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
279d5a3b1f6SAlexei Starovoitov 	id = hash & (smap->n_buckets - 1);
280557c0c6eSAlexei Starovoitov 	bucket = READ_ONCE(smap->buckets[id]);
281d5a3b1f6SAlexei Starovoitov 
282615755a7SSong Liu 	hash_matches = bucket && bucket->hash == hash;
283615755a7SSong Liu 	/* fast cmp */
284615755a7SSong Liu 	if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
285d5a3b1f6SAlexei Starovoitov 		return id;
286615755a7SSong Liu 
287615755a7SSong Liu 	if (stack_map_use_build_id(map)) {
288615755a7SSong Liu 		/* for build_id+offset, pop a bucket before slow cmp */
289615755a7SSong Liu 		new_bucket = (struct stack_map_bucket *)
290615755a7SSong Liu 			pcpu_freelist_pop(&smap->freelist);
291615755a7SSong Liu 		if (unlikely(!new_bucket))
292615755a7SSong Liu 			return -ENOMEM;
2935f412632SYonghong Song 		new_bucket->nr = trace_nr;
2945f412632SYonghong Song 		stack_map_get_build_id_offset(
2955f412632SYonghong Song 			(struct bpf_stack_build_id *)new_bucket->data,
2965f412632SYonghong Song 			ips, trace_nr, user);
297615755a7SSong Liu 		trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
298615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
299615755a7SSong Liu 		    memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
300615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
301d5a3b1f6SAlexei Starovoitov 			return id;
302d5a3b1f6SAlexei Starovoitov 		}
303615755a7SSong Liu 		if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
304615755a7SSong Liu 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
305615755a7SSong Liu 			return -EEXIST;
306615755a7SSong Liu 		}
307615755a7SSong Liu 	} else {
308615755a7SSong Liu 		if (hash_matches && bucket->nr == trace_nr &&
309615755a7SSong Liu 		    memcmp(bucket->data, ips, trace_len) == 0)
310615755a7SSong Liu 			return id;
311d5a3b1f6SAlexei Starovoitov 		if (bucket && !(flags & BPF_F_REUSE_STACKID))
312d5a3b1f6SAlexei Starovoitov 			return -EEXIST;
313d5a3b1f6SAlexei Starovoitov 
314557c0c6eSAlexei Starovoitov 		new_bucket = (struct stack_map_bucket *)
315557c0c6eSAlexei Starovoitov 			pcpu_freelist_pop(&smap->freelist);
316d5a3b1f6SAlexei Starovoitov 		if (unlikely(!new_bucket))
317d5a3b1f6SAlexei Starovoitov 			return -ENOMEM;
318615755a7SSong Liu 		memcpy(new_bucket->data, ips, trace_len);
319615755a7SSong Liu 	}
320d5a3b1f6SAlexei Starovoitov 
321d5a3b1f6SAlexei Starovoitov 	new_bucket->hash = hash;
322d5a3b1f6SAlexei Starovoitov 	new_bucket->nr = trace_nr;
323d5a3b1f6SAlexei Starovoitov 
324d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], new_bucket);
325d5a3b1f6SAlexei Starovoitov 	if (old_bucket)
326557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
327d5a3b1f6SAlexei Starovoitov 	return id;
328d5a3b1f6SAlexei Starovoitov }
329d5a3b1f6SAlexei Starovoitov 
3307b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
3317b04d6d6SSong Liu 	   u64, flags)
3327b04d6d6SSong Liu {
3337b04d6d6SSong Liu 	u32 max_depth = map->value_size / stack_map_data_size(map);
3347b04d6d6SSong Liu 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
3357b04d6d6SSong Liu 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
3367b04d6d6SSong Liu 	bool user = flags & BPF_F_USER_STACK;
3377b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
3387b04d6d6SSong Liu 	bool kernel = !user;
3397b04d6d6SSong Liu 
3407b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
3417b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
3427b04d6d6SSong Liu 		return -EINVAL;
3437b04d6d6SSong Liu 
3447b04d6d6SSong Liu 	trace = get_perf_callchain(regs, init_nr, kernel, user,
3457b04d6d6SSong Liu 				   sysctl_perf_event_max_stack, false, false);
3467b04d6d6SSong Liu 
3477b04d6d6SSong Liu 	if (unlikely(!trace))
3487b04d6d6SSong Liu 		/* couldn't fetch the stack trace */
3497b04d6d6SSong Liu 		return -EFAULT;
3507b04d6d6SSong Liu 
3517b04d6d6SSong Liu 	return __bpf_get_stackid(map, trace, flags);
3527b04d6d6SSong Liu }
3537b04d6d6SSong Liu 
354d5a3b1f6SAlexei Starovoitov const struct bpf_func_proto bpf_get_stackid_proto = {
355d5a3b1f6SAlexei Starovoitov 	.func		= bpf_get_stackid,
356d5a3b1f6SAlexei Starovoitov 	.gpl_only	= true,
357d5a3b1f6SAlexei Starovoitov 	.ret_type	= RET_INTEGER,
358d5a3b1f6SAlexei Starovoitov 	.arg1_type	= ARG_PTR_TO_CTX,
359d5a3b1f6SAlexei Starovoitov 	.arg2_type	= ARG_CONST_MAP_PTR,
360d5a3b1f6SAlexei Starovoitov 	.arg3_type	= ARG_ANYTHING,
361d5a3b1f6SAlexei Starovoitov };
362d5a3b1f6SAlexei Starovoitov 
3637b04d6d6SSong Liu static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
3647b04d6d6SSong Liu {
3657b04d6d6SSong Liu 	__u64 nr_kernel = 0;
3667b04d6d6SSong Liu 
3677b04d6d6SSong Liu 	while (nr_kernel < trace->nr) {
3687b04d6d6SSong Liu 		if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
3697b04d6d6SSong Liu 			break;
3707b04d6d6SSong Liu 		nr_kernel++;
3717b04d6d6SSong Liu 	}
3727b04d6d6SSong Liu 	return nr_kernel;
3737b04d6d6SSong Liu }
3747b04d6d6SSong Liu 
3757b04d6d6SSong Liu BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
3767b04d6d6SSong Liu 	   struct bpf_map *, map, u64, flags)
3777b04d6d6SSong Liu {
3787b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
3797b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
3807b04d6d6SSong Liu 	bool kernel, user;
3817b04d6d6SSong Liu 	__u64 nr_kernel;
3827b04d6d6SSong Liu 	int ret;
3837b04d6d6SSong Liu 
3847b04d6d6SSong Liu 	/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
3857b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
3867b04d6d6SSong Liu 		return bpf_get_stackid((unsigned long)(ctx->regs),
3877b04d6d6SSong Liu 				       (unsigned long) map, flags, 0, 0);
3887b04d6d6SSong Liu 
3897b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
3907b04d6d6SSong Liu 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
3917b04d6d6SSong Liu 		return -EINVAL;
3927b04d6d6SSong Liu 
3937b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
3947b04d6d6SSong Liu 	kernel = !user;
3957b04d6d6SSong Liu 
3967b04d6d6SSong Liu 	trace = ctx->data->callchain;
3977b04d6d6SSong Liu 	if (unlikely(!trace))
3987b04d6d6SSong Liu 		return -EFAULT;
3997b04d6d6SSong Liu 
4007b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
4017b04d6d6SSong Liu 
4027b04d6d6SSong Liu 	if (kernel) {
4037b04d6d6SSong Liu 		__u64 nr = trace->nr;
4047b04d6d6SSong Liu 
4057b04d6d6SSong Liu 		trace->nr = nr_kernel;
4067b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
4077b04d6d6SSong Liu 
4087b04d6d6SSong Liu 		/* restore nr */
4097b04d6d6SSong Liu 		trace->nr = nr;
4107b04d6d6SSong Liu 	} else { /* user */
4117b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
4127b04d6d6SSong Liu 
4137b04d6d6SSong Liu 		skip += nr_kernel;
4147b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
4157b04d6d6SSong Liu 			return -EFAULT;
4167b04d6d6SSong Liu 
4177b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
4187b04d6d6SSong Liu 		ret = __bpf_get_stackid(map, trace, flags);
4197b04d6d6SSong Liu 	}
4207b04d6d6SSong Liu 	return ret;
4217b04d6d6SSong Liu }
4227b04d6d6SSong Liu 
4237b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stackid_proto_pe = {
4247b04d6d6SSong Liu 	.func		= bpf_get_stackid_pe,
4257b04d6d6SSong Liu 	.gpl_only	= false,
4267b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
4277b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
4287b04d6d6SSong Liu 	.arg2_type	= ARG_CONST_MAP_PTR,
4297b04d6d6SSong Liu 	.arg3_type	= ARG_ANYTHING,
4307b04d6d6SSong Liu };
4317b04d6d6SSong Liu 
432fa28dcb8SSong Liu static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
4337b04d6d6SSong Liu 			    struct perf_callchain_entry *trace_in,
434fa28dcb8SSong Liu 			    void *buf, u32 size, u64 flags)
435c195651eSYonghong Song {
436c195651eSYonghong Song 	u32 init_nr, trace_nr, copy_len, elem_size, num_elem;
437c195651eSYonghong Song 	bool user_build_id = flags & BPF_F_USER_BUILD_ID;
438c195651eSYonghong Song 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
439c195651eSYonghong Song 	bool user = flags & BPF_F_USER_STACK;
440c195651eSYonghong Song 	struct perf_callchain_entry *trace;
441c195651eSYonghong Song 	bool kernel = !user;
442c195651eSYonghong Song 	int err = -EINVAL;
443c195651eSYonghong Song 	u64 *ips;
444c195651eSYonghong Song 
445c195651eSYonghong Song 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
446c195651eSYonghong Song 			       BPF_F_USER_BUILD_ID)))
447c195651eSYonghong Song 		goto clear;
448c195651eSYonghong Song 	if (kernel && user_build_id)
449c195651eSYonghong Song 		goto clear;
450c195651eSYonghong Song 
451c195651eSYonghong Song 	elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
452c195651eSYonghong Song 					    : sizeof(u64);
453c195651eSYonghong Song 	if (unlikely(size % elem_size))
454c195651eSYonghong Song 		goto clear;
455c195651eSYonghong Song 
456fa28dcb8SSong Liu 	/* cannot get valid user stack for task without user_mode regs */
457fa28dcb8SSong Liu 	if (task && user && !user_mode(regs))
458fa28dcb8SSong Liu 		goto err_fault;
459fa28dcb8SSong Liu 
460c195651eSYonghong Song 	num_elem = size / elem_size;
461c195651eSYonghong Song 	if (sysctl_perf_event_max_stack < num_elem)
462c195651eSYonghong Song 		init_nr = 0;
463c195651eSYonghong Song 	else
464c195651eSYonghong Song 		init_nr = sysctl_perf_event_max_stack - num_elem;
465fa28dcb8SSong Liu 
4667b04d6d6SSong Liu 	if (trace_in)
4677b04d6d6SSong Liu 		trace = trace_in;
4687b04d6d6SSong Liu 	else if (kernel && task)
469fa28dcb8SSong Liu 		trace = get_callchain_entry_for_task(task, init_nr);
470fa28dcb8SSong Liu 	else
471c195651eSYonghong Song 		trace = get_perf_callchain(regs, init_nr, kernel, user,
472fa28dcb8SSong Liu 					   sysctl_perf_event_max_stack,
473fa28dcb8SSong Liu 					   false, false);
474c195651eSYonghong Song 	if (unlikely(!trace))
475c195651eSYonghong Song 		goto err_fault;
476c195651eSYonghong Song 
477c195651eSYonghong Song 	trace_nr = trace->nr - init_nr;
478c195651eSYonghong Song 	if (trace_nr < skip)
479c195651eSYonghong Song 		goto err_fault;
480c195651eSYonghong Song 
481c195651eSYonghong Song 	trace_nr -= skip;
482c195651eSYonghong Song 	trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
483c195651eSYonghong Song 	copy_len = trace_nr * elem_size;
484c195651eSYonghong Song 	ips = trace->ip + skip + init_nr;
485c195651eSYonghong Song 	if (user && user_build_id)
486c195651eSYonghong Song 		stack_map_get_build_id_offset(buf, ips, trace_nr, user);
487c195651eSYonghong Song 	else
488c195651eSYonghong Song 		memcpy(buf, ips, copy_len);
489c195651eSYonghong Song 
490c195651eSYonghong Song 	if (size > copy_len)
491c195651eSYonghong Song 		memset(buf + copy_len, 0, size - copy_len);
492c195651eSYonghong Song 	return copy_len;
493c195651eSYonghong Song 
494c195651eSYonghong Song err_fault:
495c195651eSYonghong Song 	err = -EFAULT;
496c195651eSYonghong Song clear:
497c195651eSYonghong Song 	memset(buf, 0, size);
498c195651eSYonghong Song 	return err;
499c195651eSYonghong Song }
500c195651eSYonghong Song 
501fa28dcb8SSong Liu BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
502fa28dcb8SSong Liu 	   u64, flags)
503fa28dcb8SSong Liu {
5047b04d6d6SSong Liu 	return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
505fa28dcb8SSong Liu }
506fa28dcb8SSong Liu 
507c195651eSYonghong Song const struct bpf_func_proto bpf_get_stack_proto = {
508c195651eSYonghong Song 	.func		= bpf_get_stack,
509c195651eSYonghong Song 	.gpl_only	= true,
510c195651eSYonghong Song 	.ret_type	= RET_INTEGER,
511c195651eSYonghong Song 	.arg1_type	= ARG_PTR_TO_CTX,
512c195651eSYonghong Song 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
513c195651eSYonghong Song 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
514c195651eSYonghong Song 	.arg4_type	= ARG_ANYTHING,
515c195651eSYonghong Song };
516c195651eSYonghong Song 
517fa28dcb8SSong Liu BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
518fa28dcb8SSong Liu 	   u32, size, u64, flags)
519fa28dcb8SSong Liu {
52006ab134cSDave Marchevsky 	struct pt_regs *regs;
52106ab134cSDave Marchevsky 	long res;
522fa28dcb8SSong Liu 
52306ab134cSDave Marchevsky 	if (!try_get_task_stack(task))
52406ab134cSDave Marchevsky 		return -EFAULT;
52506ab134cSDave Marchevsky 
52606ab134cSDave Marchevsky 	regs = task_pt_regs(task);
52706ab134cSDave Marchevsky 	res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
52806ab134cSDave Marchevsky 	put_task_stack(task);
52906ab134cSDave Marchevsky 
53006ab134cSDave Marchevsky 	return res;
531fa28dcb8SSong Liu }
532fa28dcb8SSong Liu 
533fa28dcb8SSong Liu const struct bpf_func_proto bpf_get_task_stack_proto = {
534fa28dcb8SSong Liu 	.func		= bpf_get_task_stack,
535fa28dcb8SSong Liu 	.gpl_only	= false,
536fa28dcb8SSong Liu 	.ret_type	= RET_INTEGER,
537fa28dcb8SSong Liu 	.arg1_type	= ARG_PTR_TO_BTF_ID,
538*33c5cb36SDaniel Xu 	.arg1_btf_id	= &btf_task_struct_ids[0],
539fa28dcb8SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
540fa28dcb8SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
541fa28dcb8SSong Liu 	.arg4_type	= ARG_ANYTHING,
542fa28dcb8SSong Liu };
543fa28dcb8SSong Liu 
5447b04d6d6SSong Liu BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
5457b04d6d6SSong Liu 	   void *, buf, u32, size, u64, flags)
5467b04d6d6SSong Liu {
5472b9b305fSSong Liu 	struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
5487b04d6d6SSong Liu 	struct perf_event *event = ctx->event;
5497b04d6d6SSong Liu 	struct perf_callchain_entry *trace;
5507b04d6d6SSong Liu 	bool kernel, user;
5517b04d6d6SSong Liu 	int err = -EINVAL;
5527b04d6d6SSong Liu 	__u64 nr_kernel;
5537b04d6d6SSong Liu 
5547b04d6d6SSong Liu 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
5552b9b305fSSong Liu 		return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
5567b04d6d6SSong Liu 
5577b04d6d6SSong Liu 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
5587b04d6d6SSong Liu 			       BPF_F_USER_BUILD_ID)))
5597b04d6d6SSong Liu 		goto clear;
5607b04d6d6SSong Liu 
5617b04d6d6SSong Liu 	user = flags & BPF_F_USER_STACK;
5627b04d6d6SSong Liu 	kernel = !user;
5637b04d6d6SSong Liu 
5647b04d6d6SSong Liu 	err = -EFAULT;
5657b04d6d6SSong Liu 	trace = ctx->data->callchain;
5667b04d6d6SSong Liu 	if (unlikely(!trace))
5677b04d6d6SSong Liu 		goto clear;
5687b04d6d6SSong Liu 
5697b04d6d6SSong Liu 	nr_kernel = count_kernel_ip(trace);
5707b04d6d6SSong Liu 
5717b04d6d6SSong Liu 	if (kernel) {
5727b04d6d6SSong Liu 		__u64 nr = trace->nr;
5737b04d6d6SSong Liu 
5747b04d6d6SSong Liu 		trace->nr = nr_kernel;
5752b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5767b04d6d6SSong Liu 
5777b04d6d6SSong Liu 		/* restore nr */
5787b04d6d6SSong Liu 		trace->nr = nr;
5797b04d6d6SSong Liu 	} else { /* user */
5807b04d6d6SSong Liu 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
5817b04d6d6SSong Liu 
5827b04d6d6SSong Liu 		skip += nr_kernel;
5837b04d6d6SSong Liu 		if (skip > BPF_F_SKIP_FIELD_MASK)
5847b04d6d6SSong Liu 			goto clear;
5857b04d6d6SSong Liu 
5867b04d6d6SSong Liu 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
5872b9b305fSSong Liu 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
5887b04d6d6SSong Liu 	}
5897b04d6d6SSong Liu 	return err;
5907b04d6d6SSong Liu 
5917b04d6d6SSong Liu clear:
5927b04d6d6SSong Liu 	memset(buf, 0, size);
5937b04d6d6SSong Liu 	return err;
5947b04d6d6SSong Liu 
5957b04d6d6SSong Liu }
5967b04d6d6SSong Liu 
5977b04d6d6SSong Liu const struct bpf_func_proto bpf_get_stack_proto_pe = {
5987b04d6d6SSong Liu 	.func		= bpf_get_stack_pe,
5997b04d6d6SSong Liu 	.gpl_only	= true,
6007b04d6d6SSong Liu 	.ret_type	= RET_INTEGER,
6017b04d6d6SSong Liu 	.arg1_type	= ARG_PTR_TO_CTX,
6027b04d6d6SSong Liu 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
6037b04d6d6SSong Liu 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
6047b04d6d6SSong Liu 	.arg4_type	= ARG_ANYTHING,
6057b04d6d6SSong Liu };
6067b04d6d6SSong Liu 
607557c0c6eSAlexei Starovoitov /* Called from eBPF program */
608d5a3b1f6SAlexei Starovoitov static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
609d5a3b1f6SAlexei Starovoitov {
6103b4a63f6SPrashant Bhole 	return ERR_PTR(-EOPNOTSUPP);
611557c0c6eSAlexei Starovoitov }
612557c0c6eSAlexei Starovoitov 
613557c0c6eSAlexei Starovoitov /* Called from syscall */
614557c0c6eSAlexei Starovoitov int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
615557c0c6eSAlexei Starovoitov {
616d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
617557c0c6eSAlexei Starovoitov 	struct stack_map_bucket *bucket, *old_bucket;
618557c0c6eSAlexei Starovoitov 	u32 id = *(u32 *)key, trace_len;
619d5a3b1f6SAlexei Starovoitov 
620d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
621557c0c6eSAlexei Starovoitov 		return -ENOENT;
622557c0c6eSAlexei Starovoitov 
623557c0c6eSAlexei Starovoitov 	bucket = xchg(&smap->buckets[id], NULL);
624557c0c6eSAlexei Starovoitov 	if (!bucket)
625557c0c6eSAlexei Starovoitov 		return -ENOENT;
626557c0c6eSAlexei Starovoitov 
627615755a7SSong Liu 	trace_len = bucket->nr * stack_map_data_size(map);
628615755a7SSong Liu 	memcpy(value, bucket->data, trace_len);
629557c0c6eSAlexei Starovoitov 	memset(value + trace_len, 0, map->value_size - trace_len);
630557c0c6eSAlexei Starovoitov 
631557c0c6eSAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], bucket);
632557c0c6eSAlexei Starovoitov 	if (old_bucket)
633557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
634557c0c6eSAlexei Starovoitov 	return 0;
635d5a3b1f6SAlexei Starovoitov }
636d5a3b1f6SAlexei Starovoitov 
63716f07c55SYonghong Song static int stack_map_get_next_key(struct bpf_map *map, void *key,
63816f07c55SYonghong Song 				  void *next_key)
639d5a3b1f6SAlexei Starovoitov {
64016f07c55SYonghong Song 	struct bpf_stack_map *smap = container_of(map,
64116f07c55SYonghong Song 						  struct bpf_stack_map, map);
64216f07c55SYonghong Song 	u32 id;
64316f07c55SYonghong Song 
64416f07c55SYonghong Song 	WARN_ON_ONCE(!rcu_read_lock_held());
64516f07c55SYonghong Song 
64616f07c55SYonghong Song 	if (!key) {
64716f07c55SYonghong Song 		id = 0;
64816f07c55SYonghong Song 	} else {
64916f07c55SYonghong Song 		id = *(u32 *)key;
65016f07c55SYonghong Song 		if (id >= smap->n_buckets || !smap->buckets[id])
65116f07c55SYonghong Song 			id = 0;
65216f07c55SYonghong Song 		else
65316f07c55SYonghong Song 			id++;
65416f07c55SYonghong Song 	}
65516f07c55SYonghong Song 
65616f07c55SYonghong Song 	while (id < smap->n_buckets && !smap->buckets[id])
65716f07c55SYonghong Song 		id++;
65816f07c55SYonghong Song 
65916f07c55SYonghong Song 	if (id >= smap->n_buckets)
66016f07c55SYonghong Song 		return -ENOENT;
66116f07c55SYonghong Song 
66216f07c55SYonghong Song 	*(u32 *)next_key = id;
66316f07c55SYonghong Song 	return 0;
664d5a3b1f6SAlexei Starovoitov }
665d5a3b1f6SAlexei Starovoitov 
666d5a3b1f6SAlexei Starovoitov static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
667d5a3b1f6SAlexei Starovoitov 				 u64 map_flags)
668d5a3b1f6SAlexei Starovoitov {
669d5a3b1f6SAlexei Starovoitov 	return -EINVAL;
670d5a3b1f6SAlexei Starovoitov }
671d5a3b1f6SAlexei Starovoitov 
672d5a3b1f6SAlexei Starovoitov /* Called from syscall or from eBPF program */
673d5a3b1f6SAlexei Starovoitov static int stack_map_delete_elem(struct bpf_map *map, void *key)
674d5a3b1f6SAlexei Starovoitov {
675d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
676d5a3b1f6SAlexei Starovoitov 	struct stack_map_bucket *old_bucket;
677d5a3b1f6SAlexei Starovoitov 	u32 id = *(u32 *)key;
678d5a3b1f6SAlexei Starovoitov 
679d5a3b1f6SAlexei Starovoitov 	if (unlikely(id >= smap->n_buckets))
680d5a3b1f6SAlexei Starovoitov 		return -E2BIG;
681d5a3b1f6SAlexei Starovoitov 
682d5a3b1f6SAlexei Starovoitov 	old_bucket = xchg(&smap->buckets[id], NULL);
683d5a3b1f6SAlexei Starovoitov 	if (old_bucket) {
684557c0c6eSAlexei Starovoitov 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
685d5a3b1f6SAlexei Starovoitov 		return 0;
686d5a3b1f6SAlexei Starovoitov 	} else {
687d5a3b1f6SAlexei Starovoitov 		return -ENOENT;
688d5a3b1f6SAlexei Starovoitov 	}
689d5a3b1f6SAlexei Starovoitov }
690d5a3b1f6SAlexei Starovoitov 
691d5a3b1f6SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
692d5a3b1f6SAlexei Starovoitov static void stack_map_free(struct bpf_map *map)
693d5a3b1f6SAlexei Starovoitov {
694d5a3b1f6SAlexei Starovoitov 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
695d5a3b1f6SAlexei Starovoitov 
696d407bd25SDaniel Borkmann 	bpf_map_area_free(smap->elems);
697557c0c6eSAlexei Starovoitov 	pcpu_freelist_destroy(&smap->freelist);
698d407bd25SDaniel Borkmann 	bpf_map_area_free(smap);
699d5a3b1f6SAlexei Starovoitov 	put_callchain_buffers();
700d5a3b1f6SAlexei Starovoitov }
701d5a3b1f6SAlexei Starovoitov 
7022872e9acSAndrey Ignatov static int stack_trace_map_btf_id;
70314499160SMauricio Vasquez B const struct bpf_map_ops stack_trace_map_ops = {
704f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
705d5a3b1f6SAlexei Starovoitov 	.map_alloc = stack_map_alloc,
706d5a3b1f6SAlexei Starovoitov 	.map_free = stack_map_free,
707d5a3b1f6SAlexei Starovoitov 	.map_get_next_key = stack_map_get_next_key,
708d5a3b1f6SAlexei Starovoitov 	.map_lookup_elem = stack_map_lookup_elem,
709d5a3b1f6SAlexei Starovoitov 	.map_update_elem = stack_map_update_elem,
710d5a3b1f6SAlexei Starovoitov 	.map_delete_elem = stack_map_delete_elem,
711e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
7122872e9acSAndrey Ignatov 	.map_btf_name = "bpf_stack_map",
7132872e9acSAndrey Ignatov 	.map_btf_id = &stack_trace_map_btf_id,
714d5a3b1f6SAlexei Starovoitov };
715bae77c5eSSong Liu 
716bae77c5eSSong Liu static int __init stack_map_init(void)
717bae77c5eSSong Liu {
718bae77c5eSSong Liu 	int cpu;
719bae77c5eSSong Liu 	struct stack_map_irq_work *work;
720bae77c5eSSong Liu 
721bae77c5eSSong Liu 	for_each_possible_cpu(cpu) {
722bae77c5eSSong Liu 		work = per_cpu_ptr(&up_read_work, cpu);
723bae77c5eSSong Liu 		init_irq_work(&work->irq_work, do_up_read);
724bae77c5eSSong Liu 	}
725bae77c5eSSong Liu 	return 0;
726bae77c5eSSong Liu }
727bae77c5eSSong Liu subsys_initcall(stack_map_init);
728