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/btf.h>
11 #include <linux/filter.h>
12 #include <linux/uaccess.h>
13 #include <linux/ctype.h>
14 #include <linux/kprobes.h>
15 #include <linux/spinlock.h>
16 #include <linux/syscalls.h>
17 #include <linux/error-injection.h>
18 #include <linux/btf_ids.h>
20 #include <uapi/linux/bpf.h>
21 #include <uapi/linux/btf.h>
25 #include "trace_probe.h"
28 #define CREATE_TRACE_POINTS
29 #include "bpf_trace.h"
31 #define bpf_event_rcu_dereference(p) \
32 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
35 struct bpf_trace_module {
36 struct module *module;
37 struct list_head list;
40 static LIST_HEAD(bpf_trace_modules);
41 static DEFINE_MUTEX(bpf_module_mutex);
43 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
45 struct bpf_raw_event_map *btp, *ret = NULL;
46 struct bpf_trace_module *btm;
49 mutex_lock(&bpf_module_mutex);
50 list_for_each_entry(btm, &bpf_trace_modules, list) {
51 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
52 btp = &btm->module->bpf_raw_events[i];
53 if (!strcmp(btp->tp->name, name)) {
54 if (try_module_get(btm->module))
61 mutex_unlock(&bpf_module_mutex);
65 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
69 #endif /* CONFIG_MODULES */
71 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
72 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
74 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
75 u64 flags, const struct btf **btf,
79 * trace_call_bpf - invoke BPF program
80 * @call: tracepoint event
81 * @ctx: opaque context pointer
83 * kprobe handlers execute BPF programs via this helper.
84 * Can be used from static tracepoints in the future.
86 * Return: BPF programs always return an integer which is interpreted by
88 * 0 - return from kprobe (event is filtered out)
89 * 1 - store kprobe event into ring buffer
90 * Other values are reserved and currently alias to 1
92 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
98 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
100 * since some bpf program is already running on this cpu,
101 * don't call into another bpf program (same or different)
102 * and don't send kprobe event into ring-buffer,
103 * so return zero here
110 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
111 * to all call sites, we did a bpf_prog_array_valid() there to check
112 * whether call->prog_array is empty or not, which is
113 * a heurisitc to speed up execution.
115 * If bpf_prog_array_valid() fetched prog_array was
116 * non-NULL, we go into trace_call_bpf() and do the actual
117 * proper rcu_dereference() under RCU lock.
118 * If it turns out that prog_array is NULL then, we bail out.
119 * For the opposite, if the bpf_prog_array_valid() fetched pointer
120 * was NULL, you'll skip the prog_array with the risk of missing
121 * out of events when it was updated in between this and the
122 * rcu_dereference() which is accepted risk.
124 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
127 __this_cpu_dec(bpf_prog_active);
132 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
133 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
135 regs_set_return_value(regs, rc);
136 override_function_with_return(regs);
140 static const struct bpf_func_proto bpf_override_return_proto = {
141 .func = bpf_override_return,
143 .ret_type = RET_INTEGER,
144 .arg1_type = ARG_PTR_TO_CTX,
145 .arg2_type = ARG_ANYTHING,
149 static __always_inline int
150 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
154 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
155 if (unlikely(ret < 0))
156 memset(dst, 0, size);
160 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
161 const void __user *, unsafe_ptr)
163 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
166 const struct bpf_func_proto bpf_probe_read_user_proto = {
167 .func = bpf_probe_read_user,
169 .ret_type = RET_INTEGER,
170 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
171 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
172 .arg3_type = ARG_ANYTHING,
175 static __always_inline int
176 bpf_probe_read_user_str_common(void *dst, u32 size,
177 const void __user *unsafe_ptr)
182 * NB: We rely on strncpy_from_user() not copying junk past the NUL
183 * terminator into `dst`.
185 * strncpy_from_user() does long-sized strides in the fast path. If the
186 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
187 * then there could be junk after the NUL in `dst`. If user takes `dst`
188 * and keys a hash map with it, then semantically identical strings can
189 * occupy multiple entries in the map.
191 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
192 if (unlikely(ret < 0))
193 memset(dst, 0, size);
197 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
198 const void __user *, unsafe_ptr)
200 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
203 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
204 .func = bpf_probe_read_user_str,
206 .ret_type = RET_INTEGER,
207 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
208 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
209 .arg3_type = ARG_ANYTHING,
212 static __always_inline int
213 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
217 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
218 if (unlikely(ret < 0))
219 memset(dst, 0, size);
223 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
224 const void *, unsafe_ptr)
226 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
229 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
230 .func = bpf_probe_read_kernel,
232 .ret_type = RET_INTEGER,
233 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
234 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
235 .arg3_type = ARG_ANYTHING,
238 static __always_inline int
239 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
244 * The strncpy_from_kernel_nofault() call will likely not fill the
245 * entire buffer, but that's okay in this circumstance as we're probing
246 * arbitrary memory anyway similar to bpf_probe_read_*() and might
247 * as well probe the stack. Thus, memory is explicitly cleared
248 * only in error case, so that improper users ignoring return
249 * code altogether don't copy garbage; otherwise length of string
250 * is returned that can be used for bpf_perf_event_output() et al.
252 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
253 if (unlikely(ret < 0))
254 memset(dst, 0, size);
258 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
259 const void *, unsafe_ptr)
261 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
264 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
265 .func = bpf_probe_read_kernel_str,
267 .ret_type = RET_INTEGER,
268 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
269 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
270 .arg3_type = ARG_ANYTHING,
273 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
274 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
275 const void *, unsafe_ptr)
277 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
278 return bpf_probe_read_user_common(dst, size,
279 (__force void __user *)unsafe_ptr);
281 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
284 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
285 .func = bpf_probe_read_compat,
287 .ret_type = RET_INTEGER,
288 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
289 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
290 .arg3_type = ARG_ANYTHING,
293 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
294 const void *, unsafe_ptr)
296 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
297 return bpf_probe_read_user_str_common(dst, size,
298 (__force void __user *)unsafe_ptr);
300 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
303 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
304 .func = bpf_probe_read_compat_str,
306 .ret_type = RET_INTEGER,
307 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
308 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
309 .arg3_type = ARG_ANYTHING,
311 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
313 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
317 * Ensure we're in user context which is safe for the helper to
318 * run. This helper has no business in a kthread.
320 * access_ok() should prevent writing to non-user memory, but in
321 * some situations (nommu, temporary switch, etc) access_ok() does
322 * not provide enough validation, hence the check on KERNEL_DS.
324 * nmi_uaccess_okay() ensures the probe is not run in an interim
325 * state, when the task or mm are switched. This is specifically
326 * required to prevent the use of temporary mm.
329 if (unlikely(in_interrupt() ||
330 current->flags & (PF_KTHREAD | PF_EXITING)))
332 if (unlikely(uaccess_kernel()))
334 if (unlikely(!nmi_uaccess_okay()))
337 return copy_to_user_nofault(unsafe_ptr, src, size);
340 static const struct bpf_func_proto bpf_probe_write_user_proto = {
341 .func = bpf_probe_write_user,
343 .ret_type = RET_INTEGER,
344 .arg1_type = ARG_ANYTHING,
345 .arg2_type = ARG_PTR_TO_MEM,
346 .arg3_type = ARG_CONST_SIZE,
349 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
351 if (!capable(CAP_SYS_ADMIN))
354 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
355 current->comm, task_pid_nr(current));
357 return &bpf_probe_write_user_proto;
360 static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
363 void __user *user_ptr = (__force void __user *)unsafe_ptr;
369 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
370 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
371 strncpy_from_user_nofault(buf, user_ptr, bufsz);
377 strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
380 strncpy_from_user_nofault(buf, user_ptr, bufsz);
385 static DEFINE_RAW_SPINLOCK(trace_printk_lock);
387 #define BPF_TRACE_PRINTK_SIZE 1024
389 static __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...)
391 static char buf[BPF_TRACE_PRINTK_SIZE];
396 raw_spin_lock_irqsave(&trace_printk_lock, flags);
398 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
400 /* vsnprintf() will not append null for zero-length strings */
403 trace_bpf_trace_printk(buf);
404 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
410 * Only limited trace_printk() conversion specifiers allowed:
411 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pB %pks %pus %s
413 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
414 u64, arg2, u64, arg3)
416 int i, mod[3] = {}, fmt_cnt = 0;
417 char buf[64], fmt_ptype;
418 void *unsafe_ptr = NULL;
419 bool str_seen = false;
422 * bpf_check()->check_func_arg()->check_stack_boundary()
423 * guarantees that fmt points to bpf program stack,
424 * fmt_size bytes of it were initialized and fmt_size > 0
426 if (fmt[--fmt_size] != 0)
429 /* check format string for allowed specifiers */
430 for (i = 0; i < fmt_size; i++) {
431 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
440 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
445 } else if (fmt[i] == 'p') {
447 if ((fmt[i + 1] == 'k' ||
448 fmt[i + 1] == 'u') &&
450 fmt_ptype = fmt[i + 1];
455 if (fmt[i + 1] == 'B') {
460 /* disallow any further format extensions */
461 if (fmt[i + 1] != 0 &&
462 !isspace(fmt[i + 1]) &&
463 !ispunct(fmt[i + 1]))
467 } else if (fmt[i] == 's') {
472 /* allow only one '%s' per fmt string */
476 if (fmt[i + 1] != 0 &&
477 !isspace(fmt[i + 1]) &&
478 !ispunct(fmt[i + 1]))
483 unsafe_ptr = (void *)(long)arg1;
487 unsafe_ptr = (void *)(long)arg2;
491 unsafe_ptr = (void *)(long)arg3;
496 bpf_trace_copy_string(buf, unsafe_ptr, fmt_ptype,
506 if (fmt[i] != 'i' && fmt[i] != 'd' &&
507 fmt[i] != 'u' && fmt[i] != 'x')
513 /* Horrid workaround for getting va_list handling working with different
514 * argument type combinations generically for 32 and 64 bit archs.
516 #define __BPF_TP_EMIT() __BPF_ARG3_TP()
517 #define __BPF_TP(...) \
518 bpf_do_trace_printk(fmt, ##__VA_ARGS__)
520 #define __BPF_ARG1_TP(...) \
521 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
522 ? __BPF_TP(arg1, ##__VA_ARGS__) \
523 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
524 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
525 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
527 #define __BPF_ARG2_TP(...) \
528 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
529 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
530 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
531 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
532 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
534 #define __BPF_ARG3_TP(...) \
535 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
536 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
537 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
538 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
539 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
541 return __BPF_TP_EMIT();
544 static const struct bpf_func_proto bpf_trace_printk_proto = {
545 .func = bpf_trace_printk,
547 .ret_type = RET_INTEGER,
548 .arg1_type = ARG_PTR_TO_MEM,
549 .arg2_type = ARG_CONST_SIZE,
552 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
555 * This program might be calling bpf_trace_printk,
556 * so enable the associated bpf_trace/bpf_trace_printk event.
557 * Repeat this each time as it is possible a user has
558 * disabled bpf_trace_printk events. By loading a program
559 * calling bpf_trace_printk() however the user has expressed
560 * the intent to see such events.
562 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
563 pr_warn_ratelimited("could not enable bpf_trace_printk events");
565 return &bpf_trace_printk_proto;
568 #define MAX_SEQ_PRINTF_VARARGS 12
569 #define MAX_SEQ_PRINTF_MAX_MEMCPY 6
570 #define MAX_SEQ_PRINTF_STR_LEN 128
572 struct bpf_seq_printf_buf {
573 char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN];
575 static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf);
576 static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used);
578 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
579 const void *, data, u32, data_len)
581 int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0;
582 int i, buf_used, copy_size, num_args;
583 u64 params[MAX_SEQ_PRINTF_VARARGS];
584 struct bpf_seq_printf_buf *bufs;
585 const u64 *args = data;
587 buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used);
588 if (WARN_ON_ONCE(buf_used > 1)) {
593 bufs = this_cpu_ptr(&bpf_seq_printf_buf);
596 * bpf_check()->check_func_arg()->check_stack_boundary()
597 * guarantees that fmt points to bpf program stack,
598 * fmt_size bytes of it were initialized and fmt_size > 0
600 if (fmt[--fmt_size] != 0)
606 for (i = 0; i < fmt_size; i++) {
608 if (fmt[i + 1] == '%')
610 else if (!data || !data_len)
615 num_args = data_len / 8;
617 /* check format string for allowed specifiers */
618 for (i = 0; i < fmt_size; i++) {
619 /* only printable ascii for now. */
620 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
628 if (fmt[i + 1] == '%') {
633 if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) {
638 if (fmt_cnt >= num_args) {
643 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
646 /* skip optional "[0 +-][num]" width formating field */
647 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
650 if (fmt[i] >= '1' && fmt[i] <= '9') {
652 while (fmt[i] >= '0' && fmt[i] <= '9')
659 /* try our best to copy */
660 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
665 unsafe_ptr = (void *)(long)args[fmt_cnt];
666 err = strncpy_from_kernel_nofault(bufs->buf[memcpy_cnt],
667 unsafe_ptr, MAX_SEQ_PRINTF_STR_LEN);
669 bufs->buf[memcpy_cnt][0] = '\0';
670 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
678 if (fmt[i + 1] == 0 ||
682 /* just kernel pointers */
683 params[fmt_cnt] = args[fmt_cnt];
688 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
689 if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') {
693 if (fmt[i + 2] != '4' && fmt[i + 2] != '6') {
698 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
704 copy_size = (fmt[i + 2] == '4') ? 4 : 16;
706 err = copy_from_kernel_nofault(bufs->buf[memcpy_cnt],
707 (void *) (long) args[fmt_cnt],
710 memset(bufs->buf[memcpy_cnt], 0, copy_size);
711 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
725 if (fmt[i] != 'i' && fmt[i] != 'd' &&
726 fmt[i] != 'u' && fmt[i] != 'x' &&
732 params[fmt_cnt] = args[fmt_cnt];
736 /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give
737 * all of them to seq_printf().
739 seq_printf(m, fmt, params[0], params[1], params[2], params[3],
740 params[4], params[5], params[6], params[7], params[8],
741 params[9], params[10], params[11]);
743 err = seq_has_overflowed(m) ? -EOVERFLOW : 0;
745 this_cpu_dec(bpf_seq_printf_buf_used);
749 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
751 static const struct bpf_func_proto bpf_seq_printf_proto = {
752 .func = bpf_seq_printf,
754 .ret_type = RET_INTEGER,
755 .arg1_type = ARG_PTR_TO_BTF_ID,
756 .arg1_btf_id = &btf_seq_file_ids[0],
757 .arg2_type = ARG_PTR_TO_MEM,
758 .arg3_type = ARG_CONST_SIZE,
759 .arg4_type = ARG_PTR_TO_MEM_OR_NULL,
760 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
763 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
765 return seq_write(m, data, len) ? -EOVERFLOW : 0;
768 static const struct bpf_func_proto bpf_seq_write_proto = {
769 .func = bpf_seq_write,
771 .ret_type = RET_INTEGER,
772 .arg1_type = ARG_PTR_TO_BTF_ID,
773 .arg1_btf_id = &btf_seq_file_ids[0],
774 .arg2_type = ARG_PTR_TO_MEM,
775 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
778 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
779 u32, btf_ptr_size, u64, flags)
781 const struct btf *btf;
785 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
789 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
792 static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
793 .func = bpf_seq_printf_btf,
795 .ret_type = RET_INTEGER,
796 .arg1_type = ARG_PTR_TO_BTF_ID,
797 .arg1_btf_id = &btf_seq_file_ids[0],
798 .arg2_type = ARG_PTR_TO_MEM,
799 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
800 .arg4_type = ARG_ANYTHING,
803 static __always_inline int
804 get_map_perf_counter(struct bpf_map *map, u64 flags,
805 u64 *value, u64 *enabled, u64 *running)
807 struct bpf_array *array = container_of(map, struct bpf_array, map);
808 unsigned int cpu = smp_processor_id();
809 u64 index = flags & BPF_F_INDEX_MASK;
810 struct bpf_event_entry *ee;
812 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
814 if (index == BPF_F_CURRENT_CPU)
816 if (unlikely(index >= array->map.max_entries))
819 ee = READ_ONCE(array->ptrs[index]);
823 return perf_event_read_local(ee->event, value, enabled, running);
826 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
831 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
833 * this api is ugly since we miss [-22..-2] range of valid
834 * counter values, but that's uapi
841 static const struct bpf_func_proto bpf_perf_event_read_proto = {
842 .func = bpf_perf_event_read,
844 .ret_type = RET_INTEGER,
845 .arg1_type = ARG_CONST_MAP_PTR,
846 .arg2_type = ARG_ANYTHING,
849 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
850 struct bpf_perf_event_value *, buf, u32, size)
854 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
856 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
862 memset(buf, 0, size);
866 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
867 .func = bpf_perf_event_read_value,
869 .ret_type = RET_INTEGER,
870 .arg1_type = ARG_CONST_MAP_PTR,
871 .arg2_type = ARG_ANYTHING,
872 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
873 .arg4_type = ARG_CONST_SIZE,
876 static __always_inline u64
877 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
878 u64 flags, struct perf_sample_data *sd)
880 struct bpf_array *array = container_of(map, struct bpf_array, map);
881 unsigned int cpu = smp_processor_id();
882 u64 index = flags & BPF_F_INDEX_MASK;
883 struct bpf_event_entry *ee;
884 struct perf_event *event;
886 if (index == BPF_F_CURRENT_CPU)
888 if (unlikely(index >= array->map.max_entries))
891 ee = READ_ONCE(array->ptrs[index]);
896 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
897 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
900 if (unlikely(event->oncpu != cpu))
903 return perf_event_output(event, sd, regs);
907 * Support executing tracepoints in normal, irq, and nmi context that each call
908 * bpf_perf_event_output
910 struct bpf_trace_sample_data {
911 struct perf_sample_data sds[3];
914 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
915 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
916 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
917 u64, flags, void *, data, u64, size)
919 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
920 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
921 struct perf_raw_record raw = {
927 struct perf_sample_data *sd;
930 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
935 sd = &sds->sds[nest_level - 1];
937 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
942 perf_sample_data_init(sd, 0, 0);
945 err = __bpf_perf_event_output(regs, map, flags, sd);
948 this_cpu_dec(bpf_trace_nest_level);
952 static const struct bpf_func_proto bpf_perf_event_output_proto = {
953 .func = bpf_perf_event_output,
955 .ret_type = RET_INTEGER,
956 .arg1_type = ARG_PTR_TO_CTX,
957 .arg2_type = ARG_CONST_MAP_PTR,
958 .arg3_type = ARG_ANYTHING,
959 .arg4_type = ARG_PTR_TO_MEM,
960 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
963 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
964 struct bpf_nested_pt_regs {
965 struct pt_regs regs[3];
967 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
968 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
970 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
971 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
973 struct perf_raw_frag frag = {
978 struct perf_raw_record raw = {
981 .next = ctx_size ? &frag : NULL,
987 struct perf_sample_data *sd;
988 struct pt_regs *regs;
993 nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
995 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
999 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
1000 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
1002 perf_fetch_caller_regs(regs);
1003 perf_sample_data_init(sd, 0, 0);
1006 ret = __bpf_perf_event_output(regs, map, flags, sd);
1008 this_cpu_dec(bpf_event_output_nest_level);
1013 BPF_CALL_0(bpf_get_current_task)
1015 return (long) current;
1018 const struct bpf_func_proto bpf_get_current_task_proto = {
1019 .func = bpf_get_current_task,
1021 .ret_type = RET_INTEGER,
1024 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
1026 struct bpf_array *array = container_of(map, struct bpf_array, map);
1027 struct cgroup *cgrp;
1029 if (unlikely(idx >= array->map.max_entries))
1032 cgrp = READ_ONCE(array->ptrs[idx]);
1033 if (unlikely(!cgrp))
1036 return task_under_cgroup_hierarchy(current, cgrp);
1039 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
1040 .func = bpf_current_task_under_cgroup,
1042 .ret_type = RET_INTEGER,
1043 .arg1_type = ARG_CONST_MAP_PTR,
1044 .arg2_type = ARG_ANYTHING,
1047 struct send_signal_irq_work {
1048 struct irq_work irq_work;
1049 struct task_struct *task;
1054 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
1056 static void do_bpf_send_signal(struct irq_work *entry)
1058 struct send_signal_irq_work *work;
1060 work = container_of(entry, struct send_signal_irq_work, irq_work);
1061 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
1062 put_task_struct(work->task);
1065 static int bpf_send_signal_common(u32 sig, enum pid_type type)
1067 struct send_signal_irq_work *work = NULL;
1069 /* Similar to bpf_probe_write_user, task needs to be
1070 * in a sound condition and kernel memory access be
1071 * permitted in order to send signal to the current
1074 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
1076 if (unlikely(uaccess_kernel()))
1078 if (unlikely(!nmi_uaccess_okay()))
1080 /* Task should not be pid=1 to avoid kernel panic. */
1081 if (unlikely(is_global_init(current)))
1084 if (irqs_disabled()) {
1085 /* Do an early check on signal validity. Otherwise,
1086 * the error is lost in deferred irq_work.
1088 if (unlikely(!valid_signal(sig)))
1091 work = this_cpu_ptr(&send_signal_work);
1092 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY)
1095 /* Add the current task, which is the target of sending signal,
1096 * to the irq_work. The current task may change when queued
1097 * irq works get executed.
1099 work->task = get_task_struct(current);
1102 irq_work_queue(&work->irq_work);
1106 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
1109 BPF_CALL_1(bpf_send_signal, u32, sig)
1111 return bpf_send_signal_common(sig, PIDTYPE_TGID);
1114 static const struct bpf_func_proto bpf_send_signal_proto = {
1115 .func = bpf_send_signal,
1117 .ret_type = RET_INTEGER,
1118 .arg1_type = ARG_ANYTHING,
1121 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
1123 return bpf_send_signal_common(sig, PIDTYPE_PID);
1126 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
1127 .func = bpf_send_signal_thread,
1129 .ret_type = RET_INTEGER,
1130 .arg1_type = ARG_ANYTHING,
1133 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
1143 * The path pointer is verified as trusted and safe to use,
1144 * but let's double check it's valid anyway to workaround
1145 * potentially broken verifier.
1147 len = copy_from_kernel_nofault(©, path, sizeof(*path));
1151 p = d_path(©, buf, sz);
1156 memmove(buf, p, len);
1162 BTF_SET_START(btf_allowlist_d_path)
1163 #ifdef CONFIG_SECURITY
1164 BTF_ID(func, security_file_permission)
1165 BTF_ID(func, security_inode_getattr)
1166 BTF_ID(func, security_file_open)
1168 #ifdef CONFIG_SECURITY_PATH
1169 BTF_ID(func, security_path_truncate)
1171 BTF_ID(func, vfs_truncate)
1172 BTF_ID(func, vfs_fallocate)
1173 BTF_ID(func, dentry_open)
1174 BTF_ID(func, vfs_getattr)
1175 BTF_ID(func, filp_close)
1176 BTF_SET_END(btf_allowlist_d_path)
1178 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
1180 return btf_id_set_contains(&btf_allowlist_d_path, prog->aux->attach_btf_id);
1183 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
1185 static const struct bpf_func_proto bpf_d_path_proto = {
1188 .ret_type = RET_INTEGER,
1189 .arg1_type = ARG_PTR_TO_BTF_ID,
1190 .arg1_btf_id = &bpf_d_path_btf_ids[0],
1191 .arg2_type = ARG_PTR_TO_MEM,
1192 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1193 .allowed = bpf_d_path_allowed,
1196 #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
1197 BTF_F_PTR_RAW | BTF_F_ZERO)
1199 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
1200 u64 flags, const struct btf **btf,
1203 const struct btf_type *t;
1205 if (unlikely(flags & ~(BTF_F_ALL)))
1208 if (btf_ptr_size != sizeof(struct btf_ptr))
1211 *btf = bpf_get_btf_vmlinux();
1213 if (IS_ERR_OR_NULL(*btf))
1214 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
1216 if (ptr->type_id > 0)
1217 *btf_id = ptr->type_id;
1222 t = btf_type_by_id(*btf, *btf_id);
1223 if (*btf_id <= 0 || !t)
1229 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1230 u32, btf_ptr_size, u64, flags)
1232 const struct btf *btf;
1236 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1240 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1244 const struct bpf_func_proto bpf_snprintf_btf_proto = {
1245 .func = bpf_snprintf_btf,
1247 .ret_type = RET_INTEGER,
1248 .arg1_type = ARG_PTR_TO_MEM,
1249 .arg2_type = ARG_CONST_SIZE,
1250 .arg3_type = ARG_PTR_TO_MEM,
1251 .arg4_type = ARG_CONST_SIZE,
1252 .arg5_type = ARG_ANYTHING,
1255 const struct bpf_func_proto *
1256 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1259 case BPF_FUNC_map_lookup_elem:
1260 return &bpf_map_lookup_elem_proto;
1261 case BPF_FUNC_map_update_elem:
1262 return &bpf_map_update_elem_proto;
1263 case BPF_FUNC_map_delete_elem:
1264 return &bpf_map_delete_elem_proto;
1265 case BPF_FUNC_map_push_elem:
1266 return &bpf_map_push_elem_proto;
1267 case BPF_FUNC_map_pop_elem:
1268 return &bpf_map_pop_elem_proto;
1269 case BPF_FUNC_map_peek_elem:
1270 return &bpf_map_peek_elem_proto;
1271 case BPF_FUNC_ktime_get_ns:
1272 return &bpf_ktime_get_ns_proto;
1273 case BPF_FUNC_ktime_get_boot_ns:
1274 return &bpf_ktime_get_boot_ns_proto;
1275 case BPF_FUNC_tail_call:
1276 return &bpf_tail_call_proto;
1277 case BPF_FUNC_get_current_pid_tgid:
1278 return &bpf_get_current_pid_tgid_proto;
1279 case BPF_FUNC_get_current_task:
1280 return &bpf_get_current_task_proto;
1281 case BPF_FUNC_get_current_uid_gid:
1282 return &bpf_get_current_uid_gid_proto;
1283 case BPF_FUNC_get_current_comm:
1284 return &bpf_get_current_comm_proto;
1285 case BPF_FUNC_trace_printk:
1286 return bpf_get_trace_printk_proto();
1287 case BPF_FUNC_get_smp_processor_id:
1288 return &bpf_get_smp_processor_id_proto;
1289 case BPF_FUNC_get_numa_node_id:
1290 return &bpf_get_numa_node_id_proto;
1291 case BPF_FUNC_perf_event_read:
1292 return &bpf_perf_event_read_proto;
1293 case BPF_FUNC_current_task_under_cgroup:
1294 return &bpf_current_task_under_cgroup_proto;
1295 case BPF_FUNC_get_prandom_u32:
1296 return &bpf_get_prandom_u32_proto;
1297 case BPF_FUNC_probe_write_user:
1298 return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1299 NULL : bpf_get_probe_write_proto();
1300 case BPF_FUNC_probe_read_user:
1301 return &bpf_probe_read_user_proto;
1302 case BPF_FUNC_probe_read_kernel:
1303 return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1304 NULL : &bpf_probe_read_kernel_proto;
1305 case BPF_FUNC_probe_read_user_str:
1306 return &bpf_probe_read_user_str_proto;
1307 case BPF_FUNC_probe_read_kernel_str:
1308 return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1309 NULL : &bpf_probe_read_kernel_str_proto;
1310 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1311 case BPF_FUNC_probe_read:
1312 return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1313 NULL : &bpf_probe_read_compat_proto;
1314 case BPF_FUNC_probe_read_str:
1315 return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1316 NULL : &bpf_probe_read_compat_str_proto;
1318 #ifdef CONFIG_CGROUPS
1319 case BPF_FUNC_get_current_cgroup_id:
1320 return &bpf_get_current_cgroup_id_proto;
1322 case BPF_FUNC_send_signal:
1323 return &bpf_send_signal_proto;
1324 case BPF_FUNC_send_signal_thread:
1325 return &bpf_send_signal_thread_proto;
1326 case BPF_FUNC_perf_event_read_value:
1327 return &bpf_perf_event_read_value_proto;
1328 case BPF_FUNC_get_ns_current_pid_tgid:
1329 return &bpf_get_ns_current_pid_tgid_proto;
1330 case BPF_FUNC_ringbuf_output:
1331 return &bpf_ringbuf_output_proto;
1332 case BPF_FUNC_ringbuf_reserve:
1333 return &bpf_ringbuf_reserve_proto;
1334 case BPF_FUNC_ringbuf_submit:
1335 return &bpf_ringbuf_submit_proto;
1336 case BPF_FUNC_ringbuf_discard:
1337 return &bpf_ringbuf_discard_proto;
1338 case BPF_FUNC_ringbuf_query:
1339 return &bpf_ringbuf_query_proto;
1340 case BPF_FUNC_jiffies64:
1341 return &bpf_jiffies64_proto;
1342 case BPF_FUNC_get_task_stack:
1343 return &bpf_get_task_stack_proto;
1344 case BPF_FUNC_copy_from_user:
1345 return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
1346 case BPF_FUNC_snprintf_btf:
1347 return &bpf_snprintf_btf_proto;
1348 case BPF_FUNC_per_cpu_ptr:
1349 return &bpf_per_cpu_ptr_proto;
1350 case BPF_FUNC_this_cpu_ptr:
1351 return &bpf_this_cpu_ptr_proto;
1357 static const struct bpf_func_proto *
1358 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1361 case BPF_FUNC_perf_event_output:
1362 return &bpf_perf_event_output_proto;
1363 case BPF_FUNC_get_stackid:
1364 return &bpf_get_stackid_proto;
1365 case BPF_FUNC_get_stack:
1366 return &bpf_get_stack_proto;
1367 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1368 case BPF_FUNC_override_return:
1369 return &bpf_override_return_proto;
1372 return bpf_tracing_func_proto(func_id, prog);
1376 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1377 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1378 const struct bpf_prog *prog,
1379 struct bpf_insn_access_aux *info)
1381 if (off < 0 || off >= sizeof(struct pt_regs))
1383 if (type != BPF_READ)
1385 if (off % size != 0)
1388 * Assertion for 32 bit to make sure last 8 byte access
1389 * (BPF_DW) to the last 4 byte member is disallowed.
1391 if (off + size > sizeof(struct pt_regs))
1397 const struct bpf_verifier_ops kprobe_verifier_ops = {
1398 .get_func_proto = kprobe_prog_func_proto,
1399 .is_valid_access = kprobe_prog_is_valid_access,
1402 const struct bpf_prog_ops kprobe_prog_ops = {
1405 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1406 u64, flags, void *, data, u64, size)
1408 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1411 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1412 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1413 * from there and call the same bpf_perf_event_output() helper inline.
1415 return ____bpf_perf_event_output(regs, map, flags, data, size);
1418 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1419 .func = bpf_perf_event_output_tp,
1421 .ret_type = RET_INTEGER,
1422 .arg1_type = ARG_PTR_TO_CTX,
1423 .arg2_type = ARG_CONST_MAP_PTR,
1424 .arg3_type = ARG_ANYTHING,
1425 .arg4_type = ARG_PTR_TO_MEM,
1426 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1429 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1432 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1435 * Same comment as in bpf_perf_event_output_tp(), only that this time
1436 * the other helper's function body cannot be inlined due to being
1437 * external, thus we need to call raw helper function.
1439 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1443 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1444 .func = bpf_get_stackid_tp,
1446 .ret_type = RET_INTEGER,
1447 .arg1_type = ARG_PTR_TO_CTX,
1448 .arg2_type = ARG_CONST_MAP_PTR,
1449 .arg3_type = ARG_ANYTHING,
1452 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1455 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1457 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1458 (unsigned long) size, flags, 0);
1461 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1462 .func = bpf_get_stack_tp,
1464 .ret_type = RET_INTEGER,
1465 .arg1_type = ARG_PTR_TO_CTX,
1466 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1467 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1468 .arg4_type = ARG_ANYTHING,
1471 static const struct bpf_func_proto *
1472 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1475 case BPF_FUNC_perf_event_output:
1476 return &bpf_perf_event_output_proto_tp;
1477 case BPF_FUNC_get_stackid:
1478 return &bpf_get_stackid_proto_tp;
1479 case BPF_FUNC_get_stack:
1480 return &bpf_get_stack_proto_tp;
1482 return bpf_tracing_func_proto(func_id, prog);
1486 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1487 const struct bpf_prog *prog,
1488 struct bpf_insn_access_aux *info)
1490 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1492 if (type != BPF_READ)
1494 if (off % size != 0)
1497 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1501 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1502 .get_func_proto = tp_prog_func_proto,
1503 .is_valid_access = tp_prog_is_valid_access,
1506 const struct bpf_prog_ops tracepoint_prog_ops = {
1509 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1510 struct bpf_perf_event_value *, buf, u32, size)
1514 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1516 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1522 memset(buf, 0, size);
1526 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1527 .func = bpf_perf_prog_read_value,
1529 .ret_type = RET_INTEGER,
1530 .arg1_type = ARG_PTR_TO_CTX,
1531 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1532 .arg3_type = ARG_CONST_SIZE,
1535 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1536 void *, buf, u32, size, u64, flags)
1538 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1539 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1542 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1545 if (unlikely(!br_stack))
1548 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1549 return br_stack->nr * br_entry_size;
1551 if (!buf || (size % br_entry_size != 0))
1554 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1555 memcpy(buf, br_stack->entries, to_copy);
1560 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1561 .func = bpf_read_branch_records,
1563 .ret_type = RET_INTEGER,
1564 .arg1_type = ARG_PTR_TO_CTX,
1565 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1566 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1567 .arg4_type = ARG_ANYTHING,
1570 static const struct bpf_func_proto *
1571 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1574 case BPF_FUNC_perf_event_output:
1575 return &bpf_perf_event_output_proto_tp;
1576 case BPF_FUNC_get_stackid:
1577 return &bpf_get_stackid_proto_pe;
1578 case BPF_FUNC_get_stack:
1579 return &bpf_get_stack_proto_pe;
1580 case BPF_FUNC_perf_prog_read_value:
1581 return &bpf_perf_prog_read_value_proto;
1582 case BPF_FUNC_read_branch_records:
1583 return &bpf_read_branch_records_proto;
1585 return bpf_tracing_func_proto(func_id, prog);
1590 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1591 * to avoid potential recursive reuse issue when/if tracepoints are added
1592 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1594 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1595 * in normal, irq, and nmi context.
1597 struct bpf_raw_tp_regs {
1598 struct pt_regs regs[3];
1600 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1601 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1602 static struct pt_regs *get_bpf_raw_tp_regs(void)
1604 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1605 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1607 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1608 this_cpu_dec(bpf_raw_tp_nest_level);
1609 return ERR_PTR(-EBUSY);
1612 return &tp_regs->regs[nest_level - 1];
1615 static void put_bpf_raw_tp_regs(void)
1617 this_cpu_dec(bpf_raw_tp_nest_level);
1620 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1621 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1623 struct pt_regs *regs = get_bpf_raw_tp_regs();
1627 return PTR_ERR(regs);
1629 perf_fetch_caller_regs(regs);
1630 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1632 put_bpf_raw_tp_regs();
1636 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1637 .func = bpf_perf_event_output_raw_tp,
1639 .ret_type = RET_INTEGER,
1640 .arg1_type = ARG_PTR_TO_CTX,
1641 .arg2_type = ARG_CONST_MAP_PTR,
1642 .arg3_type = ARG_ANYTHING,
1643 .arg4_type = ARG_PTR_TO_MEM,
1644 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1647 extern const struct bpf_func_proto bpf_skb_output_proto;
1648 extern const struct bpf_func_proto bpf_xdp_output_proto;
1650 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1651 struct bpf_map *, map, u64, flags)
1653 struct pt_regs *regs = get_bpf_raw_tp_regs();
1657 return PTR_ERR(regs);
1659 perf_fetch_caller_regs(regs);
1660 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1661 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1663 put_bpf_raw_tp_regs();
1667 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1668 .func = bpf_get_stackid_raw_tp,
1670 .ret_type = RET_INTEGER,
1671 .arg1_type = ARG_PTR_TO_CTX,
1672 .arg2_type = ARG_CONST_MAP_PTR,
1673 .arg3_type = ARG_ANYTHING,
1676 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1677 void *, buf, u32, size, u64, flags)
1679 struct pt_regs *regs = get_bpf_raw_tp_regs();
1683 return PTR_ERR(regs);
1685 perf_fetch_caller_regs(regs);
1686 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1687 (unsigned long) size, flags, 0);
1688 put_bpf_raw_tp_regs();
1692 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1693 .func = bpf_get_stack_raw_tp,
1695 .ret_type = RET_INTEGER,
1696 .arg1_type = ARG_PTR_TO_CTX,
1697 .arg2_type = ARG_PTR_TO_MEM,
1698 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1699 .arg4_type = ARG_ANYTHING,
1702 static const struct bpf_func_proto *
1703 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1706 case BPF_FUNC_perf_event_output:
1707 return &bpf_perf_event_output_proto_raw_tp;
1708 case BPF_FUNC_get_stackid:
1709 return &bpf_get_stackid_proto_raw_tp;
1710 case BPF_FUNC_get_stack:
1711 return &bpf_get_stack_proto_raw_tp;
1713 return bpf_tracing_func_proto(func_id, prog);
1717 const struct bpf_func_proto *
1718 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1722 case BPF_FUNC_skb_output:
1723 return &bpf_skb_output_proto;
1724 case BPF_FUNC_xdp_output:
1725 return &bpf_xdp_output_proto;
1726 case BPF_FUNC_skc_to_tcp6_sock:
1727 return &bpf_skc_to_tcp6_sock_proto;
1728 case BPF_FUNC_skc_to_tcp_sock:
1729 return &bpf_skc_to_tcp_sock_proto;
1730 case BPF_FUNC_skc_to_tcp_timewait_sock:
1731 return &bpf_skc_to_tcp_timewait_sock_proto;
1732 case BPF_FUNC_skc_to_tcp_request_sock:
1733 return &bpf_skc_to_tcp_request_sock_proto;
1734 case BPF_FUNC_skc_to_udp6_sock:
1735 return &bpf_skc_to_udp6_sock_proto;
1737 case BPF_FUNC_seq_printf:
1738 return prog->expected_attach_type == BPF_TRACE_ITER ?
1739 &bpf_seq_printf_proto :
1741 case BPF_FUNC_seq_write:
1742 return prog->expected_attach_type == BPF_TRACE_ITER ?
1743 &bpf_seq_write_proto :
1745 case BPF_FUNC_seq_printf_btf:
1746 return prog->expected_attach_type == BPF_TRACE_ITER ?
1747 &bpf_seq_printf_btf_proto :
1749 case BPF_FUNC_d_path:
1750 return &bpf_d_path_proto;
1752 return raw_tp_prog_func_proto(func_id, prog);
1756 static bool raw_tp_prog_is_valid_access(int off, int size,
1757 enum bpf_access_type type,
1758 const struct bpf_prog *prog,
1759 struct bpf_insn_access_aux *info)
1761 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1763 if (type != BPF_READ)
1765 if (off % size != 0)
1770 static bool tracing_prog_is_valid_access(int off, int size,
1771 enum bpf_access_type type,
1772 const struct bpf_prog *prog,
1773 struct bpf_insn_access_aux *info)
1775 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1777 if (type != BPF_READ)
1779 if (off % size != 0)
1781 return btf_ctx_access(off, size, type, prog, info);
1784 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1785 const union bpf_attr *kattr,
1786 union bpf_attr __user *uattr)
1791 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1792 .get_func_proto = raw_tp_prog_func_proto,
1793 .is_valid_access = raw_tp_prog_is_valid_access,
1796 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1798 .test_run = bpf_prog_test_run_raw_tp,
1802 const struct bpf_verifier_ops tracing_verifier_ops = {
1803 .get_func_proto = tracing_prog_func_proto,
1804 .is_valid_access = tracing_prog_is_valid_access,
1807 const struct bpf_prog_ops tracing_prog_ops = {
1808 .test_run = bpf_prog_test_run_tracing,
1811 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1812 enum bpf_access_type type,
1813 const struct bpf_prog *prog,
1814 struct bpf_insn_access_aux *info)
1817 if (size != sizeof(u64) || type != BPF_READ)
1819 info->reg_type = PTR_TO_TP_BUFFER;
1821 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1824 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1825 .get_func_proto = raw_tp_prog_func_proto,
1826 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1829 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1832 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1833 const struct bpf_prog *prog,
1834 struct bpf_insn_access_aux *info)
1836 const int size_u64 = sizeof(u64);
1838 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1840 if (type != BPF_READ)
1842 if (off % size != 0) {
1843 if (sizeof(unsigned long) != 4)
1847 if (off % size != 4)
1852 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
1853 bpf_ctx_record_field_size(info, size_u64);
1854 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1857 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1858 bpf_ctx_record_field_size(info, size_u64);
1859 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1863 if (size != sizeof(long))
1870 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1871 const struct bpf_insn *si,
1872 struct bpf_insn *insn_buf,
1873 struct bpf_prog *prog, u32 *target_size)
1875 struct bpf_insn *insn = insn_buf;
1878 case offsetof(struct bpf_perf_event_data, sample_period):
1879 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1880 data), si->dst_reg, si->src_reg,
1881 offsetof(struct bpf_perf_event_data_kern, data));
1882 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1883 bpf_target_off(struct perf_sample_data, period, 8,
1886 case offsetof(struct bpf_perf_event_data, addr):
1887 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1888 data), si->dst_reg, si->src_reg,
1889 offsetof(struct bpf_perf_event_data_kern, data));
1890 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1891 bpf_target_off(struct perf_sample_data, addr, 8,
1895 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1896 regs), si->dst_reg, si->src_reg,
1897 offsetof(struct bpf_perf_event_data_kern, regs));
1898 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1903 return insn - insn_buf;
1906 const struct bpf_verifier_ops perf_event_verifier_ops = {
1907 .get_func_proto = pe_prog_func_proto,
1908 .is_valid_access = pe_prog_is_valid_access,
1909 .convert_ctx_access = pe_prog_convert_ctx_access,
1912 const struct bpf_prog_ops perf_event_prog_ops = {
1915 static DEFINE_MUTEX(bpf_event_mutex);
1917 #define BPF_TRACE_MAX_PROGS 64
1919 int perf_event_attach_bpf_prog(struct perf_event *event,
1920 struct bpf_prog *prog)
1922 struct bpf_prog_array *old_array;
1923 struct bpf_prog_array *new_array;
1927 * Kprobe override only works if they are on the function entry,
1928 * and only if they are on the opt-in list.
1930 if (prog->kprobe_override &&
1931 (!trace_kprobe_on_func_entry(event->tp_event) ||
1932 !trace_kprobe_error_injectable(event->tp_event)))
1935 mutex_lock(&bpf_event_mutex);
1940 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1942 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1947 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1951 /* set the new array to event->tp_event and set event->prog */
1953 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1954 bpf_prog_array_free(old_array);
1957 mutex_unlock(&bpf_event_mutex);
1961 void perf_event_detach_bpf_prog(struct perf_event *event)
1963 struct bpf_prog_array *old_array;
1964 struct bpf_prog_array *new_array;
1967 mutex_lock(&bpf_event_mutex);
1972 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1973 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
1977 bpf_prog_array_delete_safe(old_array, event->prog);
1979 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1980 bpf_prog_array_free(old_array);
1983 bpf_prog_put(event->prog);
1987 mutex_unlock(&bpf_event_mutex);
1990 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
1992 struct perf_event_query_bpf __user *uquery = info;
1993 struct perf_event_query_bpf query = {};
1994 struct bpf_prog_array *progs;
1995 u32 *ids, prog_cnt, ids_len;
1998 if (!perfmon_capable())
2000 if (event->attr.type != PERF_TYPE_TRACEPOINT)
2002 if (copy_from_user(&query, uquery, sizeof(query)))
2005 ids_len = query.ids_len;
2006 if (ids_len > BPF_TRACE_MAX_PROGS)
2008 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2012 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
2013 * is required when user only wants to check for uquery->prog_cnt.
2014 * There is no need to check for it since the case is handled
2015 * gracefully in bpf_prog_array_copy_info.
2018 mutex_lock(&bpf_event_mutex);
2019 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2020 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2021 mutex_unlock(&bpf_event_mutex);
2023 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2024 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2031 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2032 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2034 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2036 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2038 for (; btp < __stop__bpf_raw_tp; btp++) {
2039 if (!strcmp(btp->tp->name, name))
2043 return bpf_get_raw_tracepoint_module(name);
2046 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2051 mod = __module_address((unsigned long)btp);
2056 static __always_inline
2057 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2061 (void) BPF_PROG_RUN(prog, args);
2065 #define UNPACK(...) __VA_ARGS__
2066 #define REPEAT_1(FN, DL, X, ...) FN(X)
2067 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2068 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2069 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2070 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2071 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2072 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2073 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2074 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2075 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2076 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2077 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2078 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2080 #define SARG(X) u64 arg##X
2081 #define COPY(X) args[X] = arg##X
2083 #define __DL_COM (,)
2084 #define __DL_SEM (;)
2086 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2088 #define BPF_TRACE_DEFN_x(x) \
2089 void bpf_trace_run##x(struct bpf_prog *prog, \
2090 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2093 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2094 __bpf_trace_run(prog, args); \
2096 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2097 BPF_TRACE_DEFN_x(1);
2098 BPF_TRACE_DEFN_x(2);
2099 BPF_TRACE_DEFN_x(3);
2100 BPF_TRACE_DEFN_x(4);
2101 BPF_TRACE_DEFN_x(5);
2102 BPF_TRACE_DEFN_x(6);
2103 BPF_TRACE_DEFN_x(7);
2104 BPF_TRACE_DEFN_x(8);
2105 BPF_TRACE_DEFN_x(9);
2106 BPF_TRACE_DEFN_x(10);
2107 BPF_TRACE_DEFN_x(11);
2108 BPF_TRACE_DEFN_x(12);
2110 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2112 struct tracepoint *tp = btp->tp;
2115 * check that program doesn't access arguments beyond what's
2116 * available in this tracepoint
2118 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2121 if (prog->aux->max_tp_access > btp->writable_size)
2124 return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
2128 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2130 return __bpf_probe_register(btp, prog);
2133 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2135 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2138 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2139 u32 *fd_type, const char **buf,
2140 u64 *probe_offset, u64 *probe_addr)
2142 bool is_tracepoint, is_syscall_tp;
2143 struct bpf_prog *prog;
2150 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2151 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2154 *prog_id = prog->aux->id;
2155 flags = event->tp_event->flags;
2156 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2157 is_syscall_tp = is_syscall_trace_event(event->tp_event);
2159 if (is_tracepoint || is_syscall_tp) {
2160 *buf = is_tracepoint ? event->tp_event->tp->name
2161 : event->tp_event->name;
2162 *fd_type = BPF_FD_TYPE_TRACEPOINT;
2163 *probe_offset = 0x0;
2168 #ifdef CONFIG_KPROBE_EVENTS
2169 if (flags & TRACE_EVENT_FL_KPROBE)
2170 err = bpf_get_kprobe_info(event, fd_type, buf,
2171 probe_offset, probe_addr,
2172 event->attr.type == PERF_TYPE_TRACEPOINT);
2174 #ifdef CONFIG_UPROBE_EVENTS
2175 if (flags & TRACE_EVENT_FL_UPROBE)
2176 err = bpf_get_uprobe_info(event, fd_type, buf,
2177 probe_offset, probe_addr,
2178 event->attr.type == PERF_TYPE_TRACEPOINT);
2185 static int __init send_signal_irq_work_init(void)
2188 struct send_signal_irq_work *work;
2190 for_each_possible_cpu(cpu) {
2191 work = per_cpu_ptr(&send_signal_work, cpu);
2192 init_irq_work(&work->irq_work, do_bpf_send_signal);
2197 subsys_initcall(send_signal_irq_work_init);
2199 #ifdef CONFIG_MODULES
2200 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2203 struct bpf_trace_module *btm, *tmp;
2204 struct module *mod = module;
2207 if (mod->num_bpf_raw_events == 0 ||
2208 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2211 mutex_lock(&bpf_module_mutex);
2214 case MODULE_STATE_COMING:
2215 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2217 btm->module = module;
2218 list_add(&btm->list, &bpf_trace_modules);
2223 case MODULE_STATE_GOING:
2224 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2225 if (btm->module == module) {
2226 list_del(&btm->list);
2234 mutex_unlock(&bpf_module_mutex);
2237 return notifier_from_errno(ret);
2240 static struct notifier_block bpf_module_nb = {
2241 .notifier_call = bpf_event_notify,
2244 static int __init bpf_event_init(void)
2246 register_module_notifier(&bpf_module_nb);
2250 fs_initcall(bpf_event_init);
2251 #endif /* CONFIG_MODULES */