xref: /openbmc/linux/kernel/bpf/stackmap.c (revision 0a94608f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
3  */
4 #include <linux/bpf.h>
5 #include <linux/jhash.h>
6 #include <linux/filter.h>
7 #include <linux/kernel.h>
8 #include <linux/stacktrace.h>
9 #include <linux/perf_event.h>
10 #include <linux/btf_ids.h>
11 #include <linux/buildid.h>
12 #include "percpu_freelist.h"
13 #include "mmap_unlock_work.h"
14 
15 #define STACK_CREATE_FLAG_MASK					\
16 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY |	\
17 	 BPF_F_STACK_BUILD_ID)
18 
19 struct stack_map_bucket {
20 	struct pcpu_freelist_node fnode;
21 	u32 hash;
22 	u32 nr;
23 	u64 data[];
24 };
25 
26 struct bpf_stack_map {
27 	struct bpf_map map;
28 	void *elems;
29 	struct pcpu_freelist freelist;
30 	u32 n_buckets;
31 	struct stack_map_bucket *buckets[];
32 };
33 
34 static inline bool stack_map_use_build_id(struct bpf_map *map)
35 {
36 	return (map->map_flags & BPF_F_STACK_BUILD_ID);
37 }
38 
39 static inline int stack_map_data_size(struct bpf_map *map)
40 {
41 	return stack_map_use_build_id(map) ?
42 		sizeof(struct bpf_stack_build_id) : sizeof(u64);
43 }
44 
45 static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
46 {
47 	u64 elem_size = sizeof(struct stack_map_bucket) +
48 			(u64)smap->map.value_size;
49 	int err;
50 
51 	smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
52 					 smap->map.numa_node);
53 	if (!smap->elems)
54 		return -ENOMEM;
55 
56 	err = pcpu_freelist_init(&smap->freelist);
57 	if (err)
58 		goto free_elems;
59 
60 	pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
61 			       smap->map.max_entries);
62 	return 0;
63 
64 free_elems:
65 	bpf_map_area_free(smap->elems);
66 	return err;
67 }
68 
69 /* Called from syscall */
70 static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
71 {
72 	u32 value_size = attr->value_size;
73 	struct bpf_stack_map *smap;
74 	u64 cost, n_buckets;
75 	int err;
76 
77 	if (!bpf_capable())
78 		return ERR_PTR(-EPERM);
79 
80 	if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
81 		return ERR_PTR(-EINVAL);
82 
83 	/* check sanity of attributes */
84 	if (attr->max_entries == 0 || attr->key_size != 4 ||
85 	    value_size < 8 || value_size % 8)
86 		return ERR_PTR(-EINVAL);
87 
88 	BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
89 	if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
90 		if (value_size % sizeof(struct bpf_stack_build_id) ||
91 		    value_size / sizeof(struct bpf_stack_build_id)
92 		    > sysctl_perf_event_max_stack)
93 			return ERR_PTR(-EINVAL);
94 	} else if (value_size / 8 > sysctl_perf_event_max_stack)
95 		return ERR_PTR(-EINVAL);
96 
97 	/* hash table size must be power of 2 */
98 	n_buckets = roundup_pow_of_two(attr->max_entries);
99 	if (!n_buckets)
100 		return ERR_PTR(-E2BIG);
101 
102 	cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
103 	cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
104 	smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
105 	if (!smap)
106 		return ERR_PTR(-ENOMEM);
107 
108 	bpf_map_init_from_attr(&smap->map, attr);
109 	smap->map.value_size = value_size;
110 	smap->n_buckets = n_buckets;
111 
112 	err = get_callchain_buffers(sysctl_perf_event_max_stack);
113 	if (err)
114 		goto free_smap;
115 
116 	err = prealloc_elems_and_freelist(smap);
117 	if (err)
118 		goto put_buffers;
119 
120 	return &smap->map;
121 
122 put_buffers:
123 	put_callchain_buffers();
124 free_smap:
125 	bpf_map_area_free(smap);
126 	return ERR_PTR(err);
127 }
128 
129 static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
130 					  u64 *ips, u32 trace_nr, bool user)
131 {
132 	int i;
133 	struct mmap_unlock_irq_work *work = NULL;
134 	bool irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
135 	struct vm_area_struct *vma, *prev_vma = NULL;
136 	const char *prev_build_id;
137 
138 	/* If the irq_work is in use, fall back to report ips. Same
139 	 * fallback is used for kernel stack (!user) on a stackmap with
140 	 * build_id.
141 	 */
142 	if (!user || !current || !current->mm || irq_work_busy ||
143 	    !mmap_read_trylock(current->mm)) {
144 		/* cannot access current->mm, fall back to ips */
145 		for (i = 0; i < trace_nr; i++) {
146 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
147 			id_offs[i].ip = ips[i];
148 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
149 		}
150 		return;
151 	}
152 
153 	for (i = 0; i < trace_nr; i++) {
154 		if (range_in_vma(prev_vma, ips[i], ips[i])) {
155 			vma = prev_vma;
156 			memcpy(id_offs[i].build_id, prev_build_id,
157 			       BUILD_ID_SIZE_MAX);
158 			goto build_id_valid;
159 		}
160 		vma = find_vma(current->mm, ips[i]);
161 		if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) {
162 			/* per entry fall back to ips */
163 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
164 			id_offs[i].ip = ips[i];
165 			memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
166 			continue;
167 		}
168 build_id_valid:
169 		id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
170 			- vma->vm_start;
171 		id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
172 		prev_vma = vma;
173 		prev_build_id = id_offs[i].build_id;
174 	}
175 	bpf_mmap_unlock_mm(work, current->mm);
176 }
177 
178 static struct perf_callchain_entry *
179 get_callchain_entry_for_task(struct task_struct *task, u32 max_depth)
180 {
181 #ifdef CONFIG_STACKTRACE
182 	struct perf_callchain_entry *entry;
183 	int rctx;
184 
185 	entry = get_callchain_entry(&rctx);
186 
187 	if (!entry)
188 		return NULL;
189 
190 	entry->nr = stack_trace_save_tsk(task, (unsigned long *)entry->ip,
191 					 max_depth, 0);
192 
193 	/* stack_trace_save_tsk() works on unsigned long array, while
194 	 * perf_callchain_entry uses u64 array. For 32-bit systems, it is
195 	 * necessary to fix this mismatch.
196 	 */
197 	if (__BITS_PER_LONG != 64) {
198 		unsigned long *from = (unsigned long *) entry->ip;
199 		u64 *to = entry->ip;
200 		int i;
201 
202 		/* copy data from the end to avoid using extra buffer */
203 		for (i = entry->nr - 1; i >= 0; i--)
204 			to[i] = (u64)(from[i]);
205 	}
206 
207 	put_callchain_entry(rctx);
208 
209 	return entry;
210 #else /* CONFIG_STACKTRACE */
211 	return NULL;
212 #endif
213 }
214 
215 static long __bpf_get_stackid(struct bpf_map *map,
216 			      struct perf_callchain_entry *trace, u64 flags)
217 {
218 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
219 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
220 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
221 	u32 hash, id, trace_nr, trace_len;
222 	bool user = flags & BPF_F_USER_STACK;
223 	u64 *ips;
224 	bool hash_matches;
225 
226 	if (trace->nr <= skip)
227 		/* skipping more than usable stack trace */
228 		return -EFAULT;
229 
230 	trace_nr = trace->nr - skip;
231 	trace_len = trace_nr * sizeof(u64);
232 	ips = trace->ip + skip;
233 	hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
234 	id = hash & (smap->n_buckets - 1);
235 	bucket = READ_ONCE(smap->buckets[id]);
236 
237 	hash_matches = bucket && bucket->hash == hash;
238 	/* fast cmp */
239 	if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
240 		return id;
241 
242 	if (stack_map_use_build_id(map)) {
243 		/* for build_id+offset, pop a bucket before slow cmp */
244 		new_bucket = (struct stack_map_bucket *)
245 			pcpu_freelist_pop(&smap->freelist);
246 		if (unlikely(!new_bucket))
247 			return -ENOMEM;
248 		new_bucket->nr = trace_nr;
249 		stack_map_get_build_id_offset(
250 			(struct bpf_stack_build_id *)new_bucket->data,
251 			ips, trace_nr, user);
252 		trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
253 		if (hash_matches && bucket->nr == trace_nr &&
254 		    memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
255 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
256 			return id;
257 		}
258 		if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
259 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
260 			return -EEXIST;
261 		}
262 	} else {
263 		if (hash_matches && bucket->nr == trace_nr &&
264 		    memcmp(bucket->data, ips, trace_len) == 0)
265 			return id;
266 		if (bucket && !(flags & BPF_F_REUSE_STACKID))
267 			return -EEXIST;
268 
269 		new_bucket = (struct stack_map_bucket *)
270 			pcpu_freelist_pop(&smap->freelist);
271 		if (unlikely(!new_bucket))
272 			return -ENOMEM;
273 		memcpy(new_bucket->data, ips, trace_len);
274 	}
275 
276 	new_bucket->hash = hash;
277 	new_bucket->nr = trace_nr;
278 
279 	old_bucket = xchg(&smap->buckets[id], new_bucket);
280 	if (old_bucket)
281 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
282 	return id;
283 }
284 
285 BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
286 	   u64, flags)
287 {
288 	u32 max_depth = map->value_size / stack_map_data_size(map);
289 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
290 	bool user = flags & BPF_F_USER_STACK;
291 	struct perf_callchain_entry *trace;
292 	bool kernel = !user;
293 
294 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
295 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
296 		return -EINVAL;
297 
298 	max_depth += skip;
299 	if (max_depth > sysctl_perf_event_max_stack)
300 		max_depth = sysctl_perf_event_max_stack;
301 
302 	trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
303 				   false, false);
304 
305 	if (unlikely(!trace))
306 		/* couldn't fetch the stack trace */
307 		return -EFAULT;
308 
309 	return __bpf_get_stackid(map, trace, flags);
310 }
311 
312 const struct bpf_func_proto bpf_get_stackid_proto = {
313 	.func		= bpf_get_stackid,
314 	.gpl_only	= true,
315 	.ret_type	= RET_INTEGER,
316 	.arg1_type	= ARG_PTR_TO_CTX,
317 	.arg2_type	= ARG_CONST_MAP_PTR,
318 	.arg3_type	= ARG_ANYTHING,
319 };
320 
321 static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
322 {
323 	__u64 nr_kernel = 0;
324 
325 	while (nr_kernel < trace->nr) {
326 		if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
327 			break;
328 		nr_kernel++;
329 	}
330 	return nr_kernel;
331 }
332 
333 BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
334 	   struct bpf_map *, map, u64, flags)
335 {
336 	struct perf_event *event = ctx->event;
337 	struct perf_callchain_entry *trace;
338 	bool kernel, user;
339 	__u64 nr_kernel;
340 	int ret;
341 
342 	/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
343 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
344 		return bpf_get_stackid((unsigned long)(ctx->regs),
345 				       (unsigned long) map, flags, 0, 0);
346 
347 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
348 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
349 		return -EINVAL;
350 
351 	user = flags & BPF_F_USER_STACK;
352 	kernel = !user;
353 
354 	trace = ctx->data->callchain;
355 	if (unlikely(!trace))
356 		return -EFAULT;
357 
358 	nr_kernel = count_kernel_ip(trace);
359 
360 	if (kernel) {
361 		__u64 nr = trace->nr;
362 
363 		trace->nr = nr_kernel;
364 		ret = __bpf_get_stackid(map, trace, flags);
365 
366 		/* restore nr */
367 		trace->nr = nr;
368 	} else { /* user */
369 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
370 
371 		skip += nr_kernel;
372 		if (skip > BPF_F_SKIP_FIELD_MASK)
373 			return -EFAULT;
374 
375 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
376 		ret = __bpf_get_stackid(map, trace, flags);
377 	}
378 	return ret;
379 }
380 
381 const struct bpf_func_proto bpf_get_stackid_proto_pe = {
382 	.func		= bpf_get_stackid_pe,
383 	.gpl_only	= false,
384 	.ret_type	= RET_INTEGER,
385 	.arg1_type	= ARG_PTR_TO_CTX,
386 	.arg2_type	= ARG_CONST_MAP_PTR,
387 	.arg3_type	= ARG_ANYTHING,
388 };
389 
390 static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
391 			    struct perf_callchain_entry *trace_in,
392 			    void *buf, u32 size, u64 flags)
393 {
394 	u32 trace_nr, copy_len, elem_size, num_elem, max_depth;
395 	bool user_build_id = flags & BPF_F_USER_BUILD_ID;
396 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
397 	bool user = flags & BPF_F_USER_STACK;
398 	struct perf_callchain_entry *trace;
399 	bool kernel = !user;
400 	int err = -EINVAL;
401 	u64 *ips;
402 
403 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
404 			       BPF_F_USER_BUILD_ID)))
405 		goto clear;
406 	if (kernel && user_build_id)
407 		goto clear;
408 
409 	elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
410 					    : sizeof(u64);
411 	if (unlikely(size % elem_size))
412 		goto clear;
413 
414 	/* cannot get valid user stack for task without user_mode regs */
415 	if (task && user && !user_mode(regs))
416 		goto err_fault;
417 
418 	num_elem = size / elem_size;
419 	max_depth = num_elem + skip;
420 	if (sysctl_perf_event_max_stack < max_depth)
421 		max_depth = sysctl_perf_event_max_stack;
422 
423 	if (trace_in)
424 		trace = trace_in;
425 	else if (kernel && task)
426 		trace = get_callchain_entry_for_task(task, max_depth);
427 	else
428 		trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
429 					   false, false);
430 	if (unlikely(!trace))
431 		goto err_fault;
432 
433 	if (trace->nr < skip)
434 		goto err_fault;
435 
436 	trace_nr = trace->nr - skip;
437 	trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
438 	copy_len = trace_nr * elem_size;
439 
440 	ips = trace->ip + skip;
441 	if (user && user_build_id)
442 		stack_map_get_build_id_offset(buf, ips, trace_nr, user);
443 	else
444 		memcpy(buf, ips, copy_len);
445 
446 	if (size > copy_len)
447 		memset(buf + copy_len, 0, size - copy_len);
448 	return copy_len;
449 
450 err_fault:
451 	err = -EFAULT;
452 clear:
453 	memset(buf, 0, size);
454 	return err;
455 }
456 
457 BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
458 	   u64, flags)
459 {
460 	return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
461 }
462 
463 const struct bpf_func_proto bpf_get_stack_proto = {
464 	.func		= bpf_get_stack,
465 	.gpl_only	= true,
466 	.ret_type	= RET_INTEGER,
467 	.arg1_type	= ARG_PTR_TO_CTX,
468 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
469 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
470 	.arg4_type	= ARG_ANYTHING,
471 };
472 
473 BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
474 	   u32, size, u64, flags)
475 {
476 	struct pt_regs *regs;
477 	long res = -EINVAL;
478 
479 	if (!try_get_task_stack(task))
480 		return -EFAULT;
481 
482 	regs = task_pt_regs(task);
483 	if (regs)
484 		res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
485 	put_task_stack(task);
486 
487 	return res;
488 }
489 
490 const struct bpf_func_proto bpf_get_task_stack_proto = {
491 	.func		= bpf_get_task_stack,
492 	.gpl_only	= false,
493 	.ret_type	= RET_INTEGER,
494 	.arg1_type	= ARG_PTR_TO_BTF_ID,
495 	.arg1_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
496 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
497 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
498 	.arg4_type	= ARG_ANYTHING,
499 };
500 
501 BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
502 	   void *, buf, u32, size, u64, flags)
503 {
504 	struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
505 	struct perf_event *event = ctx->event;
506 	struct perf_callchain_entry *trace;
507 	bool kernel, user;
508 	int err = -EINVAL;
509 	__u64 nr_kernel;
510 
511 	if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
512 		return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
513 
514 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
515 			       BPF_F_USER_BUILD_ID)))
516 		goto clear;
517 
518 	user = flags & BPF_F_USER_STACK;
519 	kernel = !user;
520 
521 	err = -EFAULT;
522 	trace = ctx->data->callchain;
523 	if (unlikely(!trace))
524 		goto clear;
525 
526 	nr_kernel = count_kernel_ip(trace);
527 
528 	if (kernel) {
529 		__u64 nr = trace->nr;
530 
531 		trace->nr = nr_kernel;
532 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
533 
534 		/* restore nr */
535 		trace->nr = nr;
536 	} else { /* user */
537 		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
538 
539 		skip += nr_kernel;
540 		if (skip > BPF_F_SKIP_FIELD_MASK)
541 			goto clear;
542 
543 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
544 		err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
545 	}
546 	return err;
547 
548 clear:
549 	memset(buf, 0, size);
550 	return err;
551 
552 }
553 
554 const struct bpf_func_proto bpf_get_stack_proto_pe = {
555 	.func		= bpf_get_stack_pe,
556 	.gpl_only	= true,
557 	.ret_type	= RET_INTEGER,
558 	.arg1_type	= ARG_PTR_TO_CTX,
559 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
560 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
561 	.arg4_type	= ARG_ANYTHING,
562 };
563 
564 /* Called from eBPF program */
565 static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
566 {
567 	return ERR_PTR(-EOPNOTSUPP);
568 }
569 
570 /* Called from syscall */
571 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
572 {
573 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
574 	struct stack_map_bucket *bucket, *old_bucket;
575 	u32 id = *(u32 *)key, trace_len;
576 
577 	if (unlikely(id >= smap->n_buckets))
578 		return -ENOENT;
579 
580 	bucket = xchg(&smap->buckets[id], NULL);
581 	if (!bucket)
582 		return -ENOENT;
583 
584 	trace_len = bucket->nr * stack_map_data_size(map);
585 	memcpy(value, bucket->data, trace_len);
586 	memset(value + trace_len, 0, map->value_size - trace_len);
587 
588 	old_bucket = xchg(&smap->buckets[id], bucket);
589 	if (old_bucket)
590 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
591 	return 0;
592 }
593 
594 static int stack_map_get_next_key(struct bpf_map *map, void *key,
595 				  void *next_key)
596 {
597 	struct bpf_stack_map *smap = container_of(map,
598 						  struct bpf_stack_map, map);
599 	u32 id;
600 
601 	WARN_ON_ONCE(!rcu_read_lock_held());
602 
603 	if (!key) {
604 		id = 0;
605 	} else {
606 		id = *(u32 *)key;
607 		if (id >= smap->n_buckets || !smap->buckets[id])
608 			id = 0;
609 		else
610 			id++;
611 	}
612 
613 	while (id < smap->n_buckets && !smap->buckets[id])
614 		id++;
615 
616 	if (id >= smap->n_buckets)
617 		return -ENOENT;
618 
619 	*(u32 *)next_key = id;
620 	return 0;
621 }
622 
623 static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
624 				 u64 map_flags)
625 {
626 	return -EINVAL;
627 }
628 
629 /* Called from syscall or from eBPF program */
630 static int stack_map_delete_elem(struct bpf_map *map, void *key)
631 {
632 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
633 	struct stack_map_bucket *old_bucket;
634 	u32 id = *(u32 *)key;
635 
636 	if (unlikely(id >= smap->n_buckets))
637 		return -E2BIG;
638 
639 	old_bucket = xchg(&smap->buckets[id], NULL);
640 	if (old_bucket) {
641 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
642 		return 0;
643 	} else {
644 		return -ENOENT;
645 	}
646 }
647 
648 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
649 static void stack_map_free(struct bpf_map *map)
650 {
651 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
652 
653 	bpf_map_area_free(smap->elems);
654 	pcpu_freelist_destroy(&smap->freelist);
655 	bpf_map_area_free(smap);
656 	put_callchain_buffers();
657 }
658 
659 static int stack_trace_map_btf_id;
660 const struct bpf_map_ops stack_trace_map_ops = {
661 	.map_meta_equal = bpf_map_meta_equal,
662 	.map_alloc = stack_map_alloc,
663 	.map_free = stack_map_free,
664 	.map_get_next_key = stack_map_get_next_key,
665 	.map_lookup_elem = stack_map_lookup_elem,
666 	.map_update_elem = stack_map_update_elem,
667 	.map_delete_elem = stack_map_delete_elem,
668 	.map_check_btf = map_check_no_btf,
669 	.map_btf_name = "bpf_stack_map",
670 	.map_btf_id = &stack_trace_map_btf_id,
671 };
672