blob: 68250d433bd78b7ba0b90d84ab8500535d4633e6 [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>
Alexei Starovoitov25415172015-03-25 12:49:20 -070010#include <linux/filter.h>
11#include <linux/uaccess.h>
Alexei Starovoitov9c959c82015-03-25 12:49:22 -070012#include <linux/ctype.h>
Josef Bacik9802d862017-12-11 11:36:48 -050013#include <linux/kprobes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070014#include <linux/syscalls.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090015#include <linux/error-injection.h>
Josef Bacik9802d862017-12-11 11:36:48 -050016
Nadav Amitc7b6f292019-04-25 17:11:43 -070017#include <asm/tlb.h>
18
Josef Bacik9802d862017-12-11 11:36:48 -050019#include "trace_probe.h"
Alexei Starovoitov25415172015-03-25 12:49:20 -070020#include "trace.h"
21
Stanislav Fomicheve672db02019-05-28 14:14:44 -070022#define bpf_event_rcu_dereference(p) \
23 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
24
Matt Mullinsa38d1102018-12-12 16:42:37 -080025#ifdef CONFIG_MODULES
26struct bpf_trace_module {
27 struct module *module;
28 struct list_head list;
29};
30
31static LIST_HEAD(bpf_trace_modules);
32static DEFINE_MUTEX(bpf_module_mutex);
33
34static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
35{
36 struct bpf_raw_event_map *btp, *ret = NULL;
37 struct bpf_trace_module *btm;
38 unsigned int i;
39
40 mutex_lock(&bpf_module_mutex);
41 list_for_each_entry(btm, &bpf_trace_modules, list) {
42 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
43 btp = &btm->module->bpf_raw_events[i];
44 if (!strcmp(btp->tp->name, name)) {
45 if (try_module_get(btm->module))
46 ret = btp;
47 goto out;
48 }
49 }
50 }
51out:
52 mutex_unlock(&bpf_module_mutex);
53 return ret;
54}
55#else
56static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
57{
58 return NULL;
59}
60#endif /* CONFIG_MODULES */
61
Gianluca Borello035226b2017-10-26 01:47:42 +000062u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Yonghong Songc195651e2018-04-28 22:28:08 -070063u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Gianluca Borello035226b2017-10-26 01:47:42 +000064
Alexei Starovoitov25415172015-03-25 12:49:20 -070065/**
66 * trace_call_bpf - invoke BPF program
Yonghong Songe87c6bc2017-10-23 23:53:08 -070067 * @call: tracepoint event
Alexei Starovoitov25415172015-03-25 12:49:20 -070068 * @ctx: opaque context pointer
69 *
70 * kprobe handlers execute BPF programs via this helper.
71 * Can be used from static tracepoints in the future.
72 *
73 * Return: BPF programs always return an integer which is interpreted by
74 * kprobe handler as:
75 * 0 - return from kprobe (event is filtered out)
76 * 1 - store kprobe event into ring buffer
77 * Other values are reserved and currently alias to 1
78 */
Yonghong Songe87c6bc2017-10-23 23:53:08 -070079unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
Alexei Starovoitov25415172015-03-25 12:49:20 -070080{
81 unsigned int ret;
82
83 if (in_nmi()) /* not supported yet */
84 return 1;
85
86 preempt_disable();
87
88 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
89 /*
90 * since some bpf program is already running on this cpu,
91 * don't call into another bpf program (same or different)
92 * and don't send kprobe event into ring-buffer,
93 * so return zero here
94 */
95 ret = 0;
96 goto out;
97 }
98
Yonghong Songe87c6bc2017-10-23 23:53:08 -070099 /*
100 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
101 * to all call sites, we did a bpf_prog_array_valid() there to check
102 * whether call->prog_array is empty or not, which is
103 * a heurisitc to speed up execution.
104 *
105 * If bpf_prog_array_valid() fetched prog_array was
106 * non-NULL, we go into trace_call_bpf() and do the actual
107 * proper rcu_dereference() under RCU lock.
108 * If it turns out that prog_array is NULL then, we bail out.
109 * For the opposite, if the bpf_prog_array_valid() fetched pointer
110 * was NULL, you'll skip the prog_array with the risk of missing
111 * out of events when it was updated in between this and the
112 * rcu_dereference() which is accepted risk.
113 */
114 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700115
116 out:
117 __this_cpu_dec(bpf_prog_active);
118 preempt_enable();
119
120 return ret;
121}
122EXPORT_SYMBOL_GPL(trace_call_bpf);
123
Josef Bacik9802d862017-12-11 11:36:48 -0500124#ifdef CONFIG_BPF_KPROBE_OVERRIDE
125BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
126{
Josef Bacik9802d862017-12-11 11:36:48 -0500127 regs_set_return_value(regs, rc);
Masami Hiramatsu540adea2018-01-13 02:55:03 +0900128 override_function_with_return(regs);
Josef Bacik9802d862017-12-11 11:36:48 -0500129 return 0;
130}
131
132static const struct bpf_func_proto bpf_override_return_proto = {
133 .func = bpf_override_return,
134 .gpl_only = true,
135 .ret_type = RET_INTEGER,
136 .arg1_type = ARG_PTR_TO_CTX,
137 .arg2_type = ARG_ANYTHING,
138};
139#endif
140
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100141BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
142 const void __user *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700143{
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100144 int ret = probe_user_read(dst, unsafe_ptr, size);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700145
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200146 if (unlikely(ret < 0))
147 memset(dst, 0, size);
148
149 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700150}
151
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100152static const struct bpf_func_proto bpf_probe_read_user_proto = {
153 .func = bpf_probe_read_user,
154 .gpl_only = true,
155 .ret_type = RET_INTEGER,
156 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
157 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
158 .arg3_type = ARG_ANYTHING,
159};
160
161BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
162 const void __user *, unsafe_ptr)
163{
164 int ret = strncpy_from_unsafe_user(dst, unsafe_ptr, size);
165
166 if (unlikely(ret < 0))
167 memset(dst, 0, size);
168
169 return ret;
170}
171
172static const struct bpf_func_proto bpf_probe_read_user_str_proto = {
173 .func = bpf_probe_read_user_str,
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
181static __always_inline int
182bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr,
183 const bool compat)
184{
185 int ret = security_locked_down(LOCKDOWN_BPF_READ);
186
187 if (unlikely(ret < 0))
188 goto out;
189 ret = compat ? probe_kernel_read(dst, unsafe_ptr, size) :
190 probe_kernel_read_strict(dst, unsafe_ptr, size);
191 if (unlikely(ret < 0))
192out:
193 memset(dst, 0, size);
194 return ret;
195}
196
197BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
198 const void *, unsafe_ptr)
199{
200 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false);
201}
202
203static const struct bpf_func_proto bpf_probe_read_kernel_proto = {
204 .func = bpf_probe_read_kernel,
205 .gpl_only = true,
206 .ret_type = RET_INTEGER,
207 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
208 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
209 .arg3_type = ARG_ANYTHING,
210};
211
212BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
213 const void *, unsafe_ptr)
214{
215 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, true);
216}
217
218static const struct bpf_func_proto bpf_probe_read_compat_proto = {
219 .func = bpf_probe_read_compat,
220 .gpl_only = true,
221 .ret_type = RET_INTEGER,
222 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
223 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
224 .arg3_type = ARG_ANYTHING,
225};
226
227static __always_inline int
228bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,
229 const bool compat)
230{
231 int ret = security_locked_down(LOCKDOWN_BPF_READ);
232
233 if (unlikely(ret < 0))
234 goto out;
235 /*
236 * The strncpy_from_unsafe_*() call will likely not fill the entire
237 * buffer, but that's okay in this circumstance as we're probing
238 * arbitrary memory anyway similar to bpf_probe_read_*() and might
239 * as well probe the stack. Thus, memory is explicitly cleared
240 * only in error case, so that improper users ignoring return
241 * code altogether don't copy garbage; otherwise length of string
242 * is returned that can be used for bpf_perf_event_output() et al.
243 */
244 ret = compat ? strncpy_from_unsafe(dst, unsafe_ptr, size) :
245 strncpy_from_unsafe_strict(dst, unsafe_ptr, size);
246 if (unlikely(ret < 0))
247out:
248 memset(dst, 0, size);
249 return ret;
250}
251
252BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
253 const void *, unsafe_ptr)
254{
255 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false);
256}
257
258static const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
259 .func = bpf_probe_read_kernel_str,
260 .gpl_only = true,
261 .ret_type = RET_INTEGER,
262 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
263 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
264 .arg3_type = ARG_ANYTHING,
265};
266
267BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
268 const void *, unsafe_ptr)
269{
270 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, true);
271}
272
273static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
274 .func = bpf_probe_read_compat_str,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700275 .gpl_only = true,
276 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800277 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Yonghong Song9c019e22017-11-12 14:49:10 -0800278 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700279 .arg3_type = ARG_ANYTHING,
280};
281
Daniel Borkmanneb1b6682019-11-02 00:17:58 +0100282BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200283 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700284{
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700285 /*
286 * Ensure we're in user context which is safe for the helper to
287 * run. This helper has no business in a kthread.
288 *
289 * access_ok() should prevent writing to non-user memory, but in
290 * some situations (nommu, temporary switch, etc) access_ok() does
291 * not provide enough validation, hence the check on KERNEL_DS.
Nadav Amitc7b6f292019-04-25 17:11:43 -0700292 *
293 * nmi_uaccess_okay() ensures the probe is not run in an interim
294 * state, when the task or mm are switched. This is specifically
295 * required to prevent the use of temporary mm.
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700296 */
297
298 if (unlikely(in_interrupt() ||
299 current->flags & (PF_KTHREAD | PF_EXITING)))
300 return -EPERM;
Al Virodb68ce12017-03-20 21:08:07 -0400301 if (unlikely(uaccess_kernel()))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700302 return -EPERM;
Nadav Amitc7b6f292019-04-25 17:11:43 -0700303 if (unlikely(!nmi_uaccess_okay()))
304 return -EPERM;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700305
Daniel Borkmanneb1b6682019-11-02 00:17:58 +0100306 return probe_user_write(unsafe_ptr, src, size);
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700307}
308
309static const struct bpf_func_proto bpf_probe_write_user_proto = {
310 .func = bpf_probe_write_user,
311 .gpl_only = true,
312 .ret_type = RET_INTEGER,
313 .arg1_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800314 .arg2_type = ARG_PTR_TO_MEM,
315 .arg3_type = ARG_CONST_SIZE,
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700316};
317
318static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
319{
320 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
321 current->comm, task_pid_nr(current));
322
323 return &bpf_probe_write_user_proto;
324}
325
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700326/*
John Fastabend7bda4b42017-07-02 02:13:29 +0200327 * Only limited trace_printk() conversion specifiers allowed:
328 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700329 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200330BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
331 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700332{
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700333 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700334 int mod[3] = {};
335 int fmt_cnt = 0;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700336 u64 unsafe_addr;
337 char buf[64];
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700338 int i;
339
340 /*
341 * bpf_check()->check_func_arg()->check_stack_boundary()
342 * guarantees that fmt points to bpf program stack,
343 * fmt_size bytes of it were initialized and fmt_size > 0
344 */
345 if (fmt[--fmt_size] != 0)
346 return -EINVAL;
347
348 /* check format string for allowed specifiers */
349 for (i = 0; i < fmt_size; i++) {
350 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
351 return -EINVAL;
352
353 if (fmt[i] != '%')
354 continue;
355
356 if (fmt_cnt >= 3)
357 return -EINVAL;
358
359 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
360 i++;
361 if (fmt[i] == 'l') {
362 mod[fmt_cnt]++;
363 i++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700364 } else if (fmt[i] == 'p' || fmt[i] == 's') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700365 mod[fmt_cnt]++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100366 /* disallow any further format extensions */
367 if (fmt[i + 1] != 0 &&
368 !isspace(fmt[i + 1]) &&
369 !ispunct(fmt[i + 1]))
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700370 return -EINVAL;
371 fmt_cnt++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100372 if (fmt[i] == 's') {
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700373 if (str_seen)
374 /* allow only one '%s' per fmt string */
375 return -EINVAL;
376 str_seen = true;
377
378 switch (fmt_cnt) {
379 case 1:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200380 unsafe_addr = arg1;
381 arg1 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700382 break;
383 case 2:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200384 unsafe_addr = arg2;
385 arg2 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700386 break;
387 case 3:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200388 unsafe_addr = arg3;
389 arg3 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700390 break;
391 }
392 buf[0] = 0;
393 strncpy_from_unsafe(buf,
394 (void *) (long) unsafe_addr,
395 sizeof(buf));
396 }
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700397 continue;
398 }
399
400 if (fmt[i] == 'l') {
401 mod[fmt_cnt]++;
402 i++;
403 }
404
John Fastabend7bda4b42017-07-02 02:13:29 +0200405 if (fmt[i] != 'i' && fmt[i] != 'd' &&
406 fmt[i] != 'u' && fmt[i] != 'x')
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700407 return -EINVAL;
408 fmt_cnt++;
409 }
410
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200411/* Horrid workaround for getting va_list handling working with different
412 * argument type combinations generically for 32 and 64 bit archs.
413 */
414#define __BPF_TP_EMIT() __BPF_ARG3_TP()
415#define __BPF_TP(...) \
Yonghong Songeefa864a2018-01-17 09:19:32 -0800416 __trace_printk(0 /* Fake ip */, \
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200417 fmt, ##__VA_ARGS__)
418
419#define __BPF_ARG1_TP(...) \
420 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
421 ? __BPF_TP(arg1, ##__VA_ARGS__) \
422 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
423 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
424 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
425
426#define __BPF_ARG2_TP(...) \
427 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
428 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
429 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
430 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
431 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
432
433#define __BPF_ARG3_TP(...) \
434 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
435 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
436 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
437 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
438 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
439
440 return __BPF_TP_EMIT();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700441}
442
443static const struct bpf_func_proto bpf_trace_printk_proto = {
444 .func = bpf_trace_printk,
445 .gpl_only = true,
446 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800447 .arg1_type = ARG_PTR_TO_MEM,
448 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700449};
450
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700451const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
452{
453 /*
454 * this program might be calling bpf_trace_printk,
455 * so allocate per-cpu printk buffers
456 */
457 trace_printk_init_buffers();
458
459 return &bpf_trace_printk_proto;
460}
461
Yonghong Song908432c2017-10-05 09:19:20 -0700462static __always_inline int
463get_map_perf_counter(struct bpf_map *map, u64 flags,
464 u64 *value, u64 *enabled, u64 *running)
Kaixu Xia35578d72015-08-06 07:02:35 +0000465{
Kaixu Xia35578d72015-08-06 07:02:35 +0000466 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200467 unsigned int cpu = smp_processor_id();
468 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200469 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000470
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200471 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
472 return -EINVAL;
473 if (index == BPF_F_CURRENT_CPU)
474 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000475 if (unlikely(index >= array->map.max_entries))
476 return -E2BIG;
477
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200478 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200479 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000480 return -ENOENT;
481
Yonghong Song908432c2017-10-05 09:19:20 -0700482 return perf_event_read_local(ee->event, value, enabled, running);
483}
484
485BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
486{
487 u64 value = 0;
488 int err;
489
490 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
Kaixu Xia35578d72015-08-06 07:02:35 +0000491 /*
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700492 * this api is ugly since we miss [-22..-2] range of valid
493 * counter values, but that's uapi
Kaixu Xia35578d72015-08-06 07:02:35 +0000494 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700495 if (err)
496 return err;
497 return value;
Kaixu Xia35578d72015-08-06 07:02:35 +0000498}
499
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700500static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000501 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700502 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000503 .ret_type = RET_INTEGER,
504 .arg1_type = ARG_CONST_MAP_PTR,
505 .arg2_type = ARG_ANYTHING,
506};
507
Yonghong Song908432c2017-10-05 09:19:20 -0700508BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
509 struct bpf_perf_event_value *, buf, u32, size)
510{
511 int err = -EINVAL;
512
513 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
514 goto clear;
515 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
516 &buf->running);
517 if (unlikely(err))
518 goto clear;
519 return 0;
520clear:
521 memset(buf, 0, size);
522 return err;
523}
524
525static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
526 .func = bpf_perf_event_read_value,
527 .gpl_only = true,
528 .ret_type = RET_INTEGER,
529 .arg1_type = ARG_CONST_MAP_PTR,
530 .arg2_type = ARG_ANYTHING,
531 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
532 .arg4_type = ARG_CONST_SIZE,
533};
534
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200535static __always_inline u64
536__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
Daniel Borkmann283ca522017-12-12 02:25:30 +0100537 u64 flags, struct perf_sample_data *sd)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700538{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700539 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200540 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200541 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200542 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700543 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700544
Daniel Borkmann1e337592016-04-18 21:01:23 +0200545 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200546 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700547 if (unlikely(index >= array->map.max_entries))
548 return -E2BIG;
549
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200550 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200551 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700552 return -ENOENT;
553
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200554 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700555 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
556 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
557 return -EINVAL;
558
Daniel Borkmannd7931332016-06-28 12:18:24 +0200559 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700560 return -EOPNOTSUPP;
561
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -0300562 return perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700563}
564
Matt Mullins9594dc32019-06-11 14:53:04 -0700565/*
566 * Support executing tracepoints in normal, irq, and nmi context that each call
567 * bpf_perf_event_output
568 */
569struct bpf_trace_sample_data {
570 struct perf_sample_data sds[3];
571};
572
573static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
574static DEFINE_PER_CPU(int, bpf_trace_nest_level);
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200575BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
576 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200577{
Matt Mullins9594dc32019-06-11 14:53:04 -0700578 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
579 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200580 struct perf_raw_record raw = {
581 .frag = {
582 .size = size,
583 .data = data,
584 },
585 };
Matt Mullins9594dc32019-06-11 14:53:04 -0700586 struct perf_sample_data *sd;
587 int err;
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200588
Matt Mullins9594dc32019-06-11 14:53:04 -0700589 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
590 err = -EBUSY;
591 goto out;
592 }
593
594 sd = &sds->sds[nest_level - 1];
595
596 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
597 err = -EINVAL;
598 goto out;
599 }
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200600
Daniel Borkmann283ca522017-12-12 02:25:30 +0100601 perf_sample_data_init(sd, 0, 0);
602 sd->raw = &raw;
603
Matt Mullins9594dc32019-06-11 14:53:04 -0700604 err = __bpf_perf_event_output(regs, map, flags, sd);
605
606out:
607 this_cpu_dec(bpf_trace_nest_level);
608 return err;
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200609}
610
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700611static const struct bpf_func_proto bpf_perf_event_output_proto = {
612 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700613 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700614 .ret_type = RET_INTEGER,
615 .arg1_type = ARG_PTR_TO_CTX,
616 .arg2_type = ARG_CONST_MAP_PTR,
617 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800618 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000619 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700620};
621
Allan Zhang768fb612019-09-25 16:43:12 -0700622static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
623struct bpf_nested_pt_regs {
624 struct pt_regs regs[3];
625};
626static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
627static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200628
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200629u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
630 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200631{
Allan Zhang768fb612019-09-25 16:43:12 -0700632 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200633 struct perf_raw_frag frag = {
634 .copy = ctx_copy,
635 .size = ctx_size,
636 .data = ctx,
637 };
638 struct perf_raw_record raw = {
639 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700640 {
641 .next = ctx_size ? &frag : NULL,
642 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200643 .size = meta_size,
644 .data = meta,
645 },
646 };
Allan Zhang768fb612019-09-25 16:43:12 -0700647 struct perf_sample_data *sd;
648 struct pt_regs *regs;
649 u64 ret;
650
651 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
652 ret = -EBUSY;
653 goto out;
654 }
655 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
656 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200657
658 perf_fetch_caller_regs(regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100659 perf_sample_data_init(sd, 0, 0);
660 sd->raw = &raw;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200661
Allan Zhang768fb612019-09-25 16:43:12 -0700662 ret = __bpf_perf_event_output(regs, map, flags, sd);
663out:
664 this_cpu_dec(bpf_event_output_nest_level);
665 return ret;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200666}
667
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200668BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700669{
670 return (long) current;
671}
672
673static const struct bpf_func_proto bpf_get_current_task_proto = {
674 .func = bpf_get_current_task,
675 .gpl_only = true,
676 .ret_type = RET_INTEGER,
677};
678
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200679BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700680{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700681 struct bpf_array *array = container_of(map, struct bpf_array, map);
682 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700683
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700684 if (unlikely(idx >= array->map.max_entries))
685 return -E2BIG;
686
687 cgrp = READ_ONCE(array->ptrs[idx]);
688 if (unlikely(!cgrp))
689 return -EAGAIN;
690
691 return task_under_cgroup_hierarchy(current, cgrp);
692}
693
694static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
695 .func = bpf_current_task_under_cgroup,
696 .gpl_only = false,
697 .ret_type = RET_INTEGER,
698 .arg1_type = ARG_CONST_MAP_PTR,
699 .arg2_type = ARG_ANYTHING,
700};
701
Yonghong Song8b401f92019-05-23 14:47:45 -0700702struct send_signal_irq_work {
703 struct irq_work irq_work;
704 struct task_struct *task;
705 u32 sig;
Yonghong Song84829412020-01-14 19:50:02 -0800706 enum pid_type type;
Yonghong Song8b401f92019-05-23 14:47:45 -0700707};
708
709static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
710
711static void do_bpf_send_signal(struct irq_work *entry)
712{
713 struct send_signal_irq_work *work;
714
715 work = container_of(entry, struct send_signal_irq_work, irq_work);
Yonghong Song84829412020-01-14 19:50:02 -0800716 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
Yonghong Song8b401f92019-05-23 14:47:45 -0700717}
718
Yonghong Song84829412020-01-14 19:50:02 -0800719static int bpf_send_signal_common(u32 sig, enum pid_type type)
Yonghong Song8b401f92019-05-23 14:47:45 -0700720{
721 struct send_signal_irq_work *work = NULL;
722
723 /* Similar to bpf_probe_write_user, task needs to be
724 * in a sound condition and kernel memory access be
725 * permitted in order to send signal to the current
726 * task.
727 */
728 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
729 return -EPERM;
730 if (unlikely(uaccess_kernel()))
731 return -EPERM;
732 if (unlikely(!nmi_uaccess_okay()))
733 return -EPERM;
734
Yonghong Song1bc78962020-03-04 11:11:04 -0800735 if (irqs_disabled()) {
Yonghong Songe1afb7022019-05-25 11:57:53 -0700736 /* Do an early check on signal validity. Otherwise,
737 * the error is lost in deferred irq_work.
738 */
739 if (unlikely(!valid_signal(sig)))
740 return -EINVAL;
741
Yonghong Song8b401f92019-05-23 14:47:45 -0700742 work = this_cpu_ptr(&send_signal_work);
Frederic Weisbecker153bedb2019-11-08 17:08:55 +0100743 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY)
Yonghong Song8b401f92019-05-23 14:47:45 -0700744 return -EBUSY;
745
746 /* Add the current task, which is the target of sending signal,
747 * to the irq_work. The current task may change when queued
748 * irq works get executed.
749 */
750 work->task = current;
751 work->sig = sig;
Yonghong Song84829412020-01-14 19:50:02 -0800752 work->type = type;
Yonghong Song8b401f92019-05-23 14:47:45 -0700753 irq_work_queue(&work->irq_work);
754 return 0;
755 }
756
Yonghong Song84829412020-01-14 19:50:02 -0800757 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
758}
759
760BPF_CALL_1(bpf_send_signal, u32, sig)
761{
762 return bpf_send_signal_common(sig, PIDTYPE_TGID);
Yonghong Song8b401f92019-05-23 14:47:45 -0700763}
764
765static const struct bpf_func_proto bpf_send_signal_proto = {
766 .func = bpf_send_signal,
767 .gpl_only = false,
768 .ret_type = RET_INTEGER,
769 .arg1_type = ARG_ANYTHING,
770};
771
Yonghong Song84829412020-01-14 19:50:02 -0800772BPF_CALL_1(bpf_send_signal_thread, u32, sig)
773{
774 return bpf_send_signal_common(sig, PIDTYPE_PID);
775}
776
777static const struct bpf_func_proto bpf_send_signal_thread_proto = {
778 .func = bpf_send_signal_thread,
779 .gpl_only = false,
780 .ret_type = RET_INTEGER,
781 .arg1_type = ARG_ANYTHING,
782};
783
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700784static const struct bpf_func_proto *
785tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700786{
787 switch (func_id) {
788 case BPF_FUNC_map_lookup_elem:
789 return &bpf_map_lookup_elem_proto;
790 case BPF_FUNC_map_update_elem:
791 return &bpf_map_update_elem_proto;
792 case BPF_FUNC_map_delete_elem:
793 return &bpf_map_delete_elem_proto;
Alban Crequy02a8c812019-04-14 18:58:46 +0200794 case BPF_FUNC_map_push_elem:
795 return &bpf_map_push_elem_proto;
796 case BPF_FUNC_map_pop_elem:
797 return &bpf_map_pop_elem_proto;
798 case BPF_FUNC_map_peek_elem:
799 return &bpf_map_peek_elem_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700800 case BPF_FUNC_ktime_get_ns:
801 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700802 case BPF_FUNC_tail_call:
803 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700804 case BPF_FUNC_get_current_pid_tgid:
805 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700806 case BPF_FUNC_get_current_task:
807 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700808 case BPF_FUNC_get_current_uid_gid:
809 return &bpf_get_current_uid_gid_proto;
810 case BPF_FUNC_get_current_comm:
811 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700812 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700813 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700814 case BPF_FUNC_get_smp_processor_id:
815 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200816 case BPF_FUNC_get_numa_node_id:
817 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000818 case BPF_FUNC_perf_event_read:
819 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700820 case BPF_FUNC_probe_write_user:
821 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700822 case BPF_FUNC_current_task_under_cgroup:
823 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700824 case BPF_FUNC_get_prandom_u32:
825 return &bpf_get_prandom_u32_proto;
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100826 case BPF_FUNC_probe_read_user:
827 return &bpf_probe_read_user_proto;
828 case BPF_FUNC_probe_read_kernel:
829 return &bpf_probe_read_kernel_proto;
830 case BPF_FUNC_probe_read:
831 return &bpf_probe_read_compat_proto;
832 case BPF_FUNC_probe_read_user_str:
833 return &bpf_probe_read_user_str_proto;
834 case BPF_FUNC_probe_read_kernel_str:
835 return &bpf_probe_read_kernel_str_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000836 case BPF_FUNC_probe_read_str:
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100837 return &bpf_probe_read_compat_str_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700838#ifdef CONFIG_CGROUPS
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700839 case BPF_FUNC_get_current_cgroup_id:
840 return &bpf_get_current_cgroup_id_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700841#endif
Yonghong Song8b401f92019-05-23 14:47:45 -0700842 case BPF_FUNC_send_signal:
843 return &bpf_send_signal_proto;
Yonghong Song84829412020-01-14 19:50:02 -0800844 case BPF_FUNC_send_signal_thread:
845 return &bpf_send_signal_thread_proto;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700846 default:
847 return NULL;
848 }
849}
850
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700851static const struct bpf_func_proto *
852kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700853{
854 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700855 case BPF_FUNC_perf_event_output:
856 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800857 case BPF_FUNC_get_stackid:
858 return &bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -0700859 case BPF_FUNC_get_stack:
860 return &bpf_get_stack_proto;
Yonghong Song908432c2017-10-05 09:19:20 -0700861 case BPF_FUNC_perf_event_read_value:
862 return &bpf_perf_event_read_value_proto;
Josef Bacik9802d862017-12-11 11:36:48 -0500863#ifdef CONFIG_BPF_KPROBE_OVERRIDE
864 case BPF_FUNC_override_return:
865 return &bpf_override_return_proto;
866#endif
Alexei Starovoitov25415172015-03-25 12:49:20 -0700867 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700868 return tracing_func_proto(func_id, prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700869 }
870}
871
872/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700873static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700874 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700875 struct bpf_insn_access_aux *info)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700876{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700877 if (off < 0 || off >= sizeof(struct pt_regs))
878 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700879 if (type != BPF_READ)
880 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700881 if (off % size != 0)
882 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100883 /*
884 * Assertion for 32 bit to make sure last 8 byte access
885 * (BPF_DW) to the last 4 byte member is disallowed.
886 */
887 if (off + size > sizeof(struct pt_regs))
888 return false;
889
Alexei Starovoitov25415172015-03-25 12:49:20 -0700890 return true;
891}
892
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700893const struct bpf_verifier_ops kprobe_verifier_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700894 .get_func_proto = kprobe_prog_func_proto,
895 .is_valid_access = kprobe_prog_is_valid_access,
896};
897
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700898const struct bpf_prog_ops kprobe_prog_ops = {
899};
900
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200901BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
902 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700903{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200904 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
905
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700906 /*
907 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
908 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200909 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700910 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200911 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700912}
913
914static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
915 .func = bpf_perf_event_output_tp,
916 .gpl_only = true,
917 .ret_type = RET_INTEGER,
918 .arg1_type = ARG_PTR_TO_CTX,
919 .arg2_type = ARG_CONST_MAP_PTR,
920 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800921 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000922 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700923};
924
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200925BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
926 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700927{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200928 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700929
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200930 /*
931 * Same comment as in bpf_perf_event_output_tp(), only that this time
932 * the other helper's function body cannot be inlined due to being
933 * external, thus we need to call raw helper function.
934 */
935 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
936 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700937}
938
939static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
940 .func = bpf_get_stackid_tp,
941 .gpl_only = true,
942 .ret_type = RET_INTEGER,
943 .arg1_type = ARG_PTR_TO_CTX,
944 .arg2_type = ARG_CONST_MAP_PTR,
945 .arg3_type = ARG_ANYTHING,
946};
947
Yonghong Songc195651e2018-04-28 22:28:08 -0700948BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
949 u64, flags)
950{
951 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
952
953 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
954 (unsigned long) size, flags, 0);
955}
956
957static const struct bpf_func_proto bpf_get_stack_proto_tp = {
958 .func = bpf_get_stack_tp,
959 .gpl_only = true,
960 .ret_type = RET_INTEGER,
961 .arg1_type = ARG_PTR_TO_CTX,
962 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
963 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
964 .arg4_type = ARG_ANYTHING,
965};
966
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700967static const struct bpf_func_proto *
968tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700969{
970 switch (func_id) {
971 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700972 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700973 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700974 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700975 case BPF_FUNC_get_stack:
976 return &bpf_get_stack_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700977 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700978 return tracing_func_proto(func_id, prog);
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700979 }
980}
981
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700982static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700983 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700984 struct bpf_insn_access_aux *info)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700985{
986 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
987 return false;
988 if (type != BPF_READ)
989 return false;
990 if (off % size != 0)
991 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100992
993 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700994 return true;
995}
996
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700997const struct bpf_verifier_ops tracepoint_verifier_ops = {
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700998 .get_func_proto = tp_prog_func_proto,
999 .is_valid_access = tp_prog_is_valid_access,
1000};
1001
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001002const struct bpf_prog_ops tracepoint_prog_ops = {
1003};
1004
Yonghong Songf005afe2018-03-20 11:19:17 -07001005BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1006 struct bpf_perf_event_value *, buf, u32, size)
1007{
1008 int err = -EINVAL;
1009
1010 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1011 goto clear;
1012 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1013 &buf->running);
1014 if (unlikely(err))
1015 goto clear;
1016 return 0;
1017clear:
1018 memset(buf, 0, size);
1019 return err;
1020}
1021
1022static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1023 .func = bpf_perf_prog_read_value,
1024 .gpl_only = true,
1025 .ret_type = RET_INTEGER,
1026 .arg1_type = ARG_PTR_TO_CTX,
1027 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1028 .arg3_type = ARG_CONST_SIZE,
1029};
1030
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001031static const struct bpf_func_proto *
1032pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Yonghong Songf005afe2018-03-20 11:19:17 -07001033{
1034 switch (func_id) {
1035 case BPF_FUNC_perf_event_output:
1036 return &bpf_perf_event_output_proto_tp;
1037 case BPF_FUNC_get_stackid:
1038 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -07001039 case BPF_FUNC_get_stack:
1040 return &bpf_get_stack_proto_tp;
Yonghong Songf005afe2018-03-20 11:19:17 -07001041 case BPF_FUNC_perf_prog_read_value:
1042 return &bpf_perf_prog_read_value_proto;
1043 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001044 return tracing_func_proto(func_id, prog);
Yonghong Songf005afe2018-03-20 11:19:17 -07001045 }
1046}
1047
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001048/*
1049 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1050 * to avoid potential recursive reuse issue when/if tracepoints are added
Matt Mullins9594dc32019-06-11 14:53:04 -07001051 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1052 *
1053 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1054 * in normal, irq, and nmi context.
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001055 */
Matt Mullins9594dc32019-06-11 14:53:04 -07001056struct bpf_raw_tp_regs {
1057 struct pt_regs regs[3];
1058};
1059static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1060static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1061static struct pt_regs *get_bpf_raw_tp_regs(void)
1062{
1063 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1064 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1065
1066 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1067 this_cpu_dec(bpf_raw_tp_nest_level);
1068 return ERR_PTR(-EBUSY);
1069 }
1070
1071 return &tp_regs->regs[nest_level - 1];
1072}
1073
1074static void put_bpf_raw_tp_regs(void)
1075{
1076 this_cpu_dec(bpf_raw_tp_nest_level);
1077}
1078
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001079BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1080 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1081{
Matt Mullins9594dc32019-06-11 14:53:04 -07001082 struct pt_regs *regs = get_bpf_raw_tp_regs();
1083 int ret;
1084
1085 if (IS_ERR(regs))
1086 return PTR_ERR(regs);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001087
1088 perf_fetch_caller_regs(regs);
Matt Mullins9594dc32019-06-11 14:53:04 -07001089 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1090
1091 put_bpf_raw_tp_regs();
1092 return ret;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001093}
1094
1095static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1096 .func = bpf_perf_event_output_raw_tp,
1097 .gpl_only = true,
1098 .ret_type = RET_INTEGER,
1099 .arg1_type = ARG_PTR_TO_CTX,
1100 .arg2_type = ARG_CONST_MAP_PTR,
1101 .arg3_type = ARG_ANYTHING,
1102 .arg4_type = ARG_PTR_TO_MEM,
1103 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1104};
1105
Alexei Starovoitova7658e12019-10-15 20:25:04 -07001106extern const struct bpf_func_proto bpf_skb_output_proto;
1107
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001108BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1109 struct bpf_map *, map, u64, flags)
1110{
Matt Mullins9594dc32019-06-11 14:53:04 -07001111 struct pt_regs *regs = get_bpf_raw_tp_regs();
1112 int ret;
1113
1114 if (IS_ERR(regs))
1115 return PTR_ERR(regs);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001116
1117 perf_fetch_caller_regs(regs);
1118 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
Matt Mullins9594dc32019-06-11 14:53:04 -07001119 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1120 flags, 0, 0);
1121 put_bpf_raw_tp_regs();
1122 return ret;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001123}
1124
1125static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1126 .func = bpf_get_stackid_raw_tp,
1127 .gpl_only = true,
1128 .ret_type = RET_INTEGER,
1129 .arg1_type = ARG_PTR_TO_CTX,
1130 .arg2_type = ARG_CONST_MAP_PTR,
1131 .arg3_type = ARG_ANYTHING,
1132};
1133
Yonghong Songc195651e2018-04-28 22:28:08 -07001134BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1135 void *, buf, u32, size, u64, flags)
1136{
Matt Mullins9594dc32019-06-11 14:53:04 -07001137 struct pt_regs *regs = get_bpf_raw_tp_regs();
1138 int ret;
1139
1140 if (IS_ERR(regs))
1141 return PTR_ERR(regs);
Yonghong Songc195651e2018-04-28 22:28:08 -07001142
1143 perf_fetch_caller_regs(regs);
Matt Mullins9594dc32019-06-11 14:53:04 -07001144 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1145 (unsigned long) size, flags, 0);
1146 put_bpf_raw_tp_regs();
1147 return ret;
Yonghong Songc195651e2018-04-28 22:28:08 -07001148}
1149
1150static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1151 .func = bpf_get_stack_raw_tp,
1152 .gpl_only = true,
1153 .ret_type = RET_INTEGER,
1154 .arg1_type = ARG_PTR_TO_CTX,
1155 .arg2_type = ARG_PTR_TO_MEM,
1156 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1157 .arg4_type = ARG_ANYTHING,
1158};
1159
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001160static const struct bpf_func_proto *
1161raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001162{
1163 switch (func_id) {
1164 case BPF_FUNC_perf_event_output:
1165 return &bpf_perf_event_output_proto_raw_tp;
1166 case BPF_FUNC_get_stackid:
1167 return &bpf_get_stackid_proto_raw_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -07001168 case BPF_FUNC_get_stack:
1169 return &bpf_get_stack_proto_raw_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001170 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001171 return tracing_func_proto(func_id, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001172 }
1173}
1174
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001175static const struct bpf_func_proto *
1176tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1177{
1178 switch (func_id) {
1179#ifdef CONFIG_NET
1180 case BPF_FUNC_skb_output:
1181 return &bpf_skb_output_proto;
1182#endif
1183 default:
1184 return raw_tp_prog_func_proto(func_id, prog);
1185 }
1186}
1187
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001188static bool raw_tp_prog_is_valid_access(int off, int size,
1189 enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001190 const struct bpf_prog *prog,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001191 struct bpf_insn_access_aux *info)
1192{
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001193 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001194 return false;
1195 if (type != BPF_READ)
1196 return false;
1197 if (off % size != 0)
1198 return false;
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001199 return true;
1200}
1201
1202static bool tracing_prog_is_valid_access(int off, int size,
1203 enum bpf_access_type type,
1204 const struct bpf_prog *prog,
1205 struct bpf_insn_access_aux *info)
1206{
1207 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1208 return false;
1209 if (type != BPF_READ)
1210 return false;
1211 if (off % size != 0)
1212 return false;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001213 return btf_ctx_access(off, size, type, prog, info);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001214}
1215
1216const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1217 .get_func_proto = raw_tp_prog_func_proto,
1218 .is_valid_access = raw_tp_prog_is_valid_access,
1219};
1220
1221const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1222};
1223
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001224const struct bpf_verifier_ops tracing_verifier_ops = {
1225 .get_func_proto = tracing_prog_func_proto,
1226 .is_valid_access = tracing_prog_is_valid_access,
1227};
1228
1229const struct bpf_prog_ops tracing_prog_ops = {
1230};
1231
Matt Mullins9df1c282019-04-26 11:49:47 -07001232static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1233 enum bpf_access_type type,
1234 const struct bpf_prog *prog,
1235 struct bpf_insn_access_aux *info)
1236{
1237 if (off == 0) {
1238 if (size != sizeof(u64) || type != BPF_READ)
1239 return false;
1240 info->reg_type = PTR_TO_TP_BUFFER;
1241 }
1242 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1243}
1244
1245const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1246 .get_func_proto = raw_tp_prog_func_proto,
1247 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1248};
1249
1250const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1251};
1252
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001253static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001254 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -07001255 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001256{
Teng Qin95da0cd2018-03-06 10:55:01 -08001257 const int size_u64 = sizeof(u64);
Yonghong Song31fd8582017-06-13 15:52:13 -07001258
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001259 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1260 return false;
1261 if (type != BPF_READ)
1262 return false;
Daniel Borkmannbc231052018-06-02 23:06:39 +02001263 if (off % size != 0) {
1264 if (sizeof(unsigned long) != 4)
1265 return false;
1266 if (size != 8)
1267 return false;
1268 if (off % size != 4)
1269 return false;
1270 }
Yonghong Song31fd8582017-06-13 15:52:13 -07001271
Daniel Borkmannf96da092017-07-02 02:13:27 +02001272 switch (off) {
1273 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
Teng Qin95da0cd2018-03-06 10:55:01 -08001274 bpf_ctx_record_field_size(info, size_u64);
1275 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1276 return false;
1277 break;
1278 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1279 bpf_ctx_record_field_size(info, size_u64);
1280 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
Yonghong Song23994632017-06-22 15:07:39 -07001281 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +02001282 break;
1283 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001284 if (size != sizeof(long))
1285 return false;
1286 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02001287
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001288 return true;
1289}
1290
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001291static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1292 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001293 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +02001294 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001295{
1296 struct bpf_insn *insn = insn_buf;
1297
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001298 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001299 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +02001300 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001301 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001302 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001303 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02001304 bpf_target_off(struct perf_sample_data, period, 8,
1305 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001306 break;
Teng Qin95da0cd2018-03-06 10:55:01 -08001307 case offsetof(struct bpf_perf_event_data, addr):
1308 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1309 data), si->dst_reg, si->src_reg,
1310 offsetof(struct bpf_perf_event_data_kern, data));
1311 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1312 bpf_target_off(struct perf_sample_data, addr, 8,
1313 target_size));
1314 break;
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001315 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +02001316 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001317 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001318 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001319 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1320 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001321 break;
1322 }
1323
1324 return insn - insn_buf;
1325}
1326
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001327const struct bpf_verifier_ops perf_event_verifier_ops = {
Yonghong Songf005afe2018-03-20 11:19:17 -07001328 .get_func_proto = pe_prog_func_proto,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001329 .is_valid_access = pe_prog_is_valid_access,
1330 .convert_ctx_access = pe_prog_convert_ctx_access,
1331};
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001332
1333const struct bpf_prog_ops perf_event_prog_ops = {
1334};
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001335
1336static DEFINE_MUTEX(bpf_event_mutex);
1337
Yonghong Songc8c088b2017-11-30 13:47:54 -08001338#define BPF_TRACE_MAX_PROGS 64
1339
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001340int perf_event_attach_bpf_prog(struct perf_event *event,
1341 struct bpf_prog *prog)
1342{
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001343 struct bpf_prog_array *old_array;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001344 struct bpf_prog_array *new_array;
1345 int ret = -EEXIST;
1346
Josef Bacik9802d862017-12-11 11:36:48 -05001347 /*
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001348 * Kprobe override only works if they are on the function entry,
1349 * and only if they are on the opt-in list.
Josef Bacik9802d862017-12-11 11:36:48 -05001350 */
1351 if (prog->kprobe_override &&
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001352 (!trace_kprobe_on_func_entry(event->tp_event) ||
Josef Bacik9802d862017-12-11 11:36:48 -05001353 !trace_kprobe_error_injectable(event->tp_event)))
1354 return -EINVAL;
1355
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001356 mutex_lock(&bpf_event_mutex);
1357
1358 if (event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001359 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001360
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001361 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
Yonghong Songc8c088b2017-11-30 13:47:54 -08001362 if (old_array &&
1363 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1364 ret = -E2BIG;
1365 goto unlock;
1366 }
1367
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001368 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1369 if (ret < 0)
Yonghong Song07c41a22017-10-30 13:50:22 -07001370 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001371
1372 /* set the new array to event->tp_event and set event->prog */
1373 event->prog = prog;
1374 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1375 bpf_prog_array_free(old_array);
1376
Yonghong Song07c41a22017-10-30 13:50:22 -07001377unlock:
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001378 mutex_unlock(&bpf_event_mutex);
1379 return ret;
1380}
1381
1382void perf_event_detach_bpf_prog(struct perf_event *event)
1383{
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001384 struct bpf_prog_array *old_array;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001385 struct bpf_prog_array *new_array;
1386 int ret;
1387
1388 mutex_lock(&bpf_event_mutex);
1389
1390 if (!event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001391 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001392
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001393 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001394 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
Sean Young170a7e32018-05-27 12:24:08 +01001395 if (ret == -ENOENT)
1396 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001397 if (ret < 0) {
1398 bpf_prog_array_delete_safe(old_array, event->prog);
1399 } else {
1400 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1401 bpf_prog_array_free(old_array);
1402 }
1403
1404 bpf_prog_put(event->prog);
1405 event->prog = NULL;
1406
Yonghong Song07c41a22017-10-30 13:50:22 -07001407unlock:
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001408 mutex_unlock(&bpf_event_mutex);
1409}
Yonghong Songf371b302017-12-11 11:39:02 -08001410
Yonghong Songf4e22982017-12-13 10:35:37 -08001411int perf_event_query_prog_array(struct perf_event *event, void __user *info)
Yonghong Songf371b302017-12-11 11:39:02 -08001412{
1413 struct perf_event_query_bpf __user *uquery = info;
1414 struct perf_event_query_bpf query = {};
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001415 struct bpf_prog_array *progs;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001416 u32 *ids, prog_cnt, ids_len;
Yonghong Songf371b302017-12-11 11:39:02 -08001417 int ret;
1418
1419 if (!capable(CAP_SYS_ADMIN))
1420 return -EPERM;
1421 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1422 return -EINVAL;
1423 if (copy_from_user(&query, uquery, sizeof(query)))
1424 return -EFAULT;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001425
1426 ids_len = query.ids_len;
1427 if (ids_len > BPF_TRACE_MAX_PROGS)
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001428 return -E2BIG;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001429 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1430 if (!ids)
1431 return -ENOMEM;
1432 /*
1433 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1434 * is required when user only wants to check for uquery->prog_cnt.
1435 * There is no need to check for it since the case is handled
1436 * gracefully in bpf_prog_array_copy_info.
1437 */
Yonghong Songf371b302017-12-11 11:39:02 -08001438
1439 mutex_lock(&bpf_event_mutex);
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001440 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
1441 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
Yonghong Songf371b302017-12-11 11:39:02 -08001442 mutex_unlock(&bpf_event_mutex);
1443
Yonghong Song3a38bb92018-04-10 09:37:32 -07001444 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1445 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1446 ret = -EFAULT;
1447
1448 kfree(ids);
Yonghong Songf371b302017-12-11 11:39:02 -08001449 return ret;
1450}
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001451
1452extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1453extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1454
Matt Mullinsa38d1102018-12-12 16:42:37 -08001455struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001456{
1457 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1458
1459 for (; btp < __stop__bpf_raw_tp; btp++) {
1460 if (!strcmp(btp->tp->name, name))
1461 return btp;
1462 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001463
1464 return bpf_get_raw_tracepoint_module(name);
1465}
1466
1467void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
1468{
1469 struct module *mod = __module_address((unsigned long)btp);
1470
1471 if (mod)
1472 module_put(mod);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001473}
1474
1475static __always_inline
1476void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1477{
1478 rcu_read_lock();
1479 preempt_disable();
1480 (void) BPF_PROG_RUN(prog, args);
1481 preempt_enable();
1482 rcu_read_unlock();
1483}
1484
1485#define UNPACK(...) __VA_ARGS__
1486#define REPEAT_1(FN, DL, X, ...) FN(X)
1487#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1488#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1489#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1490#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1491#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1492#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1493#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1494#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1495#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1496#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1497#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1498#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1499
1500#define SARG(X) u64 arg##X
1501#define COPY(X) args[X] = arg##X
1502
1503#define __DL_COM (,)
1504#define __DL_SEM (;)
1505
1506#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1507
1508#define BPF_TRACE_DEFN_x(x) \
1509 void bpf_trace_run##x(struct bpf_prog *prog, \
1510 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1511 { \
1512 u64 args[x]; \
1513 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1514 __bpf_trace_run(prog, args); \
1515 } \
1516 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1517BPF_TRACE_DEFN_x(1);
1518BPF_TRACE_DEFN_x(2);
1519BPF_TRACE_DEFN_x(3);
1520BPF_TRACE_DEFN_x(4);
1521BPF_TRACE_DEFN_x(5);
1522BPF_TRACE_DEFN_x(6);
1523BPF_TRACE_DEFN_x(7);
1524BPF_TRACE_DEFN_x(8);
1525BPF_TRACE_DEFN_x(9);
1526BPF_TRACE_DEFN_x(10);
1527BPF_TRACE_DEFN_x(11);
1528BPF_TRACE_DEFN_x(12);
1529
1530static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1531{
1532 struct tracepoint *tp = btp->tp;
1533
1534 /*
1535 * check that program doesn't access arguments beyond what's
1536 * available in this tracepoint
1537 */
1538 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1539 return -EINVAL;
1540
Matt Mullins9df1c282019-04-26 11:49:47 -07001541 if (prog->aux->max_tp_access > btp->writable_size)
1542 return -EINVAL;
1543
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001544 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1545}
1546
1547int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1548{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08001549 return __bpf_probe_register(btp, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001550}
1551
1552int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1553{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08001554 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001555}
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001556
1557int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1558 u32 *fd_type, const char **buf,
1559 u64 *probe_offset, u64 *probe_addr)
1560{
1561 bool is_tracepoint, is_syscall_tp;
1562 struct bpf_prog *prog;
1563 int flags, err = 0;
1564
1565 prog = event->prog;
1566 if (!prog)
1567 return -ENOENT;
1568
1569 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1570 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1571 return -EOPNOTSUPP;
1572
1573 *prog_id = prog->aux->id;
1574 flags = event->tp_event->flags;
1575 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1576 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1577
1578 if (is_tracepoint || is_syscall_tp) {
1579 *buf = is_tracepoint ? event->tp_event->tp->name
1580 : event->tp_event->name;
1581 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1582 *probe_offset = 0x0;
1583 *probe_addr = 0x0;
1584 } else {
1585 /* kprobe/uprobe */
1586 err = -EOPNOTSUPP;
1587#ifdef CONFIG_KPROBE_EVENTS
1588 if (flags & TRACE_EVENT_FL_KPROBE)
1589 err = bpf_get_kprobe_info(event, fd_type, buf,
1590 probe_offset, probe_addr,
1591 event->attr.type == PERF_TYPE_TRACEPOINT);
1592#endif
1593#ifdef CONFIG_UPROBE_EVENTS
1594 if (flags & TRACE_EVENT_FL_UPROBE)
1595 err = bpf_get_uprobe_info(event, fd_type, buf,
1596 probe_offset,
1597 event->attr.type == PERF_TYPE_TRACEPOINT);
1598#endif
1599 }
1600
1601 return err;
1602}
Matt Mullinsa38d1102018-12-12 16:42:37 -08001603
Yonghong Song9db1ff02019-06-25 17:35:03 -07001604static int __init send_signal_irq_work_init(void)
1605{
1606 int cpu;
1607 struct send_signal_irq_work *work;
1608
1609 for_each_possible_cpu(cpu) {
1610 work = per_cpu_ptr(&send_signal_work, cpu);
1611 init_irq_work(&work->irq_work, do_bpf_send_signal);
1612 }
1613 return 0;
1614}
1615
1616subsys_initcall(send_signal_irq_work_init);
1617
Matt Mullinsa38d1102018-12-12 16:42:37 -08001618#ifdef CONFIG_MODULES
Stanislav Fomichev390e99c2019-05-13 12:04:36 -07001619static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
1620 void *module)
Matt Mullinsa38d1102018-12-12 16:42:37 -08001621{
1622 struct bpf_trace_module *btm, *tmp;
1623 struct module *mod = module;
1624
1625 if (mod->num_bpf_raw_events == 0 ||
1626 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
1627 return 0;
1628
1629 mutex_lock(&bpf_module_mutex);
1630
1631 switch (op) {
1632 case MODULE_STATE_COMING:
1633 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
1634 if (btm) {
1635 btm->module = module;
1636 list_add(&btm->list, &bpf_trace_modules);
1637 }
1638 break;
1639 case MODULE_STATE_GOING:
1640 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
1641 if (btm->module == module) {
1642 list_del(&btm->list);
1643 kfree(btm);
1644 break;
1645 }
1646 }
1647 break;
1648 }
1649
1650 mutex_unlock(&bpf_module_mutex);
1651
1652 return 0;
1653}
1654
1655static struct notifier_block bpf_module_nb = {
1656 .notifier_call = bpf_event_notify,
1657};
1658
Stanislav Fomichev390e99c2019-05-13 12:04:36 -07001659static int __init bpf_event_init(void)
Matt Mullinsa38d1102018-12-12 16:42:37 -08001660{
1661 register_module_notifier(&bpf_module_nb);
1662 return 0;
1663}
1664
1665fs_initcall(bpf_event_init);
1666#endif /* CONFIG_MODULES */