1179a0cc4SSteven Rostedt (VMware) // SPDX-License-Identifier: GPL-2.0
22541517cSAlexei Starovoitov /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
30515e599SAlexei Starovoitov * Copyright (c) 2016 Facebook
42541517cSAlexei Starovoitov */
52541517cSAlexei Starovoitov #include <linux/kernel.h>
62541517cSAlexei Starovoitov #include <linux/types.h>
72541517cSAlexei Starovoitov #include <linux/slab.h>
82541517cSAlexei Starovoitov #include <linux/bpf.h>
94279adb0SMartin KaFai Lau #include <linux/bpf_verifier.h>
100515e599SAlexei Starovoitov #include <linux/bpf_perf_event.h>
11c4d0bfb4SAlan Maguire #include <linux/btf.h>
122541517cSAlexei Starovoitov #include <linux/filter.h>
132541517cSAlexei Starovoitov #include <linux/uaccess.h>
149c959c86SAlexei Starovoitov #include <linux/ctype.h>
159802d865SJosef Bacik #include <linux/kprobes.h>
16ac5a72eaSAlan Maguire #include <linux/spinlock.h>
1741bdc4b4SYonghong Song #include <linux/syscalls.h>
18540adea3SMasami Hiramatsu #include <linux/error-injection.h>
19c9a0f3b8SJiri Olsa #include <linux/btf_ids.h>
206f100640SKP Singh #include <linux/bpf_lsm.h>
210dcac272SJiri Olsa #include <linux/fprobe.h>
22ca74823cSJiri Olsa #include <linux/bsearch.h>
23ca74823cSJiri Olsa #include <linux/sort.h>
24f3cf4134SRoberto Sassu #include <linux/key.h>
25f3cf4134SRoberto Sassu #include <linux/verification.h>
2689ae89f5SJiri Olsa #include <linux/namei.h>
276f100640SKP Singh
288e4597c6SMartin KaFai Lau #include <net/bpf_sk_storage.h>
299802d865SJosef Bacik
30c4d0bfb4SAlan Maguire #include <uapi/linux/bpf.h>
31c4d0bfb4SAlan Maguire #include <uapi/linux/btf.h>
32c4d0bfb4SAlan Maguire
33c7b6f29bSNadav Amit #include <asm/tlb.h>
34c7b6f29bSNadav Amit
359802d865SJosef Bacik #include "trace_probe.h"
362541517cSAlexei Starovoitov #include "trace.h"
372541517cSAlexei Starovoitov
38ac5a72eaSAlan Maguire #define CREATE_TRACE_POINTS
39ac5a72eaSAlan Maguire #include "bpf_trace.h"
40ac5a72eaSAlan Maguire
41e672db03SStanislav Fomichev #define bpf_event_rcu_dereference(p) \
42e672db03SStanislav Fomichev rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
43e672db03SStanislav Fomichev
445735054aSHou Tao #define MAX_UPROBE_MULTI_CNT (1U << 20)
453d83b820SHou Tao #define MAX_KPROBE_MULTI_CNT (1U << 20)
465735054aSHou Tao
47a38d1107SMatt Mullins #ifdef CONFIG_MODULES
48a38d1107SMatt Mullins struct bpf_trace_module {
49a38d1107SMatt Mullins struct module *module;
50a38d1107SMatt Mullins struct list_head list;
51a38d1107SMatt Mullins };
52a38d1107SMatt Mullins
53a38d1107SMatt Mullins static LIST_HEAD(bpf_trace_modules);
54a38d1107SMatt Mullins static DEFINE_MUTEX(bpf_module_mutex);
55a38d1107SMatt Mullins
bpf_get_raw_tracepoint_module(const char * name)56a38d1107SMatt Mullins static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
57a38d1107SMatt Mullins {
58a38d1107SMatt Mullins struct bpf_raw_event_map *btp, *ret = NULL;
59a38d1107SMatt Mullins struct bpf_trace_module *btm;
60a38d1107SMatt Mullins unsigned int i;
61a38d1107SMatt Mullins
62a38d1107SMatt Mullins mutex_lock(&bpf_module_mutex);
63a38d1107SMatt Mullins list_for_each_entry(btm, &bpf_trace_modules, list) {
64a38d1107SMatt Mullins for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
65a38d1107SMatt Mullins btp = &btm->module->bpf_raw_events[i];
66a38d1107SMatt Mullins if (!strcmp(btp->tp->name, name)) {
67a38d1107SMatt Mullins if (try_module_get(btm->module))
68a38d1107SMatt Mullins ret = btp;
69a38d1107SMatt Mullins goto out;
70a38d1107SMatt Mullins }
71a38d1107SMatt Mullins }
72a38d1107SMatt Mullins }
73a38d1107SMatt Mullins out:
74a38d1107SMatt Mullins mutex_unlock(&bpf_module_mutex);
75a38d1107SMatt Mullins return ret;
76a38d1107SMatt Mullins }
77a38d1107SMatt Mullins #else
bpf_get_raw_tracepoint_module(const char * name)78a38d1107SMatt Mullins static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
79a38d1107SMatt Mullins {
80a38d1107SMatt Mullins return NULL;
81a38d1107SMatt Mullins }
82a38d1107SMatt Mullins #endif /* CONFIG_MODULES */
83a38d1107SMatt Mullins
84035226b9SGianluca Borello u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
85c195651eSYonghong Song u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
86035226b9SGianluca Borello
87eb411377SAlan Maguire static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
88eb411377SAlan Maguire u64 flags, const struct btf **btf,
89eb411377SAlan Maguire s32 *btf_id);
90f7098690SJiri Olsa static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
91f7098690SJiri Olsa static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
92eb411377SAlan Maguire
930b779b61SJiri Olsa static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx);
94686328d8SJiri Olsa static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
950b779b61SJiri Olsa
962541517cSAlexei Starovoitov /**
972541517cSAlexei Starovoitov * trace_call_bpf - invoke BPF program
98e87c6bc3SYonghong Song * @call: tracepoint event
992541517cSAlexei Starovoitov * @ctx: opaque context pointer
1002541517cSAlexei Starovoitov *
1012541517cSAlexei Starovoitov * kprobe handlers execute BPF programs via this helper.
1022541517cSAlexei Starovoitov * Can be used from static tracepoints in the future.
1032541517cSAlexei Starovoitov *
1042541517cSAlexei Starovoitov * Return: BPF programs always return an integer which is interpreted by
1052541517cSAlexei Starovoitov * kprobe handler as:
1062541517cSAlexei Starovoitov * 0 - return from kprobe (event is filtered out)
1072541517cSAlexei Starovoitov * 1 - store kprobe event into ring buffer
1082541517cSAlexei Starovoitov * Other values are reserved and currently alias to 1
1092541517cSAlexei Starovoitov */
trace_call_bpf(struct trace_event_call * call,void * ctx)110e87c6bc3SYonghong Song unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
1112541517cSAlexei Starovoitov {
1122541517cSAlexei Starovoitov unsigned int ret;
1132541517cSAlexei Starovoitov
114b0a81b94SThomas Gleixner cant_sleep();
1152541517cSAlexei Starovoitov
1162541517cSAlexei Starovoitov if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
1172541517cSAlexei Starovoitov /*
1182541517cSAlexei Starovoitov * since some bpf program is already running on this cpu,
1192541517cSAlexei Starovoitov * don't call into another bpf program (same or different)
1202541517cSAlexei Starovoitov * and don't send kprobe event into ring-buffer,
1212541517cSAlexei Starovoitov * so return zero here
1222541517cSAlexei Starovoitov */
1232541517cSAlexei Starovoitov ret = 0;
1242541517cSAlexei Starovoitov goto out;
1252541517cSAlexei Starovoitov }
1262541517cSAlexei Starovoitov
127e87c6bc3SYonghong Song /*
128e87c6bc3SYonghong Song * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
129e87c6bc3SYonghong Song * to all call sites, we did a bpf_prog_array_valid() there to check
130e87c6bc3SYonghong Song * whether call->prog_array is empty or not, which is
1312b5894ccSQiujun Huang * a heuristic to speed up execution.
132e87c6bc3SYonghong Song *
133e87c6bc3SYonghong Song * If bpf_prog_array_valid() fetched prog_array was
134e87c6bc3SYonghong Song * non-NULL, we go into trace_call_bpf() and do the actual
135e87c6bc3SYonghong Song * proper rcu_dereference() under RCU lock.
136e87c6bc3SYonghong Song * If it turns out that prog_array is NULL then, we bail out.
137e87c6bc3SYonghong Song * For the opposite, if the bpf_prog_array_valid() fetched pointer
138e87c6bc3SYonghong Song * was NULL, you'll skip the prog_array with the risk of missing
139e87c6bc3SYonghong Song * out of events when it was updated in between this and the
140e87c6bc3SYonghong Song * rcu_dereference() which is accepted risk.
141e87c6bc3SYonghong Song */
142055eb955SStanislav Fomichev rcu_read_lock();
143055eb955SStanislav Fomichev ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
144055eb955SStanislav Fomichev ctx, bpf_prog_run);
145055eb955SStanislav Fomichev rcu_read_unlock();
1462541517cSAlexei Starovoitov
1472541517cSAlexei Starovoitov out:
1482541517cSAlexei Starovoitov __this_cpu_dec(bpf_prog_active);
1492541517cSAlexei Starovoitov
1502541517cSAlexei Starovoitov return ret;
1512541517cSAlexei Starovoitov }
1522541517cSAlexei Starovoitov
1539802d865SJosef Bacik #ifdef CONFIG_BPF_KPROBE_OVERRIDE
BPF_CALL_2(bpf_override_return,struct pt_regs *,regs,unsigned long,rc)1549802d865SJosef Bacik BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
1559802d865SJosef Bacik {
1569802d865SJosef Bacik regs_set_return_value(regs, rc);
157540adea3SMasami Hiramatsu override_function_with_return(regs);
1589802d865SJosef Bacik return 0;
1599802d865SJosef Bacik }
1609802d865SJosef Bacik
1619802d865SJosef Bacik static const struct bpf_func_proto bpf_override_return_proto = {
1629802d865SJosef Bacik .func = bpf_override_return,
1639802d865SJosef Bacik .gpl_only = true,
1649802d865SJosef Bacik .ret_type = RET_INTEGER,
1659802d865SJosef Bacik .arg1_type = ARG_PTR_TO_CTX,
1669802d865SJosef Bacik .arg2_type = ARG_ANYTHING,
1679802d865SJosef Bacik };
1689802d865SJosef Bacik #endif
1699802d865SJosef Bacik
1708d92db5cSChristoph Hellwig static __always_inline int
bpf_probe_read_user_common(void * dst,u32 size,const void __user * unsafe_ptr)1718d92db5cSChristoph Hellwig bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
1728d92db5cSChristoph Hellwig {
1738d92db5cSChristoph Hellwig int ret;
1748d92db5cSChristoph Hellwig
175c0ee37e8SChristoph Hellwig ret = copy_from_user_nofault(dst, unsafe_ptr, size);
1768d92db5cSChristoph Hellwig if (unlikely(ret < 0))
1778d92db5cSChristoph Hellwig memset(dst, 0, size);
1788d92db5cSChristoph Hellwig return ret;
1798d92db5cSChristoph Hellwig }
1808d92db5cSChristoph Hellwig
BPF_CALL_3(bpf_probe_read_user,void *,dst,u32,size,const void __user *,unsafe_ptr)1816ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
1826ae08ae3SDaniel Borkmann const void __user *, unsafe_ptr)
1832541517cSAlexei Starovoitov {
1848d92db5cSChristoph Hellwig return bpf_probe_read_user_common(dst, size, unsafe_ptr);
1852541517cSAlexei Starovoitov }
1862541517cSAlexei Starovoitov
187f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_user_proto = {
1886ae08ae3SDaniel Borkmann .func = bpf_probe_read_user,
1896ae08ae3SDaniel Borkmann .gpl_only = true,
1906ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
1916ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1926ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1936ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
1946ae08ae3SDaniel Borkmann };
1956ae08ae3SDaniel Borkmann
1968d92db5cSChristoph Hellwig static __always_inline int
bpf_probe_read_user_str_common(void * dst,u32 size,const void __user * unsafe_ptr)1978d92db5cSChristoph Hellwig bpf_probe_read_user_str_common(void *dst, u32 size,
1988d92db5cSChristoph Hellwig const void __user *unsafe_ptr)
1998d92db5cSChristoph Hellwig {
2008d92db5cSChristoph Hellwig int ret;
2018d92db5cSChristoph Hellwig
2026fa6d280SDaniel Xu /*
2036fa6d280SDaniel Xu * NB: We rely on strncpy_from_user() not copying junk past the NUL
2046fa6d280SDaniel Xu * terminator into `dst`.
2056fa6d280SDaniel Xu *
2066fa6d280SDaniel Xu * strncpy_from_user() does long-sized strides in the fast path. If the
2076fa6d280SDaniel Xu * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
2086fa6d280SDaniel Xu * then there could be junk after the NUL in `dst`. If user takes `dst`
2096fa6d280SDaniel Xu * and keys a hash map with it, then semantically identical strings can
2106fa6d280SDaniel Xu * occupy multiple entries in the map.
2116fa6d280SDaniel Xu */
2128d92db5cSChristoph Hellwig ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
2138d92db5cSChristoph Hellwig if (unlikely(ret < 0))
2148d92db5cSChristoph Hellwig memset(dst, 0, size);
2158d92db5cSChristoph Hellwig return ret;
2168d92db5cSChristoph Hellwig }
2178d92db5cSChristoph Hellwig
BPF_CALL_3(bpf_probe_read_user_str,void *,dst,u32,size,const void __user *,unsafe_ptr)2186ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
2196ae08ae3SDaniel Borkmann const void __user *, unsafe_ptr)
2206ae08ae3SDaniel Borkmann {
2218d92db5cSChristoph Hellwig return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
2226ae08ae3SDaniel Borkmann }
2236ae08ae3SDaniel Borkmann
224f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_user_str_proto = {
2256ae08ae3SDaniel Borkmann .func = bpf_probe_read_user_str,
2266ae08ae3SDaniel Borkmann .gpl_only = true,
2276ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
2286ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
2296ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2306ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
2316ae08ae3SDaniel Borkmann };
2326ae08ae3SDaniel Borkmann
BPF_CALL_3(bpf_probe_read_kernel,void *,dst,u32,size,const void *,unsafe_ptr)2336ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
2346ae08ae3SDaniel Borkmann const void *, unsafe_ptr)
2356ae08ae3SDaniel Borkmann {
2368d92db5cSChristoph Hellwig return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
2376ae08ae3SDaniel Borkmann }
2386ae08ae3SDaniel Borkmann
239f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_kernel_proto = {
2406ae08ae3SDaniel Borkmann .func = bpf_probe_read_kernel,
2416ae08ae3SDaniel Borkmann .gpl_only = true,
2426ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
2436ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
2446ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2456ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
2466ae08ae3SDaniel Borkmann };
2476ae08ae3SDaniel Borkmann
2486ae08ae3SDaniel Borkmann static __always_inline int
bpf_probe_read_kernel_str_common(void * dst,u32 size,const void * unsafe_ptr)2498d92db5cSChristoph Hellwig bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
2506ae08ae3SDaniel Borkmann {
251ff40e510SDaniel Borkmann int ret;
2528d92db5cSChristoph Hellwig
2536ae08ae3SDaniel Borkmann /*
2548d92db5cSChristoph Hellwig * The strncpy_from_kernel_nofault() call will likely not fill the
2558d92db5cSChristoph Hellwig * entire buffer, but that's okay in this circumstance as we're probing
2566ae08ae3SDaniel Borkmann * arbitrary memory anyway similar to bpf_probe_read_*() and might
2576ae08ae3SDaniel Borkmann * as well probe the stack. Thus, memory is explicitly cleared
2586ae08ae3SDaniel Borkmann * only in error case, so that improper users ignoring return
2596ae08ae3SDaniel Borkmann * code altogether don't copy garbage; otherwise length of string
2606ae08ae3SDaniel Borkmann * is returned that can be used for bpf_perf_event_output() et al.
2616ae08ae3SDaniel Borkmann */
2628d92db5cSChristoph Hellwig ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
2636ae08ae3SDaniel Borkmann if (unlikely(ret < 0))
2646ae08ae3SDaniel Borkmann memset(dst, 0, size);
2656ae08ae3SDaniel Borkmann return ret;
2666ae08ae3SDaniel Borkmann }
2676ae08ae3SDaniel Borkmann
BPF_CALL_3(bpf_probe_read_kernel_str,void *,dst,u32,size,const void *,unsafe_ptr)2686ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
2696ae08ae3SDaniel Borkmann const void *, unsafe_ptr)
2706ae08ae3SDaniel Borkmann {
2718d92db5cSChristoph Hellwig return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
2726ae08ae3SDaniel Borkmann }
2736ae08ae3SDaniel Borkmann
274f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
2756ae08ae3SDaniel Borkmann .func = bpf_probe_read_kernel_str,
2766ae08ae3SDaniel Borkmann .gpl_only = true,
2776ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
2786ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
2796ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2806ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
2816ae08ae3SDaniel Borkmann };
2826ae08ae3SDaniel Borkmann
2838d92db5cSChristoph Hellwig #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
BPF_CALL_3(bpf_probe_read_compat,void *,dst,u32,size,const void *,unsafe_ptr)2848d92db5cSChristoph Hellwig BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
2858d92db5cSChristoph Hellwig const void *, unsafe_ptr)
2868d92db5cSChristoph Hellwig {
2878d92db5cSChristoph Hellwig if ((unsigned long)unsafe_ptr < TASK_SIZE) {
2888d92db5cSChristoph Hellwig return bpf_probe_read_user_common(dst, size,
2898d92db5cSChristoph Hellwig (__force void __user *)unsafe_ptr);
2908d92db5cSChristoph Hellwig }
2918d92db5cSChristoph Hellwig return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
2928d92db5cSChristoph Hellwig }
2938d92db5cSChristoph Hellwig
2948d92db5cSChristoph Hellwig static const struct bpf_func_proto bpf_probe_read_compat_proto = {
2958d92db5cSChristoph Hellwig .func = bpf_probe_read_compat,
2968d92db5cSChristoph Hellwig .gpl_only = true,
2978d92db5cSChristoph Hellwig .ret_type = RET_INTEGER,
2988d92db5cSChristoph Hellwig .arg1_type = ARG_PTR_TO_UNINIT_MEM,
2998d92db5cSChristoph Hellwig .arg2_type = ARG_CONST_SIZE_OR_ZERO,
3008d92db5cSChristoph Hellwig .arg3_type = ARG_ANYTHING,
3018d92db5cSChristoph Hellwig };
3028d92db5cSChristoph Hellwig
BPF_CALL_3(bpf_probe_read_compat_str,void *,dst,u32,size,const void *,unsafe_ptr)3036ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
3046ae08ae3SDaniel Borkmann const void *, unsafe_ptr)
3056ae08ae3SDaniel Borkmann {
3068d92db5cSChristoph Hellwig if ((unsigned long)unsafe_ptr < TASK_SIZE) {
3078d92db5cSChristoph Hellwig return bpf_probe_read_user_str_common(dst, size,
3088d92db5cSChristoph Hellwig (__force void __user *)unsafe_ptr);
3098d92db5cSChristoph Hellwig }
3108d92db5cSChristoph Hellwig return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
3116ae08ae3SDaniel Borkmann }
3126ae08ae3SDaniel Borkmann
3136ae08ae3SDaniel Borkmann static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
3146ae08ae3SDaniel Borkmann .func = bpf_probe_read_compat_str,
3152541517cSAlexei Starovoitov .gpl_only = true,
3162541517cSAlexei Starovoitov .ret_type = RET_INTEGER,
31739f19ebbSAlexei Starovoitov .arg1_type = ARG_PTR_TO_UNINIT_MEM,
3189c019e2bSYonghong Song .arg2_type = ARG_CONST_SIZE_OR_ZERO,
3192541517cSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
3202541517cSAlexei Starovoitov };
3218d92db5cSChristoph Hellwig #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
3222541517cSAlexei Starovoitov
BPF_CALL_3(bpf_probe_write_user,void __user *,unsafe_ptr,const void *,src,u32,size)323eb1b6688SDaniel Borkmann BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
324f3694e00SDaniel Borkmann u32, size)
32596ae5227SSargun Dhillon {
32696ae5227SSargun Dhillon /*
32796ae5227SSargun Dhillon * Ensure we're in user context which is safe for the helper to
32896ae5227SSargun Dhillon * run. This helper has no business in a kthread.
32996ae5227SSargun Dhillon *
33096ae5227SSargun Dhillon * access_ok() should prevent writing to non-user memory, but in
33196ae5227SSargun Dhillon * some situations (nommu, temporary switch, etc) access_ok() does
33296ae5227SSargun Dhillon * not provide enough validation, hence the check on KERNEL_DS.
333c7b6f29bSNadav Amit *
334c7b6f29bSNadav Amit * nmi_uaccess_okay() ensures the probe is not run in an interim
335c7b6f29bSNadav Amit * state, when the task or mm are switched. This is specifically
336c7b6f29bSNadav Amit * required to prevent the use of temporary mm.
33796ae5227SSargun Dhillon */
33896ae5227SSargun Dhillon
33996ae5227SSargun Dhillon if (unlikely(in_interrupt() ||
34096ae5227SSargun Dhillon current->flags & (PF_KTHREAD | PF_EXITING)))
34196ae5227SSargun Dhillon return -EPERM;
342c7b6f29bSNadav Amit if (unlikely(!nmi_uaccess_okay()))
343c7b6f29bSNadav Amit return -EPERM;
34496ae5227SSargun Dhillon
345c0ee37e8SChristoph Hellwig return copy_to_user_nofault(unsafe_ptr, src, size);
34696ae5227SSargun Dhillon }
34796ae5227SSargun Dhillon
34896ae5227SSargun Dhillon static const struct bpf_func_proto bpf_probe_write_user_proto = {
34996ae5227SSargun Dhillon .func = bpf_probe_write_user,
35096ae5227SSargun Dhillon .gpl_only = true,
35196ae5227SSargun Dhillon .ret_type = RET_INTEGER,
35296ae5227SSargun Dhillon .arg1_type = ARG_ANYTHING,
353216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
35439f19ebbSAlexei Starovoitov .arg3_type = ARG_CONST_SIZE,
35596ae5227SSargun Dhillon };
35696ae5227SSargun Dhillon
bpf_get_probe_write_proto(void)35796ae5227SSargun Dhillon static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
35896ae5227SSargun Dhillon {
3592c78ee89SAlexei Starovoitov if (!capable(CAP_SYS_ADMIN))
3602c78ee89SAlexei Starovoitov return NULL;
3612c78ee89SAlexei Starovoitov
36296ae5227SSargun Dhillon pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
36396ae5227SSargun Dhillon current->comm, task_pid_nr(current));
36496ae5227SSargun Dhillon
36596ae5227SSargun Dhillon return &bpf_probe_write_user_proto;
36696ae5227SSargun Dhillon }
36796ae5227SSargun Dhillon
368d9c9e4dbSFlorent Revest #define MAX_TRACE_PRINTK_VARARGS 3
369ac5a72eaSAlan Maguire #define BPF_TRACE_PRINTK_SIZE 1024
370ac5a72eaSAlan Maguire
BPF_CALL_5(bpf_trace_printk,char *,fmt,u32,fmt_size,u64,arg1,u64,arg2,u64,arg3)371f3694e00SDaniel Borkmann BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
372f3694e00SDaniel Borkmann u64, arg2, u64, arg3)
3739c959c86SAlexei Starovoitov {
374d9c9e4dbSFlorent Revest u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
37578aa1cc9SJiri Olsa struct bpf_bprintf_data data = {
37678aa1cc9SJiri Olsa .get_bin_args = true,
377e2bb9e01SJiri Olsa .get_buf = true,
37878aa1cc9SJiri Olsa };
379d9c9e4dbSFlorent Revest int ret;
3809c959c86SAlexei Starovoitov
38178aa1cc9SJiri Olsa ret = bpf_bprintf_prepare(fmt, fmt_size, args,
38278aa1cc9SJiri Olsa MAX_TRACE_PRINTK_VARARGS, &data);
383d9c9e4dbSFlorent Revest if (ret < 0)
384d9c9e4dbSFlorent Revest return ret;
3859c959c86SAlexei Starovoitov
386e2bb9e01SJiri Olsa ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
3879c959c86SAlexei Starovoitov
388e2bb9e01SJiri Olsa trace_bpf_trace_printk(data.buf);
3899c959c86SAlexei Starovoitov
390f19a4050SJiri Olsa bpf_bprintf_cleanup(&data);
3919c959c86SAlexei Starovoitov
392d9c9e4dbSFlorent Revest return ret;
3939c959c86SAlexei Starovoitov }
3949c959c86SAlexei Starovoitov
3959c959c86SAlexei Starovoitov static const struct bpf_func_proto bpf_trace_printk_proto = {
3969c959c86SAlexei Starovoitov .func = bpf_trace_printk,
3979c959c86SAlexei Starovoitov .gpl_only = true,
3989c959c86SAlexei Starovoitov .ret_type = RET_INTEGER,
399216e3cd2SHao Luo .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
40039f19ebbSAlexei Starovoitov .arg2_type = ARG_CONST_SIZE,
4019c959c86SAlexei Starovoitov };
4029c959c86SAlexei Starovoitov
__set_printk_clr_event(void)40310aceb62SDave Marchevsky static void __set_printk_clr_event(void)
4040756ea3eSAlexei Starovoitov {
4050756ea3eSAlexei Starovoitov /*
406ac5a72eaSAlan Maguire * This program might be calling bpf_trace_printk,
407ac5a72eaSAlan Maguire * so enable the associated bpf_trace/bpf_trace_printk event.
408ac5a72eaSAlan Maguire * Repeat this each time as it is possible a user has
409ac5a72eaSAlan Maguire * disabled bpf_trace_printk events. By loading a program
410ac5a72eaSAlan Maguire * calling bpf_trace_printk() however the user has expressed
411ac5a72eaSAlan Maguire * the intent to see such events.
4120756ea3eSAlexei Starovoitov */
413ac5a72eaSAlan Maguire if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
414ac5a72eaSAlan Maguire pr_warn_ratelimited("could not enable bpf_trace_printk events");
41510aceb62SDave Marchevsky }
4160756ea3eSAlexei Starovoitov
bpf_get_trace_printk_proto(void)41710aceb62SDave Marchevsky const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
41810aceb62SDave Marchevsky {
41910aceb62SDave Marchevsky __set_printk_clr_event();
4200756ea3eSAlexei Starovoitov return &bpf_trace_printk_proto;
4210756ea3eSAlexei Starovoitov }
4220756ea3eSAlexei Starovoitov
BPF_CALL_4(bpf_trace_vprintk,char *,fmt,u32,fmt_size,const void *,args,u32,data_len)42378aa1cc9SJiri Olsa BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args,
42410aceb62SDave Marchevsky u32, data_len)
42510aceb62SDave Marchevsky {
42678aa1cc9SJiri Olsa struct bpf_bprintf_data data = {
42778aa1cc9SJiri Olsa .get_bin_args = true,
428e2bb9e01SJiri Olsa .get_buf = true,
42978aa1cc9SJiri Olsa };
43010aceb62SDave Marchevsky int ret, num_args;
43110aceb62SDave Marchevsky
43210aceb62SDave Marchevsky if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
43378aa1cc9SJiri Olsa (data_len && !args))
43410aceb62SDave Marchevsky return -EINVAL;
43510aceb62SDave Marchevsky num_args = data_len / 8;
43610aceb62SDave Marchevsky
43778aa1cc9SJiri Olsa ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
43810aceb62SDave Marchevsky if (ret < 0)
43910aceb62SDave Marchevsky return ret;
44010aceb62SDave Marchevsky
441e2bb9e01SJiri Olsa ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
44210aceb62SDave Marchevsky
443e2bb9e01SJiri Olsa trace_bpf_trace_printk(data.buf);
44410aceb62SDave Marchevsky
445f19a4050SJiri Olsa bpf_bprintf_cleanup(&data);
44610aceb62SDave Marchevsky
44710aceb62SDave Marchevsky return ret;
44810aceb62SDave Marchevsky }
44910aceb62SDave Marchevsky
45010aceb62SDave Marchevsky static const struct bpf_func_proto bpf_trace_vprintk_proto = {
45110aceb62SDave Marchevsky .func = bpf_trace_vprintk,
45210aceb62SDave Marchevsky .gpl_only = true,
45310aceb62SDave Marchevsky .ret_type = RET_INTEGER,
454216e3cd2SHao Luo .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
45510aceb62SDave Marchevsky .arg2_type = ARG_CONST_SIZE,
456216e3cd2SHao Luo .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
45710aceb62SDave Marchevsky .arg4_type = ARG_CONST_SIZE_OR_ZERO,
45810aceb62SDave Marchevsky };
45910aceb62SDave Marchevsky
bpf_get_trace_vprintk_proto(void)46010aceb62SDave Marchevsky const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
46110aceb62SDave Marchevsky {
46210aceb62SDave Marchevsky __set_printk_clr_event();
46310aceb62SDave Marchevsky return &bpf_trace_vprintk_proto;
46410aceb62SDave Marchevsky }
46510aceb62SDave Marchevsky
BPF_CALL_5(bpf_seq_printf,struct seq_file *,m,char *,fmt,u32,fmt_size,const void *,args,u32,data_len)466492e639fSYonghong Song BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
46778aa1cc9SJiri Olsa const void *, args, u32, data_len)
468492e639fSYonghong Song {
46978aa1cc9SJiri Olsa struct bpf_bprintf_data data = {
47078aa1cc9SJiri Olsa .get_bin_args = true,
47178aa1cc9SJiri Olsa };
472d9c9e4dbSFlorent Revest int err, num_args;
473492e639fSYonghong Song
474335ff499SDave Marchevsky if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
47578aa1cc9SJiri Olsa (data_len && !args))
476d9c9e4dbSFlorent Revest return -EINVAL;
477492e639fSYonghong Song num_args = data_len / 8;
478492e639fSYonghong Song
47978aa1cc9SJiri Olsa err = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
480492e639fSYonghong Song if (err < 0)
481d9c9e4dbSFlorent Revest return err;
482492e639fSYonghong Song
48378aa1cc9SJiri Olsa seq_bprintf(m, fmt, data.bin_args);
484492e639fSYonghong Song
485f19a4050SJiri Olsa bpf_bprintf_cleanup(&data);
486d9c9e4dbSFlorent Revest
487d9c9e4dbSFlorent Revest return seq_has_overflowed(m) ? -EOVERFLOW : 0;
488492e639fSYonghong Song }
489492e639fSYonghong Song
4909436ef6eSLorenz Bauer BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
491c9a0f3b8SJiri Olsa
492492e639fSYonghong Song static const struct bpf_func_proto bpf_seq_printf_proto = {
493492e639fSYonghong Song .func = bpf_seq_printf,
494492e639fSYonghong Song .gpl_only = true,
495492e639fSYonghong Song .ret_type = RET_INTEGER,
496492e639fSYonghong Song .arg1_type = ARG_PTR_TO_BTF_ID,
4979436ef6eSLorenz Bauer .arg1_btf_id = &btf_seq_file_ids[0],
498216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
499492e639fSYonghong Song .arg3_type = ARG_CONST_SIZE,
500216e3cd2SHao Luo .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
501492e639fSYonghong Song .arg5_type = ARG_CONST_SIZE_OR_ZERO,
502492e639fSYonghong Song };
503492e639fSYonghong Song
BPF_CALL_3(bpf_seq_write,struct seq_file *,m,const void *,data,u32,len)504492e639fSYonghong Song BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
505492e639fSYonghong Song {
506492e639fSYonghong Song return seq_write(m, data, len) ? -EOVERFLOW : 0;
507492e639fSYonghong Song }
508492e639fSYonghong Song
509492e639fSYonghong Song static const struct bpf_func_proto bpf_seq_write_proto = {
510492e639fSYonghong Song .func = bpf_seq_write,
511492e639fSYonghong Song .gpl_only = true,
512492e639fSYonghong Song .ret_type = RET_INTEGER,
513492e639fSYonghong Song .arg1_type = ARG_PTR_TO_BTF_ID,
5149436ef6eSLorenz Bauer .arg1_btf_id = &btf_seq_file_ids[0],
515216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
516492e639fSYonghong Song .arg3_type = ARG_CONST_SIZE_OR_ZERO,
517492e639fSYonghong Song };
518492e639fSYonghong Song
BPF_CALL_4(bpf_seq_printf_btf,struct seq_file *,m,struct btf_ptr *,ptr,u32,btf_ptr_size,u64,flags)519eb411377SAlan Maguire BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
520eb411377SAlan Maguire u32, btf_ptr_size, u64, flags)
521eb411377SAlan Maguire {
522eb411377SAlan Maguire const struct btf *btf;
523eb411377SAlan Maguire s32 btf_id;
524eb411377SAlan Maguire int ret;
525eb411377SAlan Maguire
526eb411377SAlan Maguire ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
527eb411377SAlan Maguire if (ret)
528eb411377SAlan Maguire return ret;
529eb411377SAlan Maguire
530eb411377SAlan Maguire return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
531eb411377SAlan Maguire }
532eb411377SAlan Maguire
533eb411377SAlan Maguire static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
534eb411377SAlan Maguire .func = bpf_seq_printf_btf,
535eb411377SAlan Maguire .gpl_only = true,
536eb411377SAlan Maguire .ret_type = RET_INTEGER,
537eb411377SAlan Maguire .arg1_type = ARG_PTR_TO_BTF_ID,
538eb411377SAlan Maguire .arg1_btf_id = &btf_seq_file_ids[0],
539216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
540eb411377SAlan Maguire .arg3_type = ARG_CONST_SIZE_OR_ZERO,
541eb411377SAlan Maguire .arg4_type = ARG_ANYTHING,
542d9847d31SAlexei Starovoitov };
543d9847d31SAlexei Starovoitov
544908432caSYonghong Song static __always_inline int
get_map_perf_counter(struct bpf_map * map,u64 flags,u64 * value,u64 * enabled,u64 * running)545908432caSYonghong Song get_map_perf_counter(struct bpf_map *map, u64 flags,
546908432caSYonghong Song u64 *value, u64 *enabled, u64 *running)
54735578d79SKaixu Xia {
54835578d79SKaixu Xia struct bpf_array *array = container_of(map, struct bpf_array, map);
5496816a7ffSDaniel Borkmann unsigned int cpu = smp_processor_id();
5506816a7ffSDaniel Borkmann u64 index = flags & BPF_F_INDEX_MASK;
5513b1efb19SDaniel Borkmann struct bpf_event_entry *ee;
55235578d79SKaixu Xia
5536816a7ffSDaniel Borkmann if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
5546816a7ffSDaniel Borkmann return -EINVAL;
5556816a7ffSDaniel Borkmann if (index == BPF_F_CURRENT_CPU)
5566816a7ffSDaniel Borkmann index = cpu;
55735578d79SKaixu Xia if (unlikely(index >= array->map.max_entries))
55835578d79SKaixu Xia return -E2BIG;
55935578d79SKaixu Xia
5603b1efb19SDaniel Borkmann ee = READ_ONCE(array->ptrs[index]);
5611ca1cc98SDaniel Borkmann if (!ee)
56235578d79SKaixu Xia return -ENOENT;
56335578d79SKaixu Xia
564908432caSYonghong Song return perf_event_read_local(ee->event, value, enabled, running);
565908432caSYonghong Song }
566908432caSYonghong Song
BPF_CALL_2(bpf_perf_event_read,struct bpf_map *,map,u64,flags)567908432caSYonghong Song BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
568908432caSYonghong Song {
569908432caSYonghong Song u64 value = 0;
570908432caSYonghong Song int err;
571908432caSYonghong Song
572908432caSYonghong Song err = get_map_perf_counter(map, flags, &value, NULL, NULL);
57335578d79SKaixu Xia /*
574f91840a3SAlexei Starovoitov * this api is ugly since we miss [-22..-2] range of valid
575f91840a3SAlexei Starovoitov * counter values, but that's uapi
57635578d79SKaixu Xia */
577f91840a3SAlexei Starovoitov if (err)
578f91840a3SAlexei Starovoitov return err;
579f91840a3SAlexei Starovoitov return value;
58035578d79SKaixu Xia }
58135578d79SKaixu Xia
58262544ce8SAlexei Starovoitov static const struct bpf_func_proto bpf_perf_event_read_proto = {
58335578d79SKaixu Xia .func = bpf_perf_event_read,
5841075ef59SAlexei Starovoitov .gpl_only = true,
58535578d79SKaixu Xia .ret_type = RET_INTEGER,
58635578d79SKaixu Xia .arg1_type = ARG_CONST_MAP_PTR,
58735578d79SKaixu Xia .arg2_type = ARG_ANYTHING,
58835578d79SKaixu Xia };
58935578d79SKaixu Xia
BPF_CALL_4(bpf_perf_event_read_value,struct bpf_map *,map,u64,flags,struct bpf_perf_event_value *,buf,u32,size)590908432caSYonghong Song BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
591908432caSYonghong Song struct bpf_perf_event_value *, buf, u32, size)
592908432caSYonghong Song {
593908432caSYonghong Song int err = -EINVAL;
594908432caSYonghong Song
595908432caSYonghong Song if (unlikely(size != sizeof(struct bpf_perf_event_value)))
596908432caSYonghong Song goto clear;
597908432caSYonghong Song err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
598908432caSYonghong Song &buf->running);
599908432caSYonghong Song if (unlikely(err))
600908432caSYonghong Song goto clear;
601908432caSYonghong Song return 0;
602908432caSYonghong Song clear:
603908432caSYonghong Song memset(buf, 0, size);
604908432caSYonghong Song return err;
605908432caSYonghong Song }
606908432caSYonghong Song
607908432caSYonghong Song static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
608908432caSYonghong Song .func = bpf_perf_event_read_value,
609908432caSYonghong Song .gpl_only = true,
610908432caSYonghong Song .ret_type = RET_INTEGER,
611908432caSYonghong Song .arg1_type = ARG_CONST_MAP_PTR,
612908432caSYonghong Song .arg2_type = ARG_ANYTHING,
613908432caSYonghong Song .arg3_type = ARG_PTR_TO_UNINIT_MEM,
614908432caSYonghong Song .arg4_type = ARG_CONST_SIZE,
615908432caSYonghong Song };
616908432caSYonghong Song
6178e7a3920SDaniel Borkmann static __always_inline u64
__bpf_perf_event_output(struct pt_regs * regs,struct bpf_map * map,u64 flags,struct perf_raw_record * raw,struct perf_sample_data * sd)6188e7a3920SDaniel Borkmann __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
619c0dbecb2SYabin Cui u64 flags, struct perf_raw_record *raw,
620c0dbecb2SYabin Cui struct perf_sample_data *sd)
621a43eec30SAlexei Starovoitov {
622a43eec30SAlexei Starovoitov struct bpf_array *array = container_of(map, struct bpf_array, map);
623d7931330SDaniel Borkmann unsigned int cpu = smp_processor_id();
6241e33759cSDaniel Borkmann u64 index = flags & BPF_F_INDEX_MASK;
6253b1efb19SDaniel Borkmann struct bpf_event_entry *ee;
626a43eec30SAlexei Starovoitov struct perf_event *event;
627a43eec30SAlexei Starovoitov
6281e33759cSDaniel Borkmann if (index == BPF_F_CURRENT_CPU)
629d7931330SDaniel Borkmann index = cpu;
630a43eec30SAlexei Starovoitov if (unlikely(index >= array->map.max_entries))
631a43eec30SAlexei Starovoitov return -E2BIG;
632a43eec30SAlexei Starovoitov
6333b1efb19SDaniel Borkmann ee = READ_ONCE(array->ptrs[index]);
6341ca1cc98SDaniel Borkmann if (!ee)
635a43eec30SAlexei Starovoitov return -ENOENT;
636a43eec30SAlexei Starovoitov
6373b1efb19SDaniel Borkmann event = ee->event;
638a43eec30SAlexei Starovoitov if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
639a43eec30SAlexei Starovoitov event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
640a43eec30SAlexei Starovoitov return -EINVAL;
641a43eec30SAlexei Starovoitov
642d7931330SDaniel Borkmann if (unlikely(event->oncpu != cpu))
643a43eec30SAlexei Starovoitov return -EOPNOTSUPP;
644a43eec30SAlexei Starovoitov
645c0dbecb2SYabin Cui perf_sample_save_raw_data(sd, event, raw);
646c0dbecb2SYabin Cui
64756201969SArnaldo Carvalho de Melo return perf_event_output(event, sd, regs);
648a43eec30SAlexei Starovoitov }
649a43eec30SAlexei Starovoitov
6509594dc3cSMatt Mullins /*
6519594dc3cSMatt Mullins * Support executing tracepoints in normal, irq, and nmi context that each call
6529594dc3cSMatt Mullins * bpf_perf_event_output
6539594dc3cSMatt Mullins */
6549594dc3cSMatt Mullins struct bpf_trace_sample_data {
6559594dc3cSMatt Mullins struct perf_sample_data sds[3];
6569594dc3cSMatt Mullins };
6579594dc3cSMatt Mullins
6589594dc3cSMatt Mullins static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
6599594dc3cSMatt Mullins static DEFINE_PER_CPU(int, bpf_trace_nest_level);
BPF_CALL_5(bpf_perf_event_output,struct pt_regs *,regs,struct bpf_map *,map,u64,flags,void *,data,u64,size)660f3694e00SDaniel Borkmann BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
661f3694e00SDaniel Borkmann u64, flags, void *, data, u64, size)
6628e7a3920SDaniel Borkmann {
663f2c67a3eSJiri Olsa struct bpf_trace_sample_data *sds;
6648e7a3920SDaniel Borkmann struct perf_raw_record raw = {
6658e7a3920SDaniel Borkmann .frag = {
6668e7a3920SDaniel Borkmann .size = size,
6678e7a3920SDaniel Borkmann .data = data,
6688e7a3920SDaniel Borkmann },
6698e7a3920SDaniel Borkmann };
6709594dc3cSMatt Mullins struct perf_sample_data *sd;
671f2c67a3eSJiri Olsa int nest_level, err;
672f2c67a3eSJiri Olsa
673f2c67a3eSJiri Olsa preempt_disable();
674f2c67a3eSJiri Olsa sds = this_cpu_ptr(&bpf_trace_sds);
675f2c67a3eSJiri Olsa nest_level = this_cpu_inc_return(bpf_trace_nest_level);
6768e7a3920SDaniel Borkmann
6779594dc3cSMatt Mullins if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
6789594dc3cSMatt Mullins err = -EBUSY;
6799594dc3cSMatt Mullins goto out;
6809594dc3cSMatt Mullins }
6819594dc3cSMatt Mullins
6829594dc3cSMatt Mullins sd = &sds->sds[nest_level - 1];
6839594dc3cSMatt Mullins
6849594dc3cSMatt Mullins if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
6859594dc3cSMatt Mullins err = -EINVAL;
6869594dc3cSMatt Mullins goto out;
6879594dc3cSMatt Mullins }
6888e7a3920SDaniel Borkmann
689283ca526SDaniel Borkmann perf_sample_data_init(sd, 0, 0);
690283ca526SDaniel Borkmann
691c0dbecb2SYabin Cui err = __bpf_perf_event_output(regs, map, flags, &raw, sd);
6929594dc3cSMatt Mullins out:
6939594dc3cSMatt Mullins this_cpu_dec(bpf_trace_nest_level);
694f2c67a3eSJiri Olsa preempt_enable();
6959594dc3cSMatt Mullins return err;
6968e7a3920SDaniel Borkmann }
6978e7a3920SDaniel Borkmann
698a43eec30SAlexei Starovoitov static const struct bpf_func_proto bpf_perf_event_output_proto = {
699a43eec30SAlexei Starovoitov .func = bpf_perf_event_output,
7001075ef59SAlexei Starovoitov .gpl_only = true,
701a43eec30SAlexei Starovoitov .ret_type = RET_INTEGER,
702a43eec30SAlexei Starovoitov .arg1_type = ARG_PTR_TO_CTX,
703a43eec30SAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
704a43eec30SAlexei Starovoitov .arg3_type = ARG_ANYTHING,
705216e3cd2SHao Luo .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
706a60dd35dSGianluca Borello .arg5_type = ARG_CONST_SIZE_OR_ZERO,
707a43eec30SAlexei Starovoitov };
708a43eec30SAlexei Starovoitov
709768fb61fSAllan Zhang static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
710768fb61fSAllan Zhang struct bpf_nested_pt_regs {
711768fb61fSAllan Zhang struct pt_regs regs[3];
712768fb61fSAllan Zhang };
713768fb61fSAllan Zhang static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
714768fb61fSAllan Zhang static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
715bd570ff9SDaniel Borkmann
bpf_event_output(struct bpf_map * map,u64 flags,void * meta,u64 meta_size,void * ctx,u64 ctx_size,bpf_ctx_copy_t ctx_copy)716555c8a86SDaniel Borkmann u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
717555c8a86SDaniel Borkmann void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
718bd570ff9SDaniel Borkmann {
719555c8a86SDaniel Borkmann struct perf_raw_frag frag = {
720555c8a86SDaniel Borkmann .copy = ctx_copy,
721555c8a86SDaniel Borkmann .size = ctx_size,
722555c8a86SDaniel Borkmann .data = ctx,
723555c8a86SDaniel Borkmann };
724555c8a86SDaniel Borkmann struct perf_raw_record raw = {
725555c8a86SDaniel Borkmann .frag = {
726183fc153SAndrew Morton {
727555c8a86SDaniel Borkmann .next = ctx_size ? &frag : NULL,
728183fc153SAndrew Morton },
729555c8a86SDaniel Borkmann .size = meta_size,
730555c8a86SDaniel Borkmann .data = meta,
731555c8a86SDaniel Borkmann },
732555c8a86SDaniel Borkmann };
733768fb61fSAllan Zhang struct perf_sample_data *sd;
734768fb61fSAllan Zhang struct pt_regs *regs;
735d62cc390SJiri Olsa int nest_level;
736768fb61fSAllan Zhang u64 ret;
737768fb61fSAllan Zhang
738d62cc390SJiri Olsa preempt_disable();
739d62cc390SJiri Olsa nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
740d62cc390SJiri Olsa
741768fb61fSAllan Zhang if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
742768fb61fSAllan Zhang ret = -EBUSY;
743768fb61fSAllan Zhang goto out;
744768fb61fSAllan Zhang }
745768fb61fSAllan Zhang sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
746768fb61fSAllan Zhang regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
747bd570ff9SDaniel Borkmann
748bd570ff9SDaniel Borkmann perf_fetch_caller_regs(regs);
749283ca526SDaniel Borkmann perf_sample_data_init(sd, 0, 0);
750bd570ff9SDaniel Borkmann
751c0dbecb2SYabin Cui ret = __bpf_perf_event_output(regs, map, flags, &raw, sd);
752768fb61fSAllan Zhang out:
753768fb61fSAllan Zhang this_cpu_dec(bpf_event_output_nest_level);
754d62cc390SJiri Olsa preempt_enable();
755768fb61fSAllan Zhang return ret;
756bd570ff9SDaniel Borkmann }
757bd570ff9SDaniel Borkmann
BPF_CALL_0(bpf_get_current_task)758f3694e00SDaniel Borkmann BPF_CALL_0(bpf_get_current_task)
759606274c5SAlexei Starovoitov {
760606274c5SAlexei Starovoitov return (long) current;
761606274c5SAlexei Starovoitov }
762606274c5SAlexei Starovoitov
763f470378cSJohn Fastabend const struct bpf_func_proto bpf_get_current_task_proto = {
764606274c5SAlexei Starovoitov .func = bpf_get_current_task,
765606274c5SAlexei Starovoitov .gpl_only = true,
766606274c5SAlexei Starovoitov .ret_type = RET_INTEGER,
767606274c5SAlexei Starovoitov };
768606274c5SAlexei Starovoitov
BPF_CALL_0(bpf_get_current_task_btf)7693ca1032aSKP Singh BPF_CALL_0(bpf_get_current_task_btf)
7703ca1032aSKP Singh {
7713ca1032aSKP Singh return (unsigned long) current;
7723ca1032aSKP Singh }
7733ca1032aSKP Singh
774a396eda5SDaniel Xu const struct bpf_func_proto bpf_get_current_task_btf_proto = {
7753ca1032aSKP Singh .func = bpf_get_current_task_btf,
7763ca1032aSKP Singh .gpl_only = true,
7773f00c523SDavid Vernet .ret_type = RET_PTR_TO_BTF_ID_TRUSTED,
778d19ddb47SSong Liu .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
7793ca1032aSKP Singh };
7803ca1032aSKP Singh
BPF_CALL_1(bpf_task_pt_regs,struct task_struct *,task)781dd6e10fbSDaniel Xu BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
782dd6e10fbSDaniel Xu {
783dd6e10fbSDaniel Xu return (unsigned long) task_pt_regs(task);
784dd6e10fbSDaniel Xu }
785dd6e10fbSDaniel Xu
786dd6e10fbSDaniel Xu BTF_ID_LIST(bpf_task_pt_regs_ids)
787dd6e10fbSDaniel Xu BTF_ID(struct, pt_regs)
788dd6e10fbSDaniel Xu
789dd6e10fbSDaniel Xu const struct bpf_func_proto bpf_task_pt_regs_proto = {
790dd6e10fbSDaniel Xu .func = bpf_task_pt_regs,
791dd6e10fbSDaniel Xu .gpl_only = true,
792dd6e10fbSDaniel Xu .arg1_type = ARG_PTR_TO_BTF_ID,
793d19ddb47SSong Liu .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
794dd6e10fbSDaniel Xu .ret_type = RET_PTR_TO_BTF_ID,
795dd6e10fbSDaniel Xu .ret_btf_id = &bpf_task_pt_regs_ids[0],
796dd6e10fbSDaniel Xu };
797dd6e10fbSDaniel Xu
BPF_CALL_2(bpf_current_task_under_cgroup,struct bpf_map *,map,u32,idx)798f3694e00SDaniel Borkmann BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
79960d20f91SSargun Dhillon {
80060d20f91SSargun Dhillon struct bpf_array *array = container_of(map, struct bpf_array, map);
80160d20f91SSargun Dhillon struct cgroup *cgrp;
80260d20f91SSargun Dhillon
80360d20f91SSargun Dhillon if (unlikely(idx >= array->map.max_entries))
80460d20f91SSargun Dhillon return -E2BIG;
80560d20f91SSargun Dhillon
80660d20f91SSargun Dhillon cgrp = READ_ONCE(array->ptrs[idx]);
80760d20f91SSargun Dhillon if (unlikely(!cgrp))
80860d20f91SSargun Dhillon return -EAGAIN;
80960d20f91SSargun Dhillon
81060d20f91SSargun Dhillon return task_under_cgroup_hierarchy(current, cgrp);
81160d20f91SSargun Dhillon }
81260d20f91SSargun Dhillon
81360d20f91SSargun Dhillon static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
81460d20f91SSargun Dhillon .func = bpf_current_task_under_cgroup,
81560d20f91SSargun Dhillon .gpl_only = false,
81660d20f91SSargun Dhillon .ret_type = RET_INTEGER,
81760d20f91SSargun Dhillon .arg1_type = ARG_CONST_MAP_PTR,
81860d20f91SSargun Dhillon .arg2_type = ARG_ANYTHING,
81960d20f91SSargun Dhillon };
82060d20f91SSargun Dhillon
8218b401f9eSYonghong Song struct send_signal_irq_work {
8228b401f9eSYonghong Song struct irq_work irq_work;
8238b401f9eSYonghong Song struct task_struct *task;
8248b401f9eSYonghong Song u32 sig;
8258482941fSYonghong Song enum pid_type type;
8268b401f9eSYonghong Song };
8278b401f9eSYonghong Song
8288b401f9eSYonghong Song static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
8298b401f9eSYonghong Song
do_bpf_send_signal(struct irq_work * entry)8308b401f9eSYonghong Song static void do_bpf_send_signal(struct irq_work *entry)
8318b401f9eSYonghong Song {
8328b401f9eSYonghong Song struct send_signal_irq_work *work;
8338b401f9eSYonghong Song
8348b401f9eSYonghong Song work = container_of(entry, struct send_signal_irq_work, irq_work);
8358482941fSYonghong Song group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
836bdb7fdb0SYonghong Song put_task_struct(work->task);
8378b401f9eSYonghong Song }
8388b401f9eSYonghong Song
bpf_send_signal_common(u32 sig,enum pid_type type)8398482941fSYonghong Song static int bpf_send_signal_common(u32 sig, enum pid_type type)
8408b401f9eSYonghong Song {
8418b401f9eSYonghong Song struct send_signal_irq_work *work = NULL;
8428b401f9eSYonghong Song
8438b401f9eSYonghong Song /* Similar to bpf_probe_write_user, task needs to be
8448b401f9eSYonghong Song * in a sound condition and kernel memory access be
8458b401f9eSYonghong Song * permitted in order to send signal to the current
8468b401f9eSYonghong Song * task.
8478b401f9eSYonghong Song */
8488b401f9eSYonghong Song if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
8498b401f9eSYonghong Song return -EPERM;
8508b401f9eSYonghong Song if (unlikely(!nmi_uaccess_okay()))
8518b401f9eSYonghong Song return -EPERM;
852a3d81bc1SHao Sun /* Task should not be pid=1 to avoid kernel panic. */
853a3d81bc1SHao Sun if (unlikely(is_global_init(current)))
854a3d81bc1SHao Sun return -EPERM;
8558b401f9eSYonghong Song
856*eeef8e65SPuranjay Mohan if (!preemptible()) {
857e1afb702SYonghong Song /* Do an early check on signal validity. Otherwise,
858e1afb702SYonghong Song * the error is lost in deferred irq_work.
859e1afb702SYonghong Song */
860e1afb702SYonghong Song if (unlikely(!valid_signal(sig)))
861e1afb702SYonghong Song return -EINVAL;
862e1afb702SYonghong Song
8638b401f9eSYonghong Song work = this_cpu_ptr(&send_signal_work);
8647a9f50a0SPeter Zijlstra if (irq_work_is_busy(&work->irq_work))
8658b401f9eSYonghong Song return -EBUSY;
8668b401f9eSYonghong Song
8678b401f9eSYonghong Song /* Add the current task, which is the target of sending signal,
8688b401f9eSYonghong Song * to the irq_work. The current task may change when queued
8698b401f9eSYonghong Song * irq works get executed.
8708b401f9eSYonghong Song */
871bdb7fdb0SYonghong Song work->task = get_task_struct(current);
8728b401f9eSYonghong Song work->sig = sig;
8738482941fSYonghong Song work->type = type;
8748b401f9eSYonghong Song irq_work_queue(&work->irq_work);
8758b401f9eSYonghong Song return 0;
8768b401f9eSYonghong Song }
8778b401f9eSYonghong Song
8788482941fSYonghong Song return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
8798482941fSYonghong Song }
8808482941fSYonghong Song
BPF_CALL_1(bpf_send_signal,u32,sig)8818482941fSYonghong Song BPF_CALL_1(bpf_send_signal, u32, sig)
8828482941fSYonghong Song {
8838482941fSYonghong Song return bpf_send_signal_common(sig, PIDTYPE_TGID);
8848b401f9eSYonghong Song }
8858b401f9eSYonghong Song
8868b401f9eSYonghong Song static const struct bpf_func_proto bpf_send_signal_proto = {
8878b401f9eSYonghong Song .func = bpf_send_signal,
8888b401f9eSYonghong Song .gpl_only = false,
8898b401f9eSYonghong Song .ret_type = RET_INTEGER,
8908b401f9eSYonghong Song .arg1_type = ARG_ANYTHING,
8918b401f9eSYonghong Song };
8928b401f9eSYonghong Song
BPF_CALL_1(bpf_send_signal_thread,u32,sig)8938482941fSYonghong Song BPF_CALL_1(bpf_send_signal_thread, u32, sig)
8948482941fSYonghong Song {
8958482941fSYonghong Song return bpf_send_signal_common(sig, PIDTYPE_PID);
8968482941fSYonghong Song }
8978482941fSYonghong Song
8988482941fSYonghong Song static const struct bpf_func_proto bpf_send_signal_thread_proto = {
8998482941fSYonghong Song .func = bpf_send_signal_thread,
9008482941fSYonghong Song .gpl_only = false,
9018482941fSYonghong Song .ret_type = RET_INTEGER,
9028482941fSYonghong Song .arg1_type = ARG_ANYTHING,
9038482941fSYonghong Song };
9048482941fSYonghong Song
BPF_CALL_3(bpf_d_path,struct path *,path,char *,buf,u32,sz)9056e22ab9dSJiri Olsa BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
9066e22ab9dSJiri Olsa {
907f46fab0eSJiri Olsa struct path copy;
9086e22ab9dSJiri Olsa long len;
9096e22ab9dSJiri Olsa char *p;
9106e22ab9dSJiri Olsa
9116e22ab9dSJiri Olsa if (!sz)
9126e22ab9dSJiri Olsa return 0;
9136e22ab9dSJiri Olsa
914f46fab0eSJiri Olsa /*
915f46fab0eSJiri Olsa * The path pointer is verified as trusted and safe to use,
916f46fab0eSJiri Olsa * but let's double check it's valid anyway to workaround
917f46fab0eSJiri Olsa * potentially broken verifier.
918f46fab0eSJiri Olsa */
919f46fab0eSJiri Olsa len = copy_from_kernel_nofault(©, path, sizeof(*path));
920f46fab0eSJiri Olsa if (len < 0)
921f46fab0eSJiri Olsa return len;
922f46fab0eSJiri Olsa
923f46fab0eSJiri Olsa p = d_path(©, buf, sz);
9246e22ab9dSJiri Olsa if (IS_ERR(p)) {
9256e22ab9dSJiri Olsa len = PTR_ERR(p);
9266e22ab9dSJiri Olsa } else {
9276e22ab9dSJiri Olsa len = buf + sz - p;
9286e22ab9dSJiri Olsa memmove(buf, p, len);
9296e22ab9dSJiri Olsa }
9306e22ab9dSJiri Olsa
9316e22ab9dSJiri Olsa return len;
9326e22ab9dSJiri Olsa }
9336e22ab9dSJiri Olsa
9346e22ab9dSJiri Olsa BTF_SET_START(btf_allowlist_d_path)
935a8a71796SJiri Olsa #ifdef CONFIG_SECURITY
BTF_ID(func,security_file_permission)936a8a71796SJiri Olsa BTF_ID(func, security_file_permission)
937a8a71796SJiri Olsa BTF_ID(func, security_inode_getattr)
938a8a71796SJiri Olsa BTF_ID(func, security_file_open)
939a8a71796SJiri Olsa #endif
940a8a71796SJiri Olsa #ifdef CONFIG_SECURITY_PATH
941a8a71796SJiri Olsa BTF_ID(func, security_path_truncate)
942a8a71796SJiri Olsa #endif
9436e22ab9dSJiri Olsa BTF_ID(func, vfs_truncate)
9446e22ab9dSJiri Olsa BTF_ID(func, vfs_fallocate)
9456e22ab9dSJiri Olsa BTF_ID(func, dentry_open)
9466e22ab9dSJiri Olsa BTF_ID(func, vfs_getattr)
9476e22ab9dSJiri Olsa BTF_ID(func, filp_close)
9486e22ab9dSJiri Olsa BTF_SET_END(btf_allowlist_d_path)
9496e22ab9dSJiri Olsa
9506e22ab9dSJiri Olsa static bool bpf_d_path_allowed(const struct bpf_prog *prog)
9516e22ab9dSJiri Olsa {
9523d06f34aSSong Liu if (prog->type == BPF_PROG_TYPE_TRACING &&
9533d06f34aSSong Liu prog->expected_attach_type == BPF_TRACE_ITER)
9543d06f34aSSong Liu return true;
9553d06f34aSSong Liu
9566f100640SKP Singh if (prog->type == BPF_PROG_TYPE_LSM)
9576f100640SKP Singh return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
9586f100640SKP Singh
9596f100640SKP Singh return btf_id_set_contains(&btf_allowlist_d_path,
9606f100640SKP Singh prog->aux->attach_btf_id);
9616e22ab9dSJiri Olsa }
9626e22ab9dSJiri Olsa
9639436ef6eSLorenz Bauer BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
9646e22ab9dSJiri Olsa
9656e22ab9dSJiri Olsa static const struct bpf_func_proto bpf_d_path_proto = {
9666e22ab9dSJiri Olsa .func = bpf_d_path,
9676e22ab9dSJiri Olsa .gpl_only = false,
9686e22ab9dSJiri Olsa .ret_type = RET_INTEGER,
9696e22ab9dSJiri Olsa .arg1_type = ARG_PTR_TO_BTF_ID,
9709436ef6eSLorenz Bauer .arg1_btf_id = &bpf_d_path_btf_ids[0],
9716e22ab9dSJiri Olsa .arg2_type = ARG_PTR_TO_MEM,
9726e22ab9dSJiri Olsa .arg3_type = ARG_CONST_SIZE_OR_ZERO,
9736e22ab9dSJiri Olsa .allowed = bpf_d_path_allowed,
9746e22ab9dSJiri Olsa };
9756e22ab9dSJiri Olsa
976c4d0bfb4SAlan Maguire #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
977c4d0bfb4SAlan Maguire BTF_F_PTR_RAW | BTF_F_ZERO)
978c4d0bfb4SAlan Maguire
bpf_btf_printf_prepare(struct btf_ptr * ptr,u32 btf_ptr_size,u64 flags,const struct btf ** btf,s32 * btf_id)979c4d0bfb4SAlan Maguire static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
980c4d0bfb4SAlan Maguire u64 flags, const struct btf **btf,
981c4d0bfb4SAlan Maguire s32 *btf_id)
982c4d0bfb4SAlan Maguire {
983c4d0bfb4SAlan Maguire const struct btf_type *t;
984c4d0bfb4SAlan Maguire
985c4d0bfb4SAlan Maguire if (unlikely(flags & ~(BTF_F_ALL)))
986c4d0bfb4SAlan Maguire return -EINVAL;
987c4d0bfb4SAlan Maguire
988c4d0bfb4SAlan Maguire if (btf_ptr_size != sizeof(struct btf_ptr))
989c4d0bfb4SAlan Maguire return -EINVAL;
990c4d0bfb4SAlan Maguire
991c4d0bfb4SAlan Maguire *btf = bpf_get_btf_vmlinux();
992c4d0bfb4SAlan Maguire
993c4d0bfb4SAlan Maguire if (IS_ERR_OR_NULL(*btf))
994abbaa433SWang Qing return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
995c4d0bfb4SAlan Maguire
996c4d0bfb4SAlan Maguire if (ptr->type_id > 0)
997c4d0bfb4SAlan Maguire *btf_id = ptr->type_id;
998c4d0bfb4SAlan Maguire else
999c4d0bfb4SAlan Maguire return -EINVAL;
1000c4d0bfb4SAlan Maguire
1001c4d0bfb4SAlan Maguire if (*btf_id > 0)
1002c4d0bfb4SAlan Maguire t = btf_type_by_id(*btf, *btf_id);
1003c4d0bfb4SAlan Maguire if (*btf_id <= 0 || !t)
1004c4d0bfb4SAlan Maguire return -ENOENT;
1005c4d0bfb4SAlan Maguire
1006c4d0bfb4SAlan Maguire return 0;
1007c4d0bfb4SAlan Maguire }
1008c4d0bfb4SAlan Maguire
BPF_CALL_5(bpf_snprintf_btf,char *,str,u32,str_size,struct btf_ptr *,ptr,u32,btf_ptr_size,u64,flags)1009c4d0bfb4SAlan Maguire BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1010c4d0bfb4SAlan Maguire u32, btf_ptr_size, u64, flags)
1011c4d0bfb4SAlan Maguire {
1012c4d0bfb4SAlan Maguire const struct btf *btf;
1013c4d0bfb4SAlan Maguire s32 btf_id;
1014c4d0bfb4SAlan Maguire int ret;
1015c4d0bfb4SAlan Maguire
1016c4d0bfb4SAlan Maguire ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1017c4d0bfb4SAlan Maguire if (ret)
1018c4d0bfb4SAlan Maguire return ret;
1019c4d0bfb4SAlan Maguire
1020c4d0bfb4SAlan Maguire return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1021c4d0bfb4SAlan Maguire flags);
1022c4d0bfb4SAlan Maguire }
1023c4d0bfb4SAlan Maguire
1024c4d0bfb4SAlan Maguire const struct bpf_func_proto bpf_snprintf_btf_proto = {
1025c4d0bfb4SAlan Maguire .func = bpf_snprintf_btf,
1026c4d0bfb4SAlan Maguire .gpl_only = false,
1027c4d0bfb4SAlan Maguire .ret_type = RET_INTEGER,
1028c4d0bfb4SAlan Maguire .arg1_type = ARG_PTR_TO_MEM,
1029c4d0bfb4SAlan Maguire .arg2_type = ARG_CONST_SIZE,
1030216e3cd2SHao Luo .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1031c4d0bfb4SAlan Maguire .arg4_type = ARG_CONST_SIZE,
1032c4d0bfb4SAlan Maguire .arg5_type = ARG_ANYTHING,
1033c4d0bfb4SAlan Maguire };
1034c4d0bfb4SAlan Maguire
BPF_CALL_1(bpf_get_func_ip_tracing,void *,ctx)10359b99edcaSJiri Olsa BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
10369b99edcaSJiri Olsa {
10379b99edcaSJiri Olsa /* This helper call is inlined by verifier. */
1038f92c1e18SJiri Olsa return ((u64 *)ctx)[-2];
10399b99edcaSJiri Olsa }
10409b99edcaSJiri Olsa
10419b99edcaSJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
10429b99edcaSJiri Olsa .func = bpf_get_func_ip_tracing,
10439b99edcaSJiri Olsa .gpl_only = true,
10449b99edcaSJiri Olsa .ret_type = RET_INTEGER,
10459b99edcaSJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
10469b99edcaSJiri Olsa };
10479b99edcaSJiri Olsa
1048c09eb2e5SJiri Olsa #ifdef CONFIG_X86_KERNEL_IBT
get_entry_ip(unsigned long fentry_ip)1049c09eb2e5SJiri Olsa static unsigned long get_entry_ip(unsigned long fentry_ip)
1050c09eb2e5SJiri Olsa {
1051c09eb2e5SJiri Olsa u32 instr;
1052c09eb2e5SJiri Olsa
1053c09eb2e5SJiri Olsa /* Being extra safe in here in case entry ip is on the page-edge. */
1054c09eb2e5SJiri Olsa if (get_kernel_nofault(instr, (u32 *) fentry_ip - 1))
1055c09eb2e5SJiri Olsa return fentry_ip;
1056c09eb2e5SJiri Olsa if (is_endbr(instr))
1057c09eb2e5SJiri Olsa fentry_ip -= ENDBR_INSN_SIZE;
1058c09eb2e5SJiri Olsa return fentry_ip;
1059c09eb2e5SJiri Olsa }
1060c09eb2e5SJiri Olsa #else
1061c09eb2e5SJiri Olsa #define get_entry_ip(fentry_ip) fentry_ip
1062c09eb2e5SJiri Olsa #endif
1063c09eb2e5SJiri Olsa
BPF_CALL_1(bpf_get_func_ip_kprobe,struct pt_regs *,regs)10649ffd9f3fSJiri Olsa BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
10659ffd9f3fSJiri Olsa {
1066a3c485a5SJiri Olsa struct bpf_trace_run_ctx *run_ctx __maybe_unused;
1067a3c485a5SJiri Olsa struct kprobe *kp;
1068a3c485a5SJiri Olsa
1069a3c485a5SJiri Olsa #ifdef CONFIG_UPROBES
1070a3c485a5SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1071a3c485a5SJiri Olsa if (run_ctx->is_uprobe)
1072a3c485a5SJiri Olsa return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr;
1073a3c485a5SJiri Olsa #endif
1074a3c485a5SJiri Olsa
1075a3c485a5SJiri Olsa kp = kprobe_running();
10769ffd9f3fSJiri Olsa
10770e253f7eSJiri Olsa if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
10780e253f7eSJiri Olsa return 0;
10790e253f7eSJiri Olsa
10800e253f7eSJiri Olsa return get_entry_ip((uintptr_t)kp->addr);
10819ffd9f3fSJiri Olsa }
10829ffd9f3fSJiri Olsa
10839ffd9f3fSJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
10849ffd9f3fSJiri Olsa .func = bpf_get_func_ip_kprobe,
10859ffd9f3fSJiri Olsa .gpl_only = true,
10869ffd9f3fSJiri Olsa .ret_type = RET_INTEGER,
10879ffd9f3fSJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
10889ffd9f3fSJiri Olsa };
10899ffd9f3fSJiri Olsa
BPF_CALL_1(bpf_get_func_ip_kprobe_multi,struct pt_regs *,regs)109042a57120SJiri Olsa BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
109142a57120SJiri Olsa {
1092f7098690SJiri Olsa return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
109342a57120SJiri Olsa }
109442a57120SJiri Olsa
109542a57120SJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
109642a57120SJiri Olsa .func = bpf_get_func_ip_kprobe_multi,
109742a57120SJiri Olsa .gpl_only = false,
109842a57120SJiri Olsa .ret_type = RET_INTEGER,
109942a57120SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
110042a57120SJiri Olsa };
110142a57120SJiri Olsa
BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi,struct pt_regs *,regs)1102ca74823cSJiri Olsa BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1103ca74823cSJiri Olsa {
1104f7098690SJiri Olsa return bpf_kprobe_multi_cookie(current->bpf_ctx);
1105ca74823cSJiri Olsa }
1106ca74823cSJiri Olsa
1107ca74823cSJiri Olsa static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1108ca74823cSJiri Olsa .func = bpf_get_attach_cookie_kprobe_multi,
1109ca74823cSJiri Olsa .gpl_only = false,
1110ca74823cSJiri Olsa .ret_type = RET_INTEGER,
1111ca74823cSJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1112ca74823cSJiri Olsa };
1113ca74823cSJiri Olsa
BPF_CALL_1(bpf_get_func_ip_uprobe_multi,struct pt_regs *,regs)1114686328d8SJiri Olsa BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs)
1115686328d8SJiri Olsa {
1116686328d8SJiri Olsa return bpf_uprobe_multi_entry_ip(current->bpf_ctx);
1117686328d8SJiri Olsa }
1118686328d8SJiri Olsa
1119686328d8SJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = {
1120686328d8SJiri Olsa .func = bpf_get_func_ip_uprobe_multi,
1121686328d8SJiri Olsa .gpl_only = false,
1122686328d8SJiri Olsa .ret_type = RET_INTEGER,
1123686328d8SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1124686328d8SJiri Olsa };
1125686328d8SJiri Olsa
BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi,struct pt_regs *,regs)11260b779b61SJiri Olsa BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs)
11270b779b61SJiri Olsa {
11280b779b61SJiri Olsa return bpf_uprobe_multi_cookie(current->bpf_ctx);
11290b779b61SJiri Olsa }
11300b779b61SJiri Olsa
11310b779b61SJiri Olsa static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = {
11320b779b61SJiri Olsa .func = bpf_get_attach_cookie_uprobe_multi,
11330b779b61SJiri Olsa .gpl_only = false,
11340b779b61SJiri Olsa .ret_type = RET_INTEGER,
11350b779b61SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
11360b779b61SJiri Olsa };
11370b779b61SJiri Olsa
BPF_CALL_1(bpf_get_attach_cookie_trace,void *,ctx)11387adfc6c9SAndrii Nakryiko BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
11397adfc6c9SAndrii Nakryiko {
11407adfc6c9SAndrii Nakryiko struct bpf_trace_run_ctx *run_ctx;
11417adfc6c9SAndrii Nakryiko
11427adfc6c9SAndrii Nakryiko run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
11437adfc6c9SAndrii Nakryiko return run_ctx->bpf_cookie;
11447adfc6c9SAndrii Nakryiko }
11457adfc6c9SAndrii Nakryiko
11467adfc6c9SAndrii Nakryiko static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
11477adfc6c9SAndrii Nakryiko .func = bpf_get_attach_cookie_trace,
11487adfc6c9SAndrii Nakryiko .gpl_only = false,
11497adfc6c9SAndrii Nakryiko .ret_type = RET_INTEGER,
11507adfc6c9SAndrii Nakryiko .arg1_type = ARG_PTR_TO_CTX,
11517adfc6c9SAndrii Nakryiko };
11527adfc6c9SAndrii Nakryiko
BPF_CALL_1(bpf_get_attach_cookie_pe,struct bpf_perf_event_data_kern *,ctx)11537adfc6c9SAndrii Nakryiko BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
11547adfc6c9SAndrii Nakryiko {
11557adfc6c9SAndrii Nakryiko return ctx->event->bpf_cookie;
11567adfc6c9SAndrii Nakryiko }
11577adfc6c9SAndrii Nakryiko
11587adfc6c9SAndrii Nakryiko static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
11597adfc6c9SAndrii Nakryiko .func = bpf_get_attach_cookie_pe,
11607adfc6c9SAndrii Nakryiko .gpl_only = false,
11617adfc6c9SAndrii Nakryiko .ret_type = RET_INTEGER,
11627adfc6c9SAndrii Nakryiko .arg1_type = ARG_PTR_TO_CTX,
11637adfc6c9SAndrii Nakryiko };
11647adfc6c9SAndrii Nakryiko
BPF_CALL_1(bpf_get_attach_cookie_tracing,void *,ctx)11652fcc8241SKui-Feng Lee BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
11662fcc8241SKui-Feng Lee {
11672fcc8241SKui-Feng Lee struct bpf_trace_run_ctx *run_ctx;
11682fcc8241SKui-Feng Lee
11692fcc8241SKui-Feng Lee run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
11702fcc8241SKui-Feng Lee return run_ctx->bpf_cookie;
11712fcc8241SKui-Feng Lee }
11722fcc8241SKui-Feng Lee
11732fcc8241SKui-Feng Lee static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
11742fcc8241SKui-Feng Lee .func = bpf_get_attach_cookie_tracing,
11752fcc8241SKui-Feng Lee .gpl_only = false,
11762fcc8241SKui-Feng Lee .ret_type = RET_INTEGER,
11772fcc8241SKui-Feng Lee .arg1_type = ARG_PTR_TO_CTX,
11782fcc8241SKui-Feng Lee };
11792fcc8241SKui-Feng Lee
BPF_CALL_3(bpf_get_branch_snapshot,void *,buf,u32,size,u64,flags)1180856c02dbSSong Liu BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1181856c02dbSSong Liu {
1182856c02dbSSong Liu #ifndef CONFIG_X86
1183856c02dbSSong Liu return -ENOENT;
1184856c02dbSSong Liu #else
1185856c02dbSSong Liu static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1186856c02dbSSong Liu u32 entry_cnt = size / br_entry_size;
1187856c02dbSSong Liu
1188856c02dbSSong Liu entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1189856c02dbSSong Liu
1190856c02dbSSong Liu if (unlikely(flags))
1191856c02dbSSong Liu return -EINVAL;
1192856c02dbSSong Liu
1193856c02dbSSong Liu if (!entry_cnt)
1194856c02dbSSong Liu return -ENOENT;
1195856c02dbSSong Liu
1196856c02dbSSong Liu return entry_cnt * br_entry_size;
1197856c02dbSSong Liu #endif
1198856c02dbSSong Liu }
1199856c02dbSSong Liu
1200856c02dbSSong Liu static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1201856c02dbSSong Liu .func = bpf_get_branch_snapshot,
1202856c02dbSSong Liu .gpl_only = true,
1203856c02dbSSong Liu .ret_type = RET_INTEGER,
1204856c02dbSSong Liu .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1205856c02dbSSong Liu .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1206856c02dbSSong Liu };
1207856c02dbSSong Liu
BPF_CALL_3(get_func_arg,void *,ctx,u32,n,u64 *,value)1208f92c1e18SJiri Olsa BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1209f92c1e18SJiri Olsa {
1210f92c1e18SJiri Olsa /* This helper call is inlined by verifier. */
1211f92c1e18SJiri Olsa u64 nr_args = ((u64 *)ctx)[-1];
1212f92c1e18SJiri Olsa
1213f92c1e18SJiri Olsa if ((u64) n >= nr_args)
1214f92c1e18SJiri Olsa return -EINVAL;
1215f92c1e18SJiri Olsa *value = ((u64 *)ctx)[n];
1216f92c1e18SJiri Olsa return 0;
1217f92c1e18SJiri Olsa }
1218f92c1e18SJiri Olsa
1219f92c1e18SJiri Olsa static const struct bpf_func_proto bpf_get_func_arg_proto = {
1220f92c1e18SJiri Olsa .func = get_func_arg,
1221f92c1e18SJiri Olsa .ret_type = RET_INTEGER,
1222f92c1e18SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1223f92c1e18SJiri Olsa .arg2_type = ARG_ANYTHING,
12248a33a047SDaniel Borkmann .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
1225a2c8dc7eSDaniel Borkmann .arg3_size = sizeof(u64),
1226f92c1e18SJiri Olsa };
1227f92c1e18SJiri Olsa
BPF_CALL_2(get_func_ret,void *,ctx,u64 *,value)1228f92c1e18SJiri Olsa BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1229f92c1e18SJiri Olsa {
1230f92c1e18SJiri Olsa /* This helper call is inlined by verifier. */
1231f92c1e18SJiri Olsa u64 nr_args = ((u64 *)ctx)[-1];
1232f92c1e18SJiri Olsa
1233f92c1e18SJiri Olsa *value = ((u64 *)ctx)[nr_args];
1234f92c1e18SJiri Olsa return 0;
1235f92c1e18SJiri Olsa }
1236f92c1e18SJiri Olsa
1237f92c1e18SJiri Olsa static const struct bpf_func_proto bpf_get_func_ret_proto = {
1238f92c1e18SJiri Olsa .func = get_func_ret,
1239f92c1e18SJiri Olsa .ret_type = RET_INTEGER,
1240f92c1e18SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
12418a33a047SDaniel Borkmann .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
1242a2c8dc7eSDaniel Borkmann .arg2_size = sizeof(u64),
1243f92c1e18SJiri Olsa };
1244f92c1e18SJiri Olsa
BPF_CALL_1(get_func_arg_cnt,void *,ctx)1245f92c1e18SJiri Olsa BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1246f92c1e18SJiri Olsa {
1247f92c1e18SJiri Olsa /* This helper call is inlined by verifier. */
1248f92c1e18SJiri Olsa return ((u64 *)ctx)[-1];
1249f92c1e18SJiri Olsa }
1250f92c1e18SJiri Olsa
1251f92c1e18SJiri Olsa static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1252f92c1e18SJiri Olsa .func = get_func_arg_cnt,
1253f92c1e18SJiri Olsa .ret_type = RET_INTEGER,
1254f92c1e18SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1255f92c1e18SJiri Olsa };
1256f92c1e18SJiri Olsa
1257f3cf4134SRoberto Sassu #ifdef CONFIG_KEYS
1258f3cf4134SRoberto Sassu __diag_push();
1259f3cf4134SRoberto Sassu __diag_ignore_all("-Wmissing-prototypes",
1260f3cf4134SRoberto Sassu "kfuncs which will be used in BPF programs");
1261f3cf4134SRoberto Sassu
1262f3cf4134SRoberto Sassu /**
1263f3cf4134SRoberto Sassu * bpf_lookup_user_key - lookup a key by its serial
1264f3cf4134SRoberto Sassu * @serial: key handle serial number
1265f3cf4134SRoberto Sassu * @flags: lookup-specific flags
1266f3cf4134SRoberto Sassu *
1267f3cf4134SRoberto Sassu * Search a key with a given *serial* and the provided *flags*.
1268f3cf4134SRoberto Sassu * If found, increment the reference count of the key by one, and
1269f3cf4134SRoberto Sassu * return it in the bpf_key structure.
1270f3cf4134SRoberto Sassu *
1271f3cf4134SRoberto Sassu * The bpf_key structure must be passed to bpf_key_put() when done
1272f3cf4134SRoberto Sassu * with it, so that the key reference count is decremented and the
1273f3cf4134SRoberto Sassu * bpf_key structure is freed.
1274f3cf4134SRoberto Sassu *
1275f3cf4134SRoberto Sassu * Permission checks are deferred to the time the key is used by
1276f3cf4134SRoberto Sassu * one of the available key-specific kfuncs.
1277f3cf4134SRoberto Sassu *
1278f3cf4134SRoberto Sassu * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
1279f3cf4134SRoberto Sassu * special keyring (e.g. session keyring), if it doesn't yet exist.
1280f3cf4134SRoberto Sassu * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
1281f3cf4134SRoberto Sassu * for the key construction, and to retrieve uninstantiated keys (keys
1282f3cf4134SRoberto Sassu * without data attached to them).
1283f3cf4134SRoberto Sassu *
1284f3cf4134SRoberto Sassu * Return: a bpf_key pointer with a valid key pointer if the key is found, a
1285f3cf4134SRoberto Sassu * NULL pointer otherwise.
1286f3cf4134SRoberto Sassu */
bpf_lookup_user_key(u32 serial,u64 flags)1287400031e0SDavid Vernet __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
1288f3cf4134SRoberto Sassu {
1289f3cf4134SRoberto Sassu key_ref_t key_ref;
1290f3cf4134SRoberto Sassu struct bpf_key *bkey;
1291f3cf4134SRoberto Sassu
1292f3cf4134SRoberto Sassu if (flags & ~KEY_LOOKUP_ALL)
1293f3cf4134SRoberto Sassu return NULL;
1294f3cf4134SRoberto Sassu
1295f3cf4134SRoberto Sassu /*
1296f3cf4134SRoberto Sassu * Permission check is deferred until the key is used, as the
1297f3cf4134SRoberto Sassu * intent of the caller is unknown here.
1298f3cf4134SRoberto Sassu */
1299f3cf4134SRoberto Sassu key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
1300f3cf4134SRoberto Sassu if (IS_ERR(key_ref))
1301f3cf4134SRoberto Sassu return NULL;
1302f3cf4134SRoberto Sassu
1303f3cf4134SRoberto Sassu bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
1304f3cf4134SRoberto Sassu if (!bkey) {
1305f3cf4134SRoberto Sassu key_put(key_ref_to_ptr(key_ref));
1306f3cf4134SRoberto Sassu return NULL;
1307f3cf4134SRoberto Sassu }
1308f3cf4134SRoberto Sassu
1309f3cf4134SRoberto Sassu bkey->key = key_ref_to_ptr(key_ref);
1310f3cf4134SRoberto Sassu bkey->has_ref = true;
1311f3cf4134SRoberto Sassu
1312f3cf4134SRoberto Sassu return bkey;
1313f3cf4134SRoberto Sassu }
1314f3cf4134SRoberto Sassu
1315f3cf4134SRoberto Sassu /**
1316f3cf4134SRoberto Sassu * bpf_lookup_system_key - lookup a key by a system-defined ID
1317f3cf4134SRoberto Sassu * @id: key ID
1318f3cf4134SRoberto Sassu *
1319f3cf4134SRoberto Sassu * Obtain a bpf_key structure with a key pointer set to the passed key ID.
1320f3cf4134SRoberto Sassu * The key pointer is marked as invalid, to prevent bpf_key_put() from
1321f3cf4134SRoberto Sassu * attempting to decrement the key reference count on that pointer. The key
1322f3cf4134SRoberto Sassu * pointer set in such way is currently understood only by
1323f3cf4134SRoberto Sassu * verify_pkcs7_signature().
1324f3cf4134SRoberto Sassu *
1325f3cf4134SRoberto Sassu * Set *id* to one of the values defined in include/linux/verification.h:
1326f3cf4134SRoberto Sassu * 0 for the primary keyring (immutable keyring of system keys);
1327f3cf4134SRoberto Sassu * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
1328f3cf4134SRoberto Sassu * (where keys can be added only if they are vouched for by existing keys
1329f3cf4134SRoberto Sassu * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
1330f3cf4134SRoberto Sassu * keyring (primarily used by the integrity subsystem to verify a kexec'ed
1331f3cf4134SRoberto Sassu * kerned image and, possibly, the initramfs signature).
1332f3cf4134SRoberto Sassu *
1333f3cf4134SRoberto Sassu * Return: a bpf_key pointer with an invalid key pointer set from the
1334f3cf4134SRoberto Sassu * pre-determined ID on success, a NULL pointer otherwise
1335f3cf4134SRoberto Sassu */
bpf_lookup_system_key(u64 id)1336400031e0SDavid Vernet __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id)
1337f3cf4134SRoberto Sassu {
1338f3cf4134SRoberto Sassu struct bpf_key *bkey;
1339f3cf4134SRoberto Sassu
1340f3cf4134SRoberto Sassu if (system_keyring_id_check(id) < 0)
1341f3cf4134SRoberto Sassu return NULL;
1342f3cf4134SRoberto Sassu
1343f3cf4134SRoberto Sassu bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
1344f3cf4134SRoberto Sassu if (!bkey)
1345f3cf4134SRoberto Sassu return NULL;
1346f3cf4134SRoberto Sassu
1347f3cf4134SRoberto Sassu bkey->key = (struct key *)(unsigned long)id;
1348f3cf4134SRoberto Sassu bkey->has_ref = false;
1349f3cf4134SRoberto Sassu
1350f3cf4134SRoberto Sassu return bkey;
1351f3cf4134SRoberto Sassu }
1352f3cf4134SRoberto Sassu
1353f3cf4134SRoberto Sassu /**
1354f3cf4134SRoberto Sassu * bpf_key_put - decrement key reference count if key is valid and free bpf_key
1355f3cf4134SRoberto Sassu * @bkey: bpf_key structure
1356f3cf4134SRoberto Sassu *
1357f3cf4134SRoberto Sassu * Decrement the reference count of the key inside *bkey*, if the pointer
1358f3cf4134SRoberto Sassu * is valid, and free *bkey*.
1359f3cf4134SRoberto Sassu */
bpf_key_put(struct bpf_key * bkey)1360400031e0SDavid Vernet __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
1361f3cf4134SRoberto Sassu {
1362f3cf4134SRoberto Sassu if (bkey->has_ref)
1363f3cf4134SRoberto Sassu key_put(bkey->key);
1364f3cf4134SRoberto Sassu
1365f3cf4134SRoberto Sassu kfree(bkey);
1366f3cf4134SRoberto Sassu }
1367f3cf4134SRoberto Sassu
1368865b0566SRoberto Sassu #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1369865b0566SRoberto Sassu /**
1370865b0566SRoberto Sassu * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
1371865b0566SRoberto Sassu * @data_ptr: data to verify
1372865b0566SRoberto Sassu * @sig_ptr: signature of the data
1373865b0566SRoberto Sassu * @trusted_keyring: keyring with keys trusted for signature verification
1374865b0566SRoberto Sassu *
1375865b0566SRoberto Sassu * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
1376865b0566SRoberto Sassu * with keys in a keyring referenced by *trusted_keyring*.
1377865b0566SRoberto Sassu *
1378865b0566SRoberto Sassu * Return: 0 on success, a negative value on error.
1379865b0566SRoberto Sassu */
bpf_verify_pkcs7_signature(struct bpf_dynptr_kern * data_ptr,struct bpf_dynptr_kern * sig_ptr,struct bpf_key * trusted_keyring)1380400031e0SDavid Vernet __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
1381865b0566SRoberto Sassu struct bpf_dynptr_kern *sig_ptr,
1382865b0566SRoberto Sassu struct bpf_key *trusted_keyring)
1383865b0566SRoberto Sassu {
1384865b0566SRoberto Sassu int ret;
1385865b0566SRoberto Sassu
1386865b0566SRoberto Sassu if (trusted_keyring->has_ref) {
1387865b0566SRoberto Sassu /*
1388865b0566SRoberto Sassu * Do the permission check deferred in bpf_lookup_user_key().
1389865b0566SRoberto Sassu * See bpf_lookup_user_key() for more details.
1390865b0566SRoberto Sassu *
1391865b0566SRoberto Sassu * A call to key_task_permission() here would be redundant, as
1392865b0566SRoberto Sassu * it is already done by keyring_search() called by
1393865b0566SRoberto Sassu * find_asymmetric_key().
1394865b0566SRoberto Sassu */
1395865b0566SRoberto Sassu ret = key_validate(trusted_keyring->key);
1396865b0566SRoberto Sassu if (ret < 0)
1397865b0566SRoberto Sassu return ret;
1398865b0566SRoberto Sassu }
1399865b0566SRoberto Sassu
1400865b0566SRoberto Sassu return verify_pkcs7_signature(data_ptr->data,
140126662d73SJoanne Koong __bpf_dynptr_size(data_ptr),
1402865b0566SRoberto Sassu sig_ptr->data,
140326662d73SJoanne Koong __bpf_dynptr_size(sig_ptr),
1404865b0566SRoberto Sassu trusted_keyring->key,
1405865b0566SRoberto Sassu VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
1406865b0566SRoberto Sassu NULL);
1407865b0566SRoberto Sassu }
1408865b0566SRoberto Sassu #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
1409865b0566SRoberto Sassu
1410f3cf4134SRoberto Sassu __diag_pop();
1411f3cf4134SRoberto Sassu
1412f3cf4134SRoberto Sassu BTF_SET8_START(key_sig_kfunc_set)
1413f3cf4134SRoberto Sassu BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
1414f3cf4134SRoberto Sassu BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
1415f3cf4134SRoberto Sassu BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
1416865b0566SRoberto Sassu #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1417865b0566SRoberto Sassu BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
1418865b0566SRoberto Sassu #endif
1419f3cf4134SRoberto Sassu BTF_SET8_END(key_sig_kfunc_set)
1420f3cf4134SRoberto Sassu
1421f3cf4134SRoberto Sassu static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
1422f3cf4134SRoberto Sassu .owner = THIS_MODULE,
1423f3cf4134SRoberto Sassu .set = &key_sig_kfunc_set,
1424f3cf4134SRoberto Sassu };
1425f3cf4134SRoberto Sassu
bpf_key_sig_kfuncs_init(void)1426f3cf4134SRoberto Sassu static int __init bpf_key_sig_kfuncs_init(void)
1427f3cf4134SRoberto Sassu {
1428f3cf4134SRoberto Sassu return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
1429f3cf4134SRoberto Sassu &bpf_key_sig_kfunc_set);
1430f3cf4134SRoberto Sassu }
1431f3cf4134SRoberto Sassu
1432f3cf4134SRoberto Sassu late_initcall(bpf_key_sig_kfuncs_init);
1433f3cf4134SRoberto Sassu #endif /* CONFIG_KEYS */
1434f3cf4134SRoberto Sassu
14357adfc6c9SAndrii Nakryiko static const struct bpf_func_proto *
bpf_tracing_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1436fc611f47SKP Singh bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14372541517cSAlexei Starovoitov {
14382541517cSAlexei Starovoitov switch (func_id) {
14392541517cSAlexei Starovoitov case BPF_FUNC_map_lookup_elem:
14402541517cSAlexei Starovoitov return &bpf_map_lookup_elem_proto;
14412541517cSAlexei Starovoitov case BPF_FUNC_map_update_elem:
14422541517cSAlexei Starovoitov return &bpf_map_update_elem_proto;
14432541517cSAlexei Starovoitov case BPF_FUNC_map_delete_elem:
14442541517cSAlexei Starovoitov return &bpf_map_delete_elem_proto;
144502a8c817SAlban Crequy case BPF_FUNC_map_push_elem:
144602a8c817SAlban Crequy return &bpf_map_push_elem_proto;
144702a8c817SAlban Crequy case BPF_FUNC_map_pop_elem:
144802a8c817SAlban Crequy return &bpf_map_pop_elem_proto;
144902a8c817SAlban Crequy case BPF_FUNC_map_peek_elem:
145002a8c817SAlban Crequy return &bpf_map_peek_elem_proto;
145107343110SFeng Zhou case BPF_FUNC_map_lookup_percpu_elem:
145207343110SFeng Zhou return &bpf_map_lookup_percpu_elem_proto;
1453d9847d31SAlexei Starovoitov case BPF_FUNC_ktime_get_ns:
1454d9847d31SAlexei Starovoitov return &bpf_ktime_get_ns_proto;
145571d19214SMaciej Żenczykowski case BPF_FUNC_ktime_get_boot_ns:
145671d19214SMaciej Żenczykowski return &bpf_ktime_get_boot_ns_proto;
145704fd61abSAlexei Starovoitov case BPF_FUNC_tail_call:
145804fd61abSAlexei Starovoitov return &bpf_tail_call_proto;
1459ffeedafbSAlexei Starovoitov case BPF_FUNC_get_current_pid_tgid:
1460ffeedafbSAlexei Starovoitov return &bpf_get_current_pid_tgid_proto;
1461606274c5SAlexei Starovoitov case BPF_FUNC_get_current_task:
1462606274c5SAlexei Starovoitov return &bpf_get_current_task_proto;
14633ca1032aSKP Singh case BPF_FUNC_get_current_task_btf:
14643ca1032aSKP Singh return &bpf_get_current_task_btf_proto;
1465dd6e10fbSDaniel Xu case BPF_FUNC_task_pt_regs:
1466dd6e10fbSDaniel Xu return &bpf_task_pt_regs_proto;
1467ffeedafbSAlexei Starovoitov case BPF_FUNC_get_current_uid_gid:
1468ffeedafbSAlexei Starovoitov return &bpf_get_current_uid_gid_proto;
1469ffeedafbSAlexei Starovoitov case BPF_FUNC_get_current_comm:
1470ffeedafbSAlexei Starovoitov return &bpf_get_current_comm_proto;
14719c959c86SAlexei Starovoitov case BPF_FUNC_trace_printk:
14720756ea3eSAlexei Starovoitov return bpf_get_trace_printk_proto();
1473ab1973d3SAlexei Starovoitov case BPF_FUNC_get_smp_processor_id:
1474ab1973d3SAlexei Starovoitov return &bpf_get_smp_processor_id_proto;
14752d0e30c3SDaniel Borkmann case BPF_FUNC_get_numa_node_id:
14762d0e30c3SDaniel Borkmann return &bpf_get_numa_node_id_proto;
147735578d79SKaixu Xia case BPF_FUNC_perf_event_read:
147835578d79SKaixu Xia return &bpf_perf_event_read_proto;
147960d20f91SSargun Dhillon case BPF_FUNC_current_task_under_cgroup:
148060d20f91SSargun Dhillon return &bpf_current_task_under_cgroup_proto;
14818937bd80SAlexei Starovoitov case BPF_FUNC_get_prandom_u32:
14828937bd80SAlexei Starovoitov return &bpf_get_prandom_u32_proto;
148351e1bb9eSDaniel Borkmann case BPF_FUNC_probe_write_user:
148451e1bb9eSDaniel Borkmann return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
148551e1bb9eSDaniel Borkmann NULL : bpf_get_probe_write_proto();
14866ae08ae3SDaniel Borkmann case BPF_FUNC_probe_read_user:
14876ae08ae3SDaniel Borkmann return &bpf_probe_read_user_proto;
14886ae08ae3SDaniel Borkmann case BPF_FUNC_probe_read_kernel:
148971330842SDaniel Borkmann return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1490ff40e510SDaniel Borkmann NULL : &bpf_probe_read_kernel_proto;
14916ae08ae3SDaniel Borkmann case BPF_FUNC_probe_read_user_str:
14926ae08ae3SDaniel Borkmann return &bpf_probe_read_user_str_proto;
14936ae08ae3SDaniel Borkmann case BPF_FUNC_probe_read_kernel_str:
149471330842SDaniel Borkmann return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1495ff40e510SDaniel Borkmann NULL : &bpf_probe_read_kernel_str_proto;
14960ebeea8cSDaniel Borkmann #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
14970ebeea8cSDaniel Borkmann case BPF_FUNC_probe_read:
149871330842SDaniel Borkmann return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1499ff40e510SDaniel Borkmann NULL : &bpf_probe_read_compat_proto;
1500a5e8c070SGianluca Borello case BPF_FUNC_probe_read_str:
150171330842SDaniel Borkmann return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1502ff40e510SDaniel Borkmann NULL : &bpf_probe_read_compat_str_proto;
15030ebeea8cSDaniel Borkmann #endif
150434ea38caSYonghong Song #ifdef CONFIG_CGROUPS
1505c4bcfb38SYonghong Song case BPF_FUNC_cgrp_storage_get:
1506c4bcfb38SYonghong Song return &bpf_cgrp_storage_get_proto;
1507c4bcfb38SYonghong Song case BPF_FUNC_cgrp_storage_delete:
1508c4bcfb38SYonghong Song return &bpf_cgrp_storage_delete_proto;
150934ea38caSYonghong Song #endif
15108b401f9eSYonghong Song case BPF_FUNC_send_signal:
15118b401f9eSYonghong Song return &bpf_send_signal_proto;
15128482941fSYonghong Song case BPF_FUNC_send_signal_thread:
15138482941fSYonghong Song return &bpf_send_signal_thread_proto;
1514b80b033bSSong Liu case BPF_FUNC_perf_event_read_value:
1515b80b033bSSong Liu return &bpf_perf_event_read_value_proto;
1516b4490c5cSCarlos Neira case BPF_FUNC_get_ns_current_pid_tgid:
1517b4490c5cSCarlos Neira return &bpf_get_ns_current_pid_tgid_proto;
1518457f4436SAndrii Nakryiko case BPF_FUNC_ringbuf_output:
1519457f4436SAndrii Nakryiko return &bpf_ringbuf_output_proto;
1520457f4436SAndrii Nakryiko case BPF_FUNC_ringbuf_reserve:
1521457f4436SAndrii Nakryiko return &bpf_ringbuf_reserve_proto;
1522457f4436SAndrii Nakryiko case BPF_FUNC_ringbuf_submit:
1523457f4436SAndrii Nakryiko return &bpf_ringbuf_submit_proto;
1524457f4436SAndrii Nakryiko case BPF_FUNC_ringbuf_discard:
1525457f4436SAndrii Nakryiko return &bpf_ringbuf_discard_proto;
1526457f4436SAndrii Nakryiko case BPF_FUNC_ringbuf_query:
1527457f4436SAndrii Nakryiko return &bpf_ringbuf_query_proto;
152872e2b2b6SYonghong Song case BPF_FUNC_jiffies64:
152972e2b2b6SYonghong Song return &bpf_jiffies64_proto;
1530fa28dcb8SSong Liu case BPF_FUNC_get_task_stack:
1531fa28dcb8SSong Liu return &bpf_get_task_stack_proto;
153207be4c4aSAlexei Starovoitov case BPF_FUNC_copy_from_user:
153301685c5bSYonghong Song return &bpf_copy_from_user_proto;
1534376040e4SKenny Yu case BPF_FUNC_copy_from_user_task:
153501685c5bSYonghong Song return &bpf_copy_from_user_task_proto;
1536c4d0bfb4SAlan Maguire case BPF_FUNC_snprintf_btf:
1537c4d0bfb4SAlan Maguire return &bpf_snprintf_btf_proto;
1538b7906b70SAndrii Nakryiko case BPF_FUNC_per_cpu_ptr:
1539eaa6bcb7SHao Luo return &bpf_per_cpu_ptr_proto;
1540b7906b70SAndrii Nakryiko case BPF_FUNC_this_cpu_ptr:
154163d9b80dSHao Luo return &bpf_this_cpu_ptr_proto;
1542a10787e6SSong Liu case BPF_FUNC_task_storage_get:
15434279adb0SMartin KaFai Lau if (bpf_prog_check_recur(prog))
15440593dd34SMartin KaFai Lau return &bpf_task_storage_get_recur_proto;
1545a10787e6SSong Liu return &bpf_task_storage_get_proto;
1546a10787e6SSong Liu case BPF_FUNC_task_storage_delete:
15478a7dac37SMartin KaFai Lau if (bpf_prog_check_recur(prog))
15480593dd34SMartin KaFai Lau return &bpf_task_storage_delete_recur_proto;
1549a10787e6SSong Liu return &bpf_task_storage_delete_proto;
155069c087baSYonghong Song case BPF_FUNC_for_each_map_elem:
155169c087baSYonghong Song return &bpf_for_each_map_elem_proto;
15527b15523aSFlorent Revest case BPF_FUNC_snprintf:
15537b15523aSFlorent Revest return &bpf_snprintf_proto;
15549b99edcaSJiri Olsa case BPF_FUNC_get_func_ip:
15559b99edcaSJiri Olsa return &bpf_get_func_ip_proto_tracing;
1556856c02dbSSong Liu case BPF_FUNC_get_branch_snapshot:
1557856c02dbSSong Liu return &bpf_get_branch_snapshot_proto;
15587c7e3d31SSong Liu case BPF_FUNC_find_vma:
15597c7e3d31SSong Liu return &bpf_find_vma_proto;
156010aceb62SDave Marchevsky case BPF_FUNC_trace_vprintk:
156110aceb62SDave Marchevsky return bpf_get_trace_vprintk_proto();
15629fd82b61SAlexei Starovoitov default:
1563b00628b1SAlexei Starovoitov return bpf_base_func_proto(func_id);
15649fd82b61SAlexei Starovoitov }
15659fd82b61SAlexei Starovoitov }
15669fd82b61SAlexei Starovoitov
15675e43f899SAndrey Ignatov static const struct bpf_func_proto *
kprobe_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)15685e43f899SAndrey Ignatov kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15699fd82b61SAlexei Starovoitov {
15709fd82b61SAlexei Starovoitov switch (func_id) {
1571a43eec30SAlexei Starovoitov case BPF_FUNC_perf_event_output:
1572a43eec30SAlexei Starovoitov return &bpf_perf_event_output_proto;
1573d5a3b1f6SAlexei Starovoitov case BPF_FUNC_get_stackid:
1574d5a3b1f6SAlexei Starovoitov return &bpf_get_stackid_proto;
1575c195651eSYonghong Song case BPF_FUNC_get_stack:
1576c195651eSYonghong Song return &bpf_get_stack_proto;
15779802d865SJosef Bacik #ifdef CONFIG_BPF_KPROBE_OVERRIDE
15789802d865SJosef Bacik case BPF_FUNC_override_return:
15799802d865SJosef Bacik return &bpf_override_return_proto;
15809802d865SJosef Bacik #endif
15819ffd9f3fSJiri Olsa case BPF_FUNC_get_func_ip:
1582686328d8SJiri Olsa if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI)
1583686328d8SJiri Olsa return &bpf_get_func_ip_proto_kprobe_multi;
1584686328d8SJiri Olsa if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
1585686328d8SJiri Olsa return &bpf_get_func_ip_proto_uprobe_multi;
1586686328d8SJiri Olsa return &bpf_get_func_ip_proto_kprobe;
15877adfc6c9SAndrii Nakryiko case BPF_FUNC_get_attach_cookie:
15880b779b61SJiri Olsa if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI)
15890b779b61SJiri Olsa return &bpf_get_attach_cookie_proto_kmulti;
15900b779b61SJiri Olsa if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
15910b779b61SJiri Olsa return &bpf_get_attach_cookie_proto_umulti;
15920b779b61SJiri Olsa return &bpf_get_attach_cookie_proto_trace;
15932541517cSAlexei Starovoitov default:
1594fc611f47SKP Singh return bpf_tracing_func_proto(func_id, prog);
15952541517cSAlexei Starovoitov }
15962541517cSAlexei Starovoitov }
15972541517cSAlexei Starovoitov
15982541517cSAlexei Starovoitov /* bpf+kprobe programs can access fields of 'struct pt_regs' */
kprobe_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)159919de99f7SAlexei Starovoitov static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
16005e43f899SAndrey Ignatov const struct bpf_prog *prog,
160123994631SYonghong Song struct bpf_insn_access_aux *info)
16022541517cSAlexei Starovoitov {
16032541517cSAlexei Starovoitov if (off < 0 || off >= sizeof(struct pt_regs))
16042541517cSAlexei Starovoitov return false;
16052541517cSAlexei Starovoitov if (type != BPF_READ)
16062541517cSAlexei Starovoitov return false;
16072541517cSAlexei Starovoitov if (off % size != 0)
16082541517cSAlexei Starovoitov return false;
16092d071c64SDaniel Borkmann /*
16102d071c64SDaniel Borkmann * Assertion for 32 bit to make sure last 8 byte access
16112d071c64SDaniel Borkmann * (BPF_DW) to the last 4 byte member is disallowed.
16122d071c64SDaniel Borkmann */
16132d071c64SDaniel Borkmann if (off + size > sizeof(struct pt_regs))
16142d071c64SDaniel Borkmann return false;
16152d071c64SDaniel Borkmann
16162541517cSAlexei Starovoitov return true;
16172541517cSAlexei Starovoitov }
16182541517cSAlexei Starovoitov
16197de16e3aSJakub Kicinski const struct bpf_verifier_ops kprobe_verifier_ops = {
16202541517cSAlexei Starovoitov .get_func_proto = kprobe_prog_func_proto,
16212541517cSAlexei Starovoitov .is_valid_access = kprobe_prog_is_valid_access,
16222541517cSAlexei Starovoitov };
16232541517cSAlexei Starovoitov
16247de16e3aSJakub Kicinski const struct bpf_prog_ops kprobe_prog_ops = {
16257de16e3aSJakub Kicinski };
16267de16e3aSJakub Kicinski
BPF_CALL_5(bpf_perf_event_output_tp,void *,tp_buff,struct bpf_map *,map,u64,flags,void *,data,u64,size)1627f3694e00SDaniel Borkmann BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1628f3694e00SDaniel Borkmann u64, flags, void *, data, u64, size)
16299940d67cSAlexei Starovoitov {
1630f3694e00SDaniel Borkmann struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1631f3694e00SDaniel Borkmann
16329940d67cSAlexei Starovoitov /*
16339940d67cSAlexei Starovoitov * r1 points to perf tracepoint buffer where first 8 bytes are hidden
16349940d67cSAlexei Starovoitov * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1635f3694e00SDaniel Borkmann * from there and call the same bpf_perf_event_output() helper inline.
16369940d67cSAlexei Starovoitov */
1637f3694e00SDaniel Borkmann return ____bpf_perf_event_output(regs, map, flags, data, size);
16389940d67cSAlexei Starovoitov }
16399940d67cSAlexei Starovoitov
16409940d67cSAlexei Starovoitov static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
16419940d67cSAlexei Starovoitov .func = bpf_perf_event_output_tp,
16429940d67cSAlexei Starovoitov .gpl_only = true,
16439940d67cSAlexei Starovoitov .ret_type = RET_INTEGER,
16449940d67cSAlexei Starovoitov .arg1_type = ARG_PTR_TO_CTX,
16459940d67cSAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
16469940d67cSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
1647216e3cd2SHao Luo .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1648a60dd35dSGianluca Borello .arg5_type = ARG_CONST_SIZE_OR_ZERO,
16499940d67cSAlexei Starovoitov };
16509940d67cSAlexei Starovoitov
BPF_CALL_3(bpf_get_stackid_tp,void *,tp_buff,struct bpf_map *,map,u64,flags)1651f3694e00SDaniel Borkmann BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1652f3694e00SDaniel Borkmann u64, flags)
16539940d67cSAlexei Starovoitov {
1654f3694e00SDaniel Borkmann struct pt_regs *regs = *(struct pt_regs **)tp_buff;
16559940d67cSAlexei Starovoitov
1656f3694e00SDaniel Borkmann /*
1657f3694e00SDaniel Borkmann * Same comment as in bpf_perf_event_output_tp(), only that this time
1658f3694e00SDaniel Borkmann * the other helper's function body cannot be inlined due to being
1659f3694e00SDaniel Borkmann * external, thus we need to call raw helper function.
1660f3694e00SDaniel Borkmann */
1661f3694e00SDaniel Borkmann return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1662f3694e00SDaniel Borkmann flags, 0, 0);
16639940d67cSAlexei Starovoitov }
16649940d67cSAlexei Starovoitov
16659940d67cSAlexei Starovoitov static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
16669940d67cSAlexei Starovoitov .func = bpf_get_stackid_tp,
16679940d67cSAlexei Starovoitov .gpl_only = true,
16689940d67cSAlexei Starovoitov .ret_type = RET_INTEGER,
16699940d67cSAlexei Starovoitov .arg1_type = ARG_PTR_TO_CTX,
16709940d67cSAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
16719940d67cSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
16729940d67cSAlexei Starovoitov };
16739940d67cSAlexei Starovoitov
BPF_CALL_4(bpf_get_stack_tp,void *,tp_buff,void *,buf,u32,size,u64,flags)1674c195651eSYonghong Song BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1675c195651eSYonghong Song u64, flags)
1676c195651eSYonghong Song {
1677c195651eSYonghong Song struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1678c195651eSYonghong Song
1679c195651eSYonghong Song return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1680c195651eSYonghong Song (unsigned long) size, flags, 0);
1681c195651eSYonghong Song }
1682c195651eSYonghong Song
1683c195651eSYonghong Song static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1684c195651eSYonghong Song .func = bpf_get_stack_tp,
1685c195651eSYonghong Song .gpl_only = true,
1686c195651eSYonghong Song .ret_type = RET_INTEGER,
1687c195651eSYonghong Song .arg1_type = ARG_PTR_TO_CTX,
1688c195651eSYonghong Song .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1689c195651eSYonghong Song .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1690c195651eSYonghong Song .arg4_type = ARG_ANYTHING,
1691c195651eSYonghong Song };
1692c195651eSYonghong Song
16935e43f899SAndrey Ignatov static const struct bpf_func_proto *
tp_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)16945e43f899SAndrey Ignatov tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
16959fd82b61SAlexei Starovoitov {
16969fd82b61SAlexei Starovoitov switch (func_id) {
16979fd82b61SAlexei Starovoitov case BPF_FUNC_perf_event_output:
16989940d67cSAlexei Starovoitov return &bpf_perf_event_output_proto_tp;
16999fd82b61SAlexei Starovoitov case BPF_FUNC_get_stackid:
17009940d67cSAlexei Starovoitov return &bpf_get_stackid_proto_tp;
1701c195651eSYonghong Song case BPF_FUNC_get_stack:
1702c195651eSYonghong Song return &bpf_get_stack_proto_tp;
17037adfc6c9SAndrii Nakryiko case BPF_FUNC_get_attach_cookie:
17047adfc6c9SAndrii Nakryiko return &bpf_get_attach_cookie_proto_trace;
17059fd82b61SAlexei Starovoitov default:
1706fc611f47SKP Singh return bpf_tracing_func_proto(func_id, prog);
17079fd82b61SAlexei Starovoitov }
17089fd82b61SAlexei Starovoitov }
17099fd82b61SAlexei Starovoitov
tp_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)171019de99f7SAlexei Starovoitov static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
17115e43f899SAndrey Ignatov const struct bpf_prog *prog,
171223994631SYonghong Song struct bpf_insn_access_aux *info)
17139fd82b61SAlexei Starovoitov {
17149fd82b61SAlexei Starovoitov if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
17159fd82b61SAlexei Starovoitov return false;
17169fd82b61SAlexei Starovoitov if (type != BPF_READ)
17179fd82b61SAlexei Starovoitov return false;
17189fd82b61SAlexei Starovoitov if (off % size != 0)
17199fd82b61SAlexei Starovoitov return false;
17202d071c64SDaniel Borkmann
17212d071c64SDaniel Borkmann BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
17229fd82b61SAlexei Starovoitov return true;
17239fd82b61SAlexei Starovoitov }
17249fd82b61SAlexei Starovoitov
17257de16e3aSJakub Kicinski const struct bpf_verifier_ops tracepoint_verifier_ops = {
17269fd82b61SAlexei Starovoitov .get_func_proto = tp_prog_func_proto,
17279fd82b61SAlexei Starovoitov .is_valid_access = tp_prog_is_valid_access,
17289fd82b61SAlexei Starovoitov };
17299fd82b61SAlexei Starovoitov
17307de16e3aSJakub Kicinski const struct bpf_prog_ops tracepoint_prog_ops = {
17317de16e3aSJakub Kicinski };
17327de16e3aSJakub Kicinski
BPF_CALL_3(bpf_perf_prog_read_value,struct bpf_perf_event_data_kern *,ctx,struct bpf_perf_event_value *,buf,u32,size)1733f005afedSYonghong Song BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1734f005afedSYonghong Song struct bpf_perf_event_value *, buf, u32, size)
1735f005afedSYonghong Song {
1736f005afedSYonghong Song int err = -EINVAL;
1737f005afedSYonghong Song
1738f005afedSYonghong Song if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1739f005afedSYonghong Song goto clear;
1740f005afedSYonghong Song err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1741f005afedSYonghong Song &buf->running);
1742f005afedSYonghong Song if (unlikely(err))
1743f005afedSYonghong Song goto clear;
1744f005afedSYonghong Song return 0;
1745f005afedSYonghong Song clear:
1746f005afedSYonghong Song memset(buf, 0, size);
1747f005afedSYonghong Song return err;
1748f005afedSYonghong Song }
1749f005afedSYonghong Song
1750f005afedSYonghong Song static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1751f005afedSYonghong Song .func = bpf_perf_prog_read_value,
1752f005afedSYonghong Song .gpl_only = true,
1753f005afedSYonghong Song .ret_type = RET_INTEGER,
1754f005afedSYonghong Song .arg1_type = ARG_PTR_TO_CTX,
1755f005afedSYonghong Song .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1756f005afedSYonghong Song .arg3_type = ARG_CONST_SIZE,
1757f005afedSYonghong Song };
1758f005afedSYonghong Song
BPF_CALL_4(bpf_read_branch_records,struct bpf_perf_event_data_kern *,ctx,void *,buf,u32,size,u64,flags)1759fff7b643SDaniel Xu BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1760fff7b643SDaniel Xu void *, buf, u32, size, u64, flags)
1761fff7b643SDaniel Xu {
1762fff7b643SDaniel Xu static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1763fff7b643SDaniel Xu struct perf_branch_stack *br_stack = ctx->data->br_stack;
1764fff7b643SDaniel Xu u32 to_copy;
1765fff7b643SDaniel Xu
1766fff7b643SDaniel Xu if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1767fff7b643SDaniel Xu return -EINVAL;
1768fff7b643SDaniel Xu
1769cce6a2d7SJiri Olsa if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
1770cce6a2d7SJiri Olsa return -ENOENT;
1771cce6a2d7SJiri Olsa
1772fff7b643SDaniel Xu if (unlikely(!br_stack))
1773db52f572SKajol Jain return -ENOENT;
1774fff7b643SDaniel Xu
1775fff7b643SDaniel Xu if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1776fff7b643SDaniel Xu return br_stack->nr * br_entry_size;
1777fff7b643SDaniel Xu
1778fff7b643SDaniel Xu if (!buf || (size % br_entry_size != 0))
1779fff7b643SDaniel Xu return -EINVAL;
1780fff7b643SDaniel Xu
1781fff7b643SDaniel Xu to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1782fff7b643SDaniel Xu memcpy(buf, br_stack->entries, to_copy);
1783fff7b643SDaniel Xu
1784fff7b643SDaniel Xu return to_copy;
1785fff7b643SDaniel Xu }
1786fff7b643SDaniel Xu
1787fff7b643SDaniel Xu static const struct bpf_func_proto bpf_read_branch_records_proto = {
1788fff7b643SDaniel Xu .func = bpf_read_branch_records,
1789fff7b643SDaniel Xu .gpl_only = true,
1790fff7b643SDaniel Xu .ret_type = RET_INTEGER,
1791fff7b643SDaniel Xu .arg1_type = ARG_PTR_TO_CTX,
1792fff7b643SDaniel Xu .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1793fff7b643SDaniel Xu .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1794fff7b643SDaniel Xu .arg4_type = ARG_ANYTHING,
1795fff7b643SDaniel Xu };
1796fff7b643SDaniel Xu
17975e43f899SAndrey Ignatov static const struct bpf_func_proto *
pe_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)17985e43f899SAndrey Ignatov pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1799f005afedSYonghong Song {
1800f005afedSYonghong Song switch (func_id) {
1801f005afedSYonghong Song case BPF_FUNC_perf_event_output:
1802f005afedSYonghong Song return &bpf_perf_event_output_proto_tp;
1803f005afedSYonghong Song case BPF_FUNC_get_stackid:
18047b04d6d6SSong Liu return &bpf_get_stackid_proto_pe;
1805c195651eSYonghong Song case BPF_FUNC_get_stack:
18067b04d6d6SSong Liu return &bpf_get_stack_proto_pe;
1807f005afedSYonghong Song case BPF_FUNC_perf_prog_read_value:
1808f005afedSYonghong Song return &bpf_perf_prog_read_value_proto;
1809fff7b643SDaniel Xu case BPF_FUNC_read_branch_records:
1810fff7b643SDaniel Xu return &bpf_read_branch_records_proto;
18117adfc6c9SAndrii Nakryiko case BPF_FUNC_get_attach_cookie:
18127adfc6c9SAndrii Nakryiko return &bpf_get_attach_cookie_proto_pe;
1813f005afedSYonghong Song default:
1814fc611f47SKP Singh return bpf_tracing_func_proto(func_id, prog);
1815f005afedSYonghong Song }
1816f005afedSYonghong Song }
1817f005afedSYonghong Song
1818c4f6699dSAlexei Starovoitov /*
1819c4f6699dSAlexei Starovoitov * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1820c4f6699dSAlexei Starovoitov * to avoid potential recursive reuse issue when/if tracepoints are added
18219594dc3cSMatt Mullins * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
18229594dc3cSMatt Mullins *
18239594dc3cSMatt Mullins * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
18249594dc3cSMatt Mullins * in normal, irq, and nmi context.
1825c4f6699dSAlexei Starovoitov */
18269594dc3cSMatt Mullins struct bpf_raw_tp_regs {
18279594dc3cSMatt Mullins struct pt_regs regs[3];
18289594dc3cSMatt Mullins };
18299594dc3cSMatt Mullins static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
18309594dc3cSMatt Mullins static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
get_bpf_raw_tp_regs(void)18319594dc3cSMatt Mullins static struct pt_regs *get_bpf_raw_tp_regs(void)
18329594dc3cSMatt Mullins {
18339594dc3cSMatt Mullins struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
18349594dc3cSMatt Mullins int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
18359594dc3cSMatt Mullins
18369594dc3cSMatt Mullins if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
18379594dc3cSMatt Mullins this_cpu_dec(bpf_raw_tp_nest_level);
18389594dc3cSMatt Mullins return ERR_PTR(-EBUSY);
18399594dc3cSMatt Mullins }
18409594dc3cSMatt Mullins
18419594dc3cSMatt Mullins return &tp_regs->regs[nest_level - 1];
18429594dc3cSMatt Mullins }
18439594dc3cSMatt Mullins
put_bpf_raw_tp_regs(void)18449594dc3cSMatt Mullins static void put_bpf_raw_tp_regs(void)
18459594dc3cSMatt Mullins {
18469594dc3cSMatt Mullins this_cpu_dec(bpf_raw_tp_nest_level);
18479594dc3cSMatt Mullins }
18489594dc3cSMatt Mullins
BPF_CALL_5(bpf_perf_event_output_raw_tp,struct bpf_raw_tracepoint_args *,args,struct bpf_map *,map,u64,flags,void *,data,u64,size)1849c4f6699dSAlexei Starovoitov BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1850c4f6699dSAlexei Starovoitov struct bpf_map *, map, u64, flags, void *, data, u64, size)
1851c4f6699dSAlexei Starovoitov {
18529594dc3cSMatt Mullins struct pt_regs *regs = get_bpf_raw_tp_regs();
18539594dc3cSMatt Mullins int ret;
18549594dc3cSMatt Mullins
18559594dc3cSMatt Mullins if (IS_ERR(regs))
18569594dc3cSMatt Mullins return PTR_ERR(regs);
1857c4f6699dSAlexei Starovoitov
1858c4f6699dSAlexei Starovoitov perf_fetch_caller_regs(regs);
18599594dc3cSMatt Mullins ret = ____bpf_perf_event_output(regs, map, flags, data, size);
18609594dc3cSMatt Mullins
18619594dc3cSMatt Mullins put_bpf_raw_tp_regs();
18629594dc3cSMatt Mullins return ret;
1863c4f6699dSAlexei Starovoitov }
1864c4f6699dSAlexei Starovoitov
1865c4f6699dSAlexei Starovoitov static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1866c4f6699dSAlexei Starovoitov .func = bpf_perf_event_output_raw_tp,
1867c4f6699dSAlexei Starovoitov .gpl_only = true,
1868c4f6699dSAlexei Starovoitov .ret_type = RET_INTEGER,
1869c4f6699dSAlexei Starovoitov .arg1_type = ARG_PTR_TO_CTX,
1870c4f6699dSAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
1871c4f6699dSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
1872216e3cd2SHao Luo .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1873c4f6699dSAlexei Starovoitov .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1874c4f6699dSAlexei Starovoitov };
1875c4f6699dSAlexei Starovoitov
1876a7658e1aSAlexei Starovoitov extern const struct bpf_func_proto bpf_skb_output_proto;
1877d831ee84SEelco Chaudron extern const struct bpf_func_proto bpf_xdp_output_proto;
1878d9917302SEelco Chaudron extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1879a7658e1aSAlexei Starovoitov
BPF_CALL_3(bpf_get_stackid_raw_tp,struct bpf_raw_tracepoint_args *,args,struct bpf_map *,map,u64,flags)1880c4f6699dSAlexei Starovoitov BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1881c4f6699dSAlexei Starovoitov struct bpf_map *, map, u64, flags)
1882c4f6699dSAlexei Starovoitov {
18839594dc3cSMatt Mullins struct pt_regs *regs = get_bpf_raw_tp_regs();
18849594dc3cSMatt Mullins int ret;
18859594dc3cSMatt Mullins
18869594dc3cSMatt Mullins if (IS_ERR(regs))
18879594dc3cSMatt Mullins return PTR_ERR(regs);
1888c4f6699dSAlexei Starovoitov
1889c4f6699dSAlexei Starovoitov perf_fetch_caller_regs(regs);
1890c4f6699dSAlexei Starovoitov /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
18919594dc3cSMatt Mullins ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1892c4f6699dSAlexei Starovoitov flags, 0, 0);
18939594dc3cSMatt Mullins put_bpf_raw_tp_regs();
18949594dc3cSMatt Mullins return ret;
1895c4f6699dSAlexei Starovoitov }
1896c4f6699dSAlexei Starovoitov
1897c4f6699dSAlexei Starovoitov static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1898c4f6699dSAlexei Starovoitov .func = bpf_get_stackid_raw_tp,
1899c4f6699dSAlexei Starovoitov .gpl_only = true,
1900c4f6699dSAlexei Starovoitov .ret_type = RET_INTEGER,
1901c4f6699dSAlexei Starovoitov .arg1_type = ARG_PTR_TO_CTX,
1902c4f6699dSAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
1903c4f6699dSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
1904c4f6699dSAlexei Starovoitov };
1905c4f6699dSAlexei Starovoitov
BPF_CALL_4(bpf_get_stack_raw_tp,struct bpf_raw_tracepoint_args *,args,void *,buf,u32,size,u64,flags)1906c195651eSYonghong Song BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1907c195651eSYonghong Song void *, buf, u32, size, u64, flags)
1908c195651eSYonghong Song {
19099594dc3cSMatt Mullins struct pt_regs *regs = get_bpf_raw_tp_regs();
19109594dc3cSMatt Mullins int ret;
19119594dc3cSMatt Mullins
19129594dc3cSMatt Mullins if (IS_ERR(regs))
19139594dc3cSMatt Mullins return PTR_ERR(regs);
1914c195651eSYonghong Song
1915c195651eSYonghong Song perf_fetch_caller_regs(regs);
19169594dc3cSMatt Mullins ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1917c195651eSYonghong Song (unsigned long) size, flags, 0);
19189594dc3cSMatt Mullins put_bpf_raw_tp_regs();
19199594dc3cSMatt Mullins return ret;
1920c195651eSYonghong Song }
1921c195651eSYonghong Song
1922c195651eSYonghong Song static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1923c195651eSYonghong Song .func = bpf_get_stack_raw_tp,
1924c195651eSYonghong Song .gpl_only = true,
1925c195651eSYonghong Song .ret_type = RET_INTEGER,
1926c195651eSYonghong Song .arg1_type = ARG_PTR_TO_CTX,
1927216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1928c195651eSYonghong Song .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1929c195651eSYonghong Song .arg4_type = ARG_ANYTHING,
1930c195651eSYonghong Song };
1931c195651eSYonghong Song
19325e43f899SAndrey Ignatov static const struct bpf_func_proto *
raw_tp_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)19335e43f899SAndrey Ignatov raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1934c4f6699dSAlexei Starovoitov {
1935c4f6699dSAlexei Starovoitov switch (func_id) {
1936c4f6699dSAlexei Starovoitov case BPF_FUNC_perf_event_output:
1937c4f6699dSAlexei Starovoitov return &bpf_perf_event_output_proto_raw_tp;
1938c4f6699dSAlexei Starovoitov case BPF_FUNC_get_stackid:
1939c4f6699dSAlexei Starovoitov return &bpf_get_stackid_proto_raw_tp;
1940c195651eSYonghong Song case BPF_FUNC_get_stack:
1941c195651eSYonghong Song return &bpf_get_stack_proto_raw_tp;
1942c4f6699dSAlexei Starovoitov default:
1943fc611f47SKP Singh return bpf_tracing_func_proto(func_id, prog);
1944c4f6699dSAlexei Starovoitov }
1945c4f6699dSAlexei Starovoitov }
1946c4f6699dSAlexei Starovoitov
1947958a3f2dSJiri Olsa const struct bpf_func_proto *
tracing_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1948f1b9509cSAlexei Starovoitov tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1949f1b9509cSAlexei Starovoitov {
19503cee6fb8SMartin KaFai Lau const struct bpf_func_proto *fn;
19513cee6fb8SMartin KaFai Lau
1952f1b9509cSAlexei Starovoitov switch (func_id) {
1953f1b9509cSAlexei Starovoitov #ifdef CONFIG_NET
1954f1b9509cSAlexei Starovoitov case BPF_FUNC_skb_output:
1955f1b9509cSAlexei Starovoitov return &bpf_skb_output_proto;
1956d831ee84SEelco Chaudron case BPF_FUNC_xdp_output:
1957d831ee84SEelco Chaudron return &bpf_xdp_output_proto;
1958af7ec138SYonghong Song case BPF_FUNC_skc_to_tcp6_sock:
1959af7ec138SYonghong Song return &bpf_skc_to_tcp6_sock_proto;
1960478cfbdfSYonghong Song case BPF_FUNC_skc_to_tcp_sock:
1961478cfbdfSYonghong Song return &bpf_skc_to_tcp_sock_proto;
1962478cfbdfSYonghong Song case BPF_FUNC_skc_to_tcp_timewait_sock:
1963478cfbdfSYonghong Song return &bpf_skc_to_tcp_timewait_sock_proto;
1964478cfbdfSYonghong Song case BPF_FUNC_skc_to_tcp_request_sock:
1965478cfbdfSYonghong Song return &bpf_skc_to_tcp_request_sock_proto;
19660d4fad3eSYonghong Song case BPF_FUNC_skc_to_udp6_sock:
19670d4fad3eSYonghong Song return &bpf_skc_to_udp6_sock_proto;
19689eeb3aa3SHengqi Chen case BPF_FUNC_skc_to_unix_sock:
19699eeb3aa3SHengqi Chen return &bpf_skc_to_unix_sock_proto;
19703bc253c2SGeliang Tang case BPF_FUNC_skc_to_mptcp_sock:
19713bc253c2SGeliang Tang return &bpf_skc_to_mptcp_sock_proto;
19728e4597c6SMartin KaFai Lau case BPF_FUNC_sk_storage_get:
19738e4597c6SMartin KaFai Lau return &bpf_sk_storage_get_tracing_proto;
19748e4597c6SMartin KaFai Lau case BPF_FUNC_sk_storage_delete:
19758e4597c6SMartin KaFai Lau return &bpf_sk_storage_delete_tracing_proto;
1976b60da495SFlorent Revest case BPF_FUNC_sock_from_file:
1977b60da495SFlorent Revest return &bpf_sock_from_file_proto;
1978c5dbb89fSFlorent Revest case BPF_FUNC_get_socket_cookie:
1979c5dbb89fSFlorent Revest return &bpf_get_socket_ptr_cookie_proto;
1980d9917302SEelco Chaudron case BPF_FUNC_xdp_get_buff_len:
1981d9917302SEelco Chaudron return &bpf_xdp_get_buff_len_trace_proto;
1982f1b9509cSAlexei Starovoitov #endif
1983492e639fSYonghong Song case BPF_FUNC_seq_printf:
1984492e639fSYonghong Song return prog->expected_attach_type == BPF_TRACE_ITER ?
1985492e639fSYonghong Song &bpf_seq_printf_proto :
1986492e639fSYonghong Song NULL;
1987492e639fSYonghong Song case BPF_FUNC_seq_write:
1988492e639fSYonghong Song return prog->expected_attach_type == BPF_TRACE_ITER ?
1989492e639fSYonghong Song &bpf_seq_write_proto :
1990492e639fSYonghong Song NULL;
1991eb411377SAlan Maguire case BPF_FUNC_seq_printf_btf:
1992eb411377SAlan Maguire return prog->expected_attach_type == BPF_TRACE_ITER ?
1993eb411377SAlan Maguire &bpf_seq_printf_btf_proto :
1994eb411377SAlan Maguire NULL;
19956e22ab9dSJiri Olsa case BPF_FUNC_d_path:
19966e22ab9dSJiri Olsa return &bpf_d_path_proto;
1997f92c1e18SJiri Olsa case BPF_FUNC_get_func_arg:
1998f92c1e18SJiri Olsa return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1999f92c1e18SJiri Olsa case BPF_FUNC_get_func_ret:
2000f92c1e18SJiri Olsa return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
2001f92c1e18SJiri Olsa case BPF_FUNC_get_func_arg_cnt:
2002f92c1e18SJiri Olsa return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
20032fcc8241SKui-Feng Lee case BPF_FUNC_get_attach_cookie:
20042fcc8241SKui-Feng Lee return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
2005f1b9509cSAlexei Starovoitov default:
20063cee6fb8SMartin KaFai Lau fn = raw_tp_prog_func_proto(func_id, prog);
20073cee6fb8SMartin KaFai Lau if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
20083cee6fb8SMartin KaFai Lau fn = bpf_iter_get_func_proto(func_id, prog);
20093cee6fb8SMartin KaFai Lau return fn;
2010f1b9509cSAlexei Starovoitov }
2011f1b9509cSAlexei Starovoitov }
2012f1b9509cSAlexei Starovoitov
raw_tp_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2013c4f6699dSAlexei Starovoitov static bool raw_tp_prog_is_valid_access(int off, int size,
2014c4f6699dSAlexei Starovoitov enum bpf_access_type type,
20155e43f899SAndrey Ignatov const struct bpf_prog *prog,
2016c4f6699dSAlexei Starovoitov struct bpf_insn_access_aux *info)
2017c4f6699dSAlexei Starovoitov {
201835346ab6SHou Tao return bpf_tracing_ctx_access(off, size, type);
2019f1b9509cSAlexei Starovoitov }
2020f1b9509cSAlexei Starovoitov
tracing_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2021f1b9509cSAlexei Starovoitov static bool tracing_prog_is_valid_access(int off, int size,
2022f1b9509cSAlexei Starovoitov enum bpf_access_type type,
2023f1b9509cSAlexei Starovoitov const struct bpf_prog *prog,
2024f1b9509cSAlexei Starovoitov struct bpf_insn_access_aux *info)
2025f1b9509cSAlexei Starovoitov {
202635346ab6SHou Tao return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
2027c4f6699dSAlexei Starovoitov }
2028c4f6699dSAlexei Starovoitov
bpf_prog_test_run_tracing(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)20293e7c67d9SKP Singh int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
20303e7c67d9SKP Singh const union bpf_attr *kattr,
20313e7c67d9SKP Singh union bpf_attr __user *uattr)
20323e7c67d9SKP Singh {
20333e7c67d9SKP Singh return -ENOTSUPP;
20343e7c67d9SKP Singh }
20353e7c67d9SKP Singh
2036c4f6699dSAlexei Starovoitov const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
2037c4f6699dSAlexei Starovoitov .get_func_proto = raw_tp_prog_func_proto,
2038c4f6699dSAlexei Starovoitov .is_valid_access = raw_tp_prog_is_valid_access,
2039c4f6699dSAlexei Starovoitov };
2040c4f6699dSAlexei Starovoitov
2041c4f6699dSAlexei Starovoitov const struct bpf_prog_ops raw_tracepoint_prog_ops = {
2042ebfb4d40SYonghong Song #ifdef CONFIG_NET
20431b4d60ecSSong Liu .test_run = bpf_prog_test_run_raw_tp,
2044ebfb4d40SYonghong Song #endif
2045c4f6699dSAlexei Starovoitov };
2046c4f6699dSAlexei Starovoitov
2047f1b9509cSAlexei Starovoitov const struct bpf_verifier_ops tracing_verifier_ops = {
2048f1b9509cSAlexei Starovoitov .get_func_proto = tracing_prog_func_proto,
2049f1b9509cSAlexei Starovoitov .is_valid_access = tracing_prog_is_valid_access,
2050f1b9509cSAlexei Starovoitov };
2051f1b9509cSAlexei Starovoitov
2052f1b9509cSAlexei Starovoitov const struct bpf_prog_ops tracing_prog_ops = {
2053da00d2f1SKP Singh .test_run = bpf_prog_test_run_tracing,
2054f1b9509cSAlexei Starovoitov };
2055f1b9509cSAlexei Starovoitov
raw_tp_writable_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)20569df1c28bSMatt Mullins static bool raw_tp_writable_prog_is_valid_access(int off, int size,
20579df1c28bSMatt Mullins enum bpf_access_type type,
20589df1c28bSMatt Mullins const struct bpf_prog *prog,
20599df1c28bSMatt Mullins struct bpf_insn_access_aux *info)
20609df1c28bSMatt Mullins {
20619df1c28bSMatt Mullins if (off == 0) {
20629df1c28bSMatt Mullins if (size != sizeof(u64) || type != BPF_READ)
20639df1c28bSMatt Mullins return false;
20649df1c28bSMatt Mullins info->reg_type = PTR_TO_TP_BUFFER;
20659df1c28bSMatt Mullins }
20669df1c28bSMatt Mullins return raw_tp_prog_is_valid_access(off, size, type, prog, info);
20679df1c28bSMatt Mullins }
20689df1c28bSMatt Mullins
20699df1c28bSMatt Mullins const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
20709df1c28bSMatt Mullins .get_func_proto = raw_tp_prog_func_proto,
20719df1c28bSMatt Mullins .is_valid_access = raw_tp_writable_prog_is_valid_access,
20729df1c28bSMatt Mullins };
20739df1c28bSMatt Mullins
20749df1c28bSMatt Mullins const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
20759df1c28bSMatt Mullins };
20769df1c28bSMatt Mullins
pe_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)20770515e599SAlexei Starovoitov static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
20785e43f899SAndrey Ignatov const struct bpf_prog *prog,
207923994631SYonghong Song struct bpf_insn_access_aux *info)
20800515e599SAlexei Starovoitov {
208195da0cdbSTeng Qin const int size_u64 = sizeof(u64);
208231fd8581SYonghong Song
20830515e599SAlexei Starovoitov if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
20840515e599SAlexei Starovoitov return false;
20850515e599SAlexei Starovoitov if (type != BPF_READ)
20860515e599SAlexei Starovoitov return false;
2087bc23105cSDaniel Borkmann if (off % size != 0) {
2088bc23105cSDaniel Borkmann if (sizeof(unsigned long) != 4)
20890515e599SAlexei Starovoitov return false;
2090bc23105cSDaniel Borkmann if (size != 8)
2091bc23105cSDaniel Borkmann return false;
2092bc23105cSDaniel Borkmann if (off % size != 4)
2093bc23105cSDaniel Borkmann return false;
2094bc23105cSDaniel Borkmann }
209531fd8581SYonghong Song
2096f96da094SDaniel Borkmann switch (off) {
2097f96da094SDaniel Borkmann case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
209895da0cdbSTeng Qin bpf_ctx_record_field_size(info, size_u64);
209995da0cdbSTeng Qin if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
210095da0cdbSTeng Qin return false;
210195da0cdbSTeng Qin break;
210295da0cdbSTeng Qin case bpf_ctx_range(struct bpf_perf_event_data, addr):
210395da0cdbSTeng Qin bpf_ctx_record_field_size(info, size_u64);
210495da0cdbSTeng Qin if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
210523994631SYonghong Song return false;
2106f96da094SDaniel Borkmann break;
2107f96da094SDaniel Borkmann default:
21080515e599SAlexei Starovoitov if (size != sizeof(long))
21090515e599SAlexei Starovoitov return false;
21100515e599SAlexei Starovoitov }
2111f96da094SDaniel Borkmann
21120515e599SAlexei Starovoitov return true;
21130515e599SAlexei Starovoitov }
21140515e599SAlexei Starovoitov
pe_prog_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)21156b8cc1d1SDaniel Borkmann static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
21166b8cc1d1SDaniel Borkmann const struct bpf_insn *si,
21170515e599SAlexei Starovoitov struct bpf_insn *insn_buf,
2118f96da094SDaniel Borkmann struct bpf_prog *prog, u32 *target_size)
21190515e599SAlexei Starovoitov {
21200515e599SAlexei Starovoitov struct bpf_insn *insn = insn_buf;
21210515e599SAlexei Starovoitov
21226b8cc1d1SDaniel Borkmann switch (si->off) {
21230515e599SAlexei Starovoitov case offsetof(struct bpf_perf_event_data, sample_period):
2124f035a515SDaniel Borkmann *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
21256b8cc1d1SDaniel Borkmann data), si->dst_reg, si->src_reg,
21260515e599SAlexei Starovoitov offsetof(struct bpf_perf_event_data_kern, data));
21276b8cc1d1SDaniel Borkmann *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2128f96da094SDaniel Borkmann bpf_target_off(struct perf_sample_data, period, 8,
2129f96da094SDaniel Borkmann target_size));
21300515e599SAlexei Starovoitov break;
213195da0cdbSTeng Qin case offsetof(struct bpf_perf_event_data, addr):
213295da0cdbSTeng Qin *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
213395da0cdbSTeng Qin data), si->dst_reg, si->src_reg,
213495da0cdbSTeng Qin offsetof(struct bpf_perf_event_data_kern, data));
213595da0cdbSTeng Qin *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
213695da0cdbSTeng Qin bpf_target_off(struct perf_sample_data, addr, 8,
213795da0cdbSTeng Qin target_size));
213895da0cdbSTeng Qin break;
21390515e599SAlexei Starovoitov default:
2140f035a515SDaniel Borkmann *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
21416b8cc1d1SDaniel Borkmann regs), si->dst_reg, si->src_reg,
21420515e599SAlexei Starovoitov offsetof(struct bpf_perf_event_data_kern, regs));
21436b8cc1d1SDaniel Borkmann *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
21446b8cc1d1SDaniel Borkmann si->off);
21450515e599SAlexei Starovoitov break;
21460515e599SAlexei Starovoitov }
21470515e599SAlexei Starovoitov
21480515e599SAlexei Starovoitov return insn - insn_buf;
21490515e599SAlexei Starovoitov }
21500515e599SAlexei Starovoitov
21517de16e3aSJakub Kicinski const struct bpf_verifier_ops perf_event_verifier_ops = {
2152f005afedSYonghong Song .get_func_proto = pe_prog_func_proto,
21530515e599SAlexei Starovoitov .is_valid_access = pe_prog_is_valid_access,
21540515e599SAlexei Starovoitov .convert_ctx_access = pe_prog_convert_ctx_access,
21550515e599SAlexei Starovoitov };
21567de16e3aSJakub Kicinski
21577de16e3aSJakub Kicinski const struct bpf_prog_ops perf_event_prog_ops = {
21587de16e3aSJakub Kicinski };
2159e87c6bc3SYonghong Song
2160e87c6bc3SYonghong Song static DEFINE_MUTEX(bpf_event_mutex);
2161e87c6bc3SYonghong Song
2162c8c088baSYonghong Song #define BPF_TRACE_MAX_PROGS 64
2163c8c088baSYonghong Song
perf_event_attach_bpf_prog(struct perf_event * event,struct bpf_prog * prog,u64 bpf_cookie)2164e87c6bc3SYonghong Song int perf_event_attach_bpf_prog(struct perf_event *event,
216582e6b1eeSAndrii Nakryiko struct bpf_prog *prog,
216682e6b1eeSAndrii Nakryiko u64 bpf_cookie)
2167e87c6bc3SYonghong Song {
2168e672db03SStanislav Fomichev struct bpf_prog_array *old_array;
2169e87c6bc3SYonghong Song struct bpf_prog_array *new_array;
2170e87c6bc3SYonghong Song int ret = -EEXIST;
2171e87c6bc3SYonghong Song
21729802d865SJosef Bacik /*
2173b4da3340SMasami Hiramatsu * Kprobe override only works if they are on the function entry,
2174b4da3340SMasami Hiramatsu * and only if they are on the opt-in list.
21759802d865SJosef Bacik */
21769802d865SJosef Bacik if (prog->kprobe_override &&
2177b4da3340SMasami Hiramatsu (!trace_kprobe_on_func_entry(event->tp_event) ||
21789802d865SJosef Bacik !trace_kprobe_error_injectable(event->tp_event)))
21799802d865SJosef Bacik return -EINVAL;
21809802d865SJosef Bacik
2181e87c6bc3SYonghong Song mutex_lock(&bpf_event_mutex);
2182e87c6bc3SYonghong Song
2183e87c6bc3SYonghong Song if (event->prog)
218407c41a29SYonghong Song goto unlock;
2185e87c6bc3SYonghong Song
2186e672db03SStanislav Fomichev old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2187c8c088baSYonghong Song if (old_array &&
2188c8c088baSYonghong Song bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
2189c8c088baSYonghong Song ret = -E2BIG;
2190c8c088baSYonghong Song goto unlock;
2191c8c088baSYonghong Song }
2192c8c088baSYonghong Song
219382e6b1eeSAndrii Nakryiko ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
2194e87c6bc3SYonghong Song if (ret < 0)
219507c41a29SYonghong Song goto unlock;
2196e87c6bc3SYonghong Song
2197e87c6bc3SYonghong Song /* set the new array to event->tp_event and set event->prog */
2198e87c6bc3SYonghong Song event->prog = prog;
219982e6b1eeSAndrii Nakryiko event->bpf_cookie = bpf_cookie;
2200e87c6bc3SYonghong Song rcu_assign_pointer(event->tp_event->prog_array, new_array);
22018c7dcb84SDelyan Kratunov bpf_prog_array_free_sleepable(old_array);
2202e87c6bc3SYonghong Song
220307c41a29SYonghong Song unlock:
2204e87c6bc3SYonghong Song mutex_unlock(&bpf_event_mutex);
2205e87c6bc3SYonghong Song return ret;
2206e87c6bc3SYonghong Song }
2207e87c6bc3SYonghong Song
perf_event_detach_bpf_prog(struct perf_event * event)2208e87c6bc3SYonghong Song void perf_event_detach_bpf_prog(struct perf_event *event)
2209e87c6bc3SYonghong Song {
2210e672db03SStanislav Fomichev struct bpf_prog_array *old_array;
2211e87c6bc3SYonghong Song struct bpf_prog_array *new_array;
2212e87c6bc3SYonghong Song int ret;
2213e87c6bc3SYonghong Song
2214e87c6bc3SYonghong Song mutex_lock(&bpf_event_mutex);
2215e87c6bc3SYonghong Song
2216e87c6bc3SYonghong Song if (!event->prog)
221707c41a29SYonghong Song goto unlock;
2218e87c6bc3SYonghong Song
2219e672db03SStanislav Fomichev old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2220c2b6b476SJiri Olsa if (!old_array)
2221c2b6b476SJiri Olsa goto put;
2222c2b6b476SJiri Olsa
222382e6b1eeSAndrii Nakryiko ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
2224e87c6bc3SYonghong Song if (ret < 0) {
2225e87c6bc3SYonghong Song bpf_prog_array_delete_safe(old_array, event->prog);
2226e87c6bc3SYonghong Song } else {
2227e87c6bc3SYonghong Song rcu_assign_pointer(event->tp_event->prog_array, new_array);
22288c7dcb84SDelyan Kratunov bpf_prog_array_free_sleepable(old_array);
2229e87c6bc3SYonghong Song }
2230e87c6bc3SYonghong Song
2231c2b6b476SJiri Olsa put:
2232f9f85df3SJann Horn /*
2233f9f85df3SJann Horn * It could be that the bpf_prog is not sleepable (and will be freed
2234f9f85df3SJann Horn * via normal RCU), but is called from a point that supports sleepable
2235f9f85df3SJann Horn * programs and uses tasks-trace-RCU.
2236f9f85df3SJann Horn */
2237f9f85df3SJann Horn synchronize_rcu_tasks_trace();
2238f9f85df3SJann Horn
2239e87c6bc3SYonghong Song bpf_prog_put(event->prog);
2240e87c6bc3SYonghong Song event->prog = NULL;
2241e87c6bc3SYonghong Song
224207c41a29SYonghong Song unlock:
2243e87c6bc3SYonghong Song mutex_unlock(&bpf_event_mutex);
2244e87c6bc3SYonghong Song }
2245f371b304SYonghong Song
perf_event_query_prog_array(struct perf_event * event,void __user * info)2246f4e2298eSYonghong Song int perf_event_query_prog_array(struct perf_event *event, void __user *info)
2247f371b304SYonghong Song {
2248f371b304SYonghong Song struct perf_event_query_bpf __user *uquery = info;
2249f371b304SYonghong Song struct perf_event_query_bpf query = {};
2250e672db03SStanislav Fomichev struct bpf_prog_array *progs;
22513a38bb98SYonghong Song u32 *ids, prog_cnt, ids_len;
2252f371b304SYonghong Song int ret;
2253f371b304SYonghong Song
2254031258daSAlexey Budankov if (!perfmon_capable())
2255f371b304SYonghong Song return -EPERM;
2256f371b304SYonghong Song if (event->attr.type != PERF_TYPE_TRACEPOINT)
2257f371b304SYonghong Song return -EINVAL;
2258f371b304SYonghong Song if (copy_from_user(&query, uquery, sizeof(query)))
2259f371b304SYonghong Song return -EFAULT;
22603a38bb98SYonghong Song
22613a38bb98SYonghong Song ids_len = query.ids_len;
22623a38bb98SYonghong Song if (ids_len > BPF_TRACE_MAX_PROGS)
22639c481b90SDaniel Borkmann return -E2BIG;
22643a38bb98SYonghong Song ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
22653a38bb98SYonghong Song if (!ids)
22663a38bb98SYonghong Song return -ENOMEM;
22673a38bb98SYonghong Song /*
22683a38bb98SYonghong Song * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
22693a38bb98SYonghong Song * is required when user only wants to check for uquery->prog_cnt.
22703a38bb98SYonghong Song * There is no need to check for it since the case is handled
22713a38bb98SYonghong Song * gracefully in bpf_prog_array_copy_info.
22723a38bb98SYonghong Song */
2273f371b304SYonghong Song
2274f371b304SYonghong Song mutex_lock(&bpf_event_mutex);
2275e672db03SStanislav Fomichev progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2276e672db03SStanislav Fomichev ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2277f371b304SYonghong Song mutex_unlock(&bpf_event_mutex);
2278f371b304SYonghong Song
22793a38bb98SYonghong Song if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
22803a38bb98SYonghong Song copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
22813a38bb98SYonghong Song ret = -EFAULT;
22823a38bb98SYonghong Song
22833a38bb98SYonghong Song kfree(ids);
2284f371b304SYonghong Song return ret;
2285f371b304SYonghong Song }
2286c4f6699dSAlexei Starovoitov
2287c4f6699dSAlexei Starovoitov extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2288c4f6699dSAlexei Starovoitov extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2289c4f6699dSAlexei Starovoitov
bpf_get_raw_tracepoint(const char * name)2290a38d1107SMatt Mullins struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2291c4f6699dSAlexei Starovoitov {
2292c4f6699dSAlexei Starovoitov struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2293c4f6699dSAlexei Starovoitov
2294c4f6699dSAlexei Starovoitov for (; btp < __stop__bpf_raw_tp; btp++) {
2295c4f6699dSAlexei Starovoitov if (!strcmp(btp->tp->name, name))
2296c4f6699dSAlexei Starovoitov return btp;
2297c4f6699dSAlexei Starovoitov }
2298a38d1107SMatt Mullins
2299a38d1107SMatt Mullins return bpf_get_raw_tracepoint_module(name);
2300a38d1107SMatt Mullins }
2301a38d1107SMatt Mullins
bpf_put_raw_tracepoint(struct bpf_raw_event_map * btp)2302a38d1107SMatt Mullins void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2303a38d1107SMatt Mullins {
230412cc126dSAndrii Nakryiko struct module *mod;
2305a38d1107SMatt Mullins
230612cc126dSAndrii Nakryiko preempt_disable();
230712cc126dSAndrii Nakryiko mod = __module_address((unsigned long)btp);
2308a38d1107SMatt Mullins module_put(mod);
230912cc126dSAndrii Nakryiko preempt_enable();
2310c4f6699dSAlexei Starovoitov }
2311c4f6699dSAlexei Starovoitov
2312c4f6699dSAlexei Starovoitov static __always_inline
__bpf_trace_run(struct bpf_prog * prog,u64 * args)2313c4f6699dSAlexei Starovoitov void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2314c4f6699dSAlexei Starovoitov {
2315f03efe49SThomas Gleixner cant_sleep();
231605b24ff9SJiri Olsa if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
231705b24ff9SJiri Olsa bpf_prog_inc_misses_counter(prog);
231805b24ff9SJiri Olsa goto out;
231905b24ff9SJiri Olsa }
2320c4f6699dSAlexei Starovoitov rcu_read_lock();
2321fb7dd8bcSAndrii Nakryiko (void) bpf_prog_run(prog, args);
2322c4f6699dSAlexei Starovoitov rcu_read_unlock();
232305b24ff9SJiri Olsa out:
232405b24ff9SJiri Olsa this_cpu_dec(*(prog->active));
2325c4f6699dSAlexei Starovoitov }
2326c4f6699dSAlexei Starovoitov
2327c4f6699dSAlexei Starovoitov #define UNPACK(...) __VA_ARGS__
2328c4f6699dSAlexei Starovoitov #define REPEAT_1(FN, DL, X, ...) FN(X)
2329c4f6699dSAlexei Starovoitov #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2330c4f6699dSAlexei Starovoitov #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2331c4f6699dSAlexei Starovoitov #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2332c4f6699dSAlexei Starovoitov #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2333c4f6699dSAlexei Starovoitov #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2334c4f6699dSAlexei Starovoitov #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2335c4f6699dSAlexei Starovoitov #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2336c4f6699dSAlexei Starovoitov #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2337c4f6699dSAlexei Starovoitov #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2338c4f6699dSAlexei Starovoitov #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2339c4f6699dSAlexei Starovoitov #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2340c4f6699dSAlexei Starovoitov #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2341c4f6699dSAlexei Starovoitov
2342c4f6699dSAlexei Starovoitov #define SARG(X) u64 arg##X
2343c4f6699dSAlexei Starovoitov #define COPY(X) args[X] = arg##X
2344c4f6699dSAlexei Starovoitov
2345c4f6699dSAlexei Starovoitov #define __DL_COM (,)
2346c4f6699dSAlexei Starovoitov #define __DL_SEM (;)
2347c4f6699dSAlexei Starovoitov
2348c4f6699dSAlexei Starovoitov #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2349c4f6699dSAlexei Starovoitov
2350c4f6699dSAlexei Starovoitov #define BPF_TRACE_DEFN_x(x) \
2351c4f6699dSAlexei Starovoitov void bpf_trace_run##x(struct bpf_prog *prog, \
2352c4f6699dSAlexei Starovoitov REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2353c4f6699dSAlexei Starovoitov { \
2354c4f6699dSAlexei Starovoitov u64 args[x]; \
2355c4f6699dSAlexei Starovoitov REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2356c4f6699dSAlexei Starovoitov __bpf_trace_run(prog, args); \
2357c4f6699dSAlexei Starovoitov } \
2358c4f6699dSAlexei Starovoitov EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2359c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(1);
2360c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(2);
2361c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(3);
2362c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(4);
2363c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(5);
2364c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(6);
2365c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(7);
2366c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(8);
2367c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(9);
2368c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(10);
2369c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(11);
2370c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(12);
2371c4f6699dSAlexei Starovoitov
__bpf_probe_register(struct bpf_raw_event_map * btp,struct bpf_prog * prog)2372c4f6699dSAlexei Starovoitov static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2373c4f6699dSAlexei Starovoitov {
2374c4f6699dSAlexei Starovoitov struct tracepoint *tp = btp->tp;
2375c4f6699dSAlexei Starovoitov
2376c4f6699dSAlexei Starovoitov /*
2377c4f6699dSAlexei Starovoitov * check that program doesn't access arguments beyond what's
2378c4f6699dSAlexei Starovoitov * available in this tracepoint
2379c4f6699dSAlexei Starovoitov */
2380c4f6699dSAlexei Starovoitov if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2381c4f6699dSAlexei Starovoitov return -EINVAL;
2382c4f6699dSAlexei Starovoitov
23839df1c28bSMatt Mullins if (prog->aux->max_tp_access > btp->writable_size)
23849df1c28bSMatt Mullins return -EINVAL;
23859df1c28bSMatt Mullins
23869913d574SSteven Rostedt (VMware) return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
23879913d574SSteven Rostedt (VMware) prog);
2388c4f6699dSAlexei Starovoitov }
2389c4f6699dSAlexei Starovoitov
bpf_probe_register(struct bpf_raw_event_map * btp,struct bpf_prog * prog)2390c4f6699dSAlexei Starovoitov int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2391c4f6699dSAlexei Starovoitov {
2392e16ec340SAlexei Starovoitov return __bpf_probe_register(btp, prog);
2393c4f6699dSAlexei Starovoitov }
2394c4f6699dSAlexei Starovoitov
bpf_probe_unregister(struct bpf_raw_event_map * btp,struct bpf_prog * prog)2395c4f6699dSAlexei Starovoitov int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2396c4f6699dSAlexei Starovoitov {
2397e16ec340SAlexei Starovoitov return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2398c4f6699dSAlexei Starovoitov }
239941bdc4b4SYonghong Song
bpf_get_perf_event_info(const struct perf_event * event,u32 * prog_id,u32 * fd_type,const char ** buf,u64 * probe_offset,u64 * probe_addr,unsigned long * missed)240041bdc4b4SYonghong Song int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
240141bdc4b4SYonghong Song u32 *fd_type, const char **buf,
240299bf10e9SJiri Olsa u64 *probe_offset, u64 *probe_addr,
240399bf10e9SJiri Olsa unsigned long *missed)
240441bdc4b4SYonghong Song {
240541bdc4b4SYonghong Song bool is_tracepoint, is_syscall_tp;
240641bdc4b4SYonghong Song struct bpf_prog *prog;
240741bdc4b4SYonghong Song int flags, err = 0;
240841bdc4b4SYonghong Song
240941bdc4b4SYonghong Song prog = event->prog;
241041bdc4b4SYonghong Song if (!prog)
241141bdc4b4SYonghong Song return -ENOENT;
241241bdc4b4SYonghong Song
241341bdc4b4SYonghong Song /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
241441bdc4b4SYonghong Song if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
241541bdc4b4SYonghong Song return -EOPNOTSUPP;
241641bdc4b4SYonghong Song
241741bdc4b4SYonghong Song *prog_id = prog->aux->id;
241841bdc4b4SYonghong Song flags = event->tp_event->flags;
241941bdc4b4SYonghong Song is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
242041bdc4b4SYonghong Song is_syscall_tp = is_syscall_trace_event(event->tp_event);
242141bdc4b4SYonghong Song
242241bdc4b4SYonghong Song if (is_tracepoint || is_syscall_tp) {
242341bdc4b4SYonghong Song *buf = is_tracepoint ? event->tp_event->tp->name
242441bdc4b4SYonghong Song : event->tp_event->name;
24251b715e1bSYafang Shao /* We allow NULL pointer for tracepoint */
24261b715e1bSYafang Shao if (fd_type)
242741bdc4b4SYonghong Song *fd_type = BPF_FD_TYPE_TRACEPOINT;
24281b715e1bSYafang Shao if (probe_offset)
242941bdc4b4SYonghong Song *probe_offset = 0x0;
24301b715e1bSYafang Shao if (probe_addr)
243141bdc4b4SYonghong Song *probe_addr = 0x0;
243241bdc4b4SYonghong Song } else {
243341bdc4b4SYonghong Song /* kprobe/uprobe */
243441bdc4b4SYonghong Song err = -EOPNOTSUPP;
243541bdc4b4SYonghong Song #ifdef CONFIG_KPROBE_EVENTS
243641bdc4b4SYonghong Song if (flags & TRACE_EVENT_FL_KPROBE)
243741bdc4b4SYonghong Song err = bpf_get_kprobe_info(event, fd_type, buf,
243899bf10e9SJiri Olsa probe_offset, probe_addr, missed,
243941bdc4b4SYonghong Song event->attr.type == PERF_TYPE_TRACEPOINT);
244041bdc4b4SYonghong Song #endif
244141bdc4b4SYonghong Song #ifdef CONFIG_UPROBE_EVENTS
244241bdc4b4SYonghong Song if (flags & TRACE_EVENT_FL_UPROBE)
244341bdc4b4SYonghong Song err = bpf_get_uprobe_info(event, fd_type, buf,
24445125e757SYafang Shao probe_offset, probe_addr,
244541bdc4b4SYonghong Song event->attr.type == PERF_TYPE_TRACEPOINT);
244641bdc4b4SYonghong Song #endif
244741bdc4b4SYonghong Song }
244841bdc4b4SYonghong Song
244941bdc4b4SYonghong Song return err;
245041bdc4b4SYonghong Song }
2451a38d1107SMatt Mullins
send_signal_irq_work_init(void)24529db1ff0aSYonghong Song static int __init send_signal_irq_work_init(void)
24539db1ff0aSYonghong Song {
24549db1ff0aSYonghong Song int cpu;
24559db1ff0aSYonghong Song struct send_signal_irq_work *work;
24569db1ff0aSYonghong Song
24579db1ff0aSYonghong Song for_each_possible_cpu(cpu) {
24589db1ff0aSYonghong Song work = per_cpu_ptr(&send_signal_work, cpu);
24599db1ff0aSYonghong Song init_irq_work(&work->irq_work, do_bpf_send_signal);
24609db1ff0aSYonghong Song }
24619db1ff0aSYonghong Song return 0;
24629db1ff0aSYonghong Song }
24639db1ff0aSYonghong Song
24649db1ff0aSYonghong Song subsys_initcall(send_signal_irq_work_init);
24659db1ff0aSYonghong Song
2466a38d1107SMatt Mullins #ifdef CONFIG_MODULES
bpf_event_notify(struct notifier_block * nb,unsigned long op,void * module)2467390e99cfSStanislav Fomichev static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2468390e99cfSStanislav Fomichev void *module)
2469a38d1107SMatt Mullins {
2470a38d1107SMatt Mullins struct bpf_trace_module *btm, *tmp;
2471a38d1107SMatt Mullins struct module *mod = module;
24720340a6b7SPeter Zijlstra int ret = 0;
2473a38d1107SMatt Mullins
2474a38d1107SMatt Mullins if (mod->num_bpf_raw_events == 0 ||
2475a38d1107SMatt Mullins (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
24760340a6b7SPeter Zijlstra goto out;
2477a38d1107SMatt Mullins
2478a38d1107SMatt Mullins mutex_lock(&bpf_module_mutex);
2479a38d1107SMatt Mullins
2480a38d1107SMatt Mullins switch (op) {
2481a38d1107SMatt Mullins case MODULE_STATE_COMING:
2482a38d1107SMatt Mullins btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2483a38d1107SMatt Mullins if (btm) {
2484a38d1107SMatt Mullins btm->module = module;
2485a38d1107SMatt Mullins list_add(&btm->list, &bpf_trace_modules);
24860340a6b7SPeter Zijlstra } else {
24870340a6b7SPeter Zijlstra ret = -ENOMEM;
2488a38d1107SMatt Mullins }
2489a38d1107SMatt Mullins break;
2490a38d1107SMatt Mullins case MODULE_STATE_GOING:
2491a38d1107SMatt Mullins list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2492a38d1107SMatt Mullins if (btm->module == module) {
2493a38d1107SMatt Mullins list_del(&btm->list);
2494a38d1107SMatt Mullins kfree(btm);
2495a38d1107SMatt Mullins break;
2496a38d1107SMatt Mullins }
2497a38d1107SMatt Mullins }
2498a38d1107SMatt Mullins break;
2499a38d1107SMatt Mullins }
2500a38d1107SMatt Mullins
2501a38d1107SMatt Mullins mutex_unlock(&bpf_module_mutex);
2502a38d1107SMatt Mullins
25030340a6b7SPeter Zijlstra out:
25040340a6b7SPeter Zijlstra return notifier_from_errno(ret);
2505a38d1107SMatt Mullins }
2506a38d1107SMatt Mullins
2507a38d1107SMatt Mullins static struct notifier_block bpf_module_nb = {
2508a38d1107SMatt Mullins .notifier_call = bpf_event_notify,
2509a38d1107SMatt Mullins };
2510a38d1107SMatt Mullins
bpf_event_init(void)2511390e99cfSStanislav Fomichev static int __init bpf_event_init(void)
2512a38d1107SMatt Mullins {
2513a38d1107SMatt Mullins register_module_notifier(&bpf_module_nb);
2514a38d1107SMatt Mullins return 0;
2515a38d1107SMatt Mullins }
2516a38d1107SMatt Mullins
2517a38d1107SMatt Mullins fs_initcall(bpf_event_init);
2518a38d1107SMatt Mullins #endif /* CONFIG_MODULES */
25190dcac272SJiri Olsa
25200dcac272SJiri Olsa #ifdef CONFIG_FPROBE
25210dcac272SJiri Olsa struct bpf_kprobe_multi_link {
25220dcac272SJiri Olsa struct bpf_link link;
25230dcac272SJiri Olsa struct fprobe fp;
25240dcac272SJiri Olsa unsigned long *addrs;
2525ca74823cSJiri Olsa u64 *cookies;
2526ca74823cSJiri Olsa u32 cnt;
2527e22061b2SJiri Olsa u32 mods_cnt;
2528e22061b2SJiri Olsa struct module **mods;
25297ac8d0d2SYafang Shao u32 flags;
25300dcac272SJiri Olsa };
25310dcac272SJiri Olsa
2532f7098690SJiri Olsa struct bpf_kprobe_multi_run_ctx {
2533f7098690SJiri Olsa struct bpf_run_ctx run_ctx;
2534f7098690SJiri Olsa struct bpf_kprobe_multi_link *link;
2535f7098690SJiri Olsa unsigned long entry_ip;
2536f7098690SJiri Olsa };
2537f7098690SJiri Olsa
25380236fec5SJiri Olsa struct user_syms {
25390236fec5SJiri Olsa const char **syms;
25400236fec5SJiri Olsa char *buf;
25410236fec5SJiri Olsa };
25420236fec5SJiri Olsa
copy_user_syms(struct user_syms * us,unsigned long __user * usyms,u32 cnt)25430236fec5SJiri Olsa static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
25440236fec5SJiri Olsa {
25450236fec5SJiri Olsa unsigned long __user usymbol;
25460236fec5SJiri Olsa const char **syms = NULL;
25470236fec5SJiri Olsa char *buf = NULL, *p;
25480236fec5SJiri Olsa int err = -ENOMEM;
25490236fec5SJiri Olsa unsigned int i;
25500236fec5SJiri Olsa
2551fd58f7dfSDan Carpenter syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
25520236fec5SJiri Olsa if (!syms)
25530236fec5SJiri Olsa goto error;
25540236fec5SJiri Olsa
2555fd58f7dfSDan Carpenter buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
25560236fec5SJiri Olsa if (!buf)
25570236fec5SJiri Olsa goto error;
25580236fec5SJiri Olsa
25590236fec5SJiri Olsa for (p = buf, i = 0; i < cnt; i++) {
25600236fec5SJiri Olsa if (__get_user(usymbol, usyms + i)) {
25610236fec5SJiri Olsa err = -EFAULT;
25620236fec5SJiri Olsa goto error;
25630236fec5SJiri Olsa }
25640236fec5SJiri Olsa err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
25650236fec5SJiri Olsa if (err == KSYM_NAME_LEN)
25660236fec5SJiri Olsa err = -E2BIG;
25670236fec5SJiri Olsa if (err < 0)
25680236fec5SJiri Olsa goto error;
25690236fec5SJiri Olsa syms[i] = p;
25700236fec5SJiri Olsa p += err + 1;
25710236fec5SJiri Olsa }
25720236fec5SJiri Olsa
25730236fec5SJiri Olsa us->syms = syms;
25740236fec5SJiri Olsa us->buf = buf;
25750236fec5SJiri Olsa return 0;
25760236fec5SJiri Olsa
25770236fec5SJiri Olsa error:
25780236fec5SJiri Olsa if (err) {
25790236fec5SJiri Olsa kvfree(syms);
25800236fec5SJiri Olsa kvfree(buf);
25810236fec5SJiri Olsa }
25820236fec5SJiri Olsa return err;
25830236fec5SJiri Olsa }
25840236fec5SJiri Olsa
kprobe_multi_put_modules(struct module ** mods,u32 cnt)2585e22061b2SJiri Olsa static void kprobe_multi_put_modules(struct module **mods, u32 cnt)
2586e22061b2SJiri Olsa {
2587e22061b2SJiri Olsa u32 i;
2588e22061b2SJiri Olsa
2589e22061b2SJiri Olsa for (i = 0; i < cnt; i++)
2590e22061b2SJiri Olsa module_put(mods[i]);
2591e22061b2SJiri Olsa }
2592e22061b2SJiri Olsa
free_user_syms(struct user_syms * us)25930236fec5SJiri Olsa static void free_user_syms(struct user_syms *us)
25940236fec5SJiri Olsa {
25950236fec5SJiri Olsa kvfree(us->syms);
25960236fec5SJiri Olsa kvfree(us->buf);
25970236fec5SJiri Olsa }
25980236fec5SJiri Olsa
bpf_kprobe_multi_link_release(struct bpf_link * link)25990dcac272SJiri Olsa static void bpf_kprobe_multi_link_release(struct bpf_link *link)
26000dcac272SJiri Olsa {
26010dcac272SJiri Olsa struct bpf_kprobe_multi_link *kmulti_link;
26020dcac272SJiri Olsa
26030dcac272SJiri Olsa kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
26040dcac272SJiri Olsa unregister_fprobe(&kmulti_link->fp);
2605e22061b2SJiri Olsa kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt);
26060dcac272SJiri Olsa }
26070dcac272SJiri Olsa
bpf_kprobe_multi_link_dealloc(struct bpf_link * link)26080dcac272SJiri Olsa static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
26090dcac272SJiri Olsa {
26100dcac272SJiri Olsa struct bpf_kprobe_multi_link *kmulti_link;
26110dcac272SJiri Olsa
26120dcac272SJiri Olsa kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
26130dcac272SJiri Olsa kvfree(kmulti_link->addrs);
2614ca74823cSJiri Olsa kvfree(kmulti_link->cookies);
2615e22061b2SJiri Olsa kfree(kmulti_link->mods);
26160dcac272SJiri Olsa kfree(kmulti_link);
26170dcac272SJiri Olsa }
26180dcac272SJiri Olsa
bpf_kprobe_multi_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)26197ac8d0d2SYafang Shao static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
26207ac8d0d2SYafang Shao struct bpf_link_info *info)
26217ac8d0d2SYafang Shao {
26227ac8d0d2SYafang Shao u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
26237ac8d0d2SYafang Shao struct bpf_kprobe_multi_link *kmulti_link;
26247ac8d0d2SYafang Shao u32 ucount = info->kprobe_multi.count;
26257ac8d0d2SYafang Shao int err = 0, i;
26267ac8d0d2SYafang Shao
26277ac8d0d2SYafang Shao if (!uaddrs ^ !ucount)
26287ac8d0d2SYafang Shao return -EINVAL;
26297ac8d0d2SYafang Shao
26307ac8d0d2SYafang Shao kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
26317ac8d0d2SYafang Shao info->kprobe_multi.count = kmulti_link->cnt;
26327ac8d0d2SYafang Shao info->kprobe_multi.flags = kmulti_link->flags;
26337ac8d0d2SYafang Shao
26347ac8d0d2SYafang Shao if (!uaddrs)
26357ac8d0d2SYafang Shao return 0;
26367ac8d0d2SYafang Shao if (ucount < kmulti_link->cnt)
26377ac8d0d2SYafang Shao err = -ENOSPC;
26387ac8d0d2SYafang Shao else
26397ac8d0d2SYafang Shao ucount = kmulti_link->cnt;
26407ac8d0d2SYafang Shao
26417ac8d0d2SYafang Shao if (kallsyms_show_value(current_cred())) {
26427ac8d0d2SYafang Shao if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
26437ac8d0d2SYafang Shao return -EFAULT;
26447ac8d0d2SYafang Shao } else {
26457ac8d0d2SYafang Shao for (i = 0; i < ucount; i++) {
26467ac8d0d2SYafang Shao if (put_user(0, uaddrs + i))
26477ac8d0d2SYafang Shao return -EFAULT;
26487ac8d0d2SYafang Shao }
26497ac8d0d2SYafang Shao }
26507ac8d0d2SYafang Shao return err;
26517ac8d0d2SYafang Shao }
26527ac8d0d2SYafang Shao
26530dcac272SJiri Olsa static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
26540dcac272SJiri Olsa .release = bpf_kprobe_multi_link_release,
2655876941f5SAndrii Nakryiko .dealloc_deferred = bpf_kprobe_multi_link_dealloc,
26567ac8d0d2SYafang Shao .fill_link_info = bpf_kprobe_multi_link_fill_link_info,
26570dcac272SJiri Olsa };
26580dcac272SJiri Olsa
bpf_kprobe_multi_cookie_swap(void * a,void * b,int size,const void * priv)2659ca74823cSJiri Olsa static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2660ca74823cSJiri Olsa {
2661ca74823cSJiri Olsa const struct bpf_kprobe_multi_link *link = priv;
2662ca74823cSJiri Olsa unsigned long *addr_a = a, *addr_b = b;
2663ca74823cSJiri Olsa u64 *cookie_a, *cookie_b;
2664ca74823cSJiri Olsa
2665ca74823cSJiri Olsa cookie_a = link->cookies + (addr_a - link->addrs);
2666ca74823cSJiri Olsa cookie_b = link->cookies + (addr_b - link->addrs);
2667ca74823cSJiri Olsa
2668ca74823cSJiri Olsa /* swap addr_a/addr_b and cookie_a/cookie_b values */
266911e17ae4SJiapeng Chong swap(*addr_a, *addr_b);
267011e17ae4SJiapeng Chong swap(*cookie_a, *cookie_b);
2671ca74823cSJiri Olsa }
2672ca74823cSJiri Olsa
bpf_kprobe_multi_addrs_cmp(const void * a,const void * b)26731a1b0716SJiri Olsa static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b)
2674ca74823cSJiri Olsa {
2675ca74823cSJiri Olsa const unsigned long *addr_a = a, *addr_b = b;
2676ca74823cSJiri Olsa
2677ca74823cSJiri Olsa if (*addr_a == *addr_b)
2678ca74823cSJiri Olsa return 0;
2679ca74823cSJiri Olsa return *addr_a < *addr_b ? -1 : 1;
2680ca74823cSJiri Olsa }
2681ca74823cSJiri Olsa
bpf_kprobe_multi_cookie_cmp(const void * a,const void * b,const void * priv)2682ca74823cSJiri Olsa static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2683ca74823cSJiri Olsa {
26841a1b0716SJiri Olsa return bpf_kprobe_multi_addrs_cmp(a, b);
2685ca74823cSJiri Olsa }
2686ca74823cSJiri Olsa
bpf_kprobe_multi_cookie(struct bpf_run_ctx * ctx)2687f7098690SJiri Olsa static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2688ca74823cSJiri Olsa {
2689f7098690SJiri Olsa struct bpf_kprobe_multi_run_ctx *run_ctx;
2690ca74823cSJiri Olsa struct bpf_kprobe_multi_link *link;
2691f7098690SJiri Olsa u64 *cookie, entry_ip;
2692ca74823cSJiri Olsa unsigned long *addr;
2693ca74823cSJiri Olsa
2694ca74823cSJiri Olsa if (WARN_ON_ONCE(!ctx))
2695ca74823cSJiri Olsa return 0;
2696f7098690SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2697f7098690SJiri Olsa link = run_ctx->link;
2698ca74823cSJiri Olsa if (!link->cookies)
2699ca74823cSJiri Olsa return 0;
2700f7098690SJiri Olsa entry_ip = run_ctx->entry_ip;
2701f7098690SJiri Olsa addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
27021a1b0716SJiri Olsa bpf_kprobe_multi_addrs_cmp);
2703ca74823cSJiri Olsa if (!addr)
2704ca74823cSJiri Olsa return 0;
2705ca74823cSJiri Olsa cookie = link->cookies + (addr - link->addrs);
2706ca74823cSJiri Olsa return *cookie;
2707ca74823cSJiri Olsa }
2708ca74823cSJiri Olsa
bpf_kprobe_multi_entry_ip(struct bpf_run_ctx * ctx)2709f7098690SJiri Olsa static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2710f7098690SJiri Olsa {
2711f7098690SJiri Olsa struct bpf_kprobe_multi_run_ctx *run_ctx;
2712f7098690SJiri Olsa
2713f7098690SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2714f7098690SJiri Olsa return run_ctx->entry_ip;
2715f7098690SJiri Olsa }
2716f7098690SJiri Olsa
27170dcac272SJiri Olsa static int
kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link * link,unsigned long entry_ip,struct pt_regs * regs)27180dcac272SJiri Olsa kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2719f7098690SJiri Olsa unsigned long entry_ip, struct pt_regs *regs)
27200dcac272SJiri Olsa {
2721f7098690SJiri Olsa struct bpf_kprobe_multi_run_ctx run_ctx = {
2722f7098690SJiri Olsa .link = link,
2723f7098690SJiri Olsa .entry_ip = entry_ip,
2724f7098690SJiri Olsa };
2725ca74823cSJiri Olsa struct bpf_run_ctx *old_run_ctx;
27260dcac272SJiri Olsa int err;
27270dcac272SJiri Olsa
27280dcac272SJiri Olsa if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
27290dcac272SJiri Olsa err = 0;
27300dcac272SJiri Olsa goto out;
27310dcac272SJiri Olsa }
27320dcac272SJiri Olsa
27330dcac272SJiri Olsa migrate_disable();
27340dcac272SJiri Olsa rcu_read_lock();
2735f7098690SJiri Olsa old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
27360dcac272SJiri Olsa err = bpf_prog_run(link->link.prog, regs);
2737ca74823cSJiri Olsa bpf_reset_run_ctx(old_run_ctx);
27380dcac272SJiri Olsa rcu_read_unlock();
27390dcac272SJiri Olsa migrate_enable();
27400dcac272SJiri Olsa
27410dcac272SJiri Olsa out:
27420dcac272SJiri Olsa __this_cpu_dec(bpf_prog_active);
27430dcac272SJiri Olsa return err;
27440dcac272SJiri Olsa }
27450dcac272SJiri Olsa
274639d95420SMasami Hiramatsu (Google) static int
kprobe_multi_link_handler(struct fprobe * fp,unsigned long fentry_ip,unsigned long ret_ip,struct pt_regs * regs,void * data)2747c09eb2e5SJiri Olsa kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
2748cb16330dSMasami Hiramatsu (Google) unsigned long ret_ip, struct pt_regs *regs,
2749cb16330dSMasami Hiramatsu (Google) void *data)
27500dcac272SJiri Olsa {
27510dcac272SJiri Olsa struct bpf_kprobe_multi_link *link;
27520dcac272SJiri Olsa
27530dcac272SJiri Olsa link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2754c09eb2e5SJiri Olsa kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
275539d95420SMasami Hiramatsu (Google) return 0;
275639d95420SMasami Hiramatsu (Google) }
275739d95420SMasami Hiramatsu (Google)
275839d95420SMasami Hiramatsu (Google) static void
kprobe_multi_link_exit_handler(struct fprobe * fp,unsigned long fentry_ip,unsigned long ret_ip,struct pt_regs * regs,void * data)275939d95420SMasami Hiramatsu (Google) kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
2760cb16330dSMasami Hiramatsu (Google) unsigned long ret_ip, struct pt_regs *regs,
2761cb16330dSMasami Hiramatsu (Google) void *data)
2762eb5fb032SJiri Olsa {
2763eb5fb032SJiri Olsa struct bpf_kprobe_multi_link *link;
2764eb5fb032SJiri Olsa
2765eb5fb032SJiri Olsa link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2766eb5fb032SJiri Olsa kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
2767eb5fb032SJiri Olsa }
2768eb5fb032SJiri Olsa
symbols_cmp_r(const void * a,const void * b,const void * priv)2769eb5fb032SJiri Olsa static int symbols_cmp_r(const void *a, const void *b, const void *priv)
2770eb5fb032SJiri Olsa {
2771eb5fb032SJiri Olsa const char **str_a = (const char **) a;
2772eb5fb032SJiri Olsa const char **str_b = (const char **) b;
2773eb5fb032SJiri Olsa
2774eb5fb032SJiri Olsa return strcmp(*str_a, *str_b);
2775eb5fb032SJiri Olsa }
2776eb5fb032SJiri Olsa
2777eb5fb032SJiri Olsa struct multi_symbols_sort {
2778eb5fb032SJiri Olsa const char **funcs;
2779eb5fb032SJiri Olsa u64 *cookies;
2780eb5fb032SJiri Olsa };
2781eb5fb032SJiri Olsa
symbols_swap_r(void * a,void * b,int size,const void * priv)2782eb5fb032SJiri Olsa static void symbols_swap_r(void *a, void *b, int size, const void *priv)
27830dcac272SJiri Olsa {
27840dcac272SJiri Olsa const struct multi_symbols_sort *data = priv;
27850dcac272SJiri Olsa const char **name_a = a, **name_b = b;
27860dcac272SJiri Olsa
2787ca74823cSJiri Olsa swap(*name_a, *name_b);
27880dcac272SJiri Olsa
27890dcac272SJiri Olsa /* If defined, swap also related cookies. */
27900dcac272SJiri Olsa if (data->cookies) {
2791ca74823cSJiri Olsa u64 *cookie_a, *cookie_b;
27920dcac272SJiri Olsa
27930dcac272SJiri Olsa cookie_a = data->cookies + (name_a - data->funcs);
27940dcac272SJiri Olsa cookie_b = data->cookies + (name_b - data->funcs);
27950dcac272SJiri Olsa swap(*cookie_a, *cookie_b);
27960dcac272SJiri Olsa }
27970dcac272SJiri Olsa }
27980dcac272SJiri Olsa
27996a5f2d6eSJiri Olsa struct modules_array {
2800e22061b2SJiri Olsa struct module **mods;
2801e22061b2SJiri Olsa int mods_cnt;
2802e22061b2SJiri Olsa int mods_cap;
2803e22061b2SJiri Olsa };
2804e22061b2SJiri Olsa
add_module(struct modules_array * arr,struct module * mod)28056a5f2d6eSJiri Olsa static int add_module(struct modules_array *arr, struct module *mod)
2806e22061b2SJiri Olsa {
2807e22061b2SJiri Olsa struct module **mods;
2808e22061b2SJiri Olsa
28096a5f2d6eSJiri Olsa if (arr->mods_cnt == arr->mods_cap) {
28106a5f2d6eSJiri Olsa arr->mods_cap = max(16, arr->mods_cap * 3 / 2);
28116a5f2d6eSJiri Olsa mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL);
2812e22061b2SJiri Olsa if (!mods)
2813e22061b2SJiri Olsa return -ENOMEM;
28146a5f2d6eSJiri Olsa arr->mods = mods;
2815e22061b2SJiri Olsa }
2816e22061b2SJiri Olsa
28176a5f2d6eSJiri Olsa arr->mods[arr->mods_cnt] = mod;
28186a5f2d6eSJiri Olsa arr->mods_cnt++;
2819e22061b2SJiri Olsa return 0;
2820e22061b2SJiri Olsa }
2821e22061b2SJiri Olsa
has_module(struct modules_array * arr,struct module * mod)28226a5f2d6eSJiri Olsa static bool has_module(struct modules_array *arr, struct module *mod)
28236a5f2d6eSJiri Olsa {
28246a5f2d6eSJiri Olsa int i;
28256a5f2d6eSJiri Olsa
28266a5f2d6eSJiri Olsa for (i = arr->mods_cnt - 1; i >= 0; i--) {
28276a5f2d6eSJiri Olsa if (arr->mods[i] == mod)
28286a5f2d6eSJiri Olsa return true;
28296a5f2d6eSJiri Olsa }
28306a5f2d6eSJiri Olsa return false;
28316a5f2d6eSJiri Olsa }
28326a5f2d6eSJiri Olsa
get_modules_for_addrs(struct module *** mods,unsigned long * addrs,u32 addrs_cnt)2833e22061b2SJiri Olsa static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt)
2834e22061b2SJiri Olsa {
28356a5f2d6eSJiri Olsa struct modules_array arr = {};
28366a5f2d6eSJiri Olsa u32 i, err = 0;
28376a5f2d6eSJiri Olsa
28386a5f2d6eSJiri Olsa for (i = 0; i < addrs_cnt; i++) {
28396a5f2d6eSJiri Olsa struct module *mod;
28406a5f2d6eSJiri Olsa
28416a5f2d6eSJiri Olsa preempt_disable();
28426a5f2d6eSJiri Olsa mod = __module_address(addrs[i]);
28436a5f2d6eSJiri Olsa /* Either no module or we it's already stored */
28446a5f2d6eSJiri Olsa if (!mod || has_module(&arr, mod)) {
28456a5f2d6eSJiri Olsa preempt_enable();
28466a5f2d6eSJiri Olsa continue;
28476a5f2d6eSJiri Olsa }
28486a5f2d6eSJiri Olsa if (!try_module_get(mod))
28496a5f2d6eSJiri Olsa err = -EINVAL;
28506a5f2d6eSJiri Olsa preempt_enable();
28516a5f2d6eSJiri Olsa if (err)
28526a5f2d6eSJiri Olsa break;
28536a5f2d6eSJiri Olsa err = add_module(&arr, mod);
28546a5f2d6eSJiri Olsa if (err) {
28556a5f2d6eSJiri Olsa module_put(mod);
28566a5f2d6eSJiri Olsa break;
28576a5f2d6eSJiri Olsa }
28586a5f2d6eSJiri Olsa }
2859e22061b2SJiri Olsa
2860e22061b2SJiri Olsa /* We return either err < 0 in case of error, ... */
2861e22061b2SJiri Olsa if (err) {
28626a5f2d6eSJiri Olsa kprobe_multi_put_modules(arr.mods, arr.mods_cnt);
28636a5f2d6eSJiri Olsa kfree(arr.mods);
2864e22061b2SJiri Olsa return err;
2865e22061b2SJiri Olsa }
2866e22061b2SJiri Olsa
2867e22061b2SJiri Olsa /* or number of modules found if everything is ok. */
28686a5f2d6eSJiri Olsa *mods = arr.mods;
28696a5f2d6eSJiri Olsa return arr.mods_cnt;
2870e22061b2SJiri Olsa }
2871e22061b2SJiri Olsa
addrs_check_error_injection_list(unsigned long * addrs,u32 cnt)287241bc46c1SJiri Olsa static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
287341bc46c1SJiri Olsa {
287441bc46c1SJiri Olsa u32 i;
287541bc46c1SJiri Olsa
287641bc46c1SJiri Olsa for (i = 0; i < cnt; i++) {
287741bc46c1SJiri Olsa if (!within_error_injection_list(addrs[i]))
287841bc46c1SJiri Olsa return -EINVAL;
287941bc46c1SJiri Olsa }
288041bc46c1SJiri Olsa return 0;
288141bc46c1SJiri Olsa }
288241bc46c1SJiri Olsa
bpf_kprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)28830dcac272SJiri Olsa int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
28840dcac272SJiri Olsa {
28850dcac272SJiri Olsa struct bpf_kprobe_multi_link *link = NULL;
28860dcac272SJiri Olsa struct bpf_link_primer link_primer;
28870dcac272SJiri Olsa void __user *ucookies;
28880dcac272SJiri Olsa unsigned long *addrs;
28890dcac272SJiri Olsa u32 flags, cnt, size;
28900dcac272SJiri Olsa void __user *uaddrs;
28910dcac272SJiri Olsa u64 *cookies = NULL;
28920dcac272SJiri Olsa void __user *usyms;
28930dcac272SJiri Olsa int err;
28940dcac272SJiri Olsa
28950dcac272SJiri Olsa /* no support for 32bit archs yet */
28960dcac272SJiri Olsa if (sizeof(u64) != sizeof(void *))
28970dcac272SJiri Olsa return -EOPNOTSUPP;
28980dcac272SJiri Olsa
28990dcac272SJiri Olsa if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
29000dcac272SJiri Olsa return -EINVAL;
29010dcac272SJiri Olsa
29020dcac272SJiri Olsa flags = attr->link_create.kprobe_multi.flags;
29030dcac272SJiri Olsa if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
29040dcac272SJiri Olsa return -EINVAL;
29050dcac272SJiri Olsa
29060dcac272SJiri Olsa uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
29070dcac272SJiri Olsa usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
29080dcac272SJiri Olsa if (!!uaddrs == !!usyms)
29090dcac272SJiri Olsa return -EINVAL;
29100dcac272SJiri Olsa
29110dcac272SJiri Olsa cnt = attr->link_create.kprobe_multi.cnt;
29120dcac272SJiri Olsa if (!cnt)
29130dcac272SJiri Olsa return -EINVAL;
29143d83b820SHou Tao if (cnt > MAX_KPROBE_MULTI_CNT)
29153d83b820SHou Tao return -E2BIG;
29160dcac272SJiri Olsa
29170dcac272SJiri Olsa size = cnt * sizeof(*addrs);
2918fd58f7dfSDan Carpenter addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
29190dcac272SJiri Olsa if (!addrs)
29200dcac272SJiri Olsa return -ENOMEM;
29210dcac272SJiri Olsa
2922ca74823cSJiri Olsa ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2923ca74823cSJiri Olsa if (ucookies) {
2924fd58f7dfSDan Carpenter cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2925ca74823cSJiri Olsa if (!cookies) {
2926ca74823cSJiri Olsa err = -ENOMEM;
2927ca74823cSJiri Olsa goto error;
2928ca74823cSJiri Olsa }
2929ca74823cSJiri Olsa if (copy_from_user(cookies, ucookies, size)) {
2930ca74823cSJiri Olsa err = -EFAULT;
2931ca74823cSJiri Olsa goto error;
2932ca74823cSJiri Olsa }
2933ca74823cSJiri Olsa }
2934ca74823cSJiri Olsa
2935eb5fb032SJiri Olsa if (uaddrs) {
2936eb5fb032SJiri Olsa if (copy_from_user(addrs, uaddrs, size)) {
2937eb5fb032SJiri Olsa err = -EFAULT;
2938eb5fb032SJiri Olsa goto error;
2939eb5fb032SJiri Olsa }
2940eb5fb032SJiri Olsa } else {
2941eb5fb032SJiri Olsa struct multi_symbols_sort data = {
2942eb5fb032SJiri Olsa .cookies = cookies,
2943eb5fb032SJiri Olsa };
2944eb5fb032SJiri Olsa struct user_syms us;
2945eb5fb032SJiri Olsa
2946eb5fb032SJiri Olsa err = copy_user_syms(&us, usyms, cnt);
2947eb5fb032SJiri Olsa if (err)
2948eb5fb032SJiri Olsa goto error;
2949eb5fb032SJiri Olsa
2950eb5fb032SJiri Olsa if (cookies)
2951eb5fb032SJiri Olsa data.funcs = us.syms;
2952eb5fb032SJiri Olsa
2953eb5fb032SJiri Olsa sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
2954eb5fb032SJiri Olsa symbols_swap_r, &data);
2955eb5fb032SJiri Olsa
2956eb5fb032SJiri Olsa err = ftrace_lookup_symbols(us.syms, cnt, addrs);
2957eb5fb032SJiri Olsa free_user_syms(&us);
2958eb5fb032SJiri Olsa if (err)
2959eb5fb032SJiri Olsa goto error;
2960eb5fb032SJiri Olsa }
2961eb5fb032SJiri Olsa
296241bc46c1SJiri Olsa if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) {
296341bc46c1SJiri Olsa err = -EINVAL;
296441bc46c1SJiri Olsa goto error;
296541bc46c1SJiri Olsa }
296641bc46c1SJiri Olsa
29670dcac272SJiri Olsa link = kzalloc(sizeof(*link), GFP_KERNEL);
29680dcac272SJiri Olsa if (!link) {
29690dcac272SJiri Olsa err = -ENOMEM;
29700dcac272SJiri Olsa goto error;
29710dcac272SJiri Olsa }
29720dcac272SJiri Olsa
29730dcac272SJiri Olsa bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
29740dcac272SJiri Olsa &bpf_kprobe_multi_link_lops, prog);
29750dcac272SJiri Olsa
29760dcac272SJiri Olsa err = bpf_link_prime(&link->link, &link_primer);
29770dcac272SJiri Olsa if (err)
29780dcac272SJiri Olsa goto error;
29790dcac272SJiri Olsa
29800dcac272SJiri Olsa if (flags & BPF_F_KPROBE_MULTI_RETURN)
298139d95420SMasami Hiramatsu (Google) link->fp.exit_handler = kprobe_multi_link_exit_handler;
29820dcac272SJiri Olsa else
29830dcac272SJiri Olsa link->fp.entry_handler = kprobe_multi_link_handler;
29840dcac272SJiri Olsa
29850dcac272SJiri Olsa link->addrs = addrs;
2986ca74823cSJiri Olsa link->cookies = cookies;
2987ca74823cSJiri Olsa link->cnt = cnt;
29887ac8d0d2SYafang Shao link->flags = flags;
2989ca74823cSJiri Olsa
2990ca74823cSJiri Olsa if (cookies) {
2991ca74823cSJiri Olsa /*
2992ca74823cSJiri Olsa * Sorting addresses will trigger sorting cookies as well
2993ca74823cSJiri Olsa * (check bpf_kprobe_multi_cookie_swap). This way we can
2994ca74823cSJiri Olsa * find cookie based on the address in bpf_get_attach_cookie
2995ca74823cSJiri Olsa * helper.
2996ca74823cSJiri Olsa */
2997ca74823cSJiri Olsa sort_r(addrs, cnt, sizeof(*addrs),
2998ca74823cSJiri Olsa bpf_kprobe_multi_cookie_cmp,
2999ca74823cSJiri Olsa bpf_kprobe_multi_cookie_swap,
3000ca74823cSJiri Olsa link);
3001ca74823cSJiri Olsa }
30020dcac272SJiri Olsa
3003e22061b2SJiri Olsa err = get_modules_for_addrs(&link->mods, addrs, cnt);
3004e22061b2SJiri Olsa if (err < 0) {
3005e22061b2SJiri Olsa bpf_link_cleanup(&link_primer);
3006e22061b2SJiri Olsa return err;
3007e22061b2SJiri Olsa }
3008e22061b2SJiri Olsa link->mods_cnt = err;
3009e22061b2SJiri Olsa
30100dcac272SJiri Olsa err = register_fprobe_ips(&link->fp, addrs, cnt);
30110dcac272SJiri Olsa if (err) {
3012e22061b2SJiri Olsa kprobe_multi_put_modules(link->mods, link->mods_cnt);
30130dcac272SJiri Olsa bpf_link_cleanup(&link_primer);
30140dcac272SJiri Olsa return err;
30150dcac272SJiri Olsa }
30160dcac272SJiri Olsa
30170dcac272SJiri Olsa return bpf_link_settle(&link_primer);
30180dcac272SJiri Olsa
30190dcac272SJiri Olsa error:
30200dcac272SJiri Olsa kfree(link);
30210dcac272SJiri Olsa kvfree(addrs);
3022ca74823cSJiri Olsa kvfree(cookies);
30230dcac272SJiri Olsa return err;
30240dcac272SJiri Olsa }
30250dcac272SJiri Olsa #else /* !CONFIG_FPROBE */
bpf_kprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)30260dcac272SJiri Olsa int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
30270dcac272SJiri Olsa {
30280dcac272SJiri Olsa return -EOPNOTSUPP;
30290dcac272SJiri Olsa }
bpf_kprobe_multi_cookie(struct bpf_run_ctx * ctx)3030f7098690SJiri Olsa static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
3031f7098690SJiri Olsa {
3032f7098690SJiri Olsa return 0;
3033f7098690SJiri Olsa }
bpf_kprobe_multi_entry_ip(struct bpf_run_ctx * ctx)3034f7098690SJiri Olsa static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3035ca74823cSJiri Olsa {
3036ca74823cSJiri Olsa return 0;
3037ca74823cSJiri Olsa }
30380dcac272SJiri Olsa #endif
303989ae89f5SJiri Olsa
304089ae89f5SJiri Olsa #ifdef CONFIG_UPROBES
304189ae89f5SJiri Olsa struct bpf_uprobe_multi_link;
304289ae89f5SJiri Olsa
304389ae89f5SJiri Olsa struct bpf_uprobe {
304489ae89f5SJiri Olsa struct bpf_uprobe_multi_link *link;
304589ae89f5SJiri Olsa loff_t offset;
30465aa03dd3SJiri Olsa unsigned long ref_ctr_offset;
30470b779b61SJiri Olsa u64 cookie;
304889ae89f5SJiri Olsa struct uprobe_consumer consumer;
304989ae89f5SJiri Olsa };
305089ae89f5SJiri Olsa
305189ae89f5SJiri Olsa struct bpf_uprobe_multi_link {
305289ae89f5SJiri Olsa struct path path;
305389ae89f5SJiri Olsa struct bpf_link link;
305489ae89f5SJiri Olsa u32 cnt;
305589ae89f5SJiri Olsa struct bpf_uprobe *uprobes;
3056b733eeadSJiri Olsa struct task_struct *task;
305789ae89f5SJiri Olsa };
305889ae89f5SJiri Olsa
305989ae89f5SJiri Olsa struct bpf_uprobe_multi_run_ctx {
306089ae89f5SJiri Olsa struct bpf_run_ctx run_ctx;
306189ae89f5SJiri Olsa unsigned long entry_ip;
30620b779b61SJiri Olsa struct bpf_uprobe *uprobe;
306389ae89f5SJiri Olsa };
306489ae89f5SJiri Olsa
bpf_uprobe_unregister(struct path * path,struct bpf_uprobe * uprobes,u32 cnt)306589ae89f5SJiri Olsa static void bpf_uprobe_unregister(struct path *path, struct bpf_uprobe *uprobes,
306689ae89f5SJiri Olsa u32 cnt)
306789ae89f5SJiri Olsa {
306889ae89f5SJiri Olsa u32 i;
306989ae89f5SJiri Olsa
307089ae89f5SJiri Olsa for (i = 0; i < cnt; i++) {
307189ae89f5SJiri Olsa uprobe_unregister(d_real_inode(path->dentry), uprobes[i].offset,
307289ae89f5SJiri Olsa &uprobes[i].consumer);
307389ae89f5SJiri Olsa }
307489ae89f5SJiri Olsa }
307589ae89f5SJiri Olsa
bpf_uprobe_multi_link_release(struct bpf_link * link)307689ae89f5SJiri Olsa static void bpf_uprobe_multi_link_release(struct bpf_link *link)
307789ae89f5SJiri Olsa {
307889ae89f5SJiri Olsa struct bpf_uprobe_multi_link *umulti_link;
307989ae89f5SJiri Olsa
308089ae89f5SJiri Olsa umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
308189ae89f5SJiri Olsa bpf_uprobe_unregister(&umulti_link->path, umulti_link->uprobes, umulti_link->cnt);
3082771690b7SAndrii Nakryiko if (umulti_link->task)
3083771690b7SAndrii Nakryiko put_task_struct(umulti_link->task);
3084771690b7SAndrii Nakryiko path_put(&umulti_link->path);
308589ae89f5SJiri Olsa }
308689ae89f5SJiri Olsa
bpf_uprobe_multi_link_dealloc(struct bpf_link * link)308789ae89f5SJiri Olsa static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link)
308889ae89f5SJiri Olsa {
308989ae89f5SJiri Olsa struct bpf_uprobe_multi_link *umulti_link;
309089ae89f5SJiri Olsa
309189ae89f5SJiri Olsa umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
309289ae89f5SJiri Olsa kvfree(umulti_link->uprobes);
309389ae89f5SJiri Olsa kfree(umulti_link);
309489ae89f5SJiri Olsa }
309589ae89f5SJiri Olsa
309689ae89f5SJiri Olsa static const struct bpf_link_ops bpf_uprobe_multi_link_lops = {
309789ae89f5SJiri Olsa .release = bpf_uprobe_multi_link_release,
3098876941f5SAndrii Nakryiko .dealloc_deferred = bpf_uprobe_multi_link_dealloc,
309989ae89f5SJiri Olsa };
310089ae89f5SJiri Olsa
uprobe_prog_run(struct bpf_uprobe * uprobe,unsigned long entry_ip,struct pt_regs * regs)310189ae89f5SJiri Olsa static int uprobe_prog_run(struct bpf_uprobe *uprobe,
310289ae89f5SJiri Olsa unsigned long entry_ip,
310389ae89f5SJiri Olsa struct pt_regs *regs)
310489ae89f5SJiri Olsa {
310589ae89f5SJiri Olsa struct bpf_uprobe_multi_link *link = uprobe->link;
310689ae89f5SJiri Olsa struct bpf_uprobe_multi_run_ctx run_ctx = {
310789ae89f5SJiri Olsa .entry_ip = entry_ip,
31080b779b61SJiri Olsa .uprobe = uprobe,
310989ae89f5SJiri Olsa };
311089ae89f5SJiri Olsa struct bpf_prog *prog = link->link.prog;
311189ae89f5SJiri Olsa bool sleepable = prog->aux->sleepable;
311289ae89f5SJiri Olsa struct bpf_run_ctx *old_run_ctx;
311389ae89f5SJiri Olsa
311439a143a2SAndrii Nakryiko if (link->task && current->mm != link->task->mm)
3115b733eeadSJiri Olsa return 0;
3116b733eeadSJiri Olsa
311789ae89f5SJiri Olsa if (sleepable)
311889ae89f5SJiri Olsa rcu_read_lock_trace();
311989ae89f5SJiri Olsa else
312089ae89f5SJiri Olsa rcu_read_lock();
312189ae89f5SJiri Olsa
312289ae89f5SJiri Olsa migrate_disable();
312389ae89f5SJiri Olsa
312489ae89f5SJiri Olsa old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
312589bf1b66SJiri Olsa bpf_prog_run(link->link.prog, regs);
312689ae89f5SJiri Olsa bpf_reset_run_ctx(old_run_ctx);
312789ae89f5SJiri Olsa
312889ae89f5SJiri Olsa migrate_enable();
312989ae89f5SJiri Olsa
313089ae89f5SJiri Olsa if (sleepable)
313189ae89f5SJiri Olsa rcu_read_unlock_trace();
313289ae89f5SJiri Olsa else
313389ae89f5SJiri Olsa rcu_read_unlock();
313489bf1b66SJiri Olsa return 0;
313589ae89f5SJiri Olsa }
313689ae89f5SJiri Olsa
3137b733eeadSJiri Olsa static bool
uprobe_multi_link_filter(struct uprobe_consumer * con,enum uprobe_filter_ctx ctx,struct mm_struct * mm)3138b733eeadSJiri Olsa uprobe_multi_link_filter(struct uprobe_consumer *con, enum uprobe_filter_ctx ctx,
3139b733eeadSJiri Olsa struct mm_struct *mm)
3140b733eeadSJiri Olsa {
3141b733eeadSJiri Olsa struct bpf_uprobe *uprobe;
3142b733eeadSJiri Olsa
3143b733eeadSJiri Olsa uprobe = container_of(con, struct bpf_uprobe, consumer);
3144b733eeadSJiri Olsa return uprobe->link->task->mm == mm;
3145b733eeadSJiri Olsa }
3146b733eeadSJiri Olsa
314789ae89f5SJiri Olsa static int
uprobe_multi_link_handler(struct uprobe_consumer * con,struct pt_regs * regs)314889ae89f5SJiri Olsa uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs)
314989ae89f5SJiri Olsa {
315089ae89f5SJiri Olsa struct bpf_uprobe *uprobe;
315189ae89f5SJiri Olsa
315289ae89f5SJiri Olsa uprobe = container_of(con, struct bpf_uprobe, consumer);
315389ae89f5SJiri Olsa return uprobe_prog_run(uprobe, instruction_pointer(regs), regs);
315489ae89f5SJiri Olsa }
315589ae89f5SJiri Olsa
315689ae89f5SJiri Olsa static int
uprobe_multi_link_ret_handler(struct uprobe_consumer * con,unsigned long func,struct pt_regs * regs)315789ae89f5SJiri Olsa uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs)
315889ae89f5SJiri Olsa {
315989ae89f5SJiri Olsa struct bpf_uprobe *uprobe;
316089ae89f5SJiri Olsa
316189ae89f5SJiri Olsa uprobe = container_of(con, struct bpf_uprobe, consumer);
316289ae89f5SJiri Olsa return uprobe_prog_run(uprobe, func, regs);
316389ae89f5SJiri Olsa }
316489ae89f5SJiri Olsa
bpf_uprobe_multi_entry_ip(struct bpf_run_ctx * ctx)3165686328d8SJiri Olsa static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3166686328d8SJiri Olsa {
3167686328d8SJiri Olsa struct bpf_uprobe_multi_run_ctx *run_ctx;
3168686328d8SJiri Olsa
3169686328d8SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
3170686328d8SJiri Olsa return run_ctx->entry_ip;
3171686328d8SJiri Olsa }
3172686328d8SJiri Olsa
bpf_uprobe_multi_cookie(struct bpf_run_ctx * ctx)31730b779b61SJiri Olsa static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
31740b779b61SJiri Olsa {
31750b779b61SJiri Olsa struct bpf_uprobe_multi_run_ctx *run_ctx;
31760b779b61SJiri Olsa
31770b779b61SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
31780b779b61SJiri Olsa return run_ctx->uprobe->cookie;
31790b779b61SJiri Olsa }
31800b779b61SJiri Olsa
bpf_uprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)318189ae89f5SJiri Olsa int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
318289ae89f5SJiri Olsa {
318389ae89f5SJiri Olsa struct bpf_uprobe_multi_link *link = NULL;
318489ae89f5SJiri Olsa unsigned long __user *uref_ctr_offsets;
318589ae89f5SJiri Olsa struct bpf_link_primer link_primer;
318689ae89f5SJiri Olsa struct bpf_uprobe *uprobes = NULL;
3187b733eeadSJiri Olsa struct task_struct *task = NULL;
318889ae89f5SJiri Olsa unsigned long __user *uoffsets;
31890b779b61SJiri Olsa u64 __user *ucookies;
319089ae89f5SJiri Olsa void __user *upath;
319189ae89f5SJiri Olsa u32 flags, cnt, i;
319289ae89f5SJiri Olsa struct path path;
319389ae89f5SJiri Olsa char *name;
3194b733eeadSJiri Olsa pid_t pid;
319589ae89f5SJiri Olsa int err;
319689ae89f5SJiri Olsa
319789ae89f5SJiri Olsa /* no support for 32bit archs yet */
319889ae89f5SJiri Olsa if (sizeof(u64) != sizeof(void *))
319989ae89f5SJiri Olsa return -EOPNOTSUPP;
320089ae89f5SJiri Olsa
320189ae89f5SJiri Olsa if (prog->expected_attach_type != BPF_TRACE_UPROBE_MULTI)
320289ae89f5SJiri Olsa return -EINVAL;
320389ae89f5SJiri Olsa
320489ae89f5SJiri Olsa flags = attr->link_create.uprobe_multi.flags;
320589ae89f5SJiri Olsa if (flags & ~BPF_F_UPROBE_MULTI_RETURN)
320689ae89f5SJiri Olsa return -EINVAL;
320789ae89f5SJiri Olsa
320889ae89f5SJiri Olsa /*
320989ae89f5SJiri Olsa * path, offsets and cnt are mandatory,
32100b779b61SJiri Olsa * ref_ctr_offsets and cookies are optional
321189ae89f5SJiri Olsa */
321289ae89f5SJiri Olsa upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path);
321389ae89f5SJiri Olsa uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets);
321489ae89f5SJiri Olsa cnt = attr->link_create.uprobe_multi.cnt;
321539a143a2SAndrii Nakryiko pid = attr->link_create.uprobe_multi.pid;
321689ae89f5SJiri Olsa
321739a143a2SAndrii Nakryiko if (!upath || !uoffsets || !cnt || pid < 0)
321889ae89f5SJiri Olsa return -EINVAL;
32195735054aSHou Tao if (cnt > MAX_UPROBE_MULTI_CNT)
32205735054aSHou Tao return -E2BIG;
322189ae89f5SJiri Olsa
322289ae89f5SJiri Olsa uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets);
32230b779b61SJiri Olsa ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies);
322489ae89f5SJiri Olsa
322589ae89f5SJiri Olsa name = strndup_user(upath, PATH_MAX);
322689ae89f5SJiri Olsa if (IS_ERR(name)) {
322789ae89f5SJiri Olsa err = PTR_ERR(name);
322889ae89f5SJiri Olsa return err;
322989ae89f5SJiri Olsa }
323089ae89f5SJiri Olsa
323189ae89f5SJiri Olsa err = kern_path(name, LOOKUP_FOLLOW, &path);
323289ae89f5SJiri Olsa kfree(name);
323389ae89f5SJiri Olsa if (err)
323489ae89f5SJiri Olsa return err;
323589ae89f5SJiri Olsa
323689ae89f5SJiri Olsa if (!d_is_reg(path.dentry)) {
323789ae89f5SJiri Olsa err = -EBADF;
323889ae89f5SJiri Olsa goto error_path_put;
323989ae89f5SJiri Olsa }
324089ae89f5SJiri Olsa
3241b733eeadSJiri Olsa if (pid) {
3242b733eeadSJiri Olsa rcu_read_lock();
324339a143a2SAndrii Nakryiko task = get_pid_task(find_vpid(pid), PIDTYPE_TGID);
3244b733eeadSJiri Olsa rcu_read_unlock();
324557eb5e1cSJiri Olsa if (!task) {
324657eb5e1cSJiri Olsa err = -ESRCH;
3247b733eeadSJiri Olsa goto error_path_put;
3248b733eeadSJiri Olsa }
324957eb5e1cSJiri Olsa }
3250b733eeadSJiri Olsa
325189ae89f5SJiri Olsa err = -ENOMEM;
325289ae89f5SJiri Olsa
325389ae89f5SJiri Olsa link = kzalloc(sizeof(*link), GFP_KERNEL);
325489ae89f5SJiri Olsa uprobes = kvcalloc(cnt, sizeof(*uprobes), GFP_KERNEL);
325589ae89f5SJiri Olsa
325689ae89f5SJiri Olsa if (!uprobes || !link)
325789ae89f5SJiri Olsa goto error_free;
325889ae89f5SJiri Olsa
325989ae89f5SJiri Olsa for (i = 0; i < cnt; i++) {
32600b779b61SJiri Olsa if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) {
32610b779b61SJiri Olsa err = -EFAULT;
32620b779b61SJiri Olsa goto error_free;
32630b779b61SJiri Olsa }
32645aa03dd3SJiri Olsa if (uref_ctr_offsets && __get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) {
326589ae89f5SJiri Olsa err = -EFAULT;
326689ae89f5SJiri Olsa goto error_free;
326789ae89f5SJiri Olsa }
326889ae89f5SJiri Olsa if (__get_user(uprobes[i].offset, uoffsets + i)) {
326989ae89f5SJiri Olsa err = -EFAULT;
327089ae89f5SJiri Olsa goto error_free;
327189ae89f5SJiri Olsa }
327289ae89f5SJiri Olsa
327389ae89f5SJiri Olsa uprobes[i].link = link;
327489ae89f5SJiri Olsa
327589ae89f5SJiri Olsa if (flags & BPF_F_UPROBE_MULTI_RETURN)
327689ae89f5SJiri Olsa uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler;
327789ae89f5SJiri Olsa else
327889ae89f5SJiri Olsa uprobes[i].consumer.handler = uprobe_multi_link_handler;
3279b733eeadSJiri Olsa
3280b733eeadSJiri Olsa if (pid)
3281b733eeadSJiri Olsa uprobes[i].consumer.filter = uprobe_multi_link_filter;
328289ae89f5SJiri Olsa }
328389ae89f5SJiri Olsa
328489ae89f5SJiri Olsa link->cnt = cnt;
328589ae89f5SJiri Olsa link->uprobes = uprobes;
328689ae89f5SJiri Olsa link->path = path;
3287b733eeadSJiri Olsa link->task = task;
328889ae89f5SJiri Olsa
328989ae89f5SJiri Olsa bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI,
329089ae89f5SJiri Olsa &bpf_uprobe_multi_link_lops, prog);
329189ae89f5SJiri Olsa
329289ae89f5SJiri Olsa for (i = 0; i < cnt; i++) {
329389ae89f5SJiri Olsa err = uprobe_register_refctr(d_real_inode(link->path.dentry),
329489ae89f5SJiri Olsa uprobes[i].offset,
32955aa03dd3SJiri Olsa uprobes[i].ref_ctr_offset,
329689ae89f5SJiri Olsa &uprobes[i].consumer);
329789ae89f5SJiri Olsa if (err) {
3298790c630aSOleg Nesterov link->cnt = i;
3299790c630aSOleg Nesterov goto error_unregister;
330089ae89f5SJiri Olsa }
330189ae89f5SJiri Olsa }
330289ae89f5SJiri Olsa
330389ae89f5SJiri Olsa err = bpf_link_prime(&link->link, &link_primer);
330489ae89f5SJiri Olsa if (err)
3305790c630aSOleg Nesterov goto error_unregister;
330689ae89f5SJiri Olsa
330789ae89f5SJiri Olsa return bpf_link_settle(&link_primer);
330889ae89f5SJiri Olsa
3309790c630aSOleg Nesterov error_unregister:
3310790c630aSOleg Nesterov bpf_uprobe_unregister(&path, uprobes, link->cnt);
3311790c630aSOleg Nesterov
331289ae89f5SJiri Olsa error_free:
331389ae89f5SJiri Olsa kvfree(uprobes);
331489ae89f5SJiri Olsa kfree(link);
3315b733eeadSJiri Olsa if (task)
3316b733eeadSJiri Olsa put_task_struct(task);
331789ae89f5SJiri Olsa error_path_put:
331889ae89f5SJiri Olsa path_put(&path);
331989ae89f5SJiri Olsa return err;
332089ae89f5SJiri Olsa }
332189ae89f5SJiri Olsa #else /* !CONFIG_UPROBES */
bpf_uprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)332289ae89f5SJiri Olsa int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
332389ae89f5SJiri Olsa {
332489ae89f5SJiri Olsa return -EOPNOTSUPP;
332589ae89f5SJiri Olsa }
bpf_uprobe_multi_cookie(struct bpf_run_ctx * ctx)33260b779b61SJiri Olsa static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
33270b779b61SJiri Olsa {
33280b779b61SJiri Olsa return 0;
33290b779b61SJiri Olsa }
bpf_uprobe_multi_entry_ip(struct bpf_run_ctx * ctx)3330686328d8SJiri Olsa static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3331686328d8SJiri Olsa {
3332686328d8SJiri Olsa return 0;
3333686328d8SJiri Olsa }
333489ae89f5SJiri Olsa #endif /* CONFIG_UPROBES */
3335