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
448b2efe51SHou Tao #define MAX_UPROBE_MULTI_CNT (1U << 20)
45d6d1e6c1SHou Tao #define MAX_KPROBE_MULTI_CNT (1U << 20)
468b2efe51SHou 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 */
123dd865789SJiri Olsa rcu_read_lock();
124dd865789SJiri Olsa bpf_prog_inc_misses_counters(rcu_dereference(call->prog_array));
125dd865789SJiri Olsa rcu_read_unlock();
1262541517cSAlexei Starovoitov ret = 0;
1272541517cSAlexei Starovoitov goto out;
1282541517cSAlexei Starovoitov }
1292541517cSAlexei Starovoitov
130e87c6bc3SYonghong Song /*
131e87c6bc3SYonghong Song * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
132e87c6bc3SYonghong Song * to all call sites, we did a bpf_prog_array_valid() there to check
133e87c6bc3SYonghong Song * whether call->prog_array is empty or not, which is
1342b5894ccSQiujun Huang * a heuristic to speed up execution.
135e87c6bc3SYonghong Song *
136e87c6bc3SYonghong Song * If bpf_prog_array_valid() fetched prog_array was
137e87c6bc3SYonghong Song * non-NULL, we go into trace_call_bpf() and do the actual
138e87c6bc3SYonghong Song * proper rcu_dereference() under RCU lock.
139e87c6bc3SYonghong Song * If it turns out that prog_array is NULL then, we bail out.
140e87c6bc3SYonghong Song * For the opposite, if the bpf_prog_array_valid() fetched pointer
141e87c6bc3SYonghong Song * was NULL, you'll skip the prog_array with the risk of missing
142e87c6bc3SYonghong Song * out of events when it was updated in between this and the
143e87c6bc3SYonghong Song * rcu_dereference() which is accepted risk.
144e87c6bc3SYonghong Song */
145055eb955SStanislav Fomichev rcu_read_lock();
146055eb955SStanislav Fomichev ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
147055eb955SStanislav Fomichev ctx, bpf_prog_run);
148055eb955SStanislav Fomichev rcu_read_unlock();
1492541517cSAlexei Starovoitov
1502541517cSAlexei Starovoitov out:
1512541517cSAlexei Starovoitov __this_cpu_dec(bpf_prog_active);
1522541517cSAlexei Starovoitov
1532541517cSAlexei Starovoitov return ret;
1542541517cSAlexei Starovoitov }
1552541517cSAlexei Starovoitov
1569802d865SJosef Bacik #ifdef CONFIG_BPF_KPROBE_OVERRIDE
BPF_CALL_2(bpf_override_return,struct pt_regs *,regs,unsigned long,rc)1579802d865SJosef Bacik BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
1589802d865SJosef Bacik {
1599802d865SJosef Bacik regs_set_return_value(regs, rc);
160540adea3SMasami Hiramatsu override_function_with_return(regs);
1619802d865SJosef Bacik return 0;
1629802d865SJosef Bacik }
1639802d865SJosef Bacik
1649802d865SJosef Bacik static const struct bpf_func_proto bpf_override_return_proto = {
1659802d865SJosef Bacik .func = bpf_override_return,
1669802d865SJosef Bacik .gpl_only = true,
1679802d865SJosef Bacik .ret_type = RET_INTEGER,
1689802d865SJosef Bacik .arg1_type = ARG_PTR_TO_CTX,
1699802d865SJosef Bacik .arg2_type = ARG_ANYTHING,
1709802d865SJosef Bacik };
1719802d865SJosef Bacik #endif
1729802d865SJosef Bacik
1738d92db5cSChristoph Hellwig static __always_inline int
bpf_probe_read_user_common(void * dst,u32 size,const void __user * unsafe_ptr)1748d92db5cSChristoph Hellwig bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
1758d92db5cSChristoph Hellwig {
1768d92db5cSChristoph Hellwig int ret;
1778d92db5cSChristoph Hellwig
178c0ee37e8SChristoph Hellwig ret = copy_from_user_nofault(dst, unsafe_ptr, size);
1798d92db5cSChristoph Hellwig if (unlikely(ret < 0))
1808d92db5cSChristoph Hellwig memset(dst, 0, size);
1818d92db5cSChristoph Hellwig return ret;
1828d92db5cSChristoph Hellwig }
1838d92db5cSChristoph Hellwig
BPF_CALL_3(bpf_probe_read_user,void *,dst,u32,size,const void __user *,unsafe_ptr)1846ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
1856ae08ae3SDaniel Borkmann const void __user *, unsafe_ptr)
1862541517cSAlexei Starovoitov {
1878d92db5cSChristoph Hellwig return bpf_probe_read_user_common(dst, size, unsafe_ptr);
1882541517cSAlexei Starovoitov }
1892541517cSAlexei Starovoitov
190f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_user_proto = {
1916ae08ae3SDaniel Borkmann .func = bpf_probe_read_user,
1926ae08ae3SDaniel Borkmann .gpl_only = true,
1936ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
1946ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1956ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1966ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
1976ae08ae3SDaniel Borkmann };
1986ae08ae3SDaniel Borkmann
1998d92db5cSChristoph Hellwig static __always_inline int
bpf_probe_read_user_str_common(void * dst,u32 size,const void __user * unsafe_ptr)2008d92db5cSChristoph Hellwig bpf_probe_read_user_str_common(void *dst, u32 size,
2018d92db5cSChristoph Hellwig const void __user *unsafe_ptr)
2028d92db5cSChristoph Hellwig {
2038d92db5cSChristoph Hellwig int ret;
2048d92db5cSChristoph Hellwig
2056fa6d280SDaniel Xu /*
2066fa6d280SDaniel Xu * NB: We rely on strncpy_from_user() not copying junk past the NUL
2076fa6d280SDaniel Xu * terminator into `dst`.
2086fa6d280SDaniel Xu *
2096fa6d280SDaniel Xu * strncpy_from_user() does long-sized strides in the fast path. If the
2106fa6d280SDaniel Xu * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
2116fa6d280SDaniel Xu * then there could be junk after the NUL in `dst`. If user takes `dst`
2126fa6d280SDaniel Xu * and keys a hash map with it, then semantically identical strings can
2136fa6d280SDaniel Xu * occupy multiple entries in the map.
2146fa6d280SDaniel Xu */
2158d92db5cSChristoph Hellwig ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
2168d92db5cSChristoph Hellwig if (unlikely(ret < 0))
2178d92db5cSChristoph Hellwig memset(dst, 0, size);
2188d92db5cSChristoph Hellwig return ret;
2198d92db5cSChristoph Hellwig }
2208d92db5cSChristoph Hellwig
BPF_CALL_3(bpf_probe_read_user_str,void *,dst,u32,size,const void __user *,unsafe_ptr)2216ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
2226ae08ae3SDaniel Borkmann const void __user *, unsafe_ptr)
2236ae08ae3SDaniel Borkmann {
2248d92db5cSChristoph Hellwig return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
2256ae08ae3SDaniel Borkmann }
2266ae08ae3SDaniel Borkmann
227f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_user_str_proto = {
2286ae08ae3SDaniel Borkmann .func = bpf_probe_read_user_str,
2296ae08ae3SDaniel Borkmann .gpl_only = true,
2306ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
2316ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
2326ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2336ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
2346ae08ae3SDaniel Borkmann };
2356ae08ae3SDaniel Borkmann
BPF_CALL_3(bpf_probe_read_kernel,void *,dst,u32,size,const void *,unsafe_ptr)2366ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
2376ae08ae3SDaniel Borkmann const void *, unsafe_ptr)
2386ae08ae3SDaniel Borkmann {
2398d92db5cSChristoph Hellwig return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
2406ae08ae3SDaniel Borkmann }
2416ae08ae3SDaniel Borkmann
242f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_kernel_proto = {
2436ae08ae3SDaniel Borkmann .func = bpf_probe_read_kernel,
2446ae08ae3SDaniel Borkmann .gpl_only = true,
2456ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
2466ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
2476ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2486ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
2496ae08ae3SDaniel Borkmann };
2506ae08ae3SDaniel Borkmann
2516ae08ae3SDaniel Borkmann static __always_inline int
bpf_probe_read_kernel_str_common(void * dst,u32 size,const void * unsafe_ptr)2528d92db5cSChristoph Hellwig bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
2536ae08ae3SDaniel Borkmann {
254ff40e510SDaniel Borkmann int ret;
2558d92db5cSChristoph Hellwig
2566ae08ae3SDaniel Borkmann /*
2578d92db5cSChristoph Hellwig * The strncpy_from_kernel_nofault() call will likely not fill the
2588d92db5cSChristoph Hellwig * entire buffer, but that's okay in this circumstance as we're probing
2596ae08ae3SDaniel Borkmann * arbitrary memory anyway similar to bpf_probe_read_*() and might
2606ae08ae3SDaniel Borkmann * as well probe the stack. Thus, memory is explicitly cleared
2616ae08ae3SDaniel Borkmann * only in error case, so that improper users ignoring return
2626ae08ae3SDaniel Borkmann * code altogether don't copy garbage; otherwise length of string
2636ae08ae3SDaniel Borkmann * is returned that can be used for bpf_perf_event_output() et al.
2646ae08ae3SDaniel Borkmann */
2658d92db5cSChristoph Hellwig ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
2666ae08ae3SDaniel Borkmann if (unlikely(ret < 0))
2676ae08ae3SDaniel Borkmann memset(dst, 0, size);
2686ae08ae3SDaniel Borkmann return ret;
2696ae08ae3SDaniel Borkmann }
2706ae08ae3SDaniel Borkmann
BPF_CALL_3(bpf_probe_read_kernel_str,void *,dst,u32,size,const void *,unsafe_ptr)2716ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
2726ae08ae3SDaniel Borkmann const void *, unsafe_ptr)
2736ae08ae3SDaniel Borkmann {
2748d92db5cSChristoph Hellwig return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
2756ae08ae3SDaniel Borkmann }
2766ae08ae3SDaniel Borkmann
277f470378cSJohn Fastabend const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
2786ae08ae3SDaniel Borkmann .func = bpf_probe_read_kernel_str,
2796ae08ae3SDaniel Borkmann .gpl_only = true,
2806ae08ae3SDaniel Borkmann .ret_type = RET_INTEGER,
2816ae08ae3SDaniel Borkmann .arg1_type = ARG_PTR_TO_UNINIT_MEM,
2826ae08ae3SDaniel Borkmann .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2836ae08ae3SDaniel Borkmann .arg3_type = ARG_ANYTHING,
2846ae08ae3SDaniel Borkmann };
2856ae08ae3SDaniel Borkmann
2868d92db5cSChristoph Hellwig #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
BPF_CALL_3(bpf_probe_read_compat,void *,dst,u32,size,const void *,unsafe_ptr)2878d92db5cSChristoph Hellwig BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
2888d92db5cSChristoph Hellwig const void *, unsafe_ptr)
2898d92db5cSChristoph Hellwig {
2908d92db5cSChristoph Hellwig if ((unsigned long)unsafe_ptr < TASK_SIZE) {
2918d92db5cSChristoph Hellwig return bpf_probe_read_user_common(dst, size,
2928d92db5cSChristoph Hellwig (__force void __user *)unsafe_ptr);
2938d92db5cSChristoph Hellwig }
2948d92db5cSChristoph Hellwig return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
2958d92db5cSChristoph Hellwig }
2968d92db5cSChristoph Hellwig
2978d92db5cSChristoph Hellwig static const struct bpf_func_proto bpf_probe_read_compat_proto = {
2988d92db5cSChristoph Hellwig .func = bpf_probe_read_compat,
2998d92db5cSChristoph Hellwig .gpl_only = true,
3008d92db5cSChristoph Hellwig .ret_type = RET_INTEGER,
3018d92db5cSChristoph Hellwig .arg1_type = ARG_PTR_TO_UNINIT_MEM,
3028d92db5cSChristoph Hellwig .arg2_type = ARG_CONST_SIZE_OR_ZERO,
3038d92db5cSChristoph Hellwig .arg3_type = ARG_ANYTHING,
3048d92db5cSChristoph Hellwig };
3058d92db5cSChristoph Hellwig
BPF_CALL_3(bpf_probe_read_compat_str,void *,dst,u32,size,const void *,unsafe_ptr)3066ae08ae3SDaniel Borkmann BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
3076ae08ae3SDaniel Borkmann const void *, unsafe_ptr)
3086ae08ae3SDaniel Borkmann {
3098d92db5cSChristoph Hellwig if ((unsigned long)unsafe_ptr < TASK_SIZE) {
3108d92db5cSChristoph Hellwig return bpf_probe_read_user_str_common(dst, size,
3118d92db5cSChristoph Hellwig (__force void __user *)unsafe_ptr);
3128d92db5cSChristoph Hellwig }
3138d92db5cSChristoph Hellwig return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
3146ae08ae3SDaniel Borkmann }
3156ae08ae3SDaniel Borkmann
3166ae08ae3SDaniel Borkmann static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
3176ae08ae3SDaniel Borkmann .func = bpf_probe_read_compat_str,
3182541517cSAlexei Starovoitov .gpl_only = true,
3192541517cSAlexei Starovoitov .ret_type = RET_INTEGER,
32039f19ebbSAlexei Starovoitov .arg1_type = ARG_PTR_TO_UNINIT_MEM,
3219c019e2bSYonghong Song .arg2_type = ARG_CONST_SIZE_OR_ZERO,
3222541517cSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
3232541517cSAlexei Starovoitov };
3248d92db5cSChristoph Hellwig #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
3252541517cSAlexei Starovoitov
BPF_CALL_3(bpf_probe_write_user,void __user *,unsafe_ptr,const void *,src,u32,size)326eb1b6688SDaniel Borkmann BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
327f3694e00SDaniel Borkmann u32, size)
32896ae5227SSargun Dhillon {
32996ae5227SSargun Dhillon /*
33096ae5227SSargun Dhillon * Ensure we're in user context which is safe for the helper to
33196ae5227SSargun Dhillon * run. This helper has no business in a kthread.
33296ae5227SSargun Dhillon *
33396ae5227SSargun Dhillon * access_ok() should prevent writing to non-user memory, but in
33496ae5227SSargun Dhillon * some situations (nommu, temporary switch, etc) access_ok() does
33596ae5227SSargun Dhillon * not provide enough validation, hence the check on KERNEL_DS.
336c7b6f29bSNadav Amit *
337c7b6f29bSNadav Amit * nmi_uaccess_okay() ensures the probe is not run in an interim
338c7b6f29bSNadav Amit * state, when the task or mm are switched. This is specifically
339c7b6f29bSNadav Amit * required to prevent the use of temporary mm.
34096ae5227SSargun Dhillon */
34196ae5227SSargun Dhillon
34296ae5227SSargun Dhillon if (unlikely(in_interrupt() ||
34396ae5227SSargun Dhillon current->flags & (PF_KTHREAD | PF_EXITING)))
34496ae5227SSargun Dhillon return -EPERM;
345c7b6f29bSNadav Amit if (unlikely(!nmi_uaccess_okay()))
346c7b6f29bSNadav Amit return -EPERM;
34796ae5227SSargun Dhillon
348c0ee37e8SChristoph Hellwig return copy_to_user_nofault(unsafe_ptr, src, size);
34996ae5227SSargun Dhillon }
35096ae5227SSargun Dhillon
35196ae5227SSargun Dhillon static const struct bpf_func_proto bpf_probe_write_user_proto = {
35296ae5227SSargun Dhillon .func = bpf_probe_write_user,
35396ae5227SSargun Dhillon .gpl_only = true,
35496ae5227SSargun Dhillon .ret_type = RET_INTEGER,
35596ae5227SSargun Dhillon .arg1_type = ARG_ANYTHING,
356216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
35739f19ebbSAlexei Starovoitov .arg3_type = ARG_CONST_SIZE,
35896ae5227SSargun Dhillon };
35996ae5227SSargun Dhillon
360d9c9e4dbSFlorent Revest #define MAX_TRACE_PRINTK_VARARGS 3
361ac5a72eaSAlan Maguire #define BPF_TRACE_PRINTK_SIZE 1024
362ac5a72eaSAlan Maguire
BPF_CALL_5(bpf_trace_printk,char *,fmt,u32,fmt_size,u64,arg1,u64,arg2,u64,arg3)363f3694e00SDaniel Borkmann BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
364f3694e00SDaniel Borkmann u64, arg2, u64, arg3)
3659c959c86SAlexei Starovoitov {
366d9c9e4dbSFlorent Revest u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
36778aa1cc9SJiri Olsa struct bpf_bprintf_data data = {
36878aa1cc9SJiri Olsa .get_bin_args = true,
369e2bb9e01SJiri Olsa .get_buf = true,
37078aa1cc9SJiri Olsa };
371d9c9e4dbSFlorent Revest int ret;
3729c959c86SAlexei Starovoitov
37378aa1cc9SJiri Olsa ret = bpf_bprintf_prepare(fmt, fmt_size, args,
37478aa1cc9SJiri Olsa MAX_TRACE_PRINTK_VARARGS, &data);
375d9c9e4dbSFlorent Revest if (ret < 0)
376d9c9e4dbSFlorent Revest return ret;
3779c959c86SAlexei Starovoitov
378e2bb9e01SJiri Olsa ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
3799c959c86SAlexei Starovoitov
380e2bb9e01SJiri Olsa trace_bpf_trace_printk(data.buf);
3819c959c86SAlexei Starovoitov
382f19a4050SJiri Olsa bpf_bprintf_cleanup(&data);
3839c959c86SAlexei Starovoitov
384d9c9e4dbSFlorent Revest return ret;
3859c959c86SAlexei Starovoitov }
3869c959c86SAlexei Starovoitov
3879c959c86SAlexei Starovoitov static const struct bpf_func_proto bpf_trace_printk_proto = {
3889c959c86SAlexei Starovoitov .func = bpf_trace_printk,
3899c959c86SAlexei Starovoitov .gpl_only = true,
3909c959c86SAlexei Starovoitov .ret_type = RET_INTEGER,
391216e3cd2SHao Luo .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
39239f19ebbSAlexei Starovoitov .arg2_type = ARG_CONST_SIZE,
3939c959c86SAlexei Starovoitov };
3949c959c86SAlexei Starovoitov
__set_printk_clr_event(struct work_struct * work)3954580f4e0SAlexei Starovoitov static void __set_printk_clr_event(struct work_struct *work)
3960756ea3eSAlexei Starovoitov {
3970756ea3eSAlexei Starovoitov /*
398ac5a72eaSAlan Maguire * This program might be calling bpf_trace_printk,
399ac5a72eaSAlan Maguire * so enable the associated bpf_trace/bpf_trace_printk event.
400ac5a72eaSAlan Maguire * Repeat this each time as it is possible a user has
401ac5a72eaSAlan Maguire * disabled bpf_trace_printk events. By loading a program
402ac5a72eaSAlan Maguire * calling bpf_trace_printk() however the user has expressed
403ac5a72eaSAlan Maguire * the intent to see such events.
4040756ea3eSAlexei Starovoitov */
405ac5a72eaSAlan Maguire if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
406ac5a72eaSAlan Maguire pr_warn_ratelimited("could not enable bpf_trace_printk events");
40710aceb62SDave Marchevsky }
4084580f4e0SAlexei Starovoitov static DECLARE_WORK(set_printk_work, __set_printk_clr_event);
4090756ea3eSAlexei Starovoitov
bpf_get_trace_printk_proto(void)41010aceb62SDave Marchevsky const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
41110aceb62SDave Marchevsky {
4124580f4e0SAlexei Starovoitov schedule_work(&set_printk_work);
4130756ea3eSAlexei Starovoitov return &bpf_trace_printk_proto;
4140756ea3eSAlexei Starovoitov }
4150756ea3eSAlexei Starovoitov
BPF_CALL_4(bpf_trace_vprintk,char *,fmt,u32,fmt_size,const void *,args,u32,data_len)41678aa1cc9SJiri Olsa BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args,
41710aceb62SDave Marchevsky u32, data_len)
41810aceb62SDave Marchevsky {
41978aa1cc9SJiri Olsa struct bpf_bprintf_data data = {
42078aa1cc9SJiri Olsa .get_bin_args = true,
421e2bb9e01SJiri Olsa .get_buf = true,
42278aa1cc9SJiri Olsa };
42310aceb62SDave Marchevsky int ret, num_args;
42410aceb62SDave Marchevsky
42510aceb62SDave Marchevsky if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
42678aa1cc9SJiri Olsa (data_len && !args))
42710aceb62SDave Marchevsky return -EINVAL;
42810aceb62SDave Marchevsky num_args = data_len / 8;
42910aceb62SDave Marchevsky
43078aa1cc9SJiri Olsa ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
43110aceb62SDave Marchevsky if (ret < 0)
43210aceb62SDave Marchevsky return ret;
43310aceb62SDave Marchevsky
434e2bb9e01SJiri Olsa ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
43510aceb62SDave Marchevsky
436e2bb9e01SJiri Olsa trace_bpf_trace_printk(data.buf);
43710aceb62SDave Marchevsky
438f19a4050SJiri Olsa bpf_bprintf_cleanup(&data);
43910aceb62SDave Marchevsky
44010aceb62SDave Marchevsky return ret;
44110aceb62SDave Marchevsky }
44210aceb62SDave Marchevsky
44310aceb62SDave Marchevsky static const struct bpf_func_proto bpf_trace_vprintk_proto = {
44410aceb62SDave Marchevsky .func = bpf_trace_vprintk,
44510aceb62SDave Marchevsky .gpl_only = true,
44610aceb62SDave Marchevsky .ret_type = RET_INTEGER,
447216e3cd2SHao Luo .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
44810aceb62SDave Marchevsky .arg2_type = ARG_CONST_SIZE,
449216e3cd2SHao Luo .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
45010aceb62SDave Marchevsky .arg4_type = ARG_CONST_SIZE_OR_ZERO,
45110aceb62SDave Marchevsky };
45210aceb62SDave Marchevsky
bpf_get_trace_vprintk_proto(void)45310aceb62SDave Marchevsky const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
45410aceb62SDave Marchevsky {
4554580f4e0SAlexei Starovoitov schedule_work(&set_printk_work);
45610aceb62SDave Marchevsky return &bpf_trace_vprintk_proto;
45710aceb62SDave Marchevsky }
45810aceb62SDave Marchevsky
BPF_CALL_5(bpf_seq_printf,struct seq_file *,m,char *,fmt,u32,fmt_size,const void *,args,u32,data_len)459492e639fSYonghong Song BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
46078aa1cc9SJiri Olsa const void *, args, u32, data_len)
461492e639fSYonghong Song {
46278aa1cc9SJiri Olsa struct bpf_bprintf_data data = {
46378aa1cc9SJiri Olsa .get_bin_args = true,
46478aa1cc9SJiri Olsa };
465d9c9e4dbSFlorent Revest int err, num_args;
466492e639fSYonghong Song
467335ff499SDave Marchevsky if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
46878aa1cc9SJiri Olsa (data_len && !args))
469d9c9e4dbSFlorent Revest return -EINVAL;
470492e639fSYonghong Song num_args = data_len / 8;
471492e639fSYonghong Song
47278aa1cc9SJiri Olsa err = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
473492e639fSYonghong Song if (err < 0)
474d9c9e4dbSFlorent Revest return err;
475492e639fSYonghong Song
47678aa1cc9SJiri Olsa seq_bprintf(m, fmt, data.bin_args);
477492e639fSYonghong Song
478f19a4050SJiri Olsa bpf_bprintf_cleanup(&data);
479d9c9e4dbSFlorent Revest
480d9c9e4dbSFlorent Revest return seq_has_overflowed(m) ? -EOVERFLOW : 0;
481492e639fSYonghong Song }
482492e639fSYonghong Song
4839436ef6eSLorenz Bauer BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
484c9a0f3b8SJiri Olsa
485492e639fSYonghong Song static const struct bpf_func_proto bpf_seq_printf_proto = {
486492e639fSYonghong Song .func = bpf_seq_printf,
487492e639fSYonghong Song .gpl_only = true,
488492e639fSYonghong Song .ret_type = RET_INTEGER,
489492e639fSYonghong Song .arg1_type = ARG_PTR_TO_BTF_ID,
4909436ef6eSLorenz Bauer .arg1_btf_id = &btf_seq_file_ids[0],
491216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
492492e639fSYonghong Song .arg3_type = ARG_CONST_SIZE,
493216e3cd2SHao Luo .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
494492e639fSYonghong Song .arg5_type = ARG_CONST_SIZE_OR_ZERO,
495492e639fSYonghong Song };
496492e639fSYonghong Song
BPF_CALL_3(bpf_seq_write,struct seq_file *,m,const void *,data,u32,len)497492e639fSYonghong Song BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
498492e639fSYonghong Song {
499492e639fSYonghong Song return seq_write(m, data, len) ? -EOVERFLOW : 0;
500492e639fSYonghong Song }
501492e639fSYonghong Song
502492e639fSYonghong Song static const struct bpf_func_proto bpf_seq_write_proto = {
503492e639fSYonghong Song .func = bpf_seq_write,
504492e639fSYonghong Song .gpl_only = true,
505492e639fSYonghong Song .ret_type = RET_INTEGER,
506492e639fSYonghong Song .arg1_type = ARG_PTR_TO_BTF_ID,
5079436ef6eSLorenz Bauer .arg1_btf_id = &btf_seq_file_ids[0],
508216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
509492e639fSYonghong Song .arg3_type = ARG_CONST_SIZE_OR_ZERO,
510492e639fSYonghong Song };
511492e639fSYonghong Song
BPF_CALL_4(bpf_seq_printf_btf,struct seq_file *,m,struct btf_ptr *,ptr,u32,btf_ptr_size,u64,flags)512eb411377SAlan Maguire BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
513eb411377SAlan Maguire u32, btf_ptr_size, u64, flags)
514eb411377SAlan Maguire {
515eb411377SAlan Maguire const struct btf *btf;
516eb411377SAlan Maguire s32 btf_id;
517eb411377SAlan Maguire int ret;
518eb411377SAlan Maguire
519eb411377SAlan Maguire ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
520eb411377SAlan Maguire if (ret)
521eb411377SAlan Maguire return ret;
522eb411377SAlan Maguire
523eb411377SAlan Maguire return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
524eb411377SAlan Maguire }
525eb411377SAlan Maguire
526eb411377SAlan Maguire static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
527eb411377SAlan Maguire .func = bpf_seq_printf_btf,
528eb411377SAlan Maguire .gpl_only = true,
529eb411377SAlan Maguire .ret_type = RET_INTEGER,
530eb411377SAlan Maguire .arg1_type = ARG_PTR_TO_BTF_ID,
531eb411377SAlan Maguire .arg1_btf_id = &btf_seq_file_ids[0],
532216e3cd2SHao Luo .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
533eb411377SAlan Maguire .arg3_type = ARG_CONST_SIZE_OR_ZERO,
534eb411377SAlan Maguire .arg4_type = ARG_ANYTHING,
535d9847d31SAlexei Starovoitov };
536d9847d31SAlexei Starovoitov
537908432caSYonghong Song static __always_inline int
get_map_perf_counter(struct bpf_map * map,u64 flags,u64 * value,u64 * enabled,u64 * running)538908432caSYonghong Song get_map_perf_counter(struct bpf_map *map, u64 flags,
539908432caSYonghong Song u64 *value, u64 *enabled, u64 *running)
54035578d79SKaixu Xia {
54135578d79SKaixu Xia struct bpf_array *array = container_of(map, struct bpf_array, map);
5426816a7ffSDaniel Borkmann unsigned int cpu = smp_processor_id();
5436816a7ffSDaniel Borkmann u64 index = flags & BPF_F_INDEX_MASK;
5443b1efb19SDaniel Borkmann struct bpf_event_entry *ee;
54535578d79SKaixu Xia
5466816a7ffSDaniel Borkmann if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
5476816a7ffSDaniel Borkmann return -EINVAL;
5486816a7ffSDaniel Borkmann if (index == BPF_F_CURRENT_CPU)
5496816a7ffSDaniel Borkmann index = cpu;
55035578d79SKaixu Xia if (unlikely(index >= array->map.max_entries))
55135578d79SKaixu Xia return -E2BIG;
55235578d79SKaixu Xia
5533b1efb19SDaniel Borkmann ee = READ_ONCE(array->ptrs[index]);
5541ca1cc98SDaniel Borkmann if (!ee)
55535578d79SKaixu Xia return -ENOENT;
55635578d79SKaixu Xia
557908432caSYonghong Song return perf_event_read_local(ee->event, value, enabled, running);
558908432caSYonghong Song }
559908432caSYonghong Song
BPF_CALL_2(bpf_perf_event_read,struct bpf_map *,map,u64,flags)560908432caSYonghong Song BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
561908432caSYonghong Song {
562908432caSYonghong Song u64 value = 0;
563908432caSYonghong Song int err;
564908432caSYonghong Song
565908432caSYonghong Song err = get_map_perf_counter(map, flags, &value, NULL, NULL);
56635578d79SKaixu Xia /*
567f91840a3SAlexei Starovoitov * this api is ugly since we miss [-22..-2] range of valid
568f91840a3SAlexei Starovoitov * counter values, but that's uapi
56935578d79SKaixu Xia */
570f91840a3SAlexei Starovoitov if (err)
571f91840a3SAlexei Starovoitov return err;
572f91840a3SAlexei Starovoitov return value;
57335578d79SKaixu Xia }
57435578d79SKaixu Xia
57562544ce8SAlexei Starovoitov static const struct bpf_func_proto bpf_perf_event_read_proto = {
57635578d79SKaixu Xia .func = bpf_perf_event_read,
5771075ef59SAlexei Starovoitov .gpl_only = true,
57835578d79SKaixu Xia .ret_type = RET_INTEGER,
57935578d79SKaixu Xia .arg1_type = ARG_CONST_MAP_PTR,
58035578d79SKaixu Xia .arg2_type = ARG_ANYTHING,
58135578d79SKaixu Xia };
58235578d79SKaixu Xia
BPF_CALL_4(bpf_perf_event_read_value,struct bpf_map *,map,u64,flags,struct bpf_perf_event_value *,buf,u32,size)583908432caSYonghong Song BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
584908432caSYonghong Song struct bpf_perf_event_value *, buf, u32, size)
585908432caSYonghong Song {
586908432caSYonghong Song int err = -EINVAL;
587908432caSYonghong Song
588908432caSYonghong Song if (unlikely(size != sizeof(struct bpf_perf_event_value)))
589908432caSYonghong Song goto clear;
590908432caSYonghong Song err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
591908432caSYonghong Song &buf->running);
592908432caSYonghong Song if (unlikely(err))
593908432caSYonghong Song goto clear;
594908432caSYonghong Song return 0;
595908432caSYonghong Song clear:
596908432caSYonghong Song memset(buf, 0, size);
597908432caSYonghong Song return err;
598908432caSYonghong Song }
599908432caSYonghong Song
600908432caSYonghong Song static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
601908432caSYonghong Song .func = bpf_perf_event_read_value,
602908432caSYonghong Song .gpl_only = true,
603908432caSYonghong Song .ret_type = RET_INTEGER,
604908432caSYonghong Song .arg1_type = ARG_CONST_MAP_PTR,
605908432caSYonghong Song .arg2_type = ARG_ANYTHING,
606908432caSYonghong Song .arg3_type = ARG_PTR_TO_UNINIT_MEM,
607908432caSYonghong Song .arg4_type = ARG_CONST_SIZE,
608908432caSYonghong Song };
609908432caSYonghong Song
bpf_get_perf_event_read_value_proto(void)610*ae0a457fSEmil Tsalapatis const struct bpf_func_proto *bpf_get_perf_event_read_value_proto(void)
611*ae0a457fSEmil Tsalapatis {
612*ae0a457fSEmil Tsalapatis return &bpf_perf_event_read_value_proto;
613*ae0a457fSEmil Tsalapatis }
614*ae0a457fSEmil Tsalapatis
6158e7a3920SDaniel 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)6168e7a3920SDaniel Borkmann __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
617b9c44b91SYabin Cui u64 flags, struct perf_raw_record *raw,
618b9c44b91SYabin Cui struct perf_sample_data *sd)
619a43eec30SAlexei Starovoitov {
620a43eec30SAlexei Starovoitov struct bpf_array *array = container_of(map, struct bpf_array, map);
621d7931330SDaniel Borkmann unsigned int cpu = smp_processor_id();
6221e33759cSDaniel Borkmann u64 index = flags & BPF_F_INDEX_MASK;
6233b1efb19SDaniel Borkmann struct bpf_event_entry *ee;
624a43eec30SAlexei Starovoitov struct perf_event *event;
625a43eec30SAlexei Starovoitov
6261e33759cSDaniel Borkmann if (index == BPF_F_CURRENT_CPU)
627d7931330SDaniel Borkmann index = cpu;
628a43eec30SAlexei Starovoitov if (unlikely(index >= array->map.max_entries))
629a43eec30SAlexei Starovoitov return -E2BIG;
630a43eec30SAlexei Starovoitov
6313b1efb19SDaniel Borkmann ee = READ_ONCE(array->ptrs[index]);
6321ca1cc98SDaniel Borkmann if (!ee)
633a43eec30SAlexei Starovoitov return -ENOENT;
634a43eec30SAlexei Starovoitov
6353b1efb19SDaniel Borkmann event = ee->event;
636a43eec30SAlexei Starovoitov if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
637a43eec30SAlexei Starovoitov event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
638a43eec30SAlexei Starovoitov return -EINVAL;
639a43eec30SAlexei Starovoitov
640d7931330SDaniel Borkmann if (unlikely(event->oncpu != cpu))
641a43eec30SAlexei Starovoitov return -EOPNOTSUPP;
642a43eec30SAlexei Starovoitov
643b9c44b91SYabin Cui perf_sample_save_raw_data(sd, event, raw);
644b9c44b91SYabin Cui
64556201969SArnaldo Carvalho de Melo return perf_event_output(event, sd, regs);
646a43eec30SAlexei Starovoitov }
647a43eec30SAlexei Starovoitov
6489594dc3cSMatt Mullins /*
6499594dc3cSMatt Mullins * Support executing tracepoints in normal, irq, and nmi context that each call
6509594dc3cSMatt Mullins * bpf_perf_event_output
6519594dc3cSMatt Mullins */
6529594dc3cSMatt Mullins struct bpf_trace_sample_data {
6539594dc3cSMatt Mullins struct perf_sample_data sds[3];
6549594dc3cSMatt Mullins };
6559594dc3cSMatt Mullins
6569594dc3cSMatt Mullins static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
6579594dc3cSMatt 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)658f3694e00SDaniel Borkmann BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
659f3694e00SDaniel Borkmann u64, flags, void *, data, u64, size)
6608e7a3920SDaniel Borkmann {
661f2c67a3eSJiri Olsa struct bpf_trace_sample_data *sds;
6628e7a3920SDaniel Borkmann struct perf_raw_record raw = {
6638e7a3920SDaniel Borkmann .frag = {
6648e7a3920SDaniel Borkmann .size = size,
6658e7a3920SDaniel Borkmann .data = data,
6668e7a3920SDaniel Borkmann },
6678e7a3920SDaniel Borkmann };
6689594dc3cSMatt Mullins struct perf_sample_data *sd;
669f2c67a3eSJiri Olsa int nest_level, err;
670f2c67a3eSJiri Olsa
671f2c67a3eSJiri Olsa preempt_disable();
672f2c67a3eSJiri Olsa sds = this_cpu_ptr(&bpf_trace_sds);
673f2c67a3eSJiri Olsa nest_level = this_cpu_inc_return(bpf_trace_nest_level);
6748e7a3920SDaniel Borkmann
6759594dc3cSMatt Mullins if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
6769594dc3cSMatt Mullins err = -EBUSY;
6779594dc3cSMatt Mullins goto out;
6789594dc3cSMatt Mullins }
6799594dc3cSMatt Mullins
6809594dc3cSMatt Mullins sd = &sds->sds[nest_level - 1];
6819594dc3cSMatt Mullins
6829594dc3cSMatt Mullins if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
6839594dc3cSMatt Mullins err = -EINVAL;
6849594dc3cSMatt Mullins goto out;
6859594dc3cSMatt Mullins }
6868e7a3920SDaniel Borkmann
687283ca526SDaniel Borkmann perf_sample_data_init(sd, 0, 0);
688283ca526SDaniel Borkmann
689b9c44b91SYabin Cui err = __bpf_perf_event_output(regs, map, flags, &raw, sd);
6909594dc3cSMatt Mullins out:
6919594dc3cSMatt Mullins this_cpu_dec(bpf_trace_nest_level);
692f2c67a3eSJiri Olsa preempt_enable();
6939594dc3cSMatt Mullins return err;
6948e7a3920SDaniel Borkmann }
6958e7a3920SDaniel Borkmann
696a43eec30SAlexei Starovoitov static const struct bpf_func_proto bpf_perf_event_output_proto = {
697a43eec30SAlexei Starovoitov .func = bpf_perf_event_output,
6981075ef59SAlexei Starovoitov .gpl_only = true,
699a43eec30SAlexei Starovoitov .ret_type = RET_INTEGER,
700a43eec30SAlexei Starovoitov .arg1_type = ARG_PTR_TO_CTX,
701a43eec30SAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
702a43eec30SAlexei Starovoitov .arg3_type = ARG_ANYTHING,
703216e3cd2SHao Luo .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
704a60dd35dSGianluca Borello .arg5_type = ARG_CONST_SIZE_OR_ZERO,
705a43eec30SAlexei Starovoitov };
706a43eec30SAlexei Starovoitov
707768fb61fSAllan Zhang static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
708768fb61fSAllan Zhang struct bpf_nested_pt_regs {
709768fb61fSAllan Zhang struct pt_regs regs[3];
710768fb61fSAllan Zhang };
711768fb61fSAllan Zhang static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
712768fb61fSAllan Zhang static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
713bd570ff9SDaniel 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)714555c8a86SDaniel Borkmann u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
715555c8a86SDaniel Borkmann void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
716bd570ff9SDaniel Borkmann {
717555c8a86SDaniel Borkmann struct perf_raw_frag frag = {
718555c8a86SDaniel Borkmann .copy = ctx_copy,
719555c8a86SDaniel Borkmann .size = ctx_size,
720555c8a86SDaniel Borkmann .data = ctx,
721555c8a86SDaniel Borkmann };
722555c8a86SDaniel Borkmann struct perf_raw_record raw = {
723555c8a86SDaniel Borkmann .frag = {
724183fc153SAndrew Morton {
725555c8a86SDaniel Borkmann .next = ctx_size ? &frag : NULL,
726183fc153SAndrew Morton },
727555c8a86SDaniel Borkmann .size = meta_size,
728555c8a86SDaniel Borkmann .data = meta,
729555c8a86SDaniel Borkmann },
730555c8a86SDaniel Borkmann };
731768fb61fSAllan Zhang struct perf_sample_data *sd;
732768fb61fSAllan Zhang struct pt_regs *regs;
733d62cc390SJiri Olsa int nest_level;
734768fb61fSAllan Zhang u64 ret;
735768fb61fSAllan Zhang
736d62cc390SJiri Olsa preempt_disable();
737d62cc390SJiri Olsa nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
738d62cc390SJiri Olsa
739768fb61fSAllan Zhang if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
740768fb61fSAllan Zhang ret = -EBUSY;
741768fb61fSAllan Zhang goto out;
742768fb61fSAllan Zhang }
743768fb61fSAllan Zhang sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
744768fb61fSAllan Zhang regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
745bd570ff9SDaniel Borkmann
746bd570ff9SDaniel Borkmann perf_fetch_caller_regs(regs);
747283ca526SDaniel Borkmann perf_sample_data_init(sd, 0, 0);
748bd570ff9SDaniel Borkmann
749b9c44b91SYabin Cui ret = __bpf_perf_event_output(regs, map, flags, &raw, sd);
750768fb61fSAllan Zhang out:
751768fb61fSAllan Zhang this_cpu_dec(bpf_event_output_nest_level);
752d62cc390SJiri Olsa preempt_enable();
753768fb61fSAllan Zhang return ret;
754bd570ff9SDaniel Borkmann }
755bd570ff9SDaniel Borkmann
BPF_CALL_0(bpf_get_current_task)756f3694e00SDaniel Borkmann BPF_CALL_0(bpf_get_current_task)
757606274c5SAlexei Starovoitov {
758606274c5SAlexei Starovoitov return (long) current;
759606274c5SAlexei Starovoitov }
760606274c5SAlexei Starovoitov
761f470378cSJohn Fastabend const struct bpf_func_proto bpf_get_current_task_proto = {
762606274c5SAlexei Starovoitov .func = bpf_get_current_task,
763606274c5SAlexei Starovoitov .gpl_only = true,
764606274c5SAlexei Starovoitov .ret_type = RET_INTEGER,
765606274c5SAlexei Starovoitov };
766606274c5SAlexei Starovoitov
BPF_CALL_0(bpf_get_current_task_btf)7673ca1032aSKP Singh BPF_CALL_0(bpf_get_current_task_btf)
7683ca1032aSKP Singh {
7693ca1032aSKP Singh return (unsigned long) current;
7703ca1032aSKP Singh }
7713ca1032aSKP Singh
772a396eda5SDaniel Xu const struct bpf_func_proto bpf_get_current_task_btf_proto = {
7733ca1032aSKP Singh .func = bpf_get_current_task_btf,
7743ca1032aSKP Singh .gpl_only = true,
7753f00c523SDavid Vernet .ret_type = RET_PTR_TO_BTF_ID_TRUSTED,
776d19ddb47SSong Liu .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
7773ca1032aSKP Singh };
7783ca1032aSKP Singh
BPF_CALL_1(bpf_task_pt_regs,struct task_struct *,task)779dd6e10fbSDaniel Xu BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
780dd6e10fbSDaniel Xu {
781dd6e10fbSDaniel Xu return (unsigned long) task_pt_regs(task);
782dd6e10fbSDaniel Xu }
783dd6e10fbSDaniel Xu
784dd6e10fbSDaniel Xu BTF_ID_LIST(bpf_task_pt_regs_ids)
785dd6e10fbSDaniel Xu BTF_ID(struct, pt_regs)
786dd6e10fbSDaniel Xu
787dd6e10fbSDaniel Xu const struct bpf_func_proto bpf_task_pt_regs_proto = {
788dd6e10fbSDaniel Xu .func = bpf_task_pt_regs,
789dd6e10fbSDaniel Xu .gpl_only = true,
790dd6e10fbSDaniel Xu .arg1_type = ARG_PTR_TO_BTF_ID,
791d19ddb47SSong Liu .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
792dd6e10fbSDaniel Xu .ret_type = RET_PTR_TO_BTF_ID,
793dd6e10fbSDaniel Xu .ret_btf_id = &bpf_task_pt_regs_ids[0],
794dd6e10fbSDaniel Xu };
795dd6e10fbSDaniel Xu
7968b401f9eSYonghong Song struct send_signal_irq_work {
7978b401f9eSYonghong Song struct irq_work irq_work;
7988b401f9eSYonghong Song struct task_struct *task;
7998b401f9eSYonghong Song u32 sig;
8008482941fSYonghong Song enum pid_type type;
8016280cf71SPuranjay Mohan bool has_siginfo;
8026280cf71SPuranjay Mohan struct kernel_siginfo info;
8038b401f9eSYonghong Song };
8048b401f9eSYonghong Song
8058b401f9eSYonghong Song static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
8068b401f9eSYonghong Song
do_bpf_send_signal(struct irq_work * entry)8078b401f9eSYonghong Song static void do_bpf_send_signal(struct irq_work *entry)
8088b401f9eSYonghong Song {
8098b401f9eSYonghong Song struct send_signal_irq_work *work;
8106280cf71SPuranjay Mohan struct kernel_siginfo *siginfo;
8118b401f9eSYonghong Song
8128b401f9eSYonghong Song work = container_of(entry, struct send_signal_irq_work, irq_work);
8136280cf71SPuranjay Mohan siginfo = work->has_siginfo ? &work->info : SEND_SIG_PRIV;
8146280cf71SPuranjay Mohan
8156280cf71SPuranjay Mohan group_send_sig_info(work->sig, siginfo, work->task, work->type);
816bdb7fdb0SYonghong Song put_task_struct(work->task);
8178b401f9eSYonghong Song }
8188b401f9eSYonghong Song
bpf_send_signal_common(u32 sig,enum pid_type type,struct task_struct * task,u64 value)8196280cf71SPuranjay Mohan static int bpf_send_signal_common(u32 sig, enum pid_type type, struct task_struct *task, u64 value)
8208b401f9eSYonghong Song {
8218b401f9eSYonghong Song struct send_signal_irq_work *work = NULL;
8226280cf71SPuranjay Mohan struct kernel_siginfo info;
8236280cf71SPuranjay Mohan struct kernel_siginfo *siginfo;
8246280cf71SPuranjay Mohan
8256280cf71SPuranjay Mohan if (!task) {
8266280cf71SPuranjay Mohan task = current;
8276280cf71SPuranjay Mohan siginfo = SEND_SIG_PRIV;
8286280cf71SPuranjay Mohan } else {
8296280cf71SPuranjay Mohan clear_siginfo(&info);
8306280cf71SPuranjay Mohan info.si_signo = sig;
8316280cf71SPuranjay Mohan info.si_errno = 0;
8326280cf71SPuranjay Mohan info.si_code = SI_KERNEL;
8336280cf71SPuranjay Mohan info.si_pid = 0;
8346280cf71SPuranjay Mohan info.si_uid = 0;
8356280cf71SPuranjay Mohan info.si_value.sival_ptr = (void *)(unsigned long)value;
8366280cf71SPuranjay Mohan siginfo = &info;
8376280cf71SPuranjay Mohan }
8388b401f9eSYonghong Song
8398b401f9eSYonghong Song /* Similar to bpf_probe_write_user, task needs to be
8408b401f9eSYonghong Song * in a sound condition and kernel memory access be
8418b401f9eSYonghong Song * permitted in order to send signal to the current
8428b401f9eSYonghong Song * task.
8438b401f9eSYonghong Song */
8446280cf71SPuranjay Mohan if (unlikely(task->flags & (PF_KTHREAD | PF_EXITING)))
8458b401f9eSYonghong Song return -EPERM;
8468b401f9eSYonghong Song if (unlikely(!nmi_uaccess_okay()))
8478b401f9eSYonghong Song return -EPERM;
848a3d81bc1SHao Sun /* Task should not be pid=1 to avoid kernel panic. */
8496280cf71SPuranjay Mohan if (unlikely(is_global_init(task)))
850a3d81bc1SHao Sun return -EPERM;
8518b401f9eSYonghong Song
852b4a8b5bbSHou Tao if (preempt_count() != 0 || irqs_disabled()) {
853e1afb702SYonghong Song /* Do an early check on signal validity. Otherwise,
854e1afb702SYonghong Song * the error is lost in deferred irq_work.
855e1afb702SYonghong Song */
856e1afb702SYonghong Song if (unlikely(!valid_signal(sig)))
857e1afb702SYonghong Song return -EINVAL;
858e1afb702SYonghong Song
8598b401f9eSYonghong Song work = this_cpu_ptr(&send_signal_work);
8607a9f50a0SPeter Zijlstra if (irq_work_is_busy(&work->irq_work))
8618b401f9eSYonghong Song return -EBUSY;
8628b401f9eSYonghong Song
8638b401f9eSYonghong Song /* Add the current task, which is the target of sending signal,
8648b401f9eSYonghong Song * to the irq_work. The current task may change when queued
8658b401f9eSYonghong Song * irq works get executed.
8668b401f9eSYonghong Song */
8676280cf71SPuranjay Mohan work->task = get_task_struct(task);
8686280cf71SPuranjay Mohan work->has_siginfo = siginfo == &info;
8696280cf71SPuranjay Mohan if (work->has_siginfo)
8706280cf71SPuranjay Mohan copy_siginfo(&work->info, &info);
8718b401f9eSYonghong Song work->sig = sig;
8728482941fSYonghong Song work->type = type;
8738b401f9eSYonghong Song irq_work_queue(&work->irq_work);
8748b401f9eSYonghong Song return 0;
8758b401f9eSYonghong Song }
8768b401f9eSYonghong Song
8776280cf71SPuranjay Mohan return group_send_sig_info(sig, siginfo, task, type);
8788482941fSYonghong Song }
8798482941fSYonghong Song
BPF_CALL_1(bpf_send_signal,u32,sig)8808482941fSYonghong Song BPF_CALL_1(bpf_send_signal, u32, sig)
8818482941fSYonghong Song {
8826280cf71SPuranjay Mohan return bpf_send_signal_common(sig, PIDTYPE_TGID, NULL, 0);
8838b401f9eSYonghong Song }
8848b401f9eSYonghong Song
8858b401f9eSYonghong Song static const struct bpf_func_proto bpf_send_signal_proto = {
8868b401f9eSYonghong Song .func = bpf_send_signal,
8878b401f9eSYonghong Song .gpl_only = false,
8888b401f9eSYonghong Song .ret_type = RET_INTEGER,
8898b401f9eSYonghong Song .arg1_type = ARG_ANYTHING,
8908b401f9eSYonghong Song };
8918b401f9eSYonghong Song
BPF_CALL_1(bpf_send_signal_thread,u32,sig)8928482941fSYonghong Song BPF_CALL_1(bpf_send_signal_thread, u32, sig)
8938482941fSYonghong Song {
8946280cf71SPuranjay Mohan return bpf_send_signal_common(sig, PIDTYPE_PID, NULL, 0);
8958482941fSYonghong Song }
8968482941fSYonghong Song
8978482941fSYonghong Song static const struct bpf_func_proto bpf_send_signal_thread_proto = {
8988482941fSYonghong Song .func = bpf_send_signal_thread,
8998482941fSYonghong Song .gpl_only = false,
9008482941fSYonghong Song .ret_type = RET_INTEGER,
9018482941fSYonghong Song .arg1_type = ARG_ANYTHING,
9028482941fSYonghong Song };
9038482941fSYonghong Song
BPF_CALL_3(bpf_d_path,struct path *,path,char *,buf,u32,sz)9046e22ab9dSJiri Olsa BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
9056e22ab9dSJiri Olsa {
906f46fab0eSJiri Olsa struct path copy;
9076e22ab9dSJiri Olsa long len;
9086e22ab9dSJiri Olsa char *p;
9096e22ab9dSJiri Olsa
9106e22ab9dSJiri Olsa if (!sz)
9116e22ab9dSJiri Olsa return 0;
9126e22ab9dSJiri Olsa
913f46fab0eSJiri Olsa /*
914f46fab0eSJiri Olsa * The path pointer is verified as trusted and safe to use,
915f46fab0eSJiri Olsa * but let's double check it's valid anyway to workaround
916f46fab0eSJiri Olsa * potentially broken verifier.
917f46fab0eSJiri Olsa */
918f46fab0eSJiri Olsa len = copy_from_kernel_nofault(©, path, sizeof(*path));
919f46fab0eSJiri Olsa if (len < 0)
920f46fab0eSJiri Olsa return len;
921f46fab0eSJiri Olsa
922f46fab0eSJiri Olsa p = d_path(©, buf, sz);
9236e22ab9dSJiri Olsa if (IS_ERR(p)) {
9246e22ab9dSJiri Olsa len = PTR_ERR(p);
9256e22ab9dSJiri Olsa } else {
9266e22ab9dSJiri Olsa len = buf + sz - p;
9276e22ab9dSJiri Olsa memmove(buf, p, len);
9286e22ab9dSJiri Olsa }
9296e22ab9dSJiri Olsa
9306e22ab9dSJiri Olsa return len;
9316e22ab9dSJiri Olsa }
9326e22ab9dSJiri Olsa
9336e22ab9dSJiri Olsa BTF_SET_START(btf_allowlist_d_path)
934a8a71796SJiri Olsa #ifdef CONFIG_SECURITY
BTF_ID(func,security_file_permission)935a8a71796SJiri Olsa BTF_ID(func, security_file_permission)
936a8a71796SJiri Olsa BTF_ID(func, security_inode_getattr)
937a8a71796SJiri Olsa BTF_ID(func, security_file_open)
938a8a71796SJiri Olsa #endif
939a8a71796SJiri Olsa #ifdef CONFIG_SECURITY_PATH
940a8a71796SJiri Olsa BTF_ID(func, security_path_truncate)
941a8a71796SJiri Olsa #endif
9426e22ab9dSJiri Olsa BTF_ID(func, vfs_truncate)
9436e22ab9dSJiri Olsa BTF_ID(func, vfs_fallocate)
9446e22ab9dSJiri Olsa BTF_ID(func, dentry_open)
9456e22ab9dSJiri Olsa BTF_ID(func, vfs_getattr)
9466e22ab9dSJiri Olsa BTF_ID(func, filp_close)
9476e22ab9dSJiri Olsa BTF_SET_END(btf_allowlist_d_path)
9486e22ab9dSJiri Olsa
9496e22ab9dSJiri Olsa static bool bpf_d_path_allowed(const struct bpf_prog *prog)
9506e22ab9dSJiri Olsa {
9513d06f34aSSong Liu if (prog->type == BPF_PROG_TYPE_TRACING &&
9523d06f34aSSong Liu prog->expected_attach_type == BPF_TRACE_ITER)
9533d06f34aSSong Liu return true;
9543d06f34aSSong Liu
9556f100640SKP Singh if (prog->type == BPF_PROG_TYPE_LSM)
9566f100640SKP Singh return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
9576f100640SKP Singh
9586f100640SKP Singh return btf_id_set_contains(&btf_allowlist_d_path,
9596f100640SKP Singh prog->aux->attach_btf_id);
9606e22ab9dSJiri Olsa }
9616e22ab9dSJiri Olsa
9629436ef6eSLorenz Bauer BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
9636e22ab9dSJiri Olsa
9646e22ab9dSJiri Olsa static const struct bpf_func_proto bpf_d_path_proto = {
9656e22ab9dSJiri Olsa .func = bpf_d_path,
9666e22ab9dSJiri Olsa .gpl_only = false,
9676e22ab9dSJiri Olsa .ret_type = RET_INTEGER,
9686e22ab9dSJiri Olsa .arg1_type = ARG_PTR_TO_BTF_ID,
9699436ef6eSLorenz Bauer .arg1_btf_id = &bpf_d_path_btf_ids[0],
9706e22ab9dSJiri Olsa .arg2_type = ARG_PTR_TO_MEM,
9716e22ab9dSJiri Olsa .arg3_type = ARG_CONST_SIZE_OR_ZERO,
9726e22ab9dSJiri Olsa .allowed = bpf_d_path_allowed,
9736e22ab9dSJiri Olsa };
9746e22ab9dSJiri Olsa
975c4d0bfb4SAlan Maguire #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
976c4d0bfb4SAlan Maguire BTF_F_PTR_RAW | BTF_F_ZERO)
977c4d0bfb4SAlan Maguire
bpf_btf_printf_prepare(struct btf_ptr * ptr,u32 btf_ptr_size,u64 flags,const struct btf ** btf,s32 * btf_id)978c4d0bfb4SAlan Maguire static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
979c4d0bfb4SAlan Maguire u64 flags, const struct btf **btf,
980c4d0bfb4SAlan Maguire s32 *btf_id)
981c4d0bfb4SAlan Maguire {
982c4d0bfb4SAlan Maguire const struct btf_type *t;
983c4d0bfb4SAlan Maguire
984c4d0bfb4SAlan Maguire if (unlikely(flags & ~(BTF_F_ALL)))
985c4d0bfb4SAlan Maguire return -EINVAL;
986c4d0bfb4SAlan Maguire
987c4d0bfb4SAlan Maguire if (btf_ptr_size != sizeof(struct btf_ptr))
988c4d0bfb4SAlan Maguire return -EINVAL;
989c4d0bfb4SAlan Maguire
990c4d0bfb4SAlan Maguire *btf = bpf_get_btf_vmlinux();
991c4d0bfb4SAlan Maguire
992c4d0bfb4SAlan Maguire if (IS_ERR_OR_NULL(*btf))
993abbaa433SWang Qing return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
994c4d0bfb4SAlan Maguire
995c4d0bfb4SAlan Maguire if (ptr->type_id > 0)
996c4d0bfb4SAlan Maguire *btf_id = ptr->type_id;
997c4d0bfb4SAlan Maguire else
998c4d0bfb4SAlan Maguire return -EINVAL;
999c4d0bfb4SAlan Maguire
1000c4d0bfb4SAlan Maguire if (*btf_id > 0)
1001c4d0bfb4SAlan Maguire t = btf_type_by_id(*btf, *btf_id);
1002c4d0bfb4SAlan Maguire if (*btf_id <= 0 || !t)
1003c4d0bfb4SAlan Maguire return -ENOENT;
1004c4d0bfb4SAlan Maguire
1005c4d0bfb4SAlan Maguire return 0;
1006c4d0bfb4SAlan Maguire }
1007c4d0bfb4SAlan Maguire
BPF_CALL_5(bpf_snprintf_btf,char *,str,u32,str_size,struct btf_ptr *,ptr,u32,btf_ptr_size,u64,flags)1008c4d0bfb4SAlan Maguire BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1009c4d0bfb4SAlan Maguire u32, btf_ptr_size, u64, flags)
1010c4d0bfb4SAlan Maguire {
1011c4d0bfb4SAlan Maguire const struct btf *btf;
1012c4d0bfb4SAlan Maguire s32 btf_id;
1013c4d0bfb4SAlan Maguire int ret;
1014c4d0bfb4SAlan Maguire
1015c4d0bfb4SAlan Maguire ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1016c4d0bfb4SAlan Maguire if (ret)
1017c4d0bfb4SAlan Maguire return ret;
1018c4d0bfb4SAlan Maguire
1019c4d0bfb4SAlan Maguire return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1020c4d0bfb4SAlan Maguire flags);
1021c4d0bfb4SAlan Maguire }
1022c4d0bfb4SAlan Maguire
1023c4d0bfb4SAlan Maguire const struct bpf_func_proto bpf_snprintf_btf_proto = {
1024c4d0bfb4SAlan Maguire .func = bpf_snprintf_btf,
1025c4d0bfb4SAlan Maguire .gpl_only = false,
1026c4d0bfb4SAlan Maguire .ret_type = RET_INTEGER,
1027c4d0bfb4SAlan Maguire .arg1_type = ARG_PTR_TO_MEM,
1028c4d0bfb4SAlan Maguire .arg2_type = ARG_CONST_SIZE,
1029216e3cd2SHao Luo .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1030c4d0bfb4SAlan Maguire .arg4_type = ARG_CONST_SIZE,
1031c4d0bfb4SAlan Maguire .arg5_type = ARG_ANYTHING,
1032c4d0bfb4SAlan Maguire };
1033c4d0bfb4SAlan Maguire
BPF_CALL_1(bpf_get_func_ip_tracing,void *,ctx)10349b99edcaSJiri Olsa BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
10359b99edcaSJiri Olsa {
10369b99edcaSJiri Olsa /* This helper call is inlined by verifier. */
1037f92c1e18SJiri Olsa return ((u64 *)ctx)[-2];
10389b99edcaSJiri Olsa }
10399b99edcaSJiri Olsa
10409b99edcaSJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
10419b99edcaSJiri Olsa .func = bpf_get_func_ip_tracing,
10429b99edcaSJiri Olsa .gpl_only = true,
10439b99edcaSJiri Olsa .ret_type = RET_INTEGER,
10449b99edcaSJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
10459b99edcaSJiri Olsa };
10469b99edcaSJiri Olsa
get_entry_ip(unsigned long fentry_ip)1047c09eb2e5SJiri Olsa static inline unsigned long get_entry_ip(unsigned long fentry_ip)
1048c09eb2e5SJiri Olsa {
1049c09eb2e5SJiri Olsa #ifdef CONFIG_X86_KERNEL_IBT
1050c09eb2e5SJiri Olsa if (is_endbr((void *)(fentry_ip - ENDBR_INSN_SIZE)))
1051c09eb2e5SJiri Olsa fentry_ip -= ENDBR_INSN_SIZE;
1052a8497506SAndrii Nakryiko #endif
1053a8497506SAndrii Nakryiko return fentry_ip;
1054a8497506SAndrii Nakryiko }
1055a8497506SAndrii Nakryiko
BPF_CALL_1(bpf_get_func_ip_kprobe,struct pt_regs *,regs)1056a8497506SAndrii Nakryiko BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1057c09eb2e5SJiri Olsa {
1058a8497506SAndrii Nakryiko struct bpf_trace_run_ctx *run_ctx __maybe_unused;
1059a8497506SAndrii Nakryiko struct kprobe *kp;
1060a8497506SAndrii Nakryiko
1061c09eb2e5SJiri Olsa #ifdef CONFIG_UPROBES
1062c09eb2e5SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1063c09eb2e5SJiri Olsa if (run_ctx->is_uprobe)
1064c09eb2e5SJiri Olsa return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr;
1065c09eb2e5SJiri Olsa #endif
1066c09eb2e5SJiri Olsa
1067c09eb2e5SJiri Olsa kp = kprobe_running();
1068c09eb2e5SJiri Olsa
10699ffd9f3fSJiri Olsa if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
10709ffd9f3fSJiri Olsa return 0;
1071a3c485a5SJiri Olsa
1072a3c485a5SJiri Olsa return get_entry_ip((uintptr_t)kp->addr);
1073a3c485a5SJiri Olsa }
1074a3c485a5SJiri Olsa
1075a3c485a5SJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1076a3c485a5SJiri Olsa .func = bpf_get_func_ip_kprobe,
1077a3c485a5SJiri Olsa .gpl_only = true,
1078a3c485a5SJiri Olsa .ret_type = RET_INTEGER,
1079a3c485a5SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1080a3c485a5SJiri Olsa };
10819ffd9f3fSJiri Olsa
BPF_CALL_1(bpf_get_func_ip_kprobe_multi,struct pt_regs *,regs)10820e253f7eSJiri Olsa BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
10830e253f7eSJiri Olsa {
10840e253f7eSJiri Olsa return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
10850e253f7eSJiri Olsa }
10869ffd9f3fSJiri Olsa
10879ffd9f3fSJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
10889ffd9f3fSJiri Olsa .func = bpf_get_func_ip_kprobe_multi,
10899ffd9f3fSJiri Olsa .gpl_only = false,
10909ffd9f3fSJiri Olsa .ret_type = RET_INTEGER,
10919ffd9f3fSJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
10929ffd9f3fSJiri Olsa };
10939ffd9f3fSJiri Olsa
BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi,struct pt_regs *,regs)10949ffd9f3fSJiri Olsa BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
109542a57120SJiri Olsa {
109642a57120SJiri Olsa return bpf_kprobe_multi_cookie(current->bpf_ctx);
1097f7098690SJiri Olsa }
109842a57120SJiri Olsa
109942a57120SJiri Olsa static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
110042a57120SJiri Olsa .func = bpf_get_attach_cookie_kprobe_multi,
110142a57120SJiri Olsa .gpl_only = false,
110242a57120SJiri Olsa .ret_type = RET_INTEGER,
110342a57120SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
110442a57120SJiri Olsa };
110542a57120SJiri Olsa
BPF_CALL_1(bpf_get_func_ip_uprobe_multi,struct pt_regs *,regs)110642a57120SJiri Olsa BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs)
1107ca74823cSJiri Olsa {
1108ca74823cSJiri Olsa return bpf_uprobe_multi_entry_ip(current->bpf_ctx);
1109f7098690SJiri Olsa }
1110ca74823cSJiri Olsa
1111ca74823cSJiri Olsa static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = {
1112ca74823cSJiri Olsa .func = bpf_get_func_ip_uprobe_multi,
1113ca74823cSJiri Olsa .gpl_only = false,
1114ca74823cSJiri Olsa .ret_type = RET_INTEGER,
1115ca74823cSJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1116ca74823cSJiri Olsa };
1117ca74823cSJiri Olsa
BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi,struct pt_regs *,regs)1118ca74823cSJiri Olsa BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs)
1119686328d8SJiri Olsa {
1120686328d8SJiri Olsa return bpf_uprobe_multi_cookie(current->bpf_ctx);
1121686328d8SJiri Olsa }
1122686328d8SJiri Olsa
1123686328d8SJiri Olsa static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = {
1124686328d8SJiri Olsa .func = bpf_get_attach_cookie_uprobe_multi,
1125686328d8SJiri Olsa .gpl_only = false,
1126686328d8SJiri Olsa .ret_type = RET_INTEGER,
1127686328d8SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1128686328d8SJiri Olsa };
1129686328d8SJiri Olsa
BPF_CALL_1(bpf_get_attach_cookie_trace,void *,ctx)1130686328d8SJiri Olsa BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
11310b779b61SJiri Olsa {
11320b779b61SJiri Olsa struct bpf_trace_run_ctx *run_ctx;
11330b779b61SJiri Olsa
11340b779b61SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
11350b779b61SJiri Olsa return run_ctx->bpf_cookie;
11360b779b61SJiri Olsa }
11370b779b61SJiri Olsa
11380b779b61SJiri Olsa static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
11390b779b61SJiri Olsa .func = bpf_get_attach_cookie_trace,
11400b779b61SJiri Olsa .gpl_only = false,
11410b779b61SJiri Olsa .ret_type = RET_INTEGER,
11420b779b61SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
11437adfc6c9SAndrii Nakryiko };
11447adfc6c9SAndrii Nakryiko
BPF_CALL_1(bpf_get_attach_cookie_pe,struct bpf_perf_event_data_kern *,ctx)11457adfc6c9SAndrii Nakryiko BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
11467adfc6c9SAndrii Nakryiko {
11477adfc6c9SAndrii Nakryiko return ctx->event->bpf_cookie;
11487adfc6c9SAndrii Nakryiko }
11497adfc6c9SAndrii Nakryiko
11507adfc6c9SAndrii Nakryiko static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
11517adfc6c9SAndrii Nakryiko .func = bpf_get_attach_cookie_pe,
11527adfc6c9SAndrii Nakryiko .gpl_only = false,
11537adfc6c9SAndrii Nakryiko .ret_type = RET_INTEGER,
11547adfc6c9SAndrii Nakryiko .arg1_type = ARG_PTR_TO_CTX,
11557adfc6c9SAndrii Nakryiko };
11567adfc6c9SAndrii Nakryiko
BPF_CALL_1(bpf_get_attach_cookie_tracing,void *,ctx)11577adfc6c9SAndrii Nakryiko BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
11587adfc6c9SAndrii Nakryiko {
11597adfc6c9SAndrii Nakryiko struct bpf_trace_run_ctx *run_ctx;
11607adfc6c9SAndrii Nakryiko
11617adfc6c9SAndrii Nakryiko run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
11627adfc6c9SAndrii Nakryiko return run_ctx->bpf_cookie;
11637adfc6c9SAndrii Nakryiko }
11647adfc6c9SAndrii Nakryiko
11657adfc6c9SAndrii Nakryiko static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
11667adfc6c9SAndrii Nakryiko .func = bpf_get_attach_cookie_tracing,
11677adfc6c9SAndrii Nakryiko .gpl_only = false,
11687adfc6c9SAndrii Nakryiko .ret_type = RET_INTEGER,
11697adfc6c9SAndrii Nakryiko .arg1_type = ARG_PTR_TO_CTX,
11702fcc8241SKui-Feng Lee };
11712fcc8241SKui-Feng Lee
BPF_CALL_3(bpf_get_branch_snapshot,void *,buf,u32,size,u64,flags)11722fcc8241SKui-Feng Lee BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
11732fcc8241SKui-Feng Lee {
11742fcc8241SKui-Feng Lee static const u32 br_entry_size = sizeof(struct perf_branch_entry);
11752fcc8241SKui-Feng Lee u32 entry_cnt = size / br_entry_size;
11762fcc8241SKui-Feng Lee
11772fcc8241SKui-Feng Lee entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
11782fcc8241SKui-Feng Lee
11792fcc8241SKui-Feng Lee if (unlikely(flags))
11802fcc8241SKui-Feng Lee return -EINVAL;
11812fcc8241SKui-Feng Lee
11822fcc8241SKui-Feng Lee if (!entry_cnt)
11832fcc8241SKui-Feng Lee return -ENOENT;
11842fcc8241SKui-Feng Lee
1185856c02dbSSong Liu return entry_cnt * br_entry_size;
1186856c02dbSSong Liu }
1187856c02dbSSong Liu
1188856c02dbSSong Liu static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1189856c02dbSSong Liu .func = bpf_get_branch_snapshot,
1190856c02dbSSong Liu .gpl_only = true,
1191856c02dbSSong Liu .ret_type = RET_INTEGER,
1192856c02dbSSong Liu .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1193856c02dbSSong Liu .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1194856c02dbSSong Liu };
1195856c02dbSSong Liu
BPF_CALL_3(get_func_arg,void *,ctx,u32,n,u64 *,value)1196856c02dbSSong Liu BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1197856c02dbSSong Liu {
1198856c02dbSSong Liu /* This helper call is inlined by verifier. */
1199856c02dbSSong Liu u64 nr_args = ((u64 *)ctx)[-1];
1200856c02dbSSong Liu
1201856c02dbSSong Liu if ((u64) n >= nr_args)
1202856c02dbSSong Liu return -EINVAL;
1203856c02dbSSong Liu *value = ((u64 *)ctx)[n];
1204856c02dbSSong Liu return 0;
1205856c02dbSSong Liu }
1206856c02dbSSong Liu
1207856c02dbSSong Liu static const struct bpf_func_proto bpf_get_func_arg_proto = {
1208856c02dbSSong Liu .func = get_func_arg,
1209f92c1e18SJiri Olsa .ret_type = RET_INTEGER,
1210f92c1e18SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1211f92c1e18SJiri Olsa .arg2_type = ARG_ANYTHING,
1212f92c1e18SJiri Olsa .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
1213f92c1e18SJiri Olsa .arg3_size = sizeof(u64),
1214f92c1e18SJiri Olsa };
1215f92c1e18SJiri Olsa
BPF_CALL_2(get_func_ret,void *,ctx,u64 *,value)1216f92c1e18SJiri Olsa BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1217f92c1e18SJiri Olsa {
1218f92c1e18SJiri Olsa /* This helper call is inlined by verifier. */
1219f92c1e18SJiri Olsa u64 nr_args = ((u64 *)ctx)[-1];
1220f92c1e18SJiri Olsa
1221f92c1e18SJiri Olsa *value = ((u64 *)ctx)[nr_args];
1222f92c1e18SJiri Olsa return 0;
1223f92c1e18SJiri Olsa }
1224f92c1e18SJiri Olsa
12256fad274fSDaniel Borkmann static const struct bpf_func_proto bpf_get_func_ret_proto = {
122632556ce9SDaniel Borkmann .func = get_func_ret,
1227f92c1e18SJiri Olsa .ret_type = RET_INTEGER,
1228f92c1e18SJiri Olsa .arg1_type = ARG_PTR_TO_CTX,
1229f92c1e18SJiri Olsa .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
1230f92c1e18SJiri Olsa .arg2_size = sizeof(u64),
1231f92c1e18SJiri Olsa };
1232f92c1e18SJiri Olsa
BPF_CALL_1(get_func_arg_cnt,void *,ctx)1233f92c1e18SJiri Olsa BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1234f92c1e18SJiri Olsa {
1235f92c1e18SJiri Olsa /* This helper call is inlined by verifier. */
1236f92c1e18SJiri Olsa return ((u64 *)ctx)[-1];
1237f92c1e18SJiri Olsa }
1238f92c1e18SJiri Olsa
1239f92c1e18SJiri Olsa static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1240f92c1e18SJiri Olsa .func = get_func_arg_cnt,
1241f92c1e18SJiri Olsa .ret_type = RET_INTEGER,
12426fad274fSDaniel Borkmann .arg1_type = ARG_PTR_TO_CTX,
124332556ce9SDaniel Borkmann };
1244f92c1e18SJiri Olsa
1245f92c1e18SJiri Olsa #ifdef CONFIG_KEYS
1246f92c1e18SJiri Olsa __bpf_kfunc_start_defs();
1247f92c1e18SJiri Olsa
1248f92c1e18SJiri Olsa /**
1249f92c1e18SJiri Olsa * bpf_lookup_user_key - lookup a key by its serial
1250f92c1e18SJiri Olsa * @serial: key handle serial number
1251f92c1e18SJiri Olsa * @flags: lookup-specific flags
1252f92c1e18SJiri Olsa *
1253f92c1e18SJiri Olsa * Search a key with a given *serial* and the provided *flags*.
1254f92c1e18SJiri Olsa * If found, increment the reference count of the key by one, and
1255f92c1e18SJiri Olsa * return it in the bpf_key structure.
1256f92c1e18SJiri Olsa *
1257f92c1e18SJiri Olsa * The bpf_key structure must be passed to bpf_key_put() when done
1258f3cf4134SRoberto Sassu * with it, so that the key reference count is decremented and the
1259391145baSDave Marchevsky * bpf_key structure is freed.
1260f3cf4134SRoberto Sassu *
1261f3cf4134SRoberto Sassu * Permission checks are deferred to the time the key is used by
1262f3cf4134SRoberto Sassu * one of the available key-specific kfuncs.
1263f3cf4134SRoberto Sassu *
1264f3cf4134SRoberto Sassu * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
1265f3cf4134SRoberto Sassu * special keyring (e.g. session keyring), if it doesn't yet exist.
1266f3cf4134SRoberto Sassu * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
1267f3cf4134SRoberto Sassu * for the key construction, and to retrieve uninstantiated keys (keys
1268f3cf4134SRoberto Sassu * without data attached to them).
1269f3cf4134SRoberto Sassu *
1270f3cf4134SRoberto Sassu * Return: a bpf_key pointer with a valid key pointer if the key is found, a
1271f3cf4134SRoberto Sassu * NULL pointer otherwise.
1272f3cf4134SRoberto Sassu */
bpf_lookup_user_key(u32 serial,u64 flags)1273f3cf4134SRoberto Sassu __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
1274f3cf4134SRoberto Sassu {
1275f3cf4134SRoberto Sassu key_ref_t key_ref;
1276f3cf4134SRoberto Sassu struct bpf_key *bkey;
1277f3cf4134SRoberto Sassu
1278f3cf4134SRoberto Sassu if (flags & ~KEY_LOOKUP_ALL)
1279f3cf4134SRoberto Sassu return NULL;
1280f3cf4134SRoberto Sassu
1281f3cf4134SRoberto Sassu /*
1282f3cf4134SRoberto Sassu * Permission check is deferred until the key is used, as the
1283f3cf4134SRoberto Sassu * intent of the caller is unknown here.
1284f3cf4134SRoberto Sassu */
1285f3cf4134SRoberto Sassu key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
1286400031e0SDavid Vernet if (IS_ERR(key_ref))
1287f3cf4134SRoberto Sassu return NULL;
1288f3cf4134SRoberto Sassu
1289f3cf4134SRoberto Sassu bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
1290f3cf4134SRoberto Sassu if (!bkey) {
1291f3cf4134SRoberto Sassu key_put(key_ref_to_ptr(key_ref));
1292f3cf4134SRoberto Sassu return NULL;
1293f3cf4134SRoberto Sassu }
1294f3cf4134SRoberto Sassu
1295f3cf4134SRoberto Sassu bkey->key = key_ref_to_ptr(key_ref);
1296f3cf4134SRoberto Sassu bkey->has_ref = true;
1297f3cf4134SRoberto Sassu
1298f3cf4134SRoberto Sassu return bkey;
1299f3cf4134SRoberto Sassu }
1300f3cf4134SRoberto Sassu
1301f3cf4134SRoberto Sassu /**
1302f3cf4134SRoberto Sassu * bpf_lookup_system_key - lookup a key by a system-defined ID
1303f3cf4134SRoberto Sassu * @id: key ID
1304f3cf4134SRoberto Sassu *
1305f3cf4134SRoberto Sassu * Obtain a bpf_key structure with a key pointer set to the passed key ID.
1306f3cf4134SRoberto Sassu * The key pointer is marked as invalid, to prevent bpf_key_put() from
1307f3cf4134SRoberto Sassu * attempting to decrement the key reference count on that pointer. The key
1308f3cf4134SRoberto Sassu * pointer set in such way is currently understood only by
1309f3cf4134SRoberto Sassu * verify_pkcs7_signature().
1310f3cf4134SRoberto Sassu *
1311f3cf4134SRoberto Sassu * Set *id* to one of the values defined in include/linux/verification.h:
1312f3cf4134SRoberto Sassu * 0 for the primary keyring (immutable keyring of system keys);
1313f3cf4134SRoberto Sassu * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
1314f3cf4134SRoberto Sassu * (where keys can be added only if they are vouched for by existing keys
1315f3cf4134SRoberto Sassu * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
1316f3cf4134SRoberto Sassu * keyring (primarily used by the integrity subsystem to verify a kexec'ed
1317f3cf4134SRoberto Sassu * kerned image and, possibly, the initramfs signature).
1318f3cf4134SRoberto Sassu *
1319f3cf4134SRoberto Sassu * Return: a bpf_key pointer with an invalid key pointer set from the
1320f3cf4134SRoberto Sassu * pre-determined ID on success, a NULL pointer otherwise
1321f3cf4134SRoberto Sassu */
bpf_lookup_system_key(u64 id)1322f3cf4134SRoberto Sassu __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id)
1323f3cf4134SRoberto Sassu {
1324f3cf4134SRoberto Sassu struct bpf_key *bkey;
1325f3cf4134SRoberto Sassu
1326f3cf4134SRoberto Sassu if (system_keyring_id_check(id) < 0)
1327f3cf4134SRoberto Sassu return NULL;
1328f3cf4134SRoberto Sassu
1329f3cf4134SRoberto Sassu bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
1330f3cf4134SRoberto Sassu if (!bkey)
1331f3cf4134SRoberto Sassu return NULL;
1332f3cf4134SRoberto Sassu
1333f3cf4134SRoberto Sassu bkey->key = (struct key *)(unsigned long)id;
1334f3cf4134SRoberto Sassu bkey->has_ref = false;
1335400031e0SDavid Vernet
1336f3cf4134SRoberto Sassu return bkey;
1337f3cf4134SRoberto Sassu }
1338f3cf4134SRoberto Sassu
1339f3cf4134SRoberto Sassu /**
1340f3cf4134SRoberto Sassu * bpf_key_put - decrement key reference count if key is valid and free bpf_key
1341f3cf4134SRoberto Sassu * @bkey: bpf_key structure
1342f3cf4134SRoberto Sassu *
1343f3cf4134SRoberto Sassu * Decrement the reference count of the key inside *bkey*, if the pointer
1344f3cf4134SRoberto Sassu * is valid, and free *bkey*.
1345f3cf4134SRoberto Sassu */
bpf_key_put(struct bpf_key * bkey)1346f3cf4134SRoberto Sassu __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
1347f3cf4134SRoberto Sassu {
1348f3cf4134SRoberto Sassu if (bkey->has_ref)
1349f3cf4134SRoberto Sassu key_put(bkey->key);
1350f3cf4134SRoberto Sassu
1351f3cf4134SRoberto Sassu kfree(bkey);
1352f3cf4134SRoberto Sassu }
1353f3cf4134SRoberto Sassu
1354f3cf4134SRoberto Sassu #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1355f3cf4134SRoberto Sassu /**
1356f3cf4134SRoberto Sassu * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
1357f3cf4134SRoberto Sassu * @data_p: data to verify
1358f3cf4134SRoberto Sassu * @sig_p: signature of the data
1359400031e0SDavid Vernet * @trusted_keyring: keyring with keys trusted for signature verification
1360f3cf4134SRoberto Sassu *
1361f3cf4134SRoberto Sassu * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
1362f3cf4134SRoberto Sassu * with keys in a keyring referenced by *trusted_keyring*.
1363f3cf4134SRoberto Sassu *
1364f3cf4134SRoberto Sassu * Return: 0 on success, a negative value on error.
1365f3cf4134SRoberto Sassu */
bpf_verify_pkcs7_signature(struct bpf_dynptr * data_p,struct bpf_dynptr * sig_p,struct bpf_key * trusted_keyring)1366f3cf4134SRoberto Sassu __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_p,
1367865b0566SRoberto Sassu struct bpf_dynptr *sig_p,
1368865b0566SRoberto Sassu struct bpf_key *trusted_keyring)
1369865b0566SRoberto Sassu {
1370cce4c40bSDaniel Xu struct bpf_dynptr_kern *data_ptr = (struct bpf_dynptr_kern *)data_p;
1371cce4c40bSDaniel Xu struct bpf_dynptr_kern *sig_ptr = (struct bpf_dynptr_kern *)sig_p;
1372865b0566SRoberto Sassu const void *data, *sig;
1373865b0566SRoberto Sassu u32 data_len, sig_len;
1374865b0566SRoberto Sassu int ret;
1375865b0566SRoberto Sassu
1376865b0566SRoberto Sassu if (trusted_keyring->has_ref) {
1377865b0566SRoberto Sassu /*
1378865b0566SRoberto Sassu * Do the permission check deferred in bpf_lookup_user_key().
1379cce4c40bSDaniel Xu * See bpf_lookup_user_key() for more details.
1380cce4c40bSDaniel Xu *
1381865b0566SRoberto Sassu * A call to key_task_permission() here would be redundant, as
1382865b0566SRoberto Sassu * it is already done by keyring_search() called by
1383cce4c40bSDaniel Xu * find_asymmetric_key().
1384cce4c40bSDaniel Xu */
138574523c06SSong Liu ret = key_validate(trusted_keyring->key);
138674523c06SSong Liu if (ret < 0)
1387865b0566SRoberto Sassu return ret;
1388865b0566SRoberto Sassu }
1389865b0566SRoberto Sassu
1390865b0566SRoberto Sassu data_len = __bpf_dynptr_size(data_ptr);
1391865b0566SRoberto Sassu data = __bpf_dynptr_data(data_ptr, data_len);
1392865b0566SRoberto Sassu sig_len = __bpf_dynptr_size(sig_ptr);
1393865b0566SRoberto Sassu sig = __bpf_dynptr_data(sig_ptr, sig_len);
1394865b0566SRoberto Sassu
1395865b0566SRoberto Sassu return verify_pkcs7_signature(data, data_len, sig, sig_len,
1396865b0566SRoberto Sassu trusted_keyring->key,
1397865b0566SRoberto Sassu VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
1398865b0566SRoberto Sassu NULL);
1399865b0566SRoberto Sassu }
1400865b0566SRoberto Sassu #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
1401865b0566SRoberto Sassu
1402865b0566SRoberto Sassu __bpf_kfunc_end_defs();
140374523c06SSong Liu
140474523c06SSong Liu BTF_KFUNCS_START(key_sig_kfunc_set)
140574523c06SSong Liu BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
140674523c06SSong Liu BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
140774523c06SSong Liu BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
140874523c06SSong Liu #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1409865b0566SRoberto Sassu BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
1410865b0566SRoberto Sassu #endif
1411865b0566SRoberto Sassu BTF_KFUNCS_END(key_sig_kfunc_set)
1412865b0566SRoberto Sassu
1413865b0566SRoberto Sassu static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
1414865b0566SRoberto Sassu .owner = THIS_MODULE,
1415391145baSDave Marchevsky .set = &key_sig_kfunc_set,
1416f3cf4134SRoberto Sassu };
14176f3189f3SDaniel Xu
bpf_key_sig_kfuncs_init(void)1418f3cf4134SRoberto Sassu static int __init bpf_key_sig_kfuncs_init(void)
1419f3cf4134SRoberto Sassu {
1420f3cf4134SRoberto Sassu return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
1421865b0566SRoberto Sassu &bpf_key_sig_kfunc_set);
1422865b0566SRoberto Sassu }
1423865b0566SRoberto Sassu
14246f3189f3SDaniel Xu late_initcall(bpf_key_sig_kfuncs_init);
1425f3cf4134SRoberto Sassu #endif /* CONFIG_KEYS */
1426f3cf4134SRoberto Sassu
1427f3cf4134SRoberto Sassu static const struct bpf_func_proto *
bpf_tracing_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1428f3cf4134SRoberto Sassu bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1429f3cf4134SRoberto Sassu {
1430f3cf4134SRoberto Sassu const struct bpf_func_proto *func_proto;
1431f3cf4134SRoberto Sassu
1432f3cf4134SRoberto Sassu switch (func_id) {
1433f3cf4134SRoberto Sassu case BPF_FUNC_map_lookup_elem:
1434f3cf4134SRoberto Sassu return &bpf_map_lookup_elem_proto;
1435f3cf4134SRoberto Sassu case BPF_FUNC_map_update_elem:
1436f3cf4134SRoberto Sassu return &bpf_map_update_elem_proto;
1437f3cf4134SRoberto Sassu case BPF_FUNC_map_delete_elem:
1438f3cf4134SRoberto Sassu return &bpf_map_delete_elem_proto;
1439f3cf4134SRoberto Sassu case BPF_FUNC_map_push_elem:
14407adfc6c9SAndrii Nakryiko return &bpf_map_push_elem_proto;
1441fc611f47SKP Singh case BPF_FUNC_map_pop_elem:
14422541517cSAlexei Starovoitov return &bpf_map_pop_elem_proto;
14433bfb49d7SMarco Elver case BPF_FUNC_map_peek_elem:
14443bfb49d7SMarco Elver return &bpf_map_peek_elem_proto;
14452541517cSAlexei Starovoitov case BPF_FUNC_map_lookup_percpu_elem:
14462541517cSAlexei Starovoitov return &bpf_map_lookup_percpu_elem_proto;
14472541517cSAlexei Starovoitov case BPF_FUNC_ktime_get_ns:
14482541517cSAlexei Starovoitov return &bpf_ktime_get_ns_proto;
14492541517cSAlexei Starovoitov case BPF_FUNC_ktime_get_boot_ns:
14502541517cSAlexei Starovoitov return &bpf_ktime_get_boot_ns_proto;
14512541517cSAlexei Starovoitov case BPF_FUNC_tail_call:
145202a8c817SAlban Crequy return &bpf_tail_call_proto;
145302a8c817SAlban Crequy case BPF_FUNC_get_current_task:
145402a8c817SAlban Crequy return &bpf_get_current_task_proto;
145502a8c817SAlban Crequy case BPF_FUNC_get_current_task_btf:
145602a8c817SAlban Crequy return &bpf_get_current_task_btf_proto;
145702a8c817SAlban Crequy case BPF_FUNC_task_pt_regs:
145807343110SFeng Zhou return &bpf_task_pt_regs_proto;
145907343110SFeng Zhou case BPF_FUNC_get_current_uid_gid:
1460d9847d31SAlexei Starovoitov return &bpf_get_current_uid_gid_proto;
1461d9847d31SAlexei Starovoitov case BPF_FUNC_get_current_comm:
146271d19214SMaciej Żenczykowski return &bpf_get_current_comm_proto;
146371d19214SMaciej Żenczykowski case BPF_FUNC_trace_printk:
146404fd61abSAlexei Starovoitov return bpf_get_trace_printk_proto();
146504fd61abSAlexei Starovoitov case BPF_FUNC_get_smp_processor_id:
1466606274c5SAlexei Starovoitov return &bpf_get_smp_processor_id_proto;
1467606274c5SAlexei Starovoitov case BPF_FUNC_get_numa_node_id:
14683ca1032aSKP Singh return &bpf_get_numa_node_id_proto;
14693ca1032aSKP Singh case BPF_FUNC_perf_event_read:
1470dd6e10fbSDaniel Xu return &bpf_perf_event_read_proto;
1471dd6e10fbSDaniel Xu case BPF_FUNC_get_prandom_u32:
1472ffeedafbSAlexei Starovoitov return &bpf_get_prandom_u32_proto;
1473ffeedafbSAlexei Starovoitov case BPF_FUNC_probe_read_user:
1474ffeedafbSAlexei Starovoitov return &bpf_probe_read_user_proto;
1475ffeedafbSAlexei Starovoitov case BPF_FUNC_probe_read_kernel:
14769c959c86SAlexei Starovoitov return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
14770756ea3eSAlexei Starovoitov NULL : &bpf_probe_read_kernel_proto;
1478ab1973d3SAlexei Starovoitov case BPF_FUNC_probe_read_user_str:
1479ab1973d3SAlexei Starovoitov return &bpf_probe_read_user_str_proto;
14802d0e30c3SDaniel Borkmann case BPF_FUNC_probe_read_kernel_str:
14812d0e30c3SDaniel Borkmann return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
148235578d79SKaixu Xia NULL : &bpf_probe_read_kernel_str_proto;
148335578d79SKaixu Xia #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
14848937bd80SAlexei Starovoitov case BPF_FUNC_probe_read:
14858937bd80SAlexei Starovoitov return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
14866ae08ae3SDaniel Borkmann NULL : &bpf_probe_read_compat_proto;
14876ae08ae3SDaniel Borkmann case BPF_FUNC_probe_read_str:
14886ae08ae3SDaniel Borkmann return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
148971330842SDaniel Borkmann NULL : &bpf_probe_read_compat_str_proto;
1490ff40e510SDaniel Borkmann #endif
14916ae08ae3SDaniel Borkmann #ifdef CONFIG_CGROUPS
14926ae08ae3SDaniel Borkmann case BPF_FUNC_cgrp_storage_get:
14936ae08ae3SDaniel Borkmann return &bpf_cgrp_storage_get_proto;
149471330842SDaniel Borkmann case BPF_FUNC_cgrp_storage_delete:
1495ff40e510SDaniel Borkmann return &bpf_cgrp_storage_delete_proto;
14960ebeea8cSDaniel Borkmann case BPF_FUNC_current_task_under_cgroup:
14970ebeea8cSDaniel Borkmann return &bpf_current_task_under_cgroup_proto;
149871330842SDaniel Borkmann #endif
1499ff40e510SDaniel Borkmann case BPF_FUNC_send_signal:
1500a5e8c070SGianluca Borello return &bpf_send_signal_proto;
150171330842SDaniel Borkmann case BPF_FUNC_send_signal_thread:
1502ff40e510SDaniel Borkmann return &bpf_send_signal_thread_proto;
15030ebeea8cSDaniel Borkmann case BPF_FUNC_perf_event_read_value:
150434ea38caSYonghong Song return &bpf_perf_event_read_value_proto;
1505c4bcfb38SYonghong Song case BPF_FUNC_ringbuf_output:
1506c4bcfb38SYonghong Song return &bpf_ringbuf_output_proto;
1507c4bcfb38SYonghong Song case BPF_FUNC_ringbuf_reserve:
1508c4bcfb38SYonghong Song return &bpf_ringbuf_reserve_proto;
15097f628741SMatteo Croce case BPF_FUNC_ringbuf_submit:
15107f628741SMatteo Croce return &bpf_ringbuf_submit_proto;
151134ea38caSYonghong Song case BPF_FUNC_ringbuf_discard:
15128b401f9eSYonghong Song return &bpf_ringbuf_discard_proto;
15138b401f9eSYonghong Song case BPF_FUNC_ringbuf_query:
15148482941fSYonghong Song return &bpf_ringbuf_query_proto;
15158482941fSYonghong Song case BPF_FUNC_jiffies64:
1516b80b033bSSong Liu return &bpf_jiffies64_proto;
1517b80b033bSSong Liu case BPF_FUNC_get_task_stack:
1518457f4436SAndrii Nakryiko return prog->sleepable ? &bpf_get_task_stack_sleepable_proto
1519457f4436SAndrii Nakryiko : &bpf_get_task_stack_proto;
1520457f4436SAndrii Nakryiko case BPF_FUNC_copy_from_user:
1521457f4436SAndrii Nakryiko return &bpf_copy_from_user_proto;
1522457f4436SAndrii Nakryiko case BPF_FUNC_copy_from_user_task:
1523457f4436SAndrii Nakryiko return &bpf_copy_from_user_task_proto;
1524457f4436SAndrii Nakryiko case BPF_FUNC_snprintf_btf:
1525457f4436SAndrii Nakryiko return &bpf_snprintf_btf_proto;
1526457f4436SAndrii Nakryiko case BPF_FUNC_per_cpu_ptr:
1527457f4436SAndrii Nakryiko return &bpf_per_cpu_ptr_proto;
152872e2b2b6SYonghong Song case BPF_FUNC_this_cpu_ptr:
152972e2b2b6SYonghong Song return &bpf_this_cpu_ptr_proto;
1530fa28dcb8SSong Liu case BPF_FUNC_task_storage_get:
1531d4dd9775SAndrii Nakryiko if (bpf_prog_check_recur(prog))
1532d4dd9775SAndrii Nakryiko return &bpf_task_storage_get_recur_proto;
153307be4c4aSAlexei Starovoitov return &bpf_task_storage_get_proto;
153401685c5bSYonghong Song case BPF_FUNC_task_storage_delete:
1535376040e4SKenny Yu if (bpf_prog_check_recur(prog))
153601685c5bSYonghong Song return &bpf_task_storage_delete_recur_proto;
1537c4d0bfb4SAlan Maguire return &bpf_task_storage_delete_proto;
1538c4d0bfb4SAlan Maguire case BPF_FUNC_for_each_map_elem:
1539b7906b70SAndrii Nakryiko return &bpf_for_each_map_elem_proto;
1540eaa6bcb7SHao Luo case BPF_FUNC_snprintf:
1541b7906b70SAndrii Nakryiko return &bpf_snprintf_proto;
154263d9b80dSHao Luo case BPF_FUNC_get_func_ip:
1543a10787e6SSong Liu return &bpf_get_func_ip_proto_tracing;
15444279adb0SMartin KaFai Lau case BPF_FUNC_get_branch_snapshot:
15450593dd34SMartin KaFai Lau return &bpf_get_branch_snapshot_proto;
1546a10787e6SSong Liu case BPF_FUNC_find_vma:
1547a10787e6SSong Liu return &bpf_find_vma_proto;
15488a7dac37SMartin KaFai Lau case BPF_FUNC_trace_vprintk:
15490593dd34SMartin KaFai Lau return bpf_get_trace_vprintk_proto();
1550a10787e6SSong Liu default:
155169c087baSYonghong Song break;
155269c087baSYonghong Song }
15537b15523aSFlorent Revest
15547b15523aSFlorent Revest func_proto = bpf_base_func_proto(func_id, prog);
15559b99edcaSJiri Olsa if (func_proto)
15569b99edcaSJiri Olsa return func_proto;
1557856c02dbSSong Liu
1558856c02dbSSong Liu if (!bpf_token_capable(prog->aux->token, CAP_SYS_ADMIN))
15597c7e3d31SSong Liu return NULL;
15607c7e3d31SSong Liu
156110aceb62SDave Marchevsky switch (func_id) {
156210aceb62SDave Marchevsky case BPF_FUNC_probe_write_user:
15639fd82b61SAlexei Starovoitov return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
15643bfb49d7SMarco Elver NULL : &bpf_probe_write_user_proto;
15653bfb49d7SMarco Elver default:
15663bfb49d7SMarco Elver return NULL;
15673bfb49d7SMarco Elver }
15683bfb49d7SMarco Elver }
15693bfb49d7SMarco Elver
is_kprobe_multi(const struct bpf_prog * prog)15703bfb49d7SMarco Elver static bool is_kprobe_multi(const struct bpf_prog *prog)
15713bfb49d7SMarco Elver {
15723bfb49d7SMarco Elver return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ||
15733bfb49d7SMarco Elver prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION;
15743bfb49d7SMarco Elver }
15753bfb49d7SMarco Elver
is_kprobe_session(const struct bpf_prog * prog)15763bfb49d7SMarco Elver static inline bool is_kprobe_session(const struct bpf_prog *prog)
15773bfb49d7SMarco Elver {
15783bfb49d7SMarco Elver return prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION;
15793bfb49d7SMarco Elver }
15809fd82b61SAlexei Starovoitov
is_uprobe_multi(const struct bpf_prog * prog)15819fd82b61SAlexei Starovoitov static inline bool is_uprobe_multi(const struct bpf_prog *prog)
15829fd82b61SAlexei Starovoitov {
1583535a3692SJiri Olsa return prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI ||
1584535a3692SJiri Olsa prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION;
1585535a3692SJiri Olsa }
1586535a3692SJiri Olsa
is_uprobe_session(const struct bpf_prog * prog)1587535a3692SJiri Olsa static inline bool is_uprobe_session(const struct bpf_prog *prog)
1588535a3692SJiri Olsa {
1589535a3692SJiri Olsa return prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION;
1590535a3692SJiri Olsa }
1591535a3692SJiri Olsa
1592535a3692SJiri Olsa static const struct bpf_func_proto *
kprobe_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1593535a3692SJiri Olsa kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1594d920179bSJiri Olsa {
1595d920179bSJiri Olsa switch (func_id) {
1596d920179bSJiri Olsa case BPF_FUNC_perf_event_output:
1597d920179bSJiri Olsa return &bpf_perf_event_output_proto;
1598d920179bSJiri Olsa case BPF_FUNC_get_stackid:
1599d920179bSJiri Olsa return &bpf_get_stackid_proto;
1600d920179bSJiri Olsa case BPF_FUNC_get_stack:
1601d920179bSJiri Olsa return prog->sleepable ? &bpf_get_stack_sleepable_proto : &bpf_get_stack_proto;
1602d920179bSJiri Olsa #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1603d920179bSJiri Olsa case BPF_FUNC_override_return:
1604d920179bSJiri Olsa return &bpf_override_return_proto;
16055e43f899SAndrey Ignatov #endif
16065e43f899SAndrey Ignatov case BPF_FUNC_get_func_ip:
16079fd82b61SAlexei Starovoitov if (is_kprobe_multi(prog))
16089fd82b61SAlexei Starovoitov return &bpf_get_func_ip_proto_kprobe_multi;
1609a43eec30SAlexei Starovoitov if (is_uprobe_multi(prog))
1610a43eec30SAlexei Starovoitov return &bpf_get_func_ip_proto_uprobe_multi;
1611d5a3b1f6SAlexei Starovoitov return &bpf_get_func_ip_proto_kprobe;
1612d5a3b1f6SAlexei Starovoitov case BPF_FUNC_get_attach_cookie:
1613c195651eSYonghong Song if (is_kprobe_multi(prog))
1614d4dd9775SAndrii Nakryiko return &bpf_get_attach_cookie_proto_kmulti;
16159802d865SJosef Bacik if (is_uprobe_multi(prog))
16169802d865SJosef Bacik return &bpf_get_attach_cookie_proto_umulti;
16179802d865SJosef Bacik return &bpf_get_attach_cookie_proto_trace;
16189802d865SJosef Bacik default:
16199ffd9f3fSJiri Olsa return bpf_tracing_func_proto(func_id, prog);
1620535a3692SJiri Olsa }
1621686328d8SJiri Olsa }
1622d920179bSJiri Olsa
1623686328d8SJiri Olsa /* 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)1624686328d8SJiri Olsa static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
16257adfc6c9SAndrii Nakryiko const struct bpf_prog *prog,
1626535a3692SJiri Olsa struct bpf_insn_access_aux *info)
16270b779b61SJiri Olsa {
1628d920179bSJiri Olsa if (off < 0 || off >= sizeof(struct pt_regs))
16290b779b61SJiri Olsa return false;
16300b779b61SJiri Olsa if (type != BPF_READ)
16312541517cSAlexei Starovoitov return false;
1632fc611f47SKP Singh if (off % size != 0)
16332541517cSAlexei Starovoitov return false;
16342541517cSAlexei Starovoitov /*
16352541517cSAlexei Starovoitov * Assertion for 32 bit to make sure last 8 byte access
16362541517cSAlexei Starovoitov * (BPF_DW) to the last 4 byte member is disallowed.
163719de99f7SAlexei Starovoitov */
16385e43f899SAndrey Ignatov if (off + size > sizeof(struct pt_regs))
163923994631SYonghong Song return false;
16402541517cSAlexei Starovoitov
16412541517cSAlexei Starovoitov return true;
16422541517cSAlexei Starovoitov }
16432541517cSAlexei Starovoitov
16442541517cSAlexei Starovoitov const struct bpf_verifier_ops kprobe_verifier_ops = {
16452541517cSAlexei Starovoitov .get_func_proto = kprobe_prog_func_proto,
16462541517cSAlexei Starovoitov .is_valid_access = kprobe_prog_is_valid_access,
16472d071c64SDaniel Borkmann };
16482d071c64SDaniel Borkmann
16492d071c64SDaniel Borkmann const struct bpf_prog_ops kprobe_prog_ops = {
16502d071c64SDaniel Borkmann };
16512d071c64SDaniel Borkmann
BPF_CALL_5(bpf_perf_event_output_tp,void *,tp_buff,struct bpf_map *,map,u64,flags,void *,data,u64,size)16522d071c64SDaniel Borkmann BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
16532d071c64SDaniel Borkmann u64, flags, void *, data, u64, size)
16542541517cSAlexei Starovoitov {
16552541517cSAlexei Starovoitov struct pt_regs *regs = *(struct pt_regs **)tp_buff;
16562541517cSAlexei Starovoitov
16577de16e3aSJakub Kicinski /*
16582541517cSAlexei Starovoitov * r1 points to perf tracepoint buffer where first 8 bytes are hidden
16592541517cSAlexei Starovoitov * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
16602541517cSAlexei Starovoitov * from there and call the same bpf_perf_event_output() helper inline.
16612541517cSAlexei Starovoitov */
16627de16e3aSJakub Kicinski return ____bpf_perf_event_output(regs, map, flags, data, size);
16637de16e3aSJakub Kicinski }
16647de16e3aSJakub Kicinski
1665f3694e00SDaniel Borkmann static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1666f3694e00SDaniel Borkmann .func = bpf_perf_event_output_tp,
16679940d67cSAlexei Starovoitov .gpl_only = true,
1668f3694e00SDaniel Borkmann .ret_type = RET_INTEGER,
1669f3694e00SDaniel Borkmann .arg1_type = ARG_PTR_TO_CTX,
16709940d67cSAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
16719940d67cSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
16729940d67cSAlexei Starovoitov .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1673f3694e00SDaniel Borkmann .arg5_type = ARG_CONST_SIZE_OR_ZERO,
16749940d67cSAlexei Starovoitov };
1675f3694e00SDaniel Borkmann
BPF_CALL_3(bpf_get_stackid_tp,void *,tp_buff,struct bpf_map *,map,u64,flags)16769940d67cSAlexei Starovoitov BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
16779940d67cSAlexei Starovoitov u64, flags)
16789940d67cSAlexei Starovoitov {
16799940d67cSAlexei Starovoitov struct pt_regs *regs = *(struct pt_regs **)tp_buff;
16809940d67cSAlexei Starovoitov
16819940d67cSAlexei Starovoitov /*
16829940d67cSAlexei Starovoitov * Same comment as in bpf_perf_event_output_tp(), only that this time
16839940d67cSAlexei Starovoitov * the other helper's function body cannot be inlined due to being
16849940d67cSAlexei Starovoitov * external, thus we need to call raw helper function.
1685216e3cd2SHao Luo */
1686a60dd35dSGianluca Borello return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
16879940d67cSAlexei Starovoitov flags, 0, 0);
16889940d67cSAlexei Starovoitov }
1689f3694e00SDaniel Borkmann
1690f3694e00SDaniel Borkmann static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
16919940d67cSAlexei Starovoitov .func = bpf_get_stackid_tp,
1692f3694e00SDaniel Borkmann .gpl_only = true,
16939940d67cSAlexei Starovoitov .ret_type = RET_INTEGER,
1694f3694e00SDaniel Borkmann .arg1_type = ARG_PTR_TO_CTX,
1695f3694e00SDaniel Borkmann .arg2_type = ARG_CONST_MAP_PTR,
1696f3694e00SDaniel Borkmann .arg3_type = ARG_ANYTHING,
1697f3694e00SDaniel Borkmann };
1698f3694e00SDaniel Borkmann
BPF_CALL_4(bpf_get_stack_tp,void *,tp_buff,void *,buf,u32,size,u64,flags)1699f3694e00SDaniel Borkmann BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1700f3694e00SDaniel Borkmann u64, flags)
17019940d67cSAlexei Starovoitov {
17029940d67cSAlexei Starovoitov struct pt_regs *regs = *(struct pt_regs **)tp_buff;
17039940d67cSAlexei Starovoitov
17049940d67cSAlexei Starovoitov return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
17059940d67cSAlexei Starovoitov (unsigned long) size, flags, 0);
17069940d67cSAlexei Starovoitov }
17079940d67cSAlexei Starovoitov
17089940d67cSAlexei Starovoitov static const struct bpf_func_proto bpf_get_stack_proto_tp = {
17099940d67cSAlexei Starovoitov .func = bpf_get_stack_tp,
17109940d67cSAlexei Starovoitov .gpl_only = true,
17119940d67cSAlexei Starovoitov .ret_type = RET_INTEGER,
1712c195651eSYonghong Song .arg1_type = ARG_PTR_TO_CTX,
1713c195651eSYonghong Song .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1714c195651eSYonghong Song .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1715c195651eSYonghong Song .arg4_type = ARG_ANYTHING,
1716c195651eSYonghong Song };
1717c195651eSYonghong Song
1718c195651eSYonghong Song static const struct bpf_func_proto *
tp_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1719c195651eSYonghong Song tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1720c195651eSYonghong Song {
1721c195651eSYonghong Song switch (func_id) {
1722c195651eSYonghong Song case BPF_FUNC_perf_event_output:
1723c195651eSYonghong Song return &bpf_perf_event_output_proto_tp;
1724c195651eSYonghong Song case BPF_FUNC_get_stackid:
1725c195651eSYonghong Song return &bpf_get_stackid_proto_tp;
1726c195651eSYonghong Song case BPF_FUNC_get_stack:
1727c195651eSYonghong Song return &bpf_get_stack_proto_tp;
1728c195651eSYonghong Song case BPF_FUNC_get_attach_cookie:
1729c195651eSYonghong Song return &bpf_get_attach_cookie_proto_trace;
1730c195651eSYonghong Song default:
17315e43f899SAndrey Ignatov return bpf_tracing_func_proto(func_id, prog);
17325e43f899SAndrey Ignatov }
17339fd82b61SAlexei Starovoitov }
17349fd82b61SAlexei 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)17359fd82b61SAlexei Starovoitov static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
17369940d67cSAlexei Starovoitov const struct bpf_prog *prog,
17379fd82b61SAlexei Starovoitov struct bpf_insn_access_aux *info)
17389940d67cSAlexei Starovoitov {
1739c195651eSYonghong Song if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1740c195651eSYonghong Song return false;
17417adfc6c9SAndrii Nakryiko if (type != BPF_READ)
17427adfc6c9SAndrii Nakryiko return false;
17439fd82b61SAlexei Starovoitov if (off % size != 0)
1744fc611f47SKP Singh return false;
17459fd82b61SAlexei Starovoitov
17469fd82b61SAlexei Starovoitov BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
17479fd82b61SAlexei Starovoitov return true;
174819de99f7SAlexei Starovoitov }
17495e43f899SAndrey Ignatov
175023994631SYonghong Song const struct bpf_verifier_ops tracepoint_verifier_ops = {
17519fd82b61SAlexei Starovoitov .get_func_proto = tp_prog_func_proto,
17529fd82b61SAlexei Starovoitov .is_valid_access = tp_prog_is_valid_access,
17539fd82b61SAlexei Starovoitov };
17549fd82b61SAlexei Starovoitov
17559fd82b61SAlexei Starovoitov const struct bpf_prog_ops tracepoint_prog_ops = {
17569fd82b61SAlexei Starovoitov };
17579fd82b61SAlexei Starovoitov
BPF_CALL_3(bpf_perf_prog_read_value,struct bpf_perf_event_data_kern *,ctx,struct bpf_perf_event_value *,buf,u32,size)17582d071c64SDaniel Borkmann BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
17592d071c64SDaniel Borkmann struct bpf_perf_event_value *, buf, u32, size)
17609fd82b61SAlexei Starovoitov {
17619fd82b61SAlexei Starovoitov int err = -EINVAL;
17629fd82b61SAlexei Starovoitov
17637de16e3aSJakub Kicinski if (unlikely(size != sizeof(struct bpf_perf_event_value)))
17649fd82b61SAlexei Starovoitov goto clear;
17659fd82b61SAlexei Starovoitov err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
17669fd82b61SAlexei Starovoitov &buf->running);
17679fd82b61SAlexei Starovoitov if (unlikely(err))
17687de16e3aSJakub Kicinski goto clear;
17697de16e3aSJakub Kicinski return 0;
17707de16e3aSJakub Kicinski clear:
1771f005afedSYonghong Song memset(buf, 0, size);
1772f005afedSYonghong Song return err;
1773f005afedSYonghong Song }
1774f005afedSYonghong Song
1775f005afedSYonghong Song static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1776f005afedSYonghong Song .func = bpf_perf_prog_read_value,
1777f005afedSYonghong Song .gpl_only = true,
1778f005afedSYonghong Song .ret_type = RET_INTEGER,
1779f005afedSYonghong Song .arg1_type = ARG_PTR_TO_CTX,
1780f005afedSYonghong Song .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1781f005afedSYonghong Song .arg3_type = ARG_CONST_SIZE,
1782f005afedSYonghong Song };
1783f005afedSYonghong Song
BPF_CALL_4(bpf_read_branch_records,struct bpf_perf_event_data_kern *,ctx,void *,buf,u32,size,u64,flags)1784f005afedSYonghong Song BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1785f005afedSYonghong Song void *, buf, u32, size, u64, flags)
1786f005afedSYonghong Song {
1787f005afedSYonghong Song static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1788f005afedSYonghong Song struct perf_branch_stack *br_stack = ctx->data->br_stack;
1789f005afedSYonghong Song u32 to_copy;
1790f005afedSYonghong Song
1791f005afedSYonghong Song if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1792f005afedSYonghong Song return -EINVAL;
1793f005afedSYonghong Song
1794f005afedSYonghong Song if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
1795f005afedSYonghong Song return -ENOENT;
1796f005afedSYonghong Song
1797fff7b643SDaniel Xu if (unlikely(!br_stack))
1798fff7b643SDaniel Xu return -ENOENT;
1799fff7b643SDaniel Xu
1800fff7b643SDaniel Xu if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1801fff7b643SDaniel Xu return br_stack->nr * br_entry_size;
1802fff7b643SDaniel Xu
1803fff7b643SDaniel Xu if (!buf || (size % br_entry_size != 0))
1804fff7b643SDaniel Xu return -EINVAL;
1805fff7b643SDaniel Xu
1806fff7b643SDaniel Xu to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1807cce6a2d7SJiri Olsa memcpy(buf, br_stack->entries, to_copy);
1808cce6a2d7SJiri Olsa
1809cce6a2d7SJiri Olsa return to_copy;
1810fff7b643SDaniel Xu }
1811db52f572SKajol Jain
1812fff7b643SDaniel Xu static const struct bpf_func_proto bpf_read_branch_records_proto = {
1813fff7b643SDaniel Xu .func = bpf_read_branch_records,
1814fff7b643SDaniel Xu .gpl_only = true,
1815fff7b643SDaniel Xu .ret_type = RET_INTEGER,
1816fff7b643SDaniel Xu .arg1_type = ARG_PTR_TO_CTX,
1817fff7b643SDaniel Xu .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1818fff7b643SDaniel Xu .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1819fff7b643SDaniel Xu .arg4_type = ARG_ANYTHING,
1820fff7b643SDaniel Xu };
1821fff7b643SDaniel Xu
1822fff7b643SDaniel Xu static const struct bpf_func_proto *
pe_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1823fff7b643SDaniel Xu pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1824fff7b643SDaniel Xu {
1825fff7b643SDaniel Xu switch (func_id) {
1826fff7b643SDaniel Xu case BPF_FUNC_perf_event_output:
1827fff7b643SDaniel Xu return &bpf_perf_event_output_proto_tp;
1828fff7b643SDaniel Xu case BPF_FUNC_get_stackid:
1829fff7b643SDaniel Xu return &bpf_get_stackid_proto_pe;
1830fff7b643SDaniel Xu case BPF_FUNC_get_stack:
1831fff7b643SDaniel Xu return &bpf_get_stack_proto_pe;
1832fff7b643SDaniel Xu case BPF_FUNC_perf_prog_read_value:
1833fff7b643SDaniel Xu return &bpf_perf_prog_read_value_proto;
1834fff7b643SDaniel Xu case BPF_FUNC_read_branch_records:
18355e43f899SAndrey Ignatov return &bpf_read_branch_records_proto;
18365e43f899SAndrey Ignatov case BPF_FUNC_get_attach_cookie:
1837f005afedSYonghong Song return &bpf_get_attach_cookie_proto_pe;
1838f005afedSYonghong Song default:
1839f005afedSYonghong Song return bpf_tracing_func_proto(func_id, prog);
1840f005afedSYonghong Song }
1841f005afedSYonghong Song }
18427b04d6d6SSong Liu
1843c195651eSYonghong Song /*
18447b04d6d6SSong Liu * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1845f005afedSYonghong Song * to avoid potential recursive reuse issue when/if tracepoints are added
1846f005afedSYonghong Song * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1847fff7b643SDaniel Xu *
1848fff7b643SDaniel Xu * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
18497adfc6c9SAndrii Nakryiko * in normal, irq, and nmi context.
18507adfc6c9SAndrii Nakryiko */
1851f005afedSYonghong Song struct bpf_raw_tp_regs {
1852fc611f47SKP Singh struct pt_regs regs[3];
1853f005afedSYonghong Song };
1854f005afedSYonghong Song static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1855f005afedSYonghong Song static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
get_bpf_raw_tp_regs(void)1856c4f6699dSAlexei Starovoitov static struct pt_regs *get_bpf_raw_tp_regs(void)
1857c4f6699dSAlexei Starovoitov {
1858c4f6699dSAlexei Starovoitov struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
18599594dc3cSMatt Mullins int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
18609594dc3cSMatt Mullins
18619594dc3cSMatt Mullins if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
18629594dc3cSMatt Mullins this_cpu_dec(bpf_raw_tp_nest_level);
1863c4f6699dSAlexei Starovoitov return ERR_PTR(-EBUSY);
18649594dc3cSMatt Mullins }
18659594dc3cSMatt Mullins
18669594dc3cSMatt Mullins return &tp_regs->regs[nest_level - 1];
18679594dc3cSMatt Mullins }
18689594dc3cSMatt Mullins
put_bpf_raw_tp_regs(void)18699594dc3cSMatt Mullins static void put_bpf_raw_tp_regs(void)
18709594dc3cSMatt Mullins {
18719594dc3cSMatt Mullins this_cpu_dec(bpf_raw_tp_nest_level);
18729594dc3cSMatt Mullins }
18739594dc3cSMatt 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)18749594dc3cSMatt Mullins BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
18759594dc3cSMatt Mullins struct bpf_map *, map, u64, flags, void *, data, u64, size)
18769594dc3cSMatt Mullins {
18779594dc3cSMatt Mullins struct pt_regs *regs = get_bpf_raw_tp_regs();
18789594dc3cSMatt Mullins int ret;
18799594dc3cSMatt Mullins
18809594dc3cSMatt Mullins if (IS_ERR(regs))
18819594dc3cSMatt Mullins return PTR_ERR(regs);
18829594dc3cSMatt Mullins
18839594dc3cSMatt Mullins perf_fetch_caller_regs(regs);
18849594dc3cSMatt Mullins ret = ____bpf_perf_event_output(regs, map, flags, data, size);
18859594dc3cSMatt Mullins
18869594dc3cSMatt Mullins put_bpf_raw_tp_regs();
1887c4f6699dSAlexei Starovoitov return ret;
1888c4f6699dSAlexei Starovoitov }
1889c4f6699dSAlexei Starovoitov
18909594dc3cSMatt Mullins static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
18919594dc3cSMatt Mullins .func = bpf_perf_event_output_raw_tp,
18929594dc3cSMatt Mullins .gpl_only = true,
18939594dc3cSMatt Mullins .ret_type = RET_INTEGER,
18949594dc3cSMatt Mullins .arg1_type = ARG_PTR_TO_CTX,
1895c4f6699dSAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
1896c4f6699dSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
18979594dc3cSMatt Mullins .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
18989594dc3cSMatt Mullins .arg5_type = ARG_CONST_SIZE_OR_ZERO,
18999594dc3cSMatt Mullins };
19009594dc3cSMatt Mullins
1901c4f6699dSAlexei Starovoitov extern const struct bpf_func_proto bpf_skb_output_proto;
1902c4f6699dSAlexei Starovoitov extern const struct bpf_func_proto bpf_xdp_output_proto;
1903c4f6699dSAlexei Starovoitov extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1904c4f6699dSAlexei Starovoitov
BPF_CALL_3(bpf_get_stackid_raw_tp,struct bpf_raw_tracepoint_args *,args,struct bpf_map *,map,u64,flags)1905c4f6699dSAlexei Starovoitov BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1906c4f6699dSAlexei Starovoitov struct bpf_map *, map, u64, flags)
1907c4f6699dSAlexei Starovoitov {
1908c4f6699dSAlexei Starovoitov struct pt_regs *regs = get_bpf_raw_tp_regs();
1909c4f6699dSAlexei Starovoitov int ret;
1910216e3cd2SHao Luo
1911c4f6699dSAlexei Starovoitov if (IS_ERR(regs))
1912c4f6699dSAlexei Starovoitov return PTR_ERR(regs);
1913c4f6699dSAlexei Starovoitov
1914a7658e1aSAlexei Starovoitov perf_fetch_caller_regs(regs);
1915d831ee84SEelco Chaudron /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1916d9917302SEelco Chaudron ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1917a7658e1aSAlexei Starovoitov flags, 0, 0);
1918c4f6699dSAlexei Starovoitov put_bpf_raw_tp_regs();
1919c4f6699dSAlexei Starovoitov return ret;
1920c4f6699dSAlexei Starovoitov }
19219594dc3cSMatt Mullins
19229594dc3cSMatt Mullins static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
19239594dc3cSMatt Mullins .func = bpf_get_stackid_raw_tp,
19249594dc3cSMatt Mullins .gpl_only = true,
19259594dc3cSMatt Mullins .ret_type = RET_INTEGER,
1926c4f6699dSAlexei Starovoitov .arg1_type = ARG_PTR_TO_CTX,
1927c4f6699dSAlexei Starovoitov .arg2_type = ARG_CONST_MAP_PTR,
1928c4f6699dSAlexei Starovoitov .arg3_type = ARG_ANYTHING,
19299594dc3cSMatt Mullins };
1930c4f6699dSAlexei Starovoitov
BPF_CALL_4(bpf_get_stack_raw_tp,struct bpf_raw_tracepoint_args *,args,void *,buf,u32,size,u64,flags)19319594dc3cSMatt Mullins BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
19329594dc3cSMatt Mullins void *, buf, u32, size, u64, flags)
1933c4f6699dSAlexei Starovoitov {
1934c4f6699dSAlexei Starovoitov struct pt_regs *regs = get_bpf_raw_tp_regs();
1935c4f6699dSAlexei Starovoitov int ret;
1936c4f6699dSAlexei Starovoitov
1937c4f6699dSAlexei Starovoitov if (IS_ERR(regs))
1938c4f6699dSAlexei Starovoitov return PTR_ERR(regs);
1939c4f6699dSAlexei Starovoitov
1940c4f6699dSAlexei Starovoitov perf_fetch_caller_regs(regs);
1941c4f6699dSAlexei Starovoitov ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1942c4f6699dSAlexei Starovoitov (unsigned long) size, flags, 0);
1943c4f6699dSAlexei Starovoitov put_bpf_raw_tp_regs();
1944c195651eSYonghong Song return ret;
1945c195651eSYonghong Song }
1946c195651eSYonghong Song
19479594dc3cSMatt Mullins static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
19489594dc3cSMatt Mullins .func = bpf_get_stack_raw_tp,
19499594dc3cSMatt Mullins .gpl_only = true,
19509594dc3cSMatt Mullins .ret_type = RET_INTEGER,
19519594dc3cSMatt Mullins .arg1_type = ARG_PTR_TO_CTX,
1952c195651eSYonghong Song .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1953c195651eSYonghong Song .arg3_type = ARG_CONST_SIZE_OR_ZERO,
19549594dc3cSMatt Mullins .arg4_type = ARG_ANYTHING,
1955c195651eSYonghong Song };
19569594dc3cSMatt Mullins
19579594dc3cSMatt Mullins static const struct bpf_func_proto *
raw_tp_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1958c195651eSYonghong Song raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1959c195651eSYonghong Song {
1960c195651eSYonghong Song switch (func_id) {
1961c195651eSYonghong Song case BPF_FUNC_perf_event_output:
1962c195651eSYonghong Song return &bpf_perf_event_output_proto_raw_tp;
1963c195651eSYonghong Song case BPF_FUNC_get_stackid:
1964c195651eSYonghong Song return &bpf_get_stackid_proto_raw_tp;
1965216e3cd2SHao Luo case BPF_FUNC_get_stack:
1966c195651eSYonghong Song return &bpf_get_stack_proto_raw_tp;
1967c195651eSYonghong Song case BPF_FUNC_get_attach_cookie:
1968c195651eSYonghong Song return &bpf_get_attach_cookie_proto_tracing;
1969c195651eSYonghong Song default:
19705e43f899SAndrey Ignatov return bpf_tracing_func_proto(func_id, prog);
19715e43f899SAndrey Ignatov }
1972c4f6699dSAlexei Starovoitov }
1973c4f6699dSAlexei Starovoitov
1974c4f6699dSAlexei Starovoitov const struct bpf_func_proto *
tracing_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1975c4f6699dSAlexei Starovoitov tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1976c4f6699dSAlexei Starovoitov {
1977c4f6699dSAlexei Starovoitov const struct bpf_func_proto *fn;
1978c195651eSYonghong Song
1979c195651eSYonghong Song switch (func_id) {
198068ca5d4eSAndrii Nakryiko #ifdef CONFIG_NET
198168ca5d4eSAndrii Nakryiko case BPF_FUNC_skb_output:
1982c4f6699dSAlexei Starovoitov return &bpf_skb_output_proto;
1983fc611f47SKP Singh case BPF_FUNC_xdp_output:
1984c4f6699dSAlexei Starovoitov return &bpf_xdp_output_proto;
1985c4f6699dSAlexei Starovoitov case BPF_FUNC_skc_to_tcp6_sock:
1986c4f6699dSAlexei Starovoitov return &bpf_skc_to_tcp6_sock_proto;
1987958a3f2dSJiri Olsa case BPF_FUNC_skc_to_tcp_sock:
1988f1b9509cSAlexei Starovoitov return &bpf_skc_to_tcp_sock_proto;
1989f1b9509cSAlexei Starovoitov case BPF_FUNC_skc_to_tcp_timewait_sock:
19903cee6fb8SMartin KaFai Lau return &bpf_skc_to_tcp_timewait_sock_proto;
19913cee6fb8SMartin KaFai Lau case BPF_FUNC_skc_to_tcp_request_sock:
1992f1b9509cSAlexei Starovoitov return &bpf_skc_to_tcp_request_sock_proto;
1993f1b9509cSAlexei Starovoitov case BPF_FUNC_skc_to_udp6_sock:
1994f1b9509cSAlexei Starovoitov return &bpf_skc_to_udp6_sock_proto;
1995f1b9509cSAlexei Starovoitov case BPF_FUNC_skc_to_unix_sock:
1996d831ee84SEelco Chaudron return &bpf_skc_to_unix_sock_proto;
1997d831ee84SEelco Chaudron case BPF_FUNC_skc_to_mptcp_sock:
1998af7ec138SYonghong Song return &bpf_skc_to_mptcp_sock_proto;
1999af7ec138SYonghong Song case BPF_FUNC_sk_storage_get:
2000478cfbdfSYonghong Song return &bpf_sk_storage_get_tracing_proto;
2001478cfbdfSYonghong Song case BPF_FUNC_sk_storage_delete:
2002478cfbdfSYonghong Song return &bpf_sk_storage_delete_tracing_proto;
2003478cfbdfSYonghong Song case BPF_FUNC_sock_from_file:
2004478cfbdfSYonghong Song return &bpf_sock_from_file_proto;
2005478cfbdfSYonghong Song case BPF_FUNC_get_socket_cookie:
20060d4fad3eSYonghong Song return &bpf_get_socket_ptr_cookie_proto;
20070d4fad3eSYonghong Song case BPF_FUNC_xdp_get_buff_len:
20089eeb3aa3SHengqi Chen return &bpf_xdp_get_buff_len_trace_proto;
20099eeb3aa3SHengqi Chen #endif
20103bc253c2SGeliang Tang case BPF_FUNC_seq_printf:
20113bc253c2SGeliang Tang return prog->expected_attach_type == BPF_TRACE_ITER ?
20128e4597c6SMartin KaFai Lau &bpf_seq_printf_proto :
20138e4597c6SMartin KaFai Lau NULL;
20148e4597c6SMartin KaFai Lau case BPF_FUNC_seq_write:
20158e4597c6SMartin KaFai Lau return prog->expected_attach_type == BPF_TRACE_ITER ?
2016b60da495SFlorent Revest &bpf_seq_write_proto :
2017b60da495SFlorent Revest NULL;
2018c5dbb89fSFlorent Revest case BPF_FUNC_seq_printf_btf:
2019c5dbb89fSFlorent Revest return prog->expected_attach_type == BPF_TRACE_ITER ?
2020d9917302SEelco Chaudron &bpf_seq_printf_btf_proto :
2021d9917302SEelco Chaudron NULL;
2022f1b9509cSAlexei Starovoitov case BPF_FUNC_d_path:
2023492e639fSYonghong Song return &bpf_d_path_proto;
2024492e639fSYonghong Song case BPF_FUNC_get_func_arg:
2025492e639fSYonghong Song return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
2026492e639fSYonghong Song case BPF_FUNC_get_func_ret:
2027492e639fSYonghong Song return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
2028492e639fSYonghong Song case BPF_FUNC_get_func_arg_cnt:
2029492e639fSYonghong Song return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
2030492e639fSYonghong Song case BPF_FUNC_get_attach_cookie:
2031eb411377SAlan Maguire if (prog->type == BPF_PROG_TYPE_TRACING &&
2032eb411377SAlan Maguire prog->expected_attach_type == BPF_TRACE_RAW_TP)
2033eb411377SAlan Maguire return &bpf_get_attach_cookie_proto_tracing;
2034eb411377SAlan Maguire return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
20356e22ab9dSJiri Olsa default:
20366e22ab9dSJiri Olsa fn = raw_tp_prog_func_proto(func_id, prog);
2037f92c1e18SJiri Olsa if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
2038f92c1e18SJiri Olsa fn = bpf_iter_get_func_proto(func_id, prog);
2039f92c1e18SJiri Olsa return fn;
2040f92c1e18SJiri Olsa }
2041f92c1e18SJiri Olsa }
2042f92c1e18SJiri Olsa
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)20432fcc8241SKui-Feng Lee static bool raw_tp_prog_is_valid_access(int off, int size,
204468ca5d4eSAndrii Nakryiko enum bpf_access_type type,
204568ca5d4eSAndrii Nakryiko const struct bpf_prog *prog,
204668ca5d4eSAndrii Nakryiko struct bpf_insn_access_aux *info)
20472fcc8241SKui-Feng Lee {
2048f1b9509cSAlexei Starovoitov return bpf_tracing_ctx_access(off, size, type);
20493cee6fb8SMartin KaFai Lau }
20503cee6fb8SMartin KaFai Lau
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)20513cee6fb8SMartin KaFai Lau static bool tracing_prog_is_valid_access(int off, int size,
20523cee6fb8SMartin KaFai Lau enum bpf_access_type type,
2053f1b9509cSAlexei Starovoitov const struct bpf_prog *prog,
2054f1b9509cSAlexei Starovoitov struct bpf_insn_access_aux *info)
2055f1b9509cSAlexei Starovoitov {
2056c4f6699dSAlexei Starovoitov return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
2057c4f6699dSAlexei Starovoitov }
20585e43f899SAndrey Ignatov
bpf_prog_test_run_tracing(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2059c4f6699dSAlexei Starovoitov int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
2060c4f6699dSAlexei Starovoitov const union bpf_attr *kattr,
206135346ab6SHou Tao union bpf_attr __user *uattr)
2062f1b9509cSAlexei Starovoitov {
2063f1b9509cSAlexei Starovoitov return -ENOTSUPP;
2064f1b9509cSAlexei Starovoitov }
2065f1b9509cSAlexei Starovoitov
2066f1b9509cSAlexei Starovoitov const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
2067f1b9509cSAlexei Starovoitov .get_func_proto = raw_tp_prog_func_proto,
2068f1b9509cSAlexei Starovoitov .is_valid_access = raw_tp_prog_is_valid_access,
206935346ab6SHou Tao };
2070c4f6699dSAlexei Starovoitov
2071c4f6699dSAlexei Starovoitov const struct bpf_prog_ops raw_tracepoint_prog_ops = {
20723e7c67d9SKP Singh #ifdef CONFIG_NET
20733e7c67d9SKP Singh .test_run = bpf_prog_test_run_raw_tp,
20743e7c67d9SKP Singh #endif
20753e7c67d9SKP Singh };
20763e7c67d9SKP Singh
20773e7c67d9SKP Singh const struct bpf_verifier_ops tracing_verifier_ops = {
20783e7c67d9SKP Singh .get_func_proto = tracing_prog_func_proto,
2079c4f6699dSAlexei Starovoitov .is_valid_access = tracing_prog_is_valid_access,
2080c4f6699dSAlexei Starovoitov };
2081c4f6699dSAlexei Starovoitov
2082c4f6699dSAlexei Starovoitov const struct bpf_prog_ops tracing_prog_ops = {
2083c4f6699dSAlexei Starovoitov .test_run = bpf_prog_test_run_tracing,
2084c4f6699dSAlexei Starovoitov };
2085ebfb4d40SYonghong Song
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)20861b4d60ecSSong Liu static bool raw_tp_writable_prog_is_valid_access(int off, int size,
2087ebfb4d40SYonghong Song enum bpf_access_type type,
2088c4f6699dSAlexei Starovoitov const struct bpf_prog *prog,
2089c4f6699dSAlexei Starovoitov struct bpf_insn_access_aux *info)
2090f1b9509cSAlexei Starovoitov {
2091f1b9509cSAlexei Starovoitov if (off == 0) {
2092f1b9509cSAlexei Starovoitov if (size != sizeof(u64) || type != BPF_READ)
2093f1b9509cSAlexei Starovoitov return false;
2094f1b9509cSAlexei Starovoitov info->reg_type = PTR_TO_TP_BUFFER;
2095f1b9509cSAlexei Starovoitov }
2096da00d2f1SKP Singh return raw_tp_prog_is_valid_access(off, size, type, prog, info);
2097f1b9509cSAlexei Starovoitov }
2098f1b9509cSAlexei Starovoitov
20999df1c28bSMatt Mullins const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
21009df1c28bSMatt Mullins .get_func_proto = raw_tp_prog_func_proto,
21019df1c28bSMatt Mullins .is_valid_access = raw_tp_writable_prog_is_valid_access,
21029df1c28bSMatt Mullins };
21039df1c28bSMatt Mullins
21049df1c28bSMatt Mullins const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
21059df1c28bSMatt Mullins };
21069df1c28bSMatt 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)21079df1c28bSMatt Mullins static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
21089df1c28bSMatt Mullins const struct bpf_prog *prog,
21099df1c28bSMatt Mullins struct bpf_insn_access_aux *info)
21109df1c28bSMatt Mullins {
21119df1c28bSMatt Mullins const int size_u64 = sizeof(u64);
21129df1c28bSMatt Mullins
21139df1c28bSMatt Mullins if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
21149df1c28bSMatt Mullins return false;
21159df1c28bSMatt Mullins if (type != BPF_READ)
21169df1c28bSMatt Mullins return false;
21179df1c28bSMatt Mullins if (off % size != 0) {
21189df1c28bSMatt Mullins if (sizeof(unsigned long) != 4)
21199df1c28bSMatt Mullins return false;
21200515e599SAlexei Starovoitov if (size != 8)
21215e43f899SAndrey Ignatov return false;
212223994631SYonghong Song if (off % size != 4)
21230515e599SAlexei Starovoitov return false;
212495da0cdbSTeng Qin }
212531fd8581SYonghong Song
21260515e599SAlexei Starovoitov switch (off) {
21270515e599SAlexei Starovoitov case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
21280515e599SAlexei Starovoitov bpf_ctx_record_field_size(info, size_u64);
21290515e599SAlexei Starovoitov if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2130bc23105cSDaniel Borkmann return false;
2131bc23105cSDaniel Borkmann break;
21320515e599SAlexei Starovoitov case bpf_ctx_range(struct bpf_perf_event_data, addr):
2133bc23105cSDaniel Borkmann bpf_ctx_record_field_size(info, size_u64);
2134bc23105cSDaniel Borkmann if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2135bc23105cSDaniel Borkmann return false;
2136bc23105cSDaniel Borkmann break;
2137bc23105cSDaniel Borkmann default:
213831fd8581SYonghong Song if (size != sizeof(long))
2139f96da094SDaniel Borkmann return false;
2140f96da094SDaniel Borkmann }
214195da0cdbSTeng Qin
214295da0cdbSTeng Qin return true;
214395da0cdbSTeng Qin }
214495da0cdbSTeng Qin
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)214595da0cdbSTeng Qin static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
214695da0cdbSTeng Qin const struct bpf_insn *si,
214795da0cdbSTeng Qin struct bpf_insn *insn_buf,
214823994631SYonghong Song struct bpf_prog *prog, u32 *target_size)
2149f96da094SDaniel Borkmann {
2150f96da094SDaniel Borkmann struct bpf_insn *insn = insn_buf;
21510515e599SAlexei Starovoitov
21520515e599SAlexei Starovoitov switch (si->off) {
21530515e599SAlexei Starovoitov case offsetof(struct bpf_perf_event_data, sample_period):
2154f96da094SDaniel Borkmann *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
21550515e599SAlexei Starovoitov data), si->dst_reg, si->src_reg,
21560515e599SAlexei Starovoitov offsetof(struct bpf_perf_event_data_kern, data));
21570515e599SAlexei Starovoitov *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
21586b8cc1d1SDaniel Borkmann bpf_target_off(struct perf_sample_data, period, 8,
21596b8cc1d1SDaniel Borkmann target_size));
21600515e599SAlexei Starovoitov break;
2161f96da094SDaniel Borkmann case offsetof(struct bpf_perf_event_data, addr):
21620515e599SAlexei Starovoitov *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
21630515e599SAlexei Starovoitov data), si->dst_reg, si->src_reg,
21640515e599SAlexei Starovoitov offsetof(struct bpf_perf_event_data_kern, data));
21656b8cc1d1SDaniel Borkmann *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
21660515e599SAlexei Starovoitov bpf_target_off(struct perf_sample_data, addr, 8,
2167f035a515SDaniel Borkmann target_size));
21686b8cc1d1SDaniel Borkmann break;
21690515e599SAlexei Starovoitov default:
21706b8cc1d1SDaniel Borkmann *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2171f96da094SDaniel Borkmann regs), si->dst_reg, si->src_reg,
2172f96da094SDaniel Borkmann offsetof(struct bpf_perf_event_data_kern, regs));
21730515e599SAlexei Starovoitov *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
217495da0cdbSTeng Qin si->off);
217595da0cdbSTeng Qin break;
217695da0cdbSTeng Qin }
217795da0cdbSTeng Qin
217895da0cdbSTeng Qin return insn - insn_buf;
217995da0cdbSTeng Qin }
218095da0cdbSTeng Qin
218195da0cdbSTeng Qin const struct bpf_verifier_ops perf_event_verifier_ops = {
21820515e599SAlexei Starovoitov .get_func_proto = pe_prog_func_proto,
2183f035a515SDaniel Borkmann .is_valid_access = pe_prog_is_valid_access,
21846b8cc1d1SDaniel Borkmann .convert_ctx_access = pe_prog_convert_ctx_access,
21850515e599SAlexei Starovoitov };
21866b8cc1d1SDaniel Borkmann
21876b8cc1d1SDaniel Borkmann const struct bpf_prog_ops perf_event_prog_ops = {
21880515e599SAlexei Starovoitov };
21890515e599SAlexei Starovoitov
21900515e599SAlexei Starovoitov static DEFINE_MUTEX(bpf_event_mutex);
21910515e599SAlexei Starovoitov
21920515e599SAlexei Starovoitov #define BPF_TRACE_MAX_PROGS 64
21930515e599SAlexei Starovoitov
perf_event_attach_bpf_prog(struct perf_event * event,struct bpf_prog * prog,u64 bpf_cookie)21947de16e3aSJakub Kicinski int perf_event_attach_bpf_prog(struct perf_event *event,
2195f005afedSYonghong Song struct bpf_prog *prog,
21960515e599SAlexei Starovoitov u64 bpf_cookie)
21970515e599SAlexei Starovoitov {
21980515e599SAlexei Starovoitov struct bpf_prog_array *old_array;
21997de16e3aSJakub Kicinski struct bpf_prog_array *new_array;
22007de16e3aSJakub Kicinski int ret = -EEXIST;
22017de16e3aSJakub Kicinski
2202e87c6bc3SYonghong Song /*
2203e87c6bc3SYonghong Song * Kprobe override only works if they are on the function entry,
2204e87c6bc3SYonghong Song * and only if they are on the opt-in list.
2205c8c088baSYonghong Song */
2206c8c088baSYonghong Song if (prog->kprobe_override &&
2207e87c6bc3SYonghong Song (!trace_kprobe_on_func_entry(event->tp_event) ||
220882e6b1eeSAndrii Nakryiko !trace_kprobe_error_injectable(event->tp_event)))
220982e6b1eeSAndrii Nakryiko return -EINVAL;
2210e87c6bc3SYonghong Song
2211e672db03SStanislav Fomichev mutex_lock(&bpf_event_mutex);
2212e87c6bc3SYonghong Song
2213e87c6bc3SYonghong Song if (event->prog)
2214e87c6bc3SYonghong Song goto unlock;
22159802d865SJosef Bacik
2216b4da3340SMasami Hiramatsu old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2217b4da3340SMasami Hiramatsu if (old_array &&
22189802d865SJosef Bacik bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
22199802d865SJosef Bacik ret = -E2BIG;
2220b4da3340SMasami Hiramatsu goto unlock;
22219802d865SJosef Bacik }
22229802d865SJosef Bacik
22239802d865SJosef Bacik ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
2224e87c6bc3SYonghong Song if (ret < 0)
2225e87c6bc3SYonghong Song goto unlock;
2226e87c6bc3SYonghong Song
222707c41a29SYonghong Song /* set the new array to event->tp_event and set event->prog */
2228e87c6bc3SYonghong Song event->prog = prog;
2229e672db03SStanislav Fomichev event->bpf_cookie = bpf_cookie;
2230c8c088baSYonghong Song rcu_assign_pointer(event->tp_event->prog_array, new_array);
2231c8c088baSYonghong Song bpf_prog_array_free_sleepable(old_array);
2232c8c088baSYonghong Song
2233c8c088baSYonghong Song unlock:
2234c8c088baSYonghong Song mutex_unlock(&bpf_event_mutex);
2235c8c088baSYonghong Song return ret;
223682e6b1eeSAndrii Nakryiko }
2237e87c6bc3SYonghong Song
perf_event_detach_bpf_prog(struct perf_event * event)223807c41a29SYonghong Song void perf_event_detach_bpf_prog(struct perf_event *event)
2239e87c6bc3SYonghong Song {
2240e87c6bc3SYonghong Song struct bpf_prog_array *old_array;
2241e87c6bc3SYonghong Song struct bpf_prog_array *new_array;
224282e6b1eeSAndrii Nakryiko struct bpf_prog *prog = NULL;
2243e87c6bc3SYonghong Song int ret;
22448c7dcb84SDelyan Kratunov
2245e87c6bc3SYonghong Song mutex_lock(&bpf_event_mutex);
224607c41a29SYonghong Song
2247e87c6bc3SYonghong Song if (!event->prog)
2248e87c6bc3SYonghong Song goto unlock;
2249e87c6bc3SYonghong Song
2250e87c6bc3SYonghong Song old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2251e87c6bc3SYonghong Song if (!old_array)
2252e87c6bc3SYonghong Song goto put;
2253e672db03SStanislav Fomichev
2254e87c6bc3SYonghong Song ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
2255ca3c4f64SPu Lehui if (ret < 0) {
2256e87c6bc3SYonghong Song bpf_prog_array_delete_safe(old_array, event->prog);
2257e87c6bc3SYonghong Song } else {
2258e87c6bc3SYonghong Song rcu_assign_pointer(event->tp_event->prog_array, new_array);
2259e87c6bc3SYonghong Song bpf_prog_array_free_sleepable(old_array);
2260e87c6bc3SYonghong Song }
226107c41a29SYonghong Song
2262e87c6bc3SYonghong Song put:
2263e672db03SStanislav Fomichev prog = event->prog;
2264978c4486SJiri Olsa event->prog = NULL;
2265978c4486SJiri Olsa
2266978c4486SJiri Olsa unlock:
226782e6b1eeSAndrii Nakryiko mutex_unlock(&bpf_event_mutex);
2268e87c6bc3SYonghong Song
2269e87c6bc3SYonghong Song if (prog) {
2270e87c6bc3SYonghong Song /*
2271e87c6bc3SYonghong Song * It could be that the bpf_prog is not sleepable (and will be freed
22728c7dcb84SDelyan Kratunov * via normal RCU), but is called from a point that supports sleepable
2273e87c6bc3SYonghong Song * programs and uses tasks-trace-RCU.
2274e87c6bc3SYonghong Song */
2275978c4486SJiri Olsa synchronize_rcu_tasks_trace();
2276ca3c4f64SPu Lehui
2277ca3c4f64SPu Lehui bpf_prog_put(prog);
2278ca3c4f64SPu Lehui }
2279ca3c4f64SPu Lehui }
2280ca3c4f64SPu Lehui
perf_event_query_prog_array(struct perf_event * event,void __user * info)2281ca3c4f64SPu Lehui int perf_event_query_prog_array(struct perf_event *event, void __user *info)
2282ca3c4f64SPu Lehui {
2283ef1b808eSJann Horn struct perf_event_query_bpf __user *uquery = info;
2284ef1b808eSJann Horn struct perf_event_query_bpf query = {};
2285ef1b808eSJann Horn struct bpf_prog_array *progs;
2286ef1b808eSJann Horn u32 *ids, prog_cnt, ids_len;
2287ef1b808eSJann Horn int ret;
2288ef1b808eSJann Horn
2289ef1b808eSJann Horn if (!perfmon_capable())
2290ca3c4f64SPu Lehui return -EPERM;
2291ca3c4f64SPu Lehui if (event->attr.type != PERF_TYPE_TRACEPOINT)
2292e87c6bc3SYonghong Song return -EINVAL;
2293f371b304SYonghong Song if (copy_from_user(&query, uquery, sizeof(query)))
2294f4e2298eSYonghong Song return -EFAULT;
2295f371b304SYonghong Song
2296f371b304SYonghong Song ids_len = query.ids_len;
2297f371b304SYonghong Song if (ids_len > BPF_TRACE_MAX_PROGS)
2298e672db03SStanislav Fomichev return -E2BIG;
22993a38bb98SYonghong Song ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2300f371b304SYonghong Song if (!ids)
2301f371b304SYonghong Song return -ENOMEM;
2302031258daSAlexey Budankov /*
2303f371b304SYonghong Song * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
2304f371b304SYonghong Song * is required when user only wants to check for uquery->prog_cnt.
2305f371b304SYonghong Song * There is no need to check for it since the case is handled
2306f371b304SYonghong Song * gracefully in bpf_prog_array_copy_info.
2307f371b304SYonghong Song */
23083a38bb98SYonghong Song
23093a38bb98SYonghong Song mutex_lock(&bpf_event_mutex);
23103a38bb98SYonghong Song progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
23119c481b90SDaniel Borkmann ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
23123a38bb98SYonghong Song mutex_unlock(&bpf_event_mutex);
23133a38bb98SYonghong Song
23143a38bb98SYonghong Song if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
23153a38bb98SYonghong Song copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
23163a38bb98SYonghong Song ret = -EFAULT;
23173a38bb98SYonghong Song
23183a38bb98SYonghong Song kfree(ids);
23193a38bb98SYonghong Song return ret;
23203a38bb98SYonghong Song }
2321f371b304SYonghong Song
2322f371b304SYonghong Song extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2323e672db03SStanislav Fomichev extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2324e672db03SStanislav Fomichev
bpf_get_raw_tracepoint(const char * name)2325f371b304SYonghong Song struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2326f371b304SYonghong Song {
23273a38bb98SYonghong Song struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
23283a38bb98SYonghong Song
23293a38bb98SYonghong Song for (; btp < __stop__bpf_raw_tp; btp++) {
23303a38bb98SYonghong Song if (!strcmp(btp->tp->name, name))
23313a38bb98SYonghong Song return btp;
2332f371b304SYonghong Song }
2333f371b304SYonghong Song
2334c4f6699dSAlexei Starovoitov return bpf_get_raw_tracepoint_module(name);
2335c4f6699dSAlexei Starovoitov }
2336c4f6699dSAlexei Starovoitov
bpf_put_raw_tracepoint(struct bpf_raw_event_map * btp)2337c4f6699dSAlexei Starovoitov void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2338a38d1107SMatt Mullins {
2339c4f6699dSAlexei Starovoitov struct module *mod;
2340c4f6699dSAlexei Starovoitov
2341c4f6699dSAlexei Starovoitov guard(rcu)();
2342c4f6699dSAlexei Starovoitov mod = __module_address((unsigned long)btp);
2343c4f6699dSAlexei Starovoitov module_put(mod);
2344c4f6699dSAlexei Starovoitov }
2345c4f6699dSAlexei Starovoitov
2346a38d1107SMatt Mullins static __always_inline
__bpf_trace_run(struct bpf_raw_tp_link * link,u64 * args)2347a38d1107SMatt Mullins void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
2348a38d1107SMatt Mullins {
2349a38d1107SMatt Mullins struct bpf_prog *prog = link->link.prog;
2350a38d1107SMatt Mullins struct bpf_run_ctx *old_run_ctx;
2351a38d1107SMatt Mullins struct bpf_trace_run_ctx run_ctx;
235212cc126dSAndrii Nakryiko
2353a38d1107SMatt Mullins cant_sleep();
235412cc126dSAndrii Nakryiko if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
235512cc126dSAndrii Nakryiko bpf_prog_inc_misses_counter(prog);
2356a38d1107SMatt Mullins goto out;
235712cc126dSAndrii Nakryiko }
2358c4f6699dSAlexei Starovoitov
2359c4f6699dSAlexei Starovoitov run_ctx.bpf_cookie = link->cookie;
2360c4f6699dSAlexei Starovoitov old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2361d4dfc570SAndrii Nakryiko
2362c4f6699dSAlexei Starovoitov rcu_read_lock();
2363d4dfc570SAndrii Nakryiko (void) bpf_prog_run(prog, args);
236468ca5d4eSAndrii Nakryiko rcu_read_unlock();
236568ca5d4eSAndrii Nakryiko
2366d4dfc570SAndrii Nakryiko bpf_reset_run_ctx(old_run_ctx);
2367f03efe49SThomas Gleixner out:
236805b24ff9SJiri Olsa this_cpu_dec(*(prog->active));
236905b24ff9SJiri Olsa }
237005b24ff9SJiri Olsa
237105b24ff9SJiri Olsa #define UNPACK(...) __VA_ARGS__
237268ca5d4eSAndrii Nakryiko #define REPEAT_1(FN, DL, X, ...) FN(X)
237368ca5d4eSAndrii Nakryiko #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
237468ca5d4eSAndrii Nakryiko #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
237568ca5d4eSAndrii Nakryiko #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2376c4f6699dSAlexei Starovoitov #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2377fb7dd8bcSAndrii Nakryiko #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2378c4f6699dSAlexei Starovoitov #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
237968ca5d4eSAndrii Nakryiko #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
238068ca5d4eSAndrii Nakryiko #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
238105b24ff9SJiri Olsa #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
238205b24ff9SJiri Olsa #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2383c4f6699dSAlexei Starovoitov #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2384c4f6699dSAlexei Starovoitov #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2385c4f6699dSAlexei Starovoitov
2386c4f6699dSAlexei Starovoitov #define SARG(X) u64 arg##X
2387c4f6699dSAlexei Starovoitov #define COPY(X) args[X] = arg##X
2388c4f6699dSAlexei Starovoitov
2389c4f6699dSAlexei Starovoitov #define __DL_COM (,)
2390c4f6699dSAlexei Starovoitov #define __DL_SEM (;)
2391c4f6699dSAlexei Starovoitov
2392c4f6699dSAlexei Starovoitov #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2393c4f6699dSAlexei Starovoitov
2394c4f6699dSAlexei Starovoitov #define BPF_TRACE_DEFN_x(x) \
2395c4f6699dSAlexei Starovoitov void bpf_trace_run##x(struct bpf_raw_tp_link *link, \
2396c4f6699dSAlexei Starovoitov REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2397c4f6699dSAlexei Starovoitov { \
2398c4f6699dSAlexei Starovoitov u64 args[x]; \
2399c4f6699dSAlexei Starovoitov REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2400c4f6699dSAlexei Starovoitov __bpf_trace_run(link, args); \
2401c4f6699dSAlexei Starovoitov } \
2402c4f6699dSAlexei Starovoitov EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2403c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(1);
2404c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(2);
2405c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(3);
2406c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(4);
2407c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(5);
2408c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(6);
2409d4dfc570SAndrii Nakryiko BPF_TRACE_DEFN_x(7);
2410c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(8);
2411c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(9);
2412c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(10);
2413c4f6699dSAlexei Starovoitov BPF_TRACE_DEFN_x(11);
2414d4dfc570SAndrii Nakryiko BPF_TRACE_DEFN_x(12);
2415c4f6699dSAlexei Starovoitov
bpf_probe_register(struct bpf_raw_event_map * btp,struct bpf_raw_tp_link * link)2416c4f6699dSAlexei Starovoitov int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
2417c4f6699dSAlexei Starovoitov {
2418c4f6699dSAlexei Starovoitov struct tracepoint *tp = btp->tp;
2419c4f6699dSAlexei Starovoitov struct bpf_prog *prog = link->link.prog;
2420c4f6699dSAlexei Starovoitov
2421c4f6699dSAlexei Starovoitov /*
2422c4f6699dSAlexei Starovoitov * check that program doesn't access arguments beyond what's
2423c4f6699dSAlexei Starovoitov * available in this tracepoint
2424c4f6699dSAlexei Starovoitov */
2425c4f6699dSAlexei Starovoitov if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2426c4f6699dSAlexei Starovoitov return -EINVAL;
2427c4f6699dSAlexei Starovoitov
2428c4f6699dSAlexei Starovoitov if (prog->aux->max_tp_access > btp->writable_size)
2429c4f6699dSAlexei Starovoitov return -EINVAL;
2430d4dfc570SAndrii Nakryiko
2431c4f6699dSAlexei Starovoitov return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func, link);
2432c4f6699dSAlexei Starovoitov }
2433d4dfc570SAndrii Nakryiko
bpf_probe_unregister(struct bpf_raw_event_map * btp,struct bpf_raw_tp_link * link)2434c4f6699dSAlexei Starovoitov int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
2435c4f6699dSAlexei Starovoitov {
2436c4f6699dSAlexei Starovoitov return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, link);
2437c4f6699dSAlexei Starovoitov }
2438c4f6699dSAlexei Starovoitov
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)2439c4f6699dSAlexei Starovoitov int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2440c4f6699dSAlexei Starovoitov u32 *fd_type, const char **buf,
2441c4f6699dSAlexei Starovoitov u64 *probe_offset, u64 *probe_addr,
24429df1c28bSMatt Mullins unsigned long *missed)
24439df1c28bSMatt Mullins {
24449df1c28bSMatt Mullins bool is_tracepoint, is_syscall_tp;
2445d4dfc570SAndrii Nakryiko struct bpf_prog *prog;
2446c4f6699dSAlexei Starovoitov int flags, err = 0;
2447c4f6699dSAlexei Starovoitov
2448d4dfc570SAndrii Nakryiko prog = event->prog;
2449c4f6699dSAlexei Starovoitov if (!prog)
2450d4dfc570SAndrii Nakryiko return -ENOENT;
2451c4f6699dSAlexei Starovoitov
245241bdc4b4SYonghong Song /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
245341bdc4b4SYonghong Song if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
245441bdc4b4SYonghong Song return -EOPNOTSUPP;
24553acf8aceSJiri Olsa
24563acf8aceSJiri Olsa *prog_id = prog->aux->id;
245741bdc4b4SYonghong Song flags = event->tp_event->flags;
245841bdc4b4SYonghong Song is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
245941bdc4b4SYonghong Song is_syscall_tp = is_syscall_trace_event(event->tp_event);
246041bdc4b4SYonghong Song
246141bdc4b4SYonghong Song if (is_tracepoint || is_syscall_tp) {
246241bdc4b4SYonghong Song *buf = is_tracepoint ? event->tp_event->tp->name
246341bdc4b4SYonghong Song : event->tp_event->name;
246441bdc4b4SYonghong Song /* We allow NULL pointer for tracepoint */
246541bdc4b4SYonghong Song if (fd_type)
246641bdc4b4SYonghong Song *fd_type = BPF_FD_TYPE_TRACEPOINT;
246741bdc4b4SYonghong Song if (probe_offset)
246841bdc4b4SYonghong Song *probe_offset = 0x0;
246941bdc4b4SYonghong Song if (probe_addr)
247041bdc4b4SYonghong Song *probe_addr = 0x0;
247141bdc4b4SYonghong Song } else {
247241bdc4b4SYonghong Song /* kprobe/uprobe */
247341bdc4b4SYonghong Song err = -EOPNOTSUPP;
247441bdc4b4SYonghong Song #ifdef CONFIG_KPROBE_EVENTS
247541bdc4b4SYonghong Song if (flags & TRACE_EVENT_FL_KPROBE)
247641bdc4b4SYonghong Song err = bpf_get_kprobe_info(event, fd_type, buf,
247741bdc4b4SYonghong Song probe_offset, probe_addr, missed,
24781b715e1bSYafang Shao event->attr.type == PERF_TYPE_TRACEPOINT);
24791b715e1bSYafang Shao #endif
248041bdc4b4SYonghong Song #ifdef CONFIG_UPROBE_EVENTS
24811b715e1bSYafang Shao if (flags & TRACE_EVENT_FL_UPROBE)
248241bdc4b4SYonghong Song err = bpf_get_uprobe_info(event, fd_type, buf,
24831b715e1bSYafang Shao probe_offset, probe_addr,
248441bdc4b4SYonghong Song event->attr.type == PERF_TYPE_TRACEPOINT);
248541bdc4b4SYonghong Song #endif
248641bdc4b4SYonghong Song }
248741bdc4b4SYonghong Song
248841bdc4b4SYonghong Song return err;
248941bdc4b4SYonghong Song }
249041bdc4b4SYonghong Song
send_signal_irq_work_init(void)24913acf8aceSJiri Olsa static int __init send_signal_irq_work_init(void)
249241bdc4b4SYonghong Song {
249341bdc4b4SYonghong Song int cpu;
249441bdc4b4SYonghong Song struct send_signal_irq_work *work;
249541bdc4b4SYonghong Song
249641bdc4b4SYonghong Song for_each_possible_cpu(cpu) {
24975125e757SYafang Shao work = per_cpu_ptr(&send_signal_work, cpu);
249841bdc4b4SYonghong Song init_irq_work(&work->irq_work, do_bpf_send_signal);
249941bdc4b4SYonghong Song }
250041bdc4b4SYonghong Song return 0;
250141bdc4b4SYonghong Song }
250241bdc4b4SYonghong Song
250341bdc4b4SYonghong Song subsys_initcall(send_signal_irq_work_init);
2504a38d1107SMatt Mullins
25059db1ff0aSYonghong Song #ifdef CONFIG_MODULES
bpf_event_notify(struct notifier_block * nb,unsigned long op,void * module)25069db1ff0aSYonghong Song static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
25079db1ff0aSYonghong Song void *module)
25089db1ff0aSYonghong Song {
25099db1ff0aSYonghong Song struct bpf_trace_module *btm, *tmp;
25109db1ff0aSYonghong Song struct module *mod = module;
25119db1ff0aSYonghong Song int ret = 0;
25129db1ff0aSYonghong Song
25139db1ff0aSYonghong Song if (mod->num_bpf_raw_events == 0 ||
25149db1ff0aSYonghong Song (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
25159db1ff0aSYonghong Song goto out;
25169db1ff0aSYonghong Song
25179db1ff0aSYonghong Song mutex_lock(&bpf_module_mutex);
25189db1ff0aSYonghong Song
2519a38d1107SMatt Mullins switch (op) {
2520390e99cfSStanislav Fomichev case MODULE_STATE_COMING:
2521390e99cfSStanislav Fomichev btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2522a38d1107SMatt Mullins if (btm) {
2523a38d1107SMatt Mullins btm->module = module;
2524a38d1107SMatt Mullins list_add(&btm->list, &bpf_trace_modules);
25250340a6b7SPeter Zijlstra } else {
2526a38d1107SMatt Mullins ret = -ENOMEM;
2527a38d1107SMatt Mullins }
2528a38d1107SMatt Mullins break;
25290340a6b7SPeter Zijlstra case MODULE_STATE_GOING:
2530a38d1107SMatt Mullins list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2531a38d1107SMatt Mullins if (btm->module == module) {
2532a38d1107SMatt Mullins list_del(&btm->list);
2533a38d1107SMatt Mullins kfree(btm);
2534a38d1107SMatt Mullins break;
2535a38d1107SMatt Mullins }
2536a38d1107SMatt Mullins }
2537a38d1107SMatt Mullins break;
2538a38d1107SMatt Mullins }
25390340a6b7SPeter Zijlstra
25400340a6b7SPeter Zijlstra mutex_unlock(&bpf_module_mutex);
2541a38d1107SMatt Mullins
2542a38d1107SMatt Mullins out:
2543a38d1107SMatt Mullins return notifier_from_errno(ret);
2544a38d1107SMatt Mullins }
2545a38d1107SMatt Mullins
2546a38d1107SMatt Mullins static struct notifier_block bpf_module_nb = {
2547a38d1107SMatt Mullins .notifier_call = bpf_event_notify,
2548a38d1107SMatt Mullins };
2549a38d1107SMatt Mullins
bpf_event_init(void)2550a38d1107SMatt Mullins static int __init bpf_event_init(void)
2551a38d1107SMatt Mullins {
2552a38d1107SMatt Mullins register_module_notifier(&bpf_module_nb);
2553a38d1107SMatt Mullins return 0;
2554a38d1107SMatt Mullins }
2555a38d1107SMatt Mullins
25560340a6b7SPeter Zijlstra fs_initcall(bpf_event_init);
25570340a6b7SPeter Zijlstra #endif /* CONFIG_MODULES */
2558a38d1107SMatt Mullins
2559a38d1107SMatt Mullins struct bpf_session_run_ctx {
2560a38d1107SMatt Mullins struct bpf_run_ctx run_ctx;
2561a38d1107SMatt Mullins bool is_return;
2562a38d1107SMatt Mullins void *data;
2563a38d1107SMatt Mullins };
2564390e99cfSStanislav Fomichev
2565a38d1107SMatt Mullins #ifdef CONFIG_FPROBE
2566a38d1107SMatt Mullins struct bpf_kprobe_multi_link {
2567a38d1107SMatt Mullins struct bpf_link link;
2568a38d1107SMatt Mullins struct fprobe fp;
2569a38d1107SMatt Mullins unsigned long *addrs;
2570a38d1107SMatt Mullins u64 *cookies;
2571a38d1107SMatt Mullins u32 cnt;
25720dcac272SJiri Olsa u32 mods_cnt;
2573adf46d88SJiri Olsa struct module **mods;
2574adf46d88SJiri Olsa u32 flags;
2575adf46d88SJiri Olsa };
25765c919aceSJiri Olsa
2577adf46d88SJiri Olsa struct bpf_kprobe_multi_run_ctx {
2578adf46d88SJiri Olsa struct bpf_session_run_ctx session_ctx;
25790dcac272SJiri Olsa struct bpf_kprobe_multi_link *link;
25800dcac272SJiri Olsa unsigned long entry_ip;
25810dcac272SJiri Olsa };
25820dcac272SJiri Olsa
25830dcac272SJiri Olsa struct user_syms {
2584ca74823cSJiri Olsa const char **syms;
2585ca74823cSJiri Olsa char *buf;
2586e22061b2SJiri Olsa };
2587e22061b2SJiri Olsa
25887ac8d0d2SYafang Shao #ifndef CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS
25890dcac272SJiri Olsa static DEFINE_PER_CPU(struct pt_regs, bpf_kprobe_multi_pt_regs);
25900dcac272SJiri Olsa #define bpf_kprobe_multi_pt_regs_ptr() this_cpu_ptr(&bpf_kprobe_multi_pt_regs)
2591f7098690SJiri Olsa #else
2592adf46d88SJiri Olsa #define bpf_kprobe_multi_pt_regs_ptr() (NULL)
2593f7098690SJiri Olsa #endif
2594f7098690SJiri Olsa
ftrace_get_entry_ip(unsigned long fentry_ip)2595f7098690SJiri Olsa static unsigned long ftrace_get_entry_ip(unsigned long fentry_ip)
2596f7098690SJiri Olsa {
25970236fec5SJiri Olsa unsigned long ip = ftrace_get_symaddr(fentry_ip);
25980236fec5SJiri Olsa
25990236fec5SJiri Olsa return ip ? : fentry_ip;
26000236fec5SJiri Olsa }
26010236fec5SJiri Olsa
copy_user_syms(struct user_syms * us,unsigned long __user * usyms,u32 cnt)26028e2759daSMasami Hiramatsu (Google) static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
26038e2759daSMasami Hiramatsu (Google) {
26048e2759daSMasami Hiramatsu (Google) unsigned long __user usymbol;
26058e2759daSMasami Hiramatsu (Google) const char **syms = NULL;
26068e2759daSMasami Hiramatsu (Google) char *buf = NULL, *p;
26078e2759daSMasami Hiramatsu (Google) int err = -ENOMEM;
26088e2759daSMasami Hiramatsu (Google) unsigned int i;
26094f7caaa2SMasami Hiramatsu (Google)
26104f7caaa2SMasami Hiramatsu (Google) syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
26114f7caaa2SMasami Hiramatsu (Google) if (!syms)
26124f7caaa2SMasami Hiramatsu (Google) goto error;
26134f7caaa2SMasami Hiramatsu (Google)
26144f7caaa2SMasami Hiramatsu (Google) buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
26154f7caaa2SMasami Hiramatsu (Google) if (!buf)
26160236fec5SJiri Olsa goto error;
26170236fec5SJiri Olsa
26180236fec5SJiri Olsa for (p = buf, i = 0; i < cnt; i++) {
26190236fec5SJiri Olsa if (__get_user(usymbol, usyms + i)) {
26200236fec5SJiri Olsa err = -EFAULT;
26210236fec5SJiri Olsa goto error;
26220236fec5SJiri Olsa }
26230236fec5SJiri Olsa err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
2624fd58f7dfSDan Carpenter if (err == KSYM_NAME_LEN)
26250236fec5SJiri Olsa err = -E2BIG;
26260236fec5SJiri Olsa if (err < 0)
26270236fec5SJiri Olsa goto error;
2628fd58f7dfSDan Carpenter syms[i] = p;
26290236fec5SJiri Olsa p += err + 1;
26300236fec5SJiri Olsa }
26310236fec5SJiri Olsa
26320236fec5SJiri Olsa us->syms = syms;
26330236fec5SJiri Olsa us->buf = buf;
26340236fec5SJiri Olsa return 0;
26350236fec5SJiri Olsa
26360236fec5SJiri Olsa error:
26370236fec5SJiri Olsa if (err) {
26380236fec5SJiri Olsa kvfree(syms);
26390236fec5SJiri Olsa kvfree(buf);
26400236fec5SJiri Olsa }
26410236fec5SJiri Olsa return err;
26420236fec5SJiri Olsa }
26430236fec5SJiri Olsa
kprobe_multi_put_modules(struct module ** mods,u32 cnt)26440236fec5SJiri Olsa static void kprobe_multi_put_modules(struct module **mods, u32 cnt)
26450236fec5SJiri Olsa {
26460236fec5SJiri Olsa u32 i;
26470236fec5SJiri Olsa
26480236fec5SJiri Olsa for (i = 0; i < cnt; i++)
26490236fec5SJiri Olsa module_put(mods[i]);
26500236fec5SJiri Olsa }
26510236fec5SJiri Olsa
free_user_syms(struct user_syms * us)26520236fec5SJiri Olsa static void free_user_syms(struct user_syms *us)
26530236fec5SJiri Olsa {
26540236fec5SJiri Olsa kvfree(us->syms);
26550236fec5SJiri Olsa kvfree(us->buf);
26560236fec5SJiri Olsa }
26570236fec5SJiri Olsa
bpf_kprobe_multi_link_release(struct bpf_link * link)2658e22061b2SJiri Olsa static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2659e22061b2SJiri Olsa {
2660e22061b2SJiri Olsa struct bpf_kprobe_multi_link *kmulti_link;
2661e22061b2SJiri Olsa
2662e22061b2SJiri Olsa kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2663e22061b2SJiri Olsa unregister_fprobe(&kmulti_link->fp);
2664e22061b2SJiri Olsa kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt);
2665e22061b2SJiri Olsa }
26660236fec5SJiri Olsa
bpf_kprobe_multi_link_dealloc(struct bpf_link * link)26670236fec5SJiri Olsa static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
26680236fec5SJiri Olsa {
26690236fec5SJiri Olsa struct bpf_kprobe_multi_link *kmulti_link;
26700236fec5SJiri Olsa
26710236fec5SJiri Olsa kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
26720dcac272SJiri Olsa kvfree(kmulti_link->addrs);
26730dcac272SJiri Olsa kvfree(kmulti_link->cookies);
26740dcac272SJiri Olsa kfree(kmulti_link->mods);
26750dcac272SJiri Olsa kfree(kmulti_link);
26760dcac272SJiri Olsa }
26770dcac272SJiri Olsa
bpf_kprobe_multi_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)2678e22061b2SJiri Olsa static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
26790dcac272SJiri Olsa struct bpf_link_info *info)
26800dcac272SJiri Olsa {
26810dcac272SJiri Olsa u64 __user *ucookies = u64_to_user_ptr(info->kprobe_multi.cookies);
26820dcac272SJiri Olsa u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
26830dcac272SJiri Olsa struct bpf_kprobe_multi_link *kmulti_link;
26840dcac272SJiri Olsa u32 ucount = info->kprobe_multi.count;
26850dcac272SJiri Olsa int err = 0, i;
26860dcac272SJiri Olsa
2687ca74823cSJiri Olsa if (!uaddrs ^ !ucount)
2688e22061b2SJiri Olsa return -EINVAL;
26890dcac272SJiri Olsa if (ucookies && !ucount)
26900dcac272SJiri Olsa return -EINVAL;
26910dcac272SJiri Olsa
26927ac8d0d2SYafang Shao kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
26937ac8d0d2SYafang Shao info->kprobe_multi.count = kmulti_link->cnt;
26947ac8d0d2SYafang Shao info->kprobe_multi.flags = kmulti_link->flags;
26959fd112b1SJiri Olsa info->kprobe_multi.missed = kmulti_link->fp.nmissed;
26967ac8d0d2SYafang Shao
26977ac8d0d2SYafang Shao if (!uaddrs)
26987ac8d0d2SYafang Shao return 0;
26997ac8d0d2SYafang Shao if (ucount < kmulti_link->cnt)
27007ac8d0d2SYafang Shao err = -ENOSPC;
27017ac8d0d2SYafang Shao else
27027ac8d0d2SYafang Shao ucount = kmulti_link->cnt;
27039fd112b1SJiri Olsa
27049fd112b1SJiri Olsa if (ucookies) {
27057ac8d0d2SYafang Shao if (kmulti_link->cookies) {
27067ac8d0d2SYafang Shao if (copy_to_user(ucookies, kmulti_link->cookies, ucount * sizeof(u64)))
27077ac8d0d2SYafang Shao return -EFAULT;
27087ac8d0d2SYafang Shao } else {
2709e2b2cd59SJiri Olsa for (i = 0; i < ucount; i++) {
27107ac8d0d2SYafang Shao if (put_user(0, ucookies + i))
27117ac8d0d2SYafang Shao return -EFAULT;
27127ac8d0d2SYafang Shao }
27137ac8d0d2SYafang Shao }
27147ac8d0d2SYafang Shao }
27157ac8d0d2SYafang Shao
27167ac8d0d2SYafang Shao if (kallsyms_show_value(current_cred())) {
27177ac8d0d2SYafang Shao if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
27189fd112b1SJiri Olsa return -EFAULT;
27199fd112b1SJiri Olsa } else {
27209fd112b1SJiri Olsa for (i = 0; i < ucount; i++) {
27219fd112b1SJiri Olsa if (put_user(0, uaddrs + i))
27229fd112b1SJiri Olsa return -EFAULT;
27239fd112b1SJiri Olsa }
27249fd112b1SJiri Olsa }
27259fd112b1SJiri Olsa return err;
27269fd112b1SJiri Olsa }
27279fd112b1SJiri Olsa
27289fd112b1SJiri Olsa static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
27299fd112b1SJiri Olsa .release = bpf_kprobe_multi_link_release,
27307ac8d0d2SYafang Shao .dealloc_deferred = bpf_kprobe_multi_link_dealloc,
27317ac8d0d2SYafang Shao .fill_link_info = bpf_kprobe_multi_link_fill_link_info,
27327ac8d0d2SYafang Shao };
27337ac8d0d2SYafang Shao
bpf_kprobe_multi_cookie_swap(void * a,void * b,int size,const void * priv)27347ac8d0d2SYafang Shao static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
27357ac8d0d2SYafang Shao {
27367ac8d0d2SYafang Shao const struct bpf_kprobe_multi_link *link = priv;
27377ac8d0d2SYafang Shao unsigned long *addr_a = a, *addr_b = b;
27387ac8d0d2SYafang Shao u64 *cookie_a, *cookie_b;
27397ac8d0d2SYafang Shao
27407ac8d0d2SYafang Shao cookie_a = link->cookies + (addr_a - link->addrs);
27417ac8d0d2SYafang Shao cookie_b = link->cookies + (addr_b - link->addrs);
27420dcac272SJiri Olsa
27430dcac272SJiri Olsa /* swap addr_a/addr_b and cookie_a/cookie_b values */
27441a80dbcbSAndrii Nakryiko swap(*addr_a, *addr_b);
27457ac8d0d2SYafang Shao swap(*cookie_a, *cookie_b);
27460dcac272SJiri Olsa }
27470dcac272SJiri Olsa
bpf_kprobe_multi_addrs_cmp(const void * a,const void * b)2748ca74823cSJiri Olsa static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b)
2749ca74823cSJiri Olsa {
2750ca74823cSJiri Olsa const unsigned long *addr_a = a, *addr_b = b;
2751ca74823cSJiri Olsa
2752ca74823cSJiri Olsa if (*addr_a == *addr_b)
2753ca74823cSJiri Olsa return 0;
2754ca74823cSJiri Olsa return *addr_a < *addr_b ? -1 : 1;
2755ca74823cSJiri Olsa }
2756ca74823cSJiri Olsa
bpf_kprobe_multi_cookie_cmp(const void * a,const void * b,const void * priv)2757ca74823cSJiri Olsa static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
275811e17ae4SJiapeng Chong {
275911e17ae4SJiapeng Chong return bpf_kprobe_multi_addrs_cmp(a, b);
2760ca74823cSJiri Olsa }
2761ca74823cSJiri Olsa
bpf_kprobe_multi_cookie(struct bpf_run_ctx * ctx)27621a1b0716SJiri Olsa static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2763ca74823cSJiri Olsa {
2764ca74823cSJiri Olsa struct bpf_kprobe_multi_run_ctx *run_ctx;
2765ca74823cSJiri Olsa struct bpf_kprobe_multi_link *link;
2766ca74823cSJiri Olsa u64 *cookie, entry_ip;
2767ca74823cSJiri Olsa unsigned long *addr;
2768ca74823cSJiri Olsa
2769ca74823cSJiri Olsa if (WARN_ON_ONCE(!ctx))
2770ca74823cSJiri Olsa return 0;
2771ca74823cSJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
2772ca74823cSJiri Olsa session_ctx.run_ctx);
27731a1b0716SJiri Olsa link = run_ctx->link;
2774ca74823cSJiri Olsa if (!link->cookies)
2775ca74823cSJiri Olsa return 0;
2776f7098690SJiri Olsa entry_ip = run_ctx->entry_ip;
2777ca74823cSJiri Olsa addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
2778f7098690SJiri Olsa bpf_kprobe_multi_addrs_cmp);
2779ca74823cSJiri Olsa if (!addr)
2780f7098690SJiri Olsa return 0;
2781ca74823cSJiri Olsa cookie = link->cookies + (addr - link->addrs);
2782ca74823cSJiri Olsa return *cookie;
2783ca74823cSJiri Olsa }
2784ca74823cSJiri Olsa
bpf_kprobe_multi_entry_ip(struct bpf_run_ctx * ctx)2785adf46d88SJiri Olsa static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2786adf46d88SJiri Olsa {
2787f7098690SJiri Olsa struct bpf_kprobe_multi_run_ctx *run_ctx;
2788ca74823cSJiri Olsa
2789ca74823cSJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
2790f7098690SJiri Olsa session_ctx.run_ctx);
2791f7098690SJiri Olsa return run_ctx->entry_ip;
27921a1b0716SJiri Olsa }
2793ca74823cSJiri Olsa
2794ca74823cSJiri Olsa static int
kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link * link,unsigned long entry_ip,struct ftrace_regs * fregs,bool is_return,void * data)2795ca74823cSJiri Olsa kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2796ca74823cSJiri Olsa unsigned long entry_ip, struct ftrace_regs *fregs,
2797ca74823cSJiri Olsa bool is_return, void *data)
2798ca74823cSJiri Olsa {
2799f7098690SJiri Olsa struct bpf_kprobe_multi_run_ctx run_ctx = {
2800f7098690SJiri Olsa .session_ctx = {
2801f7098690SJiri Olsa .is_return = is_return,
2802f7098690SJiri Olsa .data = data,
2803adf46d88SJiri Olsa },
2804adf46d88SJiri Olsa .link = link,
2805f7098690SJiri Olsa .entry_ip = entry_ip,
2806f7098690SJiri Olsa };
2807f7098690SJiri Olsa struct bpf_run_ctx *old_run_ctx;
28080dcac272SJiri Olsa struct pt_regs *regs;
28090dcac272SJiri Olsa int err;
28108e2759daSMasami Hiramatsu (Google)
28115c919aceSJiri Olsa if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
28120dcac272SJiri Olsa bpf_prog_inc_misses_counter(link->link.prog);
2813f7098690SJiri Olsa err = 1;
2814adf46d88SJiri Olsa goto out;
2815adf46d88SJiri Olsa }
28165c919aceSJiri Olsa
2817adf46d88SJiri Olsa migrate_disable();
2818f7098690SJiri Olsa rcu_read_lock();
2819f7098690SJiri Olsa regs = ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr());
2820f7098690SJiri Olsa old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx);
2821ca74823cSJiri Olsa err = bpf_prog_run(link->link.prog, regs);
28228e2759daSMasami Hiramatsu (Google) bpf_reset_run_ctx(old_run_ctx);
28230dcac272SJiri Olsa rcu_read_unlock();
28240dcac272SJiri Olsa migrate_enable();
28250dcac272SJiri Olsa
2826f915fcb3SJiri Olsa out:
28272ebadb60SJiri Olsa __this_cpu_dec(bpf_prog_active);
28280dcac272SJiri Olsa return err;
28290dcac272SJiri Olsa }
28300dcac272SJiri Olsa
28310dcac272SJiri Olsa static int
kprobe_multi_link_handler(struct fprobe * fp,unsigned long fentry_ip,unsigned long ret_ip,struct ftrace_regs * fregs,void * data)28320dcac272SJiri Olsa kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
28338e2759daSMasami Hiramatsu (Google) unsigned long ret_ip, struct ftrace_regs *fregs,
2834adf46d88SJiri Olsa void *data)
28350dcac272SJiri Olsa {
2836ca74823cSJiri Olsa struct bpf_kprobe_multi_link *link;
28370dcac272SJiri Olsa int err;
28380dcac272SJiri Olsa
28390dcac272SJiri Olsa link = container_of(fp, struct bpf_kprobe_multi_link, fp);
28400dcac272SJiri Olsa err = kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip),
28410dcac272SJiri Olsa fregs, false, data);
28420dcac272SJiri Olsa return is_kprobe_session(link->link.prog) ? err : 0;
28430dcac272SJiri Olsa }
28440dcac272SJiri Olsa
284539d95420SMasami Hiramatsu (Google) static void
kprobe_multi_link_exit_handler(struct fprobe * fp,unsigned long fentry_ip,unsigned long ret_ip,struct ftrace_regs * fregs,void * data)2846c09eb2e5SJiri Olsa kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
284746bc0823SMasami Hiramatsu (Google) unsigned long ret_ip, struct ftrace_regs *fregs,
2848cb16330dSMasami Hiramatsu (Google) void *data)
28490dcac272SJiri Olsa {
28500dcac272SJiri Olsa struct bpf_kprobe_multi_link *link;
2851535a3692SJiri Olsa
28520dcac272SJiri Olsa link = container_of(fp, struct bpf_kprobe_multi_link, fp);
28530dcac272SJiri Olsa kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip),
28544f7caaa2SMasami Hiramatsu (Google) fregs, true, data);
28554f7caaa2SMasami Hiramatsu (Google) }
2856535a3692SJiri Olsa
symbols_cmp_r(const void * a,const void * b,const void * priv)285739d95420SMasami Hiramatsu (Google) static int symbols_cmp_r(const void *a, const void *b, const void *priv)
285839d95420SMasami Hiramatsu (Google) {
285939d95420SMasami Hiramatsu (Google) const char **str_a = (const char **) a;
286039d95420SMasami Hiramatsu (Google) const char **str_b = (const char **) b;
2861762abbc0SMasami Hiramatsu (Google)
2862cb16330dSMasami Hiramatsu (Google) return strcmp(*str_a, *str_b);
28630dcac272SJiri Olsa }
28640dcac272SJiri Olsa
28650dcac272SJiri Olsa struct multi_symbols_sort {
28660dcac272SJiri Olsa const char **funcs;
28674f7caaa2SMasami Hiramatsu (Google) u64 *cookies;
28684f7caaa2SMasami Hiramatsu (Google) };
28690dcac272SJiri Olsa
symbols_swap_r(void * a,void * b,int size,const void * priv)28700dcac272SJiri Olsa static void symbols_swap_r(void *a, void *b, int size, const void *priv)
2871eb5fb032SJiri Olsa {
28720dcac272SJiri Olsa const struct multi_symbols_sort *data = priv;
28730236fec5SJiri Olsa const char **name_a = a, **name_b = b;
28740236fec5SJiri Olsa
28750dcac272SJiri Olsa swap(*name_a, *name_b);
28760236fec5SJiri Olsa
28770dcac272SJiri Olsa /* If defined, swap also related cookies. */
28780dcac272SJiri Olsa if (data->cookies) {
2879eb5fb032SJiri Olsa u64 *cookie_a, *cookie_b;
2880eb5fb032SJiri Olsa
2881eb5fb032SJiri Olsa cookie_a = data->cookies + (name_a - data->funcs);
2882eb5fb032SJiri Olsa cookie_b = data->cookies + (name_b - data->funcs);
2883eb5fb032SJiri Olsa swap(*cookie_a, *cookie_b);
2884eb5fb032SJiri Olsa }
2885eb5fb032SJiri Olsa }
2886eb5fb032SJiri Olsa
2887eb5fb032SJiri Olsa struct modules_array {
2888eb5fb032SJiri Olsa struct module **mods;
2889eb5fb032SJiri Olsa int mods_cnt;
2890eb5fb032SJiri Olsa int mods_cap;
2891eb5fb032SJiri Olsa };
2892eb5fb032SJiri Olsa
add_module(struct modules_array * arr,struct module * mod)2893eb5fb032SJiri Olsa static int add_module(struct modules_array *arr, struct module *mod)
2894eb5fb032SJiri Olsa {
2895eb5fb032SJiri Olsa struct module **mods;
2896eb5fb032SJiri Olsa
2897eb5fb032SJiri Olsa if (arr->mods_cnt == arr->mods_cap) {
2898eb5fb032SJiri Olsa arr->mods_cap = max(16, arr->mods_cap * 3 / 2);
2899eb5fb032SJiri Olsa mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL);
2900eb5fb032SJiri Olsa if (!mods)
29016a5f2d6eSJiri Olsa return -ENOMEM;
2902e22061b2SJiri Olsa arr->mods = mods;
2903e22061b2SJiri Olsa }
2904e22061b2SJiri Olsa
2905e22061b2SJiri Olsa arr->mods[arr->mods_cnt] = mod;
2906e22061b2SJiri Olsa arr->mods_cnt++;
29076a5f2d6eSJiri Olsa return 0;
2908e22061b2SJiri Olsa }
2909e22061b2SJiri Olsa
has_module(struct modules_array * arr,struct module * mod)2910e22061b2SJiri Olsa static bool has_module(struct modules_array *arr, struct module *mod)
29116a5f2d6eSJiri Olsa {
29126a5f2d6eSJiri Olsa int i;
29136a5f2d6eSJiri Olsa
2914e22061b2SJiri Olsa for (i = arr->mods_cnt - 1; i >= 0; i--) {
2915e22061b2SJiri Olsa if (arr->mods[i] == mod)
29166a5f2d6eSJiri Olsa return true;
2917e22061b2SJiri Olsa }
2918e22061b2SJiri Olsa return false;
29196a5f2d6eSJiri Olsa }
29206a5f2d6eSJiri Olsa
get_modules_for_addrs(struct module *** mods,unsigned long * addrs,u32 addrs_cnt)2921e22061b2SJiri Olsa static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt)
2922e22061b2SJiri Olsa {
2923e22061b2SJiri Olsa struct modules_array arr = {};
29246a5f2d6eSJiri Olsa u32 i, err = 0;
29256a5f2d6eSJiri Olsa
29266a5f2d6eSJiri Olsa for (i = 0; i < addrs_cnt; i++) {
29276a5f2d6eSJiri Olsa bool skip_add = false;
29286a5f2d6eSJiri Olsa struct module *mod;
29296a5f2d6eSJiri Olsa
29306a5f2d6eSJiri Olsa scoped_guard(rcu) {
29316a5f2d6eSJiri Olsa mod = __module_address(addrs[i]);
29326a5f2d6eSJiri Olsa /* Either no module or it's already stored */
29336a5f2d6eSJiri Olsa if (!mod || has_module(&arr, mod)) {
29346a5f2d6eSJiri Olsa skip_add = true;
2935e22061b2SJiri Olsa break; /* scoped_guard */
2936e22061b2SJiri Olsa }
29376a5f2d6eSJiri Olsa if (!try_module_get(mod))
29386a5f2d6eSJiri Olsa err = -EINVAL;
29396a5f2d6eSJiri Olsa }
29406a5f2d6eSJiri Olsa if (skip_add)
29416a5f2d6eSJiri Olsa continue;
29426a5f2d6eSJiri Olsa if (err)
29436a5f2d6eSJiri Olsa break;
29446a5f2d6eSJiri Olsa err = add_module(&arr, mod);
29456a5f2d6eSJiri Olsa if (err) {
29466a5f2d6eSJiri Olsa module_put(mod);
29476a5f2d6eSJiri Olsa break;
29486a5f2d6eSJiri Olsa }
29496a5f2d6eSJiri Olsa }
29506a5f2d6eSJiri Olsa
29516a5f2d6eSJiri Olsa /* We return either err < 0 in case of error, ... */
29526a5f2d6eSJiri Olsa if (err) {
29536a5f2d6eSJiri Olsa kprobe_multi_put_modules(arr.mods, arr.mods_cnt);
29546a5f2d6eSJiri Olsa kfree(arr.mods);
29556a5f2d6eSJiri Olsa return err;
29566a5f2d6eSJiri Olsa }
29576a5f2d6eSJiri Olsa
29586a5f2d6eSJiri Olsa /* or number of modules found if everything is ok. */
29596a5f2d6eSJiri Olsa *mods = arr.mods;
29606a5f2d6eSJiri Olsa return arr.mods_cnt;
2961e22061b2SJiri Olsa }
2962e22061b2SJiri Olsa
addrs_check_error_injection_list(unsigned long * addrs,u32 cnt)2963e22061b2SJiri Olsa static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
29646a5f2d6eSJiri Olsa {
29656a5f2d6eSJiri Olsa u32 i;
2966e22061b2SJiri Olsa
2967e22061b2SJiri Olsa for (i = 0; i < cnt; i++) {
2968e22061b2SJiri Olsa if (!within_error_injection_list(addrs[i]))
2969e22061b2SJiri Olsa return -EINVAL;
29706a5f2d6eSJiri Olsa }
29716a5f2d6eSJiri Olsa return 0;
2972e22061b2SJiri Olsa }
2973e22061b2SJiri Olsa
bpf_kprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)297441bc46c1SJiri Olsa int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
297541bc46c1SJiri Olsa {
297641bc46c1SJiri Olsa struct bpf_kprobe_multi_link *link = NULL;
297741bc46c1SJiri Olsa struct bpf_link_primer link_primer;
297841bc46c1SJiri Olsa void __user *ucookies;
297941bc46c1SJiri Olsa unsigned long *addrs;
298041bc46c1SJiri Olsa u32 flags, cnt, size;
298141bc46c1SJiri Olsa void __user *uaddrs;
298241bc46c1SJiri Olsa u64 *cookies = NULL;
298341bc46c1SJiri Olsa void __user *usyms;
298441bc46c1SJiri Olsa int err;
29850dcac272SJiri Olsa
29860dcac272SJiri Olsa /* no support for 32bit archs yet */
29870dcac272SJiri Olsa if (sizeof(u64) != sizeof(void *))
29880dcac272SJiri Olsa return -EOPNOTSUPP;
2989ca74823cSJiri Olsa
29900dcac272SJiri Olsa if (!is_kprobe_multi(prog))
29910dcac272SJiri Olsa return -EINVAL;
29920dcac272SJiri Olsa
2993ca74823cSJiri Olsa flags = attr->link_create.kprobe_multi.flags;
29940dcac272SJiri Olsa if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
29950dcac272SJiri Olsa return -EINVAL;
29960dcac272SJiri Olsa
29970dcac272SJiri Olsa uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
29980dcac272SJiri Olsa usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
29990dcac272SJiri Olsa if (!!uaddrs == !!usyms)
30000dcac272SJiri Olsa return -EINVAL;
3001535a3692SJiri Olsa
30020dcac272SJiri Olsa cnt = attr->link_create.kprobe_multi.cnt;
30030dcac272SJiri Olsa if (!cnt)
30040dcac272SJiri Olsa return -EINVAL;
30050dcac272SJiri Olsa if (cnt > MAX_KPROBE_MULTI_CNT)
30060dcac272SJiri Olsa return -E2BIG;
30070dcac272SJiri Olsa
30080dcac272SJiri Olsa size = cnt * sizeof(*addrs);
30090dcac272SJiri Olsa addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
30100dcac272SJiri Olsa if (!addrs)
30110dcac272SJiri Olsa return -ENOMEM;
30120dcac272SJiri Olsa
30130dcac272SJiri Olsa ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
30140dcac272SJiri Olsa if (ucookies) {
30150dcac272SJiri Olsa cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
3016d6d1e6c1SHou Tao if (!cookies) {
3017d6d1e6c1SHou Tao err = -ENOMEM;
30180dcac272SJiri Olsa goto error;
30190dcac272SJiri Olsa }
3020fd58f7dfSDan Carpenter if (copy_from_user(cookies, ucookies, size)) {
30210dcac272SJiri Olsa err = -EFAULT;
30220dcac272SJiri Olsa goto error;
30230dcac272SJiri Olsa }
3024ca74823cSJiri Olsa }
3025ca74823cSJiri Olsa
3026fd58f7dfSDan Carpenter if (uaddrs) {
3027ca74823cSJiri Olsa if (copy_from_user(addrs, uaddrs, size)) {
3028ca74823cSJiri Olsa err = -EFAULT;
3029ca74823cSJiri Olsa goto error;
3030ca74823cSJiri Olsa }
3031ca74823cSJiri Olsa } else {
3032ca74823cSJiri Olsa struct multi_symbols_sort data = {
3033ca74823cSJiri Olsa .cookies = cookies,
3034ca74823cSJiri Olsa };
3035ca74823cSJiri Olsa struct user_syms us;
3036ca74823cSJiri Olsa
3037eb5fb032SJiri Olsa err = copy_user_syms(&us, usyms, cnt);
3038eb5fb032SJiri Olsa if (err)
3039eb5fb032SJiri Olsa goto error;
3040eb5fb032SJiri Olsa
3041eb5fb032SJiri Olsa if (cookies)
3042eb5fb032SJiri Olsa data.funcs = us.syms;
3043eb5fb032SJiri Olsa
3044eb5fb032SJiri Olsa sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
3045eb5fb032SJiri Olsa symbols_swap_r, &data);
3046eb5fb032SJiri Olsa
3047eb5fb032SJiri Olsa err = ftrace_lookup_symbols(us.syms, cnt, addrs);
3048eb5fb032SJiri Olsa free_user_syms(&us);
3049eb5fb032SJiri Olsa if (err)
3050eb5fb032SJiri Olsa goto error;
3051eb5fb032SJiri Olsa }
3052eb5fb032SJiri Olsa
3053eb5fb032SJiri Olsa if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) {
3054eb5fb032SJiri Olsa err = -EINVAL;
3055eb5fb032SJiri Olsa goto error;
3056eb5fb032SJiri Olsa }
3057eb5fb032SJiri Olsa
3058eb5fb032SJiri Olsa link = kzalloc(sizeof(*link), GFP_KERNEL);
3059eb5fb032SJiri Olsa if (!link) {
3060eb5fb032SJiri Olsa err = -ENOMEM;
3061eb5fb032SJiri Olsa goto error;
3062eb5fb032SJiri Olsa }
3063eb5fb032SJiri Olsa
306441bc46c1SJiri Olsa bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
306541bc46c1SJiri Olsa &bpf_kprobe_multi_link_lops, prog);
306641bc46c1SJiri Olsa
306741bc46c1SJiri Olsa err = bpf_link_prime(&link->link, &link_primer);
306841bc46c1SJiri Olsa if (err)
30690dcac272SJiri Olsa goto error;
30700dcac272SJiri Olsa
30710dcac272SJiri Olsa if (!(flags & BPF_F_KPROBE_MULTI_RETURN))
30720dcac272SJiri Olsa link->fp.entry_handler = kprobe_multi_link_handler;
30730dcac272SJiri Olsa if ((flags & BPF_F_KPROBE_MULTI_RETURN) || is_kprobe_session(prog))
30740dcac272SJiri Olsa link->fp.exit_handler = kprobe_multi_link_exit_handler;
30750dcac272SJiri Olsa if (is_kprobe_session(prog))
30760dcac272SJiri Olsa link->fp.entry_data_size = sizeof(u64);
30770dcac272SJiri Olsa
30780dcac272SJiri Olsa link->addrs = addrs;
30790dcac272SJiri Olsa link->cookies = cookies;
30800dcac272SJiri Olsa link->cnt = cnt;
30810dcac272SJiri Olsa link->flags = flags;
3082535a3692SJiri Olsa
30830dcac272SJiri Olsa if (cookies) {
3084535a3692SJiri Olsa /*
3085535a3692SJiri Olsa * Sorting addresses will trigger sorting cookies as well
30865c919aceSJiri Olsa * (check bpf_kprobe_multi_cookie_swap). This way we can
30875c919aceSJiri Olsa * find cookie based on the address in bpf_get_attach_cookie
30880dcac272SJiri Olsa * helper.
30890dcac272SJiri Olsa */
3090ca74823cSJiri Olsa sort_r(addrs, cnt, sizeof(*addrs),
3091ca74823cSJiri Olsa bpf_kprobe_multi_cookie_cmp,
30927ac8d0d2SYafang Shao bpf_kprobe_multi_cookie_swap,
3093ca74823cSJiri Olsa link);
3094ca74823cSJiri Olsa }
3095ca74823cSJiri Olsa
3096ca74823cSJiri Olsa err = get_modules_for_addrs(&link->mods, addrs, cnt);
3097ca74823cSJiri Olsa if (err < 0) {
3098ca74823cSJiri Olsa bpf_link_cleanup(&link_primer);
3099ca74823cSJiri Olsa return err;
3100ca74823cSJiri Olsa }
3101ca74823cSJiri Olsa link->mods_cnt = err;
3102ca74823cSJiri Olsa
3103ca74823cSJiri Olsa err = register_fprobe_ips(&link->fp, addrs, cnt);
3104ca74823cSJiri Olsa if (err) {
3105ca74823cSJiri Olsa kprobe_multi_put_modules(link->mods, link->mods_cnt);
31060dcac272SJiri Olsa bpf_link_cleanup(&link_primer);
3107e22061b2SJiri Olsa return err;
3108e22061b2SJiri Olsa }
3109e22061b2SJiri Olsa
3110e22061b2SJiri Olsa return bpf_link_settle(&link_primer);
3111e22061b2SJiri Olsa
3112e22061b2SJiri Olsa error:
3113e22061b2SJiri Olsa kfree(link);
31140dcac272SJiri Olsa kvfree(addrs);
31150dcac272SJiri Olsa kvfree(cookies);
3116e22061b2SJiri Olsa return err;
31170dcac272SJiri Olsa }
31180dcac272SJiri Olsa #else /* !CONFIG_FPROBE */
bpf_kprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)31190dcac272SJiri Olsa int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
31200dcac272SJiri Olsa {
31210dcac272SJiri Olsa return -EOPNOTSUPP;
31220dcac272SJiri Olsa }
bpf_kprobe_multi_cookie(struct bpf_run_ctx * ctx)31230dcac272SJiri Olsa static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
31240dcac272SJiri Olsa {
31250dcac272SJiri Olsa return 0;
3126ca74823cSJiri Olsa }
bpf_kprobe_multi_entry_ip(struct bpf_run_ctx * ctx)31270dcac272SJiri Olsa static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
31280dcac272SJiri Olsa {
31290dcac272SJiri Olsa return 0;
31300dcac272SJiri Olsa }
31310dcac272SJiri Olsa #endif
31320dcac272SJiri Olsa
31330dcac272SJiri Olsa #ifdef CONFIG_UPROBES
3134f7098690SJiri Olsa struct bpf_uprobe_multi_link;
3135f7098690SJiri Olsa
3136f7098690SJiri Olsa struct bpf_uprobe {
3137f7098690SJiri Olsa struct bpf_uprobe_multi_link *link;
3138f7098690SJiri Olsa loff_t offset;
3139ca74823cSJiri Olsa unsigned long ref_ctr_offset;
3140ca74823cSJiri Olsa u64 cookie;
3141ca74823cSJiri Olsa struct uprobe *uprobe;
31420dcac272SJiri Olsa struct uprobe_consumer consumer;
314389ae89f5SJiri Olsa bool session;
314489ae89f5SJiri Olsa };
314589ae89f5SJiri Olsa
314689ae89f5SJiri Olsa struct bpf_uprobe_multi_link {
314789ae89f5SJiri Olsa struct path path;
314889ae89f5SJiri Olsa struct bpf_link link;
314989ae89f5SJiri Olsa u32 cnt;
31504930b7f5SJiri Olsa u32 flags;
31510b779b61SJiri Olsa struct bpf_uprobe *uprobes;
31523c83a9adSOleg Nesterov struct task_struct *task;
315389ae89f5SJiri Olsa };
3154d920179bSJiri Olsa
315589ae89f5SJiri Olsa struct bpf_uprobe_multi_run_ctx {
315689ae89f5SJiri Olsa struct bpf_session_run_ctx session_ctx;
315789ae89f5SJiri Olsa unsigned long entry_ip;
315889ae89f5SJiri Olsa struct bpf_uprobe *uprobe;
315989ae89f5SJiri Olsa };
316089ae89f5SJiri Olsa
bpf_uprobe_unregister(struct bpf_uprobe * uprobes,u32 cnt)3161e56fdbfbSJiri Olsa static void bpf_uprobe_unregister(struct bpf_uprobe *uprobes, u32 cnt)
316289ae89f5SJiri Olsa {
3163b733eeadSJiri Olsa u32 i;
316489ae89f5SJiri Olsa
316589ae89f5SJiri Olsa for (i = 0; i < cnt; i++)
316689ae89f5SJiri Olsa uprobe_unregister_nosync(uprobes[i].uprobe, &uprobes[i].consumer);
316799b403d2SJiri Olsa
316889ae89f5SJiri Olsa if (cnt)
31690b779b61SJiri Olsa uprobe_unregister_sync();
317089ae89f5SJiri Olsa }
317189ae89f5SJiri Olsa
bpf_uprobe_multi_link_release(struct bpf_link * link)31723c83a9adSOleg Nesterov static void bpf_uprobe_multi_link_release(struct bpf_link *link)
317389ae89f5SJiri Olsa {
317489ae89f5SJiri Olsa struct bpf_uprobe_multi_link *umulti_link;
317589ae89f5SJiri Olsa
31763c83a9adSOleg Nesterov umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
317704b01625SPeter Zijlstra bpf_uprobe_unregister(umulti_link->uprobes, umulti_link->cnt);
317804b01625SPeter Zijlstra if (umulti_link->task)
317904b01625SPeter Zijlstra put_task_struct(umulti_link->task);
318004b01625SPeter Zijlstra path_put(&umulti_link->path);
318189ae89f5SJiri Olsa }
318289ae89f5SJiri Olsa
bpf_uprobe_multi_link_dealloc(struct bpf_link * link)318389ae89f5SJiri Olsa static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link)
318489ae89f5SJiri Olsa {
318589ae89f5SJiri Olsa struct bpf_uprobe_multi_link *umulti_link;
318689ae89f5SJiri Olsa
318789ae89f5SJiri Olsa umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
31883c83a9adSOleg Nesterov kvfree(umulti_link->uprobes);
3189e9c856caSAndrii Nakryiko kfree(umulti_link);
3190e9c856caSAndrii Nakryiko }
3191e9c856caSAndrii Nakryiko
bpf_uprobe_multi_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)319289ae89f5SJiri Olsa static int bpf_uprobe_multi_link_fill_link_info(const struct bpf_link *link,
319389ae89f5SJiri Olsa struct bpf_link_info *info)
319489ae89f5SJiri Olsa {
319589ae89f5SJiri Olsa u64 __user *uref_ctr_offsets = u64_to_user_ptr(info->uprobe_multi.ref_ctr_offsets);
319689ae89f5SJiri Olsa u64 __user *ucookies = u64_to_user_ptr(info->uprobe_multi.cookies);
319789ae89f5SJiri Olsa u64 __user *uoffsets = u64_to_user_ptr(info->uprobe_multi.offsets);
319889ae89f5SJiri Olsa u64 __user *upath = u64_to_user_ptr(info->uprobe_multi.path);
319989ae89f5SJiri Olsa u32 upath_size = info->uprobe_multi.path_size;
320089ae89f5SJiri Olsa struct bpf_uprobe_multi_link *umulti_link;
320189ae89f5SJiri Olsa u32 ucount = info->uprobe_multi.count;
320289ae89f5SJiri Olsa int err = 0, i;
3203e56fdbfbSJiri Olsa char *p, *buf;
3204e56fdbfbSJiri Olsa long left = 0;
3205e56fdbfbSJiri Olsa
3206e56fdbfbSJiri Olsa if (!upath ^ !upath_size)
3207e56fdbfbSJiri Olsa return -EINVAL;
3208e56fdbfbSJiri Olsa
3209e56fdbfbSJiri Olsa if ((uoffsets || uref_ctr_offsets || ucookies) && !ucount)
3210e56fdbfbSJiri Olsa return -EINVAL;
3211e56fdbfbSJiri Olsa
3212e56fdbfbSJiri Olsa umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3213e56fdbfbSJiri Olsa info->uprobe_multi.count = umulti_link->cnt;
3214ad6b5b6eSTyrone Wu info->uprobe_multi.flags = umulti_link->flags;
3215ad6b5b6eSTyrone Wu info->uprobe_multi.pid = umulti_link->task ?
3216e56fdbfbSJiri Olsa task_pid_nr_ns(umulti_link->task, task_active_pid_ns(current)) : 0;
3217e56fdbfbSJiri Olsa
3218e56fdbfbSJiri Olsa upath_size = upath_size ? min_t(u32, upath_size, PATH_MAX) : PATH_MAX;
3219e56fdbfbSJiri Olsa buf = kmalloc(upath_size, GFP_KERNEL);
3220e56fdbfbSJiri Olsa if (!buf)
3221e56fdbfbSJiri Olsa return -ENOMEM;
3222e56fdbfbSJiri Olsa p = d_path(&umulti_link->path, buf, upath_size);
3223e56fdbfbSJiri Olsa if (IS_ERR(p)) {
3224e56fdbfbSJiri Olsa kfree(buf);
3225e56fdbfbSJiri Olsa return PTR_ERR(p);
3226e56fdbfbSJiri Olsa }
3227e56fdbfbSJiri Olsa upath_size = buf + upath_size - p;
3228e56fdbfbSJiri Olsa
3229ad6b5b6eSTyrone Wu if (upath)
3230e56fdbfbSJiri Olsa left = copy_to_user(upath, p, upath_size);
3231e56fdbfbSJiri Olsa kfree(buf);
3232e56fdbfbSJiri Olsa if (left)
3233e56fdbfbSJiri Olsa return -EFAULT;
3234e56fdbfbSJiri Olsa info->uprobe_multi.path_size = upath_size;
3235e56fdbfbSJiri Olsa
3236e56fdbfbSJiri Olsa if (!uoffsets && !ucookies && !uref_ctr_offsets)
3237e56fdbfbSJiri Olsa return 0;
3238e56fdbfbSJiri Olsa
3239ad6b5b6eSTyrone Wu if (ucount < umulti_link->cnt)
3240ad6b5b6eSTyrone Wu err = -ENOSPC;
3241e56fdbfbSJiri Olsa else
3242e56fdbfbSJiri Olsa ucount = umulti_link->cnt;
3243e56fdbfbSJiri Olsa
3244e56fdbfbSJiri Olsa for (i = 0; i < ucount; i++) {
3245e56fdbfbSJiri Olsa if (uoffsets &&
3246e56fdbfbSJiri Olsa put_user(umulti_link->uprobes[i].offset, uoffsets + i))
3247e56fdbfbSJiri Olsa return -EFAULT;
3248e56fdbfbSJiri Olsa if (uref_ctr_offsets &&
3249e56fdbfbSJiri Olsa put_user(umulti_link->uprobes[i].ref_ctr_offset, uref_ctr_offsets + i))
3250e56fdbfbSJiri Olsa return -EFAULT;
3251e56fdbfbSJiri Olsa if (ucookies &&
3252e56fdbfbSJiri Olsa put_user(umulti_link->uprobes[i].cookie, ucookies + i))
3253e56fdbfbSJiri Olsa return -EFAULT;
3254e56fdbfbSJiri Olsa }
3255e56fdbfbSJiri Olsa
3256e56fdbfbSJiri Olsa return err;
3257e56fdbfbSJiri Olsa }
3258e56fdbfbSJiri Olsa
3259e56fdbfbSJiri Olsa static const struct bpf_link_ops bpf_uprobe_multi_link_lops = {
3260e56fdbfbSJiri Olsa .release = bpf_uprobe_multi_link_release,
3261e56fdbfbSJiri Olsa .dealloc_deferred = bpf_uprobe_multi_link_dealloc,
3262e56fdbfbSJiri Olsa .fill_link_info = bpf_uprobe_multi_link_fill_link_info,
3263e56fdbfbSJiri Olsa };
3264e56fdbfbSJiri Olsa
uprobe_prog_run(struct bpf_uprobe * uprobe,unsigned long entry_ip,struct pt_regs * regs,bool is_return,void * data)3265e56fdbfbSJiri Olsa static int uprobe_prog_run(struct bpf_uprobe *uprobe,
3266e56fdbfbSJiri Olsa unsigned long entry_ip,
3267e56fdbfbSJiri Olsa struct pt_regs *regs,
3268e56fdbfbSJiri Olsa bool is_return, void *data)
3269e56fdbfbSJiri Olsa {
327089ae89f5SJiri Olsa struct bpf_uprobe_multi_link *link = uprobe->link;
327189ae89f5SJiri Olsa struct bpf_uprobe_multi_run_ctx run_ctx = {
32721a80dbcbSAndrii Nakryiko .session_ctx = {
3273e56fdbfbSJiri Olsa .is_return = is_return,
327489ae89f5SJiri Olsa .data = data,
327589ae89f5SJiri Olsa },
327689ae89f5SJiri Olsa .entry_ip = entry_ip,
327789ae89f5SJiri Olsa .uprobe = uprobe,
327899b403d2SJiri Olsa };
327999b403d2SJiri Olsa struct bpf_prog *prog = link->link.prog;
328089ae89f5SJiri Olsa bool sleepable = prog->sleepable;
328189ae89f5SJiri Olsa struct bpf_run_ctx *old_run_ctx;
328289ae89f5SJiri Olsa int err;
328399b403d2SJiri Olsa
328499b403d2SJiri Olsa if (link->task && !same_thread_group(current, link->task))
328599b403d2SJiri Olsa return 0;
328699b403d2SJiri Olsa
328789ae89f5SJiri Olsa if (sleepable)
32880b779b61SJiri Olsa rcu_read_lock_trace();
328989ae89f5SJiri Olsa else
329089ae89f5SJiri Olsa rcu_read_lock();
329166c84731SAndrii Nakryiko
329289ae89f5SJiri Olsa migrate_disable();
329399b403d2SJiri Olsa
329489ae89f5SJiri Olsa old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx);
3295900f362eSJiri Olsa err = bpf_prog_run(link->link.prog, regs);
3296b733eeadSJiri Olsa bpf_reset_run_ctx(old_run_ctx);
3297b733eeadSJiri Olsa
329889ae89f5SJiri Olsa migrate_enable();
329989ae89f5SJiri Olsa
330089ae89f5SJiri Olsa if (sleepable)
330189ae89f5SJiri Olsa rcu_read_unlock_trace();
330289ae89f5SJiri Olsa else
330389ae89f5SJiri Olsa rcu_read_unlock();
330489ae89f5SJiri Olsa return err;
330599b403d2SJiri Olsa }
330699b403d2SJiri Olsa
330789ae89f5SJiri Olsa static bool
uprobe_multi_link_filter(struct uprobe_consumer * con,struct mm_struct * mm)330889ae89f5SJiri Olsa uprobe_multi_link_filter(struct uprobe_consumer *con, struct mm_struct *mm)
330989ae89f5SJiri Olsa {
331089ae89f5SJiri Olsa struct bpf_uprobe *uprobe;
331189ae89f5SJiri Olsa
331289ae89f5SJiri Olsa uprobe = container_of(con, struct bpf_uprobe, consumer);
331389ae89f5SJiri Olsa return uprobe->link->task->mm == mm;
331489ae89f5SJiri Olsa }
331599b403d2SJiri Olsa
331689ae89f5SJiri Olsa static int
uprobe_multi_link_handler(struct uprobe_consumer * con,struct pt_regs * regs,__u64 * data)331789ae89f5SJiri Olsa uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs,
3318b733eeadSJiri Olsa __u64 *data)
331959da880aSAndrii Nakryiko {
3320b733eeadSJiri Olsa struct bpf_uprobe *uprobe;
3321b733eeadSJiri Olsa int ret;
3322b733eeadSJiri Olsa
3323b733eeadSJiri Olsa uprobe = container_of(con, struct bpf_uprobe, consumer);
3324b733eeadSJiri Olsa ret = uprobe_prog_run(uprobe, instruction_pointer(regs), regs, false, data);
3325b733eeadSJiri Olsa if (uprobe->session)
3326b733eeadSJiri Olsa return ret ? UPROBE_HANDLER_IGNORE : 0;
332789ae89f5SJiri Olsa return 0;
3328da09a9e0SJiri Olsa }
3329da09a9e0SJiri Olsa
333089ae89f5SJiri Olsa static int
uprobe_multi_link_ret_handler(struct uprobe_consumer * con,unsigned long func,struct pt_regs * regs,__u64 * data)333189ae89f5SJiri Olsa uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs,
3332d920179bSJiri Olsa __u64 *data)
333389ae89f5SJiri Olsa {
333489ae89f5SJiri Olsa struct bpf_uprobe *uprobe;
333599b403d2SJiri Olsa
3336d920179bSJiri Olsa uprobe = container_of(con, struct bpf_uprobe, consumer);
3337d920179bSJiri Olsa uprobe_prog_run(uprobe, func, regs, true, data);
3338d920179bSJiri Olsa return 0;
333989ae89f5SJiri Olsa }
334089ae89f5SJiri Olsa
bpf_uprobe_multi_entry_ip(struct bpf_run_ctx * ctx)334189ae89f5SJiri Olsa static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3342da09a9e0SJiri Olsa {
3343da09a9e0SJiri Olsa struct bpf_uprobe_multi_run_ctx *run_ctx;
334489ae89f5SJiri Olsa
334589ae89f5SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx,
334689ae89f5SJiri Olsa session_ctx.run_ctx);
334789ae89f5SJiri Olsa return run_ctx->entry_ip;
334899b403d2SJiri Olsa }
3349d920179bSJiri Olsa
bpf_uprobe_multi_cookie(struct bpf_run_ctx * ctx)335089ae89f5SJiri Olsa static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
335189ae89f5SJiri Olsa {
3352686328d8SJiri Olsa struct bpf_uprobe_multi_run_ctx *run_ctx;
3353686328d8SJiri Olsa
3354686328d8SJiri Olsa run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx,
3355686328d8SJiri Olsa session_ctx.run_ctx);
335699b403d2SJiri Olsa return run_ctx->uprobe->cookie;
335799b403d2SJiri Olsa }
3358686328d8SJiri Olsa
bpf_uprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)3359686328d8SJiri Olsa int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3360686328d8SJiri Olsa {
33610b779b61SJiri Olsa struct bpf_uprobe_multi_link *link = NULL;
33620b779b61SJiri Olsa unsigned long __user *uref_ctr_offsets;
33630b779b61SJiri Olsa struct bpf_link_primer link_primer;
33640b779b61SJiri Olsa struct bpf_uprobe *uprobes = NULL;
336599b403d2SJiri Olsa struct task_struct *task = NULL;
336699b403d2SJiri Olsa unsigned long __user *uoffsets;
33670b779b61SJiri Olsa u64 __user *ucookies;
33680b779b61SJiri Olsa void __user *upath;
33690b779b61SJiri Olsa u32 flags, cnt, i;
337089ae89f5SJiri Olsa struct path path;
337189ae89f5SJiri Olsa char *name;
337289ae89f5SJiri Olsa pid_t pid;
337389ae89f5SJiri Olsa int err;
337489ae89f5SJiri Olsa
337589ae89f5SJiri Olsa /* no support for 32bit archs yet */
3376b733eeadSJiri Olsa if (sizeof(u64) != sizeof(void *))
337789ae89f5SJiri Olsa return -EOPNOTSUPP;
33780b779b61SJiri Olsa
337989ae89f5SJiri Olsa if (!is_uprobe_multi(prog))
338089ae89f5SJiri Olsa return -EINVAL;
338189ae89f5SJiri Olsa
338289ae89f5SJiri Olsa flags = attr->link_create.uprobe_multi.flags;
3383b733eeadSJiri Olsa if (flags & ~BPF_F_UPROBE_MULTI_RETURN)
338489ae89f5SJiri Olsa return -EINVAL;
338589ae89f5SJiri Olsa
338689ae89f5SJiri Olsa /*
338789ae89f5SJiri Olsa * path, offsets and cnt are mandatory,
338889ae89f5SJiri Olsa * ref_ctr_offsets and cookies are optional
338989ae89f5SJiri Olsa */
3390d920179bSJiri Olsa upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path);
339189ae89f5SJiri Olsa uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets);
339289ae89f5SJiri Olsa cnt = attr->link_create.uprobe_multi.cnt;
339389ae89f5SJiri Olsa pid = attr->link_create.uprobe_multi.pid;
339489ae89f5SJiri Olsa
339589ae89f5SJiri Olsa if (!upath || !uoffsets || !cnt || pid < 0)
339689ae89f5SJiri Olsa return -EINVAL;
339789ae89f5SJiri Olsa if (cnt > MAX_UPROBE_MULTI_CNT)
339889ae89f5SJiri Olsa return -E2BIG;
33990b779b61SJiri Olsa
340089ae89f5SJiri Olsa uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets);
340189ae89f5SJiri Olsa ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies);
340289ae89f5SJiri Olsa
340389ae89f5SJiri Olsa name = strndup_user(upath, PATH_MAX);
340446ba0e49SAndrii Nakryiko if (IS_ERR(name)) {
340589ae89f5SJiri Olsa err = PTR_ERR(name);
340646ba0e49SAndrii Nakryiko return err;
340789ae89f5SJiri Olsa }
34088b2efe51SHou Tao
34098b2efe51SHou Tao err = kern_path(name, LOOKUP_FOLLOW, &path);
341089ae89f5SJiri Olsa kfree(name);
341189ae89f5SJiri Olsa if (err)
34120b779b61SJiri Olsa return err;
341389ae89f5SJiri Olsa
341489ae89f5SJiri Olsa if (!d_is_reg(path.dentry)) {
341589ae89f5SJiri Olsa err = -EBADF;
341689ae89f5SJiri Olsa goto error_path_put;
341789ae89f5SJiri Olsa }
341889ae89f5SJiri Olsa
341989ae89f5SJiri Olsa if (pid) {
342089ae89f5SJiri Olsa task = get_pid_task(find_vpid(pid), PIDTYPE_TGID);
342189ae89f5SJiri Olsa if (!task) {
342289ae89f5SJiri Olsa err = -ESRCH;
342389ae89f5SJiri Olsa goto error_path_put;
342489ae89f5SJiri Olsa }
342589ae89f5SJiri Olsa }
342689ae89f5SJiri Olsa
342789ae89f5SJiri Olsa err = -ENOMEM;
342889ae89f5SJiri Olsa
342989ae89f5SJiri Olsa link = kzalloc(sizeof(*link), GFP_KERNEL);
3430b733eeadSJiri Olsa uprobes = kvcalloc(cnt, sizeof(*uprobes), GFP_KERNEL);
343146ba0e49SAndrii Nakryiko
343257eb5e1cSJiri Olsa if (!uprobes || !link)
343357eb5e1cSJiri Olsa goto error_free;
3434b733eeadSJiri Olsa
3435b733eeadSJiri Olsa for (i = 0; i < cnt; i++) {
343657eb5e1cSJiri Olsa if (__get_user(uprobes[i].offset, uoffsets + i)) {
3437b733eeadSJiri Olsa err = -EFAULT;
343889ae89f5SJiri Olsa goto error_free;
343989ae89f5SJiri Olsa }
344089ae89f5SJiri Olsa if (uprobes[i].offset < 0) {
344189ae89f5SJiri Olsa err = -EINVAL;
344289ae89f5SJiri Olsa goto error_free;
344389ae89f5SJiri Olsa }
344489ae89f5SJiri Olsa if (uref_ctr_offsets && __get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) {
344589ae89f5SJiri Olsa err = -EFAULT;
344689ae89f5SJiri Olsa goto error_free;
34473983c002SJiri Olsa }
34480b779b61SJiri Olsa if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) {
34490b779b61SJiri Olsa err = -EFAULT;
34500b779b61SJiri Olsa goto error_free;
34513983c002SJiri Olsa }
34523983c002SJiri Olsa
34533983c002SJiri Olsa uprobes[i].link = link;
34543983c002SJiri Olsa
34554930b7f5SJiri Olsa if (!(flags & BPF_F_UPROBE_MULTI_RETURN))
345689ae89f5SJiri Olsa uprobes[i].consumer.handler = uprobe_multi_link_handler;
345789ae89f5SJiri Olsa if (flags & BPF_F_UPROBE_MULTI_RETURN || is_uprobe_session(prog))
345889ae89f5SJiri Olsa uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler;
34593983c002SJiri Olsa if (is_uprobe_session(prog))
346089ae89f5SJiri Olsa uprobes[i].session = true;
346189ae89f5SJiri Olsa if (pid)
346289ae89f5SJiri Olsa uprobes[i].consumer.filter = uprobe_multi_link_filter;
346389ae89f5SJiri Olsa }
346489ae89f5SJiri Olsa
346589ae89f5SJiri Olsa link->cnt = cnt;
3466d920179bSJiri Olsa link->uprobes = uprobes;
346789ae89f5SJiri Olsa link->path = path;
3468d920179bSJiri Olsa link->task = task;
3469d920179bSJiri Olsa link->flags = flags;
3470d920179bSJiri Olsa
3471d920179bSJiri Olsa bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI,
3472b733eeadSJiri Olsa &bpf_uprobe_multi_link_lops, prog);
3473b733eeadSJiri Olsa
347489ae89f5SJiri Olsa for (i = 0; i < cnt; i++) {
347589ae89f5SJiri Olsa uprobes[i].uprobe = uprobe_register(d_real_inode(link->path.dentry),
347689ae89f5SJiri Olsa uprobes[i].offset,
347789ae89f5SJiri Olsa uprobes[i].ref_ctr_offset,
347889ae89f5SJiri Olsa &uprobes[i].consumer);
3479b733eeadSJiri Olsa if (IS_ERR(uprobes[i].uprobe)) {
3480e56fdbfbSJiri Olsa err = PTR_ERR(uprobes[i].uprobe);
348189ae89f5SJiri Olsa link->cnt = i;
348289ae89f5SJiri Olsa goto error_unregister;
348389ae89f5SJiri Olsa }
348489ae89f5SJiri Olsa }
348589ae89f5SJiri Olsa
34863c83a9adSOleg Nesterov err = bpf_link_prime(&link->link, &link_primer);
348789ae89f5SJiri Olsa if (err)
34884930b7f5SJiri Olsa goto error_unregister;
348989ae89f5SJiri Olsa
34903c83a9adSOleg Nesterov return bpf_link_settle(&link_primer);
34913c83a9adSOleg Nesterov
34925fe6e308SOleg Nesterov error_unregister:
34935fe6e308SOleg Nesterov bpf_uprobe_unregister(uprobes, link->cnt);
349489ae89f5SJiri Olsa
349589ae89f5SJiri Olsa error_free:
349689ae89f5SJiri Olsa kvfree(uprobes);
349789ae89f5SJiri Olsa kfree(link);
349889ae89f5SJiri Olsa if (task)
34995fe6e308SOleg Nesterov put_task_struct(task);
350089ae89f5SJiri Olsa error_path_put:
350189ae89f5SJiri Olsa path_put(&path);
350289ae89f5SJiri Olsa return err;
35035fe6e308SOleg Nesterov }
35045fe6e308SOleg Nesterov #else /* !CONFIG_UPROBES */
bpf_uprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)35055fe6e308SOleg Nesterov int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
350689ae89f5SJiri Olsa {
350789ae89f5SJiri Olsa return -EOPNOTSUPP;
350889ae89f5SJiri Olsa }
bpf_uprobe_multi_cookie(struct bpf_run_ctx * ctx)3509b733eeadSJiri Olsa static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
3510b733eeadSJiri Olsa {
351189ae89f5SJiri Olsa return 0;
351289ae89f5SJiri Olsa }
bpf_uprobe_multi_entry_ip(struct bpf_run_ctx * ctx)351389ae89f5SJiri Olsa static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
351489ae89f5SJiri Olsa {
351589ae89f5SJiri Olsa return 0;
351689ae89f5SJiri Olsa }
351789ae89f5SJiri Olsa #endif /* CONFIG_UPROBES */
351889ae89f5SJiri Olsa
351989ae89f5SJiri Olsa __bpf_kfunc_start_defs();
35200b779b61SJiri Olsa
bpf_session_is_return(void)35210b779b61SJiri Olsa __bpf_kfunc bool bpf_session_is_return(void)
35220b779b61SJiri Olsa {
35230b779b61SJiri Olsa struct bpf_session_run_ctx *session_ctx;
3524686328d8SJiri Olsa
3525686328d8SJiri Olsa session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx);
3526686328d8SJiri Olsa return session_ctx->is_return;
3527686328d8SJiri Olsa }
352889ae89f5SJiri Olsa
bpf_session_cookie(void)3529adf46d88SJiri Olsa __bpf_kfunc __u64 *bpf_session_cookie(void)
3530adf46d88SJiri Olsa {
3531adf46d88SJiri Olsa struct bpf_session_run_ctx *session_ctx;
3532adf46d88SJiri Olsa
3533adf46d88SJiri Olsa session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx);
3534adf46d88SJiri Olsa return session_ctx->data;
3535adf46d88SJiri Olsa }
3536adf46d88SJiri Olsa
3537adf46d88SJiri Olsa __bpf_kfunc_end_defs();
3538adf46d88SJiri Olsa
3539adf46d88SJiri Olsa BTF_KFUNCS_START(kprobe_multi_kfunc_set_ids)
BTF_ID_FLAGS(func,bpf_session_is_return)3540717d6313SJiri Olsa BTF_ID_FLAGS(func, bpf_session_is_return)
35415c919aceSJiri Olsa BTF_ID_FLAGS(func, bpf_session_cookie)
35425c919aceSJiri Olsa BTF_KFUNCS_END(kprobe_multi_kfunc_set_ids)
35435c919aceSJiri Olsa
35445c919aceSJiri Olsa static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id)
35455c919aceSJiri Olsa {
35465c919aceSJiri Olsa if (!btf_id_set8_contains(&kprobe_multi_kfunc_set_ids, kfunc_id))
35475c919aceSJiri Olsa return 0;
3548adf46d88SJiri Olsa
3549adf46d88SJiri Olsa if (!is_kprobe_session(prog) && !is_uprobe_session(prog))
3550adf46d88SJiri Olsa return -EACCES;
3551adf46d88SJiri Olsa
35525c919aceSJiri Olsa return 0;
3553adf46d88SJiri Olsa }
3554adf46d88SJiri Olsa
3555adf46d88SJiri Olsa static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = {
3556adf46d88SJiri Olsa .owner = THIS_MODULE,
3557adf46d88SJiri Olsa .set = &kprobe_multi_kfunc_set_ids,
3558adf46d88SJiri Olsa .filter = bpf_kprobe_multi_filter,
3559adf46d88SJiri Olsa };
356099b403d2SJiri Olsa
bpf_kprobe_multi_kfuncs_init(void)3561adf46d88SJiri Olsa static int __init bpf_kprobe_multi_kfuncs_init(void)
3562adf46d88SJiri Olsa {
3563adf46d88SJiri Olsa return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set);
3564adf46d88SJiri Olsa }
3565adf46d88SJiri Olsa
3566adf46d88SJiri Olsa late_initcall(bpf_kprobe_multi_kfuncs_init);
3567adf46d88SJiri Olsa
3568adf46d88SJiri Olsa __bpf_kfunc_start_defs();
3569adf46d88SJiri Olsa
bpf_send_signal_task(struct task_struct * task,int sig,enum pid_type type,u64 value)3570adf46d88SJiri Olsa __bpf_kfunc int bpf_send_signal_task(struct task_struct *task, int sig, enum pid_type type,
3571adf46d88SJiri Olsa u64 value)
3572adf46d88SJiri Olsa {
3573adf46d88SJiri Olsa if (type != PIDTYPE_PID && type != PIDTYPE_TGID)
3574adf46d88SJiri Olsa return -EINVAL;
3575adf46d88SJiri Olsa
3576adf46d88SJiri Olsa return bpf_send_signal_common(sig, type, task, value);
3577adf46d88SJiri Olsa }
35786280cf71SPuranjay Mohan
35796280cf71SPuranjay Mohan __bpf_kfunc_end_defs();
35806280cf71SPuranjay Mohan