• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/stacktrace.h>
8 #include <linux/perf_event.h>
9 #include <linux/elf.h>
10 #include <linux/pagemap.h>
11 #include <linux/irq_work.h>
12 #include "percpu_freelist.h"
13 
14 #define STACK_CREATE_FLAG_MASK					\
15 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY |	\
16 	 BPF_F_STACK_BUILD_ID)
17 
18 struct stack_map_bucket {
19 	struct pcpu_freelist_node fnode;
20 	u32 hash;
21 	u32 nr;
22 	u64 data[];
23 };
24 
25 struct bpf_stack_map {
26 	struct bpf_map map;
27 	void *elems;
28 	struct pcpu_freelist freelist;
29 	u32 n_buckets;
30 	struct stack_map_bucket *buckets[];
31 };
32 
33 /* irq_work to run up_read() for build_id lookup in nmi context */
34 struct stack_map_irq_work {
35 	struct irq_work irq_work;
36 	struct rw_semaphore *sem;
37 };
38 
do_up_read(struct irq_work * entry)39 static void do_up_read(struct irq_work *entry)
40 {
41 	struct stack_map_irq_work *work;
42 
43 	work = container_of(entry, struct stack_map_irq_work, irq_work);
44 	up_read_non_owner(work->sem);
45 	work->sem = NULL;
46 }
47 
48 static DEFINE_PER_CPU(struct stack_map_irq_work, up_read_work);
49 
stack_map_use_build_id(struct bpf_map * map)50 static inline bool stack_map_use_build_id(struct bpf_map *map)
51 {
52 	return (map->map_flags & BPF_F_STACK_BUILD_ID);
53 }
54 
stack_map_data_size(struct bpf_map * map)55 static inline int stack_map_data_size(struct bpf_map *map)
56 {
57 	return stack_map_use_build_id(map) ?
58 		sizeof(struct bpf_stack_build_id) : sizeof(u64);
59 }
60 
prealloc_elems_and_freelist(struct bpf_stack_map * smap)61 static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
62 {
63 	u64 elem_size = sizeof(struct stack_map_bucket) +
64 			(u64)smap->map.value_size;
65 	int err;
66 
67 	smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
68 					 smap->map.numa_node);
69 	if (!smap->elems)
70 		return -ENOMEM;
71 
72 	err = pcpu_freelist_init(&smap->freelist);
73 	if (err)
74 		goto free_elems;
75 
76 	pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
77 			       smap->map.max_entries);
78 	return 0;
79 
80 free_elems:
81 	bpf_map_area_free(smap->elems);
82 	return err;
83 }
84 
85 /* Called from syscall */
stack_map_alloc(union bpf_attr * attr)86 static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
87 {
88 	u32 value_size = attr->value_size;
89 	struct bpf_stack_map *smap;
90 	struct bpf_map_memory mem;
91 	u64 cost, n_buckets;
92 	int err;
93 
94 	if (!capable(CAP_SYS_ADMIN))
95 		return ERR_PTR(-EPERM);
96 
97 	if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
98 		return ERR_PTR(-EINVAL);
99 
100 	/* check sanity of attributes */
101 	if (attr->max_entries == 0 || attr->key_size != 4 ||
102 	    value_size < 8 || value_size % 8)
103 		return ERR_PTR(-EINVAL);
104 
105 	BUILD_BUG_ON(sizeof(struct bpf_stack_build_id) % sizeof(u64));
106 	if (attr->map_flags & BPF_F_STACK_BUILD_ID) {
107 		if (value_size % sizeof(struct bpf_stack_build_id) ||
108 		    value_size / sizeof(struct bpf_stack_build_id)
109 		    > sysctl_perf_event_max_stack)
110 			return ERR_PTR(-EINVAL);
111 	} else if (value_size / 8 > sysctl_perf_event_max_stack)
112 		return ERR_PTR(-EINVAL);
113 
114 	/* hash table size must be power of 2; roundup_pow_of_two() can overflow
115 	 * into UB on 32-bit arches, so check that first
116 	 */
117 	if (attr->max_entries > 1UL << 31)
118 		return ERR_PTR(-E2BIG);
119 
120 	n_buckets = roundup_pow_of_two(attr->max_entries);
121 
122 	cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
123 	err = bpf_map_charge_init(&mem, cost + attr->max_entries *
124 			   (sizeof(struct stack_map_bucket) + (u64)value_size));
125 	if (err)
126 		return ERR_PTR(err);
127 
128 	smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
129 	if (!smap) {
130 		bpf_map_charge_finish(&mem);
131 		return ERR_PTR(-ENOMEM);
132 	}
133 
134 	bpf_map_init_from_attr(&smap->map, attr);
135 	smap->map.value_size = value_size;
136 	smap->n_buckets = n_buckets;
137 
138 	err = get_callchain_buffers(sysctl_perf_event_max_stack);
139 	if (err)
140 		goto free_charge;
141 
142 	err = prealloc_elems_and_freelist(smap);
143 	if (err)
144 		goto put_buffers;
145 
146 	bpf_map_charge_move(&smap->map.memory, &mem);
147 
148 	return &smap->map;
149 
150 put_buffers:
151 	put_callchain_buffers();
152 free_charge:
153 	bpf_map_charge_finish(&mem);
154 	bpf_map_area_free(smap);
155 	return ERR_PTR(err);
156 }
157 
158 #define BPF_BUILD_ID 3
159 /*
160  * Parse build id from the note segment. This logic can be shared between
161  * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
162  * identical.
163  */
stack_map_parse_build_id(void * page_addr,unsigned char * build_id,void * note_start,Elf32_Word note_size)164 static inline int stack_map_parse_build_id(void *page_addr,
165 					   unsigned char *build_id,
166 					   void *note_start,
167 					   Elf32_Word note_size)
168 {
169 	Elf32_Word note_offs = 0, new_offs;
170 
171 	/* check for overflow */
172 	if (note_start < page_addr || note_start + note_size < note_start)
173 		return -EINVAL;
174 
175 	/* only supports note that fits in the first page */
176 	if (note_start + note_size > page_addr + PAGE_SIZE)
177 		return -EINVAL;
178 
179 	while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
180 		Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
181 
182 		if (nhdr->n_type == BPF_BUILD_ID &&
183 		    nhdr->n_namesz == sizeof("GNU") &&
184 		    nhdr->n_descsz > 0 &&
185 		    nhdr->n_descsz <= BPF_BUILD_ID_SIZE) {
186 			memcpy(build_id,
187 			       note_start + note_offs +
188 			       ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
189 			       nhdr->n_descsz);
190 			memset(build_id + nhdr->n_descsz, 0,
191 			       BPF_BUILD_ID_SIZE - nhdr->n_descsz);
192 			return 0;
193 		}
194 		new_offs = note_offs + sizeof(Elf32_Nhdr) +
195 			ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
196 		if (new_offs <= note_offs)  /* overflow */
197 			break;
198 		note_offs = new_offs;
199 	}
200 	return -EINVAL;
201 }
202 
203 /* Parse build ID from 32-bit ELF */
stack_map_get_build_id_32(void * page_addr,unsigned char * build_id)204 static int stack_map_get_build_id_32(void *page_addr,
205 				     unsigned char *build_id)
206 {
207 	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
208 	Elf32_Phdr *phdr;
209 	int i;
210 
211 	/* only supports phdr that fits in one page */
212 	if (ehdr->e_phnum >
213 	    (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
214 		return -EINVAL;
215 
216 	phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
217 
218 	for (i = 0; i < ehdr->e_phnum; ++i)
219 		if (phdr[i].p_type == PT_NOTE)
220 			return stack_map_parse_build_id(page_addr, build_id,
221 					page_addr + phdr[i].p_offset,
222 					phdr[i].p_filesz);
223 	return -EINVAL;
224 }
225 
226 /* Parse build ID from 64-bit ELF */
stack_map_get_build_id_64(void * page_addr,unsigned char * build_id)227 static int stack_map_get_build_id_64(void *page_addr,
228 				     unsigned char *build_id)
229 {
230 	Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
231 	Elf64_Phdr *phdr;
232 	int i;
233 
234 	/* only supports phdr that fits in one page */
235 	if (ehdr->e_phnum >
236 	    (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
237 		return -EINVAL;
238 
239 	phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
240 
241 	for (i = 0; i < ehdr->e_phnum; ++i)
242 		if (phdr[i].p_type == PT_NOTE)
243 			return stack_map_parse_build_id(page_addr, build_id,
244 					page_addr + phdr[i].p_offset,
245 					phdr[i].p_filesz);
246 	return -EINVAL;
247 }
248 
249 /* Parse build ID of ELF file mapped to vma */
stack_map_get_build_id(struct vm_area_struct * vma,unsigned char * build_id)250 static int stack_map_get_build_id(struct vm_area_struct *vma,
251 				  unsigned char *build_id)
252 {
253 	Elf32_Ehdr *ehdr;
254 	struct page *page;
255 	void *page_addr;
256 	int ret;
257 
258 	/* only works for page backed storage  */
259 	if (!vma->vm_file)
260 		return -EINVAL;
261 
262 	page = find_get_page(vma->vm_file->f_mapping, 0);
263 	if (!page)
264 		return -EFAULT;	/* page not mapped */
265 
266 	ret = -EINVAL;
267 	page_addr = kmap_atomic(page);
268 	ehdr = (Elf32_Ehdr *)page_addr;
269 
270 	/* compare magic x7f "ELF" */
271 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
272 		goto out;
273 
274 	/* only support executable file and shared object file */
275 	if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
276 		goto out;
277 
278 	if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
279 		ret = stack_map_get_build_id_32(page_addr, build_id);
280 	else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
281 		ret = stack_map_get_build_id_64(page_addr, build_id);
282 out:
283 	kunmap_atomic(page_addr);
284 	put_page(page);
285 	return ret;
286 }
287 
stack_map_get_build_id_offset(struct bpf_stack_build_id * id_offs,u64 * ips,u32 trace_nr,bool user)288 static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
289 					  u64 *ips, u32 trace_nr, bool user)
290 {
291 	int i;
292 	struct vm_area_struct *vma;
293 	bool irq_work_busy = false;
294 	struct stack_map_irq_work *work = NULL;
295 
296 	if (irqs_disabled()) {
297 		work = this_cpu_ptr(&up_read_work);
298 		if (work->irq_work.flags & IRQ_WORK_BUSY)
299 			/* cannot queue more up_read, fallback */
300 			irq_work_busy = true;
301 	}
302 
303 	/*
304 	 * We cannot do up_read() when the irq is disabled, because of
305 	 * risk to deadlock with rq_lock. To do build_id lookup when the
306 	 * irqs are disabled, we need to run up_read() in irq_work. We use
307 	 * a percpu variable to do the irq_work. If the irq_work is
308 	 * already used by another lookup, we fall back to report ips.
309 	 *
310 	 * Same fallback is used for kernel stack (!user) on a stackmap
311 	 * with build_id.
312 	 */
313 	if (!user || !current || !current->mm || irq_work_busy ||
314 	    down_read_trylock(&current->mm->mmap_sem) == 0) {
315 		/* cannot access current->mm, fall back to ips */
316 		for (i = 0; i < trace_nr; i++) {
317 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
318 			id_offs[i].ip = ips[i];
319 			memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE);
320 		}
321 		return;
322 	}
323 
324 	for (i = 0; i < trace_nr; i++) {
325 		vma = find_vma(current->mm, ips[i]);
326 		if (!vma || stack_map_get_build_id(vma, id_offs[i].build_id)) {
327 			/* per entry fall back to ips */
328 			id_offs[i].status = BPF_STACK_BUILD_ID_IP;
329 			id_offs[i].ip = ips[i];
330 			memset(id_offs[i].build_id, 0, BPF_BUILD_ID_SIZE);
331 			continue;
332 		}
333 		id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
334 			- vma->vm_start;
335 		id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
336 	}
337 
338 	if (!work) {
339 		up_read(&current->mm->mmap_sem);
340 	} else {
341 		work->sem = &current->mm->mmap_sem;
342 		irq_work_queue(&work->irq_work);
343 		/*
344 		 * The irq_work will release the mmap_sem with
345 		 * up_read_non_owner(). The rwsem_release() is called
346 		 * here to release the lock from lockdep's perspective.
347 		 */
348 		rwsem_release(&current->mm->mmap_sem.dep_map, 1, _RET_IP_);
349 	}
350 }
351 
BPF_CALL_3(bpf_get_stackid,struct pt_regs *,regs,struct bpf_map *,map,u64,flags)352 BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
353 	   u64, flags)
354 {
355 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
356 	struct perf_callchain_entry *trace;
357 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
358 	u32 max_depth = map->value_size / stack_map_data_size(map);
359 	/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
360 	u32 init_nr = sysctl_perf_event_max_stack - max_depth;
361 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
362 	u32 hash, id, trace_nr, trace_len;
363 	bool user = flags & BPF_F_USER_STACK;
364 	bool kernel = !user;
365 	u64 *ips;
366 	bool hash_matches;
367 
368 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
369 			       BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
370 		return -EINVAL;
371 
372 	trace = get_perf_callchain(regs, init_nr, kernel, user,
373 				   sysctl_perf_event_max_stack, false, false);
374 
375 	if (unlikely(!trace))
376 		/* couldn't fetch the stack trace */
377 		return -EFAULT;
378 
379 	/* get_perf_callchain() guarantees that trace->nr >= init_nr
380 	 * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
381 	 */
382 	trace_nr = trace->nr - init_nr;
383 
384 	if (trace_nr <= skip)
385 		/* skipping more than usable stack trace */
386 		return -EFAULT;
387 
388 	trace_nr -= skip;
389 	trace_len = trace_nr * sizeof(u64);
390 	ips = trace->ip + skip + init_nr;
391 	hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
392 	id = hash & (smap->n_buckets - 1);
393 	bucket = READ_ONCE(smap->buckets[id]);
394 
395 	hash_matches = bucket && bucket->hash == hash;
396 	/* fast cmp */
397 	if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
398 		return id;
399 
400 	if (stack_map_use_build_id(map)) {
401 		/* for build_id+offset, pop a bucket before slow cmp */
402 		new_bucket = (struct stack_map_bucket *)
403 			pcpu_freelist_pop(&smap->freelist);
404 		if (unlikely(!new_bucket))
405 			return -ENOMEM;
406 		new_bucket->nr = trace_nr;
407 		stack_map_get_build_id_offset(
408 			(struct bpf_stack_build_id *)new_bucket->data,
409 			ips, trace_nr, user);
410 		trace_len = trace_nr * sizeof(struct bpf_stack_build_id);
411 		if (hash_matches && bucket->nr == trace_nr &&
412 		    memcmp(bucket->data, new_bucket->data, trace_len) == 0) {
413 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
414 			return id;
415 		}
416 		if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
417 			pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
418 			return -EEXIST;
419 		}
420 	} else {
421 		if (hash_matches && bucket->nr == trace_nr &&
422 		    memcmp(bucket->data, ips, trace_len) == 0)
423 			return id;
424 		if (bucket && !(flags & BPF_F_REUSE_STACKID))
425 			return -EEXIST;
426 
427 		new_bucket = (struct stack_map_bucket *)
428 			pcpu_freelist_pop(&smap->freelist);
429 		if (unlikely(!new_bucket))
430 			return -ENOMEM;
431 		memcpy(new_bucket->data, ips, trace_len);
432 	}
433 
434 	new_bucket->hash = hash;
435 	new_bucket->nr = trace_nr;
436 
437 	old_bucket = xchg(&smap->buckets[id], new_bucket);
438 	if (old_bucket)
439 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
440 	return id;
441 }
442 
443 const struct bpf_func_proto bpf_get_stackid_proto = {
444 	.func		= bpf_get_stackid,
445 	.gpl_only	= true,
446 	.ret_type	= RET_INTEGER,
447 	.arg1_type	= ARG_PTR_TO_CTX,
448 	.arg2_type	= ARG_CONST_MAP_PTR,
449 	.arg3_type	= ARG_ANYTHING,
450 };
451 
BPF_CALL_4(bpf_get_stack,struct pt_regs *,regs,void *,buf,u32,size,u64,flags)452 BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
453 	   u64, flags)
454 {
455 	u32 init_nr, trace_nr, copy_len, elem_size, num_elem;
456 	bool user_build_id = flags & BPF_F_USER_BUILD_ID;
457 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
458 	bool user = flags & BPF_F_USER_STACK;
459 	struct perf_callchain_entry *trace;
460 	bool kernel = !user;
461 	int err = -EINVAL;
462 	u64 *ips;
463 
464 	if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
465 			       BPF_F_USER_BUILD_ID)))
466 		goto clear;
467 	if (kernel && user_build_id)
468 		goto clear;
469 
470 	elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
471 					    : sizeof(u64);
472 	if (unlikely(size % elem_size))
473 		goto clear;
474 
475 	num_elem = size / elem_size;
476 	if (sysctl_perf_event_max_stack < num_elem)
477 		init_nr = 0;
478 	else
479 		init_nr = sysctl_perf_event_max_stack - num_elem;
480 	trace = get_perf_callchain(regs, init_nr, kernel, user,
481 				   sysctl_perf_event_max_stack, false, false);
482 	if (unlikely(!trace))
483 		goto err_fault;
484 
485 	trace_nr = trace->nr - init_nr;
486 	if (trace_nr < skip)
487 		goto err_fault;
488 
489 	trace_nr -= skip;
490 	trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
491 	copy_len = trace_nr * elem_size;
492 	ips = trace->ip + skip + init_nr;
493 	if (user && user_build_id)
494 		stack_map_get_build_id_offset(buf, ips, trace_nr, user);
495 	else
496 		memcpy(buf, ips, copy_len);
497 
498 	if (size > copy_len)
499 		memset(buf + copy_len, 0, size - copy_len);
500 	return copy_len;
501 
502 err_fault:
503 	err = -EFAULT;
504 clear:
505 	memset(buf, 0, size);
506 	return err;
507 }
508 
509 const struct bpf_func_proto bpf_get_stack_proto = {
510 	.func		= bpf_get_stack,
511 	.gpl_only	= true,
512 	.ret_type	= RET_INTEGER,
513 	.arg1_type	= ARG_PTR_TO_CTX,
514 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
515 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
516 	.arg4_type	= ARG_ANYTHING,
517 };
518 
519 /* Called from eBPF program */
stack_map_lookup_elem(struct bpf_map * map,void * key)520 static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
521 {
522 	return ERR_PTR(-EOPNOTSUPP);
523 }
524 
525 /* Called from syscall */
bpf_stackmap_copy(struct bpf_map * map,void * key,void * value)526 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
527 {
528 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
529 	struct stack_map_bucket *bucket, *old_bucket;
530 	u32 id = *(u32 *)key, trace_len;
531 
532 	if (unlikely(id >= smap->n_buckets))
533 		return -ENOENT;
534 
535 	bucket = xchg(&smap->buckets[id], NULL);
536 	if (!bucket)
537 		return -ENOENT;
538 
539 	trace_len = bucket->nr * stack_map_data_size(map);
540 	memcpy(value, bucket->data, trace_len);
541 	memset(value + trace_len, 0, map->value_size - trace_len);
542 
543 	old_bucket = xchg(&smap->buckets[id], bucket);
544 	if (old_bucket)
545 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
546 	return 0;
547 }
548 
stack_map_get_next_key(struct bpf_map * map,void * key,void * next_key)549 static int stack_map_get_next_key(struct bpf_map *map, void *key,
550 				  void *next_key)
551 {
552 	struct bpf_stack_map *smap = container_of(map,
553 						  struct bpf_stack_map, map);
554 	u32 id;
555 
556 	WARN_ON_ONCE(!rcu_read_lock_held());
557 
558 	if (!key) {
559 		id = 0;
560 	} else {
561 		id = *(u32 *)key;
562 		if (id >= smap->n_buckets || !smap->buckets[id])
563 			id = 0;
564 		else
565 			id++;
566 	}
567 
568 	while (id < smap->n_buckets && !smap->buckets[id])
569 		id++;
570 
571 	if (id >= smap->n_buckets)
572 		return -ENOENT;
573 
574 	*(u32 *)next_key = id;
575 	return 0;
576 }
577 
stack_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)578 static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
579 				 u64 map_flags)
580 {
581 	return -EINVAL;
582 }
583 
584 /* Called from syscall or from eBPF program */
stack_map_delete_elem(struct bpf_map * map,void * key)585 static int stack_map_delete_elem(struct bpf_map *map, void *key)
586 {
587 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
588 	struct stack_map_bucket *old_bucket;
589 	u32 id = *(u32 *)key;
590 
591 	if (unlikely(id >= smap->n_buckets))
592 		return -E2BIG;
593 
594 	old_bucket = xchg(&smap->buckets[id], NULL);
595 	if (old_bucket) {
596 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
597 		return 0;
598 	} else {
599 		return -ENOENT;
600 	}
601 }
602 
603 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
stack_map_free(struct bpf_map * map)604 static void stack_map_free(struct bpf_map *map)
605 {
606 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
607 
608 	/* wait for bpf programs to complete before freeing stack map */
609 	synchronize_rcu();
610 
611 	bpf_map_area_free(smap->elems);
612 	pcpu_freelist_destroy(&smap->freelist);
613 	bpf_map_area_free(smap);
614 	put_callchain_buffers();
615 }
616 
617 const struct bpf_map_ops stack_trace_map_ops = {
618 	.map_alloc = stack_map_alloc,
619 	.map_free = stack_map_free,
620 	.map_get_next_key = stack_map_get_next_key,
621 	.map_lookup_elem = stack_map_lookup_elem,
622 	.map_update_elem = stack_map_update_elem,
623 	.map_delete_elem = stack_map_delete_elem,
624 	.map_check_btf = map_check_no_btf,
625 };
626 
stack_map_init(void)627 static int __init stack_map_init(void)
628 {
629 	int cpu;
630 	struct stack_map_irq_work *work;
631 
632 	for_each_possible_cpu(cpu) {
633 		work = per_cpu_ptr(&up_read_work, cpu);
634 		init_irq_work(&work->irq_work, do_up_read);
635 	}
636 	return 0;
637 }
638 subsys_initcall(stack_map_init);
639