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>
23 #include <linux/key.h>
24 #include <linux/verification.h>
26 #include <net/bpf_sk_storage.h>
28 #include <uapi/linux/bpf.h>
29 #include <uapi/linux/btf.h>
33 #include "trace_probe.h"
36 #define CREATE_TRACE_POINTS
37 #include "bpf_trace.h"
39 #define bpf_event_rcu_dereference(p) \
40 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
43 struct bpf_trace_module {
44 struct module *module;
45 struct list_head list;
48 static LIST_HEAD(bpf_trace_modules);
49 static DEFINE_MUTEX(bpf_module_mutex);
51 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
53 struct bpf_raw_event_map *btp, *ret = NULL;
54 struct bpf_trace_module *btm;
57 mutex_lock(&bpf_module_mutex);
58 list_for_each_entry(btm, &bpf_trace_modules, list) {
59 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
60 btp = &btm->module->bpf_raw_events[i];
61 if (!strcmp(btp->tp->name, name)) {
62 if (try_module_get(btm->module))
69 mutex_unlock(&bpf_module_mutex);
73 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
77 #endif /* CONFIG_MODULES */
79 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
80 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
82 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
83 u64 flags, const struct btf **btf,
85 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
86 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
89 * trace_call_bpf - invoke BPF program
90 * @call: tracepoint event
91 * @ctx: opaque context pointer
93 * kprobe handlers execute BPF programs via this helper.
94 * Can be used from static tracepoints in the future.
96 * Return: BPF programs always return an integer which is interpreted by
98 * 0 - return from kprobe (event is filtered out)
99 * 1 - store kprobe event into ring buffer
100 * Other values are reserved and currently alias to 1
102 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
108 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
110 * since some bpf program is already running on this cpu,
111 * don't call into another bpf program (same or different)
112 * and don't send kprobe event into ring-buffer,
113 * so return zero here
120 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
121 * to all call sites, we did a bpf_prog_array_valid() there to check
122 * whether call->prog_array is empty or not, which is
123 * a heuristic to speed up execution.
125 * If bpf_prog_array_valid() fetched prog_array was
126 * non-NULL, we go into trace_call_bpf() and do the actual
127 * proper rcu_dereference() under RCU lock.
128 * If it turns out that prog_array is NULL then, we bail out.
129 * For the opposite, if the bpf_prog_array_valid() fetched pointer
130 * was NULL, you'll skip the prog_array with the risk of missing
131 * out of events when it was updated in between this and the
132 * rcu_dereference() which is accepted risk.
135 ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
140 __this_cpu_dec(bpf_prog_active);
145 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
146 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
148 regs_set_return_value(regs, rc);
149 override_function_with_return(regs);
153 static const struct bpf_func_proto bpf_override_return_proto = {
154 .func = bpf_override_return,
156 .ret_type = RET_INTEGER,
157 .arg1_type = ARG_PTR_TO_CTX,
158 .arg2_type = ARG_ANYTHING,
162 static __always_inline int
163 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
167 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
168 if (unlikely(ret < 0))
169 memset(dst, 0, size);
173 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
174 const void __user *, unsafe_ptr)
176 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
179 const struct bpf_func_proto bpf_probe_read_user_proto = {
180 .func = bpf_probe_read_user,
182 .ret_type = RET_INTEGER,
183 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
184 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
185 .arg3_type = ARG_ANYTHING,
188 static __always_inline int
189 bpf_probe_read_user_str_common(void *dst, u32 size,
190 const void __user *unsafe_ptr)
195 * NB: We rely on strncpy_from_user() not copying junk past the NUL
196 * terminator into `dst`.
198 * strncpy_from_user() does long-sized strides in the fast path. If the
199 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
200 * then there could be junk after the NUL in `dst`. If user takes `dst`
201 * and keys a hash map with it, then semantically identical strings can
202 * occupy multiple entries in the map.
204 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
205 if (unlikely(ret < 0))
206 memset(dst, 0, size);
210 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
211 const void __user *, unsafe_ptr)
213 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
216 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
217 .func = bpf_probe_read_user_str,
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_common(void *dst, u32 size, const void *unsafe_ptr)
230 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
231 if (unlikely(ret < 0))
232 memset(dst, 0, size);
236 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
237 const void *, unsafe_ptr)
239 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
242 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
243 .func = bpf_probe_read_kernel,
245 .ret_type = RET_INTEGER,
246 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
247 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
248 .arg3_type = ARG_ANYTHING,
251 static __always_inline int
252 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
257 * The strncpy_from_kernel_nofault() call will likely not fill the
258 * entire buffer, but that's okay in this circumstance as we're probing
259 * arbitrary memory anyway similar to bpf_probe_read_*() and might
260 * as well probe the stack. Thus, memory is explicitly cleared
261 * only in error case, so that improper users ignoring return
262 * code altogether don't copy garbage; otherwise length of string
263 * is returned that can be used for bpf_perf_event_output() et al.
265 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
266 if (unlikely(ret < 0))
267 memset(dst, 0, size);
271 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
272 const void *, unsafe_ptr)
274 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
277 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
278 .func = bpf_probe_read_kernel_str,
280 .ret_type = RET_INTEGER,
281 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
282 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
283 .arg3_type = ARG_ANYTHING,
286 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
287 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
288 const void *, unsafe_ptr)
290 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
291 return bpf_probe_read_user_common(dst, size,
292 (__force void __user *)unsafe_ptr);
294 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
297 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
298 .func = bpf_probe_read_compat,
300 .ret_type = RET_INTEGER,
301 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
302 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
303 .arg3_type = ARG_ANYTHING,
306 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
307 const void *, unsafe_ptr)
309 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
310 return bpf_probe_read_user_str_common(dst, size,
311 (__force void __user *)unsafe_ptr);
313 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
316 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
317 .func = bpf_probe_read_compat_str,
319 .ret_type = RET_INTEGER,
320 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
321 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
322 .arg3_type = ARG_ANYTHING,
324 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
326 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
330 * Ensure we're in user context which is safe for the helper to
331 * run. This helper has no business in a kthread.
333 * access_ok() should prevent writing to non-user memory, but in
334 * some situations (nommu, temporary switch, etc) access_ok() does
335 * not provide enough validation, hence the check on KERNEL_DS.
337 * nmi_uaccess_okay() ensures the probe is not run in an interim
338 * state, when the task or mm are switched. This is specifically
339 * required to prevent the use of temporary mm.
342 if (unlikely(in_interrupt() ||
343 current->flags & (PF_KTHREAD | PF_EXITING)))
345 if (unlikely(!nmi_uaccess_okay()))
348 return copy_to_user_nofault(unsafe_ptr, src, size);
351 static const struct bpf_func_proto bpf_probe_write_user_proto = {
352 .func = bpf_probe_write_user,
354 .ret_type = RET_INTEGER,
355 .arg1_type = ARG_ANYTHING,
356 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
357 .arg3_type = ARG_CONST_SIZE,
360 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
362 if (!capable(CAP_SYS_ADMIN))
365 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
366 current->comm, task_pid_nr(current));
368 return &bpf_probe_write_user_proto;
371 static DEFINE_RAW_SPINLOCK(trace_printk_lock);
373 #define MAX_TRACE_PRINTK_VARARGS 3
374 #define BPF_TRACE_PRINTK_SIZE 1024
376 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
377 u64, arg2, u64, arg3)
379 u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
381 static char buf[BPF_TRACE_PRINTK_SIZE];
385 ret = bpf_bprintf_prepare(fmt, fmt_size, args, &bin_args,
386 MAX_TRACE_PRINTK_VARARGS);
390 raw_spin_lock_irqsave(&trace_printk_lock, flags);
391 ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
393 trace_bpf_trace_printk(buf);
394 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
396 bpf_bprintf_cleanup();
401 static const struct bpf_func_proto bpf_trace_printk_proto = {
402 .func = bpf_trace_printk,
404 .ret_type = RET_INTEGER,
405 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
406 .arg2_type = ARG_CONST_SIZE,
409 static void __set_printk_clr_event(void)
412 * This program might be calling bpf_trace_printk,
413 * so enable the associated bpf_trace/bpf_trace_printk event.
414 * Repeat this each time as it is possible a user has
415 * disabled bpf_trace_printk events. By loading a program
416 * calling bpf_trace_printk() however the user has expressed
417 * the intent to see such events.
419 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
420 pr_warn_ratelimited("could not enable bpf_trace_printk events");
423 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
425 __set_printk_clr_event();
426 return &bpf_trace_printk_proto;
429 BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, data,
432 static char buf[BPF_TRACE_PRINTK_SIZE];
437 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
440 num_args = data_len / 8;
442 ret = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
446 raw_spin_lock_irqsave(&trace_printk_lock, flags);
447 ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
449 trace_bpf_trace_printk(buf);
450 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
452 bpf_bprintf_cleanup();
457 static const struct bpf_func_proto bpf_trace_vprintk_proto = {
458 .func = bpf_trace_vprintk,
460 .ret_type = RET_INTEGER,
461 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
462 .arg2_type = ARG_CONST_SIZE,
463 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
464 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
467 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
469 __set_printk_clr_event();
470 return &bpf_trace_vprintk_proto;
473 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
474 const void *, data, u32, data_len)
479 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
482 num_args = data_len / 8;
484 err = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
488 seq_bprintf(m, fmt, bin_args);
490 bpf_bprintf_cleanup();
492 return seq_has_overflowed(m) ? -EOVERFLOW : 0;
495 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
497 static const struct bpf_func_proto bpf_seq_printf_proto = {
498 .func = bpf_seq_printf,
500 .ret_type = RET_INTEGER,
501 .arg1_type = ARG_PTR_TO_BTF_ID,
502 .arg1_btf_id = &btf_seq_file_ids[0],
503 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
504 .arg3_type = ARG_CONST_SIZE,
505 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
506 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
509 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
511 return seq_write(m, data, len) ? -EOVERFLOW : 0;
514 static const struct bpf_func_proto bpf_seq_write_proto = {
515 .func = bpf_seq_write,
517 .ret_type = RET_INTEGER,
518 .arg1_type = ARG_PTR_TO_BTF_ID,
519 .arg1_btf_id = &btf_seq_file_ids[0],
520 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
521 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
524 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
525 u32, btf_ptr_size, u64, flags)
527 const struct btf *btf;
531 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
535 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
538 static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
539 .func = bpf_seq_printf_btf,
541 .ret_type = RET_INTEGER,
542 .arg1_type = ARG_PTR_TO_BTF_ID,
543 .arg1_btf_id = &btf_seq_file_ids[0],
544 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
545 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
546 .arg4_type = ARG_ANYTHING,
549 static __always_inline int
550 get_map_perf_counter(struct bpf_map *map, u64 flags,
551 u64 *value, u64 *enabled, u64 *running)
553 struct bpf_array *array = container_of(map, struct bpf_array, map);
554 unsigned int cpu = smp_processor_id();
555 u64 index = flags & BPF_F_INDEX_MASK;
556 struct bpf_event_entry *ee;
558 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
560 if (index == BPF_F_CURRENT_CPU)
562 if (unlikely(index >= array->map.max_entries))
565 ee = READ_ONCE(array->ptrs[index]);
569 return perf_event_read_local(ee->event, value, enabled, running);
572 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
577 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
579 * this api is ugly since we miss [-22..-2] range of valid
580 * counter values, but that's uapi
587 static const struct bpf_func_proto bpf_perf_event_read_proto = {
588 .func = bpf_perf_event_read,
590 .ret_type = RET_INTEGER,
591 .arg1_type = ARG_CONST_MAP_PTR,
592 .arg2_type = ARG_ANYTHING,
595 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
596 struct bpf_perf_event_value *, buf, u32, size)
600 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
602 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
608 memset(buf, 0, size);
612 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
613 .func = bpf_perf_event_read_value,
615 .ret_type = RET_INTEGER,
616 .arg1_type = ARG_CONST_MAP_PTR,
617 .arg2_type = ARG_ANYTHING,
618 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
619 .arg4_type = ARG_CONST_SIZE,
622 static __always_inline u64
623 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
624 u64 flags, struct perf_sample_data *sd)
626 struct bpf_array *array = container_of(map, struct bpf_array, map);
627 unsigned int cpu = smp_processor_id();
628 u64 index = flags & BPF_F_INDEX_MASK;
629 struct bpf_event_entry *ee;
630 struct perf_event *event;
632 if (index == BPF_F_CURRENT_CPU)
634 if (unlikely(index >= array->map.max_entries))
637 ee = READ_ONCE(array->ptrs[index]);
642 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
643 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
646 if (unlikely(event->oncpu != cpu))
649 return perf_event_output(event, sd, regs);
653 * Support executing tracepoints in normal, irq, and nmi context that each call
654 * bpf_perf_event_output
656 struct bpf_trace_sample_data {
657 struct perf_sample_data sds[3];
660 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
661 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
662 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
663 u64, flags, void *, data, u64, size)
665 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
666 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
667 struct perf_raw_record raw = {
673 struct perf_sample_data *sd;
676 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
681 sd = &sds->sds[nest_level - 1];
683 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
688 perf_sample_data_init(sd, 0, 0);
690 sd->sample_flags |= PERF_SAMPLE_RAW;
692 err = __bpf_perf_event_output(regs, map, flags, sd);
695 this_cpu_dec(bpf_trace_nest_level);
699 static const struct bpf_func_proto bpf_perf_event_output_proto = {
700 .func = bpf_perf_event_output,
702 .ret_type = RET_INTEGER,
703 .arg1_type = ARG_PTR_TO_CTX,
704 .arg2_type = ARG_CONST_MAP_PTR,
705 .arg3_type = ARG_ANYTHING,
706 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
707 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
710 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
711 struct bpf_nested_pt_regs {
712 struct pt_regs regs[3];
714 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
715 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
717 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
718 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
720 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
721 struct perf_raw_frag frag = {
726 struct perf_raw_record raw = {
729 .next = ctx_size ? &frag : NULL,
735 struct perf_sample_data *sd;
736 struct pt_regs *regs;
739 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
743 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
744 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
746 perf_fetch_caller_regs(regs);
747 perf_sample_data_init(sd, 0, 0);
749 sd->sample_flags |= PERF_SAMPLE_RAW;
751 ret = __bpf_perf_event_output(regs, map, flags, sd);
753 this_cpu_dec(bpf_event_output_nest_level);
757 BPF_CALL_0(bpf_get_current_task)
759 return (long) current;
762 const struct bpf_func_proto bpf_get_current_task_proto = {
763 .func = bpf_get_current_task,
765 .ret_type = RET_INTEGER,
768 BPF_CALL_0(bpf_get_current_task_btf)
770 return (unsigned long) current;
773 const struct bpf_func_proto bpf_get_current_task_btf_proto = {
774 .func = bpf_get_current_task_btf,
776 .ret_type = RET_PTR_TO_BTF_ID,
777 .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
780 BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
782 return (unsigned long) task_pt_regs(task);
785 BTF_ID_LIST(bpf_task_pt_regs_ids)
786 BTF_ID(struct, pt_regs)
788 const struct bpf_func_proto bpf_task_pt_regs_proto = {
789 .func = bpf_task_pt_regs,
791 .arg1_type = ARG_PTR_TO_BTF_ID,
792 .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
793 .ret_type = RET_PTR_TO_BTF_ID,
794 .ret_btf_id = &bpf_task_pt_regs_ids[0],
797 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
799 struct bpf_array *array = container_of(map, struct bpf_array, map);
802 if (unlikely(idx >= array->map.max_entries))
805 cgrp = READ_ONCE(array->ptrs[idx]);
809 return task_under_cgroup_hierarchy(current, cgrp);
812 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
813 .func = bpf_current_task_under_cgroup,
815 .ret_type = RET_INTEGER,
816 .arg1_type = ARG_CONST_MAP_PTR,
817 .arg2_type = ARG_ANYTHING,
820 struct send_signal_irq_work {
821 struct irq_work irq_work;
822 struct task_struct *task;
827 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
829 static void do_bpf_send_signal(struct irq_work *entry)
831 struct send_signal_irq_work *work;
833 work = container_of(entry, struct send_signal_irq_work, irq_work);
834 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
835 put_task_struct(work->task);
838 static int bpf_send_signal_common(u32 sig, enum pid_type type)
840 struct send_signal_irq_work *work = NULL;
842 /* Similar to bpf_probe_write_user, task needs to be
843 * in a sound condition and kernel memory access be
844 * permitted in order to send signal to the current
847 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
849 if (unlikely(!nmi_uaccess_okay()))
851 /* Task should not be pid=1 to avoid kernel panic. */
852 if (unlikely(is_global_init(current)))
855 if (irqs_disabled()) {
856 /* Do an early check on signal validity. Otherwise,
857 * the error is lost in deferred irq_work.
859 if (unlikely(!valid_signal(sig)))
862 work = this_cpu_ptr(&send_signal_work);
863 if (irq_work_is_busy(&work->irq_work))
866 /* Add the current task, which is the target of sending signal,
867 * to the irq_work. The current task may change when queued
868 * irq works get executed.
870 work->task = get_task_struct(current);
873 irq_work_queue(&work->irq_work);
877 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
880 BPF_CALL_1(bpf_send_signal, u32, sig)
882 return bpf_send_signal_common(sig, PIDTYPE_TGID);
885 static const struct bpf_func_proto bpf_send_signal_proto = {
886 .func = bpf_send_signal,
888 .ret_type = RET_INTEGER,
889 .arg1_type = ARG_ANYTHING,
892 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
894 return bpf_send_signal_common(sig, PIDTYPE_PID);
897 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
898 .func = bpf_send_signal_thread,
900 .ret_type = RET_INTEGER,
901 .arg1_type = ARG_ANYTHING,
904 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
914 * The path pointer is verified as trusted and safe to use,
915 * but let's double check it's valid anyway to workaround
916 * potentially broken verifier.
918 len = copy_from_kernel_nofault(©, path, sizeof(*path));
922 p = d_path(©, buf, sz);
927 memmove(buf, p, len);
933 BTF_SET_START(btf_allowlist_d_path)
934 #ifdef CONFIG_SECURITY
935 BTF_ID(func, security_file_permission)
936 BTF_ID(func, security_inode_getattr)
937 BTF_ID(func, security_file_open)
939 #ifdef CONFIG_SECURITY_PATH
940 BTF_ID(func, security_path_truncate)
942 BTF_ID(func, vfs_truncate)
943 BTF_ID(func, vfs_fallocate)
944 BTF_ID(func, dentry_open)
945 BTF_ID(func, vfs_getattr)
946 BTF_ID(func, filp_close)
947 BTF_SET_END(btf_allowlist_d_path)
949 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
951 if (prog->type == BPF_PROG_TYPE_TRACING &&
952 prog->expected_attach_type == BPF_TRACE_ITER)
955 if (prog->type == BPF_PROG_TYPE_LSM)
956 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
958 return btf_id_set_contains(&btf_allowlist_d_path,
959 prog->aux->attach_btf_id);
962 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
964 static const struct bpf_func_proto bpf_d_path_proto = {
967 .ret_type = RET_INTEGER,
968 .arg1_type = ARG_PTR_TO_BTF_ID,
969 .arg1_btf_id = &bpf_d_path_btf_ids[0],
970 .arg2_type = ARG_PTR_TO_MEM,
971 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
972 .allowed = bpf_d_path_allowed,
975 #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
976 BTF_F_PTR_RAW | BTF_F_ZERO)
978 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
979 u64 flags, const struct btf **btf,
982 const struct btf_type *t;
984 if (unlikely(flags & ~(BTF_F_ALL)))
987 if (btf_ptr_size != sizeof(struct btf_ptr))
990 *btf = bpf_get_btf_vmlinux();
992 if (IS_ERR_OR_NULL(*btf))
993 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
995 if (ptr->type_id > 0)
996 *btf_id = ptr->type_id;
1001 t = btf_type_by_id(*btf, *btf_id);
1002 if (*btf_id <= 0 || !t)
1008 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1009 u32, btf_ptr_size, u64, flags)
1011 const struct btf *btf;
1015 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1019 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1023 const struct bpf_func_proto bpf_snprintf_btf_proto = {
1024 .func = bpf_snprintf_btf,
1026 .ret_type = RET_INTEGER,
1027 .arg1_type = ARG_PTR_TO_MEM,
1028 .arg2_type = ARG_CONST_SIZE,
1029 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1030 .arg4_type = ARG_CONST_SIZE,
1031 .arg5_type = ARG_ANYTHING,
1034 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
1036 /* This helper call is inlined by verifier. */
1037 return ((u64 *)ctx)[-2];
1040 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
1041 .func = bpf_get_func_ip_tracing,
1043 .ret_type = RET_INTEGER,
1044 .arg1_type = ARG_PTR_TO_CTX,
1047 #ifdef CONFIG_X86_KERNEL_IBT
1048 static unsigned long get_entry_ip(unsigned long fentry_ip)
1052 /* Being extra safe in here in case entry ip is on the page-edge. */
1053 if (get_kernel_nofault(instr, (u32 *) fentry_ip - 1))
1055 if (is_endbr(instr))
1056 fentry_ip -= ENDBR_INSN_SIZE;
1060 #define get_entry_ip(fentry_ip) fentry_ip
1063 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1065 struct kprobe *kp = kprobe_running();
1067 if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
1070 return get_entry_ip((uintptr_t)kp->addr);
1073 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1074 .func = bpf_get_func_ip_kprobe,
1076 .ret_type = RET_INTEGER,
1077 .arg1_type = ARG_PTR_TO_CTX,
1080 BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
1082 return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
1085 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
1086 .func = bpf_get_func_ip_kprobe_multi,
1088 .ret_type = RET_INTEGER,
1089 .arg1_type = ARG_PTR_TO_CTX,
1092 BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1094 return bpf_kprobe_multi_cookie(current->bpf_ctx);
1097 static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1098 .func = bpf_get_attach_cookie_kprobe_multi,
1100 .ret_type = RET_INTEGER,
1101 .arg1_type = ARG_PTR_TO_CTX,
1104 BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
1106 struct bpf_trace_run_ctx *run_ctx;
1108 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1109 return run_ctx->bpf_cookie;
1112 static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
1113 .func = bpf_get_attach_cookie_trace,
1115 .ret_type = RET_INTEGER,
1116 .arg1_type = ARG_PTR_TO_CTX,
1119 BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
1121 return ctx->event->bpf_cookie;
1124 static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
1125 .func = bpf_get_attach_cookie_pe,
1127 .ret_type = RET_INTEGER,
1128 .arg1_type = ARG_PTR_TO_CTX,
1131 BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
1133 struct bpf_trace_run_ctx *run_ctx;
1135 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1136 return run_ctx->bpf_cookie;
1139 static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
1140 .func = bpf_get_attach_cookie_tracing,
1142 .ret_type = RET_INTEGER,
1143 .arg1_type = ARG_PTR_TO_CTX,
1146 BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1151 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1152 u32 entry_cnt = size / br_entry_size;
1154 entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1156 if (unlikely(flags))
1162 return entry_cnt * br_entry_size;
1166 static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1167 .func = bpf_get_branch_snapshot,
1169 .ret_type = RET_INTEGER,
1170 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1171 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1174 BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1176 /* This helper call is inlined by verifier. */
1177 u64 nr_args = ((u64 *)ctx)[-1];
1179 if ((u64) n >= nr_args)
1181 *value = ((u64 *)ctx)[n];
1185 static const struct bpf_func_proto bpf_get_func_arg_proto = {
1186 .func = get_func_arg,
1187 .ret_type = RET_INTEGER,
1188 .arg1_type = ARG_PTR_TO_CTX,
1189 .arg2_type = ARG_ANYTHING,
1190 .arg3_type = ARG_PTR_TO_LONG,
1193 BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1195 /* This helper call is inlined by verifier. */
1196 u64 nr_args = ((u64 *)ctx)[-1];
1198 *value = ((u64 *)ctx)[nr_args];
1202 static const struct bpf_func_proto bpf_get_func_ret_proto = {
1203 .func = get_func_ret,
1204 .ret_type = RET_INTEGER,
1205 .arg1_type = ARG_PTR_TO_CTX,
1206 .arg2_type = ARG_PTR_TO_LONG,
1209 BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1211 /* This helper call is inlined by verifier. */
1212 return ((u64 *)ctx)[-1];
1215 static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1216 .func = get_func_arg_cnt,
1217 .ret_type = RET_INTEGER,
1218 .arg1_type = ARG_PTR_TO_CTX,
1223 __diag_ignore_all("-Wmissing-prototypes",
1224 "kfuncs which will be used in BPF programs");
1227 * bpf_lookup_user_key - lookup a key by its serial
1228 * @serial: key handle serial number
1229 * @flags: lookup-specific flags
1231 * Search a key with a given *serial* and the provided *flags*.
1232 * If found, increment the reference count of the key by one, and
1233 * return it in the bpf_key structure.
1235 * The bpf_key structure must be passed to bpf_key_put() when done
1236 * with it, so that the key reference count is decremented and the
1237 * bpf_key structure is freed.
1239 * Permission checks are deferred to the time the key is used by
1240 * one of the available key-specific kfuncs.
1242 * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
1243 * special keyring (e.g. session keyring), if it doesn't yet exist.
1244 * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
1245 * for the key construction, and to retrieve uninstantiated keys (keys
1246 * without data attached to them).
1248 * Return: a bpf_key pointer with a valid key pointer if the key is found, a
1249 * NULL pointer otherwise.
1251 struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
1254 struct bpf_key *bkey;
1256 if (flags & ~KEY_LOOKUP_ALL)
1260 * Permission check is deferred until the key is used, as the
1261 * intent of the caller is unknown here.
1263 key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
1264 if (IS_ERR(key_ref))
1267 bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
1269 key_put(key_ref_to_ptr(key_ref));
1273 bkey->key = key_ref_to_ptr(key_ref);
1274 bkey->has_ref = true;
1280 * bpf_lookup_system_key - lookup a key by a system-defined ID
1283 * Obtain a bpf_key structure with a key pointer set to the passed key ID.
1284 * The key pointer is marked as invalid, to prevent bpf_key_put() from
1285 * attempting to decrement the key reference count on that pointer. The key
1286 * pointer set in such way is currently understood only by
1287 * verify_pkcs7_signature().
1289 * Set *id* to one of the values defined in include/linux/verification.h:
1290 * 0 for the primary keyring (immutable keyring of system keys);
1291 * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
1292 * (where keys can be added only if they are vouched for by existing keys
1293 * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
1294 * keyring (primarily used by the integrity subsystem to verify a kexec'ed
1295 * kerned image and, possibly, the initramfs signature).
1297 * Return: a bpf_key pointer with an invalid key pointer set from the
1298 * pre-determined ID on success, a NULL pointer otherwise
1300 struct bpf_key *bpf_lookup_system_key(u64 id)
1302 struct bpf_key *bkey;
1304 if (system_keyring_id_check(id) < 0)
1307 bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
1311 bkey->key = (struct key *)(unsigned long)id;
1312 bkey->has_ref = false;
1318 * bpf_key_put - decrement key reference count if key is valid and free bpf_key
1319 * @bkey: bpf_key structure
1321 * Decrement the reference count of the key inside *bkey*, if the pointer
1322 * is valid, and free *bkey*.
1324 void bpf_key_put(struct bpf_key *bkey)
1332 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1334 * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
1335 * @data_ptr: data to verify
1336 * @sig_ptr: signature of the data
1337 * @trusted_keyring: keyring with keys trusted for signature verification
1339 * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
1340 * with keys in a keyring referenced by *trusted_keyring*.
1342 * Return: 0 on success, a negative value on error.
1344 int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
1345 struct bpf_dynptr_kern *sig_ptr,
1346 struct bpf_key *trusted_keyring)
1350 if (trusted_keyring->has_ref) {
1352 * Do the permission check deferred in bpf_lookup_user_key().
1353 * See bpf_lookup_user_key() for more details.
1355 * A call to key_task_permission() here would be redundant, as
1356 * it is already done by keyring_search() called by
1357 * find_asymmetric_key().
1359 ret = key_validate(trusted_keyring->key);
1364 return verify_pkcs7_signature(data_ptr->data,
1365 bpf_dynptr_get_size(data_ptr),
1367 bpf_dynptr_get_size(sig_ptr),
1368 trusted_keyring->key,
1369 VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
1372 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
1376 BTF_SET8_START(key_sig_kfunc_set)
1377 BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
1378 BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
1379 BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
1380 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1381 BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
1383 BTF_SET8_END(key_sig_kfunc_set)
1385 static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
1386 .owner = THIS_MODULE,
1387 .set = &key_sig_kfunc_set,
1390 static int __init bpf_key_sig_kfuncs_init(void)
1392 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
1393 &bpf_key_sig_kfunc_set);
1396 late_initcall(bpf_key_sig_kfuncs_init);
1397 #endif /* CONFIG_KEYS */
1399 static const struct bpf_func_proto *
1400 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1403 case BPF_FUNC_map_lookup_elem:
1404 return &bpf_map_lookup_elem_proto;
1405 case BPF_FUNC_map_update_elem:
1406 return &bpf_map_update_elem_proto;
1407 case BPF_FUNC_map_delete_elem:
1408 return &bpf_map_delete_elem_proto;
1409 case BPF_FUNC_map_push_elem:
1410 return &bpf_map_push_elem_proto;
1411 case BPF_FUNC_map_pop_elem:
1412 return &bpf_map_pop_elem_proto;
1413 case BPF_FUNC_map_peek_elem:
1414 return &bpf_map_peek_elem_proto;
1415 case BPF_FUNC_map_lookup_percpu_elem:
1416 return &bpf_map_lookup_percpu_elem_proto;
1417 case BPF_FUNC_ktime_get_ns:
1418 return &bpf_ktime_get_ns_proto;
1419 case BPF_FUNC_ktime_get_boot_ns:
1420 return &bpf_ktime_get_boot_ns_proto;
1421 case BPF_FUNC_tail_call:
1422 return &bpf_tail_call_proto;
1423 case BPF_FUNC_get_current_pid_tgid:
1424 return &bpf_get_current_pid_tgid_proto;
1425 case BPF_FUNC_get_current_task:
1426 return &bpf_get_current_task_proto;
1427 case BPF_FUNC_get_current_task_btf:
1428 return &bpf_get_current_task_btf_proto;
1429 case BPF_FUNC_task_pt_regs:
1430 return &bpf_task_pt_regs_proto;
1431 case BPF_FUNC_get_current_uid_gid:
1432 return &bpf_get_current_uid_gid_proto;
1433 case BPF_FUNC_get_current_comm:
1434 return &bpf_get_current_comm_proto;
1435 case BPF_FUNC_trace_printk:
1436 return bpf_get_trace_printk_proto();
1437 case BPF_FUNC_get_smp_processor_id:
1438 return &bpf_get_smp_processor_id_proto;
1439 case BPF_FUNC_get_numa_node_id:
1440 return &bpf_get_numa_node_id_proto;
1441 case BPF_FUNC_perf_event_read:
1442 return &bpf_perf_event_read_proto;
1443 case BPF_FUNC_current_task_under_cgroup:
1444 return &bpf_current_task_under_cgroup_proto;
1445 case BPF_FUNC_get_prandom_u32:
1446 return &bpf_get_prandom_u32_proto;
1447 case BPF_FUNC_probe_write_user:
1448 return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1449 NULL : bpf_get_probe_write_proto();
1450 case BPF_FUNC_probe_read_user:
1451 return &bpf_probe_read_user_proto;
1452 case BPF_FUNC_probe_read_kernel:
1453 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1454 NULL : &bpf_probe_read_kernel_proto;
1455 case BPF_FUNC_probe_read_user_str:
1456 return &bpf_probe_read_user_str_proto;
1457 case BPF_FUNC_probe_read_kernel_str:
1458 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1459 NULL : &bpf_probe_read_kernel_str_proto;
1460 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1461 case BPF_FUNC_probe_read:
1462 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1463 NULL : &bpf_probe_read_compat_proto;
1464 case BPF_FUNC_probe_read_str:
1465 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1466 NULL : &bpf_probe_read_compat_str_proto;
1468 #ifdef CONFIG_CGROUPS
1469 case BPF_FUNC_get_current_cgroup_id:
1470 return &bpf_get_current_cgroup_id_proto;
1471 case BPF_FUNC_get_current_ancestor_cgroup_id:
1472 return &bpf_get_current_ancestor_cgroup_id_proto;
1474 case BPF_FUNC_send_signal:
1475 return &bpf_send_signal_proto;
1476 case BPF_FUNC_send_signal_thread:
1477 return &bpf_send_signal_thread_proto;
1478 case BPF_FUNC_perf_event_read_value:
1479 return &bpf_perf_event_read_value_proto;
1480 case BPF_FUNC_get_ns_current_pid_tgid:
1481 return &bpf_get_ns_current_pid_tgid_proto;
1482 case BPF_FUNC_ringbuf_output:
1483 return &bpf_ringbuf_output_proto;
1484 case BPF_FUNC_ringbuf_reserve:
1485 return &bpf_ringbuf_reserve_proto;
1486 case BPF_FUNC_ringbuf_submit:
1487 return &bpf_ringbuf_submit_proto;
1488 case BPF_FUNC_ringbuf_discard:
1489 return &bpf_ringbuf_discard_proto;
1490 case BPF_FUNC_ringbuf_query:
1491 return &bpf_ringbuf_query_proto;
1492 case BPF_FUNC_jiffies64:
1493 return &bpf_jiffies64_proto;
1494 case BPF_FUNC_get_task_stack:
1495 return &bpf_get_task_stack_proto;
1496 case BPF_FUNC_copy_from_user:
1497 return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
1498 case BPF_FUNC_copy_from_user_task:
1499 return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
1500 case BPF_FUNC_snprintf_btf:
1501 return &bpf_snprintf_btf_proto;
1502 case BPF_FUNC_per_cpu_ptr:
1503 return &bpf_per_cpu_ptr_proto;
1504 case BPF_FUNC_this_cpu_ptr:
1505 return &bpf_this_cpu_ptr_proto;
1506 case BPF_FUNC_task_storage_get:
1507 return &bpf_task_storage_get_proto;
1508 case BPF_FUNC_task_storage_delete:
1509 return &bpf_task_storage_delete_proto;
1510 case BPF_FUNC_for_each_map_elem:
1511 return &bpf_for_each_map_elem_proto;
1512 case BPF_FUNC_snprintf:
1513 return &bpf_snprintf_proto;
1514 case BPF_FUNC_get_func_ip:
1515 return &bpf_get_func_ip_proto_tracing;
1516 case BPF_FUNC_get_branch_snapshot:
1517 return &bpf_get_branch_snapshot_proto;
1518 case BPF_FUNC_find_vma:
1519 return &bpf_find_vma_proto;
1520 case BPF_FUNC_trace_vprintk:
1521 return bpf_get_trace_vprintk_proto();
1523 return bpf_base_func_proto(func_id);
1527 static const struct bpf_func_proto *
1528 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1531 case BPF_FUNC_perf_event_output:
1532 return &bpf_perf_event_output_proto;
1533 case BPF_FUNC_get_stackid:
1534 return &bpf_get_stackid_proto;
1535 case BPF_FUNC_get_stack:
1536 return &bpf_get_stack_proto;
1537 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1538 case BPF_FUNC_override_return:
1539 return &bpf_override_return_proto;
1541 case BPF_FUNC_get_func_ip:
1542 return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1543 &bpf_get_func_ip_proto_kprobe_multi :
1544 &bpf_get_func_ip_proto_kprobe;
1545 case BPF_FUNC_get_attach_cookie:
1546 return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1547 &bpf_get_attach_cookie_proto_kmulti :
1548 &bpf_get_attach_cookie_proto_trace;
1550 return bpf_tracing_func_proto(func_id, prog);
1554 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1555 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1556 const struct bpf_prog *prog,
1557 struct bpf_insn_access_aux *info)
1559 if (off < 0 || off >= sizeof(struct pt_regs))
1561 if (type != BPF_READ)
1563 if (off % size != 0)
1566 * Assertion for 32 bit to make sure last 8 byte access
1567 * (BPF_DW) to the last 4 byte member is disallowed.
1569 if (off + size > sizeof(struct pt_regs))
1575 const struct bpf_verifier_ops kprobe_verifier_ops = {
1576 .get_func_proto = kprobe_prog_func_proto,
1577 .is_valid_access = kprobe_prog_is_valid_access,
1580 const struct bpf_prog_ops kprobe_prog_ops = {
1583 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1584 u64, flags, void *, data, u64, size)
1586 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1589 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1590 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1591 * from there and call the same bpf_perf_event_output() helper inline.
1593 return ____bpf_perf_event_output(regs, map, flags, data, size);
1596 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1597 .func = bpf_perf_event_output_tp,
1599 .ret_type = RET_INTEGER,
1600 .arg1_type = ARG_PTR_TO_CTX,
1601 .arg2_type = ARG_CONST_MAP_PTR,
1602 .arg3_type = ARG_ANYTHING,
1603 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1604 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1607 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1610 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1613 * Same comment as in bpf_perf_event_output_tp(), only that this time
1614 * the other helper's function body cannot be inlined due to being
1615 * external, thus we need to call raw helper function.
1617 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1621 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1622 .func = bpf_get_stackid_tp,
1624 .ret_type = RET_INTEGER,
1625 .arg1_type = ARG_PTR_TO_CTX,
1626 .arg2_type = ARG_CONST_MAP_PTR,
1627 .arg3_type = ARG_ANYTHING,
1630 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1633 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1635 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1636 (unsigned long) size, flags, 0);
1639 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1640 .func = bpf_get_stack_tp,
1642 .ret_type = RET_INTEGER,
1643 .arg1_type = ARG_PTR_TO_CTX,
1644 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1645 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1646 .arg4_type = ARG_ANYTHING,
1649 static const struct bpf_func_proto *
1650 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1653 case BPF_FUNC_perf_event_output:
1654 return &bpf_perf_event_output_proto_tp;
1655 case BPF_FUNC_get_stackid:
1656 return &bpf_get_stackid_proto_tp;
1657 case BPF_FUNC_get_stack:
1658 return &bpf_get_stack_proto_tp;
1659 case BPF_FUNC_get_attach_cookie:
1660 return &bpf_get_attach_cookie_proto_trace;
1662 return bpf_tracing_func_proto(func_id, prog);
1666 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1667 const struct bpf_prog *prog,
1668 struct bpf_insn_access_aux *info)
1670 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1672 if (type != BPF_READ)
1674 if (off % size != 0)
1677 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1681 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1682 .get_func_proto = tp_prog_func_proto,
1683 .is_valid_access = tp_prog_is_valid_access,
1686 const struct bpf_prog_ops tracepoint_prog_ops = {
1689 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1690 struct bpf_perf_event_value *, buf, u32, size)
1694 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1696 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1702 memset(buf, 0, size);
1706 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1707 .func = bpf_perf_prog_read_value,
1709 .ret_type = RET_INTEGER,
1710 .arg1_type = ARG_PTR_TO_CTX,
1711 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1712 .arg3_type = ARG_CONST_SIZE,
1715 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1716 void *, buf, u32, size, u64, flags)
1718 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1719 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1722 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1725 if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
1728 if (unlikely(!br_stack))
1731 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1732 return br_stack->nr * br_entry_size;
1734 if (!buf || (size % br_entry_size != 0))
1737 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1738 memcpy(buf, br_stack->entries, to_copy);
1743 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1744 .func = bpf_read_branch_records,
1746 .ret_type = RET_INTEGER,
1747 .arg1_type = ARG_PTR_TO_CTX,
1748 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1749 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1750 .arg4_type = ARG_ANYTHING,
1753 static const struct bpf_func_proto *
1754 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1757 case BPF_FUNC_perf_event_output:
1758 return &bpf_perf_event_output_proto_tp;
1759 case BPF_FUNC_get_stackid:
1760 return &bpf_get_stackid_proto_pe;
1761 case BPF_FUNC_get_stack:
1762 return &bpf_get_stack_proto_pe;
1763 case BPF_FUNC_perf_prog_read_value:
1764 return &bpf_perf_prog_read_value_proto;
1765 case BPF_FUNC_read_branch_records:
1766 return &bpf_read_branch_records_proto;
1767 case BPF_FUNC_get_attach_cookie:
1768 return &bpf_get_attach_cookie_proto_pe;
1770 return bpf_tracing_func_proto(func_id, prog);
1775 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1776 * to avoid potential recursive reuse issue when/if tracepoints are added
1777 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1779 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1780 * in normal, irq, and nmi context.
1782 struct bpf_raw_tp_regs {
1783 struct pt_regs regs[3];
1785 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1786 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1787 static struct pt_regs *get_bpf_raw_tp_regs(void)
1789 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1790 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1792 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1793 this_cpu_dec(bpf_raw_tp_nest_level);
1794 return ERR_PTR(-EBUSY);
1797 return &tp_regs->regs[nest_level - 1];
1800 static void put_bpf_raw_tp_regs(void)
1802 this_cpu_dec(bpf_raw_tp_nest_level);
1805 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1806 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1808 struct pt_regs *regs = get_bpf_raw_tp_regs();
1812 return PTR_ERR(regs);
1814 perf_fetch_caller_regs(regs);
1815 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1817 put_bpf_raw_tp_regs();
1821 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1822 .func = bpf_perf_event_output_raw_tp,
1824 .ret_type = RET_INTEGER,
1825 .arg1_type = ARG_PTR_TO_CTX,
1826 .arg2_type = ARG_CONST_MAP_PTR,
1827 .arg3_type = ARG_ANYTHING,
1828 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1829 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1832 extern const struct bpf_func_proto bpf_skb_output_proto;
1833 extern const struct bpf_func_proto bpf_xdp_output_proto;
1834 extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1836 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1837 struct bpf_map *, map, u64, flags)
1839 struct pt_regs *regs = get_bpf_raw_tp_regs();
1843 return PTR_ERR(regs);
1845 perf_fetch_caller_regs(regs);
1846 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1847 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1849 put_bpf_raw_tp_regs();
1853 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1854 .func = bpf_get_stackid_raw_tp,
1856 .ret_type = RET_INTEGER,
1857 .arg1_type = ARG_PTR_TO_CTX,
1858 .arg2_type = ARG_CONST_MAP_PTR,
1859 .arg3_type = ARG_ANYTHING,
1862 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1863 void *, buf, u32, size, u64, flags)
1865 struct pt_regs *regs = get_bpf_raw_tp_regs();
1869 return PTR_ERR(regs);
1871 perf_fetch_caller_regs(regs);
1872 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1873 (unsigned long) size, flags, 0);
1874 put_bpf_raw_tp_regs();
1878 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1879 .func = bpf_get_stack_raw_tp,
1881 .ret_type = RET_INTEGER,
1882 .arg1_type = ARG_PTR_TO_CTX,
1883 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1884 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1885 .arg4_type = ARG_ANYTHING,
1888 static const struct bpf_func_proto *
1889 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1892 case BPF_FUNC_perf_event_output:
1893 return &bpf_perf_event_output_proto_raw_tp;
1894 case BPF_FUNC_get_stackid:
1895 return &bpf_get_stackid_proto_raw_tp;
1896 case BPF_FUNC_get_stack:
1897 return &bpf_get_stack_proto_raw_tp;
1899 return bpf_tracing_func_proto(func_id, prog);
1903 const struct bpf_func_proto *
1904 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1906 const struct bpf_func_proto *fn;
1910 case BPF_FUNC_skb_output:
1911 return &bpf_skb_output_proto;
1912 case BPF_FUNC_xdp_output:
1913 return &bpf_xdp_output_proto;
1914 case BPF_FUNC_skc_to_tcp6_sock:
1915 return &bpf_skc_to_tcp6_sock_proto;
1916 case BPF_FUNC_skc_to_tcp_sock:
1917 return &bpf_skc_to_tcp_sock_proto;
1918 case BPF_FUNC_skc_to_tcp_timewait_sock:
1919 return &bpf_skc_to_tcp_timewait_sock_proto;
1920 case BPF_FUNC_skc_to_tcp_request_sock:
1921 return &bpf_skc_to_tcp_request_sock_proto;
1922 case BPF_FUNC_skc_to_udp6_sock:
1923 return &bpf_skc_to_udp6_sock_proto;
1924 case BPF_FUNC_skc_to_unix_sock:
1925 return &bpf_skc_to_unix_sock_proto;
1926 case BPF_FUNC_skc_to_mptcp_sock:
1927 return &bpf_skc_to_mptcp_sock_proto;
1928 case BPF_FUNC_sk_storage_get:
1929 return &bpf_sk_storage_get_tracing_proto;
1930 case BPF_FUNC_sk_storage_delete:
1931 return &bpf_sk_storage_delete_tracing_proto;
1932 case BPF_FUNC_sock_from_file:
1933 return &bpf_sock_from_file_proto;
1934 case BPF_FUNC_get_socket_cookie:
1935 return &bpf_get_socket_ptr_cookie_proto;
1936 case BPF_FUNC_xdp_get_buff_len:
1937 return &bpf_xdp_get_buff_len_trace_proto;
1939 case BPF_FUNC_seq_printf:
1940 return prog->expected_attach_type == BPF_TRACE_ITER ?
1941 &bpf_seq_printf_proto :
1943 case BPF_FUNC_seq_write:
1944 return prog->expected_attach_type == BPF_TRACE_ITER ?
1945 &bpf_seq_write_proto :
1947 case BPF_FUNC_seq_printf_btf:
1948 return prog->expected_attach_type == BPF_TRACE_ITER ?
1949 &bpf_seq_printf_btf_proto :
1951 case BPF_FUNC_d_path:
1952 return &bpf_d_path_proto;
1953 case BPF_FUNC_get_func_arg:
1954 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1955 case BPF_FUNC_get_func_ret:
1956 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
1957 case BPF_FUNC_get_func_arg_cnt:
1958 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
1959 case BPF_FUNC_get_attach_cookie:
1960 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
1962 fn = raw_tp_prog_func_proto(func_id, prog);
1963 if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
1964 fn = bpf_iter_get_func_proto(func_id, prog);
1969 static bool raw_tp_prog_is_valid_access(int off, int size,
1970 enum bpf_access_type type,
1971 const struct bpf_prog *prog,
1972 struct bpf_insn_access_aux *info)
1974 return bpf_tracing_ctx_access(off, size, type);
1977 static bool tracing_prog_is_valid_access(int off, int size,
1978 enum bpf_access_type type,
1979 const struct bpf_prog *prog,
1980 struct bpf_insn_access_aux *info)
1982 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
1985 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1986 const union bpf_attr *kattr,
1987 union bpf_attr __user *uattr)
1992 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1993 .get_func_proto = raw_tp_prog_func_proto,
1994 .is_valid_access = raw_tp_prog_is_valid_access,
1997 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1999 .test_run = bpf_prog_test_run_raw_tp,
2003 const struct bpf_verifier_ops tracing_verifier_ops = {
2004 .get_func_proto = tracing_prog_func_proto,
2005 .is_valid_access = tracing_prog_is_valid_access,
2008 const struct bpf_prog_ops tracing_prog_ops = {
2009 .test_run = bpf_prog_test_run_tracing,
2012 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
2013 enum bpf_access_type type,
2014 const struct bpf_prog *prog,
2015 struct bpf_insn_access_aux *info)
2018 if (size != sizeof(u64) || type != BPF_READ)
2020 info->reg_type = PTR_TO_TP_BUFFER;
2022 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
2025 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
2026 .get_func_proto = raw_tp_prog_func_proto,
2027 .is_valid_access = raw_tp_writable_prog_is_valid_access,
2030 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
2033 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
2034 const struct bpf_prog *prog,
2035 struct bpf_insn_access_aux *info)
2037 const int size_u64 = sizeof(u64);
2039 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
2041 if (type != BPF_READ)
2043 if (off % size != 0) {
2044 if (sizeof(unsigned long) != 4)
2048 if (off % size != 4)
2053 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
2054 bpf_ctx_record_field_size(info, size_u64);
2055 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2058 case bpf_ctx_range(struct bpf_perf_event_data, addr):
2059 bpf_ctx_record_field_size(info, size_u64);
2060 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2064 if (size != sizeof(long))
2071 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
2072 const struct bpf_insn *si,
2073 struct bpf_insn *insn_buf,
2074 struct bpf_prog *prog, u32 *target_size)
2076 struct bpf_insn *insn = insn_buf;
2079 case offsetof(struct bpf_perf_event_data, sample_period):
2080 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2081 data), si->dst_reg, si->src_reg,
2082 offsetof(struct bpf_perf_event_data_kern, data));
2083 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2084 bpf_target_off(struct perf_sample_data, period, 8,
2087 case offsetof(struct bpf_perf_event_data, addr):
2088 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2089 data), si->dst_reg, si->src_reg,
2090 offsetof(struct bpf_perf_event_data_kern, data));
2091 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2092 bpf_target_off(struct perf_sample_data, addr, 8,
2096 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2097 regs), si->dst_reg, si->src_reg,
2098 offsetof(struct bpf_perf_event_data_kern, regs));
2099 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
2104 return insn - insn_buf;
2107 const struct bpf_verifier_ops perf_event_verifier_ops = {
2108 .get_func_proto = pe_prog_func_proto,
2109 .is_valid_access = pe_prog_is_valid_access,
2110 .convert_ctx_access = pe_prog_convert_ctx_access,
2113 const struct bpf_prog_ops perf_event_prog_ops = {
2116 static DEFINE_MUTEX(bpf_event_mutex);
2118 #define BPF_TRACE_MAX_PROGS 64
2120 int perf_event_attach_bpf_prog(struct perf_event *event,
2121 struct bpf_prog *prog,
2124 struct bpf_prog_array *old_array;
2125 struct bpf_prog_array *new_array;
2129 * Kprobe override only works if they are on the function entry,
2130 * and only if they are on the opt-in list.
2132 if (prog->kprobe_override &&
2133 (!trace_kprobe_on_func_entry(event->tp_event) ||
2134 !trace_kprobe_error_injectable(event->tp_event)))
2137 mutex_lock(&bpf_event_mutex);
2142 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2144 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
2149 ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
2153 /* set the new array to event->tp_event and set event->prog */
2155 event->bpf_cookie = bpf_cookie;
2156 rcu_assign_pointer(event->tp_event->prog_array, new_array);
2157 bpf_prog_array_free_sleepable(old_array);
2160 mutex_unlock(&bpf_event_mutex);
2164 void perf_event_detach_bpf_prog(struct perf_event *event)
2166 struct bpf_prog_array *old_array;
2167 struct bpf_prog_array *new_array;
2170 mutex_lock(&bpf_event_mutex);
2175 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2176 ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
2180 bpf_prog_array_delete_safe(old_array, event->prog);
2182 rcu_assign_pointer(event->tp_event->prog_array, new_array);
2183 bpf_prog_array_free_sleepable(old_array);
2186 bpf_prog_put(event->prog);
2190 mutex_unlock(&bpf_event_mutex);
2193 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
2195 struct perf_event_query_bpf __user *uquery = info;
2196 struct perf_event_query_bpf query = {};
2197 struct bpf_prog_array *progs;
2198 u32 *ids, prog_cnt, ids_len;
2201 if (!perfmon_capable())
2203 if (event->attr.type != PERF_TYPE_TRACEPOINT)
2205 if (copy_from_user(&query, uquery, sizeof(query)))
2208 ids_len = query.ids_len;
2209 if (ids_len > BPF_TRACE_MAX_PROGS)
2211 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2215 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
2216 * is required when user only wants to check for uquery->prog_cnt.
2217 * There is no need to check for it since the case is handled
2218 * gracefully in bpf_prog_array_copy_info.
2221 mutex_lock(&bpf_event_mutex);
2222 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2223 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2224 mutex_unlock(&bpf_event_mutex);
2226 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2227 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2234 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2235 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2237 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2239 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2241 for (; btp < __stop__bpf_raw_tp; btp++) {
2242 if (!strcmp(btp->tp->name, name))
2246 return bpf_get_raw_tracepoint_module(name);
2249 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2254 mod = __module_address((unsigned long)btp);
2259 static __always_inline
2260 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2263 if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
2264 bpf_prog_inc_misses_counter(prog);
2268 (void) bpf_prog_run(prog, args);
2271 this_cpu_dec(*(prog->active));
2274 #define UNPACK(...) __VA_ARGS__
2275 #define REPEAT_1(FN, DL, X, ...) FN(X)
2276 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2277 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2278 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2279 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2280 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2281 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2282 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2283 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2284 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2285 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2286 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2287 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2289 #define SARG(X) u64 arg##X
2290 #define COPY(X) args[X] = arg##X
2292 #define __DL_COM (,)
2293 #define __DL_SEM (;)
2295 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2297 #define BPF_TRACE_DEFN_x(x) \
2298 void bpf_trace_run##x(struct bpf_prog *prog, \
2299 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2302 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2303 __bpf_trace_run(prog, args); \
2305 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2306 BPF_TRACE_DEFN_x(1);
2307 BPF_TRACE_DEFN_x(2);
2308 BPF_TRACE_DEFN_x(3);
2309 BPF_TRACE_DEFN_x(4);
2310 BPF_TRACE_DEFN_x(5);
2311 BPF_TRACE_DEFN_x(6);
2312 BPF_TRACE_DEFN_x(7);
2313 BPF_TRACE_DEFN_x(8);
2314 BPF_TRACE_DEFN_x(9);
2315 BPF_TRACE_DEFN_x(10);
2316 BPF_TRACE_DEFN_x(11);
2317 BPF_TRACE_DEFN_x(12);
2319 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2321 struct tracepoint *tp = btp->tp;
2324 * check that program doesn't access arguments beyond what's
2325 * available in this tracepoint
2327 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2330 if (prog->aux->max_tp_access > btp->writable_size)
2333 return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
2337 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2339 return __bpf_probe_register(btp, prog);
2342 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2344 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2347 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2348 u32 *fd_type, const char **buf,
2349 u64 *probe_offset, u64 *probe_addr)
2351 bool is_tracepoint, is_syscall_tp;
2352 struct bpf_prog *prog;
2359 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2360 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2363 *prog_id = prog->aux->id;
2364 flags = event->tp_event->flags;
2365 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2366 is_syscall_tp = is_syscall_trace_event(event->tp_event);
2368 if (is_tracepoint || is_syscall_tp) {
2369 *buf = is_tracepoint ? event->tp_event->tp->name
2370 : event->tp_event->name;
2371 *fd_type = BPF_FD_TYPE_TRACEPOINT;
2372 *probe_offset = 0x0;
2377 #ifdef CONFIG_KPROBE_EVENTS
2378 if (flags & TRACE_EVENT_FL_KPROBE)
2379 err = bpf_get_kprobe_info(event, fd_type, buf,
2380 probe_offset, probe_addr,
2381 event->attr.type == PERF_TYPE_TRACEPOINT);
2383 #ifdef CONFIG_UPROBE_EVENTS
2384 if (flags & TRACE_EVENT_FL_UPROBE)
2385 err = bpf_get_uprobe_info(event, fd_type, buf,
2387 event->attr.type == PERF_TYPE_TRACEPOINT);
2394 static int __init send_signal_irq_work_init(void)
2397 struct send_signal_irq_work *work;
2399 for_each_possible_cpu(cpu) {
2400 work = per_cpu_ptr(&send_signal_work, cpu);
2401 init_irq_work(&work->irq_work, do_bpf_send_signal);
2406 subsys_initcall(send_signal_irq_work_init);
2408 #ifdef CONFIG_MODULES
2409 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2412 struct bpf_trace_module *btm, *tmp;
2413 struct module *mod = module;
2416 if (mod->num_bpf_raw_events == 0 ||
2417 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2420 mutex_lock(&bpf_module_mutex);
2423 case MODULE_STATE_COMING:
2424 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2426 btm->module = module;
2427 list_add(&btm->list, &bpf_trace_modules);
2432 case MODULE_STATE_GOING:
2433 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2434 if (btm->module == module) {
2435 list_del(&btm->list);
2443 mutex_unlock(&bpf_module_mutex);
2446 return notifier_from_errno(ret);
2449 static struct notifier_block bpf_module_nb = {
2450 .notifier_call = bpf_event_notify,
2453 static int __init bpf_event_init(void)
2455 register_module_notifier(&bpf_module_nb);
2459 fs_initcall(bpf_event_init);
2460 #endif /* CONFIG_MODULES */
2462 #ifdef CONFIG_FPROBE
2463 struct bpf_kprobe_multi_link {
2464 struct bpf_link link;
2466 unsigned long *addrs;
2471 struct bpf_kprobe_multi_run_ctx {
2472 struct bpf_run_ctx run_ctx;
2473 struct bpf_kprobe_multi_link *link;
2474 unsigned long entry_ip;
2482 static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
2484 unsigned long __user usymbol;
2485 const char **syms = NULL;
2486 char *buf = NULL, *p;
2490 syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
2494 buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
2498 for (p = buf, i = 0; i < cnt; i++) {
2499 if (__get_user(usymbol, usyms + i)) {
2503 err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
2504 if (err == KSYM_NAME_LEN)
2524 static void free_user_syms(struct user_syms *us)
2530 static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2532 struct bpf_kprobe_multi_link *kmulti_link;
2534 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2535 unregister_fprobe(&kmulti_link->fp);
2538 static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
2540 struct bpf_kprobe_multi_link *kmulti_link;
2542 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2543 kvfree(kmulti_link->addrs);
2544 kvfree(kmulti_link->cookies);
2548 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
2549 .release = bpf_kprobe_multi_link_release,
2550 .dealloc = bpf_kprobe_multi_link_dealloc,
2553 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2555 const struct bpf_kprobe_multi_link *link = priv;
2556 unsigned long *addr_a = a, *addr_b = b;
2557 u64 *cookie_a, *cookie_b;
2559 cookie_a = link->cookies + (addr_a - link->addrs);
2560 cookie_b = link->cookies + (addr_b - link->addrs);
2562 /* swap addr_a/addr_b and cookie_a/cookie_b values */
2563 swap(*addr_a, *addr_b);
2564 swap(*cookie_a, *cookie_b);
2567 static int __bpf_kprobe_multi_cookie_cmp(const void *a, const void *b)
2569 const unsigned long *addr_a = a, *addr_b = b;
2571 if (*addr_a == *addr_b)
2573 return *addr_a < *addr_b ? -1 : 1;
2576 static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2578 return __bpf_kprobe_multi_cookie_cmp(a, b);
2581 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2583 struct bpf_kprobe_multi_run_ctx *run_ctx;
2584 struct bpf_kprobe_multi_link *link;
2585 u64 *cookie, entry_ip;
2586 unsigned long *addr;
2588 if (WARN_ON_ONCE(!ctx))
2590 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2591 link = run_ctx->link;
2594 entry_ip = run_ctx->entry_ip;
2595 addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
2596 __bpf_kprobe_multi_cookie_cmp);
2599 cookie = link->cookies + (addr - link->addrs);
2603 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2605 struct bpf_kprobe_multi_run_ctx *run_ctx;
2607 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2608 return run_ctx->entry_ip;
2612 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2613 unsigned long entry_ip, struct pt_regs *regs)
2615 struct bpf_kprobe_multi_run_ctx run_ctx = {
2617 .entry_ip = entry_ip,
2619 struct bpf_run_ctx *old_run_ctx;
2622 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
2629 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2630 err = bpf_prog_run(link->link.prog, regs);
2631 bpf_reset_run_ctx(old_run_ctx);
2636 __this_cpu_dec(bpf_prog_active);
2641 kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
2642 struct pt_regs *regs)
2644 struct bpf_kprobe_multi_link *link;
2646 link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2647 kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
2650 static int symbols_cmp_r(const void *a, const void *b, const void *priv)
2652 const char **str_a = (const char **) a;
2653 const char **str_b = (const char **) b;
2655 return strcmp(*str_a, *str_b);
2658 struct multi_symbols_sort {
2663 static void symbols_swap_r(void *a, void *b, int size, const void *priv)
2665 const struct multi_symbols_sort *data = priv;
2666 const char **name_a = a, **name_b = b;
2668 swap(*name_a, *name_b);
2670 /* If defined, swap also related cookies. */
2671 if (data->cookies) {
2672 u64 *cookie_a, *cookie_b;
2674 cookie_a = data->cookies + (name_a - data->funcs);
2675 cookie_b = data->cookies + (name_b - data->funcs);
2676 swap(*cookie_a, *cookie_b);
2680 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2682 struct bpf_kprobe_multi_link *link = NULL;
2683 struct bpf_link_primer link_primer;
2684 void __user *ucookies;
2685 unsigned long *addrs;
2686 u32 flags, cnt, size;
2687 void __user *uaddrs;
2688 u64 *cookies = NULL;
2692 /* no support for 32bit archs yet */
2693 if (sizeof(u64) != sizeof(void *))
2696 if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
2699 flags = attr->link_create.kprobe_multi.flags;
2700 if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
2703 uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
2704 usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
2705 if (!!uaddrs == !!usyms)
2708 cnt = attr->link_create.kprobe_multi.cnt;
2712 size = cnt * sizeof(*addrs);
2713 addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2717 ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2719 cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2724 if (copy_from_user(cookies, ucookies, size)) {
2731 if (copy_from_user(addrs, uaddrs, size)) {
2736 struct multi_symbols_sort data = {
2739 struct user_syms us;
2741 err = copy_user_syms(&us, usyms, cnt);
2746 data.funcs = us.syms;
2748 sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
2749 symbols_swap_r, &data);
2751 err = ftrace_lookup_symbols(us.syms, cnt, addrs);
2752 free_user_syms(&us);
2757 link = kzalloc(sizeof(*link), GFP_KERNEL);
2763 bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
2764 &bpf_kprobe_multi_link_lops, prog);
2766 err = bpf_link_prime(&link->link, &link_primer);
2770 if (flags & BPF_F_KPROBE_MULTI_RETURN)
2771 link->fp.exit_handler = kprobe_multi_link_handler;
2773 link->fp.entry_handler = kprobe_multi_link_handler;
2775 link->addrs = addrs;
2776 link->cookies = cookies;
2781 * Sorting addresses will trigger sorting cookies as well
2782 * (check bpf_kprobe_multi_cookie_swap). This way we can
2783 * find cookie based on the address in bpf_get_attach_cookie
2786 sort_r(addrs, cnt, sizeof(*addrs),
2787 bpf_kprobe_multi_cookie_cmp,
2788 bpf_kprobe_multi_cookie_swap,
2792 err = register_fprobe_ips(&link->fp, addrs, cnt);
2794 bpf_link_cleanup(&link_primer);
2798 return bpf_link_settle(&link_primer);
2806 #else /* !CONFIG_FPROBE */
2807 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2811 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2815 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)