1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <linux/slab.h>
9 #include <linux/bpf_perf_event.h>
10 #include <linux/filter.h>
11 #include <linux/uaccess.h>
12 #include <linux/ctype.h>
13 #include <linux/kprobes.h>
14 #include <linux/syscalls.h>
15 #include <linux/error-injection.h>
17 #include "trace_probe.h"
20 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
21 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
24 * trace_call_bpf - invoke BPF program
25 * @call: tracepoint event
26 * @ctx: opaque context pointer
28 * kprobe handlers execute BPF programs via this helper.
29 * Can be used from static tracepoints in the future.
31 * Return: BPF programs always return an integer which is interpreted by
33 * 0 - return from kprobe (event is filtered out)
34 * 1 - store kprobe event into ring buffer
35 * Other values are reserved and currently alias to 1
37 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
41 if (in_nmi()) /* not supported yet */
46 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
48 * since some bpf program is already running on this cpu,
49 * don't call into another bpf program (same or different)
50 * and don't send kprobe event into ring-buffer,
58 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
59 * to all call sites, we did a bpf_prog_array_valid() there to check
60 * whether call->prog_array is empty or not, which is
61 * a heurisitc to speed up execution.
63 * If bpf_prog_array_valid() fetched prog_array was
64 * non-NULL, we go into trace_call_bpf() and do the actual
65 * proper rcu_dereference() under RCU lock.
66 * If it turns out that prog_array is NULL then, we bail out.
67 * For the opposite, if the bpf_prog_array_valid() fetched pointer
68 * was NULL, you'll skip the prog_array with the risk of missing
69 * out of events when it was updated in between this and the
70 * rcu_dereference() which is accepted risk.
72 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
75 __this_cpu_dec(bpf_prog_active);
80 EXPORT_SYMBOL_GPL(trace_call_bpf);
82 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
83 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
85 regs_set_return_value(regs, rc);
86 override_function_with_return(regs);
90 static const struct bpf_func_proto bpf_override_return_proto = {
91 .func = bpf_override_return,
93 .ret_type = RET_INTEGER,
94 .arg1_type = ARG_PTR_TO_CTX,
95 .arg2_type = ARG_ANYTHING,
99 BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
103 ret = probe_kernel_read(dst, unsafe_ptr, size);
104 if (unlikely(ret < 0))
105 memset(dst, 0, size);
110 static const struct bpf_func_proto bpf_probe_read_proto = {
111 .func = bpf_probe_read,
113 .ret_type = RET_INTEGER,
114 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
115 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
116 .arg3_type = ARG_ANYTHING,
119 BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
123 * Ensure we're in user context which is safe for the helper to
124 * run. This helper has no business in a kthread.
126 * access_ok() should prevent writing to non-user memory, but in
127 * some situations (nommu, temporary switch, etc) access_ok() does
128 * not provide enough validation, hence the check on KERNEL_DS.
131 if (unlikely(in_interrupt() ||
132 current->flags & (PF_KTHREAD | PF_EXITING)))
134 if (unlikely(uaccess_kernel()))
136 if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
139 return probe_kernel_write(unsafe_ptr, src, size);
142 static const struct bpf_func_proto bpf_probe_write_user_proto = {
143 .func = bpf_probe_write_user,
145 .ret_type = RET_INTEGER,
146 .arg1_type = ARG_ANYTHING,
147 .arg2_type = ARG_PTR_TO_MEM,
148 .arg3_type = ARG_CONST_SIZE,
151 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
153 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
154 current->comm, task_pid_nr(current));
156 return &bpf_probe_write_user_proto;
160 * Only limited trace_printk() conversion specifiers allowed:
161 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
163 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
164 u64, arg2, u64, arg3)
166 bool str_seen = false;
174 * bpf_check()->check_func_arg()->check_stack_boundary()
175 * guarantees that fmt points to bpf program stack,
176 * fmt_size bytes of it were initialized and fmt_size > 0
178 if (fmt[--fmt_size] != 0)
181 /* check format string for allowed specifiers */
182 for (i = 0; i < fmt_size; i++) {
183 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
192 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
197 } else if (fmt[i] == 'p' || fmt[i] == 's') {
199 /* disallow any further format extensions */
200 if (fmt[i + 1] != 0 &&
201 !isspace(fmt[i + 1]) &&
202 !ispunct(fmt[i + 1]))
207 /* allow only one '%s' per fmt string */
226 strncpy_from_unsafe(buf,
227 (void *) (long) unsafe_addr,
238 if (fmt[i] != 'i' && fmt[i] != 'd' &&
239 fmt[i] != 'u' && fmt[i] != 'x')
244 /* Horrid workaround for getting va_list handling working with different
245 * argument type combinations generically for 32 and 64 bit archs.
247 #define __BPF_TP_EMIT() __BPF_ARG3_TP()
248 #define __BPF_TP(...) \
249 __trace_printk(0 /* Fake ip */, \
252 #define __BPF_ARG1_TP(...) \
253 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
254 ? __BPF_TP(arg1, ##__VA_ARGS__) \
255 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
256 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
257 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
259 #define __BPF_ARG2_TP(...) \
260 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
261 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
262 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
263 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
264 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
266 #define __BPF_ARG3_TP(...) \
267 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
268 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
269 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
270 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
271 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
273 return __BPF_TP_EMIT();
276 static const struct bpf_func_proto bpf_trace_printk_proto = {
277 .func = bpf_trace_printk,
279 .ret_type = RET_INTEGER,
280 .arg1_type = ARG_PTR_TO_MEM,
281 .arg2_type = ARG_CONST_SIZE,
284 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
287 * this program might be calling bpf_trace_printk,
288 * so allocate per-cpu printk buffers
290 trace_printk_init_buffers();
292 return &bpf_trace_printk_proto;
295 static __always_inline int
296 get_map_perf_counter(struct bpf_map *map, u64 flags,
297 u64 *value, u64 *enabled, u64 *running)
299 struct bpf_array *array = container_of(map, struct bpf_array, map);
300 unsigned int cpu = smp_processor_id();
301 u64 index = flags & BPF_F_INDEX_MASK;
302 struct bpf_event_entry *ee;
304 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
306 if (index == BPF_F_CURRENT_CPU)
308 if (unlikely(index >= array->map.max_entries))
311 ee = READ_ONCE(array->ptrs[index]);
315 return perf_event_read_local(ee->event, value, enabled, running);
318 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
323 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
325 * this api is ugly since we miss [-22..-2] range of valid
326 * counter values, but that's uapi
333 static const struct bpf_func_proto bpf_perf_event_read_proto = {
334 .func = bpf_perf_event_read,
336 .ret_type = RET_INTEGER,
337 .arg1_type = ARG_CONST_MAP_PTR,
338 .arg2_type = ARG_ANYTHING,
341 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
342 struct bpf_perf_event_value *, buf, u32, size)
346 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
348 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
354 memset(buf, 0, size);
358 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
359 .func = bpf_perf_event_read_value,
361 .ret_type = RET_INTEGER,
362 .arg1_type = ARG_CONST_MAP_PTR,
363 .arg2_type = ARG_ANYTHING,
364 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
365 .arg4_type = ARG_CONST_SIZE,
368 static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
370 static __always_inline u64
371 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
372 u64 flags, struct perf_sample_data *sd)
374 struct bpf_array *array = container_of(map, struct bpf_array, map);
375 unsigned int cpu = smp_processor_id();
376 u64 index = flags & BPF_F_INDEX_MASK;
377 struct bpf_event_entry *ee;
378 struct perf_event *event;
380 if (index == BPF_F_CURRENT_CPU)
382 if (unlikely(index >= array->map.max_entries))
385 ee = READ_ONCE(array->ptrs[index]);
390 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
391 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
394 if (unlikely(event->oncpu != cpu))
397 perf_event_output(event, sd, regs);
401 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
402 u64, flags, void *, data, u64, size)
404 struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
405 struct perf_raw_record raw = {
412 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
415 perf_sample_data_init(sd, 0, 0);
418 return __bpf_perf_event_output(regs, map, flags, sd);
421 static const struct bpf_func_proto bpf_perf_event_output_proto = {
422 .func = bpf_perf_event_output,
424 .ret_type = RET_INTEGER,
425 .arg1_type = ARG_PTR_TO_CTX,
426 .arg2_type = ARG_CONST_MAP_PTR,
427 .arg3_type = ARG_ANYTHING,
428 .arg4_type = ARG_PTR_TO_MEM,
429 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
432 static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
433 static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
435 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
436 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
438 struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
439 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
440 struct perf_raw_frag frag = {
445 struct perf_raw_record raw = {
448 .next = ctx_size ? &frag : NULL,
455 perf_fetch_caller_regs(regs);
456 perf_sample_data_init(sd, 0, 0);
459 return __bpf_perf_event_output(regs, map, flags, sd);
462 BPF_CALL_0(bpf_get_current_task)
464 return (long) current;
467 static const struct bpf_func_proto bpf_get_current_task_proto = {
468 .func = bpf_get_current_task,
470 .ret_type = RET_INTEGER,
473 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
475 struct bpf_array *array = container_of(map, struct bpf_array, map);
478 if (unlikely(idx >= array->map.max_entries))
481 cgrp = READ_ONCE(array->ptrs[idx]);
485 return task_under_cgroup_hierarchy(current, cgrp);
488 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
489 .func = bpf_current_task_under_cgroup,
491 .ret_type = RET_INTEGER,
492 .arg1_type = ARG_CONST_MAP_PTR,
493 .arg2_type = ARG_ANYTHING,
496 BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
497 const void *, unsafe_ptr)
502 * The strncpy_from_unsafe() call will likely not fill the entire
503 * buffer, but that's okay in this circumstance as we're probing
504 * arbitrary memory anyway similar to bpf_probe_read() and might
505 * as well probe the stack. Thus, memory is explicitly cleared
506 * only in error case, so that improper users ignoring return
507 * code altogether don't copy garbage; otherwise length of string
508 * is returned that can be used for bpf_perf_event_output() et al.
510 ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
511 if (unlikely(ret < 0))
512 memset(dst, 0, size);
517 static const struct bpf_func_proto bpf_probe_read_str_proto = {
518 .func = bpf_probe_read_str,
520 .ret_type = RET_INTEGER,
521 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
522 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
523 .arg3_type = ARG_ANYTHING,
526 static const struct bpf_func_proto *
527 tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
530 case BPF_FUNC_map_lookup_elem:
531 return &bpf_map_lookup_elem_proto;
532 case BPF_FUNC_map_update_elem:
533 return &bpf_map_update_elem_proto;
534 case BPF_FUNC_map_delete_elem:
535 return &bpf_map_delete_elem_proto;
536 case BPF_FUNC_probe_read:
537 return &bpf_probe_read_proto;
538 case BPF_FUNC_ktime_get_ns:
539 return &bpf_ktime_get_ns_proto;
540 case BPF_FUNC_tail_call:
541 return &bpf_tail_call_proto;
542 case BPF_FUNC_get_current_pid_tgid:
543 return &bpf_get_current_pid_tgid_proto;
544 case BPF_FUNC_get_current_task:
545 return &bpf_get_current_task_proto;
546 case BPF_FUNC_get_current_uid_gid:
547 return &bpf_get_current_uid_gid_proto;
548 case BPF_FUNC_get_current_comm:
549 return &bpf_get_current_comm_proto;
550 case BPF_FUNC_trace_printk:
551 return bpf_get_trace_printk_proto();
552 case BPF_FUNC_get_smp_processor_id:
553 return &bpf_get_smp_processor_id_proto;
554 case BPF_FUNC_get_numa_node_id:
555 return &bpf_get_numa_node_id_proto;
556 case BPF_FUNC_perf_event_read:
557 return &bpf_perf_event_read_proto;
558 case BPF_FUNC_probe_write_user:
559 return bpf_get_probe_write_proto();
560 case BPF_FUNC_current_task_under_cgroup:
561 return &bpf_current_task_under_cgroup_proto;
562 case BPF_FUNC_get_prandom_u32:
563 return &bpf_get_prandom_u32_proto;
564 case BPF_FUNC_probe_read_str:
565 return &bpf_probe_read_str_proto;
566 #ifdef CONFIG_CGROUPS
567 case BPF_FUNC_get_current_cgroup_id:
568 return &bpf_get_current_cgroup_id_proto;
575 static const struct bpf_func_proto *
576 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
579 case BPF_FUNC_perf_event_output:
580 return &bpf_perf_event_output_proto;
581 case BPF_FUNC_get_stackid:
582 return &bpf_get_stackid_proto;
583 case BPF_FUNC_get_stack:
584 return &bpf_get_stack_proto;
585 case BPF_FUNC_perf_event_read_value:
586 return &bpf_perf_event_read_value_proto;
587 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
588 case BPF_FUNC_override_return:
589 return &bpf_override_return_proto;
592 return tracing_func_proto(func_id, prog);
596 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
597 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
598 const struct bpf_prog *prog,
599 struct bpf_insn_access_aux *info)
601 if (off < 0 || off >= sizeof(struct pt_regs))
603 if (type != BPF_READ)
608 * Assertion for 32 bit to make sure last 8 byte access
609 * (BPF_DW) to the last 4 byte member is disallowed.
611 if (off + size > sizeof(struct pt_regs))
617 const struct bpf_verifier_ops kprobe_verifier_ops = {
618 .get_func_proto = kprobe_prog_func_proto,
619 .is_valid_access = kprobe_prog_is_valid_access,
622 const struct bpf_prog_ops kprobe_prog_ops = {
625 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
626 u64, flags, void *, data, u64, size)
628 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
631 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
632 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
633 * from there and call the same bpf_perf_event_output() helper inline.
635 return ____bpf_perf_event_output(regs, map, flags, data, size);
638 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
639 .func = bpf_perf_event_output_tp,
641 .ret_type = RET_INTEGER,
642 .arg1_type = ARG_PTR_TO_CTX,
643 .arg2_type = ARG_CONST_MAP_PTR,
644 .arg3_type = ARG_ANYTHING,
645 .arg4_type = ARG_PTR_TO_MEM,
646 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
649 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
652 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
655 * Same comment as in bpf_perf_event_output_tp(), only that this time
656 * the other helper's function body cannot be inlined due to being
657 * external, thus we need to call raw helper function.
659 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
663 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
664 .func = bpf_get_stackid_tp,
666 .ret_type = RET_INTEGER,
667 .arg1_type = ARG_PTR_TO_CTX,
668 .arg2_type = ARG_CONST_MAP_PTR,
669 .arg3_type = ARG_ANYTHING,
672 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
675 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
677 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
678 (unsigned long) size, flags, 0);
681 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
682 .func = bpf_get_stack_tp,
684 .ret_type = RET_INTEGER,
685 .arg1_type = ARG_PTR_TO_CTX,
686 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
687 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
688 .arg4_type = ARG_ANYTHING,
691 static const struct bpf_func_proto *
692 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
695 case BPF_FUNC_perf_event_output:
696 return &bpf_perf_event_output_proto_tp;
697 case BPF_FUNC_get_stackid:
698 return &bpf_get_stackid_proto_tp;
699 case BPF_FUNC_get_stack:
700 return &bpf_get_stack_proto_tp;
702 return tracing_func_proto(func_id, prog);
706 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
707 const struct bpf_prog *prog,
708 struct bpf_insn_access_aux *info)
710 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
712 if (type != BPF_READ)
717 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
721 const struct bpf_verifier_ops tracepoint_verifier_ops = {
722 .get_func_proto = tp_prog_func_proto,
723 .is_valid_access = tp_prog_is_valid_access,
726 const struct bpf_prog_ops tracepoint_prog_ops = {
729 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
730 struct bpf_perf_event_value *, buf, u32, size)
734 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
736 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
742 memset(buf, 0, size);
746 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
747 .func = bpf_perf_prog_read_value,
749 .ret_type = RET_INTEGER,
750 .arg1_type = ARG_PTR_TO_CTX,
751 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
752 .arg3_type = ARG_CONST_SIZE,
755 static const struct bpf_func_proto *
756 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
759 case BPF_FUNC_perf_event_output:
760 return &bpf_perf_event_output_proto_tp;
761 case BPF_FUNC_get_stackid:
762 return &bpf_get_stackid_proto_tp;
763 case BPF_FUNC_get_stack:
764 return &bpf_get_stack_proto_tp;
765 case BPF_FUNC_perf_prog_read_value:
766 return &bpf_perf_prog_read_value_proto;
768 return tracing_func_proto(func_id, prog);
773 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
774 * to avoid potential recursive reuse issue when/if tracepoints are added
775 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
777 static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
778 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
779 struct bpf_map *, map, u64, flags, void *, data, u64, size)
781 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
783 perf_fetch_caller_regs(regs);
784 return ____bpf_perf_event_output(regs, map, flags, data, size);
787 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
788 .func = bpf_perf_event_output_raw_tp,
790 .ret_type = RET_INTEGER,
791 .arg1_type = ARG_PTR_TO_CTX,
792 .arg2_type = ARG_CONST_MAP_PTR,
793 .arg3_type = ARG_ANYTHING,
794 .arg4_type = ARG_PTR_TO_MEM,
795 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
798 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
799 struct bpf_map *, map, u64, flags)
801 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
803 perf_fetch_caller_regs(regs);
804 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
805 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
809 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
810 .func = bpf_get_stackid_raw_tp,
812 .ret_type = RET_INTEGER,
813 .arg1_type = ARG_PTR_TO_CTX,
814 .arg2_type = ARG_CONST_MAP_PTR,
815 .arg3_type = ARG_ANYTHING,
818 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
819 void *, buf, u32, size, u64, flags)
821 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
823 perf_fetch_caller_regs(regs);
824 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
825 (unsigned long) size, flags, 0);
828 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
829 .func = bpf_get_stack_raw_tp,
831 .ret_type = RET_INTEGER,
832 .arg1_type = ARG_PTR_TO_CTX,
833 .arg2_type = ARG_PTR_TO_MEM,
834 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
835 .arg4_type = ARG_ANYTHING,
838 static const struct bpf_func_proto *
839 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
842 case BPF_FUNC_perf_event_output:
843 return &bpf_perf_event_output_proto_raw_tp;
844 case BPF_FUNC_get_stackid:
845 return &bpf_get_stackid_proto_raw_tp;
846 case BPF_FUNC_get_stack:
847 return &bpf_get_stack_proto_raw_tp;
849 return tracing_func_proto(func_id, prog);
853 static bool raw_tp_prog_is_valid_access(int off, int size,
854 enum bpf_access_type type,
855 const struct bpf_prog *prog,
856 struct bpf_insn_access_aux *info)
858 /* largest tracepoint in the kernel has 12 args */
859 if (off < 0 || off >= sizeof(__u64) * 12)
861 if (type != BPF_READ)
868 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
869 .get_func_proto = raw_tp_prog_func_proto,
870 .is_valid_access = raw_tp_prog_is_valid_access,
873 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
876 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
877 const struct bpf_prog *prog,
878 struct bpf_insn_access_aux *info)
880 const int size_u64 = sizeof(u64);
882 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
884 if (type != BPF_READ)
886 if (off % size != 0) {
887 if (sizeof(unsigned long) != 4)
896 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
897 bpf_ctx_record_field_size(info, size_u64);
898 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
901 case bpf_ctx_range(struct bpf_perf_event_data, addr):
902 bpf_ctx_record_field_size(info, size_u64);
903 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
907 if (size != sizeof(long))
914 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
915 const struct bpf_insn *si,
916 struct bpf_insn *insn_buf,
917 struct bpf_prog *prog, u32 *target_size)
919 struct bpf_insn *insn = insn_buf;
922 case offsetof(struct bpf_perf_event_data, sample_period):
923 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
924 data), si->dst_reg, si->src_reg,
925 offsetof(struct bpf_perf_event_data_kern, data));
926 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
927 bpf_target_off(struct perf_sample_data, period, 8,
930 case offsetof(struct bpf_perf_event_data, addr):
931 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
932 data), si->dst_reg, si->src_reg,
933 offsetof(struct bpf_perf_event_data_kern, data));
934 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
935 bpf_target_off(struct perf_sample_data, addr, 8,
939 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
940 regs), si->dst_reg, si->src_reg,
941 offsetof(struct bpf_perf_event_data_kern, regs));
942 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
947 return insn - insn_buf;
950 const struct bpf_verifier_ops perf_event_verifier_ops = {
951 .get_func_proto = pe_prog_func_proto,
952 .is_valid_access = pe_prog_is_valid_access,
953 .convert_ctx_access = pe_prog_convert_ctx_access,
956 const struct bpf_prog_ops perf_event_prog_ops = {
959 static DEFINE_MUTEX(bpf_event_mutex);
961 #define BPF_TRACE_MAX_PROGS 64
963 int perf_event_attach_bpf_prog(struct perf_event *event,
964 struct bpf_prog *prog)
966 struct bpf_prog_array __rcu *old_array;
967 struct bpf_prog_array *new_array;
971 * Kprobe override only works if they are on the function entry,
972 * and only if they are on the opt-in list.
974 if (prog->kprobe_override &&
975 (!trace_kprobe_on_func_entry(event->tp_event) ||
976 !trace_kprobe_error_injectable(event->tp_event)))
979 mutex_lock(&bpf_event_mutex);
984 old_array = event->tp_event->prog_array;
986 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
991 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
995 /* set the new array to event->tp_event and set event->prog */
997 rcu_assign_pointer(event->tp_event->prog_array, new_array);
998 bpf_prog_array_free(old_array);
1001 mutex_unlock(&bpf_event_mutex);
1005 void perf_event_detach_bpf_prog(struct perf_event *event)
1007 struct bpf_prog_array __rcu *old_array;
1008 struct bpf_prog_array *new_array;
1011 mutex_lock(&bpf_event_mutex);
1016 old_array = event->tp_event->prog_array;
1017 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
1021 bpf_prog_array_delete_safe(old_array, event->prog);
1023 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1024 bpf_prog_array_free(old_array);
1027 bpf_prog_put(event->prog);
1031 mutex_unlock(&bpf_event_mutex);
1034 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
1036 struct perf_event_query_bpf __user *uquery = info;
1037 struct perf_event_query_bpf query = {};
1038 u32 *ids, prog_cnt, ids_len;
1041 if (!capable(CAP_SYS_ADMIN))
1043 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1045 if (copy_from_user(&query, uquery, sizeof(query)))
1048 ids_len = query.ids_len;
1049 if (ids_len > BPF_TRACE_MAX_PROGS)
1051 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1055 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1056 * is required when user only wants to check for uquery->prog_cnt.
1057 * There is no need to check for it since the case is handled
1058 * gracefully in bpf_prog_array_copy_info.
1061 mutex_lock(&bpf_event_mutex);
1062 ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
1066 mutex_unlock(&bpf_event_mutex);
1068 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1069 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1076 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1077 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1079 struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
1081 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1083 for (; btp < __stop__bpf_raw_tp; btp++) {
1084 if (!strcmp(btp->tp->name, name))
1090 static __always_inline
1091 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1095 (void) BPF_PROG_RUN(prog, args);
1100 #define UNPACK(...) __VA_ARGS__
1101 #define REPEAT_1(FN, DL, X, ...) FN(X)
1102 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1103 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1104 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1105 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1106 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1107 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1108 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1109 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1110 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1111 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1112 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1113 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1115 #define SARG(X) u64 arg##X
1116 #define COPY(X) args[X] = arg##X
1118 #define __DL_COM (,)
1119 #define __DL_SEM (;)
1121 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1123 #define BPF_TRACE_DEFN_x(x) \
1124 void bpf_trace_run##x(struct bpf_prog *prog, \
1125 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1128 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1129 __bpf_trace_run(prog, args); \
1131 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1132 BPF_TRACE_DEFN_x(1);
1133 BPF_TRACE_DEFN_x(2);
1134 BPF_TRACE_DEFN_x(3);
1135 BPF_TRACE_DEFN_x(4);
1136 BPF_TRACE_DEFN_x(5);
1137 BPF_TRACE_DEFN_x(6);
1138 BPF_TRACE_DEFN_x(7);
1139 BPF_TRACE_DEFN_x(8);
1140 BPF_TRACE_DEFN_x(9);
1141 BPF_TRACE_DEFN_x(10);
1142 BPF_TRACE_DEFN_x(11);
1143 BPF_TRACE_DEFN_x(12);
1145 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1147 struct tracepoint *tp = btp->tp;
1150 * check that program doesn't access arguments beyond what's
1151 * available in this tracepoint
1153 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1156 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1159 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1163 mutex_lock(&bpf_event_mutex);
1164 err = __bpf_probe_register(btp, prog);
1165 mutex_unlock(&bpf_event_mutex);
1169 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1173 mutex_lock(&bpf_event_mutex);
1174 err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
1175 mutex_unlock(&bpf_event_mutex);
1179 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1180 u32 *fd_type, const char **buf,
1181 u64 *probe_offset, u64 *probe_addr)
1183 bool is_tracepoint, is_syscall_tp;
1184 struct bpf_prog *prog;
1191 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1192 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1195 *prog_id = prog->aux->id;
1196 flags = event->tp_event->flags;
1197 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1198 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1200 if (is_tracepoint || is_syscall_tp) {
1201 *buf = is_tracepoint ? event->tp_event->tp->name
1202 : event->tp_event->name;
1203 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1204 *probe_offset = 0x0;
1209 #ifdef CONFIG_KPROBE_EVENTS
1210 if (flags & TRACE_EVENT_FL_KPROBE)
1211 err = bpf_get_kprobe_info(event, fd_type, buf,
1212 probe_offset, probe_addr,
1213 event->attr.type == PERF_TYPE_TRACEPOINT);
1215 #ifdef CONFIG_UPROBE_EVENTS
1216 if (flags & TRACE_EVENT_FL_UPROBE)
1217 err = bpf_get_uprobe_info(event, fd_type, buf,
1219 event->attr.type == PERF_TYPE_TRACEPOINT);