1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
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"
15 #define STACK_CREATE_FLAG_MASK \
16 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY | \
19 struct stack_map_bucket {
20 struct pcpu_freelist_node fnode;
26 struct bpf_stack_map {
29 struct pcpu_freelist freelist;
31 struct stack_map_bucket *buckets[];
34 static inline bool stack_map_use_build_id(struct bpf_map *map)
36 return (map->map_flags & BPF_F_STACK_BUILD_ID);
39 static inline int stack_map_data_size(struct bpf_map *map)
41 return stack_map_use_build_id(map) ?
42 sizeof(struct bpf_stack_build_id) : sizeof(u64);
45 static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
47 u64 elem_size = sizeof(struct stack_map_bucket) +
48 (u64)smap->map.value_size;
51 smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
56 err = pcpu_freelist_init(&smap->freelist);
60 pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
61 smap->map.max_entries);
65 bpf_map_area_free(smap->elems);
69 /* Called from syscall */
70 static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
72 u32 value_size = attr->value_size;
73 struct bpf_stack_map *smap;
78 return ERR_PTR(-EPERM);
80 if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
81 return ERR_PTR(-EINVAL);
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);
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);
97 /* hash table size must be power of 2 */
98 n_buckets = roundup_pow_of_two(attr->max_entries);
100 return ERR_PTR(-E2BIG);
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));
106 return ERR_PTR(-ENOMEM);
108 bpf_map_init_from_attr(&smap->map, attr);
109 smap->map.value_size = value_size;
110 smap->n_buckets = n_buckets;
112 err = get_callchain_buffers(sysctl_perf_event_max_stack);
116 err = prealloc_elems_and_freelist(smap);
123 put_callchain_buffers();
125 bpf_map_area_free(smap);
129 static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
130 u64 *ips, u32 trace_nr, bool user)
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;
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
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);
153 for (i = 0; i < trace_nr; i++) {
154 if (range_in_vma(prev_vma, ips[i], ips[i])) {
156 memcpy(id_offs[i].build_id, prev_build_id,
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);
169 id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
171 id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
173 prev_build_id = id_offs[i].build_id;
175 bpf_mmap_unlock_mm(work, current->mm);
178 static struct perf_callchain_entry *
179 get_callchain_entry_for_task(struct task_struct *task, u32 max_depth)
181 #ifdef CONFIG_STACKTRACE
182 struct perf_callchain_entry *entry;
185 entry = get_callchain_entry(&rctx);
190 entry->nr = stack_trace_save_tsk(task, (unsigned long *)entry->ip,
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.
197 if (__BITS_PER_LONG != 64) {
198 unsigned long *from = (unsigned long *) entry->ip;
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]);
207 put_callchain_entry(rctx);
210 #else /* CONFIG_STACKTRACE */
215 static long __bpf_get_stackid(struct bpf_map *map,
216 struct perf_callchain_entry *trace, u64 flags)
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;
226 if (trace->nr <= skip)
227 /* skipping more than usable stack trace */
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]);
237 hash_matches = bucket && bucket->hash == hash;
239 if (hash_matches && flags & BPF_F_FAST_STACK_CMP)
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))
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);
258 if (bucket && !(flags & BPF_F_REUSE_STACKID)) {
259 pcpu_freelist_push(&smap->freelist, &new_bucket->fnode);
263 if (hash_matches && bucket->nr == trace_nr &&
264 memcmp(bucket->data, ips, trace_len) == 0)
266 if (bucket && !(flags & BPF_F_REUSE_STACKID))
269 new_bucket = (struct stack_map_bucket *)
270 pcpu_freelist_pop(&smap->freelist);
271 if (unlikely(!new_bucket))
273 memcpy(new_bucket->data, ips, trace_len);
276 new_bucket->hash = hash;
277 new_bucket->nr = trace_nr;
279 old_bucket = xchg(&smap->buckets[id], new_bucket);
281 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
285 BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
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;
294 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
295 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
299 if (max_depth > sysctl_perf_event_max_stack)
300 max_depth = sysctl_perf_event_max_stack;
302 trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
305 if (unlikely(!trace))
306 /* couldn't fetch the stack trace */
309 return __bpf_get_stackid(map, trace, flags);
312 const struct bpf_func_proto bpf_get_stackid_proto = {
313 .func = bpf_get_stackid,
315 .ret_type = RET_INTEGER,
316 .arg1_type = ARG_PTR_TO_CTX,
317 .arg2_type = ARG_CONST_MAP_PTR,
318 .arg3_type = ARG_ANYTHING,
321 static __u64 count_kernel_ip(struct perf_callchain_entry *trace)
325 while (nr_kernel < trace->nr) {
326 if (trace->ip[nr_kernel] == PERF_CONTEXT_USER)
333 BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
334 struct bpf_map *, map, u64, flags)
336 struct perf_event *event = ctx->event;
337 struct perf_callchain_entry *trace;
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);
347 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
348 BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
351 user = flags & BPF_F_USER_STACK;
354 trace = ctx->data->callchain;
355 if (unlikely(!trace))
358 nr_kernel = count_kernel_ip(trace);
361 __u64 nr = trace->nr;
363 trace->nr = nr_kernel;
364 ret = __bpf_get_stackid(map, trace, flags);
369 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
372 if (skip > BPF_F_SKIP_FIELD_MASK)
375 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
376 ret = __bpf_get_stackid(map, trace, flags);
381 const struct bpf_func_proto bpf_get_stackid_proto_pe = {
382 .func = bpf_get_stackid_pe,
384 .ret_type = RET_INTEGER,
385 .arg1_type = ARG_PTR_TO_CTX,
386 .arg2_type = ARG_CONST_MAP_PTR,
387 .arg3_type = ARG_ANYTHING,
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)
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;
403 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
404 BPF_F_USER_BUILD_ID)))
406 if (kernel && user_build_id)
409 elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id)
411 if (unlikely(size % elem_size))
414 /* cannot get valid user stack for task without user_mode regs */
415 if (task && user && !user_mode(regs))
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;
425 else if (kernel && task)
426 trace = get_callchain_entry_for_task(task, max_depth);
428 trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
430 if (unlikely(!trace))
433 if (trace->nr < skip)
436 trace_nr = trace->nr - skip;
437 trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
438 copy_len = trace_nr * elem_size;
440 ips = trace->ip + skip;
441 if (user && user_build_id)
442 stack_map_get_build_id_offset(buf, ips, trace_nr, user);
444 memcpy(buf, ips, copy_len);
447 memset(buf + copy_len, 0, size - copy_len);
453 memset(buf, 0, size);
457 BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size,
460 return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
463 const struct bpf_func_proto bpf_get_stack_proto = {
464 .func = bpf_get_stack,
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,
473 BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf,
474 u32, size, u64, flags)
476 struct pt_regs *regs;
479 if (!try_get_task_stack(task))
482 regs = task_pt_regs(task);
484 res = __bpf_get_stack(regs, task, NULL, buf, size, flags);
485 put_task_stack(task);
490 const struct bpf_func_proto bpf_get_task_stack_proto = {
491 .func = bpf_get_task_stack,
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,
501 BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
502 void *, buf, u32, size, u64, flags)
504 struct pt_regs *regs = (struct pt_regs *)(ctx->regs);
505 struct perf_event *event = ctx->event;
506 struct perf_callchain_entry *trace;
511 if (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
512 return __bpf_get_stack(regs, NULL, NULL, buf, size, flags);
514 if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
515 BPF_F_USER_BUILD_ID)))
518 user = flags & BPF_F_USER_STACK;
522 trace = ctx->data->callchain;
523 if (unlikely(!trace))
526 nr_kernel = count_kernel_ip(trace);
529 __u64 nr = trace->nr;
531 trace->nr = nr_kernel;
532 err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
537 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
540 if (skip > BPF_F_SKIP_FIELD_MASK)
543 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
544 err = __bpf_get_stack(regs, NULL, trace, buf, size, flags);
549 memset(buf, 0, size);
554 const struct bpf_func_proto bpf_get_stack_proto_pe = {
555 .func = bpf_get_stack_pe,
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,
564 /* Called from eBPF program */
565 static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
567 return ERR_PTR(-EOPNOTSUPP);
570 /* Called from syscall */
571 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
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;
577 if (unlikely(id >= smap->n_buckets))
580 bucket = xchg(&smap->buckets[id], NULL);
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);
588 old_bucket = xchg(&smap->buckets[id], bucket);
590 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
594 static int stack_map_get_next_key(struct bpf_map *map, void *key,
597 struct bpf_stack_map *smap = container_of(map,
598 struct bpf_stack_map, map);
601 WARN_ON_ONCE(!rcu_read_lock_held());
607 if (id >= smap->n_buckets || !smap->buckets[id])
613 while (id < smap->n_buckets && !smap->buckets[id])
616 if (id >= smap->n_buckets)
619 *(u32 *)next_key = id;
623 static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
629 /* Called from syscall or from eBPF program */
630 static int stack_map_delete_elem(struct bpf_map *map, void *key)
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;
636 if (unlikely(id >= smap->n_buckets))
639 old_bucket = xchg(&smap->buckets[id], NULL);
641 pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
648 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
649 static void stack_map_free(struct bpf_map *map)
651 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
653 bpf_map_area_free(smap->elems);
654 pcpu_freelist_destroy(&smap->freelist);
655 bpf_map_area_free(smap);
656 put_callchain_buffers();
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,