blob: 0b9e4fd9c61b0c74ff97f4de3cef439692cddfff [file] [log] [blame]
Steven Rostedt (VMware)179a0cc2018-08-16 11:20:54 -04001// SPDX-License-Identifier: GPL-2.0
Alexei Starovoitov25415172015-03-25 12:49:20 -07002/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
Alexei Starovoitov0515e592016-09-01 18:37:22 -07003 * Copyright (c) 2016 Facebook
Alexei Starovoitov25415172015-03-25 12:49:20 -07004 */
5#include <linux/kernel.h>
6#include <linux/types.h>
7#include <linux/slab.h>
8#include <linux/bpf.h>
Alexei Starovoitov0515e592016-09-01 18:37:22 -07009#include <linux/bpf_perf_event.h>
Alan Maguirec4d0bfb2020-09-28 12:31:05 +010010#include <linux/btf.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070011#include <linux/filter.h>
12#include <linux/uaccess.h>
Alexei Starovoitov9c959c82015-03-25 12:49:22 -070013#include <linux/ctype.h>
Josef Bacik9802d862017-12-11 11:36:48 -050014#include <linux/kprobes.h>
Alan Maguireac5a72e2020-07-13 12:52:33 +010015#include <linux/spinlock.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070016#include <linux/syscalls.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090017#include <linux/error-injection.h>
Jiri Olsac9a0f3b2020-07-11 23:53:24 +020018#include <linux/btf_ids.h>
KP Singh6f100642020-11-13 00:59:30 +000019#include <linux/bpf_lsm.h>
20
Martin KaFai Lau8e4597c2020-11-12 13:13:13 -080021#include <net/bpf_sk_storage.h>
Josef Bacik9802d862017-12-11 11:36:48 -050022
Alan Maguirec4d0bfb2020-09-28 12:31:05 +010023#include <uapi/linux/bpf.h>
24#include <uapi/linux/btf.h>
25
Nadav Amitc7b6f292019-04-25 17:11:43 -070026#include <asm/tlb.h>
27
Josef Bacik9802d862017-12-11 11:36:48 -050028#include "trace_probe.h"
Alexei Starovoitov25415172015-03-25 12:49:20 -070029#include "trace.h"
30
Alan Maguireac5a72e2020-07-13 12:52:33 +010031#define CREATE_TRACE_POINTS
32#include "bpf_trace.h"
33
Stanislav Fomicheve672db02019-05-28 14:14:44 -070034#define bpf_event_rcu_dereference(p) \
35 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
36
Matt Mullinsa38d1102018-12-12 16:42:37 -080037#ifdef CONFIG_MODULES
38struct bpf_trace_module {
39 struct module *module;
40 struct list_head list;
41};
42
43static LIST_HEAD(bpf_trace_modules);
44static DEFINE_MUTEX(bpf_module_mutex);
45
46static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
47{
48 struct bpf_raw_event_map *btp, *ret = NULL;
49 struct bpf_trace_module *btm;
50 unsigned int i;
51
52 mutex_lock(&bpf_module_mutex);
53 list_for_each_entry(btm, &bpf_trace_modules, list) {
54 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
55 btp = &btm->module->bpf_raw_events[i];
56 if (!strcmp(btp->tp->name, name)) {
57 if (try_module_get(btm->module))
58 ret = btp;
59 goto out;
60 }
61 }
62 }
63out:
64 mutex_unlock(&bpf_module_mutex);
65 return ret;
66}
67#else
68static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
69{
70 return NULL;
71}
72#endif /* CONFIG_MODULES */
73
Gianluca Borello035226b2017-10-26 01:47:42 +000074u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Yonghong Songc195651e2018-04-28 22:28:08 -070075u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Gianluca Borello035226b2017-10-26 01:47:42 +000076
Alan Maguireeb411372020-09-28 12:31:09 +010077static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
78 u64 flags, const struct btf **btf,
79 s32 *btf_id);
80
Alexei Starovoitov25415172015-03-25 12:49:20 -070081/**
82 * trace_call_bpf - invoke BPF program
Yonghong Songe87c6bc2017-10-23 23:53:08 -070083 * @call: tracepoint event
Alexei Starovoitov25415172015-03-25 12:49:20 -070084 * @ctx: opaque context pointer
85 *
86 * kprobe handlers execute BPF programs via this helper.
87 * Can be used from static tracepoints in the future.
88 *
89 * Return: BPF programs always return an integer which is interpreted by
90 * kprobe handler as:
91 * 0 - return from kprobe (event is filtered out)
92 * 1 - store kprobe event into ring buffer
93 * Other values are reserved and currently alias to 1
94 */
Yonghong Songe87c6bc2017-10-23 23:53:08 -070095unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
Alexei Starovoitov25415172015-03-25 12:49:20 -070096{
97 unsigned int ret;
98
99 if (in_nmi()) /* not supported yet */
100 return 1;
101
Thomas Gleixnerb0a81b92020-02-24 15:01:37 +0100102 cant_sleep();
Alexei Starovoitov25415172015-03-25 12:49:20 -0700103
104 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
105 /*
106 * since some bpf program is already running on this cpu,
107 * don't call into another bpf program (same or different)
108 * and don't send kprobe event into ring-buffer,
109 * so return zero here
110 */
111 ret = 0;
112 goto out;
113 }
114
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700115 /*
116 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
117 * to all call sites, we did a bpf_prog_array_valid() there to check
118 * whether call->prog_array is empty or not, which is
Qiujun Huang2b5894c2020-10-29 23:05:54 +0800119 * a heuristic to speed up execution.
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700120 *
121 * If bpf_prog_array_valid() fetched prog_array was
122 * non-NULL, we go into trace_call_bpf() and do the actual
123 * proper rcu_dereference() under RCU lock.
124 * If it turns out that prog_array is NULL then, we bail out.
125 * For the opposite, if the bpf_prog_array_valid() fetched pointer
126 * was NULL, you'll skip the prog_array with the risk of missing
127 * out of events when it was updated in between this and the
128 * rcu_dereference() which is accepted risk.
129 */
130 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700131
132 out:
133 __this_cpu_dec(bpf_prog_active);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700134
135 return ret;
136}
Alexei Starovoitov25415172015-03-25 12:49:20 -0700137
Josef Bacik9802d862017-12-11 11:36:48 -0500138#ifdef CONFIG_BPF_KPROBE_OVERRIDE
139BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
140{
Josef Bacik9802d862017-12-11 11:36:48 -0500141 regs_set_return_value(regs, rc);
Masami Hiramatsu540adea2018-01-13 02:55:03 +0900142 override_function_with_return(regs);
Josef Bacik9802d862017-12-11 11:36:48 -0500143 return 0;
144}
145
146static const struct bpf_func_proto bpf_override_return_proto = {
147 .func = bpf_override_return,
148 .gpl_only = true,
149 .ret_type = RET_INTEGER,
150 .arg1_type = ARG_PTR_TO_CTX,
151 .arg2_type = ARG_ANYTHING,
152};
153#endif
154
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700155static __always_inline int
156bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
157{
158 int ret;
159
Christoph Hellwigc0ee37e2020-06-17 09:37:54 +0200160 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700161 if (unlikely(ret < 0))
162 memset(dst, 0, size);
163 return ret;
164}
165
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100166BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
167 const void __user *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700168{
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700169 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700170}
171
John Fastabendf4703782020-05-24 09:50:55 -0700172const struct bpf_func_proto bpf_probe_read_user_proto = {
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100173 .func = bpf_probe_read_user,
174 .gpl_only = true,
175 .ret_type = RET_INTEGER,
176 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
177 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
178 .arg3_type = ARG_ANYTHING,
179};
180
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700181static __always_inline int
182bpf_probe_read_user_str_common(void *dst, u32 size,
183 const void __user *unsafe_ptr)
184{
185 int ret;
186
Daniel Xu6fa6d282020-11-17 12:05:45 -0800187 /*
188 * NB: We rely on strncpy_from_user() not copying junk past the NUL
189 * terminator into `dst`.
190 *
191 * strncpy_from_user() does long-sized strides in the fast path. If the
192 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
193 * then there could be junk after the NUL in `dst`. If user takes `dst`
194 * and keys a hash map with it, then semantically identical strings can
195 * occupy multiple entries in the map.
196 */
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700197 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
198 if (unlikely(ret < 0))
199 memset(dst, 0, size);
200 return ret;
201}
202
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100203BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
204 const void __user *, unsafe_ptr)
205{
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700206 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100207}
208
John Fastabendf4703782020-05-24 09:50:55 -0700209const struct bpf_func_proto bpf_probe_read_user_str_proto = {
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100210 .func = bpf_probe_read_user_str,
211 .gpl_only = true,
212 .ret_type = RET_INTEGER,
213 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
214 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
215 .arg3_type = ARG_ANYTHING,
216};
217
218static __always_inline int
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700219bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100220{
221 int ret = security_locked_down(LOCKDOWN_BPF_READ);
222
223 if (unlikely(ret < 0))
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700224 goto fail;
Christoph Hellwigfe557312020-06-17 09:37:53 +0200225 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100226 if (unlikely(ret < 0))
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700227 goto fail;
228 return ret;
229fail:
230 memset(dst, 0, size);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100231 return ret;
232}
233
234BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
235 const void *, unsafe_ptr)
236{
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700237 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100238}
239
John Fastabendf4703782020-05-24 09:50:55 -0700240const struct bpf_func_proto bpf_probe_read_kernel_proto = {
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100241 .func = bpf_probe_read_kernel,
242 .gpl_only = true,
243 .ret_type = RET_INTEGER,
244 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
245 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
246 .arg3_type = ARG_ANYTHING,
247};
248
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100249static __always_inline int
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700250bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100251{
252 int ret = security_locked_down(LOCKDOWN_BPF_READ);
253
254 if (unlikely(ret < 0))
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700255 goto fail;
256
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100257 /*
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700258 * The strncpy_from_kernel_nofault() call will likely not fill the
259 * entire buffer, but that's okay in this circumstance as we're probing
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100260 * arbitrary memory anyway similar to bpf_probe_read_*() and might
261 * as well probe the stack. Thus, memory is explicitly cleared
262 * only in error case, so that improper users ignoring return
263 * code altogether don't copy garbage; otherwise length of string
264 * is returned that can be used for bpf_perf_event_output() et al.
265 */
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700266 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100267 if (unlikely(ret < 0))
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700268 goto fail;
269
Andrii Nakryiko02553b92020-06-15 22:04:30 -0700270 return ret;
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700271fail:
272 memset(dst, 0, size);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100273 return ret;
274}
275
276BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
277 const void *, unsafe_ptr)
278{
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700279 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100280}
281
John Fastabendf4703782020-05-24 09:50:55 -0700282const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100283 .func = bpf_probe_read_kernel_str,
284 .gpl_only = true,
285 .ret_type = RET_INTEGER,
286 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
287 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
288 .arg3_type = ARG_ANYTHING,
289};
290
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700291#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
292BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
293 const void *, unsafe_ptr)
294{
295 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
296 return bpf_probe_read_user_common(dst, size,
297 (__force void __user *)unsafe_ptr);
298 }
299 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
300}
301
302static const struct bpf_func_proto bpf_probe_read_compat_proto = {
303 .func = bpf_probe_read_compat,
304 .gpl_only = true,
305 .ret_type = RET_INTEGER,
306 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
307 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
308 .arg3_type = ARG_ANYTHING,
309};
310
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100311BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
312 const void *, unsafe_ptr)
313{
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700314 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
315 return bpf_probe_read_user_str_common(dst, size,
316 (__force void __user *)unsafe_ptr);
317 }
318 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100319}
320
321static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
322 .func = bpf_probe_read_compat_str,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700323 .gpl_only = true,
324 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800325 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Yonghong Song9c019e22017-11-12 14:49:10 -0800326 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700327 .arg3_type = ARG_ANYTHING,
328};
Christoph Hellwig8d92db52020-06-08 21:34:40 -0700329#endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700330
Daniel Borkmanneb1b6682019-11-02 00:17:58 +0100331BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200332 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700333{
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700334 /*
335 * Ensure we're in user context which is safe for the helper to
336 * run. This helper has no business in a kthread.
337 *
338 * access_ok() should prevent writing to non-user memory, but in
339 * some situations (nommu, temporary switch, etc) access_ok() does
340 * not provide enough validation, hence the check on KERNEL_DS.
Nadav Amitc7b6f292019-04-25 17:11:43 -0700341 *
342 * nmi_uaccess_okay() ensures the probe is not run in an interim
343 * state, when the task or mm are switched. This is specifically
344 * required to prevent the use of temporary mm.
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700345 */
346
347 if (unlikely(in_interrupt() ||
348 current->flags & (PF_KTHREAD | PF_EXITING)))
349 return -EPERM;
Al Virodb68ce12017-03-20 21:08:07 -0400350 if (unlikely(uaccess_kernel()))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700351 return -EPERM;
Nadav Amitc7b6f292019-04-25 17:11:43 -0700352 if (unlikely(!nmi_uaccess_okay()))
353 return -EPERM;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700354
Christoph Hellwigc0ee37e2020-06-17 09:37:54 +0200355 return copy_to_user_nofault(unsafe_ptr, src, size);
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700356}
357
358static const struct bpf_func_proto bpf_probe_write_user_proto = {
359 .func = bpf_probe_write_user,
360 .gpl_only = true,
361 .ret_type = RET_INTEGER,
362 .arg1_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800363 .arg2_type = ARG_PTR_TO_MEM,
364 .arg3_type = ARG_CONST_SIZE,
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700365};
366
367static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
368{
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -0700369 if (!capable(CAP_SYS_ADMIN))
370 return NULL;
371
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700372 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
373 current->comm, task_pid_nr(current));
374
375 return &bpf_probe_write_user_proto;
376}
377
Christoph Hellwigd7b29772020-06-08 21:34:30 -0700378static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
379 size_t bufsz)
380{
381 void __user *user_ptr = (__force void __user *)unsafe_ptr;
382
383 buf[0] = 0;
384
385 switch (fmt_ptype) {
386 case 's':
387#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
Christoph Hellwigaec6ce52020-06-08 21:34:33 -0700388 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
389 strncpy_from_user_nofault(buf, user_ptr, bufsz);
390 break;
391 }
392 fallthrough;
Christoph Hellwigd7b29772020-06-08 21:34:30 -0700393#endif
394 case 'k':
395 strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
396 break;
397 case 'u':
398 strncpy_from_user_nofault(buf, user_ptr, bufsz);
399 break;
400 }
401}
402
Alan Maguireac5a72e2020-07-13 12:52:33 +0100403static DEFINE_RAW_SPINLOCK(trace_printk_lock);
404
405#define BPF_TRACE_PRINTK_SIZE 1024
406
Stanislav Fomichev0d360d62020-08-06 11:26:12 -0700407static __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...)
Alan Maguireac5a72e2020-07-13 12:52:33 +0100408{
409 static char buf[BPF_TRACE_PRINTK_SIZE];
410 unsigned long flags;
411 va_list ap;
412 int ret;
413
414 raw_spin_lock_irqsave(&trace_printk_lock, flags);
415 va_start(ap, fmt);
416 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
417 va_end(ap);
418 /* vsnprintf() will not append null for zero-length strings */
419 if (ret == 0)
420 buf[0] = '\0';
421 trace_bpf_trace_printk(buf);
422 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
423
424 return ret;
425}
426
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700427/*
John Fastabend7bda4b42017-07-02 02:13:29 +0200428 * Only limited trace_printk() conversion specifiers allowed:
Song Liu2df6bb542020-06-29 23:28:45 -0700429 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pB %pks %pus %s
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700430 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200431BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
432 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700433{
Daniel Borkmannb2a52122020-05-15 12:11:18 +0200434 int i, mod[3] = {}, fmt_cnt = 0;
435 char buf[64], fmt_ptype;
436 void *unsafe_ptr = NULL;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700437 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700438
439 /*
440 * bpf_check()->check_func_arg()->check_stack_boundary()
441 * guarantees that fmt points to bpf program stack,
442 * fmt_size bytes of it were initialized and fmt_size > 0
443 */
444 if (fmt[--fmt_size] != 0)
445 return -EINVAL;
446
447 /* check format string for allowed specifiers */
448 for (i = 0; i < fmt_size; i++) {
449 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
450 return -EINVAL;
451
452 if (fmt[i] != '%')
453 continue;
454
455 if (fmt_cnt >= 3)
456 return -EINVAL;
457
458 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
459 i++;
460 if (fmt[i] == 'l') {
461 mod[fmt_cnt]++;
462 i++;
Daniel Borkmannb2a52122020-05-15 12:11:18 +0200463 } else if (fmt[i] == 'p') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700464 mod[fmt_cnt]++;
Daniel Borkmannb2a52122020-05-15 12:11:18 +0200465 if ((fmt[i + 1] == 'k' ||
466 fmt[i + 1] == 'u') &&
467 fmt[i + 2] == 's') {
468 fmt_ptype = fmt[i + 1];
469 i += 2;
470 goto fmt_str;
471 }
472
Song Liu2df6bb542020-06-29 23:28:45 -0700473 if (fmt[i + 1] == 'B') {
474 i++;
475 goto fmt_next;
476 }
477
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100478 /* disallow any further format extensions */
479 if (fmt[i + 1] != 0 &&
480 !isspace(fmt[i + 1]) &&
481 !ispunct(fmt[i + 1]))
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700482 return -EINVAL;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700483
Daniel Borkmannb2a52122020-05-15 12:11:18 +0200484 goto fmt_next;
485 } else if (fmt[i] == 's') {
486 mod[fmt_cnt]++;
487 fmt_ptype = fmt[i];
488fmt_str:
489 if (str_seen)
490 /* allow only one '%s' per fmt string */
491 return -EINVAL;
492 str_seen = true;
493
494 if (fmt[i + 1] != 0 &&
495 !isspace(fmt[i + 1]) &&
496 !ispunct(fmt[i + 1]))
497 return -EINVAL;
498
499 switch (fmt_cnt) {
500 case 0:
501 unsafe_ptr = (void *)(long)arg1;
502 arg1 = (long)buf;
503 break;
504 case 1:
505 unsafe_ptr = (void *)(long)arg2;
506 arg2 = (long)buf;
507 break;
508 case 2:
509 unsafe_ptr = (void *)(long)arg3;
510 arg3 = (long)buf;
511 break;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700512 }
Daniel Borkmannb2a52122020-05-15 12:11:18 +0200513
Christoph Hellwigd7b29772020-06-08 21:34:30 -0700514 bpf_trace_copy_string(buf, unsafe_ptr, fmt_ptype,
515 sizeof(buf));
Daniel Borkmannb2a52122020-05-15 12:11:18 +0200516 goto fmt_next;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700517 }
518
519 if (fmt[i] == 'l') {
520 mod[fmt_cnt]++;
521 i++;
522 }
523
John Fastabend7bda4b42017-07-02 02:13:29 +0200524 if (fmt[i] != 'i' && fmt[i] != 'd' &&
525 fmt[i] != 'u' && fmt[i] != 'x')
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700526 return -EINVAL;
Daniel Borkmannb2a52122020-05-15 12:11:18 +0200527fmt_next:
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700528 fmt_cnt++;
529 }
530
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200531/* Horrid workaround for getting va_list handling working with different
532 * argument type combinations generically for 32 and 64 bit archs.
533 */
534#define __BPF_TP_EMIT() __BPF_ARG3_TP()
535#define __BPF_TP(...) \
Alan Maguireac5a72e2020-07-13 12:52:33 +0100536 bpf_do_trace_printk(fmt, ##__VA_ARGS__)
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200537
538#define __BPF_ARG1_TP(...) \
539 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
540 ? __BPF_TP(arg1, ##__VA_ARGS__) \
541 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
542 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
543 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
544
545#define __BPF_ARG2_TP(...) \
546 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
547 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
548 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
549 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
550 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
551
552#define __BPF_ARG3_TP(...) \
553 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
554 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
555 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
556 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
557 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
558
559 return __BPF_TP_EMIT();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700560}
561
562static const struct bpf_func_proto bpf_trace_printk_proto = {
563 .func = bpf_trace_printk,
564 .gpl_only = true,
565 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800566 .arg1_type = ARG_PTR_TO_MEM,
567 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700568};
569
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700570const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
571{
572 /*
Alan Maguireac5a72e2020-07-13 12:52:33 +0100573 * This program might be calling bpf_trace_printk,
574 * so enable the associated bpf_trace/bpf_trace_printk event.
575 * Repeat this each time as it is possible a user has
576 * disabled bpf_trace_printk events. By loading a program
577 * calling bpf_trace_printk() however the user has expressed
578 * the intent to see such events.
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700579 */
Alan Maguireac5a72e2020-07-13 12:52:33 +0100580 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
581 pr_warn_ratelimited("could not enable bpf_trace_printk events");
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700582
583 return &bpf_trace_printk_proto;
584}
585
Yonghong Song492e6392020-05-09 10:59:14 -0700586#define MAX_SEQ_PRINTF_VARARGS 12
587#define MAX_SEQ_PRINTF_MAX_MEMCPY 6
588#define MAX_SEQ_PRINTF_STR_LEN 128
589
590struct bpf_seq_printf_buf {
591 char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN];
592};
593static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf);
594static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used);
595
596BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
597 const void *, data, u32, data_len)
598{
599 int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0;
600 int i, buf_used, copy_size, num_args;
601 u64 params[MAX_SEQ_PRINTF_VARARGS];
602 struct bpf_seq_printf_buf *bufs;
603 const u64 *args = data;
604
605 buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used);
606 if (WARN_ON_ONCE(buf_used > 1)) {
607 err = -EBUSY;
608 goto out;
609 }
610
611 bufs = this_cpu_ptr(&bpf_seq_printf_buf);
612
613 /*
614 * bpf_check()->check_func_arg()->check_stack_boundary()
615 * guarantees that fmt points to bpf program stack,
616 * fmt_size bytes of it were initialized and fmt_size > 0
617 */
618 if (fmt[--fmt_size] != 0)
619 goto out;
620
621 if (data_len & 7)
622 goto out;
623
624 for (i = 0; i < fmt_size; i++) {
625 if (fmt[i] == '%') {
626 if (fmt[i + 1] == '%')
627 i++;
628 else if (!data || !data_len)
629 goto out;
630 }
631 }
632
633 num_args = data_len / 8;
634
635 /* check format string for allowed specifiers */
636 for (i = 0; i < fmt_size; i++) {
637 /* only printable ascii for now. */
638 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
639 err = -EINVAL;
640 goto out;
641 }
642
643 if (fmt[i] != '%')
644 continue;
645
646 if (fmt[i + 1] == '%') {
647 i++;
648 continue;
649 }
650
651 if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) {
652 err = -E2BIG;
653 goto out;
654 }
655
656 if (fmt_cnt >= num_args) {
657 err = -EINVAL;
658 goto out;
659 }
660
661 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
662 i++;
663
664 /* skip optional "[0 +-][num]" width formating field */
665 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
666 fmt[i] == ' ')
667 i++;
668 if (fmt[i] >= '1' && fmt[i] <= '9') {
669 i++;
670 while (fmt[i] >= '0' && fmt[i] <= '9')
671 i++;
672 }
673
674 if (fmt[i] == 's') {
Andrew Morton19c8d8a2020-06-08 21:34:37 -0700675 void *unsafe_ptr;
676
Yonghong Song492e6392020-05-09 10:59:14 -0700677 /* try our best to copy */
678 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
679 err = -E2BIG;
680 goto out;
681 }
682
Andrew Morton19c8d8a2020-06-08 21:34:37 -0700683 unsafe_ptr = (void *)(long)args[fmt_cnt];
684 err = strncpy_from_kernel_nofault(bufs->buf[memcpy_cnt],
685 unsafe_ptr, MAX_SEQ_PRINTF_STR_LEN);
Yonghong Song492e6392020-05-09 10:59:14 -0700686 if (err < 0)
687 bufs->buf[memcpy_cnt][0] = '\0';
688 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
689
690 fmt_cnt++;
691 memcpy_cnt++;
692 continue;
693 }
694
695 if (fmt[i] == 'p') {
696 if (fmt[i + 1] == 0 ||
697 fmt[i + 1] == 'K' ||
Song Liu2df6bb542020-06-29 23:28:45 -0700698 fmt[i + 1] == 'x' ||
699 fmt[i + 1] == 'B') {
Yonghong Song492e6392020-05-09 10:59:14 -0700700 /* just kernel pointers */
701 params[fmt_cnt] = args[fmt_cnt];
702 fmt_cnt++;
703 continue;
704 }
705
706 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
707 if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') {
708 err = -EINVAL;
709 goto out;
710 }
711 if (fmt[i + 2] != '4' && fmt[i + 2] != '6') {
712 err = -EINVAL;
713 goto out;
714 }
715
716 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
717 err = -E2BIG;
718 goto out;
719 }
720
721
722 copy_size = (fmt[i + 2] == '4') ? 4 : 16;
723
Christoph Hellwigfe557312020-06-17 09:37:53 +0200724 err = copy_from_kernel_nofault(bufs->buf[memcpy_cnt],
Yonghong Song492e6392020-05-09 10:59:14 -0700725 (void *) (long) args[fmt_cnt],
726 copy_size);
727 if (err < 0)
728 memset(bufs->buf[memcpy_cnt], 0, copy_size);
729 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
730
731 i += 2;
732 fmt_cnt++;
733 memcpy_cnt++;
734 continue;
735 }
736
737 if (fmt[i] == 'l') {
738 i++;
739 if (fmt[i] == 'l')
740 i++;
741 }
742
743 if (fmt[i] != 'i' && fmt[i] != 'd' &&
Yonghong Songc06b0222020-06-23 16:08:07 -0700744 fmt[i] != 'u' && fmt[i] != 'x' &&
745 fmt[i] != 'X') {
Yonghong Song492e6392020-05-09 10:59:14 -0700746 err = -EINVAL;
747 goto out;
748 }
749
750 params[fmt_cnt] = args[fmt_cnt];
751 fmt_cnt++;
752 }
753
754 /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give
755 * all of them to seq_printf().
756 */
757 seq_printf(m, fmt, params[0], params[1], params[2], params[3],
758 params[4], params[5], params[6], params[7], params[8],
759 params[9], params[10], params[11]);
760
761 err = seq_has_overflowed(m) ? -EOVERFLOW : 0;
762out:
763 this_cpu_dec(bpf_seq_printf_buf_used);
764 return err;
765}
766
Lorenz Bauer9436ef62020-09-21 13:12:20 +0100767BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
Jiri Olsac9a0f3b2020-07-11 23:53:24 +0200768
Yonghong Song492e6392020-05-09 10:59:14 -0700769static const struct bpf_func_proto bpf_seq_printf_proto = {
770 .func = bpf_seq_printf,
771 .gpl_only = true,
772 .ret_type = RET_INTEGER,
773 .arg1_type = ARG_PTR_TO_BTF_ID,
Lorenz Bauer9436ef62020-09-21 13:12:20 +0100774 .arg1_btf_id = &btf_seq_file_ids[0],
Yonghong Song492e6392020-05-09 10:59:14 -0700775 .arg2_type = ARG_PTR_TO_MEM,
776 .arg3_type = ARG_CONST_SIZE,
777 .arg4_type = ARG_PTR_TO_MEM_OR_NULL,
778 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Yonghong Song492e6392020-05-09 10:59:14 -0700779};
780
781BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
782{
783 return seq_write(m, data, len) ? -EOVERFLOW : 0;
784}
785
Yonghong Song492e6392020-05-09 10:59:14 -0700786static const struct bpf_func_proto bpf_seq_write_proto = {
787 .func = bpf_seq_write,
788 .gpl_only = true,
789 .ret_type = RET_INTEGER,
790 .arg1_type = ARG_PTR_TO_BTF_ID,
Lorenz Bauer9436ef62020-09-21 13:12:20 +0100791 .arg1_btf_id = &btf_seq_file_ids[0],
Yonghong Song492e6392020-05-09 10:59:14 -0700792 .arg2_type = ARG_PTR_TO_MEM,
793 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
Yonghong Song492e6392020-05-09 10:59:14 -0700794};
795
Alan Maguireeb411372020-09-28 12:31:09 +0100796BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
797 u32, btf_ptr_size, u64, flags)
798{
799 const struct btf *btf;
800 s32 btf_id;
801 int ret;
802
803 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
804 if (ret)
805 return ret;
806
807 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
808}
809
810static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
811 .func = bpf_seq_printf_btf,
812 .gpl_only = true,
813 .ret_type = RET_INTEGER,
814 .arg1_type = ARG_PTR_TO_BTF_ID,
815 .arg1_btf_id = &btf_seq_file_ids[0],
816 .arg2_type = ARG_PTR_TO_MEM,
817 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
818 .arg4_type = ARG_ANYTHING,
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700819};
820
Yonghong Song908432c2017-10-05 09:19:20 -0700821static __always_inline int
822get_map_perf_counter(struct bpf_map *map, u64 flags,
823 u64 *value, u64 *enabled, u64 *running)
Kaixu Xia35578d72015-08-06 07:02:35 +0000824{
Kaixu Xia35578d72015-08-06 07:02:35 +0000825 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200826 unsigned int cpu = smp_processor_id();
827 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200828 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000829
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200830 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
831 return -EINVAL;
832 if (index == BPF_F_CURRENT_CPU)
833 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000834 if (unlikely(index >= array->map.max_entries))
835 return -E2BIG;
836
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200837 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200838 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000839 return -ENOENT;
840
Yonghong Song908432c2017-10-05 09:19:20 -0700841 return perf_event_read_local(ee->event, value, enabled, running);
842}
843
844BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
845{
846 u64 value = 0;
847 int err;
848
849 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
Kaixu Xia35578d72015-08-06 07:02:35 +0000850 /*
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700851 * this api is ugly since we miss [-22..-2] range of valid
852 * counter values, but that's uapi
Kaixu Xia35578d72015-08-06 07:02:35 +0000853 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700854 if (err)
855 return err;
856 return value;
Kaixu Xia35578d72015-08-06 07:02:35 +0000857}
858
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700859static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000860 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700861 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000862 .ret_type = RET_INTEGER,
863 .arg1_type = ARG_CONST_MAP_PTR,
864 .arg2_type = ARG_ANYTHING,
865};
866
Yonghong Song908432c2017-10-05 09:19:20 -0700867BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
868 struct bpf_perf_event_value *, buf, u32, size)
869{
870 int err = -EINVAL;
871
872 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
873 goto clear;
874 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
875 &buf->running);
876 if (unlikely(err))
877 goto clear;
878 return 0;
879clear:
880 memset(buf, 0, size);
881 return err;
882}
883
884static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
885 .func = bpf_perf_event_read_value,
886 .gpl_only = true,
887 .ret_type = RET_INTEGER,
888 .arg1_type = ARG_CONST_MAP_PTR,
889 .arg2_type = ARG_ANYTHING,
890 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
891 .arg4_type = ARG_CONST_SIZE,
892};
893
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200894static __always_inline u64
895__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
Daniel Borkmann283ca522017-12-12 02:25:30 +0100896 u64 flags, struct perf_sample_data *sd)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700897{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700898 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200899 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200900 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200901 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700902 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700903
Daniel Borkmann1e337592016-04-18 21:01:23 +0200904 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200905 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700906 if (unlikely(index >= array->map.max_entries))
907 return -E2BIG;
908
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200909 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200910 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700911 return -ENOENT;
912
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200913 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700914 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
915 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
916 return -EINVAL;
917
Daniel Borkmannd7931332016-06-28 12:18:24 +0200918 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700919 return -EOPNOTSUPP;
920
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -0300921 return perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700922}
923
Matt Mullins9594dc32019-06-11 14:53:04 -0700924/*
925 * Support executing tracepoints in normal, irq, and nmi context that each call
926 * bpf_perf_event_output
927 */
928struct bpf_trace_sample_data {
929 struct perf_sample_data sds[3];
930};
931
932static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
933static DEFINE_PER_CPU(int, bpf_trace_nest_level);
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200934BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
935 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200936{
Matt Mullins9594dc32019-06-11 14:53:04 -0700937 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
938 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200939 struct perf_raw_record raw = {
940 .frag = {
941 .size = size,
942 .data = data,
943 },
944 };
Matt Mullins9594dc32019-06-11 14:53:04 -0700945 struct perf_sample_data *sd;
946 int err;
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200947
Matt Mullins9594dc32019-06-11 14:53:04 -0700948 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
949 err = -EBUSY;
950 goto out;
951 }
952
953 sd = &sds->sds[nest_level - 1];
954
955 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
956 err = -EINVAL;
957 goto out;
958 }
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200959
Daniel Borkmann283ca522017-12-12 02:25:30 +0100960 perf_sample_data_init(sd, 0, 0);
961 sd->raw = &raw;
962
Matt Mullins9594dc32019-06-11 14:53:04 -0700963 err = __bpf_perf_event_output(regs, map, flags, sd);
964
965out:
966 this_cpu_dec(bpf_trace_nest_level);
967 return err;
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200968}
969
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700970static const struct bpf_func_proto bpf_perf_event_output_proto = {
971 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700972 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700973 .ret_type = RET_INTEGER,
974 .arg1_type = ARG_PTR_TO_CTX,
975 .arg2_type = ARG_CONST_MAP_PTR,
976 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800977 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000978 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700979};
980
Allan Zhang768fb612019-09-25 16:43:12 -0700981static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
982struct bpf_nested_pt_regs {
983 struct pt_regs regs[3];
984};
985static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
986static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200987
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200988u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
989 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200990{
Allan Zhang768fb612019-09-25 16:43:12 -0700991 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200992 struct perf_raw_frag frag = {
993 .copy = ctx_copy,
994 .size = ctx_size,
995 .data = ctx,
996 };
997 struct perf_raw_record raw = {
998 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700999 {
1000 .next = ctx_size ? &frag : NULL,
1001 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +02001002 .size = meta_size,
1003 .data = meta,
1004 },
1005 };
Allan Zhang768fb612019-09-25 16:43:12 -07001006 struct perf_sample_data *sd;
1007 struct pt_regs *regs;
1008 u64 ret;
1009
1010 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
1011 ret = -EBUSY;
1012 goto out;
1013 }
1014 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
1015 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001016
1017 perf_fetch_caller_regs(regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +01001018 perf_sample_data_init(sd, 0, 0);
1019 sd->raw = &raw;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001020
Allan Zhang768fb612019-09-25 16:43:12 -07001021 ret = __bpf_perf_event_output(regs, map, flags, sd);
1022out:
1023 this_cpu_dec(bpf_event_output_nest_level);
1024 return ret;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02001025}
1026
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001027BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -07001028{
1029 return (long) current;
1030}
1031
John Fastabendf4703782020-05-24 09:50:55 -07001032const struct bpf_func_proto bpf_get_current_task_proto = {
Alexei Starovoitov606274c2016-07-06 22:38:36 -07001033 .func = bpf_get_current_task,
1034 .gpl_only = true,
1035 .ret_type = RET_INTEGER,
1036};
1037
KP Singh3ca10322020-11-06 10:37:43 +00001038BPF_CALL_0(bpf_get_current_task_btf)
1039{
1040 return (unsigned long) current;
1041}
1042
1043BTF_ID_LIST_SINGLE(bpf_get_current_btf_ids, struct, task_struct)
1044
1045static const struct bpf_func_proto bpf_get_current_task_btf_proto = {
1046 .func = bpf_get_current_task_btf,
1047 .gpl_only = true,
1048 .ret_type = RET_PTR_TO_BTF_ID,
1049 .ret_btf_id = &bpf_get_current_btf_ids[0],
1050};
1051
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001052BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001053{
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001054 struct bpf_array *array = container_of(map, struct bpf_array, map);
1055 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001056
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001057 if (unlikely(idx >= array->map.max_entries))
1058 return -E2BIG;
1059
1060 cgrp = READ_ONCE(array->ptrs[idx]);
1061 if (unlikely(!cgrp))
1062 return -EAGAIN;
1063
1064 return task_under_cgroup_hierarchy(current, cgrp);
1065}
1066
1067static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
1068 .func = bpf_current_task_under_cgroup,
1069 .gpl_only = false,
1070 .ret_type = RET_INTEGER,
1071 .arg1_type = ARG_CONST_MAP_PTR,
1072 .arg2_type = ARG_ANYTHING,
1073};
1074
Yonghong Song8b401f92019-05-23 14:47:45 -07001075struct send_signal_irq_work {
1076 struct irq_work irq_work;
1077 struct task_struct *task;
1078 u32 sig;
Yonghong Song84829412020-01-14 19:50:02 -08001079 enum pid_type type;
Yonghong Song8b401f92019-05-23 14:47:45 -07001080};
1081
1082static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
1083
1084static void do_bpf_send_signal(struct irq_work *entry)
1085{
1086 struct send_signal_irq_work *work;
1087
1088 work = container_of(entry, struct send_signal_irq_work, irq_work);
Yonghong Song84829412020-01-14 19:50:02 -08001089 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
Yonghong Song8b401f92019-05-23 14:47:45 -07001090}
1091
Yonghong Song84829412020-01-14 19:50:02 -08001092static int bpf_send_signal_common(u32 sig, enum pid_type type)
Yonghong Song8b401f92019-05-23 14:47:45 -07001093{
1094 struct send_signal_irq_work *work = NULL;
1095
1096 /* Similar to bpf_probe_write_user, task needs to be
1097 * in a sound condition and kernel memory access be
1098 * permitted in order to send signal to the current
1099 * task.
1100 */
1101 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
1102 return -EPERM;
1103 if (unlikely(uaccess_kernel()))
1104 return -EPERM;
1105 if (unlikely(!nmi_uaccess_okay()))
1106 return -EPERM;
1107
Yonghong Song1bc78962020-03-04 11:11:04 -08001108 if (irqs_disabled()) {
Yonghong Songe1afb7022019-05-25 11:57:53 -07001109 /* Do an early check on signal validity. Otherwise,
1110 * the error is lost in deferred irq_work.
1111 */
1112 if (unlikely(!valid_signal(sig)))
1113 return -EINVAL;
1114
Yonghong Song8b401f92019-05-23 14:47:45 -07001115 work = this_cpu_ptr(&send_signal_work);
Peter Zijlstra7a9f50a2020-06-15 11:51:29 +02001116 if (irq_work_is_busy(&work->irq_work))
Yonghong Song8b401f92019-05-23 14:47:45 -07001117 return -EBUSY;
1118
1119 /* Add the current task, which is the target of sending signal,
1120 * to the irq_work. The current task may change when queued
1121 * irq works get executed.
1122 */
1123 work->task = current;
1124 work->sig = sig;
Yonghong Song84829412020-01-14 19:50:02 -08001125 work->type = type;
Yonghong Song8b401f92019-05-23 14:47:45 -07001126 irq_work_queue(&work->irq_work);
1127 return 0;
1128 }
1129
Yonghong Song84829412020-01-14 19:50:02 -08001130 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
1131}
1132
1133BPF_CALL_1(bpf_send_signal, u32, sig)
1134{
1135 return bpf_send_signal_common(sig, PIDTYPE_TGID);
Yonghong Song8b401f92019-05-23 14:47:45 -07001136}
1137
1138static const struct bpf_func_proto bpf_send_signal_proto = {
1139 .func = bpf_send_signal,
1140 .gpl_only = false,
1141 .ret_type = RET_INTEGER,
1142 .arg1_type = ARG_ANYTHING,
1143};
1144
Yonghong Song84829412020-01-14 19:50:02 -08001145BPF_CALL_1(bpf_send_signal_thread, u32, sig)
1146{
1147 return bpf_send_signal_common(sig, PIDTYPE_PID);
1148}
1149
1150static const struct bpf_func_proto bpf_send_signal_thread_proto = {
1151 .func = bpf_send_signal_thread,
1152 .gpl_only = false,
1153 .ret_type = RET_INTEGER,
1154 .arg1_type = ARG_ANYTHING,
1155};
1156
Jiri Olsa6e22ab92020-08-25 21:21:20 +02001157BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
1158{
1159 long len;
1160 char *p;
1161
1162 if (!sz)
1163 return 0;
1164
1165 p = d_path(path, buf, sz);
1166 if (IS_ERR(p)) {
1167 len = PTR_ERR(p);
1168 } else {
1169 len = buf + sz - p;
1170 memmove(buf, p, len);
1171 }
1172
1173 return len;
1174}
1175
1176BTF_SET_START(btf_allowlist_d_path)
Jiri Olsaa8a71792020-09-18 13:23:38 +02001177#ifdef CONFIG_SECURITY
1178BTF_ID(func, security_file_permission)
1179BTF_ID(func, security_inode_getattr)
1180BTF_ID(func, security_file_open)
1181#endif
1182#ifdef CONFIG_SECURITY_PATH
1183BTF_ID(func, security_path_truncate)
1184#endif
Jiri Olsa6e22ab92020-08-25 21:21:20 +02001185BTF_ID(func, vfs_truncate)
1186BTF_ID(func, vfs_fallocate)
1187BTF_ID(func, dentry_open)
1188BTF_ID(func, vfs_getattr)
1189BTF_ID(func, filp_close)
1190BTF_SET_END(btf_allowlist_d_path)
1191
1192static bool bpf_d_path_allowed(const struct bpf_prog *prog)
1193{
Song Liu3d06f342021-02-12 10:31:06 -08001194 if (prog->type == BPF_PROG_TYPE_TRACING &&
1195 prog->expected_attach_type == BPF_TRACE_ITER)
1196 return true;
1197
KP Singh6f100642020-11-13 00:59:30 +00001198 if (prog->type == BPF_PROG_TYPE_LSM)
1199 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
1200
1201 return btf_id_set_contains(&btf_allowlist_d_path,
1202 prog->aux->attach_btf_id);
Jiri Olsa6e22ab92020-08-25 21:21:20 +02001203}
1204
Lorenz Bauer9436ef62020-09-21 13:12:20 +01001205BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
Jiri Olsa6e22ab92020-08-25 21:21:20 +02001206
1207static const struct bpf_func_proto bpf_d_path_proto = {
1208 .func = bpf_d_path,
1209 .gpl_only = false,
1210 .ret_type = RET_INTEGER,
1211 .arg1_type = ARG_PTR_TO_BTF_ID,
Lorenz Bauer9436ef62020-09-21 13:12:20 +01001212 .arg1_btf_id = &bpf_d_path_btf_ids[0],
Jiri Olsa6e22ab92020-08-25 21:21:20 +02001213 .arg2_type = ARG_PTR_TO_MEM,
1214 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
Jiri Olsa6e22ab92020-08-25 21:21:20 +02001215 .allowed = bpf_d_path_allowed,
1216};
1217
Alan Maguirec4d0bfb2020-09-28 12:31:05 +01001218#define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
1219 BTF_F_PTR_RAW | BTF_F_ZERO)
1220
1221static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
1222 u64 flags, const struct btf **btf,
1223 s32 *btf_id)
1224{
1225 const struct btf_type *t;
1226
1227 if (unlikely(flags & ~(BTF_F_ALL)))
1228 return -EINVAL;
1229
1230 if (btf_ptr_size != sizeof(struct btf_ptr))
1231 return -EINVAL;
1232
1233 *btf = bpf_get_btf_vmlinux();
1234
1235 if (IS_ERR_OR_NULL(*btf))
Wang Qingabbaa432020-11-07 15:45:44 +08001236 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
Alan Maguirec4d0bfb2020-09-28 12:31:05 +01001237
1238 if (ptr->type_id > 0)
1239 *btf_id = ptr->type_id;
1240 else
1241 return -EINVAL;
1242
1243 if (*btf_id > 0)
1244 t = btf_type_by_id(*btf, *btf_id);
1245 if (*btf_id <= 0 || !t)
1246 return -ENOENT;
1247
1248 return 0;
1249}
1250
1251BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1252 u32, btf_ptr_size, u64, flags)
1253{
1254 const struct btf *btf;
1255 s32 btf_id;
1256 int ret;
1257
1258 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1259 if (ret)
1260 return ret;
1261
1262 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1263 flags);
1264}
1265
1266const struct bpf_func_proto bpf_snprintf_btf_proto = {
1267 .func = bpf_snprintf_btf,
1268 .gpl_only = false,
1269 .ret_type = RET_INTEGER,
1270 .arg1_type = ARG_PTR_TO_MEM,
1271 .arg2_type = ARG_CONST_SIZE,
1272 .arg3_type = ARG_PTR_TO_MEM,
1273 .arg4_type = ARG_CONST_SIZE,
1274 .arg5_type = ARG_ANYTHING,
1275};
1276
KP Singhfc611f42020-03-29 01:43:49 +01001277const struct bpf_func_proto *
1278bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov25415172015-03-25 12:49:20 -07001279{
1280 switch (func_id) {
1281 case BPF_FUNC_map_lookup_elem:
1282 return &bpf_map_lookup_elem_proto;
1283 case BPF_FUNC_map_update_elem:
1284 return &bpf_map_update_elem_proto;
1285 case BPF_FUNC_map_delete_elem:
1286 return &bpf_map_delete_elem_proto;
Alban Crequy02a8c812019-04-14 18:58:46 +02001287 case BPF_FUNC_map_push_elem:
1288 return &bpf_map_push_elem_proto;
1289 case BPF_FUNC_map_pop_elem:
1290 return &bpf_map_pop_elem_proto;
1291 case BPF_FUNC_map_peek_elem:
1292 return &bpf_map_peek_elem_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -07001293 case BPF_FUNC_ktime_get_ns:
1294 return &bpf_ktime_get_ns_proto;
Maciej Żenczykowski71d19212020-04-26 09:15:25 -07001295 case BPF_FUNC_ktime_get_boot_ns:
1296 return &bpf_ktime_get_boot_ns_proto;
Dmitrii Banshchikovd0551262020-11-17 18:45:49 +00001297 case BPF_FUNC_ktime_get_coarse_ns:
1298 return &bpf_ktime_get_coarse_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001299 case BPF_FUNC_tail_call:
1300 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001301 case BPF_FUNC_get_current_pid_tgid:
1302 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -07001303 case BPF_FUNC_get_current_task:
1304 return &bpf_get_current_task_proto;
KP Singh3ca10322020-11-06 10:37:43 +00001305 case BPF_FUNC_get_current_task_btf:
1306 return &bpf_get_current_task_btf_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -07001307 case BPF_FUNC_get_current_uid_gid:
1308 return &bpf_get_current_uid_gid_proto;
1309 case BPF_FUNC_get_current_comm:
1310 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -07001311 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001312 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -07001313 case BPF_FUNC_get_smp_processor_id:
1314 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +02001315 case BPF_FUNC_get_numa_node_id:
1316 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +00001317 case BPF_FUNC_perf_event_read:
1318 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -07001319 case BPF_FUNC_probe_write_user:
1320 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -07001321 case BPF_FUNC_current_task_under_cgroup:
1322 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -07001323 case BPF_FUNC_get_prandom_u32:
1324 return &bpf_get_prandom_u32_proto;
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +01001325 case BPF_FUNC_probe_read_user:
1326 return &bpf_probe_read_user_proto;
1327 case BPF_FUNC_probe_read_kernel:
1328 return &bpf_probe_read_kernel_proto;
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +01001329 case BPF_FUNC_probe_read_user_str:
1330 return &bpf_probe_read_user_str_proto;
1331 case BPF_FUNC_probe_read_kernel_str:
1332 return &bpf_probe_read_kernel_str_proto;
Daniel Borkmann0ebeea82020-05-15 12:11:16 +02001333#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1334 case BPF_FUNC_probe_read:
1335 return &bpf_probe_read_compat_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +00001336 case BPF_FUNC_probe_read_str:
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +01001337 return &bpf_probe_read_compat_str_proto;
Daniel Borkmann0ebeea82020-05-15 12:11:16 +02001338#endif
Yonghong Song34ea38c2018-06-04 08:53:41 -07001339#ifdef CONFIG_CGROUPS
Yonghong Songbf6fa2c82018-06-03 15:59:41 -07001340 case BPF_FUNC_get_current_cgroup_id:
1341 return &bpf_get_current_cgroup_id_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -07001342#endif
Yonghong Song8b401f92019-05-23 14:47:45 -07001343 case BPF_FUNC_send_signal:
1344 return &bpf_send_signal_proto;
Yonghong Song84829412020-01-14 19:50:02 -08001345 case BPF_FUNC_send_signal_thread:
1346 return &bpf_send_signal_thread_proto;
Song Liub80b0332020-02-14 15:41:46 -08001347 case BPF_FUNC_perf_event_read_value:
1348 return &bpf_perf_event_read_value_proto;
Carlos Neirab4490c52020-03-04 17:41:56 -03001349 case BPF_FUNC_get_ns_current_pid_tgid:
1350 return &bpf_get_ns_current_pid_tgid_proto;
Andrii Nakryiko457f4432020-05-29 00:54:20 -07001351 case BPF_FUNC_ringbuf_output:
1352 return &bpf_ringbuf_output_proto;
1353 case BPF_FUNC_ringbuf_reserve:
1354 return &bpf_ringbuf_reserve_proto;
1355 case BPF_FUNC_ringbuf_submit:
1356 return &bpf_ringbuf_submit_proto;
1357 case BPF_FUNC_ringbuf_discard:
1358 return &bpf_ringbuf_discard_proto;
1359 case BPF_FUNC_ringbuf_query:
1360 return &bpf_ringbuf_query_proto;
Yonghong Song72e2b2b2020-06-23 16:08:08 -07001361 case BPF_FUNC_jiffies64:
1362 return &bpf_jiffies64_proto;
Song Liufa28dcb2020-06-29 23:28:44 -07001363 case BPF_FUNC_get_task_stack:
1364 return &bpf_get_task_stack_proto;
Alexei Starovoitov07be4c42020-08-27 15:01:12 -07001365 case BPF_FUNC_copy_from_user:
1366 return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
Alan Maguirec4d0bfb2020-09-28 12:31:05 +01001367 case BPF_FUNC_snprintf_btf:
1368 return &bpf_snprintf_btf_proto;
Andrii Nakryikob7906b72020-12-11 22:36:25 +01001369 case BPF_FUNC_per_cpu_ptr:
Hao Luoeaa6bcb2020-09-29 16:50:47 -07001370 return &bpf_per_cpu_ptr_proto;
Andrii Nakryikob7906b72020-12-11 22:36:25 +01001371 case BPF_FUNC_this_cpu_ptr:
Hao Luo63d9b802020-09-29 16:50:48 -07001372 return &bpf_this_cpu_ptr_proto;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001373 default:
1374 return NULL;
1375 }
1376}
1377
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001378static const struct bpf_func_proto *
1379kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001380{
1381 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -07001382 case BPF_FUNC_perf_event_output:
1383 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -08001384 case BPF_FUNC_get_stackid:
1385 return &bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -07001386 case BPF_FUNC_get_stack:
1387 return &bpf_get_stack_proto;
Josef Bacik9802d862017-12-11 11:36:48 -05001388#ifdef CONFIG_BPF_KPROBE_OVERRIDE
1389 case BPF_FUNC_override_return:
1390 return &bpf_override_return_proto;
1391#endif
Alexei Starovoitov25415172015-03-25 12:49:20 -07001392 default:
KP Singhfc611f42020-03-29 01:43:49 +01001393 return bpf_tracing_func_proto(func_id, prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07001394 }
1395}
1396
1397/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001398static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001399 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -07001400 struct bpf_insn_access_aux *info)
Alexei Starovoitov25415172015-03-25 12:49:20 -07001401{
Alexei Starovoitov25415172015-03-25 12:49:20 -07001402 if (off < 0 || off >= sizeof(struct pt_regs))
1403 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -07001404 if (type != BPF_READ)
1405 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -07001406 if (off % size != 0)
1407 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +01001408 /*
1409 * Assertion for 32 bit to make sure last 8 byte access
1410 * (BPF_DW) to the last 4 byte member is disallowed.
1411 */
1412 if (off + size > sizeof(struct pt_regs))
1413 return false;
1414
Alexei Starovoitov25415172015-03-25 12:49:20 -07001415 return true;
1416}
1417
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001418const struct bpf_verifier_ops kprobe_verifier_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -07001419 .get_func_proto = kprobe_prog_func_proto,
1420 .is_valid_access = kprobe_prog_is_valid_access,
1421};
1422
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001423const struct bpf_prog_ops kprobe_prog_ops = {
1424};
1425
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001426BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1427 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001428{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001429 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1430
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001431 /*
1432 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1433 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001434 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001435 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001436 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001437}
1438
1439static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1440 .func = bpf_perf_event_output_tp,
1441 .gpl_only = true,
1442 .ret_type = RET_INTEGER,
1443 .arg1_type = ARG_PTR_TO_CTX,
1444 .arg2_type = ARG_CONST_MAP_PTR,
1445 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08001446 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +00001447 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001448};
1449
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001450BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1451 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001452{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001453 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001454
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001455 /*
1456 * Same comment as in bpf_perf_event_output_tp(), only that this time
1457 * the other helper's function body cannot be inlined due to being
1458 * external, thus we need to call raw helper function.
1459 */
1460 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1461 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001462}
1463
1464static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1465 .func = bpf_get_stackid_tp,
1466 .gpl_only = true,
1467 .ret_type = RET_INTEGER,
1468 .arg1_type = ARG_PTR_TO_CTX,
1469 .arg2_type = ARG_CONST_MAP_PTR,
1470 .arg3_type = ARG_ANYTHING,
1471};
1472
Yonghong Songc195651e2018-04-28 22:28:08 -07001473BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1474 u64, flags)
1475{
1476 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1477
1478 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1479 (unsigned long) size, flags, 0);
1480}
1481
1482static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1483 .func = bpf_get_stack_tp,
1484 .gpl_only = true,
1485 .ret_type = RET_INTEGER,
1486 .arg1_type = ARG_PTR_TO_CTX,
1487 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1488 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1489 .arg4_type = ARG_ANYTHING,
1490};
1491
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001492static const struct bpf_func_proto *
1493tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001494{
1495 switch (func_id) {
1496 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001497 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001498 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -07001499 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -07001500 case BPF_FUNC_get_stack:
1501 return &bpf_get_stack_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001502 default:
KP Singhfc611f42020-03-29 01:43:49 +01001503 return bpf_tracing_func_proto(func_id, prog);
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001504 }
1505}
1506
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001507static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001508 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -07001509 struct bpf_insn_access_aux *info)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001510{
1511 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1512 return false;
1513 if (type != BPF_READ)
1514 return false;
1515 if (off % size != 0)
1516 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +01001517
1518 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001519 return true;
1520}
1521
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001522const struct bpf_verifier_ops tracepoint_verifier_ops = {
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -07001523 .get_func_proto = tp_prog_func_proto,
1524 .is_valid_access = tp_prog_is_valid_access,
1525};
1526
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001527const struct bpf_prog_ops tracepoint_prog_ops = {
1528};
1529
Yonghong Songf005afe2018-03-20 11:19:17 -07001530BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1531 struct bpf_perf_event_value *, buf, u32, size)
1532{
1533 int err = -EINVAL;
1534
1535 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1536 goto clear;
1537 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1538 &buf->running);
1539 if (unlikely(err))
1540 goto clear;
1541 return 0;
1542clear:
1543 memset(buf, 0, size);
1544 return err;
1545}
1546
1547static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1548 .func = bpf_perf_prog_read_value,
1549 .gpl_only = true,
1550 .ret_type = RET_INTEGER,
1551 .arg1_type = ARG_PTR_TO_CTX,
1552 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1553 .arg3_type = ARG_CONST_SIZE,
1554};
1555
Daniel Xufff7b642020-02-17 19:04:31 -08001556BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1557 void *, buf, u32, size, u64, flags)
1558{
1559#ifndef CONFIG_X86
1560 return -ENOENT;
1561#else
1562 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1563 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1564 u32 to_copy;
1565
1566 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1567 return -EINVAL;
1568
1569 if (unlikely(!br_stack))
1570 return -EINVAL;
1571
1572 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1573 return br_stack->nr * br_entry_size;
1574
1575 if (!buf || (size % br_entry_size != 0))
1576 return -EINVAL;
1577
1578 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1579 memcpy(buf, br_stack->entries, to_copy);
1580
1581 return to_copy;
1582#endif
1583}
1584
1585static const struct bpf_func_proto bpf_read_branch_records_proto = {
1586 .func = bpf_read_branch_records,
1587 .gpl_only = true,
1588 .ret_type = RET_INTEGER,
1589 .arg1_type = ARG_PTR_TO_CTX,
1590 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1591 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1592 .arg4_type = ARG_ANYTHING,
1593};
1594
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001595static const struct bpf_func_proto *
1596pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Yonghong Songf005afe2018-03-20 11:19:17 -07001597{
1598 switch (func_id) {
1599 case BPF_FUNC_perf_event_output:
1600 return &bpf_perf_event_output_proto_tp;
1601 case BPF_FUNC_get_stackid:
Song Liu7b04d6d2020-07-23 11:06:44 -07001602 return &bpf_get_stackid_proto_pe;
Yonghong Songc195651e2018-04-28 22:28:08 -07001603 case BPF_FUNC_get_stack:
Song Liu7b04d6d2020-07-23 11:06:44 -07001604 return &bpf_get_stack_proto_pe;
Yonghong Songf005afe2018-03-20 11:19:17 -07001605 case BPF_FUNC_perf_prog_read_value:
1606 return &bpf_perf_prog_read_value_proto;
Daniel Xufff7b642020-02-17 19:04:31 -08001607 case BPF_FUNC_read_branch_records:
1608 return &bpf_read_branch_records_proto;
Yonghong Songf005afe2018-03-20 11:19:17 -07001609 default:
KP Singhfc611f42020-03-29 01:43:49 +01001610 return bpf_tracing_func_proto(func_id, prog);
Yonghong Songf005afe2018-03-20 11:19:17 -07001611 }
1612}
1613
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001614/*
1615 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1616 * to avoid potential recursive reuse issue when/if tracepoints are added
Matt Mullins9594dc32019-06-11 14:53:04 -07001617 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1618 *
1619 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1620 * in normal, irq, and nmi context.
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001621 */
Matt Mullins9594dc32019-06-11 14:53:04 -07001622struct bpf_raw_tp_regs {
1623 struct pt_regs regs[3];
1624};
1625static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1626static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1627static struct pt_regs *get_bpf_raw_tp_regs(void)
1628{
1629 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1630 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1631
1632 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1633 this_cpu_dec(bpf_raw_tp_nest_level);
1634 return ERR_PTR(-EBUSY);
1635 }
1636
1637 return &tp_regs->regs[nest_level - 1];
1638}
1639
1640static void put_bpf_raw_tp_regs(void)
1641{
1642 this_cpu_dec(bpf_raw_tp_nest_level);
1643}
1644
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001645BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1646 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1647{
Matt Mullins9594dc32019-06-11 14:53:04 -07001648 struct pt_regs *regs = get_bpf_raw_tp_regs();
1649 int ret;
1650
1651 if (IS_ERR(regs))
1652 return PTR_ERR(regs);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001653
1654 perf_fetch_caller_regs(regs);
Matt Mullins9594dc32019-06-11 14:53:04 -07001655 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1656
1657 put_bpf_raw_tp_regs();
1658 return ret;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001659}
1660
1661static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1662 .func = bpf_perf_event_output_raw_tp,
1663 .gpl_only = true,
1664 .ret_type = RET_INTEGER,
1665 .arg1_type = ARG_PTR_TO_CTX,
1666 .arg2_type = ARG_CONST_MAP_PTR,
1667 .arg3_type = ARG_ANYTHING,
1668 .arg4_type = ARG_PTR_TO_MEM,
1669 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1670};
1671
Alexei Starovoitova7658e12019-10-15 20:25:04 -07001672extern const struct bpf_func_proto bpf_skb_output_proto;
Eelco Chaudrond831ee82020-03-06 08:59:23 +00001673extern const struct bpf_func_proto bpf_xdp_output_proto;
Alexei Starovoitova7658e12019-10-15 20:25:04 -07001674
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001675BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1676 struct bpf_map *, map, u64, flags)
1677{
Matt Mullins9594dc32019-06-11 14:53:04 -07001678 struct pt_regs *regs = get_bpf_raw_tp_regs();
1679 int ret;
1680
1681 if (IS_ERR(regs))
1682 return PTR_ERR(regs);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001683
1684 perf_fetch_caller_regs(regs);
1685 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
Matt Mullins9594dc32019-06-11 14:53:04 -07001686 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1687 flags, 0, 0);
1688 put_bpf_raw_tp_regs();
1689 return ret;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001690}
1691
1692static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1693 .func = bpf_get_stackid_raw_tp,
1694 .gpl_only = true,
1695 .ret_type = RET_INTEGER,
1696 .arg1_type = ARG_PTR_TO_CTX,
1697 .arg2_type = ARG_CONST_MAP_PTR,
1698 .arg3_type = ARG_ANYTHING,
1699};
1700
Yonghong Songc195651e2018-04-28 22:28:08 -07001701BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1702 void *, buf, u32, size, u64, flags)
1703{
Matt Mullins9594dc32019-06-11 14:53:04 -07001704 struct pt_regs *regs = get_bpf_raw_tp_regs();
1705 int ret;
1706
1707 if (IS_ERR(regs))
1708 return PTR_ERR(regs);
Yonghong Songc195651e2018-04-28 22:28:08 -07001709
1710 perf_fetch_caller_regs(regs);
Matt Mullins9594dc32019-06-11 14:53:04 -07001711 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1712 (unsigned long) size, flags, 0);
1713 put_bpf_raw_tp_regs();
1714 return ret;
Yonghong Songc195651e2018-04-28 22:28:08 -07001715}
1716
1717static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1718 .func = bpf_get_stack_raw_tp,
1719 .gpl_only = true,
1720 .ret_type = RET_INTEGER,
1721 .arg1_type = ARG_PTR_TO_CTX,
1722 .arg2_type = ARG_PTR_TO_MEM,
1723 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1724 .arg4_type = ARG_ANYTHING,
1725};
1726
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001727static const struct bpf_func_proto *
1728raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001729{
1730 switch (func_id) {
1731 case BPF_FUNC_perf_event_output:
1732 return &bpf_perf_event_output_proto_raw_tp;
1733 case BPF_FUNC_get_stackid:
1734 return &bpf_get_stackid_proto_raw_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -07001735 case BPF_FUNC_get_stack:
1736 return &bpf_get_stack_proto_raw_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001737 default:
KP Singhfc611f42020-03-29 01:43:49 +01001738 return bpf_tracing_func_proto(func_id, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001739 }
1740}
1741
Jiri Olsa958a3f22020-05-31 17:42:55 +02001742const struct bpf_func_proto *
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001743tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1744{
1745 switch (func_id) {
1746#ifdef CONFIG_NET
1747 case BPF_FUNC_skb_output:
1748 return &bpf_skb_output_proto;
Eelco Chaudrond831ee82020-03-06 08:59:23 +00001749 case BPF_FUNC_xdp_output:
1750 return &bpf_xdp_output_proto;
Yonghong Songaf7ec132020-06-23 16:08:09 -07001751 case BPF_FUNC_skc_to_tcp6_sock:
1752 return &bpf_skc_to_tcp6_sock_proto;
Yonghong Song478cfbd2020-06-23 16:08:11 -07001753 case BPF_FUNC_skc_to_tcp_sock:
1754 return &bpf_skc_to_tcp_sock_proto;
1755 case BPF_FUNC_skc_to_tcp_timewait_sock:
1756 return &bpf_skc_to_tcp_timewait_sock_proto;
1757 case BPF_FUNC_skc_to_tcp_request_sock:
1758 return &bpf_skc_to_tcp_request_sock_proto;
Yonghong Song0d4fad32020-06-23 16:08:15 -07001759 case BPF_FUNC_skc_to_udp6_sock:
1760 return &bpf_skc_to_udp6_sock_proto;
Martin KaFai Lau8e4597c2020-11-12 13:13:13 -08001761 case BPF_FUNC_sk_storage_get:
1762 return &bpf_sk_storage_get_tracing_proto;
1763 case BPF_FUNC_sk_storage_delete:
1764 return &bpf_sk_storage_delete_tracing_proto;
Florent Revestb60da492020-12-08 18:36:23 +01001765 case BPF_FUNC_sock_from_file:
1766 return &bpf_sock_from_file_proto;
Florent Revestc5dbb892021-02-10 12:14:03 +01001767 case BPF_FUNC_get_socket_cookie:
1768 return &bpf_get_socket_ptr_cookie_proto;
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001769#endif
Yonghong Song492e6392020-05-09 10:59:14 -07001770 case BPF_FUNC_seq_printf:
1771 return prog->expected_attach_type == BPF_TRACE_ITER ?
1772 &bpf_seq_printf_proto :
1773 NULL;
1774 case BPF_FUNC_seq_write:
1775 return prog->expected_attach_type == BPF_TRACE_ITER ?
1776 &bpf_seq_write_proto :
1777 NULL;
Alan Maguireeb411372020-09-28 12:31:09 +01001778 case BPF_FUNC_seq_printf_btf:
1779 return prog->expected_attach_type == BPF_TRACE_ITER ?
1780 &bpf_seq_printf_btf_proto :
1781 NULL;
Jiri Olsa6e22ab92020-08-25 21:21:20 +02001782 case BPF_FUNC_d_path:
1783 return &bpf_d_path_proto;
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001784 default:
1785 return raw_tp_prog_func_proto(func_id, prog);
1786 }
1787}
1788
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001789static bool raw_tp_prog_is_valid_access(int off, int size,
1790 enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001791 const struct bpf_prog *prog,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001792 struct bpf_insn_access_aux *info)
1793{
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001794 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001795 return false;
1796 if (type != BPF_READ)
1797 return false;
1798 if (off % size != 0)
1799 return false;
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001800 return true;
1801}
1802
1803static bool tracing_prog_is_valid_access(int off, int size,
1804 enum bpf_access_type type,
1805 const struct bpf_prog *prog,
1806 struct bpf_insn_access_aux *info)
1807{
1808 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1809 return false;
1810 if (type != BPF_READ)
1811 return false;
1812 if (off % size != 0)
1813 return false;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001814 return btf_ctx_access(off, size, type, prog, info);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001815}
1816
KP Singh3e7c67d2020-03-05 23:01:27 +01001817int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1818 const union bpf_attr *kattr,
1819 union bpf_attr __user *uattr)
1820{
1821 return -ENOTSUPP;
1822}
1823
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001824const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1825 .get_func_proto = raw_tp_prog_func_proto,
1826 .is_valid_access = raw_tp_prog_is_valid_access,
1827};
1828
1829const struct bpf_prog_ops raw_tracepoint_prog_ops = {
Yonghong Songebfb4d42020-10-06 23:29:33 -07001830#ifdef CONFIG_NET
Song Liu1b4d60e2020-09-25 13:54:29 -07001831 .test_run = bpf_prog_test_run_raw_tp,
Yonghong Songebfb4d42020-10-06 23:29:33 -07001832#endif
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001833};
1834
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001835const struct bpf_verifier_ops tracing_verifier_ops = {
1836 .get_func_proto = tracing_prog_func_proto,
1837 .is_valid_access = tracing_prog_is_valid_access,
1838};
1839
1840const struct bpf_prog_ops tracing_prog_ops = {
KP Singhda00d2f2020-03-04 20:18:52 +01001841 .test_run = bpf_prog_test_run_tracing,
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001842};
1843
Matt Mullins9df1c282019-04-26 11:49:47 -07001844static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1845 enum bpf_access_type type,
1846 const struct bpf_prog *prog,
1847 struct bpf_insn_access_aux *info)
1848{
1849 if (off == 0) {
1850 if (size != sizeof(u64) || type != BPF_READ)
1851 return false;
1852 info->reg_type = PTR_TO_TP_BUFFER;
1853 }
1854 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1855}
1856
1857const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1858 .get_func_proto = raw_tp_prog_func_proto,
1859 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1860};
1861
1862const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1863};
1864
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001865static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001866 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -07001867 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001868{
Teng Qin95da0cd2018-03-06 10:55:01 -08001869 const int size_u64 = sizeof(u64);
Yonghong Song31fd8582017-06-13 15:52:13 -07001870
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001871 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1872 return false;
1873 if (type != BPF_READ)
1874 return false;
Daniel Borkmannbc231052018-06-02 23:06:39 +02001875 if (off % size != 0) {
1876 if (sizeof(unsigned long) != 4)
1877 return false;
1878 if (size != 8)
1879 return false;
1880 if (off % size != 4)
1881 return false;
1882 }
Yonghong Song31fd8582017-06-13 15:52:13 -07001883
Daniel Borkmannf96da092017-07-02 02:13:27 +02001884 switch (off) {
1885 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
Teng Qin95da0cd2018-03-06 10:55:01 -08001886 bpf_ctx_record_field_size(info, size_u64);
1887 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1888 return false;
1889 break;
1890 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1891 bpf_ctx_record_field_size(info, size_u64);
1892 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
Yonghong Song23994632017-06-22 15:07:39 -07001893 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +02001894 break;
1895 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001896 if (size != sizeof(long))
1897 return false;
1898 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02001899
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001900 return true;
1901}
1902
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001903static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1904 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001905 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +02001906 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001907{
1908 struct bpf_insn *insn = insn_buf;
1909
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001910 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001911 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +02001912 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001913 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001914 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001915 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02001916 bpf_target_off(struct perf_sample_data, period, 8,
1917 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001918 break;
Teng Qin95da0cd2018-03-06 10:55:01 -08001919 case offsetof(struct bpf_perf_event_data, addr):
1920 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1921 data), si->dst_reg, si->src_reg,
1922 offsetof(struct bpf_perf_event_data_kern, data));
1923 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1924 bpf_target_off(struct perf_sample_data, addr, 8,
1925 target_size));
1926 break;
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001927 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +02001928 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001929 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001930 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001931 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1932 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001933 break;
1934 }
1935
1936 return insn - insn_buf;
1937}
1938
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001939const struct bpf_verifier_ops perf_event_verifier_ops = {
Yonghong Songf005afe2018-03-20 11:19:17 -07001940 .get_func_proto = pe_prog_func_proto,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001941 .is_valid_access = pe_prog_is_valid_access,
1942 .convert_ctx_access = pe_prog_convert_ctx_access,
1943};
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001944
1945const struct bpf_prog_ops perf_event_prog_ops = {
1946};
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001947
1948static DEFINE_MUTEX(bpf_event_mutex);
1949
Yonghong Songc8c088b2017-11-30 13:47:54 -08001950#define BPF_TRACE_MAX_PROGS 64
1951
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001952int perf_event_attach_bpf_prog(struct perf_event *event,
1953 struct bpf_prog *prog)
1954{
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001955 struct bpf_prog_array *old_array;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001956 struct bpf_prog_array *new_array;
1957 int ret = -EEXIST;
1958
Josef Bacik9802d862017-12-11 11:36:48 -05001959 /*
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001960 * Kprobe override only works if they are on the function entry,
1961 * and only if they are on the opt-in list.
Josef Bacik9802d862017-12-11 11:36:48 -05001962 */
1963 if (prog->kprobe_override &&
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001964 (!trace_kprobe_on_func_entry(event->tp_event) ||
Josef Bacik9802d862017-12-11 11:36:48 -05001965 !trace_kprobe_error_injectable(event->tp_event)))
1966 return -EINVAL;
1967
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001968 mutex_lock(&bpf_event_mutex);
1969
1970 if (event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001971 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001972
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001973 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
Yonghong Songc8c088b2017-11-30 13:47:54 -08001974 if (old_array &&
1975 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1976 ret = -E2BIG;
1977 goto unlock;
1978 }
1979
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001980 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1981 if (ret < 0)
Yonghong Song07c41a22017-10-30 13:50:22 -07001982 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001983
1984 /* set the new array to event->tp_event and set event->prog */
1985 event->prog = prog;
1986 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1987 bpf_prog_array_free(old_array);
1988
Yonghong Song07c41a22017-10-30 13:50:22 -07001989unlock:
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001990 mutex_unlock(&bpf_event_mutex);
1991 return ret;
1992}
1993
1994void perf_event_detach_bpf_prog(struct perf_event *event)
1995{
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001996 struct bpf_prog_array *old_array;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001997 struct bpf_prog_array *new_array;
1998 int ret;
1999
2000 mutex_lock(&bpf_event_mutex);
2001
2002 if (!event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07002003 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07002004
Stanislav Fomicheve672db02019-05-28 14:14:44 -07002005 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
Yonghong Songe87c6bc2017-10-23 23:53:08 -07002006 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
Sean Young170a7e32018-05-27 12:24:08 +01002007 if (ret == -ENOENT)
2008 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07002009 if (ret < 0) {
2010 bpf_prog_array_delete_safe(old_array, event->prog);
2011 } else {
2012 rcu_assign_pointer(event->tp_event->prog_array, new_array);
2013 bpf_prog_array_free(old_array);
2014 }
2015
2016 bpf_prog_put(event->prog);
2017 event->prog = NULL;
2018
Yonghong Song07c41a22017-10-30 13:50:22 -07002019unlock:
Yonghong Songe87c6bc2017-10-23 23:53:08 -07002020 mutex_unlock(&bpf_event_mutex);
2021}
Yonghong Songf371b302017-12-11 11:39:02 -08002022
Yonghong Songf4e22982017-12-13 10:35:37 -08002023int perf_event_query_prog_array(struct perf_event *event, void __user *info)
Yonghong Songf371b302017-12-11 11:39:02 -08002024{
2025 struct perf_event_query_bpf __user *uquery = info;
2026 struct perf_event_query_bpf query = {};
Stanislav Fomicheve672db02019-05-28 14:14:44 -07002027 struct bpf_prog_array *progs;
Yonghong Song3a38bb92018-04-10 09:37:32 -07002028 u32 *ids, prog_cnt, ids_len;
Yonghong Songf371b302017-12-11 11:39:02 -08002029 int ret;
2030
Alexey Budankov031258d2020-04-02 11:48:54 +03002031 if (!perfmon_capable())
Yonghong Songf371b302017-12-11 11:39:02 -08002032 return -EPERM;
2033 if (event->attr.type != PERF_TYPE_TRACEPOINT)
2034 return -EINVAL;
2035 if (copy_from_user(&query, uquery, sizeof(query)))
2036 return -EFAULT;
Yonghong Song3a38bb92018-04-10 09:37:32 -07002037
2038 ids_len = query.ids_len;
2039 if (ids_len > BPF_TRACE_MAX_PROGS)
Daniel Borkmann9c481b92018-02-14 15:31:00 +01002040 return -E2BIG;
Yonghong Song3a38bb92018-04-10 09:37:32 -07002041 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2042 if (!ids)
2043 return -ENOMEM;
2044 /*
2045 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
2046 * is required when user only wants to check for uquery->prog_cnt.
2047 * There is no need to check for it since the case is handled
2048 * gracefully in bpf_prog_array_copy_info.
2049 */
Yonghong Songf371b302017-12-11 11:39:02 -08002050
2051 mutex_lock(&bpf_event_mutex);
Stanislav Fomicheve672db02019-05-28 14:14:44 -07002052 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2053 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
Yonghong Songf371b302017-12-11 11:39:02 -08002054 mutex_unlock(&bpf_event_mutex);
2055
Yonghong Song3a38bb92018-04-10 09:37:32 -07002056 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2057 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2058 ret = -EFAULT;
2059
2060 kfree(ids);
Yonghong Songf371b302017-12-11 11:39:02 -08002061 return ret;
2062}
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002063
2064extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2065extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2066
Matt Mullinsa38d1102018-12-12 16:42:37 -08002067struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002068{
2069 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2070
2071 for (; btp < __stop__bpf_raw_tp; btp++) {
2072 if (!strcmp(btp->tp->name, name))
2073 return btp;
2074 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08002075
2076 return bpf_get_raw_tracepoint_module(name);
2077}
2078
2079void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2080{
Andrii Nakryiko12cc1262020-12-03 12:46:21 -08002081 struct module *mod;
Matt Mullinsa38d1102018-12-12 16:42:37 -08002082
Andrii Nakryiko12cc1262020-12-03 12:46:21 -08002083 preempt_disable();
2084 mod = __module_address((unsigned long)btp);
2085 module_put(mod);
2086 preempt_enable();
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002087}
2088
2089static __always_inline
2090void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2091{
Thomas Gleixnerf03efe42020-02-24 15:01:35 +01002092 cant_sleep();
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002093 rcu_read_lock();
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002094 (void) BPF_PROG_RUN(prog, args);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002095 rcu_read_unlock();
2096}
2097
2098#define UNPACK(...) __VA_ARGS__
2099#define REPEAT_1(FN, DL, X, ...) FN(X)
2100#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2101#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2102#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2103#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2104#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2105#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2106#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2107#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2108#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2109#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2110#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2111#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2112
2113#define SARG(X) u64 arg##X
2114#define COPY(X) args[X] = arg##X
2115
2116#define __DL_COM (,)
2117#define __DL_SEM (;)
2118
2119#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2120
2121#define BPF_TRACE_DEFN_x(x) \
2122 void bpf_trace_run##x(struct bpf_prog *prog, \
2123 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2124 { \
2125 u64 args[x]; \
2126 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2127 __bpf_trace_run(prog, args); \
2128 } \
2129 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2130BPF_TRACE_DEFN_x(1);
2131BPF_TRACE_DEFN_x(2);
2132BPF_TRACE_DEFN_x(3);
2133BPF_TRACE_DEFN_x(4);
2134BPF_TRACE_DEFN_x(5);
2135BPF_TRACE_DEFN_x(6);
2136BPF_TRACE_DEFN_x(7);
2137BPF_TRACE_DEFN_x(8);
2138BPF_TRACE_DEFN_x(9);
2139BPF_TRACE_DEFN_x(10);
2140BPF_TRACE_DEFN_x(11);
2141BPF_TRACE_DEFN_x(12);
2142
2143static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2144{
2145 struct tracepoint *tp = btp->tp;
2146
2147 /*
2148 * check that program doesn't access arguments beyond what's
2149 * available in this tracepoint
2150 */
2151 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2152 return -EINVAL;
2153
Matt Mullins9df1c282019-04-26 11:49:47 -07002154 if (prog->aux->max_tp_access > btp->writable_size)
2155 return -EINVAL;
2156
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002157 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
2158}
2159
2160int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2161{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08002162 return __bpf_probe_register(btp, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002163}
2164
2165int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2166{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08002167 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002168}
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002169
2170int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2171 u32 *fd_type, const char **buf,
2172 u64 *probe_offset, u64 *probe_addr)
2173{
2174 bool is_tracepoint, is_syscall_tp;
2175 struct bpf_prog *prog;
2176 int flags, err = 0;
2177
2178 prog = event->prog;
2179 if (!prog)
2180 return -ENOENT;
2181
2182 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2183 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2184 return -EOPNOTSUPP;
2185
2186 *prog_id = prog->aux->id;
2187 flags = event->tp_event->flags;
2188 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2189 is_syscall_tp = is_syscall_trace_event(event->tp_event);
2190
2191 if (is_tracepoint || is_syscall_tp) {
2192 *buf = is_tracepoint ? event->tp_event->tp->name
2193 : event->tp_event->name;
2194 *fd_type = BPF_FD_TYPE_TRACEPOINT;
2195 *probe_offset = 0x0;
2196 *probe_addr = 0x0;
2197 } else {
2198 /* kprobe/uprobe */
2199 err = -EOPNOTSUPP;
2200#ifdef CONFIG_KPROBE_EVENTS
2201 if (flags & TRACE_EVENT_FL_KPROBE)
2202 err = bpf_get_kprobe_info(event, fd_type, buf,
2203 probe_offset, probe_addr,
2204 event->attr.type == PERF_TYPE_TRACEPOINT);
2205#endif
2206#ifdef CONFIG_UPROBE_EVENTS
2207 if (flags & TRACE_EVENT_FL_UPROBE)
2208 err = bpf_get_uprobe_info(event, fd_type, buf,
2209 probe_offset,
2210 event->attr.type == PERF_TYPE_TRACEPOINT);
2211#endif
2212 }
2213
2214 return err;
2215}
Matt Mullinsa38d1102018-12-12 16:42:37 -08002216
Yonghong Song9db1ff02019-06-25 17:35:03 -07002217static int __init send_signal_irq_work_init(void)
2218{
2219 int cpu;
2220 struct send_signal_irq_work *work;
2221
2222 for_each_possible_cpu(cpu) {
2223 work = per_cpu_ptr(&send_signal_work, cpu);
2224 init_irq_work(&work->irq_work, do_bpf_send_signal);
2225 }
2226 return 0;
2227}
2228
2229subsys_initcall(send_signal_irq_work_init);
2230
Matt Mullinsa38d1102018-12-12 16:42:37 -08002231#ifdef CONFIG_MODULES
Stanislav Fomichev390e99c2019-05-13 12:04:36 -07002232static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2233 void *module)
Matt Mullinsa38d1102018-12-12 16:42:37 -08002234{
2235 struct bpf_trace_module *btm, *tmp;
2236 struct module *mod = module;
Peter Zijlstra0340a6b2020-08-18 15:57:37 +02002237 int ret = 0;
Matt Mullinsa38d1102018-12-12 16:42:37 -08002238
2239 if (mod->num_bpf_raw_events == 0 ||
2240 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
Peter Zijlstra0340a6b2020-08-18 15:57:37 +02002241 goto out;
Matt Mullinsa38d1102018-12-12 16:42:37 -08002242
2243 mutex_lock(&bpf_module_mutex);
2244
2245 switch (op) {
2246 case MODULE_STATE_COMING:
2247 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2248 if (btm) {
2249 btm->module = module;
2250 list_add(&btm->list, &bpf_trace_modules);
Peter Zijlstra0340a6b2020-08-18 15:57:37 +02002251 } else {
2252 ret = -ENOMEM;
Matt Mullinsa38d1102018-12-12 16:42:37 -08002253 }
2254 break;
2255 case MODULE_STATE_GOING:
2256 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2257 if (btm->module == module) {
2258 list_del(&btm->list);
2259 kfree(btm);
2260 break;
2261 }
2262 }
2263 break;
2264 }
2265
2266 mutex_unlock(&bpf_module_mutex);
2267
Peter Zijlstra0340a6b2020-08-18 15:57:37 +02002268out:
2269 return notifier_from_errno(ret);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002270}
2271
2272static struct notifier_block bpf_module_nb = {
2273 .notifier_call = bpf_event_notify,
2274};
2275
Stanislav Fomichev390e99c2019-05-13 12:04:36 -07002276static int __init bpf_event_init(void)
Matt Mullinsa38d1102018-12-12 16:42:37 -08002277{
2278 register_module_notifier(&bpf_module_nb);
2279 return 0;
2280}
2281
2282fs_initcall(bpf_event_init);
2283#endif /* CONFIG_MODULES */