1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <linux/slab.h>
9 #include <linux/bpf_perf_event.h>
10 #include <linux/btf.h>
11 #include <linux/filter.h>
12 #include <linux/uaccess.h>
13 #include <linux/ctype.h>
14 #include <linux/kprobes.h>
15 #include <linux/spinlock.h>
16 #include <linux/syscalls.h>
17 #include <linux/error-injection.h>
18 #include <linux/btf_ids.h>
19 #include <linux/bpf_lsm.h>
20 #include <linux/fprobe.h>
21 #include <linux/bsearch.h>
22 #include <linux/sort.h>
24 #include <net/bpf_sk_storage.h>
26 #include <uapi/linux/bpf.h>
27 #include <uapi/linux/btf.h>
31 #include "trace_probe.h"
34 #define CREATE_TRACE_POINTS
35 #include "bpf_trace.h"
37 #define bpf_event_rcu_dereference(p) \
38 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
41 struct bpf_trace_module {
42 struct module *module;
43 struct list_head list;
46 static LIST_HEAD(bpf_trace_modules);
47 static DEFINE_MUTEX(bpf_module_mutex);
49 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
51 struct bpf_raw_event_map *btp, *ret = NULL;
52 struct bpf_trace_module *btm;
55 mutex_lock(&bpf_module_mutex);
56 list_for_each_entry(btm, &bpf_trace_modules, list) {
57 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
58 btp = &btm->module->bpf_raw_events[i];
59 if (!strcmp(btp->tp->name, name)) {
60 if (try_module_get(btm->module))
67 mutex_unlock(&bpf_module_mutex);
71 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
75 #endif /* CONFIG_MODULES */
77 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
78 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
80 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
81 u64 flags, const struct btf **btf,
83 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
84 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
87 * trace_call_bpf - invoke BPF program
88 * @call: tracepoint event
89 * @ctx: opaque context pointer
91 * kprobe handlers execute BPF programs via this helper.
92 * Can be used from static tracepoints in the future.
94 * Return: BPF programs always return an integer which is interpreted by
96 * 0 - return from kprobe (event is filtered out)
97 * 1 - store kprobe event into ring buffer
98 * Other values are reserved and currently alias to 1
100 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
106 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
108 * since some bpf program is already running on this cpu,
109 * don't call into another bpf program (same or different)
110 * and don't send kprobe event into ring-buffer,
111 * so return zero here
118 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
119 * to all call sites, we did a bpf_prog_array_valid() there to check
120 * whether call->prog_array is empty or not, which is
121 * a heuristic to speed up execution.
123 * If bpf_prog_array_valid() fetched prog_array was
124 * non-NULL, we go into trace_call_bpf() and do the actual
125 * proper rcu_dereference() under RCU lock.
126 * If it turns out that prog_array is NULL then, we bail out.
127 * For the opposite, if the bpf_prog_array_valid() fetched pointer
128 * was NULL, you'll skip the prog_array with the risk of missing
129 * out of events when it was updated in between this and the
130 * rcu_dereference() which is accepted risk.
133 ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
138 __this_cpu_dec(bpf_prog_active);
143 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
144 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
146 regs_set_return_value(regs, rc);
147 override_function_with_return(regs);
151 static const struct bpf_func_proto bpf_override_return_proto = {
152 .func = bpf_override_return,
154 .ret_type = RET_INTEGER,
155 .arg1_type = ARG_PTR_TO_CTX,
156 .arg2_type = ARG_ANYTHING,
160 static __always_inline int
161 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
165 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
166 if (unlikely(ret < 0))
167 memset(dst, 0, size);
171 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
172 const void __user *, unsafe_ptr)
174 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
177 const struct bpf_func_proto bpf_probe_read_user_proto = {
178 .func = bpf_probe_read_user,
180 .ret_type = RET_INTEGER,
181 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
182 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
183 .arg3_type = ARG_ANYTHING,
186 static __always_inline int
187 bpf_probe_read_user_str_common(void *dst, u32 size,
188 const void __user *unsafe_ptr)
193 * NB: We rely on strncpy_from_user() not copying junk past the NUL
194 * terminator into `dst`.
196 * strncpy_from_user() does long-sized strides in the fast path. If the
197 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
198 * then there could be junk after the NUL in `dst`. If user takes `dst`
199 * and keys a hash map with it, then semantically identical strings can
200 * occupy multiple entries in the map.
202 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
203 if (unlikely(ret < 0))
204 memset(dst, 0, size);
208 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
209 const void __user *, unsafe_ptr)
211 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
214 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
215 .func = bpf_probe_read_user_str,
217 .ret_type = RET_INTEGER,
218 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
219 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
220 .arg3_type = ARG_ANYTHING,
223 static __always_inline int
224 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
228 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
229 if (unlikely(ret < 0))
230 memset(dst, 0, size);
234 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
235 const void *, unsafe_ptr)
237 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
240 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
241 .func = bpf_probe_read_kernel,
243 .ret_type = RET_INTEGER,
244 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
245 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
246 .arg3_type = ARG_ANYTHING,
249 static __always_inline int
250 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
255 * The strncpy_from_kernel_nofault() call will likely not fill the
256 * entire buffer, but that's okay in this circumstance as we're probing
257 * arbitrary memory anyway similar to bpf_probe_read_*() and might
258 * as well probe the stack. Thus, memory is explicitly cleared
259 * only in error case, so that improper users ignoring return
260 * code altogether don't copy garbage; otherwise length of string
261 * is returned that can be used for bpf_perf_event_output() et al.
263 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
264 if (unlikely(ret < 0))
265 memset(dst, 0, size);
269 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
270 const void *, unsafe_ptr)
272 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
275 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
276 .func = bpf_probe_read_kernel_str,
278 .ret_type = RET_INTEGER,
279 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
280 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
281 .arg3_type = ARG_ANYTHING,
284 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
285 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
286 const void *, unsafe_ptr)
288 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
289 return bpf_probe_read_user_common(dst, size,
290 (__force void __user *)unsafe_ptr);
292 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
295 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
296 .func = bpf_probe_read_compat,
298 .ret_type = RET_INTEGER,
299 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
300 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
301 .arg3_type = ARG_ANYTHING,
304 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
305 const void *, unsafe_ptr)
307 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
308 return bpf_probe_read_user_str_common(dst, size,
309 (__force void __user *)unsafe_ptr);
311 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
314 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
315 .func = bpf_probe_read_compat_str,
317 .ret_type = RET_INTEGER,
318 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
319 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
320 .arg3_type = ARG_ANYTHING,
322 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
324 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
328 * Ensure we're in user context which is safe for the helper to
329 * run. This helper has no business in a kthread.
331 * access_ok() should prevent writing to non-user memory, but in
332 * some situations (nommu, temporary switch, etc) access_ok() does
333 * not provide enough validation, hence the check on KERNEL_DS.
335 * nmi_uaccess_okay() ensures the probe is not run in an interim
336 * state, when the task or mm are switched. This is specifically
337 * required to prevent the use of temporary mm.
340 if (unlikely(in_interrupt() ||
341 current->flags & (PF_KTHREAD | PF_EXITING)))
343 if (unlikely(!nmi_uaccess_okay()))
346 return copy_to_user_nofault(unsafe_ptr, src, size);
349 static const struct bpf_func_proto bpf_probe_write_user_proto = {
350 .func = bpf_probe_write_user,
352 .ret_type = RET_INTEGER,
353 .arg1_type = ARG_ANYTHING,
354 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
355 .arg3_type = ARG_CONST_SIZE,
358 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
360 if (!capable(CAP_SYS_ADMIN))
363 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
364 current->comm, task_pid_nr(current));
366 return &bpf_probe_write_user_proto;
369 static DEFINE_RAW_SPINLOCK(trace_printk_lock);
371 #define MAX_TRACE_PRINTK_VARARGS 3
372 #define BPF_TRACE_PRINTK_SIZE 1024
374 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
375 u64, arg2, u64, arg3)
377 u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
379 static char buf[BPF_TRACE_PRINTK_SIZE];
383 ret = bpf_bprintf_prepare(fmt, fmt_size, args, &bin_args,
384 MAX_TRACE_PRINTK_VARARGS);
388 raw_spin_lock_irqsave(&trace_printk_lock, flags);
389 ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
391 trace_bpf_trace_printk(buf);
392 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
394 bpf_bprintf_cleanup();
399 static const struct bpf_func_proto bpf_trace_printk_proto = {
400 .func = bpf_trace_printk,
402 .ret_type = RET_INTEGER,
403 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
404 .arg2_type = ARG_CONST_SIZE,
407 static void __set_printk_clr_event(void)
410 * This program might be calling bpf_trace_printk,
411 * so enable the associated bpf_trace/bpf_trace_printk event.
412 * Repeat this each time as it is possible a user has
413 * disabled bpf_trace_printk events. By loading a program
414 * calling bpf_trace_printk() however the user has expressed
415 * the intent to see such events.
417 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
418 pr_warn_ratelimited("could not enable bpf_trace_printk events");
421 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
423 __set_printk_clr_event();
424 return &bpf_trace_printk_proto;
427 BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, data,
430 static char buf[BPF_TRACE_PRINTK_SIZE];
435 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
438 num_args = data_len / 8;
440 ret = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
444 raw_spin_lock_irqsave(&trace_printk_lock, flags);
445 ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
447 trace_bpf_trace_printk(buf);
448 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
450 bpf_bprintf_cleanup();
455 static const struct bpf_func_proto bpf_trace_vprintk_proto = {
456 .func = bpf_trace_vprintk,
458 .ret_type = RET_INTEGER,
459 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
460 .arg2_type = ARG_CONST_SIZE,
461 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
462 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
465 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
467 __set_printk_clr_event();
468 return &bpf_trace_vprintk_proto;
471 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
472 const void *, data, u32, data_len)
477 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
480 num_args = data_len / 8;
482 err = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
486 seq_bprintf(m, fmt, bin_args);
488 bpf_bprintf_cleanup();
490 return seq_has_overflowed(m) ? -EOVERFLOW : 0;
493 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
495 static const struct bpf_func_proto bpf_seq_printf_proto = {
496 .func = bpf_seq_printf,
498 .ret_type = RET_INTEGER,
499 .arg1_type = ARG_PTR_TO_BTF_ID,
500 .arg1_btf_id = &btf_seq_file_ids[0],
501 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
502 .arg3_type = ARG_CONST_SIZE,
503 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
504 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
507 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
509 return seq_write(m, data, len) ? -EOVERFLOW : 0;
512 static const struct bpf_func_proto bpf_seq_write_proto = {
513 .func = bpf_seq_write,
515 .ret_type = RET_INTEGER,
516 .arg1_type = ARG_PTR_TO_BTF_ID,
517 .arg1_btf_id = &btf_seq_file_ids[0],
518 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
519 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
522 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
523 u32, btf_ptr_size, u64, flags)
525 const struct btf *btf;
529 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
533 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
536 static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
537 .func = bpf_seq_printf_btf,
539 .ret_type = RET_INTEGER,
540 .arg1_type = ARG_PTR_TO_BTF_ID,
541 .arg1_btf_id = &btf_seq_file_ids[0],
542 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
543 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
544 .arg4_type = ARG_ANYTHING,
547 static __always_inline int
548 get_map_perf_counter(struct bpf_map *map, u64 flags,
549 u64 *value, u64 *enabled, u64 *running)
551 struct bpf_array *array = container_of(map, struct bpf_array, map);
552 unsigned int cpu = smp_processor_id();
553 u64 index = flags & BPF_F_INDEX_MASK;
554 struct bpf_event_entry *ee;
556 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
558 if (index == BPF_F_CURRENT_CPU)
560 if (unlikely(index >= array->map.max_entries))
563 ee = READ_ONCE(array->ptrs[index]);
567 return perf_event_read_local(ee->event, value, enabled, running);
570 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
575 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
577 * this api is ugly since we miss [-22..-2] range of valid
578 * counter values, but that's uapi
585 static const struct bpf_func_proto bpf_perf_event_read_proto = {
586 .func = bpf_perf_event_read,
588 .ret_type = RET_INTEGER,
589 .arg1_type = ARG_CONST_MAP_PTR,
590 .arg2_type = ARG_ANYTHING,
593 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
594 struct bpf_perf_event_value *, buf, u32, size)
598 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
600 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
606 memset(buf, 0, size);
610 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
611 .func = bpf_perf_event_read_value,
613 .ret_type = RET_INTEGER,
614 .arg1_type = ARG_CONST_MAP_PTR,
615 .arg2_type = ARG_ANYTHING,
616 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
617 .arg4_type = ARG_CONST_SIZE,
620 static __always_inline u64
621 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
622 u64 flags, struct perf_sample_data *sd)
624 struct bpf_array *array = container_of(map, struct bpf_array, map);
625 unsigned int cpu = smp_processor_id();
626 u64 index = flags & BPF_F_INDEX_MASK;
627 struct bpf_event_entry *ee;
628 struct perf_event *event;
630 if (index == BPF_F_CURRENT_CPU)
632 if (unlikely(index >= array->map.max_entries))
635 ee = READ_ONCE(array->ptrs[index]);
640 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
641 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
644 if (unlikely(event->oncpu != cpu))
647 return perf_event_output(event, sd, regs);
651 * Support executing tracepoints in normal, irq, and nmi context that each call
652 * bpf_perf_event_output
654 struct bpf_trace_sample_data {
655 struct perf_sample_data sds[3];
658 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
659 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
660 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
661 u64, flags, void *, data, u64, size)
663 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
664 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
665 struct perf_raw_record raw = {
671 struct perf_sample_data *sd;
674 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
679 sd = &sds->sds[nest_level - 1];
681 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
686 perf_sample_data_init(sd, 0, 0);
689 err = __bpf_perf_event_output(regs, map, flags, sd);
692 this_cpu_dec(bpf_trace_nest_level);
696 static const struct bpf_func_proto bpf_perf_event_output_proto = {
697 .func = bpf_perf_event_output,
699 .ret_type = RET_INTEGER,
700 .arg1_type = ARG_PTR_TO_CTX,
701 .arg2_type = ARG_CONST_MAP_PTR,
702 .arg3_type = ARG_ANYTHING,
703 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
704 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
707 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
708 struct bpf_nested_pt_regs {
709 struct pt_regs regs[3];
711 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
712 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
714 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
715 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
717 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
718 struct perf_raw_frag frag = {
723 struct perf_raw_record raw = {
726 .next = ctx_size ? &frag : NULL,
732 struct perf_sample_data *sd;
733 struct pt_regs *regs;
736 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
740 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
741 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
743 perf_fetch_caller_regs(regs);
744 perf_sample_data_init(sd, 0, 0);
747 ret = __bpf_perf_event_output(regs, map, flags, sd);
749 this_cpu_dec(bpf_event_output_nest_level);
753 BPF_CALL_0(bpf_get_current_task)
755 return (long) current;
758 const struct bpf_func_proto bpf_get_current_task_proto = {
759 .func = bpf_get_current_task,
761 .ret_type = RET_INTEGER,
764 BPF_CALL_0(bpf_get_current_task_btf)
766 return (unsigned long) current;
769 const struct bpf_func_proto bpf_get_current_task_btf_proto = {
770 .func = bpf_get_current_task_btf,
772 .ret_type = RET_PTR_TO_BTF_ID,
773 .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
776 BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
778 return (unsigned long) task_pt_regs(task);
781 BTF_ID_LIST(bpf_task_pt_regs_ids)
782 BTF_ID(struct, pt_regs)
784 const struct bpf_func_proto bpf_task_pt_regs_proto = {
785 .func = bpf_task_pt_regs,
787 .arg1_type = ARG_PTR_TO_BTF_ID,
788 .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
789 .ret_type = RET_PTR_TO_BTF_ID,
790 .ret_btf_id = &bpf_task_pt_regs_ids[0],
793 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
795 struct bpf_array *array = container_of(map, struct bpf_array, map);
798 if (unlikely(idx >= array->map.max_entries))
801 cgrp = READ_ONCE(array->ptrs[idx]);
805 return task_under_cgroup_hierarchy(current, cgrp);
808 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
809 .func = bpf_current_task_under_cgroup,
811 .ret_type = RET_INTEGER,
812 .arg1_type = ARG_CONST_MAP_PTR,
813 .arg2_type = ARG_ANYTHING,
816 struct send_signal_irq_work {
817 struct irq_work irq_work;
818 struct task_struct *task;
823 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
825 static void do_bpf_send_signal(struct irq_work *entry)
827 struct send_signal_irq_work *work;
829 work = container_of(entry, struct send_signal_irq_work, irq_work);
830 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
833 static int bpf_send_signal_common(u32 sig, enum pid_type type)
835 struct send_signal_irq_work *work = NULL;
837 /* Similar to bpf_probe_write_user, task needs to be
838 * in a sound condition and kernel memory access be
839 * permitted in order to send signal to the current
842 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
844 if (unlikely(!nmi_uaccess_okay()))
847 if (irqs_disabled()) {
848 /* Do an early check on signal validity. Otherwise,
849 * the error is lost in deferred irq_work.
851 if (unlikely(!valid_signal(sig)))
854 work = this_cpu_ptr(&send_signal_work);
855 if (irq_work_is_busy(&work->irq_work))
858 /* Add the current task, which is the target of sending signal,
859 * to the irq_work. The current task may change when queued
860 * irq works get executed.
862 work->task = current;
865 irq_work_queue(&work->irq_work);
869 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
872 BPF_CALL_1(bpf_send_signal, u32, sig)
874 return bpf_send_signal_common(sig, PIDTYPE_TGID);
877 static const struct bpf_func_proto bpf_send_signal_proto = {
878 .func = bpf_send_signal,
880 .ret_type = RET_INTEGER,
881 .arg1_type = ARG_ANYTHING,
884 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
886 return bpf_send_signal_common(sig, PIDTYPE_PID);
889 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
890 .func = bpf_send_signal_thread,
892 .ret_type = RET_INTEGER,
893 .arg1_type = ARG_ANYTHING,
896 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
904 p = d_path(path, buf, sz);
909 memmove(buf, p, len);
915 BTF_SET_START(btf_allowlist_d_path)
916 #ifdef CONFIG_SECURITY
917 BTF_ID(func, security_file_permission)
918 BTF_ID(func, security_inode_getattr)
919 BTF_ID(func, security_file_open)
921 #ifdef CONFIG_SECURITY_PATH
922 BTF_ID(func, security_path_truncate)
924 BTF_ID(func, vfs_truncate)
925 BTF_ID(func, vfs_fallocate)
926 BTF_ID(func, dentry_open)
927 BTF_ID(func, vfs_getattr)
928 BTF_ID(func, filp_close)
929 BTF_SET_END(btf_allowlist_d_path)
931 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
933 if (prog->type == BPF_PROG_TYPE_TRACING &&
934 prog->expected_attach_type == BPF_TRACE_ITER)
937 if (prog->type == BPF_PROG_TYPE_LSM)
938 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
940 return btf_id_set_contains(&btf_allowlist_d_path,
941 prog->aux->attach_btf_id);
944 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
946 static const struct bpf_func_proto bpf_d_path_proto = {
949 .ret_type = RET_INTEGER,
950 .arg1_type = ARG_PTR_TO_BTF_ID,
951 .arg1_btf_id = &bpf_d_path_btf_ids[0],
952 .arg2_type = ARG_PTR_TO_MEM,
953 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
954 .allowed = bpf_d_path_allowed,
957 #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
958 BTF_F_PTR_RAW | BTF_F_ZERO)
960 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
961 u64 flags, const struct btf **btf,
964 const struct btf_type *t;
966 if (unlikely(flags & ~(BTF_F_ALL)))
969 if (btf_ptr_size != sizeof(struct btf_ptr))
972 *btf = bpf_get_btf_vmlinux();
974 if (IS_ERR_OR_NULL(*btf))
975 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
977 if (ptr->type_id > 0)
978 *btf_id = ptr->type_id;
983 t = btf_type_by_id(*btf, *btf_id);
984 if (*btf_id <= 0 || !t)
990 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
991 u32, btf_ptr_size, u64, flags)
993 const struct btf *btf;
997 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1001 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1005 const struct bpf_func_proto bpf_snprintf_btf_proto = {
1006 .func = bpf_snprintf_btf,
1008 .ret_type = RET_INTEGER,
1009 .arg1_type = ARG_PTR_TO_MEM,
1010 .arg2_type = ARG_CONST_SIZE,
1011 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1012 .arg4_type = ARG_CONST_SIZE,
1013 .arg5_type = ARG_ANYTHING,
1016 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
1018 /* This helper call is inlined by verifier. */
1019 return ((u64 *)ctx)[-2];
1022 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
1023 .func = bpf_get_func_ip_tracing,
1025 .ret_type = RET_INTEGER,
1026 .arg1_type = ARG_PTR_TO_CTX,
1029 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1031 struct kprobe *kp = kprobe_running();
1033 return kp ? (uintptr_t)kp->addr : 0;
1036 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1037 .func = bpf_get_func_ip_kprobe,
1039 .ret_type = RET_INTEGER,
1040 .arg1_type = ARG_PTR_TO_CTX,
1043 BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
1045 return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
1048 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
1049 .func = bpf_get_func_ip_kprobe_multi,
1051 .ret_type = RET_INTEGER,
1052 .arg1_type = ARG_PTR_TO_CTX,
1055 BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1057 return bpf_kprobe_multi_cookie(current->bpf_ctx);
1060 static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1061 .func = bpf_get_attach_cookie_kprobe_multi,
1063 .ret_type = RET_INTEGER,
1064 .arg1_type = ARG_PTR_TO_CTX,
1067 BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
1069 struct bpf_trace_run_ctx *run_ctx;
1071 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1072 return run_ctx->bpf_cookie;
1075 static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
1076 .func = bpf_get_attach_cookie_trace,
1078 .ret_type = RET_INTEGER,
1079 .arg1_type = ARG_PTR_TO_CTX,
1082 BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
1084 return ctx->event->bpf_cookie;
1087 static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
1088 .func = bpf_get_attach_cookie_pe,
1090 .ret_type = RET_INTEGER,
1091 .arg1_type = ARG_PTR_TO_CTX,
1094 BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
1096 struct bpf_trace_run_ctx *run_ctx;
1098 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1099 return run_ctx->bpf_cookie;
1102 static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
1103 .func = bpf_get_attach_cookie_tracing,
1105 .ret_type = RET_INTEGER,
1106 .arg1_type = ARG_PTR_TO_CTX,
1109 BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1114 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1115 u32 entry_cnt = size / br_entry_size;
1117 entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1119 if (unlikely(flags))
1125 return entry_cnt * br_entry_size;
1129 static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1130 .func = bpf_get_branch_snapshot,
1132 .ret_type = RET_INTEGER,
1133 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1134 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1137 BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1139 /* This helper call is inlined by verifier. */
1140 u64 nr_args = ((u64 *)ctx)[-1];
1142 if ((u64) n >= nr_args)
1144 *value = ((u64 *)ctx)[n];
1148 static const struct bpf_func_proto bpf_get_func_arg_proto = {
1149 .func = get_func_arg,
1150 .ret_type = RET_INTEGER,
1151 .arg1_type = ARG_PTR_TO_CTX,
1152 .arg2_type = ARG_ANYTHING,
1153 .arg3_type = ARG_PTR_TO_LONG,
1156 BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1158 /* This helper call is inlined by verifier. */
1159 u64 nr_args = ((u64 *)ctx)[-1];
1161 *value = ((u64 *)ctx)[nr_args];
1165 static const struct bpf_func_proto bpf_get_func_ret_proto = {
1166 .func = get_func_ret,
1167 .ret_type = RET_INTEGER,
1168 .arg1_type = ARG_PTR_TO_CTX,
1169 .arg2_type = ARG_PTR_TO_LONG,
1172 BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1174 /* This helper call is inlined by verifier. */
1175 return ((u64 *)ctx)[-1];
1178 static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1179 .func = get_func_arg_cnt,
1180 .ret_type = RET_INTEGER,
1181 .arg1_type = ARG_PTR_TO_CTX,
1184 static const struct bpf_func_proto *
1185 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1188 case BPF_FUNC_map_lookup_elem:
1189 return &bpf_map_lookup_elem_proto;
1190 case BPF_FUNC_map_update_elem:
1191 return &bpf_map_update_elem_proto;
1192 case BPF_FUNC_map_delete_elem:
1193 return &bpf_map_delete_elem_proto;
1194 case BPF_FUNC_map_push_elem:
1195 return &bpf_map_push_elem_proto;
1196 case BPF_FUNC_map_pop_elem:
1197 return &bpf_map_pop_elem_proto;
1198 case BPF_FUNC_map_peek_elem:
1199 return &bpf_map_peek_elem_proto;
1200 case BPF_FUNC_map_lookup_percpu_elem:
1201 return &bpf_map_lookup_percpu_elem_proto;
1202 case BPF_FUNC_ktime_get_ns:
1203 return &bpf_ktime_get_ns_proto;
1204 case BPF_FUNC_ktime_get_boot_ns:
1205 return &bpf_ktime_get_boot_ns_proto;
1206 case BPF_FUNC_tail_call:
1207 return &bpf_tail_call_proto;
1208 case BPF_FUNC_get_current_pid_tgid:
1209 return &bpf_get_current_pid_tgid_proto;
1210 case BPF_FUNC_get_current_task:
1211 return &bpf_get_current_task_proto;
1212 case BPF_FUNC_get_current_task_btf:
1213 return &bpf_get_current_task_btf_proto;
1214 case BPF_FUNC_task_pt_regs:
1215 return &bpf_task_pt_regs_proto;
1216 case BPF_FUNC_get_current_uid_gid:
1217 return &bpf_get_current_uid_gid_proto;
1218 case BPF_FUNC_get_current_comm:
1219 return &bpf_get_current_comm_proto;
1220 case BPF_FUNC_trace_printk:
1221 return bpf_get_trace_printk_proto();
1222 case BPF_FUNC_get_smp_processor_id:
1223 return &bpf_get_smp_processor_id_proto;
1224 case BPF_FUNC_get_numa_node_id:
1225 return &bpf_get_numa_node_id_proto;
1226 case BPF_FUNC_perf_event_read:
1227 return &bpf_perf_event_read_proto;
1228 case BPF_FUNC_current_task_under_cgroup:
1229 return &bpf_current_task_under_cgroup_proto;
1230 case BPF_FUNC_get_prandom_u32:
1231 return &bpf_get_prandom_u32_proto;
1232 case BPF_FUNC_probe_write_user:
1233 return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1234 NULL : bpf_get_probe_write_proto();
1235 case BPF_FUNC_probe_read_user:
1236 return &bpf_probe_read_user_proto;
1237 case BPF_FUNC_probe_read_kernel:
1238 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1239 NULL : &bpf_probe_read_kernel_proto;
1240 case BPF_FUNC_probe_read_user_str:
1241 return &bpf_probe_read_user_str_proto;
1242 case BPF_FUNC_probe_read_kernel_str:
1243 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1244 NULL : &bpf_probe_read_kernel_str_proto;
1245 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1246 case BPF_FUNC_probe_read:
1247 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1248 NULL : &bpf_probe_read_compat_proto;
1249 case BPF_FUNC_probe_read_str:
1250 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1251 NULL : &bpf_probe_read_compat_str_proto;
1253 #ifdef CONFIG_CGROUPS
1254 case BPF_FUNC_get_current_cgroup_id:
1255 return &bpf_get_current_cgroup_id_proto;
1256 case BPF_FUNC_get_current_ancestor_cgroup_id:
1257 return &bpf_get_current_ancestor_cgroup_id_proto;
1259 case BPF_FUNC_send_signal:
1260 return &bpf_send_signal_proto;
1261 case BPF_FUNC_send_signal_thread:
1262 return &bpf_send_signal_thread_proto;
1263 case BPF_FUNC_perf_event_read_value:
1264 return &bpf_perf_event_read_value_proto;
1265 case BPF_FUNC_get_ns_current_pid_tgid:
1266 return &bpf_get_ns_current_pid_tgid_proto;
1267 case BPF_FUNC_ringbuf_output:
1268 return &bpf_ringbuf_output_proto;
1269 case BPF_FUNC_ringbuf_reserve:
1270 return &bpf_ringbuf_reserve_proto;
1271 case BPF_FUNC_ringbuf_submit:
1272 return &bpf_ringbuf_submit_proto;
1273 case BPF_FUNC_ringbuf_discard:
1274 return &bpf_ringbuf_discard_proto;
1275 case BPF_FUNC_ringbuf_query:
1276 return &bpf_ringbuf_query_proto;
1277 case BPF_FUNC_jiffies64:
1278 return &bpf_jiffies64_proto;
1279 case BPF_FUNC_get_task_stack:
1280 return &bpf_get_task_stack_proto;
1281 case BPF_FUNC_copy_from_user:
1282 return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
1283 case BPF_FUNC_copy_from_user_task:
1284 return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
1285 case BPF_FUNC_snprintf_btf:
1286 return &bpf_snprintf_btf_proto;
1287 case BPF_FUNC_per_cpu_ptr:
1288 return &bpf_per_cpu_ptr_proto;
1289 case BPF_FUNC_this_cpu_ptr:
1290 return &bpf_this_cpu_ptr_proto;
1291 case BPF_FUNC_task_storage_get:
1292 return &bpf_task_storage_get_proto;
1293 case BPF_FUNC_task_storage_delete:
1294 return &bpf_task_storage_delete_proto;
1295 case BPF_FUNC_for_each_map_elem:
1296 return &bpf_for_each_map_elem_proto;
1297 case BPF_FUNC_snprintf:
1298 return &bpf_snprintf_proto;
1299 case BPF_FUNC_get_func_ip:
1300 return &bpf_get_func_ip_proto_tracing;
1301 case BPF_FUNC_get_branch_snapshot:
1302 return &bpf_get_branch_snapshot_proto;
1303 case BPF_FUNC_find_vma:
1304 return &bpf_find_vma_proto;
1305 case BPF_FUNC_trace_vprintk:
1306 return bpf_get_trace_vprintk_proto();
1308 return bpf_base_func_proto(func_id);
1312 static const struct bpf_func_proto *
1313 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1316 case BPF_FUNC_perf_event_output:
1317 return &bpf_perf_event_output_proto;
1318 case BPF_FUNC_get_stackid:
1319 return &bpf_get_stackid_proto;
1320 case BPF_FUNC_get_stack:
1321 return &bpf_get_stack_proto;
1322 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1323 case BPF_FUNC_override_return:
1324 return &bpf_override_return_proto;
1326 case BPF_FUNC_get_func_ip:
1327 return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1328 &bpf_get_func_ip_proto_kprobe_multi :
1329 &bpf_get_func_ip_proto_kprobe;
1330 case BPF_FUNC_get_attach_cookie:
1331 return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1332 &bpf_get_attach_cookie_proto_kmulti :
1333 &bpf_get_attach_cookie_proto_trace;
1335 return bpf_tracing_func_proto(func_id, prog);
1339 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1340 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1341 const struct bpf_prog *prog,
1342 struct bpf_insn_access_aux *info)
1344 if (off < 0 || off >= sizeof(struct pt_regs))
1346 if (type != BPF_READ)
1348 if (off % size != 0)
1351 * Assertion for 32 bit to make sure last 8 byte access
1352 * (BPF_DW) to the last 4 byte member is disallowed.
1354 if (off + size > sizeof(struct pt_regs))
1360 const struct bpf_verifier_ops kprobe_verifier_ops = {
1361 .get_func_proto = kprobe_prog_func_proto,
1362 .is_valid_access = kprobe_prog_is_valid_access,
1365 const struct bpf_prog_ops kprobe_prog_ops = {
1368 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1369 u64, flags, void *, data, u64, size)
1371 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1374 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1375 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1376 * from there and call the same bpf_perf_event_output() helper inline.
1378 return ____bpf_perf_event_output(regs, map, flags, data, size);
1381 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1382 .func = bpf_perf_event_output_tp,
1384 .ret_type = RET_INTEGER,
1385 .arg1_type = ARG_PTR_TO_CTX,
1386 .arg2_type = ARG_CONST_MAP_PTR,
1387 .arg3_type = ARG_ANYTHING,
1388 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1389 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1392 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1395 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1398 * Same comment as in bpf_perf_event_output_tp(), only that this time
1399 * the other helper's function body cannot be inlined due to being
1400 * external, thus we need to call raw helper function.
1402 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1406 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1407 .func = bpf_get_stackid_tp,
1409 .ret_type = RET_INTEGER,
1410 .arg1_type = ARG_PTR_TO_CTX,
1411 .arg2_type = ARG_CONST_MAP_PTR,
1412 .arg3_type = ARG_ANYTHING,
1415 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1418 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1420 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1421 (unsigned long) size, flags, 0);
1424 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1425 .func = bpf_get_stack_tp,
1427 .ret_type = RET_INTEGER,
1428 .arg1_type = ARG_PTR_TO_CTX,
1429 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1430 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1431 .arg4_type = ARG_ANYTHING,
1434 static const struct bpf_func_proto *
1435 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1438 case BPF_FUNC_perf_event_output:
1439 return &bpf_perf_event_output_proto_tp;
1440 case BPF_FUNC_get_stackid:
1441 return &bpf_get_stackid_proto_tp;
1442 case BPF_FUNC_get_stack:
1443 return &bpf_get_stack_proto_tp;
1444 case BPF_FUNC_get_attach_cookie:
1445 return &bpf_get_attach_cookie_proto_trace;
1447 return bpf_tracing_func_proto(func_id, prog);
1451 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1452 const struct bpf_prog *prog,
1453 struct bpf_insn_access_aux *info)
1455 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1457 if (type != BPF_READ)
1459 if (off % size != 0)
1462 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1466 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1467 .get_func_proto = tp_prog_func_proto,
1468 .is_valid_access = tp_prog_is_valid_access,
1471 const struct bpf_prog_ops tracepoint_prog_ops = {
1474 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1475 struct bpf_perf_event_value *, buf, u32, size)
1479 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1481 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1487 memset(buf, 0, size);
1491 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1492 .func = bpf_perf_prog_read_value,
1494 .ret_type = RET_INTEGER,
1495 .arg1_type = ARG_PTR_TO_CTX,
1496 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1497 .arg3_type = ARG_CONST_SIZE,
1500 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1501 void *, buf, u32, size, u64, flags)
1503 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1504 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1507 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1510 if (unlikely(!br_stack))
1513 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1514 return br_stack->nr * br_entry_size;
1516 if (!buf || (size % br_entry_size != 0))
1519 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1520 memcpy(buf, br_stack->entries, to_copy);
1525 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1526 .func = bpf_read_branch_records,
1528 .ret_type = RET_INTEGER,
1529 .arg1_type = ARG_PTR_TO_CTX,
1530 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1531 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1532 .arg4_type = ARG_ANYTHING,
1535 static const struct bpf_func_proto *
1536 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1539 case BPF_FUNC_perf_event_output:
1540 return &bpf_perf_event_output_proto_tp;
1541 case BPF_FUNC_get_stackid:
1542 return &bpf_get_stackid_proto_pe;
1543 case BPF_FUNC_get_stack:
1544 return &bpf_get_stack_proto_pe;
1545 case BPF_FUNC_perf_prog_read_value:
1546 return &bpf_perf_prog_read_value_proto;
1547 case BPF_FUNC_read_branch_records:
1548 return &bpf_read_branch_records_proto;
1549 case BPF_FUNC_get_attach_cookie:
1550 return &bpf_get_attach_cookie_proto_pe;
1552 return bpf_tracing_func_proto(func_id, prog);
1557 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1558 * to avoid potential recursive reuse issue when/if tracepoints are added
1559 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1561 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1562 * in normal, irq, and nmi context.
1564 struct bpf_raw_tp_regs {
1565 struct pt_regs regs[3];
1567 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1568 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1569 static struct pt_regs *get_bpf_raw_tp_regs(void)
1571 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1572 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1574 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1575 this_cpu_dec(bpf_raw_tp_nest_level);
1576 return ERR_PTR(-EBUSY);
1579 return &tp_regs->regs[nest_level - 1];
1582 static void put_bpf_raw_tp_regs(void)
1584 this_cpu_dec(bpf_raw_tp_nest_level);
1587 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1588 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1590 struct pt_regs *regs = get_bpf_raw_tp_regs();
1594 return PTR_ERR(regs);
1596 perf_fetch_caller_regs(regs);
1597 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1599 put_bpf_raw_tp_regs();
1603 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1604 .func = bpf_perf_event_output_raw_tp,
1606 .ret_type = RET_INTEGER,
1607 .arg1_type = ARG_PTR_TO_CTX,
1608 .arg2_type = ARG_CONST_MAP_PTR,
1609 .arg3_type = ARG_ANYTHING,
1610 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1611 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1614 extern const struct bpf_func_proto bpf_skb_output_proto;
1615 extern const struct bpf_func_proto bpf_xdp_output_proto;
1616 extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1618 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1619 struct bpf_map *, map, u64, flags)
1621 struct pt_regs *regs = get_bpf_raw_tp_regs();
1625 return PTR_ERR(regs);
1627 perf_fetch_caller_regs(regs);
1628 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1629 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1631 put_bpf_raw_tp_regs();
1635 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1636 .func = bpf_get_stackid_raw_tp,
1638 .ret_type = RET_INTEGER,
1639 .arg1_type = ARG_PTR_TO_CTX,
1640 .arg2_type = ARG_CONST_MAP_PTR,
1641 .arg3_type = ARG_ANYTHING,
1644 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1645 void *, buf, u32, size, u64, flags)
1647 struct pt_regs *regs = get_bpf_raw_tp_regs();
1651 return PTR_ERR(regs);
1653 perf_fetch_caller_regs(regs);
1654 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1655 (unsigned long) size, flags, 0);
1656 put_bpf_raw_tp_regs();
1660 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1661 .func = bpf_get_stack_raw_tp,
1663 .ret_type = RET_INTEGER,
1664 .arg1_type = ARG_PTR_TO_CTX,
1665 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1666 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1667 .arg4_type = ARG_ANYTHING,
1670 static const struct bpf_func_proto *
1671 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1674 case BPF_FUNC_perf_event_output:
1675 return &bpf_perf_event_output_proto_raw_tp;
1676 case BPF_FUNC_get_stackid:
1677 return &bpf_get_stackid_proto_raw_tp;
1678 case BPF_FUNC_get_stack:
1679 return &bpf_get_stack_proto_raw_tp;
1681 return bpf_tracing_func_proto(func_id, prog);
1685 const struct bpf_func_proto *
1686 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1688 const struct bpf_func_proto *fn;
1692 case BPF_FUNC_skb_output:
1693 return &bpf_skb_output_proto;
1694 case BPF_FUNC_xdp_output:
1695 return &bpf_xdp_output_proto;
1696 case BPF_FUNC_skc_to_tcp6_sock:
1697 return &bpf_skc_to_tcp6_sock_proto;
1698 case BPF_FUNC_skc_to_tcp_sock:
1699 return &bpf_skc_to_tcp_sock_proto;
1700 case BPF_FUNC_skc_to_tcp_timewait_sock:
1701 return &bpf_skc_to_tcp_timewait_sock_proto;
1702 case BPF_FUNC_skc_to_tcp_request_sock:
1703 return &bpf_skc_to_tcp_request_sock_proto;
1704 case BPF_FUNC_skc_to_udp6_sock:
1705 return &bpf_skc_to_udp6_sock_proto;
1706 case BPF_FUNC_skc_to_unix_sock:
1707 return &bpf_skc_to_unix_sock_proto;
1708 case BPF_FUNC_skc_to_mptcp_sock:
1709 return &bpf_skc_to_mptcp_sock_proto;
1710 case BPF_FUNC_sk_storage_get:
1711 return &bpf_sk_storage_get_tracing_proto;
1712 case BPF_FUNC_sk_storage_delete:
1713 return &bpf_sk_storage_delete_tracing_proto;
1714 case BPF_FUNC_sock_from_file:
1715 return &bpf_sock_from_file_proto;
1716 case BPF_FUNC_get_socket_cookie:
1717 return &bpf_get_socket_ptr_cookie_proto;
1718 case BPF_FUNC_xdp_get_buff_len:
1719 return &bpf_xdp_get_buff_len_trace_proto;
1721 case BPF_FUNC_seq_printf:
1722 return prog->expected_attach_type == BPF_TRACE_ITER ?
1723 &bpf_seq_printf_proto :
1725 case BPF_FUNC_seq_write:
1726 return prog->expected_attach_type == BPF_TRACE_ITER ?
1727 &bpf_seq_write_proto :
1729 case BPF_FUNC_seq_printf_btf:
1730 return prog->expected_attach_type == BPF_TRACE_ITER ?
1731 &bpf_seq_printf_btf_proto :
1733 case BPF_FUNC_d_path:
1734 return &bpf_d_path_proto;
1735 case BPF_FUNC_get_func_arg:
1736 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1737 case BPF_FUNC_get_func_ret:
1738 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
1739 case BPF_FUNC_get_func_arg_cnt:
1740 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
1741 case BPF_FUNC_get_attach_cookie:
1742 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
1744 fn = raw_tp_prog_func_proto(func_id, prog);
1745 if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
1746 fn = bpf_iter_get_func_proto(func_id, prog);
1751 static bool raw_tp_prog_is_valid_access(int off, int size,
1752 enum bpf_access_type type,
1753 const struct bpf_prog *prog,
1754 struct bpf_insn_access_aux *info)
1756 return bpf_tracing_ctx_access(off, size, type);
1759 static bool tracing_prog_is_valid_access(int off, int size,
1760 enum bpf_access_type type,
1761 const struct bpf_prog *prog,
1762 struct bpf_insn_access_aux *info)
1764 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
1767 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1768 const union bpf_attr *kattr,
1769 union bpf_attr __user *uattr)
1774 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1775 .get_func_proto = raw_tp_prog_func_proto,
1776 .is_valid_access = raw_tp_prog_is_valid_access,
1779 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1781 .test_run = bpf_prog_test_run_raw_tp,
1785 const struct bpf_verifier_ops tracing_verifier_ops = {
1786 .get_func_proto = tracing_prog_func_proto,
1787 .is_valid_access = tracing_prog_is_valid_access,
1790 const struct bpf_prog_ops tracing_prog_ops = {
1791 .test_run = bpf_prog_test_run_tracing,
1794 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1795 enum bpf_access_type type,
1796 const struct bpf_prog *prog,
1797 struct bpf_insn_access_aux *info)
1800 if (size != sizeof(u64) || type != BPF_READ)
1802 info->reg_type = PTR_TO_TP_BUFFER;
1804 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1807 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1808 .get_func_proto = raw_tp_prog_func_proto,
1809 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1812 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1815 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1816 const struct bpf_prog *prog,
1817 struct bpf_insn_access_aux *info)
1819 const int size_u64 = sizeof(u64);
1821 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1823 if (type != BPF_READ)
1825 if (off % size != 0) {
1826 if (sizeof(unsigned long) != 4)
1830 if (off % size != 4)
1835 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
1836 bpf_ctx_record_field_size(info, size_u64);
1837 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1840 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1841 bpf_ctx_record_field_size(info, size_u64);
1842 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1846 if (size != sizeof(long))
1853 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1854 const struct bpf_insn *si,
1855 struct bpf_insn *insn_buf,
1856 struct bpf_prog *prog, u32 *target_size)
1858 struct bpf_insn *insn = insn_buf;
1861 case offsetof(struct bpf_perf_event_data, sample_period):
1862 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1863 data), si->dst_reg, si->src_reg,
1864 offsetof(struct bpf_perf_event_data_kern, data));
1865 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1866 bpf_target_off(struct perf_sample_data, period, 8,
1869 case offsetof(struct bpf_perf_event_data, addr):
1870 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1871 data), si->dst_reg, si->src_reg,
1872 offsetof(struct bpf_perf_event_data_kern, data));
1873 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1874 bpf_target_off(struct perf_sample_data, addr, 8,
1878 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1879 regs), si->dst_reg, si->src_reg,
1880 offsetof(struct bpf_perf_event_data_kern, regs));
1881 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1886 return insn - insn_buf;
1889 const struct bpf_verifier_ops perf_event_verifier_ops = {
1890 .get_func_proto = pe_prog_func_proto,
1891 .is_valid_access = pe_prog_is_valid_access,
1892 .convert_ctx_access = pe_prog_convert_ctx_access,
1895 const struct bpf_prog_ops perf_event_prog_ops = {
1898 static DEFINE_MUTEX(bpf_event_mutex);
1900 #define BPF_TRACE_MAX_PROGS 64
1902 int perf_event_attach_bpf_prog(struct perf_event *event,
1903 struct bpf_prog *prog,
1906 struct bpf_prog_array *old_array;
1907 struct bpf_prog_array *new_array;
1911 * Kprobe override only works if they are on the function entry,
1912 * and only if they are on the opt-in list.
1914 if (prog->kprobe_override &&
1915 (!trace_kprobe_on_func_entry(event->tp_event) ||
1916 !trace_kprobe_error_injectable(event->tp_event)))
1919 mutex_lock(&bpf_event_mutex);
1924 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1926 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1931 ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
1935 /* set the new array to event->tp_event and set event->prog */
1937 event->bpf_cookie = bpf_cookie;
1938 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1939 bpf_prog_array_free(old_array);
1942 mutex_unlock(&bpf_event_mutex);
1946 void perf_event_detach_bpf_prog(struct perf_event *event)
1948 struct bpf_prog_array *old_array;
1949 struct bpf_prog_array *new_array;
1952 mutex_lock(&bpf_event_mutex);
1957 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1958 ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
1962 bpf_prog_array_delete_safe(old_array, event->prog);
1964 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1965 bpf_prog_array_free(old_array);
1968 bpf_prog_put(event->prog);
1972 mutex_unlock(&bpf_event_mutex);
1975 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
1977 struct perf_event_query_bpf __user *uquery = info;
1978 struct perf_event_query_bpf query = {};
1979 struct bpf_prog_array *progs;
1980 u32 *ids, prog_cnt, ids_len;
1983 if (!perfmon_capable())
1985 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1987 if (copy_from_user(&query, uquery, sizeof(query)))
1990 ids_len = query.ids_len;
1991 if (ids_len > BPF_TRACE_MAX_PROGS)
1993 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1997 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1998 * is required when user only wants to check for uquery->prog_cnt.
1999 * There is no need to check for it since the case is handled
2000 * gracefully in bpf_prog_array_copy_info.
2003 mutex_lock(&bpf_event_mutex);
2004 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2005 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2006 mutex_unlock(&bpf_event_mutex);
2008 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2009 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2016 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2017 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2019 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2021 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2023 for (; btp < __stop__bpf_raw_tp; btp++) {
2024 if (!strcmp(btp->tp->name, name))
2028 return bpf_get_raw_tracepoint_module(name);
2031 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2036 mod = __module_address((unsigned long)btp);
2041 static __always_inline
2042 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2046 (void) bpf_prog_run(prog, args);
2050 #define UNPACK(...) __VA_ARGS__
2051 #define REPEAT_1(FN, DL, X, ...) FN(X)
2052 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2053 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2054 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2055 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2056 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2057 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2058 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2059 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2060 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2061 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2062 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2063 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2065 #define SARG(X) u64 arg##X
2066 #define COPY(X) args[X] = arg##X
2068 #define __DL_COM (,)
2069 #define __DL_SEM (;)
2071 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2073 #define BPF_TRACE_DEFN_x(x) \
2074 void bpf_trace_run##x(struct bpf_prog *prog, \
2075 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2078 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2079 __bpf_trace_run(prog, args); \
2081 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2082 BPF_TRACE_DEFN_x(1);
2083 BPF_TRACE_DEFN_x(2);
2084 BPF_TRACE_DEFN_x(3);
2085 BPF_TRACE_DEFN_x(4);
2086 BPF_TRACE_DEFN_x(5);
2087 BPF_TRACE_DEFN_x(6);
2088 BPF_TRACE_DEFN_x(7);
2089 BPF_TRACE_DEFN_x(8);
2090 BPF_TRACE_DEFN_x(9);
2091 BPF_TRACE_DEFN_x(10);
2092 BPF_TRACE_DEFN_x(11);
2093 BPF_TRACE_DEFN_x(12);
2095 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2097 struct tracepoint *tp = btp->tp;
2100 * check that program doesn't access arguments beyond what's
2101 * available in this tracepoint
2103 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2106 if (prog->aux->max_tp_access > btp->writable_size)
2109 return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
2113 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2115 return __bpf_probe_register(btp, prog);
2118 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2120 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2123 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2124 u32 *fd_type, const char **buf,
2125 u64 *probe_offset, u64 *probe_addr)
2127 bool is_tracepoint, is_syscall_tp;
2128 struct bpf_prog *prog;
2135 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2136 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2139 *prog_id = prog->aux->id;
2140 flags = event->tp_event->flags;
2141 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2142 is_syscall_tp = is_syscall_trace_event(event->tp_event);
2144 if (is_tracepoint || is_syscall_tp) {
2145 *buf = is_tracepoint ? event->tp_event->tp->name
2146 : event->tp_event->name;
2147 *fd_type = BPF_FD_TYPE_TRACEPOINT;
2148 *probe_offset = 0x0;
2153 #ifdef CONFIG_KPROBE_EVENTS
2154 if (flags & TRACE_EVENT_FL_KPROBE)
2155 err = bpf_get_kprobe_info(event, fd_type, buf,
2156 probe_offset, probe_addr,
2157 event->attr.type == PERF_TYPE_TRACEPOINT);
2159 #ifdef CONFIG_UPROBE_EVENTS
2160 if (flags & TRACE_EVENT_FL_UPROBE)
2161 err = bpf_get_uprobe_info(event, fd_type, buf,
2163 event->attr.type == PERF_TYPE_TRACEPOINT);
2170 static int __init send_signal_irq_work_init(void)
2173 struct send_signal_irq_work *work;
2175 for_each_possible_cpu(cpu) {
2176 work = per_cpu_ptr(&send_signal_work, cpu);
2177 init_irq_work(&work->irq_work, do_bpf_send_signal);
2182 subsys_initcall(send_signal_irq_work_init);
2184 #ifdef CONFIG_MODULES
2185 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2188 struct bpf_trace_module *btm, *tmp;
2189 struct module *mod = module;
2192 if (mod->num_bpf_raw_events == 0 ||
2193 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2196 mutex_lock(&bpf_module_mutex);
2199 case MODULE_STATE_COMING:
2200 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2202 btm->module = module;
2203 list_add(&btm->list, &bpf_trace_modules);
2208 case MODULE_STATE_GOING:
2209 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2210 if (btm->module == module) {
2211 list_del(&btm->list);
2219 mutex_unlock(&bpf_module_mutex);
2222 return notifier_from_errno(ret);
2225 static struct notifier_block bpf_module_nb = {
2226 .notifier_call = bpf_event_notify,
2229 static int __init bpf_event_init(void)
2231 register_module_notifier(&bpf_module_nb);
2235 fs_initcall(bpf_event_init);
2236 #endif /* CONFIG_MODULES */
2238 #ifdef CONFIG_FPROBE
2239 struct bpf_kprobe_multi_link {
2240 struct bpf_link link;
2242 unsigned long *addrs;
2247 struct bpf_kprobe_multi_run_ctx {
2248 struct bpf_run_ctx run_ctx;
2249 struct bpf_kprobe_multi_link *link;
2250 unsigned long entry_ip;
2258 static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
2260 unsigned long __user usymbol;
2261 const char **syms = NULL;
2262 char *buf = NULL, *p;
2266 syms = kvmalloc(cnt * sizeof(*syms), GFP_KERNEL);
2270 buf = kvmalloc(cnt * KSYM_NAME_LEN, GFP_KERNEL);
2274 for (p = buf, i = 0; i < cnt; i++) {
2275 if (__get_user(usymbol, usyms + i)) {
2279 err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
2280 if (err == KSYM_NAME_LEN)
2300 static void free_user_syms(struct user_syms *us)
2306 static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2308 struct bpf_kprobe_multi_link *kmulti_link;
2310 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2311 unregister_fprobe(&kmulti_link->fp);
2314 static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
2316 struct bpf_kprobe_multi_link *kmulti_link;
2318 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2319 kvfree(kmulti_link->addrs);
2320 kvfree(kmulti_link->cookies);
2324 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
2325 .release = bpf_kprobe_multi_link_release,
2326 .dealloc = bpf_kprobe_multi_link_dealloc,
2329 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2331 const struct bpf_kprobe_multi_link *link = priv;
2332 unsigned long *addr_a = a, *addr_b = b;
2333 u64 *cookie_a, *cookie_b;
2335 cookie_a = link->cookies + (addr_a - link->addrs);
2336 cookie_b = link->cookies + (addr_b - link->addrs);
2338 /* swap addr_a/addr_b and cookie_a/cookie_b values */
2339 swap(*addr_a, *addr_b);
2340 swap(*cookie_a, *cookie_b);
2343 static int __bpf_kprobe_multi_cookie_cmp(const void *a, const void *b)
2345 const unsigned long *addr_a = a, *addr_b = b;
2347 if (*addr_a == *addr_b)
2349 return *addr_a < *addr_b ? -1 : 1;
2352 static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2354 return __bpf_kprobe_multi_cookie_cmp(a, b);
2357 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2359 struct bpf_kprobe_multi_run_ctx *run_ctx;
2360 struct bpf_kprobe_multi_link *link;
2361 u64 *cookie, entry_ip;
2362 unsigned long *addr;
2364 if (WARN_ON_ONCE(!ctx))
2366 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2367 link = run_ctx->link;
2370 entry_ip = run_ctx->entry_ip;
2371 addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
2372 __bpf_kprobe_multi_cookie_cmp);
2375 cookie = link->cookies + (addr - link->addrs);
2379 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2381 struct bpf_kprobe_multi_run_ctx *run_ctx;
2383 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2384 return run_ctx->entry_ip;
2388 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2389 unsigned long entry_ip, struct pt_regs *regs)
2391 struct bpf_kprobe_multi_run_ctx run_ctx = {
2393 .entry_ip = entry_ip,
2395 struct bpf_run_ctx *old_run_ctx;
2398 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
2405 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2406 err = bpf_prog_run(link->link.prog, regs);
2407 bpf_reset_run_ctx(old_run_ctx);
2412 __this_cpu_dec(bpf_prog_active);
2417 kprobe_multi_link_handler(struct fprobe *fp, unsigned long entry_ip,
2418 struct pt_regs *regs)
2420 struct bpf_kprobe_multi_link *link;
2422 link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2423 kprobe_multi_link_prog_run(link, entry_ip, regs);
2426 static int symbols_cmp(const void *a, const void *b)
2428 const char **str_a = (const char **) a;
2429 const char **str_b = (const char **) b;
2431 return strcmp(*str_a, *str_b);
2434 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2436 struct bpf_kprobe_multi_link *link = NULL;
2437 struct bpf_link_primer link_primer;
2438 void __user *ucookies;
2439 unsigned long *addrs;
2440 u32 flags, cnt, size;
2441 void __user *uaddrs;
2442 u64 *cookies = NULL;
2446 /* no support for 32bit archs yet */
2447 if (sizeof(u64) != sizeof(void *))
2450 if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
2453 flags = attr->link_create.kprobe_multi.flags;
2454 if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
2457 uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
2458 usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
2459 if (!!uaddrs == !!usyms)
2462 cnt = attr->link_create.kprobe_multi.cnt;
2466 size = cnt * sizeof(*addrs);
2467 addrs = kvmalloc(size, GFP_KERNEL);
2472 if (copy_from_user(addrs, uaddrs, size)) {
2477 struct user_syms us;
2479 err = copy_user_syms(&us, usyms, cnt);
2483 sort(us.syms, cnt, sizeof(*us.syms), symbols_cmp, NULL);
2484 err = ftrace_lookup_symbols(us.syms, cnt, addrs);
2485 free_user_syms(&us);
2490 ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2492 cookies = kvmalloc(size, GFP_KERNEL);
2497 if (copy_from_user(cookies, ucookies, size)) {
2503 link = kzalloc(sizeof(*link), GFP_KERNEL);
2509 bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
2510 &bpf_kprobe_multi_link_lops, prog);
2512 err = bpf_link_prime(&link->link, &link_primer);
2516 if (flags & BPF_F_KPROBE_MULTI_RETURN)
2517 link->fp.exit_handler = kprobe_multi_link_handler;
2519 link->fp.entry_handler = kprobe_multi_link_handler;
2521 link->addrs = addrs;
2522 link->cookies = cookies;
2527 * Sorting addresses will trigger sorting cookies as well
2528 * (check bpf_kprobe_multi_cookie_swap). This way we can
2529 * find cookie based on the address in bpf_get_attach_cookie
2532 sort_r(addrs, cnt, sizeof(*addrs),
2533 bpf_kprobe_multi_cookie_cmp,
2534 bpf_kprobe_multi_cookie_swap,
2538 err = register_fprobe_ips(&link->fp, addrs, cnt);
2540 bpf_link_cleanup(&link_primer);
2544 return bpf_link_settle(&link_primer);
2552 #else /* !CONFIG_FPROBE */
2553 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2557 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2561 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)