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>
16 #include <linux/btf_ids.h>
20 #include "trace_probe.h"
23 #define bpf_event_rcu_dereference(p) \
24 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
27 struct bpf_trace_module {
28 struct module *module;
29 struct list_head list;
32 static LIST_HEAD(bpf_trace_modules);
33 static DEFINE_MUTEX(bpf_module_mutex);
35 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
37 struct bpf_raw_event_map *btp, *ret = NULL;
38 struct bpf_trace_module *btm;
41 mutex_lock(&bpf_module_mutex);
42 list_for_each_entry(btm, &bpf_trace_modules, list) {
43 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
44 btp = &btm->module->bpf_raw_events[i];
45 if (!strcmp(btp->tp->name, name)) {
46 if (try_module_get(btm->module))
53 mutex_unlock(&bpf_module_mutex);
57 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
61 #endif /* CONFIG_MODULES */
63 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
64 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
67 * trace_call_bpf - invoke BPF program
68 * @call: tracepoint event
69 * @ctx: opaque context pointer
71 * kprobe handlers execute BPF programs via this helper.
72 * Can be used from static tracepoints in the future.
74 * Return: BPF programs always return an integer which is interpreted by
76 * 0 - return from kprobe (event is filtered out)
77 * 1 - store kprobe event into ring buffer
78 * Other values are reserved and currently alias to 1
80 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
84 if (in_nmi()) /* not supported yet */
89 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
91 * since some bpf program is already running on this cpu,
92 * don't call into another bpf program (same or different)
93 * and don't send kprobe event into ring-buffer,
101 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
102 * to all call sites, we did a bpf_prog_array_valid() there to check
103 * whether call->prog_array is empty or not, which is
104 * a heurisitc to speed up execution.
106 * If bpf_prog_array_valid() fetched prog_array was
107 * non-NULL, we go into trace_call_bpf() and do the actual
108 * proper rcu_dereference() under RCU lock.
109 * If it turns out that prog_array is NULL then, we bail out.
110 * For the opposite, if the bpf_prog_array_valid() fetched pointer
111 * was NULL, you'll skip the prog_array with the risk of missing
112 * out of events when it was updated in between this and the
113 * rcu_dereference() which is accepted risk.
115 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
118 __this_cpu_dec(bpf_prog_active);
123 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
124 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
126 regs_set_return_value(regs, rc);
127 override_function_with_return(regs);
131 static const struct bpf_func_proto bpf_override_return_proto = {
132 .func = bpf_override_return,
134 .ret_type = RET_INTEGER,
135 .arg1_type = ARG_PTR_TO_CTX,
136 .arg2_type = ARG_ANYTHING,
140 static __always_inline int
141 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
145 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
146 if (unlikely(ret < 0))
147 memset(dst, 0, size);
151 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
152 const void __user *, unsafe_ptr)
154 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
157 const struct bpf_func_proto bpf_probe_read_user_proto = {
158 .func = bpf_probe_read_user,
160 .ret_type = RET_INTEGER,
161 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
162 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
163 .arg3_type = ARG_ANYTHING,
166 static __always_inline int
167 bpf_probe_read_user_str_common(void *dst, u32 size,
168 const void __user *unsafe_ptr)
172 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
173 if (unlikely(ret < 0))
174 memset(dst, 0, size);
178 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
179 const void __user *, unsafe_ptr)
181 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
184 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
185 .func = bpf_probe_read_user_str,
187 .ret_type = RET_INTEGER,
188 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
189 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
190 .arg3_type = ARG_ANYTHING,
193 static __always_inline int
194 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
196 int ret = security_locked_down(LOCKDOWN_BPF_READ);
198 if (unlikely(ret < 0))
200 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
201 if (unlikely(ret < 0))
205 memset(dst, 0, size);
209 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
210 const void *, unsafe_ptr)
212 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
215 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
216 .func = bpf_probe_read_kernel,
218 .ret_type = RET_INTEGER,
219 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
220 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
221 .arg3_type = ARG_ANYTHING,
224 static __always_inline int
225 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
227 int ret = security_locked_down(LOCKDOWN_BPF_READ);
229 if (unlikely(ret < 0))
233 * The strncpy_from_kernel_nofault() call will likely not fill the
234 * entire buffer, but that's okay in this circumstance as we're probing
235 * arbitrary memory anyway similar to bpf_probe_read_*() and might
236 * as well probe the stack. Thus, memory is explicitly cleared
237 * only in error case, so that improper users ignoring return
238 * code altogether don't copy garbage; otherwise length of string
239 * is returned that can be used for bpf_perf_event_output() et al.
241 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
242 if (unlikely(ret < 0))
247 memset(dst, 0, size);
251 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
252 const void *, unsafe_ptr)
254 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
257 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
258 .func = bpf_probe_read_kernel_str,
260 .ret_type = RET_INTEGER,
261 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
262 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
263 .arg3_type = ARG_ANYTHING,
266 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
267 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
268 const void *, unsafe_ptr)
270 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
271 return bpf_probe_read_user_common(dst, size,
272 (__force void __user *)unsafe_ptr);
274 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
277 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
278 .func = bpf_probe_read_compat,
280 .ret_type = RET_INTEGER,
281 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
282 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
283 .arg3_type = ARG_ANYTHING,
286 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
287 const void *, unsafe_ptr)
289 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
290 return bpf_probe_read_user_str_common(dst, size,
291 (__force void __user *)unsafe_ptr);
293 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
296 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
297 .func = bpf_probe_read_compat_str,
299 .ret_type = RET_INTEGER,
300 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
301 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
302 .arg3_type = ARG_ANYTHING,
304 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
306 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
310 * Ensure we're in user context which is safe for the helper to
311 * run. This helper has no business in a kthread.
313 * access_ok() should prevent writing to non-user memory, but in
314 * some situations (nommu, temporary switch, etc) access_ok() does
315 * not provide enough validation, hence the check on KERNEL_DS.
317 * nmi_uaccess_okay() ensures the probe is not run in an interim
318 * state, when the task or mm are switched. This is specifically
319 * required to prevent the use of temporary mm.
322 if (unlikely(in_interrupt() ||
323 current->flags & (PF_KTHREAD | PF_EXITING)))
325 if (unlikely(uaccess_kernel()))
327 if (unlikely(!nmi_uaccess_okay()))
330 return copy_to_user_nofault(unsafe_ptr, src, size);
333 static const struct bpf_func_proto bpf_probe_write_user_proto = {
334 .func = bpf_probe_write_user,
336 .ret_type = RET_INTEGER,
337 .arg1_type = ARG_ANYTHING,
338 .arg2_type = ARG_PTR_TO_MEM,
339 .arg3_type = ARG_CONST_SIZE,
342 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
344 if (!capable(CAP_SYS_ADMIN))
347 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
348 current->comm, task_pid_nr(current));
350 return &bpf_probe_write_user_proto;
353 static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
356 void __user *user_ptr = (__force void __user *)unsafe_ptr;
362 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
363 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
364 strncpy_from_user_nofault(buf, user_ptr, bufsz);
370 strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
373 strncpy_from_user_nofault(buf, user_ptr, bufsz);
379 * Only limited trace_printk() conversion specifiers allowed:
380 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pB %pks %pus %s
382 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
383 u64, arg2, u64, arg3)
385 int i, mod[3] = {}, fmt_cnt = 0;
386 char buf[64], fmt_ptype;
387 void *unsafe_ptr = NULL;
388 bool str_seen = false;
391 * bpf_check()->check_func_arg()->check_stack_boundary()
392 * guarantees that fmt points to bpf program stack,
393 * fmt_size bytes of it were initialized and fmt_size > 0
395 if (fmt[--fmt_size] != 0)
398 /* check format string for allowed specifiers */
399 for (i = 0; i < fmt_size; i++) {
400 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
409 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
414 } else if (fmt[i] == 'p') {
416 if ((fmt[i + 1] == 'k' ||
417 fmt[i + 1] == 'u') &&
419 fmt_ptype = fmt[i + 1];
424 if (fmt[i + 1] == 'B') {
429 /* disallow any further format extensions */
430 if (fmt[i + 1] != 0 &&
431 !isspace(fmt[i + 1]) &&
432 !ispunct(fmt[i + 1]))
436 } else if (fmt[i] == 's') {
441 /* allow only one '%s' per fmt string */
445 if (fmt[i + 1] != 0 &&
446 !isspace(fmt[i + 1]) &&
447 !ispunct(fmt[i + 1]))
452 unsafe_ptr = (void *)(long)arg1;
456 unsafe_ptr = (void *)(long)arg2;
460 unsafe_ptr = (void *)(long)arg3;
465 bpf_trace_copy_string(buf, unsafe_ptr, fmt_ptype,
475 if (fmt[i] != 'i' && fmt[i] != 'd' &&
476 fmt[i] != 'u' && fmt[i] != 'x')
482 /* Horrid workaround for getting va_list handling working with different
483 * argument type combinations generically for 32 and 64 bit archs.
485 #define __BPF_TP_EMIT() __BPF_ARG3_TP()
486 #define __BPF_TP(...) \
487 __trace_printk(0 /* Fake ip */, \
490 #define __BPF_ARG1_TP(...) \
491 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
492 ? __BPF_TP(arg1, ##__VA_ARGS__) \
493 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
494 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
495 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
497 #define __BPF_ARG2_TP(...) \
498 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
499 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
500 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
501 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
502 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
504 #define __BPF_ARG3_TP(...) \
505 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
506 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
507 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
508 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
509 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
511 return __BPF_TP_EMIT();
514 static const struct bpf_func_proto bpf_trace_printk_proto = {
515 .func = bpf_trace_printk,
517 .ret_type = RET_INTEGER,
518 .arg1_type = ARG_PTR_TO_MEM,
519 .arg2_type = ARG_CONST_SIZE,
522 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
525 * this program might be calling bpf_trace_printk,
526 * so allocate per-cpu printk buffers
528 trace_printk_init_buffers();
530 return &bpf_trace_printk_proto;
533 #define MAX_SEQ_PRINTF_VARARGS 12
534 #define MAX_SEQ_PRINTF_MAX_MEMCPY 6
535 #define MAX_SEQ_PRINTF_STR_LEN 128
537 struct bpf_seq_printf_buf {
538 char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN];
540 static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf);
541 static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used);
543 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
544 const void *, data, u32, data_len)
546 int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0;
547 int i, buf_used, copy_size, num_args;
548 u64 params[MAX_SEQ_PRINTF_VARARGS];
549 struct bpf_seq_printf_buf *bufs;
550 const u64 *args = data;
552 buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used);
553 if (WARN_ON_ONCE(buf_used > 1)) {
558 bufs = this_cpu_ptr(&bpf_seq_printf_buf);
561 * bpf_check()->check_func_arg()->check_stack_boundary()
562 * guarantees that fmt points to bpf program stack,
563 * fmt_size bytes of it were initialized and fmt_size > 0
565 if (fmt[--fmt_size] != 0)
571 for (i = 0; i < fmt_size; i++) {
573 if (fmt[i + 1] == '%')
575 else if (!data || !data_len)
580 num_args = data_len / 8;
582 /* check format string for allowed specifiers */
583 for (i = 0; i < fmt_size; i++) {
584 /* only printable ascii for now. */
585 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
593 if (fmt[i + 1] == '%') {
598 if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) {
603 if (fmt_cnt >= num_args) {
608 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
611 /* skip optional "[0 +-][num]" width formating field */
612 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
615 if (fmt[i] >= '1' && fmt[i] <= '9') {
617 while (fmt[i] >= '0' && fmt[i] <= '9')
624 /* try our best to copy */
625 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
630 unsafe_ptr = (void *)(long)args[fmt_cnt];
631 err = strncpy_from_kernel_nofault(bufs->buf[memcpy_cnt],
632 unsafe_ptr, MAX_SEQ_PRINTF_STR_LEN);
634 bufs->buf[memcpy_cnt][0] = '\0';
635 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
643 if (fmt[i + 1] == 0 ||
647 /* just kernel pointers */
648 params[fmt_cnt] = args[fmt_cnt];
653 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
654 if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') {
658 if (fmt[i + 2] != '4' && fmt[i + 2] != '6') {
663 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
669 copy_size = (fmt[i + 2] == '4') ? 4 : 16;
671 err = copy_from_kernel_nofault(bufs->buf[memcpy_cnt],
672 (void *) (long) args[fmt_cnt],
675 memset(bufs->buf[memcpy_cnt], 0, copy_size);
676 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
690 if (fmt[i] != 'i' && fmt[i] != 'd' &&
691 fmt[i] != 'u' && fmt[i] != 'x' &&
697 params[fmt_cnt] = args[fmt_cnt];
701 /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give
702 * all of them to seq_printf().
704 seq_printf(m, fmt, params[0], params[1], params[2], params[3],
705 params[4], params[5], params[6], params[7], params[8],
706 params[9], params[10], params[11]);
708 err = seq_has_overflowed(m) ? -EOVERFLOW : 0;
710 this_cpu_dec(bpf_seq_printf_buf_used);
714 BTF_ID_LIST(bpf_seq_printf_btf_ids)
715 BTF_ID(struct, seq_file)
717 static const struct bpf_func_proto bpf_seq_printf_proto = {
718 .func = bpf_seq_printf,
720 .ret_type = RET_INTEGER,
721 .arg1_type = ARG_PTR_TO_BTF_ID,
722 .arg2_type = ARG_PTR_TO_MEM,
723 .arg3_type = ARG_CONST_SIZE,
724 .arg4_type = ARG_PTR_TO_MEM_OR_NULL,
725 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
726 .btf_id = bpf_seq_printf_btf_ids,
729 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
731 return seq_write(m, data, len) ? -EOVERFLOW : 0;
734 BTF_ID_LIST(bpf_seq_write_btf_ids)
735 BTF_ID(struct, seq_file)
737 static const struct bpf_func_proto bpf_seq_write_proto = {
738 .func = bpf_seq_write,
740 .ret_type = RET_INTEGER,
741 .arg1_type = ARG_PTR_TO_BTF_ID,
742 .arg2_type = ARG_PTR_TO_MEM,
743 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
744 .btf_id = bpf_seq_write_btf_ids,
747 static __always_inline int
748 get_map_perf_counter(struct bpf_map *map, u64 flags,
749 u64 *value, u64 *enabled, u64 *running)
751 struct bpf_array *array = container_of(map, struct bpf_array, map);
752 unsigned int cpu = smp_processor_id();
753 u64 index = flags & BPF_F_INDEX_MASK;
754 struct bpf_event_entry *ee;
756 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
758 if (index == BPF_F_CURRENT_CPU)
760 if (unlikely(index >= array->map.max_entries))
763 ee = READ_ONCE(array->ptrs[index]);
767 return perf_event_read_local(ee->event, value, enabled, running);
770 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
775 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
777 * this api is ugly since we miss [-22..-2] range of valid
778 * counter values, but that's uapi
785 static const struct bpf_func_proto bpf_perf_event_read_proto = {
786 .func = bpf_perf_event_read,
788 .ret_type = RET_INTEGER,
789 .arg1_type = ARG_CONST_MAP_PTR,
790 .arg2_type = ARG_ANYTHING,
793 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
794 struct bpf_perf_event_value *, buf, u32, size)
798 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
800 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
806 memset(buf, 0, size);
810 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
811 .func = bpf_perf_event_read_value,
813 .ret_type = RET_INTEGER,
814 .arg1_type = ARG_CONST_MAP_PTR,
815 .arg2_type = ARG_ANYTHING,
816 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
817 .arg4_type = ARG_CONST_SIZE,
820 static __always_inline u64
821 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
822 u64 flags, struct perf_sample_data *sd)
824 struct bpf_array *array = container_of(map, struct bpf_array, map);
825 unsigned int cpu = smp_processor_id();
826 u64 index = flags & BPF_F_INDEX_MASK;
827 struct bpf_event_entry *ee;
828 struct perf_event *event;
830 if (index == BPF_F_CURRENT_CPU)
832 if (unlikely(index >= array->map.max_entries))
835 ee = READ_ONCE(array->ptrs[index]);
840 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
841 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
844 if (unlikely(event->oncpu != cpu))
847 return perf_event_output(event, sd, regs);
851 * Support executing tracepoints in normal, irq, and nmi context that each call
852 * bpf_perf_event_output
854 struct bpf_trace_sample_data {
855 struct perf_sample_data sds[3];
858 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
859 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
860 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
861 u64, flags, void *, data, u64, size)
863 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
864 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
865 struct perf_raw_record raw = {
871 struct perf_sample_data *sd;
874 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
879 sd = &sds->sds[nest_level - 1];
881 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
886 perf_sample_data_init(sd, 0, 0);
889 err = __bpf_perf_event_output(regs, map, flags, sd);
892 this_cpu_dec(bpf_trace_nest_level);
896 static const struct bpf_func_proto bpf_perf_event_output_proto = {
897 .func = bpf_perf_event_output,
899 .ret_type = RET_INTEGER,
900 .arg1_type = ARG_PTR_TO_CTX,
901 .arg2_type = ARG_CONST_MAP_PTR,
902 .arg3_type = ARG_ANYTHING,
903 .arg4_type = ARG_PTR_TO_MEM,
904 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
907 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
908 struct bpf_nested_pt_regs {
909 struct pt_regs regs[3];
911 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
912 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
914 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
915 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
917 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
918 struct perf_raw_frag frag = {
923 struct perf_raw_record raw = {
926 .next = ctx_size ? &frag : NULL,
932 struct perf_sample_data *sd;
933 struct pt_regs *regs;
936 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
940 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
941 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
943 perf_fetch_caller_regs(regs);
944 perf_sample_data_init(sd, 0, 0);
947 ret = __bpf_perf_event_output(regs, map, flags, sd);
949 this_cpu_dec(bpf_event_output_nest_level);
953 BPF_CALL_0(bpf_get_current_task)
955 return (long) current;
958 const struct bpf_func_proto bpf_get_current_task_proto = {
959 .func = bpf_get_current_task,
961 .ret_type = RET_INTEGER,
964 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
966 struct bpf_array *array = container_of(map, struct bpf_array, map);
969 if (unlikely(idx >= array->map.max_entries))
972 cgrp = READ_ONCE(array->ptrs[idx]);
976 return task_under_cgroup_hierarchy(current, cgrp);
979 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
980 .func = bpf_current_task_under_cgroup,
982 .ret_type = RET_INTEGER,
983 .arg1_type = ARG_CONST_MAP_PTR,
984 .arg2_type = ARG_ANYTHING,
987 struct send_signal_irq_work {
988 struct irq_work irq_work;
989 struct task_struct *task;
994 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
996 static void do_bpf_send_signal(struct irq_work *entry)
998 struct send_signal_irq_work *work;
1000 work = container_of(entry, struct send_signal_irq_work, irq_work);
1001 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
1004 static int bpf_send_signal_common(u32 sig, enum pid_type type)
1006 struct send_signal_irq_work *work = NULL;
1008 /* Similar to bpf_probe_write_user, task needs to be
1009 * in a sound condition and kernel memory access be
1010 * permitted in order to send signal to the current
1013 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
1015 if (unlikely(uaccess_kernel()))
1017 if (unlikely(!nmi_uaccess_okay()))
1020 if (irqs_disabled()) {
1021 /* Do an early check on signal validity. Otherwise,
1022 * the error is lost in deferred irq_work.
1024 if (unlikely(!valid_signal(sig)))
1027 work = this_cpu_ptr(&send_signal_work);
1028 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY)
1031 /* Add the current task, which is the target of sending signal,
1032 * to the irq_work. The current task may change when queued
1033 * irq works get executed.
1035 work->task = current;
1038 irq_work_queue(&work->irq_work);
1042 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
1045 BPF_CALL_1(bpf_send_signal, u32, sig)
1047 return bpf_send_signal_common(sig, PIDTYPE_TGID);
1050 static const struct bpf_func_proto bpf_send_signal_proto = {
1051 .func = bpf_send_signal,
1053 .ret_type = RET_INTEGER,
1054 .arg1_type = ARG_ANYTHING,
1057 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
1059 return bpf_send_signal_common(sig, PIDTYPE_PID);
1062 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
1063 .func = bpf_send_signal_thread,
1065 .ret_type = RET_INTEGER,
1066 .arg1_type = ARG_ANYTHING,
1069 const struct bpf_func_proto *
1070 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1073 case BPF_FUNC_map_lookup_elem:
1074 return &bpf_map_lookup_elem_proto;
1075 case BPF_FUNC_map_update_elem:
1076 return &bpf_map_update_elem_proto;
1077 case BPF_FUNC_map_delete_elem:
1078 return &bpf_map_delete_elem_proto;
1079 case BPF_FUNC_map_push_elem:
1080 return &bpf_map_push_elem_proto;
1081 case BPF_FUNC_map_pop_elem:
1082 return &bpf_map_pop_elem_proto;
1083 case BPF_FUNC_map_peek_elem:
1084 return &bpf_map_peek_elem_proto;
1085 case BPF_FUNC_ktime_get_ns:
1086 return &bpf_ktime_get_ns_proto;
1087 case BPF_FUNC_ktime_get_boot_ns:
1088 return &bpf_ktime_get_boot_ns_proto;
1089 case BPF_FUNC_tail_call:
1090 return &bpf_tail_call_proto;
1091 case BPF_FUNC_get_current_pid_tgid:
1092 return &bpf_get_current_pid_tgid_proto;
1093 case BPF_FUNC_get_current_task:
1094 return &bpf_get_current_task_proto;
1095 case BPF_FUNC_get_current_uid_gid:
1096 return &bpf_get_current_uid_gid_proto;
1097 case BPF_FUNC_get_current_comm:
1098 return &bpf_get_current_comm_proto;
1099 case BPF_FUNC_trace_printk:
1100 return bpf_get_trace_printk_proto();
1101 case BPF_FUNC_get_smp_processor_id:
1102 return &bpf_get_smp_processor_id_proto;
1103 case BPF_FUNC_get_numa_node_id:
1104 return &bpf_get_numa_node_id_proto;
1105 case BPF_FUNC_perf_event_read:
1106 return &bpf_perf_event_read_proto;
1107 case BPF_FUNC_probe_write_user:
1108 return bpf_get_probe_write_proto();
1109 case BPF_FUNC_current_task_under_cgroup:
1110 return &bpf_current_task_under_cgroup_proto;
1111 case BPF_FUNC_get_prandom_u32:
1112 return &bpf_get_prandom_u32_proto;
1113 case BPF_FUNC_probe_read_user:
1114 return &bpf_probe_read_user_proto;
1115 case BPF_FUNC_probe_read_kernel:
1116 return &bpf_probe_read_kernel_proto;
1117 case BPF_FUNC_probe_read_user_str:
1118 return &bpf_probe_read_user_str_proto;
1119 case BPF_FUNC_probe_read_kernel_str:
1120 return &bpf_probe_read_kernel_str_proto;
1121 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1122 case BPF_FUNC_probe_read:
1123 return &bpf_probe_read_compat_proto;
1124 case BPF_FUNC_probe_read_str:
1125 return &bpf_probe_read_compat_str_proto;
1127 #ifdef CONFIG_CGROUPS
1128 case BPF_FUNC_get_current_cgroup_id:
1129 return &bpf_get_current_cgroup_id_proto;
1131 case BPF_FUNC_send_signal:
1132 return &bpf_send_signal_proto;
1133 case BPF_FUNC_send_signal_thread:
1134 return &bpf_send_signal_thread_proto;
1135 case BPF_FUNC_perf_event_read_value:
1136 return &bpf_perf_event_read_value_proto;
1137 case BPF_FUNC_get_ns_current_pid_tgid:
1138 return &bpf_get_ns_current_pid_tgid_proto;
1139 case BPF_FUNC_ringbuf_output:
1140 return &bpf_ringbuf_output_proto;
1141 case BPF_FUNC_ringbuf_reserve:
1142 return &bpf_ringbuf_reserve_proto;
1143 case BPF_FUNC_ringbuf_submit:
1144 return &bpf_ringbuf_submit_proto;
1145 case BPF_FUNC_ringbuf_discard:
1146 return &bpf_ringbuf_discard_proto;
1147 case BPF_FUNC_ringbuf_query:
1148 return &bpf_ringbuf_query_proto;
1149 case BPF_FUNC_jiffies64:
1150 return &bpf_jiffies64_proto;
1151 case BPF_FUNC_get_task_stack:
1152 return &bpf_get_task_stack_proto;
1158 static const struct bpf_func_proto *
1159 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1162 case BPF_FUNC_perf_event_output:
1163 return &bpf_perf_event_output_proto;
1164 case BPF_FUNC_get_stackid:
1165 return &bpf_get_stackid_proto;
1166 case BPF_FUNC_get_stack:
1167 return &bpf_get_stack_proto;
1168 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1169 case BPF_FUNC_override_return:
1170 return &bpf_override_return_proto;
1173 return bpf_tracing_func_proto(func_id, prog);
1177 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1178 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1179 const struct bpf_prog *prog,
1180 struct bpf_insn_access_aux *info)
1182 if (off < 0 || off >= sizeof(struct pt_regs))
1184 if (type != BPF_READ)
1186 if (off % size != 0)
1189 * Assertion for 32 bit to make sure last 8 byte access
1190 * (BPF_DW) to the last 4 byte member is disallowed.
1192 if (off + size > sizeof(struct pt_regs))
1198 const struct bpf_verifier_ops kprobe_verifier_ops = {
1199 .get_func_proto = kprobe_prog_func_proto,
1200 .is_valid_access = kprobe_prog_is_valid_access,
1203 const struct bpf_prog_ops kprobe_prog_ops = {
1206 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1207 u64, flags, void *, data, u64, size)
1209 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1212 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1213 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1214 * from there and call the same bpf_perf_event_output() helper inline.
1216 return ____bpf_perf_event_output(regs, map, flags, data, size);
1219 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1220 .func = bpf_perf_event_output_tp,
1222 .ret_type = RET_INTEGER,
1223 .arg1_type = ARG_PTR_TO_CTX,
1224 .arg2_type = ARG_CONST_MAP_PTR,
1225 .arg3_type = ARG_ANYTHING,
1226 .arg4_type = ARG_PTR_TO_MEM,
1227 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1230 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1233 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1236 * Same comment as in bpf_perf_event_output_tp(), only that this time
1237 * the other helper's function body cannot be inlined due to being
1238 * external, thus we need to call raw helper function.
1240 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1244 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1245 .func = bpf_get_stackid_tp,
1247 .ret_type = RET_INTEGER,
1248 .arg1_type = ARG_PTR_TO_CTX,
1249 .arg2_type = ARG_CONST_MAP_PTR,
1250 .arg3_type = ARG_ANYTHING,
1253 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1256 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1258 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1259 (unsigned long) size, flags, 0);
1262 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1263 .func = bpf_get_stack_tp,
1265 .ret_type = RET_INTEGER,
1266 .arg1_type = ARG_PTR_TO_CTX,
1267 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1268 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1269 .arg4_type = ARG_ANYTHING,
1272 static const struct bpf_func_proto *
1273 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1276 case BPF_FUNC_perf_event_output:
1277 return &bpf_perf_event_output_proto_tp;
1278 case BPF_FUNC_get_stackid:
1279 return &bpf_get_stackid_proto_tp;
1280 case BPF_FUNC_get_stack:
1281 return &bpf_get_stack_proto_tp;
1283 return bpf_tracing_func_proto(func_id, prog);
1287 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1288 const struct bpf_prog *prog,
1289 struct bpf_insn_access_aux *info)
1291 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1293 if (type != BPF_READ)
1295 if (off % size != 0)
1298 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1302 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1303 .get_func_proto = tp_prog_func_proto,
1304 .is_valid_access = tp_prog_is_valid_access,
1307 const struct bpf_prog_ops tracepoint_prog_ops = {
1310 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1311 struct bpf_perf_event_value *, buf, u32, size)
1315 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1317 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1323 memset(buf, 0, size);
1327 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1328 .func = bpf_perf_prog_read_value,
1330 .ret_type = RET_INTEGER,
1331 .arg1_type = ARG_PTR_TO_CTX,
1332 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1333 .arg3_type = ARG_CONST_SIZE,
1336 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1337 void *, buf, u32, size, u64, flags)
1342 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1343 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1346 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1349 if (unlikely(!br_stack))
1352 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1353 return br_stack->nr * br_entry_size;
1355 if (!buf || (size % br_entry_size != 0))
1358 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1359 memcpy(buf, br_stack->entries, to_copy);
1365 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1366 .func = bpf_read_branch_records,
1368 .ret_type = RET_INTEGER,
1369 .arg1_type = ARG_PTR_TO_CTX,
1370 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1371 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1372 .arg4_type = ARG_ANYTHING,
1375 static const struct bpf_func_proto *
1376 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1379 case BPF_FUNC_perf_event_output:
1380 return &bpf_perf_event_output_proto_tp;
1381 case BPF_FUNC_get_stackid:
1382 return &bpf_get_stackid_proto_tp;
1383 case BPF_FUNC_get_stack:
1384 return &bpf_get_stack_proto_tp;
1385 case BPF_FUNC_perf_prog_read_value:
1386 return &bpf_perf_prog_read_value_proto;
1387 case BPF_FUNC_read_branch_records:
1388 return &bpf_read_branch_records_proto;
1390 return bpf_tracing_func_proto(func_id, prog);
1395 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1396 * to avoid potential recursive reuse issue when/if tracepoints are added
1397 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1399 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1400 * in normal, irq, and nmi context.
1402 struct bpf_raw_tp_regs {
1403 struct pt_regs regs[3];
1405 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1406 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1407 static struct pt_regs *get_bpf_raw_tp_regs(void)
1409 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1410 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1412 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1413 this_cpu_dec(bpf_raw_tp_nest_level);
1414 return ERR_PTR(-EBUSY);
1417 return &tp_regs->regs[nest_level - 1];
1420 static void put_bpf_raw_tp_regs(void)
1422 this_cpu_dec(bpf_raw_tp_nest_level);
1425 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1426 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1428 struct pt_regs *regs = get_bpf_raw_tp_regs();
1432 return PTR_ERR(regs);
1434 perf_fetch_caller_regs(regs);
1435 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1437 put_bpf_raw_tp_regs();
1441 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1442 .func = bpf_perf_event_output_raw_tp,
1444 .ret_type = RET_INTEGER,
1445 .arg1_type = ARG_PTR_TO_CTX,
1446 .arg2_type = ARG_CONST_MAP_PTR,
1447 .arg3_type = ARG_ANYTHING,
1448 .arg4_type = ARG_PTR_TO_MEM,
1449 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1452 extern const struct bpf_func_proto bpf_skb_output_proto;
1453 extern const struct bpf_func_proto bpf_xdp_output_proto;
1455 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1456 struct bpf_map *, map, u64, flags)
1458 struct pt_regs *regs = get_bpf_raw_tp_regs();
1462 return PTR_ERR(regs);
1464 perf_fetch_caller_regs(regs);
1465 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1466 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1468 put_bpf_raw_tp_regs();
1472 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1473 .func = bpf_get_stackid_raw_tp,
1475 .ret_type = RET_INTEGER,
1476 .arg1_type = ARG_PTR_TO_CTX,
1477 .arg2_type = ARG_CONST_MAP_PTR,
1478 .arg3_type = ARG_ANYTHING,
1481 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1482 void *, buf, u32, size, u64, flags)
1484 struct pt_regs *regs = get_bpf_raw_tp_regs();
1488 return PTR_ERR(regs);
1490 perf_fetch_caller_regs(regs);
1491 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1492 (unsigned long) size, flags, 0);
1493 put_bpf_raw_tp_regs();
1497 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1498 .func = bpf_get_stack_raw_tp,
1500 .ret_type = RET_INTEGER,
1501 .arg1_type = ARG_PTR_TO_CTX,
1502 .arg2_type = ARG_PTR_TO_MEM,
1503 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1504 .arg4_type = ARG_ANYTHING,
1507 static const struct bpf_func_proto *
1508 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1511 case BPF_FUNC_perf_event_output:
1512 return &bpf_perf_event_output_proto_raw_tp;
1513 case BPF_FUNC_get_stackid:
1514 return &bpf_get_stackid_proto_raw_tp;
1515 case BPF_FUNC_get_stack:
1516 return &bpf_get_stack_proto_raw_tp;
1518 return bpf_tracing_func_proto(func_id, prog);
1522 const struct bpf_func_proto *
1523 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1527 case BPF_FUNC_skb_output:
1528 return &bpf_skb_output_proto;
1529 case BPF_FUNC_xdp_output:
1530 return &bpf_xdp_output_proto;
1531 case BPF_FUNC_skc_to_tcp6_sock:
1532 return &bpf_skc_to_tcp6_sock_proto;
1533 case BPF_FUNC_skc_to_tcp_sock:
1534 return &bpf_skc_to_tcp_sock_proto;
1535 case BPF_FUNC_skc_to_tcp_timewait_sock:
1536 return &bpf_skc_to_tcp_timewait_sock_proto;
1537 case BPF_FUNC_skc_to_tcp_request_sock:
1538 return &bpf_skc_to_tcp_request_sock_proto;
1539 case BPF_FUNC_skc_to_udp6_sock:
1540 return &bpf_skc_to_udp6_sock_proto;
1542 case BPF_FUNC_seq_printf:
1543 return prog->expected_attach_type == BPF_TRACE_ITER ?
1544 &bpf_seq_printf_proto :
1546 case BPF_FUNC_seq_write:
1547 return prog->expected_attach_type == BPF_TRACE_ITER ?
1548 &bpf_seq_write_proto :
1551 return raw_tp_prog_func_proto(func_id, prog);
1555 static bool raw_tp_prog_is_valid_access(int off, int size,
1556 enum bpf_access_type type,
1557 const struct bpf_prog *prog,
1558 struct bpf_insn_access_aux *info)
1560 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1562 if (type != BPF_READ)
1564 if (off % size != 0)
1569 static bool tracing_prog_is_valid_access(int off, int size,
1570 enum bpf_access_type type,
1571 const struct bpf_prog *prog,
1572 struct bpf_insn_access_aux *info)
1574 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1576 if (type != BPF_READ)
1578 if (off % size != 0)
1580 return btf_ctx_access(off, size, type, prog, info);
1583 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1584 const union bpf_attr *kattr,
1585 union bpf_attr __user *uattr)
1590 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1591 .get_func_proto = raw_tp_prog_func_proto,
1592 .is_valid_access = raw_tp_prog_is_valid_access,
1595 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1598 const struct bpf_verifier_ops tracing_verifier_ops = {
1599 .get_func_proto = tracing_prog_func_proto,
1600 .is_valid_access = tracing_prog_is_valid_access,
1603 const struct bpf_prog_ops tracing_prog_ops = {
1604 .test_run = bpf_prog_test_run_tracing,
1607 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1608 enum bpf_access_type type,
1609 const struct bpf_prog *prog,
1610 struct bpf_insn_access_aux *info)
1613 if (size != sizeof(u64) || type != BPF_READ)
1615 info->reg_type = PTR_TO_TP_BUFFER;
1617 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1620 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1621 .get_func_proto = raw_tp_prog_func_proto,
1622 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1625 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1628 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1629 const struct bpf_prog *prog,
1630 struct bpf_insn_access_aux *info)
1632 const int size_u64 = sizeof(u64);
1634 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1636 if (type != BPF_READ)
1638 if (off % size != 0) {
1639 if (sizeof(unsigned long) != 4)
1643 if (off % size != 4)
1648 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
1649 bpf_ctx_record_field_size(info, size_u64);
1650 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1653 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1654 bpf_ctx_record_field_size(info, size_u64);
1655 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1659 if (size != sizeof(long))
1666 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1667 const struct bpf_insn *si,
1668 struct bpf_insn *insn_buf,
1669 struct bpf_prog *prog, u32 *target_size)
1671 struct bpf_insn *insn = insn_buf;
1674 case offsetof(struct bpf_perf_event_data, sample_period):
1675 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1676 data), si->dst_reg, si->src_reg,
1677 offsetof(struct bpf_perf_event_data_kern, data));
1678 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1679 bpf_target_off(struct perf_sample_data, period, 8,
1682 case offsetof(struct bpf_perf_event_data, addr):
1683 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1684 data), si->dst_reg, si->src_reg,
1685 offsetof(struct bpf_perf_event_data_kern, data));
1686 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1687 bpf_target_off(struct perf_sample_data, addr, 8,
1691 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1692 regs), si->dst_reg, si->src_reg,
1693 offsetof(struct bpf_perf_event_data_kern, regs));
1694 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1699 return insn - insn_buf;
1702 const struct bpf_verifier_ops perf_event_verifier_ops = {
1703 .get_func_proto = pe_prog_func_proto,
1704 .is_valid_access = pe_prog_is_valid_access,
1705 .convert_ctx_access = pe_prog_convert_ctx_access,
1708 const struct bpf_prog_ops perf_event_prog_ops = {
1711 static DEFINE_MUTEX(bpf_event_mutex);
1713 #define BPF_TRACE_MAX_PROGS 64
1715 int perf_event_attach_bpf_prog(struct perf_event *event,
1716 struct bpf_prog *prog)
1718 struct bpf_prog_array *old_array;
1719 struct bpf_prog_array *new_array;
1723 * Kprobe override only works if they are on the function entry,
1724 * and only if they are on the opt-in list.
1726 if (prog->kprobe_override &&
1727 (!trace_kprobe_on_func_entry(event->tp_event) ||
1728 !trace_kprobe_error_injectable(event->tp_event)))
1731 mutex_lock(&bpf_event_mutex);
1736 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1738 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1743 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1747 /* set the new array to event->tp_event and set event->prog */
1749 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1750 bpf_prog_array_free(old_array);
1753 mutex_unlock(&bpf_event_mutex);
1757 void perf_event_detach_bpf_prog(struct perf_event *event)
1759 struct bpf_prog_array *old_array;
1760 struct bpf_prog_array *new_array;
1763 mutex_lock(&bpf_event_mutex);
1768 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1769 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
1773 bpf_prog_array_delete_safe(old_array, event->prog);
1775 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1776 bpf_prog_array_free(old_array);
1779 bpf_prog_put(event->prog);
1783 mutex_unlock(&bpf_event_mutex);
1786 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
1788 struct perf_event_query_bpf __user *uquery = info;
1789 struct perf_event_query_bpf query = {};
1790 struct bpf_prog_array *progs;
1791 u32 *ids, prog_cnt, ids_len;
1794 if (!perfmon_capable())
1796 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1798 if (copy_from_user(&query, uquery, sizeof(query)))
1801 ids_len = query.ids_len;
1802 if (ids_len > BPF_TRACE_MAX_PROGS)
1804 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1808 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1809 * is required when user only wants to check for uquery->prog_cnt.
1810 * There is no need to check for it since the case is handled
1811 * gracefully in bpf_prog_array_copy_info.
1814 mutex_lock(&bpf_event_mutex);
1815 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
1816 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
1817 mutex_unlock(&bpf_event_mutex);
1819 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1820 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1827 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1828 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1830 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
1832 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1834 for (; btp < __stop__bpf_raw_tp; btp++) {
1835 if (!strcmp(btp->tp->name, name))
1839 return bpf_get_raw_tracepoint_module(name);
1842 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
1844 struct module *mod = __module_address((unsigned long)btp);
1850 static __always_inline
1851 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1855 (void) BPF_PROG_RUN(prog, args);
1859 #define UNPACK(...) __VA_ARGS__
1860 #define REPEAT_1(FN, DL, X, ...) FN(X)
1861 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1862 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1863 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1864 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1865 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1866 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1867 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1868 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1869 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1870 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1871 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1872 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1874 #define SARG(X) u64 arg##X
1875 #define COPY(X) args[X] = arg##X
1877 #define __DL_COM (,)
1878 #define __DL_SEM (;)
1880 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1882 #define BPF_TRACE_DEFN_x(x) \
1883 void bpf_trace_run##x(struct bpf_prog *prog, \
1884 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1887 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1888 __bpf_trace_run(prog, args); \
1890 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1891 BPF_TRACE_DEFN_x(1);
1892 BPF_TRACE_DEFN_x(2);
1893 BPF_TRACE_DEFN_x(3);
1894 BPF_TRACE_DEFN_x(4);
1895 BPF_TRACE_DEFN_x(5);
1896 BPF_TRACE_DEFN_x(6);
1897 BPF_TRACE_DEFN_x(7);
1898 BPF_TRACE_DEFN_x(8);
1899 BPF_TRACE_DEFN_x(9);
1900 BPF_TRACE_DEFN_x(10);
1901 BPF_TRACE_DEFN_x(11);
1902 BPF_TRACE_DEFN_x(12);
1904 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1906 struct tracepoint *tp = btp->tp;
1909 * check that program doesn't access arguments beyond what's
1910 * available in this tracepoint
1912 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1915 if (prog->aux->max_tp_access > btp->writable_size)
1918 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1921 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1923 return __bpf_probe_register(btp, prog);
1926 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1928 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
1931 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1932 u32 *fd_type, const char **buf,
1933 u64 *probe_offset, u64 *probe_addr)
1935 bool is_tracepoint, is_syscall_tp;
1936 struct bpf_prog *prog;
1943 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1944 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1947 *prog_id = prog->aux->id;
1948 flags = event->tp_event->flags;
1949 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1950 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1952 if (is_tracepoint || is_syscall_tp) {
1953 *buf = is_tracepoint ? event->tp_event->tp->name
1954 : event->tp_event->name;
1955 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1956 *probe_offset = 0x0;
1961 #ifdef CONFIG_KPROBE_EVENTS
1962 if (flags & TRACE_EVENT_FL_KPROBE)
1963 err = bpf_get_kprobe_info(event, fd_type, buf,
1964 probe_offset, probe_addr,
1965 event->attr.type == PERF_TYPE_TRACEPOINT);
1967 #ifdef CONFIG_UPROBE_EVENTS
1968 if (flags & TRACE_EVENT_FL_UPROBE)
1969 err = bpf_get_uprobe_info(event, fd_type, buf,
1971 event->attr.type == PERF_TYPE_TRACEPOINT);
1978 static int __init send_signal_irq_work_init(void)
1981 struct send_signal_irq_work *work;
1983 for_each_possible_cpu(cpu) {
1984 work = per_cpu_ptr(&send_signal_work, cpu);
1985 init_irq_work(&work->irq_work, do_bpf_send_signal);
1990 subsys_initcall(send_signal_irq_work_init);
1992 #ifdef CONFIG_MODULES
1993 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
1996 struct bpf_trace_module *btm, *tmp;
1997 struct module *mod = module;
1999 if (mod->num_bpf_raw_events == 0 ||
2000 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2003 mutex_lock(&bpf_module_mutex);
2006 case MODULE_STATE_COMING:
2007 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2009 btm->module = module;
2010 list_add(&btm->list, &bpf_trace_modules);
2013 case MODULE_STATE_GOING:
2014 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2015 if (btm->module == module) {
2016 list_del(&btm->list);
2024 mutex_unlock(&bpf_module_mutex);
2029 static struct notifier_block bpf_module_nb = {
2030 .notifier_call = bpf_event_notify,
2033 static int __init bpf_event_init(void)
2035 register_module_notifier(&bpf_module_nb);
2039 fs_initcall(bpf_event_init);
2040 #endif /* CONFIG_MODULES */