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>
19 #include "trace_probe.h"
22 #define bpf_event_rcu_dereference(p) \
23 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
26 struct bpf_trace_module {
27 struct module *module;
28 struct list_head list;
31 static LIST_HEAD(bpf_trace_modules);
32 static DEFINE_MUTEX(bpf_module_mutex);
34 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
36 struct bpf_raw_event_map *btp, *ret = NULL;
37 struct bpf_trace_module *btm;
40 mutex_lock(&bpf_module_mutex);
41 list_for_each_entry(btm, &bpf_trace_modules, list) {
42 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
43 btp = &btm->module->bpf_raw_events[i];
44 if (!strcmp(btp->tp->name, name)) {
45 if (try_module_get(btm->module))
52 mutex_unlock(&bpf_module_mutex);
56 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
60 #endif /* CONFIG_MODULES */
62 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
63 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
66 * trace_call_bpf - invoke BPF program
67 * @call: tracepoint event
68 * @ctx: opaque context pointer
70 * kprobe handlers execute BPF programs via this helper.
71 * Can be used from static tracepoints in the future.
73 * Return: BPF programs always return an integer which is interpreted by
75 * 0 - return from kprobe (event is filtered out)
76 * 1 - store kprobe event into ring buffer
77 * Other values are reserved and currently alias to 1
79 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
83 if (in_nmi()) /* not supported yet */
88 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
90 * since some bpf program is already running on this cpu,
91 * don't call into another bpf program (same or different)
92 * and don't send kprobe event into ring-buffer,
100 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
101 * to all call sites, we did a bpf_prog_array_valid() there to check
102 * whether call->prog_array is empty or not, which is
103 * a heurisitc to speed up execution.
105 * If bpf_prog_array_valid() fetched prog_array was
106 * non-NULL, we go into trace_call_bpf() and do the actual
107 * proper rcu_dereference() under RCU lock.
108 * If it turns out that prog_array is NULL then, we bail out.
109 * For the opposite, if the bpf_prog_array_valid() fetched pointer
110 * was NULL, you'll skip the prog_array with the risk of missing
111 * out of events when it was updated in between this and the
112 * rcu_dereference() which is accepted risk.
114 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
117 __this_cpu_dec(bpf_prog_active);
122 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
123 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
125 regs_set_return_value(regs, rc);
126 override_function_with_return(regs);
130 static const struct bpf_func_proto bpf_override_return_proto = {
131 .func = bpf_override_return,
133 .ret_type = RET_INTEGER,
134 .arg1_type = ARG_PTR_TO_CTX,
135 .arg2_type = ARG_ANYTHING,
139 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
140 const void __user *, unsafe_ptr)
142 int ret = probe_user_read(dst, unsafe_ptr, size);
144 if (unlikely(ret < 0))
145 memset(dst, 0, size);
150 const struct bpf_func_proto bpf_probe_read_user_proto = {
151 .func = bpf_probe_read_user,
153 .ret_type = RET_INTEGER,
154 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
155 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
156 .arg3_type = ARG_ANYTHING,
159 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
160 const void __user *, unsafe_ptr)
162 int ret = strncpy_from_unsafe_user(dst, unsafe_ptr, size);
164 if (unlikely(ret < 0))
165 memset(dst, 0, size);
170 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
171 .func = bpf_probe_read_user_str,
173 .ret_type = RET_INTEGER,
174 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
175 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
176 .arg3_type = ARG_ANYTHING,
179 static __always_inline int
180 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr,
183 int ret = security_locked_down(LOCKDOWN_BPF_READ);
185 if (unlikely(ret < 0))
187 ret = compat ? probe_kernel_read(dst, unsafe_ptr, size) :
188 probe_kernel_read_strict(dst, unsafe_ptr, size);
189 if (unlikely(ret < 0))
191 memset(dst, 0, size);
195 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
196 const void *, unsafe_ptr)
198 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false);
201 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
202 .func = bpf_probe_read_kernel,
204 .ret_type = RET_INTEGER,
205 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
206 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
207 .arg3_type = ARG_ANYTHING,
210 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
211 const void *, unsafe_ptr)
213 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, true);
216 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
217 .func = bpf_probe_read_compat,
219 .ret_type = RET_INTEGER,
220 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
221 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
222 .arg3_type = ARG_ANYTHING,
225 static __always_inline int
226 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,
229 int ret = security_locked_down(LOCKDOWN_BPF_READ);
231 if (unlikely(ret < 0))
234 * The strncpy_from_unsafe_*() call will likely not fill the entire
235 * buffer, but that's okay in this circumstance as we're probing
236 * arbitrary memory anyway similar to bpf_probe_read_*() and might
237 * as well probe the stack. Thus, memory is explicitly cleared
238 * only in error case, so that improper users ignoring return
239 * code altogether don't copy garbage; otherwise length of string
240 * is returned that can be used for bpf_perf_event_output() et al.
242 ret = compat ? strncpy_from_unsafe(dst, unsafe_ptr, size) :
243 strncpy_from_unsafe_strict(dst, unsafe_ptr, size);
244 if (unlikely(ret < 0))
246 memset(dst, 0, size);
250 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
251 const void *, unsafe_ptr)
253 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false);
256 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
257 .func = bpf_probe_read_kernel_str,
259 .ret_type = RET_INTEGER,
260 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
261 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
262 .arg3_type = ARG_ANYTHING,
265 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
266 const void *, unsafe_ptr)
268 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, true);
271 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
272 .func = bpf_probe_read_compat_str,
274 .ret_type = RET_INTEGER,
275 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
276 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
277 .arg3_type = ARG_ANYTHING,
280 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
284 * Ensure we're in user context which is safe for the helper to
285 * run. This helper has no business in a kthread.
287 * access_ok() should prevent writing to non-user memory, but in
288 * some situations (nommu, temporary switch, etc) access_ok() does
289 * not provide enough validation, hence the check on KERNEL_DS.
291 * nmi_uaccess_okay() ensures the probe is not run in an interim
292 * state, when the task or mm are switched. This is specifically
293 * required to prevent the use of temporary mm.
296 if (unlikely(in_interrupt() ||
297 current->flags & (PF_KTHREAD | PF_EXITING)))
299 if (unlikely(uaccess_kernel()))
301 if (unlikely(!nmi_uaccess_okay()))
304 return probe_user_write(unsafe_ptr, src, size);
307 static const struct bpf_func_proto bpf_probe_write_user_proto = {
308 .func = bpf_probe_write_user,
310 .ret_type = RET_INTEGER,
311 .arg1_type = ARG_ANYTHING,
312 .arg2_type = ARG_PTR_TO_MEM,
313 .arg3_type = ARG_CONST_SIZE,
316 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
318 if (!capable(CAP_SYS_ADMIN))
321 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
322 current->comm, task_pid_nr(current));
324 return &bpf_probe_write_user_proto;
328 * Only limited trace_printk() conversion specifiers allowed:
329 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pks %pus %s
331 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
332 u64, arg2, u64, arg3)
334 int i, mod[3] = {}, fmt_cnt = 0;
335 char buf[64], fmt_ptype;
336 void *unsafe_ptr = NULL;
337 bool str_seen = false;
340 * bpf_check()->check_func_arg()->check_stack_boundary()
341 * guarantees that fmt points to bpf program stack,
342 * fmt_size bytes of it were initialized and fmt_size > 0
344 if (fmt[--fmt_size] != 0)
347 /* check format string for allowed specifiers */
348 for (i = 0; i < fmt_size; i++) {
349 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
358 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
363 } else if (fmt[i] == 'p') {
365 if ((fmt[i + 1] == 'k' ||
366 fmt[i + 1] == 'u') &&
368 fmt_ptype = fmt[i + 1];
373 /* disallow any further format extensions */
374 if (fmt[i + 1] != 0 &&
375 !isspace(fmt[i + 1]) &&
376 !ispunct(fmt[i + 1]))
380 } else if (fmt[i] == 's') {
385 /* allow only one '%s' per fmt string */
389 if (fmt[i + 1] != 0 &&
390 !isspace(fmt[i + 1]) &&
391 !ispunct(fmt[i + 1]))
396 unsafe_ptr = (void *)(long)arg1;
400 unsafe_ptr = (void *)(long)arg2;
404 unsafe_ptr = (void *)(long)arg3;
412 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
413 strncpy_from_unsafe(buf, unsafe_ptr,
418 strncpy_from_unsafe_strict(buf, unsafe_ptr,
422 strncpy_from_unsafe_user(buf,
423 (__force void __user *)unsafe_ptr,
435 if (fmt[i] != 'i' && fmt[i] != 'd' &&
436 fmt[i] != 'u' && fmt[i] != 'x')
442 /* Horrid workaround for getting va_list handling working with different
443 * argument type combinations generically for 32 and 64 bit archs.
445 #define __BPF_TP_EMIT() __BPF_ARG3_TP()
446 #define __BPF_TP(...) \
447 __trace_printk(0 /* Fake ip */, \
450 #define __BPF_ARG1_TP(...) \
451 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
452 ? __BPF_TP(arg1, ##__VA_ARGS__) \
453 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
454 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
455 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
457 #define __BPF_ARG2_TP(...) \
458 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
459 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
460 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
461 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
462 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
464 #define __BPF_ARG3_TP(...) \
465 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
466 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
467 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
468 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
469 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
471 return __BPF_TP_EMIT();
474 static const struct bpf_func_proto bpf_trace_printk_proto = {
475 .func = bpf_trace_printk,
477 .ret_type = RET_INTEGER,
478 .arg1_type = ARG_PTR_TO_MEM,
479 .arg2_type = ARG_CONST_SIZE,
482 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
485 * this program might be calling bpf_trace_printk,
486 * so allocate per-cpu printk buffers
488 trace_printk_init_buffers();
490 return &bpf_trace_printk_proto;
493 #define MAX_SEQ_PRINTF_VARARGS 12
494 #define MAX_SEQ_PRINTF_MAX_MEMCPY 6
495 #define MAX_SEQ_PRINTF_STR_LEN 128
497 struct bpf_seq_printf_buf {
498 char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN];
500 static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf);
501 static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used);
503 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
504 const void *, data, u32, data_len)
506 int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0;
507 int i, buf_used, copy_size, num_args;
508 u64 params[MAX_SEQ_PRINTF_VARARGS];
509 struct bpf_seq_printf_buf *bufs;
510 const u64 *args = data;
512 buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used);
513 if (WARN_ON_ONCE(buf_used > 1)) {
518 bufs = this_cpu_ptr(&bpf_seq_printf_buf);
521 * bpf_check()->check_func_arg()->check_stack_boundary()
522 * guarantees that fmt points to bpf program stack,
523 * fmt_size bytes of it were initialized and fmt_size > 0
525 if (fmt[--fmt_size] != 0)
531 for (i = 0; i < fmt_size; i++) {
533 if (fmt[i + 1] == '%')
535 else if (!data || !data_len)
540 num_args = data_len / 8;
542 /* check format string for allowed specifiers */
543 for (i = 0; i < fmt_size; i++) {
544 /* only printable ascii for now. */
545 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
553 if (fmt[i + 1] == '%') {
558 if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) {
563 if (fmt_cnt >= num_args) {
568 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
571 /* skip optional "[0 +-][num]" width formating field */
572 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
575 if (fmt[i] >= '1' && fmt[i] <= '9') {
577 while (fmt[i] >= '0' && fmt[i] <= '9')
582 /* try our best to copy */
583 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
588 err = strncpy_from_unsafe(bufs->buf[memcpy_cnt],
589 (void *) (long) args[fmt_cnt],
590 MAX_SEQ_PRINTF_STR_LEN);
592 bufs->buf[memcpy_cnt][0] = '\0';
593 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
601 if (fmt[i + 1] == 0 ||
604 /* just kernel pointers */
605 params[fmt_cnt] = args[fmt_cnt];
610 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
611 if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') {
615 if (fmt[i + 2] != '4' && fmt[i + 2] != '6') {
620 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
626 copy_size = (fmt[i + 2] == '4') ? 4 : 16;
628 err = probe_kernel_read(bufs->buf[memcpy_cnt],
629 (void *) (long) args[fmt_cnt],
632 memset(bufs->buf[memcpy_cnt], 0, copy_size);
633 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
647 if (fmt[i] != 'i' && fmt[i] != 'd' &&
648 fmt[i] != 'u' && fmt[i] != 'x') {
653 params[fmt_cnt] = args[fmt_cnt];
657 /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give
658 * all of them to seq_printf().
660 seq_printf(m, fmt, params[0], params[1], params[2], params[3],
661 params[4], params[5], params[6], params[7], params[8],
662 params[9], params[10], params[11]);
664 err = seq_has_overflowed(m) ? -EOVERFLOW : 0;
666 this_cpu_dec(bpf_seq_printf_buf_used);
670 static int bpf_seq_printf_btf_ids[5];
671 static const struct bpf_func_proto bpf_seq_printf_proto = {
672 .func = bpf_seq_printf,
674 .ret_type = RET_INTEGER,
675 .arg1_type = ARG_PTR_TO_BTF_ID,
676 .arg2_type = ARG_PTR_TO_MEM,
677 .arg3_type = ARG_CONST_SIZE,
678 .arg4_type = ARG_PTR_TO_MEM_OR_NULL,
679 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
680 .btf_id = bpf_seq_printf_btf_ids,
683 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
685 return seq_write(m, data, len) ? -EOVERFLOW : 0;
688 static int bpf_seq_write_btf_ids[5];
689 static const struct bpf_func_proto bpf_seq_write_proto = {
690 .func = bpf_seq_write,
692 .ret_type = RET_INTEGER,
693 .arg1_type = ARG_PTR_TO_BTF_ID,
694 .arg2_type = ARG_PTR_TO_MEM,
695 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
696 .btf_id = bpf_seq_write_btf_ids,
699 static __always_inline int
700 get_map_perf_counter(struct bpf_map *map, u64 flags,
701 u64 *value, u64 *enabled, u64 *running)
703 struct bpf_array *array = container_of(map, struct bpf_array, map);
704 unsigned int cpu = smp_processor_id();
705 u64 index = flags & BPF_F_INDEX_MASK;
706 struct bpf_event_entry *ee;
708 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
710 if (index == BPF_F_CURRENT_CPU)
712 if (unlikely(index >= array->map.max_entries))
715 ee = READ_ONCE(array->ptrs[index]);
719 return perf_event_read_local(ee->event, value, enabled, running);
722 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
727 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
729 * this api is ugly since we miss [-22..-2] range of valid
730 * counter values, but that's uapi
737 static const struct bpf_func_proto bpf_perf_event_read_proto = {
738 .func = bpf_perf_event_read,
740 .ret_type = RET_INTEGER,
741 .arg1_type = ARG_CONST_MAP_PTR,
742 .arg2_type = ARG_ANYTHING,
745 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
746 struct bpf_perf_event_value *, buf, u32, size)
750 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
752 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
758 memset(buf, 0, size);
762 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
763 .func = bpf_perf_event_read_value,
765 .ret_type = RET_INTEGER,
766 .arg1_type = ARG_CONST_MAP_PTR,
767 .arg2_type = ARG_ANYTHING,
768 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
769 .arg4_type = ARG_CONST_SIZE,
772 static __always_inline u64
773 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
774 u64 flags, struct perf_sample_data *sd)
776 struct bpf_array *array = container_of(map, struct bpf_array, map);
777 unsigned int cpu = smp_processor_id();
778 u64 index = flags & BPF_F_INDEX_MASK;
779 struct bpf_event_entry *ee;
780 struct perf_event *event;
782 if (index == BPF_F_CURRENT_CPU)
784 if (unlikely(index >= array->map.max_entries))
787 ee = READ_ONCE(array->ptrs[index]);
792 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
793 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
796 if (unlikely(event->oncpu != cpu))
799 return perf_event_output(event, sd, regs);
803 * Support executing tracepoints in normal, irq, and nmi context that each call
804 * bpf_perf_event_output
806 struct bpf_trace_sample_data {
807 struct perf_sample_data sds[3];
810 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
811 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
812 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
813 u64, flags, void *, data, u64, size)
815 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
816 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
817 struct perf_raw_record raw = {
823 struct perf_sample_data *sd;
826 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
831 sd = &sds->sds[nest_level - 1];
833 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
838 perf_sample_data_init(sd, 0, 0);
841 err = __bpf_perf_event_output(regs, map, flags, sd);
844 this_cpu_dec(bpf_trace_nest_level);
848 static const struct bpf_func_proto bpf_perf_event_output_proto = {
849 .func = bpf_perf_event_output,
851 .ret_type = RET_INTEGER,
852 .arg1_type = ARG_PTR_TO_CTX,
853 .arg2_type = ARG_CONST_MAP_PTR,
854 .arg3_type = ARG_ANYTHING,
855 .arg4_type = ARG_PTR_TO_MEM,
856 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
859 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
860 struct bpf_nested_pt_regs {
861 struct pt_regs regs[3];
863 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
864 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
866 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
867 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
869 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
870 struct perf_raw_frag frag = {
875 struct perf_raw_record raw = {
878 .next = ctx_size ? &frag : NULL,
884 struct perf_sample_data *sd;
885 struct pt_regs *regs;
888 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
892 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
893 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
895 perf_fetch_caller_regs(regs);
896 perf_sample_data_init(sd, 0, 0);
899 ret = __bpf_perf_event_output(regs, map, flags, sd);
901 this_cpu_dec(bpf_event_output_nest_level);
905 BPF_CALL_0(bpf_get_current_task)
907 return (long) current;
910 const struct bpf_func_proto bpf_get_current_task_proto = {
911 .func = bpf_get_current_task,
913 .ret_type = RET_INTEGER,
916 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
918 struct bpf_array *array = container_of(map, struct bpf_array, map);
921 if (unlikely(idx >= array->map.max_entries))
924 cgrp = READ_ONCE(array->ptrs[idx]);
928 return task_under_cgroup_hierarchy(current, cgrp);
931 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
932 .func = bpf_current_task_under_cgroup,
934 .ret_type = RET_INTEGER,
935 .arg1_type = ARG_CONST_MAP_PTR,
936 .arg2_type = ARG_ANYTHING,
939 struct send_signal_irq_work {
940 struct irq_work irq_work;
941 struct task_struct *task;
946 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
948 static void do_bpf_send_signal(struct irq_work *entry)
950 struct send_signal_irq_work *work;
952 work = container_of(entry, struct send_signal_irq_work, irq_work);
953 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
956 static int bpf_send_signal_common(u32 sig, enum pid_type type)
958 struct send_signal_irq_work *work = NULL;
960 /* Similar to bpf_probe_write_user, task needs to be
961 * in a sound condition and kernel memory access be
962 * permitted in order to send signal to the current
965 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
967 if (unlikely(uaccess_kernel()))
969 if (unlikely(!nmi_uaccess_okay()))
972 if (irqs_disabled()) {
973 /* Do an early check on signal validity. Otherwise,
974 * the error is lost in deferred irq_work.
976 if (unlikely(!valid_signal(sig)))
979 work = this_cpu_ptr(&send_signal_work);
980 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY)
983 /* Add the current task, which is the target of sending signal,
984 * to the irq_work. The current task may change when queued
985 * irq works get executed.
987 work->task = current;
990 irq_work_queue(&work->irq_work);
994 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
997 BPF_CALL_1(bpf_send_signal, u32, sig)
999 return bpf_send_signal_common(sig, PIDTYPE_TGID);
1002 static const struct bpf_func_proto bpf_send_signal_proto = {
1003 .func = bpf_send_signal,
1005 .ret_type = RET_INTEGER,
1006 .arg1_type = ARG_ANYTHING,
1009 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
1011 return bpf_send_signal_common(sig, PIDTYPE_PID);
1014 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
1015 .func = bpf_send_signal_thread,
1017 .ret_type = RET_INTEGER,
1018 .arg1_type = ARG_ANYTHING,
1021 const struct bpf_func_proto *
1022 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1025 case BPF_FUNC_map_lookup_elem:
1026 return &bpf_map_lookup_elem_proto;
1027 case BPF_FUNC_map_update_elem:
1028 return &bpf_map_update_elem_proto;
1029 case BPF_FUNC_map_delete_elem:
1030 return &bpf_map_delete_elem_proto;
1031 case BPF_FUNC_map_push_elem:
1032 return &bpf_map_push_elem_proto;
1033 case BPF_FUNC_map_pop_elem:
1034 return &bpf_map_pop_elem_proto;
1035 case BPF_FUNC_map_peek_elem:
1036 return &bpf_map_peek_elem_proto;
1037 case BPF_FUNC_ktime_get_ns:
1038 return &bpf_ktime_get_ns_proto;
1039 case BPF_FUNC_ktime_get_boot_ns:
1040 return &bpf_ktime_get_boot_ns_proto;
1041 case BPF_FUNC_tail_call:
1042 return &bpf_tail_call_proto;
1043 case BPF_FUNC_get_current_pid_tgid:
1044 return &bpf_get_current_pid_tgid_proto;
1045 case BPF_FUNC_get_current_task:
1046 return &bpf_get_current_task_proto;
1047 case BPF_FUNC_get_current_uid_gid:
1048 return &bpf_get_current_uid_gid_proto;
1049 case BPF_FUNC_get_current_comm:
1050 return &bpf_get_current_comm_proto;
1051 case BPF_FUNC_trace_printk:
1052 return bpf_get_trace_printk_proto();
1053 case BPF_FUNC_get_smp_processor_id:
1054 return &bpf_get_smp_processor_id_proto;
1055 case BPF_FUNC_get_numa_node_id:
1056 return &bpf_get_numa_node_id_proto;
1057 case BPF_FUNC_perf_event_read:
1058 return &bpf_perf_event_read_proto;
1059 case BPF_FUNC_probe_write_user:
1060 return bpf_get_probe_write_proto();
1061 case BPF_FUNC_current_task_under_cgroup:
1062 return &bpf_current_task_under_cgroup_proto;
1063 case BPF_FUNC_get_prandom_u32:
1064 return &bpf_get_prandom_u32_proto;
1065 case BPF_FUNC_probe_read_user:
1066 return &bpf_probe_read_user_proto;
1067 case BPF_FUNC_probe_read_kernel:
1068 return &bpf_probe_read_kernel_proto;
1069 case BPF_FUNC_probe_read_user_str:
1070 return &bpf_probe_read_user_str_proto;
1071 case BPF_FUNC_probe_read_kernel_str:
1072 return &bpf_probe_read_kernel_str_proto;
1073 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1074 case BPF_FUNC_probe_read:
1075 return &bpf_probe_read_compat_proto;
1076 case BPF_FUNC_probe_read_str:
1077 return &bpf_probe_read_compat_str_proto;
1079 #ifdef CONFIG_CGROUPS
1080 case BPF_FUNC_get_current_cgroup_id:
1081 return &bpf_get_current_cgroup_id_proto;
1083 case BPF_FUNC_send_signal:
1084 return &bpf_send_signal_proto;
1085 case BPF_FUNC_send_signal_thread:
1086 return &bpf_send_signal_thread_proto;
1087 case BPF_FUNC_perf_event_read_value:
1088 return &bpf_perf_event_read_value_proto;
1089 case BPF_FUNC_get_ns_current_pid_tgid:
1090 return &bpf_get_ns_current_pid_tgid_proto;
1096 static const struct bpf_func_proto *
1097 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1100 case BPF_FUNC_perf_event_output:
1101 return &bpf_perf_event_output_proto;
1102 case BPF_FUNC_get_stackid:
1103 return &bpf_get_stackid_proto;
1104 case BPF_FUNC_get_stack:
1105 return &bpf_get_stack_proto;
1106 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1107 case BPF_FUNC_override_return:
1108 return &bpf_override_return_proto;
1111 return bpf_tracing_func_proto(func_id, prog);
1115 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1116 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1117 const struct bpf_prog *prog,
1118 struct bpf_insn_access_aux *info)
1120 if (off < 0 || off >= sizeof(struct pt_regs))
1122 if (type != BPF_READ)
1124 if (off % size != 0)
1127 * Assertion for 32 bit to make sure last 8 byte access
1128 * (BPF_DW) to the last 4 byte member is disallowed.
1130 if (off + size > sizeof(struct pt_regs))
1136 const struct bpf_verifier_ops kprobe_verifier_ops = {
1137 .get_func_proto = kprobe_prog_func_proto,
1138 .is_valid_access = kprobe_prog_is_valid_access,
1141 const struct bpf_prog_ops kprobe_prog_ops = {
1144 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1145 u64, flags, void *, data, u64, size)
1147 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1150 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1151 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1152 * from there and call the same bpf_perf_event_output() helper inline.
1154 return ____bpf_perf_event_output(regs, map, flags, data, size);
1157 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1158 .func = bpf_perf_event_output_tp,
1160 .ret_type = RET_INTEGER,
1161 .arg1_type = ARG_PTR_TO_CTX,
1162 .arg2_type = ARG_CONST_MAP_PTR,
1163 .arg3_type = ARG_ANYTHING,
1164 .arg4_type = ARG_PTR_TO_MEM,
1165 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1168 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1171 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1174 * Same comment as in bpf_perf_event_output_tp(), only that this time
1175 * the other helper's function body cannot be inlined due to being
1176 * external, thus we need to call raw helper function.
1178 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1182 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1183 .func = bpf_get_stackid_tp,
1185 .ret_type = RET_INTEGER,
1186 .arg1_type = ARG_PTR_TO_CTX,
1187 .arg2_type = ARG_CONST_MAP_PTR,
1188 .arg3_type = ARG_ANYTHING,
1191 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1194 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1196 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1197 (unsigned long) size, flags, 0);
1200 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1201 .func = bpf_get_stack_tp,
1203 .ret_type = RET_INTEGER,
1204 .arg1_type = ARG_PTR_TO_CTX,
1205 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1206 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1207 .arg4_type = ARG_ANYTHING,
1210 static const struct bpf_func_proto *
1211 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1214 case BPF_FUNC_perf_event_output:
1215 return &bpf_perf_event_output_proto_tp;
1216 case BPF_FUNC_get_stackid:
1217 return &bpf_get_stackid_proto_tp;
1218 case BPF_FUNC_get_stack:
1219 return &bpf_get_stack_proto_tp;
1221 return bpf_tracing_func_proto(func_id, prog);
1225 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1226 const struct bpf_prog *prog,
1227 struct bpf_insn_access_aux *info)
1229 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1231 if (type != BPF_READ)
1233 if (off % size != 0)
1236 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1240 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1241 .get_func_proto = tp_prog_func_proto,
1242 .is_valid_access = tp_prog_is_valid_access,
1245 const struct bpf_prog_ops tracepoint_prog_ops = {
1248 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1249 struct bpf_perf_event_value *, buf, u32, size)
1253 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1255 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1261 memset(buf, 0, size);
1265 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1266 .func = bpf_perf_prog_read_value,
1268 .ret_type = RET_INTEGER,
1269 .arg1_type = ARG_PTR_TO_CTX,
1270 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1271 .arg3_type = ARG_CONST_SIZE,
1274 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1275 void *, buf, u32, size, u64, flags)
1280 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1281 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1284 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1287 if (unlikely(!br_stack))
1290 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1291 return br_stack->nr * br_entry_size;
1293 if (!buf || (size % br_entry_size != 0))
1296 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1297 memcpy(buf, br_stack->entries, to_copy);
1303 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1304 .func = bpf_read_branch_records,
1306 .ret_type = RET_INTEGER,
1307 .arg1_type = ARG_PTR_TO_CTX,
1308 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1309 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1310 .arg4_type = ARG_ANYTHING,
1313 static const struct bpf_func_proto *
1314 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1317 case BPF_FUNC_perf_event_output:
1318 return &bpf_perf_event_output_proto_tp;
1319 case BPF_FUNC_get_stackid:
1320 return &bpf_get_stackid_proto_tp;
1321 case BPF_FUNC_get_stack:
1322 return &bpf_get_stack_proto_tp;
1323 case BPF_FUNC_perf_prog_read_value:
1324 return &bpf_perf_prog_read_value_proto;
1325 case BPF_FUNC_read_branch_records:
1326 return &bpf_read_branch_records_proto;
1328 return bpf_tracing_func_proto(func_id, prog);
1333 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1334 * to avoid potential recursive reuse issue when/if tracepoints are added
1335 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1337 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1338 * in normal, irq, and nmi context.
1340 struct bpf_raw_tp_regs {
1341 struct pt_regs regs[3];
1343 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1344 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1345 static struct pt_regs *get_bpf_raw_tp_regs(void)
1347 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1348 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1350 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1351 this_cpu_dec(bpf_raw_tp_nest_level);
1352 return ERR_PTR(-EBUSY);
1355 return &tp_regs->regs[nest_level - 1];
1358 static void put_bpf_raw_tp_regs(void)
1360 this_cpu_dec(bpf_raw_tp_nest_level);
1363 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1364 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1366 struct pt_regs *regs = get_bpf_raw_tp_regs();
1370 return PTR_ERR(regs);
1372 perf_fetch_caller_regs(regs);
1373 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1375 put_bpf_raw_tp_regs();
1379 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1380 .func = bpf_perf_event_output_raw_tp,
1382 .ret_type = RET_INTEGER,
1383 .arg1_type = ARG_PTR_TO_CTX,
1384 .arg2_type = ARG_CONST_MAP_PTR,
1385 .arg3_type = ARG_ANYTHING,
1386 .arg4_type = ARG_PTR_TO_MEM,
1387 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1390 extern const struct bpf_func_proto bpf_skb_output_proto;
1391 extern const struct bpf_func_proto bpf_xdp_output_proto;
1393 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1394 struct bpf_map *, map, u64, flags)
1396 struct pt_regs *regs = get_bpf_raw_tp_regs();
1400 return PTR_ERR(regs);
1402 perf_fetch_caller_regs(regs);
1403 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1404 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1406 put_bpf_raw_tp_regs();
1410 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1411 .func = bpf_get_stackid_raw_tp,
1413 .ret_type = RET_INTEGER,
1414 .arg1_type = ARG_PTR_TO_CTX,
1415 .arg2_type = ARG_CONST_MAP_PTR,
1416 .arg3_type = ARG_ANYTHING,
1419 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1420 void *, buf, u32, size, u64, flags)
1422 struct pt_regs *regs = get_bpf_raw_tp_regs();
1426 return PTR_ERR(regs);
1428 perf_fetch_caller_regs(regs);
1429 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1430 (unsigned long) size, flags, 0);
1431 put_bpf_raw_tp_regs();
1435 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1436 .func = bpf_get_stack_raw_tp,
1438 .ret_type = RET_INTEGER,
1439 .arg1_type = ARG_PTR_TO_CTX,
1440 .arg2_type = ARG_PTR_TO_MEM,
1441 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1442 .arg4_type = ARG_ANYTHING,
1445 static const struct bpf_func_proto *
1446 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1449 case BPF_FUNC_perf_event_output:
1450 return &bpf_perf_event_output_proto_raw_tp;
1451 case BPF_FUNC_get_stackid:
1452 return &bpf_get_stackid_proto_raw_tp;
1453 case BPF_FUNC_get_stack:
1454 return &bpf_get_stack_proto_raw_tp;
1456 return bpf_tracing_func_proto(func_id, prog);
1460 static const struct bpf_func_proto *
1461 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1465 case BPF_FUNC_skb_output:
1466 return &bpf_skb_output_proto;
1467 case BPF_FUNC_xdp_output:
1468 return &bpf_xdp_output_proto;
1470 case BPF_FUNC_seq_printf:
1471 return prog->expected_attach_type == BPF_TRACE_ITER ?
1472 &bpf_seq_printf_proto :
1474 case BPF_FUNC_seq_write:
1475 return prog->expected_attach_type == BPF_TRACE_ITER ?
1476 &bpf_seq_write_proto :
1479 return raw_tp_prog_func_proto(func_id, prog);
1483 static bool raw_tp_prog_is_valid_access(int off, int size,
1484 enum bpf_access_type type,
1485 const struct bpf_prog *prog,
1486 struct bpf_insn_access_aux *info)
1488 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1490 if (type != BPF_READ)
1492 if (off % size != 0)
1497 static bool tracing_prog_is_valid_access(int off, int size,
1498 enum bpf_access_type type,
1499 const struct bpf_prog *prog,
1500 struct bpf_insn_access_aux *info)
1502 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1504 if (type != BPF_READ)
1506 if (off % size != 0)
1508 return btf_ctx_access(off, size, type, prog, info);
1511 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1512 const union bpf_attr *kattr,
1513 union bpf_attr __user *uattr)
1518 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1519 .get_func_proto = raw_tp_prog_func_proto,
1520 .is_valid_access = raw_tp_prog_is_valid_access,
1523 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1526 const struct bpf_verifier_ops tracing_verifier_ops = {
1527 .get_func_proto = tracing_prog_func_proto,
1528 .is_valid_access = tracing_prog_is_valid_access,
1531 const struct bpf_prog_ops tracing_prog_ops = {
1532 .test_run = bpf_prog_test_run_tracing,
1535 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1536 enum bpf_access_type type,
1537 const struct bpf_prog *prog,
1538 struct bpf_insn_access_aux *info)
1541 if (size != sizeof(u64) || type != BPF_READ)
1543 info->reg_type = PTR_TO_TP_BUFFER;
1545 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1548 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1549 .get_func_proto = raw_tp_prog_func_proto,
1550 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1553 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1556 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1557 const struct bpf_prog *prog,
1558 struct bpf_insn_access_aux *info)
1560 const int size_u64 = sizeof(u64);
1562 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1564 if (type != BPF_READ)
1566 if (off % size != 0) {
1567 if (sizeof(unsigned long) != 4)
1571 if (off % size != 4)
1576 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
1577 bpf_ctx_record_field_size(info, size_u64);
1578 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1581 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1582 bpf_ctx_record_field_size(info, size_u64);
1583 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1587 if (size != sizeof(long))
1594 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1595 const struct bpf_insn *si,
1596 struct bpf_insn *insn_buf,
1597 struct bpf_prog *prog, u32 *target_size)
1599 struct bpf_insn *insn = insn_buf;
1602 case offsetof(struct bpf_perf_event_data, sample_period):
1603 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1604 data), si->dst_reg, si->src_reg,
1605 offsetof(struct bpf_perf_event_data_kern, data));
1606 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1607 bpf_target_off(struct perf_sample_data, period, 8,
1610 case offsetof(struct bpf_perf_event_data, addr):
1611 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1612 data), si->dst_reg, si->src_reg,
1613 offsetof(struct bpf_perf_event_data_kern, data));
1614 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1615 bpf_target_off(struct perf_sample_data, addr, 8,
1619 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1620 regs), si->dst_reg, si->src_reg,
1621 offsetof(struct bpf_perf_event_data_kern, regs));
1622 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1627 return insn - insn_buf;
1630 const struct bpf_verifier_ops perf_event_verifier_ops = {
1631 .get_func_proto = pe_prog_func_proto,
1632 .is_valid_access = pe_prog_is_valid_access,
1633 .convert_ctx_access = pe_prog_convert_ctx_access,
1636 const struct bpf_prog_ops perf_event_prog_ops = {
1639 static DEFINE_MUTEX(bpf_event_mutex);
1641 #define BPF_TRACE_MAX_PROGS 64
1643 int perf_event_attach_bpf_prog(struct perf_event *event,
1644 struct bpf_prog *prog)
1646 struct bpf_prog_array *old_array;
1647 struct bpf_prog_array *new_array;
1651 * Kprobe override only works if they are on the function entry,
1652 * and only if they are on the opt-in list.
1654 if (prog->kprobe_override &&
1655 (!trace_kprobe_on_func_entry(event->tp_event) ||
1656 !trace_kprobe_error_injectable(event->tp_event)))
1659 mutex_lock(&bpf_event_mutex);
1664 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1666 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1671 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1675 /* set the new array to event->tp_event and set event->prog */
1677 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1678 bpf_prog_array_free(old_array);
1681 mutex_unlock(&bpf_event_mutex);
1685 void perf_event_detach_bpf_prog(struct perf_event *event)
1687 struct bpf_prog_array *old_array;
1688 struct bpf_prog_array *new_array;
1691 mutex_lock(&bpf_event_mutex);
1696 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1697 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
1701 bpf_prog_array_delete_safe(old_array, event->prog);
1703 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1704 bpf_prog_array_free(old_array);
1707 bpf_prog_put(event->prog);
1711 mutex_unlock(&bpf_event_mutex);
1714 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
1716 struct perf_event_query_bpf __user *uquery = info;
1717 struct perf_event_query_bpf query = {};
1718 struct bpf_prog_array *progs;
1719 u32 *ids, prog_cnt, ids_len;
1722 if (!capable(CAP_SYS_ADMIN))
1724 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1726 if (copy_from_user(&query, uquery, sizeof(query)))
1729 ids_len = query.ids_len;
1730 if (ids_len > BPF_TRACE_MAX_PROGS)
1732 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1736 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1737 * is required when user only wants to check for uquery->prog_cnt.
1738 * There is no need to check for it since the case is handled
1739 * gracefully in bpf_prog_array_copy_info.
1742 mutex_lock(&bpf_event_mutex);
1743 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
1744 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
1745 mutex_unlock(&bpf_event_mutex);
1747 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1748 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1755 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1756 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1758 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
1760 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1762 for (; btp < __stop__bpf_raw_tp; btp++) {
1763 if (!strcmp(btp->tp->name, name))
1767 return bpf_get_raw_tracepoint_module(name);
1770 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
1772 struct module *mod = __module_address((unsigned long)btp);
1778 static __always_inline
1779 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1783 (void) BPF_PROG_RUN(prog, args);
1787 #define UNPACK(...) __VA_ARGS__
1788 #define REPEAT_1(FN, DL, X, ...) FN(X)
1789 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1790 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1791 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1792 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1793 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1794 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1795 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1796 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1797 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1798 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1799 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1800 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1802 #define SARG(X) u64 arg##X
1803 #define COPY(X) args[X] = arg##X
1805 #define __DL_COM (,)
1806 #define __DL_SEM (;)
1808 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1810 #define BPF_TRACE_DEFN_x(x) \
1811 void bpf_trace_run##x(struct bpf_prog *prog, \
1812 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1815 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1816 __bpf_trace_run(prog, args); \
1818 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1819 BPF_TRACE_DEFN_x(1);
1820 BPF_TRACE_DEFN_x(2);
1821 BPF_TRACE_DEFN_x(3);
1822 BPF_TRACE_DEFN_x(4);
1823 BPF_TRACE_DEFN_x(5);
1824 BPF_TRACE_DEFN_x(6);
1825 BPF_TRACE_DEFN_x(7);
1826 BPF_TRACE_DEFN_x(8);
1827 BPF_TRACE_DEFN_x(9);
1828 BPF_TRACE_DEFN_x(10);
1829 BPF_TRACE_DEFN_x(11);
1830 BPF_TRACE_DEFN_x(12);
1832 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1834 struct tracepoint *tp = btp->tp;
1837 * check that program doesn't access arguments beyond what's
1838 * available in this tracepoint
1840 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1843 if (prog->aux->max_tp_access > btp->writable_size)
1846 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1849 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1851 return __bpf_probe_register(btp, prog);
1854 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1856 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
1859 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1860 u32 *fd_type, const char **buf,
1861 u64 *probe_offset, u64 *probe_addr)
1863 bool is_tracepoint, is_syscall_tp;
1864 struct bpf_prog *prog;
1871 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1872 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1875 *prog_id = prog->aux->id;
1876 flags = event->tp_event->flags;
1877 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1878 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1880 if (is_tracepoint || is_syscall_tp) {
1881 *buf = is_tracepoint ? event->tp_event->tp->name
1882 : event->tp_event->name;
1883 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1884 *probe_offset = 0x0;
1889 #ifdef CONFIG_KPROBE_EVENTS
1890 if (flags & TRACE_EVENT_FL_KPROBE)
1891 err = bpf_get_kprobe_info(event, fd_type, buf,
1892 probe_offset, probe_addr,
1893 event->attr.type == PERF_TYPE_TRACEPOINT);
1895 #ifdef CONFIG_UPROBE_EVENTS
1896 if (flags & TRACE_EVENT_FL_UPROBE)
1897 err = bpf_get_uprobe_info(event, fd_type, buf,
1899 event->attr.type == PERF_TYPE_TRACEPOINT);
1906 static int __init send_signal_irq_work_init(void)
1909 struct send_signal_irq_work *work;
1911 for_each_possible_cpu(cpu) {
1912 work = per_cpu_ptr(&send_signal_work, cpu);
1913 init_irq_work(&work->irq_work, do_bpf_send_signal);
1918 subsys_initcall(send_signal_irq_work_init);
1920 #ifdef CONFIG_MODULES
1921 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
1924 struct bpf_trace_module *btm, *tmp;
1925 struct module *mod = module;
1927 if (mod->num_bpf_raw_events == 0 ||
1928 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
1931 mutex_lock(&bpf_module_mutex);
1934 case MODULE_STATE_COMING:
1935 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
1937 btm->module = module;
1938 list_add(&btm->list, &bpf_trace_modules);
1941 case MODULE_STATE_GOING:
1942 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
1943 if (btm->module == module) {
1944 list_del(&btm->list);
1952 mutex_unlock(&bpf_module_mutex);
1957 static struct notifier_block bpf_module_nb = {
1958 .notifier_call = bpf_event_notify,
1961 static int __init bpf_event_init(void)
1963 register_module_notifier(&bpf_module_nb);
1967 fs_initcall(bpf_event_init);
1968 #endif /* CONFIG_MODULES */