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_verifier.h>
10 #include <linux/bpf_perf_event.h>
11 #include <linux/btf.h>
12 #include <linux/filter.h>
13 #include <linux/uaccess.h>
14 #include <linux/ctype.h>
15 #include <linux/kprobes.h>
16 #include <linux/spinlock.h>
17 #include <linux/syscalls.h>
18 #include <linux/error-injection.h>
19 #include <linux/btf_ids.h>
20 #include <linux/bpf_lsm.h>
21 #include <linux/fprobe.h>
22 #include <linux/bsearch.h>
23 #include <linux/sort.h>
24 #include <linux/key.h>
25 #include <linux/verification.h>
26 #include <linux/namei.h>
28 #include <net/bpf_sk_storage.h>
30 #include <uapi/linux/bpf.h>
31 #include <uapi/linux/btf.h>
35 #include "trace_probe.h"
38 #define CREATE_TRACE_POINTS
39 #include "bpf_trace.h"
41 #define bpf_event_rcu_dereference(p) \
42 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
45 struct bpf_trace_module {
46 struct module *module;
47 struct list_head list;
50 static LIST_HEAD(bpf_trace_modules);
51 static DEFINE_MUTEX(bpf_module_mutex);
53 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
55 struct bpf_raw_event_map *btp, *ret = NULL;
56 struct bpf_trace_module *btm;
59 mutex_lock(&bpf_module_mutex);
60 list_for_each_entry(btm, &bpf_trace_modules, list) {
61 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
62 btp = &btm->module->bpf_raw_events[i];
63 if (!strcmp(btp->tp->name, name)) {
64 if (try_module_get(btm->module))
71 mutex_unlock(&bpf_module_mutex);
75 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
79 #endif /* CONFIG_MODULES */
81 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
82 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
84 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
85 u64 flags, const struct btf **btf,
87 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
88 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
90 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx);
91 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
94 * trace_call_bpf - invoke BPF program
95 * @call: tracepoint event
96 * @ctx: opaque context pointer
98 * kprobe handlers execute BPF programs via this helper.
99 * Can be used from static tracepoints in the future.
101 * Return: BPF programs always return an integer which is interpreted by
103 * 0 - return from kprobe (event is filtered out)
104 * 1 - store kprobe event into ring buffer
105 * Other values are reserved and currently alias to 1
107 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
113 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
115 * since some bpf program is already running on this cpu,
116 * don't call into another bpf program (same or different)
117 * and don't send kprobe event into ring-buffer,
118 * so return zero here
125 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
126 * to all call sites, we did a bpf_prog_array_valid() there to check
127 * whether call->prog_array is empty or not, which is
128 * a heuristic to speed up execution.
130 * If bpf_prog_array_valid() fetched prog_array was
131 * non-NULL, we go into trace_call_bpf() and do the actual
132 * proper rcu_dereference() under RCU lock.
133 * If it turns out that prog_array is NULL then, we bail out.
134 * For the opposite, if the bpf_prog_array_valid() fetched pointer
135 * was NULL, you'll skip the prog_array with the risk of missing
136 * out of events when it was updated in between this and the
137 * rcu_dereference() which is accepted risk.
140 ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
145 __this_cpu_dec(bpf_prog_active);
150 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
151 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
153 regs_set_return_value(regs, rc);
154 override_function_with_return(regs);
158 static const struct bpf_func_proto bpf_override_return_proto = {
159 .func = bpf_override_return,
161 .ret_type = RET_INTEGER,
162 .arg1_type = ARG_PTR_TO_CTX,
163 .arg2_type = ARG_ANYTHING,
167 static __always_inline int
168 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
172 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
173 if (unlikely(ret < 0))
174 memset(dst, 0, size);
178 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
179 const void __user *, unsafe_ptr)
181 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
184 const struct bpf_func_proto bpf_probe_read_user_proto = {
185 .func = bpf_probe_read_user,
187 .ret_type = RET_INTEGER,
188 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
189 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
190 .arg3_type = ARG_ANYTHING,
193 static __always_inline int
194 bpf_probe_read_user_str_common(void *dst, u32 size,
195 const void __user *unsafe_ptr)
200 * NB: We rely on strncpy_from_user() not copying junk past the NUL
201 * terminator into `dst`.
203 * strncpy_from_user() does long-sized strides in the fast path. If the
204 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
205 * then there could be junk after the NUL in `dst`. If user takes `dst`
206 * and keys a hash map with it, then semantically identical strings can
207 * occupy multiple entries in the map.
209 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
210 if (unlikely(ret < 0))
211 memset(dst, 0, size);
215 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
216 const void __user *, unsafe_ptr)
218 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
221 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
222 .func = bpf_probe_read_user_str,
224 .ret_type = RET_INTEGER,
225 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
226 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
227 .arg3_type = ARG_ANYTHING,
230 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
231 const void *, unsafe_ptr)
233 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
236 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
237 .func = bpf_probe_read_kernel,
239 .ret_type = RET_INTEGER,
240 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
241 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
242 .arg3_type = ARG_ANYTHING,
245 static __always_inline int
246 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
251 * The strncpy_from_kernel_nofault() call will likely not fill the
252 * entire buffer, but that's okay in this circumstance as we're probing
253 * arbitrary memory anyway similar to bpf_probe_read_*() and might
254 * as well probe the stack. Thus, memory is explicitly cleared
255 * only in error case, so that improper users ignoring return
256 * code altogether don't copy garbage; otherwise length of string
257 * is returned that can be used for bpf_perf_event_output() et al.
259 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
260 if (unlikely(ret < 0))
261 memset(dst, 0, size);
265 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
266 const void *, unsafe_ptr)
268 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
271 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
272 .func = bpf_probe_read_kernel_str,
274 .ret_type = RET_INTEGER,
275 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
276 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
277 .arg3_type = ARG_ANYTHING,
280 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
281 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
282 const void *, unsafe_ptr)
284 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
285 return bpf_probe_read_user_common(dst, size,
286 (__force void __user *)unsafe_ptr);
288 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
291 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
292 .func = bpf_probe_read_compat,
294 .ret_type = RET_INTEGER,
295 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
296 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
297 .arg3_type = ARG_ANYTHING,
300 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
301 const void *, unsafe_ptr)
303 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
304 return bpf_probe_read_user_str_common(dst, size,
305 (__force void __user *)unsafe_ptr);
307 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
310 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
311 .func = bpf_probe_read_compat_str,
313 .ret_type = RET_INTEGER,
314 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
315 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
316 .arg3_type = ARG_ANYTHING,
318 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
320 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
324 * Ensure we're in user context which is safe for the helper to
325 * run. This helper has no business in a kthread.
327 * access_ok() should prevent writing to non-user memory, but in
328 * some situations (nommu, temporary switch, etc) access_ok() does
329 * not provide enough validation, hence the check on KERNEL_DS.
331 * nmi_uaccess_okay() ensures the probe is not run in an interim
332 * state, when the task or mm are switched. This is specifically
333 * required to prevent the use of temporary mm.
336 if (unlikely(in_interrupt() ||
337 current->flags & (PF_KTHREAD | PF_EXITING)))
339 if (unlikely(!nmi_uaccess_okay()))
342 return copy_to_user_nofault(unsafe_ptr, src, size);
345 static const struct bpf_func_proto bpf_probe_write_user_proto = {
346 .func = bpf_probe_write_user,
348 .ret_type = RET_INTEGER,
349 .arg1_type = ARG_ANYTHING,
350 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
351 .arg3_type = ARG_CONST_SIZE,
354 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
356 if (!capable(CAP_SYS_ADMIN))
359 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
360 current->comm, task_pid_nr(current));
362 return &bpf_probe_write_user_proto;
365 #define MAX_TRACE_PRINTK_VARARGS 3
366 #define BPF_TRACE_PRINTK_SIZE 1024
368 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
369 u64, arg2, u64, arg3)
371 u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
372 struct bpf_bprintf_data data = {
373 .get_bin_args = true,
378 ret = bpf_bprintf_prepare(fmt, fmt_size, args,
379 MAX_TRACE_PRINTK_VARARGS, &data);
383 ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
385 trace_bpf_trace_printk(data.buf);
387 bpf_bprintf_cleanup(&data);
392 static const struct bpf_func_proto bpf_trace_printk_proto = {
393 .func = bpf_trace_printk,
395 .ret_type = RET_INTEGER,
396 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
397 .arg2_type = ARG_CONST_SIZE,
400 static void __set_printk_clr_event(void)
403 * This program might be calling bpf_trace_printk,
404 * so enable the associated bpf_trace/bpf_trace_printk event.
405 * Repeat this each time as it is possible a user has
406 * disabled bpf_trace_printk events. By loading a program
407 * calling bpf_trace_printk() however the user has expressed
408 * the intent to see such events.
410 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
411 pr_warn_ratelimited("could not enable bpf_trace_printk events");
414 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
416 __set_printk_clr_event();
417 return &bpf_trace_printk_proto;
420 BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args,
423 struct bpf_bprintf_data data = {
424 .get_bin_args = true,
429 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
432 num_args = data_len / 8;
434 ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
438 ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
440 trace_bpf_trace_printk(data.buf);
442 bpf_bprintf_cleanup(&data);
447 static const struct bpf_func_proto bpf_trace_vprintk_proto = {
448 .func = bpf_trace_vprintk,
450 .ret_type = RET_INTEGER,
451 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
452 .arg2_type = ARG_CONST_SIZE,
453 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
454 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
457 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
459 __set_printk_clr_event();
460 return &bpf_trace_vprintk_proto;
463 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
464 const void *, args, u32, data_len)
466 struct bpf_bprintf_data data = {
467 .get_bin_args = true,
471 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
474 num_args = data_len / 8;
476 err = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
480 seq_bprintf(m, fmt, data.bin_args);
482 bpf_bprintf_cleanup(&data);
484 return seq_has_overflowed(m) ? -EOVERFLOW : 0;
487 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
489 static const struct bpf_func_proto bpf_seq_printf_proto = {
490 .func = bpf_seq_printf,
492 .ret_type = RET_INTEGER,
493 .arg1_type = ARG_PTR_TO_BTF_ID,
494 .arg1_btf_id = &btf_seq_file_ids[0],
495 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
496 .arg3_type = ARG_CONST_SIZE,
497 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
498 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
501 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
503 return seq_write(m, data, len) ? -EOVERFLOW : 0;
506 static const struct bpf_func_proto bpf_seq_write_proto = {
507 .func = bpf_seq_write,
509 .ret_type = RET_INTEGER,
510 .arg1_type = ARG_PTR_TO_BTF_ID,
511 .arg1_btf_id = &btf_seq_file_ids[0],
512 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
513 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
516 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
517 u32, btf_ptr_size, u64, flags)
519 const struct btf *btf;
523 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
527 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
530 static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
531 .func = bpf_seq_printf_btf,
533 .ret_type = RET_INTEGER,
534 .arg1_type = ARG_PTR_TO_BTF_ID,
535 .arg1_btf_id = &btf_seq_file_ids[0],
536 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
537 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
538 .arg4_type = ARG_ANYTHING,
541 static __always_inline int
542 get_map_perf_counter(struct bpf_map *map, u64 flags,
543 u64 *value, u64 *enabled, u64 *running)
545 struct bpf_array *array = container_of(map, struct bpf_array, map);
546 unsigned int cpu = smp_processor_id();
547 u64 index = flags & BPF_F_INDEX_MASK;
548 struct bpf_event_entry *ee;
550 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
552 if (index == BPF_F_CURRENT_CPU)
554 if (unlikely(index >= array->map.max_entries))
557 ee = READ_ONCE(array->ptrs[index]);
561 return perf_event_read_local(ee->event, value, enabled, running);
564 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
569 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
571 * this api is ugly since we miss [-22..-2] range of valid
572 * counter values, but that's uapi
579 static const struct bpf_func_proto bpf_perf_event_read_proto = {
580 .func = bpf_perf_event_read,
582 .ret_type = RET_INTEGER,
583 .arg1_type = ARG_CONST_MAP_PTR,
584 .arg2_type = ARG_ANYTHING,
587 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
588 struct bpf_perf_event_value *, buf, u32, size)
592 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
594 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
600 memset(buf, 0, size);
604 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
605 .func = bpf_perf_event_read_value,
607 .ret_type = RET_INTEGER,
608 .arg1_type = ARG_CONST_MAP_PTR,
609 .arg2_type = ARG_ANYTHING,
610 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
611 .arg4_type = ARG_CONST_SIZE,
614 static __always_inline u64
615 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
616 u64 flags, struct perf_sample_data *sd)
618 struct bpf_array *array = container_of(map, struct bpf_array, map);
619 unsigned int cpu = smp_processor_id();
620 u64 index = flags & BPF_F_INDEX_MASK;
621 struct bpf_event_entry *ee;
622 struct perf_event *event;
624 if (index == BPF_F_CURRENT_CPU)
626 if (unlikely(index >= array->map.max_entries))
629 ee = READ_ONCE(array->ptrs[index]);
634 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
635 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
638 if (unlikely(event->oncpu != cpu))
641 return perf_event_output(event, sd, regs);
645 * Support executing tracepoints in normal, irq, and nmi context that each call
646 * bpf_perf_event_output
648 struct bpf_trace_sample_data {
649 struct perf_sample_data sds[3];
652 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
653 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
654 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
655 u64, flags, void *, data, u64, size)
657 struct bpf_trace_sample_data *sds;
658 struct perf_raw_record raw = {
664 struct perf_sample_data *sd;
668 sds = this_cpu_ptr(&bpf_trace_sds);
669 nest_level = this_cpu_inc_return(bpf_trace_nest_level);
671 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
676 sd = &sds->sds[nest_level - 1];
678 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
683 perf_sample_data_init(sd, 0, 0);
684 perf_sample_save_raw_data(sd, &raw);
686 err = __bpf_perf_event_output(regs, map, flags, sd);
688 this_cpu_dec(bpf_trace_nest_level);
693 static const struct bpf_func_proto bpf_perf_event_output_proto = {
694 .func = bpf_perf_event_output,
696 .ret_type = RET_INTEGER,
697 .arg1_type = ARG_PTR_TO_CTX,
698 .arg2_type = ARG_CONST_MAP_PTR,
699 .arg3_type = ARG_ANYTHING,
700 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
701 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
704 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
705 struct bpf_nested_pt_regs {
706 struct pt_regs regs[3];
708 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
709 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
711 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
712 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
714 struct perf_raw_frag frag = {
719 struct perf_raw_record raw = {
722 .next = ctx_size ? &frag : NULL,
728 struct perf_sample_data *sd;
729 struct pt_regs *regs;
734 nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
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);
745 perf_sample_save_raw_data(sd, &raw);
747 ret = __bpf_perf_event_output(regs, map, flags, sd);
749 this_cpu_dec(bpf_event_output_nest_level);
754 BPF_CALL_0(bpf_get_current_task)
756 return (long) current;
759 const struct bpf_func_proto bpf_get_current_task_proto = {
760 .func = bpf_get_current_task,
762 .ret_type = RET_INTEGER,
765 BPF_CALL_0(bpf_get_current_task_btf)
767 return (unsigned long) current;
770 const struct bpf_func_proto bpf_get_current_task_btf_proto = {
771 .func = bpf_get_current_task_btf,
773 .ret_type = RET_PTR_TO_BTF_ID_TRUSTED,
774 .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
777 BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
779 return (unsigned long) task_pt_regs(task);
782 BTF_ID_LIST(bpf_task_pt_regs_ids)
783 BTF_ID(struct, pt_regs)
785 const struct bpf_func_proto bpf_task_pt_regs_proto = {
786 .func = bpf_task_pt_regs,
788 .arg1_type = ARG_PTR_TO_BTF_ID,
789 .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
790 .ret_type = RET_PTR_TO_BTF_ID,
791 .ret_btf_id = &bpf_task_pt_regs_ids[0],
794 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
796 struct bpf_array *array = container_of(map, struct bpf_array, map);
799 if (unlikely(idx >= array->map.max_entries))
802 cgrp = READ_ONCE(array->ptrs[idx]);
806 return task_under_cgroup_hierarchy(current, cgrp);
809 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
810 .func = bpf_current_task_under_cgroup,
812 .ret_type = RET_INTEGER,
813 .arg1_type = ARG_CONST_MAP_PTR,
814 .arg2_type = ARG_ANYTHING,
817 struct send_signal_irq_work {
818 struct irq_work irq_work;
819 struct task_struct *task;
824 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
826 static void do_bpf_send_signal(struct irq_work *entry)
828 struct send_signal_irq_work *work;
830 work = container_of(entry, struct send_signal_irq_work, irq_work);
831 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
832 put_task_struct(work->task);
835 static int bpf_send_signal_common(u32 sig, enum pid_type type)
837 struct send_signal_irq_work *work = NULL;
839 /* Similar to bpf_probe_write_user, task needs to be
840 * in a sound condition and kernel memory access be
841 * permitted in order to send signal to the current
844 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
846 if (unlikely(!nmi_uaccess_okay()))
848 /* Task should not be pid=1 to avoid kernel panic. */
849 if (unlikely(is_global_init(current)))
852 if (irqs_disabled()) {
853 /* Do an early check on signal validity. Otherwise,
854 * the error is lost in deferred irq_work.
856 if (unlikely(!valid_signal(sig)))
859 work = this_cpu_ptr(&send_signal_work);
860 if (irq_work_is_busy(&work->irq_work))
863 /* Add the current task, which is the target of sending signal,
864 * to the irq_work. The current task may change when queued
865 * irq works get executed.
867 work->task = get_task_struct(current);
870 irq_work_queue(&work->irq_work);
874 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
877 BPF_CALL_1(bpf_send_signal, u32, sig)
879 return bpf_send_signal_common(sig, PIDTYPE_TGID);
882 static const struct bpf_func_proto bpf_send_signal_proto = {
883 .func = bpf_send_signal,
885 .ret_type = RET_INTEGER,
886 .arg1_type = ARG_ANYTHING,
889 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
891 return bpf_send_signal_common(sig, PIDTYPE_PID);
894 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
895 .func = bpf_send_signal_thread,
897 .ret_type = RET_INTEGER,
898 .arg1_type = ARG_ANYTHING,
901 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
911 * The path pointer is verified as trusted and safe to use,
912 * but let's double check it's valid anyway to workaround
913 * potentially broken verifier.
915 len = copy_from_kernel_nofault(©, path, sizeof(*path));
919 p = d_path(©, buf, sz);
924 memmove(buf, p, len);
930 BTF_SET_START(btf_allowlist_d_path)
931 #ifdef CONFIG_SECURITY
932 BTF_ID(func, security_file_permission)
933 BTF_ID(func, security_inode_getattr)
934 BTF_ID(func, security_file_open)
936 #ifdef CONFIG_SECURITY_PATH
937 BTF_ID(func, security_path_truncate)
939 BTF_ID(func, vfs_truncate)
940 BTF_ID(func, vfs_fallocate)
941 BTF_ID(func, dentry_open)
942 BTF_ID(func, vfs_getattr)
943 BTF_ID(func, filp_close)
944 BTF_SET_END(btf_allowlist_d_path)
946 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
948 if (prog->type == BPF_PROG_TYPE_TRACING &&
949 prog->expected_attach_type == BPF_TRACE_ITER)
952 if (prog->type == BPF_PROG_TYPE_LSM)
953 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
955 return btf_id_set_contains(&btf_allowlist_d_path,
956 prog->aux->attach_btf_id);
959 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
961 static const struct bpf_func_proto bpf_d_path_proto = {
964 .ret_type = RET_INTEGER,
965 .arg1_type = ARG_PTR_TO_BTF_ID,
966 .arg1_btf_id = &bpf_d_path_btf_ids[0],
967 .arg2_type = ARG_PTR_TO_MEM,
968 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
969 .allowed = bpf_d_path_allowed,
972 #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
973 BTF_F_PTR_RAW | BTF_F_ZERO)
975 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
976 u64 flags, const struct btf **btf,
979 const struct btf_type *t;
981 if (unlikely(flags & ~(BTF_F_ALL)))
984 if (btf_ptr_size != sizeof(struct btf_ptr))
987 *btf = bpf_get_btf_vmlinux();
989 if (IS_ERR_OR_NULL(*btf))
990 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
992 if (ptr->type_id > 0)
993 *btf_id = ptr->type_id;
998 t = btf_type_by_id(*btf, *btf_id);
999 if (*btf_id <= 0 || !t)
1005 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1006 u32, btf_ptr_size, u64, flags)
1008 const struct btf *btf;
1012 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1016 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1020 const struct bpf_func_proto bpf_snprintf_btf_proto = {
1021 .func = bpf_snprintf_btf,
1023 .ret_type = RET_INTEGER,
1024 .arg1_type = ARG_PTR_TO_MEM,
1025 .arg2_type = ARG_CONST_SIZE,
1026 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1027 .arg4_type = ARG_CONST_SIZE,
1028 .arg5_type = ARG_ANYTHING,
1031 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
1033 /* This helper call is inlined by verifier. */
1034 return ((u64 *)ctx)[-2];
1037 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
1038 .func = bpf_get_func_ip_tracing,
1040 .ret_type = RET_INTEGER,
1041 .arg1_type = ARG_PTR_TO_CTX,
1044 #ifdef CONFIG_X86_KERNEL_IBT
1045 static unsigned long get_entry_ip(unsigned long fentry_ip)
1049 /* Being extra safe in here in case entry ip is on the page-edge. */
1050 if (get_kernel_nofault(instr, (u32 *) fentry_ip - 1))
1052 if (is_endbr(instr))
1053 fentry_ip -= ENDBR_INSN_SIZE;
1057 #define get_entry_ip(fentry_ip) fentry_ip
1060 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1062 struct bpf_trace_run_ctx *run_ctx __maybe_unused;
1065 #ifdef CONFIG_UPROBES
1066 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1067 if (run_ctx->is_uprobe)
1068 return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr;
1071 kp = kprobe_running();
1073 if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
1076 return get_entry_ip((uintptr_t)kp->addr);
1079 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1080 .func = bpf_get_func_ip_kprobe,
1082 .ret_type = RET_INTEGER,
1083 .arg1_type = ARG_PTR_TO_CTX,
1086 BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
1088 return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
1091 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
1092 .func = bpf_get_func_ip_kprobe_multi,
1094 .ret_type = RET_INTEGER,
1095 .arg1_type = ARG_PTR_TO_CTX,
1098 BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1100 return bpf_kprobe_multi_cookie(current->bpf_ctx);
1103 static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1104 .func = bpf_get_attach_cookie_kprobe_multi,
1106 .ret_type = RET_INTEGER,
1107 .arg1_type = ARG_PTR_TO_CTX,
1110 BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs)
1112 return bpf_uprobe_multi_entry_ip(current->bpf_ctx);
1115 static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = {
1116 .func = bpf_get_func_ip_uprobe_multi,
1118 .ret_type = RET_INTEGER,
1119 .arg1_type = ARG_PTR_TO_CTX,
1122 BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs)
1124 return bpf_uprobe_multi_cookie(current->bpf_ctx);
1127 static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = {
1128 .func = bpf_get_attach_cookie_uprobe_multi,
1130 .ret_type = RET_INTEGER,
1131 .arg1_type = ARG_PTR_TO_CTX,
1134 BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
1136 struct bpf_trace_run_ctx *run_ctx;
1138 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1139 return run_ctx->bpf_cookie;
1142 static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
1143 .func = bpf_get_attach_cookie_trace,
1145 .ret_type = RET_INTEGER,
1146 .arg1_type = ARG_PTR_TO_CTX,
1149 BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
1151 return ctx->event->bpf_cookie;
1154 static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
1155 .func = bpf_get_attach_cookie_pe,
1157 .ret_type = RET_INTEGER,
1158 .arg1_type = ARG_PTR_TO_CTX,
1161 BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
1163 struct bpf_trace_run_ctx *run_ctx;
1165 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1166 return run_ctx->bpf_cookie;
1169 static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
1170 .func = bpf_get_attach_cookie_tracing,
1172 .ret_type = RET_INTEGER,
1173 .arg1_type = ARG_PTR_TO_CTX,
1176 BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1181 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1182 u32 entry_cnt = size / br_entry_size;
1184 entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1186 if (unlikely(flags))
1192 return entry_cnt * br_entry_size;
1196 static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1197 .func = bpf_get_branch_snapshot,
1199 .ret_type = RET_INTEGER,
1200 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1201 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1204 BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1206 /* This helper call is inlined by verifier. */
1207 u64 nr_args = ((u64 *)ctx)[-1];
1209 if ((u64) n >= nr_args)
1211 *value = ((u64 *)ctx)[n];
1215 static const struct bpf_func_proto bpf_get_func_arg_proto = {
1216 .func = get_func_arg,
1217 .ret_type = RET_INTEGER,
1218 .arg1_type = ARG_PTR_TO_CTX,
1219 .arg2_type = ARG_ANYTHING,
1220 .arg3_type = ARG_PTR_TO_LONG,
1223 BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1225 /* This helper call is inlined by verifier. */
1226 u64 nr_args = ((u64 *)ctx)[-1];
1228 *value = ((u64 *)ctx)[nr_args];
1232 static const struct bpf_func_proto bpf_get_func_ret_proto = {
1233 .func = get_func_ret,
1234 .ret_type = RET_INTEGER,
1235 .arg1_type = ARG_PTR_TO_CTX,
1236 .arg2_type = ARG_PTR_TO_LONG,
1239 BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1241 /* This helper call is inlined by verifier. */
1242 return ((u64 *)ctx)[-1];
1245 static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1246 .func = get_func_arg_cnt,
1247 .ret_type = RET_INTEGER,
1248 .arg1_type = ARG_PTR_TO_CTX,
1253 __diag_ignore_all("-Wmissing-prototypes",
1254 "kfuncs which will be used in BPF programs");
1257 * bpf_lookup_user_key - lookup a key by its serial
1258 * @serial: key handle serial number
1259 * @flags: lookup-specific flags
1261 * Search a key with a given *serial* and the provided *flags*.
1262 * If found, increment the reference count of the key by one, and
1263 * return it in the bpf_key structure.
1265 * The bpf_key structure must be passed to bpf_key_put() when done
1266 * with it, so that the key reference count is decremented and the
1267 * bpf_key structure is freed.
1269 * Permission checks are deferred to the time the key is used by
1270 * one of the available key-specific kfuncs.
1272 * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
1273 * special keyring (e.g. session keyring), if it doesn't yet exist.
1274 * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
1275 * for the key construction, and to retrieve uninstantiated keys (keys
1276 * without data attached to them).
1278 * Return: a bpf_key pointer with a valid key pointer if the key is found, a
1279 * NULL pointer otherwise.
1281 __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
1284 struct bpf_key *bkey;
1286 if (flags & ~KEY_LOOKUP_ALL)
1290 * Permission check is deferred until the key is used, as the
1291 * intent of the caller is unknown here.
1293 key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
1294 if (IS_ERR(key_ref))
1297 bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
1299 key_put(key_ref_to_ptr(key_ref));
1303 bkey->key = key_ref_to_ptr(key_ref);
1304 bkey->has_ref = true;
1310 * bpf_lookup_system_key - lookup a key by a system-defined ID
1313 * Obtain a bpf_key structure with a key pointer set to the passed key ID.
1314 * The key pointer is marked as invalid, to prevent bpf_key_put() from
1315 * attempting to decrement the key reference count on that pointer. The key
1316 * pointer set in such way is currently understood only by
1317 * verify_pkcs7_signature().
1319 * Set *id* to one of the values defined in include/linux/verification.h:
1320 * 0 for the primary keyring (immutable keyring of system keys);
1321 * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
1322 * (where keys can be added only if they are vouched for by existing keys
1323 * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
1324 * keyring (primarily used by the integrity subsystem to verify a kexec'ed
1325 * kerned image and, possibly, the initramfs signature).
1327 * Return: a bpf_key pointer with an invalid key pointer set from the
1328 * pre-determined ID on success, a NULL pointer otherwise
1330 __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id)
1332 struct bpf_key *bkey;
1334 if (system_keyring_id_check(id) < 0)
1337 bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
1341 bkey->key = (struct key *)(unsigned long)id;
1342 bkey->has_ref = false;
1348 * bpf_key_put - decrement key reference count if key is valid and free bpf_key
1349 * @bkey: bpf_key structure
1351 * Decrement the reference count of the key inside *bkey*, if the pointer
1352 * is valid, and free *bkey*.
1354 __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
1362 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1364 * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
1365 * @data_ptr: data to verify
1366 * @sig_ptr: signature of the data
1367 * @trusted_keyring: keyring with keys trusted for signature verification
1369 * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
1370 * with keys in a keyring referenced by *trusted_keyring*.
1372 * Return: 0 on success, a negative value on error.
1374 __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
1375 struct bpf_dynptr_kern *sig_ptr,
1376 struct bpf_key *trusted_keyring)
1380 if (trusted_keyring->has_ref) {
1382 * Do the permission check deferred in bpf_lookup_user_key().
1383 * See bpf_lookup_user_key() for more details.
1385 * A call to key_task_permission() here would be redundant, as
1386 * it is already done by keyring_search() called by
1387 * find_asymmetric_key().
1389 ret = key_validate(trusted_keyring->key);
1394 return verify_pkcs7_signature(data_ptr->data,
1395 __bpf_dynptr_size(data_ptr),
1397 __bpf_dynptr_size(sig_ptr),
1398 trusted_keyring->key,
1399 VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
1402 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
1406 BTF_SET8_START(key_sig_kfunc_set)
1407 BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
1408 BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
1409 BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
1410 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1411 BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
1413 BTF_SET8_END(key_sig_kfunc_set)
1415 static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
1416 .owner = THIS_MODULE,
1417 .set = &key_sig_kfunc_set,
1420 static int __init bpf_key_sig_kfuncs_init(void)
1422 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
1423 &bpf_key_sig_kfunc_set);
1426 late_initcall(bpf_key_sig_kfuncs_init);
1427 #endif /* CONFIG_KEYS */
1429 static const struct bpf_func_proto *
1430 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1433 case BPF_FUNC_map_lookup_elem:
1434 return &bpf_map_lookup_elem_proto;
1435 case BPF_FUNC_map_update_elem:
1436 return &bpf_map_update_elem_proto;
1437 case BPF_FUNC_map_delete_elem:
1438 return &bpf_map_delete_elem_proto;
1439 case BPF_FUNC_map_push_elem:
1440 return &bpf_map_push_elem_proto;
1441 case BPF_FUNC_map_pop_elem:
1442 return &bpf_map_pop_elem_proto;
1443 case BPF_FUNC_map_peek_elem:
1444 return &bpf_map_peek_elem_proto;
1445 case BPF_FUNC_map_lookup_percpu_elem:
1446 return &bpf_map_lookup_percpu_elem_proto;
1447 case BPF_FUNC_ktime_get_ns:
1448 return &bpf_ktime_get_ns_proto;
1449 case BPF_FUNC_ktime_get_boot_ns:
1450 return &bpf_ktime_get_boot_ns_proto;
1451 case BPF_FUNC_tail_call:
1452 return &bpf_tail_call_proto;
1453 case BPF_FUNC_get_current_pid_tgid:
1454 return &bpf_get_current_pid_tgid_proto;
1455 case BPF_FUNC_get_current_task:
1456 return &bpf_get_current_task_proto;
1457 case BPF_FUNC_get_current_task_btf:
1458 return &bpf_get_current_task_btf_proto;
1459 case BPF_FUNC_task_pt_regs:
1460 return &bpf_task_pt_regs_proto;
1461 case BPF_FUNC_get_current_uid_gid:
1462 return &bpf_get_current_uid_gid_proto;
1463 case BPF_FUNC_get_current_comm:
1464 return &bpf_get_current_comm_proto;
1465 case BPF_FUNC_trace_printk:
1466 return bpf_get_trace_printk_proto();
1467 case BPF_FUNC_get_smp_processor_id:
1468 return &bpf_get_smp_processor_id_proto;
1469 case BPF_FUNC_get_numa_node_id:
1470 return &bpf_get_numa_node_id_proto;
1471 case BPF_FUNC_perf_event_read:
1472 return &bpf_perf_event_read_proto;
1473 case BPF_FUNC_current_task_under_cgroup:
1474 return &bpf_current_task_under_cgroup_proto;
1475 case BPF_FUNC_get_prandom_u32:
1476 return &bpf_get_prandom_u32_proto;
1477 case BPF_FUNC_probe_write_user:
1478 return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1479 NULL : bpf_get_probe_write_proto();
1480 case BPF_FUNC_probe_read_user:
1481 return &bpf_probe_read_user_proto;
1482 case BPF_FUNC_probe_read_kernel:
1483 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1484 NULL : &bpf_probe_read_kernel_proto;
1485 case BPF_FUNC_probe_read_user_str:
1486 return &bpf_probe_read_user_str_proto;
1487 case BPF_FUNC_probe_read_kernel_str:
1488 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1489 NULL : &bpf_probe_read_kernel_str_proto;
1490 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1491 case BPF_FUNC_probe_read:
1492 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1493 NULL : &bpf_probe_read_compat_proto;
1494 case BPF_FUNC_probe_read_str:
1495 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1496 NULL : &bpf_probe_read_compat_str_proto;
1498 #ifdef CONFIG_CGROUPS
1499 case BPF_FUNC_cgrp_storage_get:
1500 return &bpf_cgrp_storage_get_proto;
1501 case BPF_FUNC_cgrp_storage_delete:
1502 return &bpf_cgrp_storage_delete_proto;
1504 case BPF_FUNC_send_signal:
1505 return &bpf_send_signal_proto;
1506 case BPF_FUNC_send_signal_thread:
1507 return &bpf_send_signal_thread_proto;
1508 case BPF_FUNC_perf_event_read_value:
1509 return &bpf_perf_event_read_value_proto;
1510 case BPF_FUNC_get_ns_current_pid_tgid:
1511 return &bpf_get_ns_current_pid_tgid_proto;
1512 case BPF_FUNC_ringbuf_output:
1513 return &bpf_ringbuf_output_proto;
1514 case BPF_FUNC_ringbuf_reserve:
1515 return &bpf_ringbuf_reserve_proto;
1516 case BPF_FUNC_ringbuf_submit:
1517 return &bpf_ringbuf_submit_proto;
1518 case BPF_FUNC_ringbuf_discard:
1519 return &bpf_ringbuf_discard_proto;
1520 case BPF_FUNC_ringbuf_query:
1521 return &bpf_ringbuf_query_proto;
1522 case BPF_FUNC_jiffies64:
1523 return &bpf_jiffies64_proto;
1524 case BPF_FUNC_get_task_stack:
1525 return &bpf_get_task_stack_proto;
1526 case BPF_FUNC_copy_from_user:
1527 return &bpf_copy_from_user_proto;
1528 case BPF_FUNC_copy_from_user_task:
1529 return &bpf_copy_from_user_task_proto;
1530 case BPF_FUNC_snprintf_btf:
1531 return &bpf_snprintf_btf_proto;
1532 case BPF_FUNC_per_cpu_ptr:
1533 return &bpf_per_cpu_ptr_proto;
1534 case BPF_FUNC_this_cpu_ptr:
1535 return &bpf_this_cpu_ptr_proto;
1536 case BPF_FUNC_task_storage_get:
1537 if (bpf_prog_check_recur(prog))
1538 return &bpf_task_storage_get_recur_proto;
1539 return &bpf_task_storage_get_proto;
1540 case BPF_FUNC_task_storage_delete:
1541 if (bpf_prog_check_recur(prog))
1542 return &bpf_task_storage_delete_recur_proto;
1543 return &bpf_task_storage_delete_proto;
1544 case BPF_FUNC_for_each_map_elem:
1545 return &bpf_for_each_map_elem_proto;
1546 case BPF_FUNC_snprintf:
1547 return &bpf_snprintf_proto;
1548 case BPF_FUNC_get_func_ip:
1549 return &bpf_get_func_ip_proto_tracing;
1550 case BPF_FUNC_get_branch_snapshot:
1551 return &bpf_get_branch_snapshot_proto;
1552 case BPF_FUNC_find_vma:
1553 return &bpf_find_vma_proto;
1554 case BPF_FUNC_trace_vprintk:
1555 return bpf_get_trace_vprintk_proto();
1557 return bpf_base_func_proto(func_id);
1561 static const struct bpf_func_proto *
1562 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1565 case BPF_FUNC_perf_event_output:
1566 return &bpf_perf_event_output_proto;
1567 case BPF_FUNC_get_stackid:
1568 return &bpf_get_stackid_proto;
1569 case BPF_FUNC_get_stack:
1570 return &bpf_get_stack_proto;
1571 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1572 case BPF_FUNC_override_return:
1573 return &bpf_override_return_proto;
1575 case BPF_FUNC_get_func_ip:
1576 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI)
1577 return &bpf_get_func_ip_proto_kprobe_multi;
1578 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
1579 return &bpf_get_func_ip_proto_uprobe_multi;
1580 return &bpf_get_func_ip_proto_kprobe;
1581 case BPF_FUNC_get_attach_cookie:
1582 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI)
1583 return &bpf_get_attach_cookie_proto_kmulti;
1584 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
1585 return &bpf_get_attach_cookie_proto_umulti;
1586 return &bpf_get_attach_cookie_proto_trace;
1588 return bpf_tracing_func_proto(func_id, prog);
1592 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1593 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1594 const struct bpf_prog *prog,
1595 struct bpf_insn_access_aux *info)
1597 if (off < 0 || off >= sizeof(struct pt_regs))
1599 if (type != BPF_READ)
1601 if (off % size != 0)
1604 * Assertion for 32 bit to make sure last 8 byte access
1605 * (BPF_DW) to the last 4 byte member is disallowed.
1607 if (off + size > sizeof(struct pt_regs))
1613 const struct bpf_verifier_ops kprobe_verifier_ops = {
1614 .get_func_proto = kprobe_prog_func_proto,
1615 .is_valid_access = kprobe_prog_is_valid_access,
1618 const struct bpf_prog_ops kprobe_prog_ops = {
1621 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1622 u64, flags, void *, data, u64, size)
1624 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1627 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1628 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1629 * from there and call the same bpf_perf_event_output() helper inline.
1631 return ____bpf_perf_event_output(regs, map, flags, data, size);
1634 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1635 .func = bpf_perf_event_output_tp,
1637 .ret_type = RET_INTEGER,
1638 .arg1_type = ARG_PTR_TO_CTX,
1639 .arg2_type = ARG_CONST_MAP_PTR,
1640 .arg3_type = ARG_ANYTHING,
1641 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1642 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1645 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1648 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1651 * Same comment as in bpf_perf_event_output_tp(), only that this time
1652 * the other helper's function body cannot be inlined due to being
1653 * external, thus we need to call raw helper function.
1655 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1659 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1660 .func = bpf_get_stackid_tp,
1662 .ret_type = RET_INTEGER,
1663 .arg1_type = ARG_PTR_TO_CTX,
1664 .arg2_type = ARG_CONST_MAP_PTR,
1665 .arg3_type = ARG_ANYTHING,
1668 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1671 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1673 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1674 (unsigned long) size, flags, 0);
1677 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1678 .func = bpf_get_stack_tp,
1680 .ret_type = RET_INTEGER,
1681 .arg1_type = ARG_PTR_TO_CTX,
1682 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1683 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1684 .arg4_type = ARG_ANYTHING,
1687 static const struct bpf_func_proto *
1688 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1691 case BPF_FUNC_perf_event_output:
1692 return &bpf_perf_event_output_proto_tp;
1693 case BPF_FUNC_get_stackid:
1694 return &bpf_get_stackid_proto_tp;
1695 case BPF_FUNC_get_stack:
1696 return &bpf_get_stack_proto_tp;
1697 case BPF_FUNC_get_attach_cookie:
1698 return &bpf_get_attach_cookie_proto_trace;
1700 return bpf_tracing_func_proto(func_id, prog);
1704 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1705 const struct bpf_prog *prog,
1706 struct bpf_insn_access_aux *info)
1708 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1710 if (type != BPF_READ)
1712 if (off % size != 0)
1715 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1719 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1720 .get_func_proto = tp_prog_func_proto,
1721 .is_valid_access = tp_prog_is_valid_access,
1724 const struct bpf_prog_ops tracepoint_prog_ops = {
1727 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1728 struct bpf_perf_event_value *, buf, u32, size)
1732 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1734 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1740 memset(buf, 0, size);
1744 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1745 .func = bpf_perf_prog_read_value,
1747 .ret_type = RET_INTEGER,
1748 .arg1_type = ARG_PTR_TO_CTX,
1749 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1750 .arg3_type = ARG_CONST_SIZE,
1753 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1754 void *, buf, u32, size, u64, flags)
1756 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1757 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1760 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1763 if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
1766 if (unlikely(!br_stack))
1769 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1770 return br_stack->nr * br_entry_size;
1772 if (!buf || (size % br_entry_size != 0))
1775 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1776 memcpy(buf, br_stack->entries, to_copy);
1781 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1782 .func = bpf_read_branch_records,
1784 .ret_type = RET_INTEGER,
1785 .arg1_type = ARG_PTR_TO_CTX,
1786 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1787 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1788 .arg4_type = ARG_ANYTHING,
1791 static const struct bpf_func_proto *
1792 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1795 case BPF_FUNC_perf_event_output:
1796 return &bpf_perf_event_output_proto_tp;
1797 case BPF_FUNC_get_stackid:
1798 return &bpf_get_stackid_proto_pe;
1799 case BPF_FUNC_get_stack:
1800 return &bpf_get_stack_proto_pe;
1801 case BPF_FUNC_perf_prog_read_value:
1802 return &bpf_perf_prog_read_value_proto;
1803 case BPF_FUNC_read_branch_records:
1804 return &bpf_read_branch_records_proto;
1805 case BPF_FUNC_get_attach_cookie:
1806 return &bpf_get_attach_cookie_proto_pe;
1808 return bpf_tracing_func_proto(func_id, prog);
1813 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1814 * to avoid potential recursive reuse issue when/if tracepoints are added
1815 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1817 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1818 * in normal, irq, and nmi context.
1820 struct bpf_raw_tp_regs {
1821 struct pt_regs regs[3];
1823 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1824 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1825 static struct pt_regs *get_bpf_raw_tp_regs(void)
1827 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1828 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1830 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1831 this_cpu_dec(bpf_raw_tp_nest_level);
1832 return ERR_PTR(-EBUSY);
1835 return &tp_regs->regs[nest_level - 1];
1838 static void put_bpf_raw_tp_regs(void)
1840 this_cpu_dec(bpf_raw_tp_nest_level);
1843 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1844 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1846 struct pt_regs *regs = get_bpf_raw_tp_regs();
1850 return PTR_ERR(regs);
1852 perf_fetch_caller_regs(regs);
1853 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1855 put_bpf_raw_tp_regs();
1859 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1860 .func = bpf_perf_event_output_raw_tp,
1862 .ret_type = RET_INTEGER,
1863 .arg1_type = ARG_PTR_TO_CTX,
1864 .arg2_type = ARG_CONST_MAP_PTR,
1865 .arg3_type = ARG_ANYTHING,
1866 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1867 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1870 extern const struct bpf_func_proto bpf_skb_output_proto;
1871 extern const struct bpf_func_proto bpf_xdp_output_proto;
1872 extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1874 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1875 struct bpf_map *, map, u64, flags)
1877 struct pt_regs *regs = get_bpf_raw_tp_regs();
1881 return PTR_ERR(regs);
1883 perf_fetch_caller_regs(regs);
1884 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1885 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1887 put_bpf_raw_tp_regs();
1891 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1892 .func = bpf_get_stackid_raw_tp,
1894 .ret_type = RET_INTEGER,
1895 .arg1_type = ARG_PTR_TO_CTX,
1896 .arg2_type = ARG_CONST_MAP_PTR,
1897 .arg3_type = ARG_ANYTHING,
1900 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1901 void *, buf, u32, size, u64, flags)
1903 struct pt_regs *regs = get_bpf_raw_tp_regs();
1907 return PTR_ERR(regs);
1909 perf_fetch_caller_regs(regs);
1910 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1911 (unsigned long) size, flags, 0);
1912 put_bpf_raw_tp_regs();
1916 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1917 .func = bpf_get_stack_raw_tp,
1919 .ret_type = RET_INTEGER,
1920 .arg1_type = ARG_PTR_TO_CTX,
1921 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1922 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1923 .arg4_type = ARG_ANYTHING,
1926 static const struct bpf_func_proto *
1927 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1930 case BPF_FUNC_perf_event_output:
1931 return &bpf_perf_event_output_proto_raw_tp;
1932 case BPF_FUNC_get_stackid:
1933 return &bpf_get_stackid_proto_raw_tp;
1934 case BPF_FUNC_get_stack:
1935 return &bpf_get_stack_proto_raw_tp;
1937 return bpf_tracing_func_proto(func_id, prog);
1941 const struct bpf_func_proto *
1942 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1944 const struct bpf_func_proto *fn;
1948 case BPF_FUNC_skb_output:
1949 return &bpf_skb_output_proto;
1950 case BPF_FUNC_xdp_output:
1951 return &bpf_xdp_output_proto;
1952 case BPF_FUNC_skc_to_tcp6_sock:
1953 return &bpf_skc_to_tcp6_sock_proto;
1954 case BPF_FUNC_skc_to_tcp_sock:
1955 return &bpf_skc_to_tcp_sock_proto;
1956 case BPF_FUNC_skc_to_tcp_timewait_sock:
1957 return &bpf_skc_to_tcp_timewait_sock_proto;
1958 case BPF_FUNC_skc_to_tcp_request_sock:
1959 return &bpf_skc_to_tcp_request_sock_proto;
1960 case BPF_FUNC_skc_to_udp6_sock:
1961 return &bpf_skc_to_udp6_sock_proto;
1962 case BPF_FUNC_skc_to_unix_sock:
1963 return &bpf_skc_to_unix_sock_proto;
1964 case BPF_FUNC_skc_to_mptcp_sock:
1965 return &bpf_skc_to_mptcp_sock_proto;
1966 case BPF_FUNC_sk_storage_get:
1967 return &bpf_sk_storage_get_tracing_proto;
1968 case BPF_FUNC_sk_storage_delete:
1969 return &bpf_sk_storage_delete_tracing_proto;
1970 case BPF_FUNC_sock_from_file:
1971 return &bpf_sock_from_file_proto;
1972 case BPF_FUNC_get_socket_cookie:
1973 return &bpf_get_socket_ptr_cookie_proto;
1974 case BPF_FUNC_xdp_get_buff_len:
1975 return &bpf_xdp_get_buff_len_trace_proto;
1977 case BPF_FUNC_seq_printf:
1978 return prog->expected_attach_type == BPF_TRACE_ITER ?
1979 &bpf_seq_printf_proto :
1981 case BPF_FUNC_seq_write:
1982 return prog->expected_attach_type == BPF_TRACE_ITER ?
1983 &bpf_seq_write_proto :
1985 case BPF_FUNC_seq_printf_btf:
1986 return prog->expected_attach_type == BPF_TRACE_ITER ?
1987 &bpf_seq_printf_btf_proto :
1989 case BPF_FUNC_d_path:
1990 return &bpf_d_path_proto;
1991 case BPF_FUNC_get_func_arg:
1992 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1993 case BPF_FUNC_get_func_ret:
1994 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
1995 case BPF_FUNC_get_func_arg_cnt:
1996 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
1997 case BPF_FUNC_get_attach_cookie:
1998 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
2000 fn = raw_tp_prog_func_proto(func_id, prog);
2001 if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
2002 fn = bpf_iter_get_func_proto(func_id, prog);
2007 static bool raw_tp_prog_is_valid_access(int off, int size,
2008 enum bpf_access_type type,
2009 const struct bpf_prog *prog,
2010 struct bpf_insn_access_aux *info)
2012 return bpf_tracing_ctx_access(off, size, type);
2015 static bool tracing_prog_is_valid_access(int off, int size,
2016 enum bpf_access_type type,
2017 const struct bpf_prog *prog,
2018 struct bpf_insn_access_aux *info)
2020 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
2023 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
2024 const union bpf_attr *kattr,
2025 union bpf_attr __user *uattr)
2030 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
2031 .get_func_proto = raw_tp_prog_func_proto,
2032 .is_valid_access = raw_tp_prog_is_valid_access,
2035 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
2037 .test_run = bpf_prog_test_run_raw_tp,
2041 const struct bpf_verifier_ops tracing_verifier_ops = {
2042 .get_func_proto = tracing_prog_func_proto,
2043 .is_valid_access = tracing_prog_is_valid_access,
2046 const struct bpf_prog_ops tracing_prog_ops = {
2047 .test_run = bpf_prog_test_run_tracing,
2050 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
2051 enum bpf_access_type type,
2052 const struct bpf_prog *prog,
2053 struct bpf_insn_access_aux *info)
2056 if (size != sizeof(u64) || type != BPF_READ)
2058 info->reg_type = PTR_TO_TP_BUFFER;
2060 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
2063 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
2064 .get_func_proto = raw_tp_prog_func_proto,
2065 .is_valid_access = raw_tp_writable_prog_is_valid_access,
2068 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
2071 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
2072 const struct bpf_prog *prog,
2073 struct bpf_insn_access_aux *info)
2075 const int size_u64 = sizeof(u64);
2077 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
2079 if (type != BPF_READ)
2081 if (off % size != 0) {
2082 if (sizeof(unsigned long) != 4)
2086 if (off % size != 4)
2091 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
2092 bpf_ctx_record_field_size(info, size_u64);
2093 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2096 case bpf_ctx_range(struct bpf_perf_event_data, addr):
2097 bpf_ctx_record_field_size(info, size_u64);
2098 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2102 if (size != sizeof(long))
2109 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
2110 const struct bpf_insn *si,
2111 struct bpf_insn *insn_buf,
2112 struct bpf_prog *prog, u32 *target_size)
2114 struct bpf_insn *insn = insn_buf;
2117 case offsetof(struct bpf_perf_event_data, sample_period):
2118 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2119 data), si->dst_reg, si->src_reg,
2120 offsetof(struct bpf_perf_event_data_kern, data));
2121 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2122 bpf_target_off(struct perf_sample_data, period, 8,
2125 case offsetof(struct bpf_perf_event_data, addr):
2126 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2127 data), si->dst_reg, si->src_reg,
2128 offsetof(struct bpf_perf_event_data_kern, data));
2129 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2130 bpf_target_off(struct perf_sample_data, addr, 8,
2134 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2135 regs), si->dst_reg, si->src_reg,
2136 offsetof(struct bpf_perf_event_data_kern, regs));
2137 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
2142 return insn - insn_buf;
2145 const struct bpf_verifier_ops perf_event_verifier_ops = {
2146 .get_func_proto = pe_prog_func_proto,
2147 .is_valid_access = pe_prog_is_valid_access,
2148 .convert_ctx_access = pe_prog_convert_ctx_access,
2151 const struct bpf_prog_ops perf_event_prog_ops = {
2154 static DEFINE_MUTEX(bpf_event_mutex);
2156 #define BPF_TRACE_MAX_PROGS 64
2158 int perf_event_attach_bpf_prog(struct perf_event *event,
2159 struct bpf_prog *prog,
2162 struct bpf_prog_array *old_array;
2163 struct bpf_prog_array *new_array;
2167 * Kprobe override only works if they are on the function entry,
2168 * and only if they are on the opt-in list.
2170 if (prog->kprobe_override &&
2171 (!trace_kprobe_on_func_entry(event->tp_event) ||
2172 !trace_kprobe_error_injectable(event->tp_event)))
2175 mutex_lock(&bpf_event_mutex);
2180 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2182 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
2187 ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
2191 /* set the new array to event->tp_event and set event->prog */
2193 event->bpf_cookie = bpf_cookie;
2194 rcu_assign_pointer(event->tp_event->prog_array, new_array);
2195 bpf_prog_array_free_sleepable(old_array);
2198 mutex_unlock(&bpf_event_mutex);
2202 void perf_event_detach_bpf_prog(struct perf_event *event)
2204 struct bpf_prog_array *old_array;
2205 struct bpf_prog_array *new_array;
2208 mutex_lock(&bpf_event_mutex);
2213 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2214 ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
2218 bpf_prog_array_delete_safe(old_array, event->prog);
2220 rcu_assign_pointer(event->tp_event->prog_array, new_array);
2221 bpf_prog_array_free_sleepable(old_array);
2224 bpf_prog_put(event->prog);
2228 mutex_unlock(&bpf_event_mutex);
2231 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
2233 struct perf_event_query_bpf __user *uquery = info;
2234 struct perf_event_query_bpf query = {};
2235 struct bpf_prog_array *progs;
2236 u32 *ids, prog_cnt, ids_len;
2239 if (!perfmon_capable())
2241 if (event->attr.type != PERF_TYPE_TRACEPOINT)
2243 if (copy_from_user(&query, uquery, sizeof(query)))
2246 ids_len = query.ids_len;
2247 if (ids_len > BPF_TRACE_MAX_PROGS)
2249 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2253 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
2254 * is required when user only wants to check for uquery->prog_cnt.
2255 * There is no need to check for it since the case is handled
2256 * gracefully in bpf_prog_array_copy_info.
2259 mutex_lock(&bpf_event_mutex);
2260 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2261 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2262 mutex_unlock(&bpf_event_mutex);
2264 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2265 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2272 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2273 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2275 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2277 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2279 for (; btp < __stop__bpf_raw_tp; btp++) {
2280 if (!strcmp(btp->tp->name, name))
2284 return bpf_get_raw_tracepoint_module(name);
2287 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2292 mod = __module_address((unsigned long)btp);
2297 static __always_inline
2298 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2301 if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
2302 bpf_prog_inc_misses_counter(prog);
2306 (void) bpf_prog_run(prog, args);
2309 this_cpu_dec(*(prog->active));
2312 #define UNPACK(...) __VA_ARGS__
2313 #define REPEAT_1(FN, DL, X, ...) FN(X)
2314 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2315 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2316 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2317 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2318 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2319 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2320 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2321 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2322 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2323 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2324 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2325 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2327 #define SARG(X) u64 arg##X
2328 #define COPY(X) args[X] = arg##X
2330 #define __DL_COM (,)
2331 #define __DL_SEM (;)
2333 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2335 #define BPF_TRACE_DEFN_x(x) \
2336 void bpf_trace_run##x(struct bpf_prog *prog, \
2337 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2340 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2341 __bpf_trace_run(prog, args); \
2343 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2344 BPF_TRACE_DEFN_x(1);
2345 BPF_TRACE_DEFN_x(2);
2346 BPF_TRACE_DEFN_x(3);
2347 BPF_TRACE_DEFN_x(4);
2348 BPF_TRACE_DEFN_x(5);
2349 BPF_TRACE_DEFN_x(6);
2350 BPF_TRACE_DEFN_x(7);
2351 BPF_TRACE_DEFN_x(8);
2352 BPF_TRACE_DEFN_x(9);
2353 BPF_TRACE_DEFN_x(10);
2354 BPF_TRACE_DEFN_x(11);
2355 BPF_TRACE_DEFN_x(12);
2357 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2359 struct tracepoint *tp = btp->tp;
2362 * check that program doesn't access arguments beyond what's
2363 * available in this tracepoint
2365 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2368 if (prog->aux->max_tp_access > btp->writable_size)
2371 return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
2375 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2377 return __bpf_probe_register(btp, prog);
2380 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2382 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2385 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2386 u32 *fd_type, const char **buf,
2387 u64 *probe_offset, u64 *probe_addr)
2389 bool is_tracepoint, is_syscall_tp;
2390 struct bpf_prog *prog;
2397 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2398 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2401 *prog_id = prog->aux->id;
2402 flags = event->tp_event->flags;
2403 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2404 is_syscall_tp = is_syscall_trace_event(event->tp_event);
2406 if (is_tracepoint || is_syscall_tp) {
2407 *buf = is_tracepoint ? event->tp_event->tp->name
2408 : event->tp_event->name;
2409 /* We allow NULL pointer for tracepoint */
2411 *fd_type = BPF_FD_TYPE_TRACEPOINT;
2413 *probe_offset = 0x0;
2419 #ifdef CONFIG_KPROBE_EVENTS
2420 if (flags & TRACE_EVENT_FL_KPROBE)
2421 err = bpf_get_kprobe_info(event, fd_type, buf,
2422 probe_offset, probe_addr,
2423 event->attr.type == PERF_TYPE_TRACEPOINT);
2425 #ifdef CONFIG_UPROBE_EVENTS
2426 if (flags & TRACE_EVENT_FL_UPROBE)
2427 err = bpf_get_uprobe_info(event, fd_type, buf,
2428 probe_offset, probe_addr,
2429 event->attr.type == PERF_TYPE_TRACEPOINT);
2436 static int __init send_signal_irq_work_init(void)
2439 struct send_signal_irq_work *work;
2441 for_each_possible_cpu(cpu) {
2442 work = per_cpu_ptr(&send_signal_work, cpu);
2443 init_irq_work(&work->irq_work, do_bpf_send_signal);
2448 subsys_initcall(send_signal_irq_work_init);
2450 #ifdef CONFIG_MODULES
2451 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2454 struct bpf_trace_module *btm, *tmp;
2455 struct module *mod = module;
2458 if (mod->num_bpf_raw_events == 0 ||
2459 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2462 mutex_lock(&bpf_module_mutex);
2465 case MODULE_STATE_COMING:
2466 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2468 btm->module = module;
2469 list_add(&btm->list, &bpf_trace_modules);
2474 case MODULE_STATE_GOING:
2475 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2476 if (btm->module == module) {
2477 list_del(&btm->list);
2485 mutex_unlock(&bpf_module_mutex);
2488 return notifier_from_errno(ret);
2491 static struct notifier_block bpf_module_nb = {
2492 .notifier_call = bpf_event_notify,
2495 static int __init bpf_event_init(void)
2497 register_module_notifier(&bpf_module_nb);
2501 fs_initcall(bpf_event_init);
2502 #endif /* CONFIG_MODULES */
2504 #ifdef CONFIG_FPROBE
2505 struct bpf_kprobe_multi_link {
2506 struct bpf_link link;
2508 unsigned long *addrs;
2512 struct module **mods;
2516 struct bpf_kprobe_multi_run_ctx {
2517 struct bpf_run_ctx run_ctx;
2518 struct bpf_kprobe_multi_link *link;
2519 unsigned long entry_ip;
2527 static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
2529 unsigned long __user usymbol;
2530 const char **syms = NULL;
2531 char *buf = NULL, *p;
2535 syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
2539 buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
2543 for (p = buf, i = 0; i < cnt; i++) {
2544 if (__get_user(usymbol, usyms + i)) {
2548 err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
2549 if (err == KSYM_NAME_LEN)
2569 static void kprobe_multi_put_modules(struct module **mods, u32 cnt)
2573 for (i = 0; i < cnt; i++)
2574 module_put(mods[i]);
2577 static void free_user_syms(struct user_syms *us)
2583 static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2585 struct bpf_kprobe_multi_link *kmulti_link;
2587 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2588 unregister_fprobe(&kmulti_link->fp);
2589 kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt);
2592 static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
2594 struct bpf_kprobe_multi_link *kmulti_link;
2596 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2597 kvfree(kmulti_link->addrs);
2598 kvfree(kmulti_link->cookies);
2599 kfree(kmulti_link->mods);
2603 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
2604 struct bpf_link_info *info)
2606 u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
2607 struct bpf_kprobe_multi_link *kmulti_link;
2608 u32 ucount = info->kprobe_multi.count;
2611 if (!uaddrs ^ !ucount)
2614 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2615 info->kprobe_multi.count = kmulti_link->cnt;
2616 info->kprobe_multi.flags = kmulti_link->flags;
2620 if (ucount < kmulti_link->cnt)
2623 ucount = kmulti_link->cnt;
2625 if (kallsyms_show_value(current_cred())) {
2626 if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
2629 for (i = 0; i < ucount; i++) {
2630 if (put_user(0, uaddrs + i))
2637 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
2638 .release = bpf_kprobe_multi_link_release,
2639 .dealloc = bpf_kprobe_multi_link_dealloc,
2640 .fill_link_info = bpf_kprobe_multi_link_fill_link_info,
2643 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2645 const struct bpf_kprobe_multi_link *link = priv;
2646 unsigned long *addr_a = a, *addr_b = b;
2647 u64 *cookie_a, *cookie_b;
2649 cookie_a = link->cookies + (addr_a - link->addrs);
2650 cookie_b = link->cookies + (addr_b - link->addrs);
2652 /* swap addr_a/addr_b and cookie_a/cookie_b values */
2653 swap(*addr_a, *addr_b);
2654 swap(*cookie_a, *cookie_b);
2657 static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b)
2659 const unsigned long *addr_a = a, *addr_b = b;
2661 if (*addr_a == *addr_b)
2663 return *addr_a < *addr_b ? -1 : 1;
2666 static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2668 return bpf_kprobe_multi_addrs_cmp(a, b);
2671 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2673 struct bpf_kprobe_multi_run_ctx *run_ctx;
2674 struct bpf_kprobe_multi_link *link;
2675 u64 *cookie, entry_ip;
2676 unsigned long *addr;
2678 if (WARN_ON_ONCE(!ctx))
2680 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2681 link = run_ctx->link;
2684 entry_ip = run_ctx->entry_ip;
2685 addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
2686 bpf_kprobe_multi_addrs_cmp);
2689 cookie = link->cookies + (addr - link->addrs);
2693 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2695 struct bpf_kprobe_multi_run_ctx *run_ctx;
2697 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2698 return run_ctx->entry_ip;
2702 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2703 unsigned long entry_ip, struct pt_regs *regs)
2705 struct bpf_kprobe_multi_run_ctx run_ctx = {
2707 .entry_ip = entry_ip,
2709 struct bpf_run_ctx *old_run_ctx;
2712 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
2719 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2720 err = bpf_prog_run(link->link.prog, regs);
2721 bpf_reset_run_ctx(old_run_ctx);
2726 __this_cpu_dec(bpf_prog_active);
2731 kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
2732 unsigned long ret_ip, struct pt_regs *regs,
2735 struct bpf_kprobe_multi_link *link;
2737 link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2738 kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
2743 kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
2744 unsigned long ret_ip, struct pt_regs *regs,
2747 struct bpf_kprobe_multi_link *link;
2749 link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2750 kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
2753 static int symbols_cmp_r(const void *a, const void *b, const void *priv)
2755 const char **str_a = (const char **) a;
2756 const char **str_b = (const char **) b;
2758 return strcmp(*str_a, *str_b);
2761 struct multi_symbols_sort {
2766 static void symbols_swap_r(void *a, void *b, int size, const void *priv)
2768 const struct multi_symbols_sort *data = priv;
2769 const char **name_a = a, **name_b = b;
2771 swap(*name_a, *name_b);
2773 /* If defined, swap also related cookies. */
2774 if (data->cookies) {
2775 u64 *cookie_a, *cookie_b;
2777 cookie_a = data->cookies + (name_a - data->funcs);
2778 cookie_b = data->cookies + (name_b - data->funcs);
2779 swap(*cookie_a, *cookie_b);
2783 struct modules_array {
2784 struct module **mods;
2789 static int add_module(struct modules_array *arr, struct module *mod)
2791 struct module **mods;
2793 if (arr->mods_cnt == arr->mods_cap) {
2794 arr->mods_cap = max(16, arr->mods_cap * 3 / 2);
2795 mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL);
2801 arr->mods[arr->mods_cnt] = mod;
2806 static bool has_module(struct modules_array *arr, struct module *mod)
2810 for (i = arr->mods_cnt - 1; i >= 0; i--) {
2811 if (arr->mods[i] == mod)
2817 static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt)
2819 struct modules_array arr = {};
2822 for (i = 0; i < addrs_cnt; i++) {
2826 mod = __module_address(addrs[i]);
2827 /* Either no module or we it's already stored */
2828 if (!mod || has_module(&arr, mod)) {
2832 if (!try_module_get(mod))
2837 err = add_module(&arr, mod);
2844 /* We return either err < 0 in case of error, ... */
2846 kprobe_multi_put_modules(arr.mods, arr.mods_cnt);
2851 /* or number of modules found if everything is ok. */
2853 return arr.mods_cnt;
2856 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2858 struct bpf_kprobe_multi_link *link = NULL;
2859 struct bpf_link_primer link_primer;
2860 void __user *ucookies;
2861 unsigned long *addrs;
2862 u32 flags, cnt, size;
2863 void __user *uaddrs;
2864 u64 *cookies = NULL;
2868 /* no support for 32bit archs yet */
2869 if (sizeof(u64) != sizeof(void *))
2872 if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
2875 flags = attr->link_create.kprobe_multi.flags;
2876 if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
2879 uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
2880 usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
2881 if (!!uaddrs == !!usyms)
2884 cnt = attr->link_create.kprobe_multi.cnt;
2888 size = cnt * sizeof(*addrs);
2889 addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2893 ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2895 cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2900 if (copy_from_user(cookies, ucookies, size)) {
2907 if (copy_from_user(addrs, uaddrs, size)) {
2912 struct multi_symbols_sort data = {
2915 struct user_syms us;
2917 err = copy_user_syms(&us, usyms, cnt);
2922 data.funcs = us.syms;
2924 sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
2925 symbols_swap_r, &data);
2927 err = ftrace_lookup_symbols(us.syms, cnt, addrs);
2928 free_user_syms(&us);
2933 link = kzalloc(sizeof(*link), GFP_KERNEL);
2939 bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
2940 &bpf_kprobe_multi_link_lops, prog);
2942 err = bpf_link_prime(&link->link, &link_primer);
2946 if (flags & BPF_F_KPROBE_MULTI_RETURN)
2947 link->fp.exit_handler = kprobe_multi_link_exit_handler;
2949 link->fp.entry_handler = kprobe_multi_link_handler;
2951 link->addrs = addrs;
2952 link->cookies = cookies;
2954 link->flags = flags;
2958 * Sorting addresses will trigger sorting cookies as well
2959 * (check bpf_kprobe_multi_cookie_swap). This way we can
2960 * find cookie based on the address in bpf_get_attach_cookie
2963 sort_r(addrs, cnt, sizeof(*addrs),
2964 bpf_kprobe_multi_cookie_cmp,
2965 bpf_kprobe_multi_cookie_swap,
2969 err = get_modules_for_addrs(&link->mods, addrs, cnt);
2971 bpf_link_cleanup(&link_primer);
2974 link->mods_cnt = err;
2976 err = register_fprobe_ips(&link->fp, addrs, cnt);
2978 kprobe_multi_put_modules(link->mods, link->mods_cnt);
2979 bpf_link_cleanup(&link_primer);
2983 return bpf_link_settle(&link_primer);
2991 #else /* !CONFIG_FPROBE */
2992 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2996 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
3000 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3006 #ifdef CONFIG_UPROBES
3007 struct bpf_uprobe_multi_link;
3010 struct bpf_uprobe_multi_link *link;
3013 struct uprobe_consumer consumer;
3016 struct bpf_uprobe_multi_link {
3018 struct bpf_link link;
3020 struct bpf_uprobe *uprobes;
3021 struct task_struct *task;
3024 struct bpf_uprobe_multi_run_ctx {
3025 struct bpf_run_ctx run_ctx;
3026 unsigned long entry_ip;
3027 struct bpf_uprobe *uprobe;
3030 static void bpf_uprobe_unregister(struct path *path, struct bpf_uprobe *uprobes,
3035 for (i = 0; i < cnt; i++) {
3036 uprobe_unregister(d_real_inode(path->dentry), uprobes[i].offset,
3037 &uprobes[i].consumer);
3041 static void bpf_uprobe_multi_link_release(struct bpf_link *link)
3043 struct bpf_uprobe_multi_link *umulti_link;
3045 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3046 bpf_uprobe_unregister(&umulti_link->path, umulti_link->uprobes, umulti_link->cnt);
3049 static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link)
3051 struct bpf_uprobe_multi_link *umulti_link;
3053 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3054 if (umulti_link->task)
3055 put_task_struct(umulti_link->task);
3056 path_put(&umulti_link->path);
3057 kvfree(umulti_link->uprobes);
3061 static const struct bpf_link_ops bpf_uprobe_multi_link_lops = {
3062 .release = bpf_uprobe_multi_link_release,
3063 .dealloc = bpf_uprobe_multi_link_dealloc,
3066 static int uprobe_prog_run(struct bpf_uprobe *uprobe,
3067 unsigned long entry_ip,
3068 struct pt_regs *regs)
3070 struct bpf_uprobe_multi_link *link = uprobe->link;
3071 struct bpf_uprobe_multi_run_ctx run_ctx = {
3072 .entry_ip = entry_ip,
3075 struct bpf_prog *prog = link->link.prog;
3076 bool sleepable = prog->aux->sleepable;
3077 struct bpf_run_ctx *old_run_ctx;
3080 if (link->task && current != link->task)
3084 rcu_read_lock_trace();
3090 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
3091 err = bpf_prog_run(link->link.prog, regs);
3092 bpf_reset_run_ctx(old_run_ctx);
3097 rcu_read_unlock_trace();
3104 uprobe_multi_link_filter(struct uprobe_consumer *con, enum uprobe_filter_ctx ctx,
3105 struct mm_struct *mm)
3107 struct bpf_uprobe *uprobe;
3109 uprobe = container_of(con, struct bpf_uprobe, consumer);
3110 return uprobe->link->task->mm == mm;
3114 uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs)
3116 struct bpf_uprobe *uprobe;
3118 uprobe = container_of(con, struct bpf_uprobe, consumer);
3119 return uprobe_prog_run(uprobe, instruction_pointer(regs), regs);
3123 uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs)
3125 struct bpf_uprobe *uprobe;
3127 uprobe = container_of(con, struct bpf_uprobe, consumer);
3128 return uprobe_prog_run(uprobe, func, regs);
3131 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3133 struct bpf_uprobe_multi_run_ctx *run_ctx;
3135 run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
3136 return run_ctx->entry_ip;
3139 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
3141 struct bpf_uprobe_multi_run_ctx *run_ctx;
3143 run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
3144 return run_ctx->uprobe->cookie;
3147 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3149 struct bpf_uprobe_multi_link *link = NULL;
3150 unsigned long __user *uref_ctr_offsets;
3151 unsigned long *ref_ctr_offsets = NULL;
3152 struct bpf_link_primer link_primer;
3153 struct bpf_uprobe *uprobes = NULL;
3154 struct task_struct *task = NULL;
3155 unsigned long __user *uoffsets;
3156 u64 __user *ucookies;
3164 /* no support for 32bit archs yet */
3165 if (sizeof(u64) != sizeof(void *))
3168 if (prog->expected_attach_type != BPF_TRACE_UPROBE_MULTI)
3171 flags = attr->link_create.uprobe_multi.flags;
3172 if (flags & ~BPF_F_UPROBE_MULTI_RETURN)
3176 * path, offsets and cnt are mandatory,
3177 * ref_ctr_offsets and cookies are optional
3179 upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path);
3180 uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets);
3181 cnt = attr->link_create.uprobe_multi.cnt;
3183 if (!upath || !uoffsets || !cnt)
3186 uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets);
3187 ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies);
3189 name = strndup_user(upath, PATH_MAX);
3191 err = PTR_ERR(name);
3195 err = kern_path(name, LOOKUP_FOLLOW, &path);
3200 if (!d_is_reg(path.dentry)) {
3202 goto error_path_put;
3205 pid = attr->link_create.uprobe_multi.pid;
3208 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3211 goto error_path_put;
3216 link = kzalloc(sizeof(*link), GFP_KERNEL);
3217 uprobes = kvcalloc(cnt, sizeof(*uprobes), GFP_KERNEL);
3219 if (!uprobes || !link)
3222 if (uref_ctr_offsets) {
3223 ref_ctr_offsets = kvcalloc(cnt, sizeof(*ref_ctr_offsets), GFP_KERNEL);
3224 if (!ref_ctr_offsets)
3228 for (i = 0; i < cnt; i++) {
3229 if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) {
3233 if (uref_ctr_offsets && __get_user(ref_ctr_offsets[i], uref_ctr_offsets + i)) {
3237 if (__get_user(uprobes[i].offset, uoffsets + i)) {
3242 uprobes[i].link = link;
3244 if (flags & BPF_F_UPROBE_MULTI_RETURN)
3245 uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler;
3247 uprobes[i].consumer.handler = uprobe_multi_link_handler;
3250 uprobes[i].consumer.filter = uprobe_multi_link_filter;
3254 link->uprobes = uprobes;
3258 bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI,
3259 &bpf_uprobe_multi_link_lops, prog);
3261 for (i = 0; i < cnt; i++) {
3262 err = uprobe_register_refctr(d_real_inode(link->path.dentry),
3264 ref_ctr_offsets ? ref_ctr_offsets[i] : 0,
3265 &uprobes[i].consumer);
3267 bpf_uprobe_unregister(&path, uprobes, i);
3272 err = bpf_link_prime(&link->link, &link_primer);
3276 kvfree(ref_ctr_offsets);
3277 return bpf_link_settle(&link_primer);
3280 kvfree(ref_ctr_offsets);
3284 put_task_struct(task);
3289 #else /* !CONFIG_UPROBES */
3290 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3294 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
3298 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3302 #endif /* CONFIG_UPROBES */