blob: b5071c7e93ca9604309d8495f5787d930bda26dd [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
Thomas Gleixnerb0a81b92020-02-24 15:01:37 +010086 cant_sleep();
Alexei Starovoitov25415172015-03-25 12:49:20 -070087
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);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700118
119 return ret;
120}
Alexei Starovoitov25415172015-03-25 12:49:20 -0700121
Josef Bacik9802d862017-12-11 11:36:48 -0500122#ifdef CONFIG_BPF_KPROBE_OVERRIDE
123BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
124{
Josef Bacik9802d862017-12-11 11:36:48 -0500125 regs_set_return_value(regs, rc);
Masami Hiramatsu540adea2018-01-13 02:55:03 +0900126 override_function_with_return(regs);
Josef Bacik9802d862017-12-11 11:36:48 -0500127 return 0;
128}
129
130static const struct bpf_func_proto bpf_override_return_proto = {
131 .func = bpf_override_return,
132 .gpl_only = true,
133 .ret_type = RET_INTEGER,
134 .arg1_type = ARG_PTR_TO_CTX,
135 .arg2_type = ARG_ANYTHING,
136};
137#endif
138
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100139BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
140 const void __user *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700141{
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100142 int ret = probe_user_read(dst, unsafe_ptr, size);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700143
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200144 if (unlikely(ret < 0))
145 memset(dst, 0, size);
146
147 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700148}
149
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100150static const struct bpf_func_proto bpf_probe_read_user_proto = {
151 .func = bpf_probe_read_user,
152 .gpl_only = true,
153 .ret_type = RET_INTEGER,
154 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
155 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
156 .arg3_type = ARG_ANYTHING,
157};
158
159BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
160 const void __user *, unsafe_ptr)
161{
162 int ret = strncpy_from_unsafe_user(dst, unsafe_ptr, size);
163
164 if (unlikely(ret < 0))
165 memset(dst, 0, size);
166
167 return ret;
168}
169
170static const struct bpf_func_proto bpf_probe_read_user_str_proto = {
171 .func = bpf_probe_read_user_str,
172 .gpl_only = true,
173 .ret_type = RET_INTEGER,
174 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
175 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
176 .arg3_type = ARG_ANYTHING,
177};
178
179static __always_inline int
180bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr,
181 const bool compat)
182{
183 int ret = security_locked_down(LOCKDOWN_BPF_READ);
184
185 if (unlikely(ret < 0))
186 goto out;
187 ret = compat ? probe_kernel_read(dst, unsafe_ptr, size) :
188 probe_kernel_read_strict(dst, unsafe_ptr, size);
189 if (unlikely(ret < 0))
190out:
191 memset(dst, 0, size);
192 return ret;
193}
194
195BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
196 const void *, unsafe_ptr)
197{
198 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false);
199}
200
201static const struct bpf_func_proto bpf_probe_read_kernel_proto = {
202 .func = bpf_probe_read_kernel,
203 .gpl_only = true,
204 .ret_type = RET_INTEGER,
205 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
206 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
207 .arg3_type = ARG_ANYTHING,
208};
209
210BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
211 const void *, unsafe_ptr)
212{
213 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, true);
214}
215
216static const struct bpf_func_proto bpf_probe_read_compat_proto = {
217 .func = bpf_probe_read_compat,
218 .gpl_only = true,
219 .ret_type = RET_INTEGER,
220 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
221 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
222 .arg3_type = ARG_ANYTHING,
223};
224
225static __always_inline int
226bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,
227 const bool compat)
228{
229 int ret = security_locked_down(LOCKDOWN_BPF_READ);
230
231 if (unlikely(ret < 0))
232 goto out;
233 /*
234 * The strncpy_from_unsafe_*() call will likely not fill the entire
235 * buffer, but that's okay in this circumstance as we're probing
236 * arbitrary memory anyway similar to bpf_probe_read_*() and might
237 * as well probe the stack. Thus, memory is explicitly cleared
238 * only in error case, so that improper users ignoring return
239 * code altogether don't copy garbage; otherwise length of string
240 * is returned that can be used for bpf_perf_event_output() et al.
241 */
242 ret = compat ? strncpy_from_unsafe(dst, unsafe_ptr, size) :
243 strncpy_from_unsafe_strict(dst, unsafe_ptr, size);
244 if (unlikely(ret < 0))
245out:
246 memset(dst, 0, size);
247 return ret;
248}
249
250BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
251 const void *, unsafe_ptr)
252{
253 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false);
254}
255
256static const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
257 .func = bpf_probe_read_kernel_str,
258 .gpl_only = true,
259 .ret_type = RET_INTEGER,
260 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
261 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
262 .arg3_type = ARG_ANYTHING,
263};
264
265BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
266 const void *, unsafe_ptr)
267{
268 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, true);
269}
270
271static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
272 .func = bpf_probe_read_compat_str,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700273 .gpl_only = true,
274 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800275 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Yonghong Song9c019e22017-11-12 14:49:10 -0800276 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700277 .arg3_type = ARG_ANYTHING,
278};
279
Daniel Borkmanneb1b6682019-11-02 00:17:58 +0100280BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200281 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700282{
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700283 /*
284 * Ensure we're in user context which is safe for the helper to
285 * run. This helper has no business in a kthread.
286 *
287 * access_ok() should prevent writing to non-user memory, but in
288 * some situations (nommu, temporary switch, etc) access_ok() does
289 * not provide enough validation, hence the check on KERNEL_DS.
Nadav Amitc7b6f292019-04-25 17:11:43 -0700290 *
291 * nmi_uaccess_okay() ensures the probe is not run in an interim
292 * state, when the task or mm are switched. This is specifically
293 * required to prevent the use of temporary mm.
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700294 */
295
296 if (unlikely(in_interrupt() ||
297 current->flags & (PF_KTHREAD | PF_EXITING)))
298 return -EPERM;
Al Virodb68ce12017-03-20 21:08:07 -0400299 if (unlikely(uaccess_kernel()))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700300 return -EPERM;
Nadav Amitc7b6f292019-04-25 17:11:43 -0700301 if (unlikely(!nmi_uaccess_okay()))
302 return -EPERM;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700303
Daniel Borkmanneb1b6682019-11-02 00:17:58 +0100304 return probe_user_write(unsafe_ptr, src, size);
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700305}
306
307static const struct bpf_func_proto bpf_probe_write_user_proto = {
308 .func = bpf_probe_write_user,
309 .gpl_only = true,
310 .ret_type = RET_INTEGER,
311 .arg1_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800312 .arg2_type = ARG_PTR_TO_MEM,
313 .arg3_type = ARG_CONST_SIZE,
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700314};
315
316static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
317{
318 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
319 current->comm, task_pid_nr(current));
320
321 return &bpf_probe_write_user_proto;
322}
323
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700324/*
John Fastabend7bda4b42017-07-02 02:13:29 +0200325 * Only limited trace_printk() conversion specifiers allowed:
326 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700327 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200328BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
329 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700330{
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700331 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700332 int mod[3] = {};
333 int fmt_cnt = 0;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700334 u64 unsafe_addr;
335 char buf[64];
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700336 int i;
337
338 /*
339 * bpf_check()->check_func_arg()->check_stack_boundary()
340 * guarantees that fmt points to bpf program stack,
341 * fmt_size bytes of it were initialized and fmt_size > 0
342 */
343 if (fmt[--fmt_size] != 0)
344 return -EINVAL;
345
346 /* check format string for allowed specifiers */
347 for (i = 0; i < fmt_size; i++) {
348 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
349 return -EINVAL;
350
351 if (fmt[i] != '%')
352 continue;
353
354 if (fmt_cnt >= 3)
355 return -EINVAL;
356
357 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
358 i++;
359 if (fmt[i] == 'l') {
360 mod[fmt_cnt]++;
361 i++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700362 } else if (fmt[i] == 'p' || fmt[i] == 's') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700363 mod[fmt_cnt]++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100364 /* disallow any further format extensions */
365 if (fmt[i + 1] != 0 &&
366 !isspace(fmt[i + 1]) &&
367 !ispunct(fmt[i + 1]))
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700368 return -EINVAL;
369 fmt_cnt++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100370 if (fmt[i] == 's') {
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700371 if (str_seen)
372 /* allow only one '%s' per fmt string */
373 return -EINVAL;
374 str_seen = true;
375
376 switch (fmt_cnt) {
377 case 1:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200378 unsafe_addr = arg1;
379 arg1 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700380 break;
381 case 2:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200382 unsafe_addr = arg2;
383 arg2 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700384 break;
385 case 3:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200386 unsafe_addr = arg3;
387 arg3 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700388 break;
389 }
390 buf[0] = 0;
391 strncpy_from_unsafe(buf,
392 (void *) (long) unsafe_addr,
393 sizeof(buf));
394 }
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700395 continue;
396 }
397
398 if (fmt[i] == 'l') {
399 mod[fmt_cnt]++;
400 i++;
401 }
402
John Fastabend7bda4b42017-07-02 02:13:29 +0200403 if (fmt[i] != 'i' && fmt[i] != 'd' &&
404 fmt[i] != 'u' && fmt[i] != 'x')
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700405 return -EINVAL;
406 fmt_cnt++;
407 }
408
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200409/* Horrid workaround for getting va_list handling working with different
410 * argument type combinations generically for 32 and 64 bit archs.
411 */
412#define __BPF_TP_EMIT() __BPF_ARG3_TP()
413#define __BPF_TP(...) \
Yonghong Songeefa864a2018-01-17 09:19:32 -0800414 __trace_printk(0 /* Fake ip */, \
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200415 fmt, ##__VA_ARGS__)
416
417#define __BPF_ARG1_TP(...) \
418 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
419 ? __BPF_TP(arg1, ##__VA_ARGS__) \
420 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
421 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
422 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
423
424#define __BPF_ARG2_TP(...) \
425 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
426 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
427 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
428 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
429 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
430
431#define __BPF_ARG3_TP(...) \
432 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
433 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
434 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
435 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
436 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
437
438 return __BPF_TP_EMIT();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700439}
440
441static const struct bpf_func_proto bpf_trace_printk_proto = {
442 .func = bpf_trace_printk,
443 .gpl_only = true,
444 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800445 .arg1_type = ARG_PTR_TO_MEM,
446 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700447};
448
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700449const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
450{
451 /*
452 * this program might be calling bpf_trace_printk,
453 * so allocate per-cpu printk buffers
454 */
455 trace_printk_init_buffers();
456
457 return &bpf_trace_printk_proto;
458}
459
Yonghong Song908432c2017-10-05 09:19:20 -0700460static __always_inline int
461get_map_perf_counter(struct bpf_map *map, u64 flags,
462 u64 *value, u64 *enabled, u64 *running)
Kaixu Xia35578d72015-08-06 07:02:35 +0000463{
Kaixu Xia35578d72015-08-06 07:02:35 +0000464 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200465 unsigned int cpu = smp_processor_id();
466 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200467 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000468
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200469 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
470 return -EINVAL;
471 if (index == BPF_F_CURRENT_CPU)
472 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000473 if (unlikely(index >= array->map.max_entries))
474 return -E2BIG;
475
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200476 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200477 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000478 return -ENOENT;
479
Yonghong Song908432c2017-10-05 09:19:20 -0700480 return perf_event_read_local(ee->event, value, enabled, running);
481}
482
483BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
484{
485 u64 value = 0;
486 int err;
487
488 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
Kaixu Xia35578d72015-08-06 07:02:35 +0000489 /*
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700490 * this api is ugly since we miss [-22..-2] range of valid
491 * counter values, but that's uapi
Kaixu Xia35578d72015-08-06 07:02:35 +0000492 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700493 if (err)
494 return err;
495 return value;
Kaixu Xia35578d72015-08-06 07:02:35 +0000496}
497
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700498static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000499 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700500 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000501 .ret_type = RET_INTEGER,
502 .arg1_type = ARG_CONST_MAP_PTR,
503 .arg2_type = ARG_ANYTHING,
504};
505
Yonghong Song908432c2017-10-05 09:19:20 -0700506BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
507 struct bpf_perf_event_value *, buf, u32, size)
508{
509 int err = -EINVAL;
510
511 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
512 goto clear;
513 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
514 &buf->running);
515 if (unlikely(err))
516 goto clear;
517 return 0;
518clear:
519 memset(buf, 0, size);
520 return err;
521}
522
523static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
524 .func = bpf_perf_event_read_value,
525 .gpl_only = true,
526 .ret_type = RET_INTEGER,
527 .arg1_type = ARG_CONST_MAP_PTR,
528 .arg2_type = ARG_ANYTHING,
529 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
530 .arg4_type = ARG_CONST_SIZE,
531};
532
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200533static __always_inline u64
534__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
Daniel Borkmann283ca522017-12-12 02:25:30 +0100535 u64 flags, struct perf_sample_data *sd)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700536{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700537 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200538 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200539 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200540 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700541 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700542
Daniel Borkmann1e337592016-04-18 21:01:23 +0200543 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200544 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700545 if (unlikely(index >= array->map.max_entries))
546 return -E2BIG;
547
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200548 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200549 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700550 return -ENOENT;
551
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200552 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700553 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
554 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
555 return -EINVAL;
556
Daniel Borkmannd7931332016-06-28 12:18:24 +0200557 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700558 return -EOPNOTSUPP;
559
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -0300560 return perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700561}
562
Matt Mullins9594dc32019-06-11 14:53:04 -0700563/*
564 * Support executing tracepoints in normal, irq, and nmi context that each call
565 * bpf_perf_event_output
566 */
567struct bpf_trace_sample_data {
568 struct perf_sample_data sds[3];
569};
570
571static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
572static DEFINE_PER_CPU(int, bpf_trace_nest_level);
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200573BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
574 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200575{
Matt Mullins9594dc32019-06-11 14:53:04 -0700576 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
577 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200578 struct perf_raw_record raw = {
579 .frag = {
580 .size = size,
581 .data = data,
582 },
583 };
Matt Mullins9594dc32019-06-11 14:53:04 -0700584 struct perf_sample_data *sd;
585 int err;
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200586
Matt Mullins9594dc32019-06-11 14:53:04 -0700587 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
588 err = -EBUSY;
589 goto out;
590 }
591
592 sd = &sds->sds[nest_level - 1];
593
594 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
595 err = -EINVAL;
596 goto out;
597 }
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200598
Daniel Borkmann283ca522017-12-12 02:25:30 +0100599 perf_sample_data_init(sd, 0, 0);
600 sd->raw = &raw;
601
Matt Mullins9594dc32019-06-11 14:53:04 -0700602 err = __bpf_perf_event_output(regs, map, flags, sd);
603
604out:
605 this_cpu_dec(bpf_trace_nest_level);
606 return err;
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200607}
608
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700609static const struct bpf_func_proto bpf_perf_event_output_proto = {
610 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700611 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700612 .ret_type = RET_INTEGER,
613 .arg1_type = ARG_PTR_TO_CTX,
614 .arg2_type = ARG_CONST_MAP_PTR,
615 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800616 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000617 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700618};
619
Allan Zhang768fb612019-09-25 16:43:12 -0700620static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
621struct bpf_nested_pt_regs {
622 struct pt_regs regs[3];
623};
624static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
625static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200626
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200627u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
628 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200629{
Allan Zhang768fb612019-09-25 16:43:12 -0700630 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200631 struct perf_raw_frag frag = {
632 .copy = ctx_copy,
633 .size = ctx_size,
634 .data = ctx,
635 };
636 struct perf_raw_record raw = {
637 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700638 {
639 .next = ctx_size ? &frag : NULL,
640 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200641 .size = meta_size,
642 .data = meta,
643 },
644 };
Allan Zhang768fb612019-09-25 16:43:12 -0700645 struct perf_sample_data *sd;
646 struct pt_regs *regs;
647 u64 ret;
648
649 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
650 ret = -EBUSY;
651 goto out;
652 }
653 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
654 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200655
656 perf_fetch_caller_regs(regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100657 perf_sample_data_init(sd, 0, 0);
658 sd->raw = &raw;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200659
Allan Zhang768fb612019-09-25 16:43:12 -0700660 ret = __bpf_perf_event_output(regs, map, flags, sd);
661out:
662 this_cpu_dec(bpf_event_output_nest_level);
663 return ret;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200664}
665
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200666BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700667{
668 return (long) current;
669}
670
671static const struct bpf_func_proto bpf_get_current_task_proto = {
672 .func = bpf_get_current_task,
673 .gpl_only = true,
674 .ret_type = RET_INTEGER,
675};
676
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200677BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700678{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700679 struct bpf_array *array = container_of(map, struct bpf_array, map);
680 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700681
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700682 if (unlikely(idx >= array->map.max_entries))
683 return -E2BIG;
684
685 cgrp = READ_ONCE(array->ptrs[idx]);
686 if (unlikely(!cgrp))
687 return -EAGAIN;
688
689 return task_under_cgroup_hierarchy(current, cgrp);
690}
691
692static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
693 .func = bpf_current_task_under_cgroup,
694 .gpl_only = false,
695 .ret_type = RET_INTEGER,
696 .arg1_type = ARG_CONST_MAP_PTR,
697 .arg2_type = ARG_ANYTHING,
698};
699
Yonghong Song8b401f92019-05-23 14:47:45 -0700700struct send_signal_irq_work {
701 struct irq_work irq_work;
702 struct task_struct *task;
703 u32 sig;
Yonghong Song84829412020-01-14 19:50:02 -0800704 enum pid_type type;
Yonghong Song8b401f92019-05-23 14:47:45 -0700705};
706
707static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
708
709static void do_bpf_send_signal(struct irq_work *entry)
710{
711 struct send_signal_irq_work *work;
712
713 work = container_of(entry, struct send_signal_irq_work, irq_work);
Yonghong Song84829412020-01-14 19:50:02 -0800714 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
Yonghong Song8b401f92019-05-23 14:47:45 -0700715}
716
Yonghong Song84829412020-01-14 19:50:02 -0800717static int bpf_send_signal_common(u32 sig, enum pid_type type)
Yonghong Song8b401f92019-05-23 14:47:45 -0700718{
719 struct send_signal_irq_work *work = NULL;
720
721 /* Similar to bpf_probe_write_user, task needs to be
722 * in a sound condition and kernel memory access be
723 * permitted in order to send signal to the current
724 * task.
725 */
726 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
727 return -EPERM;
728 if (unlikely(uaccess_kernel()))
729 return -EPERM;
730 if (unlikely(!nmi_uaccess_okay()))
731 return -EPERM;
732
733 if (in_nmi()) {
Yonghong Songe1afb7022019-05-25 11:57:53 -0700734 /* Do an early check on signal validity. Otherwise,
735 * the error is lost in deferred irq_work.
736 */
737 if (unlikely(!valid_signal(sig)))
738 return -EINVAL;
739
Yonghong Song8b401f92019-05-23 14:47:45 -0700740 work = this_cpu_ptr(&send_signal_work);
Frederic Weisbecker153bedb2019-11-08 17:08:55 +0100741 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY)
Yonghong Song8b401f92019-05-23 14:47:45 -0700742 return -EBUSY;
743
744 /* Add the current task, which is the target of sending signal,
745 * to the irq_work. The current task may change when queued
746 * irq works get executed.
747 */
748 work->task = current;
749 work->sig = sig;
Yonghong Song84829412020-01-14 19:50:02 -0800750 work->type = type;
Yonghong Song8b401f92019-05-23 14:47:45 -0700751 irq_work_queue(&work->irq_work);
752 return 0;
753 }
754
Yonghong Song84829412020-01-14 19:50:02 -0800755 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
756}
757
758BPF_CALL_1(bpf_send_signal, u32, sig)
759{
760 return bpf_send_signal_common(sig, PIDTYPE_TGID);
Yonghong Song8b401f92019-05-23 14:47:45 -0700761}
762
763static const struct bpf_func_proto bpf_send_signal_proto = {
764 .func = bpf_send_signal,
765 .gpl_only = false,
766 .ret_type = RET_INTEGER,
767 .arg1_type = ARG_ANYTHING,
768};
769
Yonghong Song84829412020-01-14 19:50:02 -0800770BPF_CALL_1(bpf_send_signal_thread, u32, sig)
771{
772 return bpf_send_signal_common(sig, PIDTYPE_PID);
773}
774
775static const struct bpf_func_proto bpf_send_signal_thread_proto = {
776 .func = bpf_send_signal_thread,
777 .gpl_only = false,
778 .ret_type = RET_INTEGER,
779 .arg1_type = ARG_ANYTHING,
780};
781
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700782static const struct bpf_func_proto *
783tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700784{
785 switch (func_id) {
786 case BPF_FUNC_map_lookup_elem:
787 return &bpf_map_lookup_elem_proto;
788 case BPF_FUNC_map_update_elem:
789 return &bpf_map_update_elem_proto;
790 case BPF_FUNC_map_delete_elem:
791 return &bpf_map_delete_elem_proto;
Alban Crequy02a8c812019-04-14 18:58:46 +0200792 case BPF_FUNC_map_push_elem:
793 return &bpf_map_push_elem_proto;
794 case BPF_FUNC_map_pop_elem:
795 return &bpf_map_pop_elem_proto;
796 case BPF_FUNC_map_peek_elem:
797 return &bpf_map_peek_elem_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700798 case BPF_FUNC_ktime_get_ns:
799 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700800 case BPF_FUNC_tail_call:
801 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700802 case BPF_FUNC_get_current_pid_tgid:
803 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700804 case BPF_FUNC_get_current_task:
805 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700806 case BPF_FUNC_get_current_uid_gid:
807 return &bpf_get_current_uid_gid_proto;
808 case BPF_FUNC_get_current_comm:
809 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700810 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700811 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700812 case BPF_FUNC_get_smp_processor_id:
813 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200814 case BPF_FUNC_get_numa_node_id:
815 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000816 case BPF_FUNC_perf_event_read:
817 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700818 case BPF_FUNC_probe_write_user:
819 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700820 case BPF_FUNC_current_task_under_cgroup:
821 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700822 case BPF_FUNC_get_prandom_u32:
823 return &bpf_get_prandom_u32_proto;
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100824 case BPF_FUNC_probe_read_user:
825 return &bpf_probe_read_user_proto;
826 case BPF_FUNC_probe_read_kernel:
827 return &bpf_probe_read_kernel_proto;
828 case BPF_FUNC_probe_read:
829 return &bpf_probe_read_compat_proto;
830 case BPF_FUNC_probe_read_user_str:
831 return &bpf_probe_read_user_str_proto;
832 case BPF_FUNC_probe_read_kernel_str:
833 return &bpf_probe_read_kernel_str_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000834 case BPF_FUNC_probe_read_str:
Daniel Borkmann6ae08ae2019-11-02 00:17:59 +0100835 return &bpf_probe_read_compat_str_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700836#ifdef CONFIG_CGROUPS
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700837 case BPF_FUNC_get_current_cgroup_id:
838 return &bpf_get_current_cgroup_id_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700839#endif
Yonghong Song8b401f92019-05-23 14:47:45 -0700840 case BPF_FUNC_send_signal:
841 return &bpf_send_signal_proto;
Yonghong Song84829412020-01-14 19:50:02 -0800842 case BPF_FUNC_send_signal_thread:
843 return &bpf_send_signal_thread_proto;
Song Liub80b0332020-02-14 15:41:46 -0800844 case BPF_FUNC_perf_event_read_value:
845 return &bpf_perf_event_read_value_proto;
Carlos Neirab4490c52020-03-04 17:41:56 -0300846 case BPF_FUNC_get_ns_current_pid_tgid:
847 return &bpf_get_ns_current_pid_tgid_proto;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700848 default:
849 return NULL;
850 }
851}
852
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700853static const struct bpf_func_proto *
854kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700855{
856 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700857 case BPF_FUNC_perf_event_output:
858 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800859 case BPF_FUNC_get_stackid:
860 return &bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -0700861 case BPF_FUNC_get_stack:
862 return &bpf_get_stack_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
Daniel Xufff7b642020-02-17 19:04:31 -08001031BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1032 void *, buf, u32, size, u64, flags)
1033{
1034#ifndef CONFIG_X86
1035 return -ENOENT;
1036#else
1037 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1038 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1039 u32 to_copy;
1040
1041 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1042 return -EINVAL;
1043
1044 if (unlikely(!br_stack))
1045 return -EINVAL;
1046
1047 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1048 return br_stack->nr * br_entry_size;
1049
1050 if (!buf || (size % br_entry_size != 0))
1051 return -EINVAL;
1052
1053 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1054 memcpy(buf, br_stack->entries, to_copy);
1055
1056 return to_copy;
1057#endif
1058}
1059
1060static const struct bpf_func_proto bpf_read_branch_records_proto = {
1061 .func = bpf_read_branch_records,
1062 .gpl_only = true,
1063 .ret_type = RET_INTEGER,
1064 .arg1_type = ARG_PTR_TO_CTX,
1065 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1066 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1067 .arg4_type = ARG_ANYTHING,
1068};
1069
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001070static const struct bpf_func_proto *
1071pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Yonghong Songf005afe2018-03-20 11:19:17 -07001072{
1073 switch (func_id) {
1074 case BPF_FUNC_perf_event_output:
1075 return &bpf_perf_event_output_proto_tp;
1076 case BPF_FUNC_get_stackid:
1077 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -07001078 case BPF_FUNC_get_stack:
1079 return &bpf_get_stack_proto_tp;
Yonghong Songf005afe2018-03-20 11:19:17 -07001080 case BPF_FUNC_perf_prog_read_value:
1081 return &bpf_perf_prog_read_value_proto;
Daniel Xufff7b642020-02-17 19:04:31 -08001082 case BPF_FUNC_read_branch_records:
1083 return &bpf_read_branch_records_proto;
Yonghong Songf005afe2018-03-20 11:19:17 -07001084 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001085 return tracing_func_proto(func_id, prog);
Yonghong Songf005afe2018-03-20 11:19:17 -07001086 }
1087}
1088
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001089/*
1090 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1091 * to avoid potential recursive reuse issue when/if tracepoints are added
Matt Mullins9594dc32019-06-11 14:53:04 -07001092 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1093 *
1094 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1095 * in normal, irq, and nmi context.
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001096 */
Matt Mullins9594dc32019-06-11 14:53:04 -07001097struct bpf_raw_tp_regs {
1098 struct pt_regs regs[3];
1099};
1100static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1101static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1102static struct pt_regs *get_bpf_raw_tp_regs(void)
1103{
1104 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1105 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1106
1107 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1108 this_cpu_dec(bpf_raw_tp_nest_level);
1109 return ERR_PTR(-EBUSY);
1110 }
1111
1112 return &tp_regs->regs[nest_level - 1];
1113}
1114
1115static void put_bpf_raw_tp_regs(void)
1116{
1117 this_cpu_dec(bpf_raw_tp_nest_level);
1118}
1119
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001120BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1121 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1122{
Matt Mullins9594dc32019-06-11 14:53:04 -07001123 struct pt_regs *regs = get_bpf_raw_tp_regs();
1124 int ret;
1125
1126 if (IS_ERR(regs))
1127 return PTR_ERR(regs);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001128
1129 perf_fetch_caller_regs(regs);
Matt Mullins9594dc32019-06-11 14:53:04 -07001130 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1131
1132 put_bpf_raw_tp_regs();
1133 return ret;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001134}
1135
1136static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1137 .func = bpf_perf_event_output_raw_tp,
1138 .gpl_only = true,
1139 .ret_type = RET_INTEGER,
1140 .arg1_type = ARG_PTR_TO_CTX,
1141 .arg2_type = ARG_CONST_MAP_PTR,
1142 .arg3_type = ARG_ANYTHING,
1143 .arg4_type = ARG_PTR_TO_MEM,
1144 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1145};
1146
Alexei Starovoitova7658e12019-10-15 20:25:04 -07001147extern const struct bpf_func_proto bpf_skb_output_proto;
1148
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001149BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1150 struct bpf_map *, map, u64, flags)
1151{
Matt Mullins9594dc32019-06-11 14:53:04 -07001152 struct pt_regs *regs = get_bpf_raw_tp_regs();
1153 int ret;
1154
1155 if (IS_ERR(regs))
1156 return PTR_ERR(regs);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001157
1158 perf_fetch_caller_regs(regs);
1159 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
Matt Mullins9594dc32019-06-11 14:53:04 -07001160 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1161 flags, 0, 0);
1162 put_bpf_raw_tp_regs();
1163 return ret;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001164}
1165
1166static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1167 .func = bpf_get_stackid_raw_tp,
1168 .gpl_only = true,
1169 .ret_type = RET_INTEGER,
1170 .arg1_type = ARG_PTR_TO_CTX,
1171 .arg2_type = ARG_CONST_MAP_PTR,
1172 .arg3_type = ARG_ANYTHING,
1173};
1174
Yonghong Songc195651e2018-04-28 22:28:08 -07001175BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1176 void *, buf, u32, size, u64, flags)
1177{
Matt Mullins9594dc32019-06-11 14:53:04 -07001178 struct pt_regs *regs = get_bpf_raw_tp_regs();
1179 int ret;
1180
1181 if (IS_ERR(regs))
1182 return PTR_ERR(regs);
Yonghong Songc195651e2018-04-28 22:28:08 -07001183
1184 perf_fetch_caller_regs(regs);
Matt Mullins9594dc32019-06-11 14:53:04 -07001185 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1186 (unsigned long) size, flags, 0);
1187 put_bpf_raw_tp_regs();
1188 return ret;
Yonghong Songc195651e2018-04-28 22:28:08 -07001189}
1190
1191static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1192 .func = bpf_get_stack_raw_tp,
1193 .gpl_only = true,
1194 .ret_type = RET_INTEGER,
1195 .arg1_type = ARG_PTR_TO_CTX,
1196 .arg2_type = ARG_PTR_TO_MEM,
1197 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1198 .arg4_type = ARG_ANYTHING,
1199};
1200
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001201static const struct bpf_func_proto *
1202raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001203{
1204 switch (func_id) {
1205 case BPF_FUNC_perf_event_output:
1206 return &bpf_perf_event_output_proto_raw_tp;
1207 case BPF_FUNC_get_stackid:
1208 return &bpf_get_stackid_proto_raw_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -07001209 case BPF_FUNC_get_stack:
1210 return &bpf_get_stack_proto_raw_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001211 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001212 return tracing_func_proto(func_id, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001213 }
1214}
1215
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001216static const struct bpf_func_proto *
1217tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1218{
1219 switch (func_id) {
1220#ifdef CONFIG_NET
1221 case BPF_FUNC_skb_output:
1222 return &bpf_skb_output_proto;
1223#endif
1224 default:
1225 return raw_tp_prog_func_proto(func_id, prog);
1226 }
1227}
1228
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001229static bool raw_tp_prog_is_valid_access(int off, int size,
1230 enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001231 const struct bpf_prog *prog,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001232 struct bpf_insn_access_aux *info)
1233{
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001234 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001235 return false;
1236 if (type != BPF_READ)
1237 return false;
1238 if (off % size != 0)
1239 return false;
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001240 return true;
1241}
1242
1243static bool tracing_prog_is_valid_access(int off, int size,
1244 enum bpf_access_type type,
1245 const struct bpf_prog *prog,
1246 struct bpf_insn_access_aux *info)
1247{
1248 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1249 return false;
1250 if (type != BPF_READ)
1251 return false;
1252 if (off % size != 0)
1253 return false;
Alexei Starovoitov9e15db62019-10-15 20:25:00 -07001254 return btf_ctx_access(off, size, type, prog, info);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001255}
1256
KP Singh3e7c67d2020-03-05 23:01:27 +01001257int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1258 const union bpf_attr *kattr,
1259 union bpf_attr __user *uattr)
1260{
1261 return -ENOTSUPP;
1262}
1263
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001264const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1265 .get_func_proto = raw_tp_prog_func_proto,
1266 .is_valid_access = raw_tp_prog_is_valid_access,
1267};
1268
1269const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1270};
1271
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001272const struct bpf_verifier_ops tracing_verifier_ops = {
1273 .get_func_proto = tracing_prog_func_proto,
1274 .is_valid_access = tracing_prog_is_valid_access,
1275};
1276
1277const struct bpf_prog_ops tracing_prog_ops = {
KP Singhda00d2f2020-03-04 20:18:52 +01001278 .test_run = bpf_prog_test_run_tracing,
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001279};
1280
Matt Mullins9df1c282019-04-26 11:49:47 -07001281static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1282 enum bpf_access_type type,
1283 const struct bpf_prog *prog,
1284 struct bpf_insn_access_aux *info)
1285{
1286 if (off == 0) {
1287 if (size != sizeof(u64) || type != BPF_READ)
1288 return false;
1289 info->reg_type = PTR_TO_TP_BUFFER;
1290 }
1291 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1292}
1293
1294const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1295 .get_func_proto = raw_tp_prog_func_proto,
1296 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1297};
1298
1299const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1300};
1301
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001302static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001303 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -07001304 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001305{
Teng Qin95da0cd2018-03-06 10:55:01 -08001306 const int size_u64 = sizeof(u64);
Yonghong Song31fd8582017-06-13 15:52:13 -07001307
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001308 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1309 return false;
1310 if (type != BPF_READ)
1311 return false;
Daniel Borkmannbc231052018-06-02 23:06:39 +02001312 if (off % size != 0) {
1313 if (sizeof(unsigned long) != 4)
1314 return false;
1315 if (size != 8)
1316 return false;
1317 if (off % size != 4)
1318 return false;
1319 }
Yonghong Song31fd8582017-06-13 15:52:13 -07001320
Daniel Borkmannf96da092017-07-02 02:13:27 +02001321 switch (off) {
1322 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
Teng Qin95da0cd2018-03-06 10:55:01 -08001323 bpf_ctx_record_field_size(info, size_u64);
1324 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1325 return false;
1326 break;
1327 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1328 bpf_ctx_record_field_size(info, size_u64);
1329 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
Yonghong Song23994632017-06-22 15:07:39 -07001330 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +02001331 break;
1332 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001333 if (size != sizeof(long))
1334 return false;
1335 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02001336
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001337 return true;
1338}
1339
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001340static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1341 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001342 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +02001343 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001344{
1345 struct bpf_insn *insn = insn_buf;
1346
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001347 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001348 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +02001349 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001350 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001351 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001352 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02001353 bpf_target_off(struct perf_sample_data, period, 8,
1354 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001355 break;
Teng Qin95da0cd2018-03-06 10:55:01 -08001356 case offsetof(struct bpf_perf_event_data, addr):
1357 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1358 data), si->dst_reg, si->src_reg,
1359 offsetof(struct bpf_perf_event_data_kern, data));
1360 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1361 bpf_target_off(struct perf_sample_data, addr, 8,
1362 target_size));
1363 break;
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001364 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +02001365 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001366 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001367 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +01001368 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1369 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001370 break;
1371 }
1372
1373 return insn - insn_buf;
1374}
1375
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001376const struct bpf_verifier_ops perf_event_verifier_ops = {
Yonghong Songf005afe2018-03-20 11:19:17 -07001377 .get_func_proto = pe_prog_func_proto,
Alexei Starovoitov0515e592016-09-01 18:37:22 -07001378 .is_valid_access = pe_prog_is_valid_access,
1379 .convert_ctx_access = pe_prog_convert_ctx_access,
1380};
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001381
1382const struct bpf_prog_ops perf_event_prog_ops = {
1383};
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001384
1385static DEFINE_MUTEX(bpf_event_mutex);
1386
Yonghong Songc8c088b2017-11-30 13:47:54 -08001387#define BPF_TRACE_MAX_PROGS 64
1388
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001389int perf_event_attach_bpf_prog(struct perf_event *event,
1390 struct bpf_prog *prog)
1391{
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001392 struct bpf_prog_array *old_array;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001393 struct bpf_prog_array *new_array;
1394 int ret = -EEXIST;
1395
Josef Bacik9802d862017-12-11 11:36:48 -05001396 /*
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001397 * Kprobe override only works if they are on the function entry,
1398 * and only if they are on the opt-in list.
Josef Bacik9802d862017-12-11 11:36:48 -05001399 */
1400 if (prog->kprobe_override &&
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001401 (!trace_kprobe_on_func_entry(event->tp_event) ||
Josef Bacik9802d862017-12-11 11:36:48 -05001402 !trace_kprobe_error_injectable(event->tp_event)))
1403 return -EINVAL;
1404
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001405 mutex_lock(&bpf_event_mutex);
1406
1407 if (event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001408 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001409
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001410 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
Yonghong Songc8c088b2017-11-30 13:47:54 -08001411 if (old_array &&
1412 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1413 ret = -E2BIG;
1414 goto unlock;
1415 }
1416
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001417 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1418 if (ret < 0)
Yonghong Song07c41a22017-10-30 13:50:22 -07001419 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001420
1421 /* set the new array to event->tp_event and set event->prog */
1422 event->prog = prog;
1423 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1424 bpf_prog_array_free(old_array);
1425
Yonghong Song07c41a22017-10-30 13:50:22 -07001426unlock:
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001427 mutex_unlock(&bpf_event_mutex);
1428 return ret;
1429}
1430
1431void perf_event_detach_bpf_prog(struct perf_event *event)
1432{
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001433 struct bpf_prog_array *old_array;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001434 struct bpf_prog_array *new_array;
1435 int ret;
1436
1437 mutex_lock(&bpf_event_mutex);
1438
1439 if (!event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001440 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001441
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001442 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001443 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
Sean Young170a7e32018-05-27 12:24:08 +01001444 if (ret == -ENOENT)
1445 goto unlock;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001446 if (ret < 0) {
1447 bpf_prog_array_delete_safe(old_array, event->prog);
1448 } else {
1449 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1450 bpf_prog_array_free(old_array);
1451 }
1452
1453 bpf_prog_put(event->prog);
1454 event->prog = NULL;
1455
Yonghong Song07c41a22017-10-30 13:50:22 -07001456unlock:
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001457 mutex_unlock(&bpf_event_mutex);
1458}
Yonghong Songf371b302017-12-11 11:39:02 -08001459
Yonghong Songf4e22982017-12-13 10:35:37 -08001460int perf_event_query_prog_array(struct perf_event *event, void __user *info)
Yonghong Songf371b302017-12-11 11:39:02 -08001461{
1462 struct perf_event_query_bpf __user *uquery = info;
1463 struct perf_event_query_bpf query = {};
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001464 struct bpf_prog_array *progs;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001465 u32 *ids, prog_cnt, ids_len;
Yonghong Songf371b302017-12-11 11:39:02 -08001466 int ret;
1467
1468 if (!capable(CAP_SYS_ADMIN))
1469 return -EPERM;
1470 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1471 return -EINVAL;
1472 if (copy_from_user(&query, uquery, sizeof(query)))
1473 return -EFAULT;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001474
1475 ids_len = query.ids_len;
1476 if (ids_len > BPF_TRACE_MAX_PROGS)
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001477 return -E2BIG;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001478 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1479 if (!ids)
1480 return -ENOMEM;
1481 /*
1482 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1483 * is required when user only wants to check for uquery->prog_cnt.
1484 * There is no need to check for it since the case is handled
1485 * gracefully in bpf_prog_array_copy_info.
1486 */
Yonghong Songf371b302017-12-11 11:39:02 -08001487
1488 mutex_lock(&bpf_event_mutex);
Stanislav Fomicheve672db02019-05-28 14:14:44 -07001489 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
1490 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
Yonghong Songf371b302017-12-11 11:39:02 -08001491 mutex_unlock(&bpf_event_mutex);
1492
Yonghong Song3a38bb92018-04-10 09:37:32 -07001493 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1494 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1495 ret = -EFAULT;
1496
1497 kfree(ids);
Yonghong Songf371b302017-12-11 11:39:02 -08001498 return ret;
1499}
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001500
1501extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1502extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1503
Matt Mullinsa38d1102018-12-12 16:42:37 -08001504struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001505{
1506 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1507
1508 for (; btp < __stop__bpf_raw_tp; btp++) {
1509 if (!strcmp(btp->tp->name, name))
1510 return btp;
1511 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001512
1513 return bpf_get_raw_tracepoint_module(name);
1514}
1515
1516void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
1517{
1518 struct module *mod = __module_address((unsigned long)btp);
1519
1520 if (mod)
1521 module_put(mod);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001522}
1523
1524static __always_inline
1525void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1526{
Thomas Gleixnerf03efe42020-02-24 15:01:35 +01001527 cant_sleep();
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001528 rcu_read_lock();
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001529 (void) BPF_PROG_RUN(prog, args);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001530 rcu_read_unlock();
1531}
1532
1533#define UNPACK(...) __VA_ARGS__
1534#define REPEAT_1(FN, DL, X, ...) FN(X)
1535#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1536#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1537#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1538#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1539#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1540#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1541#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1542#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1543#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1544#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1545#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1546#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1547
1548#define SARG(X) u64 arg##X
1549#define COPY(X) args[X] = arg##X
1550
1551#define __DL_COM (,)
1552#define __DL_SEM (;)
1553
1554#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1555
1556#define BPF_TRACE_DEFN_x(x) \
1557 void bpf_trace_run##x(struct bpf_prog *prog, \
1558 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1559 { \
1560 u64 args[x]; \
1561 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1562 __bpf_trace_run(prog, args); \
1563 } \
1564 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1565BPF_TRACE_DEFN_x(1);
1566BPF_TRACE_DEFN_x(2);
1567BPF_TRACE_DEFN_x(3);
1568BPF_TRACE_DEFN_x(4);
1569BPF_TRACE_DEFN_x(5);
1570BPF_TRACE_DEFN_x(6);
1571BPF_TRACE_DEFN_x(7);
1572BPF_TRACE_DEFN_x(8);
1573BPF_TRACE_DEFN_x(9);
1574BPF_TRACE_DEFN_x(10);
1575BPF_TRACE_DEFN_x(11);
1576BPF_TRACE_DEFN_x(12);
1577
1578static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1579{
1580 struct tracepoint *tp = btp->tp;
1581
1582 /*
1583 * check that program doesn't access arguments beyond what's
1584 * available in this tracepoint
1585 */
1586 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1587 return -EINVAL;
1588
Matt Mullins9df1c282019-04-26 11:49:47 -07001589 if (prog->aux->max_tp_access > btp->writable_size)
1590 return -EINVAL;
1591
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001592 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1593}
1594
1595int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1596{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08001597 return __bpf_probe_register(btp, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001598}
1599
1600int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1601{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08001602 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001603}
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001604
1605int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1606 u32 *fd_type, const char **buf,
1607 u64 *probe_offset, u64 *probe_addr)
1608{
1609 bool is_tracepoint, is_syscall_tp;
1610 struct bpf_prog *prog;
1611 int flags, err = 0;
1612
1613 prog = event->prog;
1614 if (!prog)
1615 return -ENOENT;
1616
1617 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1618 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1619 return -EOPNOTSUPP;
1620
1621 *prog_id = prog->aux->id;
1622 flags = event->tp_event->flags;
1623 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1624 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1625
1626 if (is_tracepoint || is_syscall_tp) {
1627 *buf = is_tracepoint ? event->tp_event->tp->name
1628 : event->tp_event->name;
1629 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1630 *probe_offset = 0x0;
1631 *probe_addr = 0x0;
1632 } else {
1633 /* kprobe/uprobe */
1634 err = -EOPNOTSUPP;
1635#ifdef CONFIG_KPROBE_EVENTS
1636 if (flags & TRACE_EVENT_FL_KPROBE)
1637 err = bpf_get_kprobe_info(event, fd_type, buf,
1638 probe_offset, probe_addr,
1639 event->attr.type == PERF_TYPE_TRACEPOINT);
1640#endif
1641#ifdef CONFIG_UPROBE_EVENTS
1642 if (flags & TRACE_EVENT_FL_UPROBE)
1643 err = bpf_get_uprobe_info(event, fd_type, buf,
1644 probe_offset,
1645 event->attr.type == PERF_TYPE_TRACEPOINT);
1646#endif
1647 }
1648
1649 return err;
1650}
Matt Mullinsa38d1102018-12-12 16:42:37 -08001651
Yonghong Song9db1ff02019-06-25 17:35:03 -07001652static int __init send_signal_irq_work_init(void)
1653{
1654 int cpu;
1655 struct send_signal_irq_work *work;
1656
1657 for_each_possible_cpu(cpu) {
1658 work = per_cpu_ptr(&send_signal_work, cpu);
1659 init_irq_work(&work->irq_work, do_bpf_send_signal);
1660 }
1661 return 0;
1662}
1663
1664subsys_initcall(send_signal_irq_work_init);
1665
Matt Mullinsa38d1102018-12-12 16:42:37 -08001666#ifdef CONFIG_MODULES
Stanislav Fomichev390e99c2019-05-13 12:04:36 -07001667static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
1668 void *module)
Matt Mullinsa38d1102018-12-12 16:42:37 -08001669{
1670 struct bpf_trace_module *btm, *tmp;
1671 struct module *mod = module;
1672
1673 if (mod->num_bpf_raw_events == 0 ||
1674 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
1675 return 0;
1676
1677 mutex_lock(&bpf_module_mutex);
1678
1679 switch (op) {
1680 case MODULE_STATE_COMING:
1681 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
1682 if (btm) {
1683 btm->module = module;
1684 list_add(&btm->list, &bpf_trace_modules);
1685 }
1686 break;
1687 case MODULE_STATE_GOING:
1688 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
1689 if (btm->module == module) {
1690 list_del(&btm->list);
1691 kfree(btm);
1692 break;
1693 }
1694 }
1695 break;
1696 }
1697
1698 mutex_unlock(&bpf_module_mutex);
1699
1700 return 0;
1701}
1702
1703static struct notifier_block bpf_module_nb = {
1704 .notifier_call = bpf_event_notify,
1705};
1706
Stanislav Fomichev390e99c2019-05-13 12:04:36 -07001707static int __init bpf_event_init(void)
Matt Mullinsa38d1102018-12-12 16:42:37 -08001708{
1709 register_module_notifier(&bpf_module_nb);
1710 return 0;
1711}
1712
1713fs_initcall(bpf_event_init);
1714#endif /* CONFIG_MODULES */