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