]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/trace/bpf_trace.c
bpf:bpf_seq_printf(): handle potentially unsafe format string better
[mirror_ubuntu-jammy-kernel.git] / kernel / trace / bpf_trace.c
CommitLineData
179a0cc4 1// SPDX-License-Identifier: GPL-2.0
2541517c 2/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
0515e599 3 * Copyright (c) 2016 Facebook
2541517c
AS
4 */
5#include <linux/kernel.h>
6#include <linux/types.h>
7#include <linux/slab.h>
8#include <linux/bpf.h>
0515e599 9#include <linux/bpf_perf_event.h>
2541517c
AS
10#include <linux/filter.h>
11#include <linux/uaccess.h>
9c959c86 12#include <linux/ctype.h>
9802d865 13#include <linux/kprobes.h>
41bdc4b4 14#include <linux/syscalls.h>
540adea3 15#include <linux/error-injection.h>
9802d865 16
c7b6f29b
NA
17#include <asm/tlb.h>
18
9802d865 19#include "trace_probe.h"
2541517c
AS
20#include "trace.h"
21
e672db03
SF
22#define bpf_event_rcu_dereference(p) \
23 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
24
a38d1107
MM
25#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
035226b9 62u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
c195651e 63u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
035226b9 64
2541517c
AS
65/**
66 * trace_call_bpf - invoke BPF program
e87c6bc3 67 * @call: tracepoint event
2541517c
AS
68 * @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 */
e87c6bc3 79unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
2541517c
AS
80{
81 unsigned int ret;
82
83 if (in_nmi()) /* not supported yet */
84 return 1;
85
b0a81b94 86 cant_sleep();
2541517c
AS
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
e87c6bc3
YS
99 /*
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);
2541517c
AS
115
116 out:
117 __this_cpu_dec(bpf_prog_active);
2541517c
AS
118
119 return ret;
120}
2541517c 121
9802d865
JB
122#ifdef CONFIG_BPF_KPROBE_OVERRIDE
123BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
124{
9802d865 125 regs_set_return_value(regs, rc);
540adea3 126 override_function_with_return(regs);
9802d865
JB
127 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
6ae08ae3
DB
139BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
140 const void __user *, unsafe_ptr)
2541517c 141{
6ae08ae3 142 int ret = probe_user_read(dst, unsafe_ptr, size);
2541517c 143
6ae08ae3
DB
144 if (unlikely(ret < 0))
145 memset(dst, 0, size);
146
147 return ret;
148}
149
f470378c 150const struct bpf_func_proto bpf_probe_read_user_proto = {
6ae08ae3
DB
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{
bd88bb5d 162 int ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
6ae08ae3
DB
163
164 if (unlikely(ret < 0))
165 memset(dst, 0, size);
166
167 return ret;
168}
169
f470378c 170const struct bpf_func_proto bpf_probe_read_user_str_proto = {
6ae08ae3
DB
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);
9d1f8be5 184
6ae08ae3
DB
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);
074f528e 189 if (unlikely(ret < 0))
9d1f8be5 190out:
074f528e 191 memset(dst, 0, size);
6ae08ae3
DB
192 return ret;
193}
074f528e 194
6ae08ae3
DB
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
f470378c 201const struct bpf_func_proto bpf_probe_read_kernel_proto = {
6ae08ae3
DB
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) :
c4cb1644 243 strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
6ae08ae3
DB
244 if (unlikely(ret < 0))
245out:
246 memset(dst, 0, size);
074f528e 247 return ret;
2541517c
AS
248}
249
6ae08ae3
DB
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
f470378c 256const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
6ae08ae3
DB
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,
2541517c
AS
273 .gpl_only = true,
274 .ret_type = RET_INTEGER,
39f19ebb 275 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
9c019e2b 276 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2541517c
AS
277 .arg3_type = ARG_ANYTHING,
278};
279
eb1b6688 280BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
f3694e00 281 u32, size)
96ae5227 282{
96ae5227
SD
283 /*
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.
c7b6f29b
NA
290 *
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.
96ae5227
SD
294 */
295
296 if (unlikely(in_interrupt() ||
297 current->flags & (PF_KTHREAD | PF_EXITING)))
298 return -EPERM;
db68ce10 299 if (unlikely(uaccess_kernel()))
96ae5227 300 return -EPERM;
c7b6f29b
NA
301 if (unlikely(!nmi_uaccess_okay()))
302 return -EPERM;
96ae5227 303
eb1b6688 304 return probe_user_write(unsafe_ptr, src, size);
96ae5227
SD
305}
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,
39f19ebb
AS
312 .arg2_type = ARG_PTR_TO_MEM,
313 .arg3_type = ARG_CONST_SIZE,
96ae5227
SD
314};
315
316static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
317{
2c78ee89
AS
318 if (!capable(CAP_SYS_ADMIN))
319 return NULL;
320
96ae5227
SD
321 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
322 current->comm, task_pid_nr(current));
323
324 return &bpf_probe_write_user_proto;
325}
326
d7b2977b
CH
327static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
328 size_t bufsz)
329{
330 void __user *user_ptr = (__force void __user *)unsafe_ptr;
331
332 buf[0] = 0;
333
334 switch (fmt_ptype) {
335 case 's':
336#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
aec6ce59
CH
337 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
338 strncpy_from_user_nofault(buf, user_ptr, bufsz);
339 break;
340 }
341 fallthrough;
d7b2977b
CH
342#endif
343 case 'k':
344 strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
345 break;
346 case 'u':
347 strncpy_from_user_nofault(buf, user_ptr, bufsz);
348 break;
349 }
350}
351
9c959c86 352/*
7bda4b40 353 * Only limited trace_printk() conversion specifiers allowed:
b2a5212f 354 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pks %pus %s
9c959c86 355 */
f3694e00
DB
356BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
357 u64, arg2, u64, arg3)
9c959c86 358{
b2a5212f
DB
359 int i, mod[3] = {}, fmt_cnt = 0;
360 char buf[64], fmt_ptype;
361 void *unsafe_ptr = NULL;
8d3b7dce 362 bool str_seen = false;
9c959c86
AS
363
364 /*
365 * bpf_check()->check_func_arg()->check_stack_boundary()
366 * guarantees that fmt points to bpf program stack,
367 * fmt_size bytes of it were initialized and fmt_size > 0
368 */
369 if (fmt[--fmt_size] != 0)
370 return -EINVAL;
371
372 /* check format string for allowed specifiers */
373 for (i = 0; i < fmt_size; i++) {
374 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
375 return -EINVAL;
376
377 if (fmt[i] != '%')
378 continue;
379
380 if (fmt_cnt >= 3)
381 return -EINVAL;
382
383 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
384 i++;
385 if (fmt[i] == 'l') {
386 mod[fmt_cnt]++;
387 i++;
b2a5212f 388 } else if (fmt[i] == 'p') {
9c959c86 389 mod[fmt_cnt]++;
b2a5212f
DB
390 if ((fmt[i + 1] == 'k' ||
391 fmt[i + 1] == 'u') &&
392 fmt[i + 2] == 's') {
393 fmt_ptype = fmt[i + 1];
394 i += 2;
395 goto fmt_str;
396 }
397
1efb6ee3
MP
398 /* disallow any further format extensions */
399 if (fmt[i + 1] != 0 &&
400 !isspace(fmt[i + 1]) &&
401 !ispunct(fmt[i + 1]))
9c959c86 402 return -EINVAL;
b2a5212f
DB
403
404 goto fmt_next;
405 } else if (fmt[i] == 's') {
406 mod[fmt_cnt]++;
407 fmt_ptype = fmt[i];
408fmt_str:
409 if (str_seen)
410 /* allow only one '%s' per fmt string */
411 return -EINVAL;
412 str_seen = true;
413
414 if (fmt[i + 1] != 0 &&
415 !isspace(fmt[i + 1]) &&
416 !ispunct(fmt[i + 1]))
417 return -EINVAL;
418
419 switch (fmt_cnt) {
420 case 0:
421 unsafe_ptr = (void *)(long)arg1;
422 arg1 = (long)buf;
423 break;
424 case 1:
425 unsafe_ptr = (void *)(long)arg2;
426 arg2 = (long)buf;
427 break;
428 case 2:
429 unsafe_ptr = (void *)(long)arg3;
430 arg3 = (long)buf;
431 break;
432 }
433
d7b2977b
CH
434 bpf_trace_copy_string(buf, unsafe_ptr, fmt_ptype,
435 sizeof(buf));
b2a5212f 436 goto fmt_next;
9c959c86
AS
437 }
438
439 if (fmt[i] == 'l') {
440 mod[fmt_cnt]++;
441 i++;
442 }
443
7bda4b40
JF
444 if (fmt[i] != 'i' && fmt[i] != 'd' &&
445 fmt[i] != 'u' && fmt[i] != 'x')
9c959c86 446 return -EINVAL;
b2a5212f 447fmt_next:
9c959c86
AS
448 fmt_cnt++;
449 }
450
88a5c690
DB
451/* Horrid workaround for getting va_list handling working with different
452 * argument type combinations generically for 32 and 64 bit archs.
453 */
454#define __BPF_TP_EMIT() __BPF_ARG3_TP()
455#define __BPF_TP(...) \
eefa864a 456 __trace_printk(0 /* Fake ip */, \
88a5c690
DB
457 fmt, ##__VA_ARGS__)
458
459#define __BPF_ARG1_TP(...) \
460 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
461 ? __BPF_TP(arg1, ##__VA_ARGS__) \
462 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
463 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
464 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
465
466#define __BPF_ARG2_TP(...) \
467 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
468 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
469 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
470 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
471 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
472
473#define __BPF_ARG3_TP(...) \
474 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
475 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
476 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
477 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
478 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
479
480 return __BPF_TP_EMIT();
9c959c86
AS
481}
482
483static const struct bpf_func_proto bpf_trace_printk_proto = {
484 .func = bpf_trace_printk,
485 .gpl_only = true,
486 .ret_type = RET_INTEGER,
39f19ebb
AS
487 .arg1_type = ARG_PTR_TO_MEM,
488 .arg2_type = ARG_CONST_SIZE,
9c959c86
AS
489};
490
0756ea3e
AS
491const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
492{
493 /*
494 * this program might be calling bpf_trace_printk,
495 * so allocate per-cpu printk buffers
496 */
497 trace_printk_init_buffers();
498
499 return &bpf_trace_printk_proto;
500}
501
492e639f
YS
502#define MAX_SEQ_PRINTF_VARARGS 12
503#define MAX_SEQ_PRINTF_MAX_MEMCPY 6
504#define MAX_SEQ_PRINTF_STR_LEN 128
505
506struct bpf_seq_printf_buf {
507 char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN];
508};
509static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf);
510static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used);
511
512BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
513 const void *, data, u32, data_len)
514{
515 int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0;
516 int i, buf_used, copy_size, num_args;
517 u64 params[MAX_SEQ_PRINTF_VARARGS];
518 struct bpf_seq_printf_buf *bufs;
519 const u64 *args = data;
520
521 buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used);
522 if (WARN_ON_ONCE(buf_used > 1)) {
523 err = -EBUSY;
524 goto out;
525 }
526
527 bufs = this_cpu_ptr(&bpf_seq_printf_buf);
528
529 /*
530 * bpf_check()->check_func_arg()->check_stack_boundary()
531 * guarantees that fmt points to bpf program stack,
532 * fmt_size bytes of it were initialized and fmt_size > 0
533 */
534 if (fmt[--fmt_size] != 0)
535 goto out;
536
537 if (data_len & 7)
538 goto out;
539
540 for (i = 0; i < fmt_size; i++) {
541 if (fmt[i] == '%') {
542 if (fmt[i + 1] == '%')
543 i++;
544 else if (!data || !data_len)
545 goto out;
546 }
547 }
548
549 num_args = data_len / 8;
550
551 /* check format string for allowed specifiers */
552 for (i = 0; i < fmt_size; i++) {
553 /* only printable ascii for now. */
554 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
555 err = -EINVAL;
556 goto out;
557 }
558
559 if (fmt[i] != '%')
560 continue;
561
562 if (fmt[i + 1] == '%') {
563 i++;
564 continue;
565 }
566
567 if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) {
568 err = -E2BIG;
569 goto out;
570 }
571
572 if (fmt_cnt >= num_args) {
573 err = -EINVAL;
574 goto out;
575 }
576
577 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
578 i++;
579
580 /* skip optional "[0 +-][num]" width formating field */
581 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
582 fmt[i] == ' ')
583 i++;
584 if (fmt[i] >= '1' && fmt[i] <= '9') {
585 i++;
586 while (fmt[i] >= '0' && fmt[i] <= '9')
587 i++;
588 }
589
590 if (fmt[i] == 's') {
19c8d8ac
AM
591 void *unsafe_ptr;
592
492e639f
YS
593 /* try our best to copy */
594 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
595 err = -E2BIG;
596 goto out;
597 }
598
19c8d8ac
AM
599 unsafe_ptr = (void *)(long)args[fmt_cnt];
600 err = strncpy_from_kernel_nofault(bufs->buf[memcpy_cnt],
601 unsafe_ptr, MAX_SEQ_PRINTF_STR_LEN);
492e639f
YS
602 if (err < 0)
603 bufs->buf[memcpy_cnt][0] = '\0';
604 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
605
606 fmt_cnt++;
607 memcpy_cnt++;
608 continue;
609 }
610
611 if (fmt[i] == 'p') {
612 if (fmt[i + 1] == 0 ||
613 fmt[i + 1] == 'K' ||
614 fmt[i + 1] == 'x') {
615 /* just kernel pointers */
616 params[fmt_cnt] = args[fmt_cnt];
617 fmt_cnt++;
618 continue;
619 }
620
621 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
622 if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') {
623 err = -EINVAL;
624 goto out;
625 }
626 if (fmt[i + 2] != '4' && fmt[i + 2] != '6') {
627 err = -EINVAL;
628 goto out;
629 }
630
631 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) {
632 err = -E2BIG;
633 goto out;
634 }
635
636
637 copy_size = (fmt[i + 2] == '4') ? 4 : 16;
638
639 err = probe_kernel_read(bufs->buf[memcpy_cnt],
640 (void *) (long) args[fmt_cnt],
641 copy_size);
642 if (err < 0)
643 memset(bufs->buf[memcpy_cnt], 0, copy_size);
644 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
645
646 i += 2;
647 fmt_cnt++;
648 memcpy_cnt++;
649 continue;
650 }
651
652 if (fmt[i] == 'l') {
653 i++;
654 if (fmt[i] == 'l')
655 i++;
656 }
657
658 if (fmt[i] != 'i' && fmt[i] != 'd' &&
659 fmt[i] != 'u' && fmt[i] != 'x') {
660 err = -EINVAL;
661 goto out;
662 }
663
664 params[fmt_cnt] = args[fmt_cnt];
665 fmt_cnt++;
666 }
667
668 /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give
669 * all of them to seq_printf().
670 */
671 seq_printf(m, fmt, params[0], params[1], params[2], params[3],
672 params[4], params[5], params[6], params[7], params[8],
673 params[9], params[10], params[11]);
674
675 err = seq_has_overflowed(m) ? -EOVERFLOW : 0;
676out:
677 this_cpu_dec(bpf_seq_printf_buf_used);
678 return err;
679}
680
681static int bpf_seq_printf_btf_ids[5];
682static const struct bpf_func_proto bpf_seq_printf_proto = {
683 .func = bpf_seq_printf,
684 .gpl_only = true,
685 .ret_type = RET_INTEGER,
686 .arg1_type = ARG_PTR_TO_BTF_ID,
687 .arg2_type = ARG_PTR_TO_MEM,
688 .arg3_type = ARG_CONST_SIZE,
689 .arg4_type = ARG_PTR_TO_MEM_OR_NULL,
690 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
691 .btf_id = bpf_seq_printf_btf_ids,
692};
693
694BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
695{
696 return seq_write(m, data, len) ? -EOVERFLOW : 0;
697}
698
699static int bpf_seq_write_btf_ids[5];
700static const struct bpf_func_proto bpf_seq_write_proto = {
701 .func = bpf_seq_write,
702 .gpl_only = true,
703 .ret_type = RET_INTEGER,
704 .arg1_type = ARG_PTR_TO_BTF_ID,
705 .arg2_type = ARG_PTR_TO_MEM,
706 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
707 .btf_id = bpf_seq_write_btf_ids,
708};
709
908432ca
YS
710static __always_inline int
711get_map_perf_counter(struct bpf_map *map, u64 flags,
712 u64 *value, u64 *enabled, u64 *running)
35578d79 713{
35578d79 714 struct bpf_array *array = container_of(map, struct bpf_array, map);
6816a7ff
DB
715 unsigned int cpu = smp_processor_id();
716 u64 index = flags & BPF_F_INDEX_MASK;
3b1efb19 717 struct bpf_event_entry *ee;
35578d79 718
6816a7ff
DB
719 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
720 return -EINVAL;
721 if (index == BPF_F_CURRENT_CPU)
722 index = cpu;
35578d79
KX
723 if (unlikely(index >= array->map.max_entries))
724 return -E2BIG;
725
3b1efb19 726 ee = READ_ONCE(array->ptrs[index]);
1ca1cc98 727 if (!ee)
35578d79
KX
728 return -ENOENT;
729
908432ca
YS
730 return perf_event_read_local(ee->event, value, enabled, running);
731}
732
733BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
734{
735 u64 value = 0;
736 int err;
737
738 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
35578d79 739 /*
f91840a3
AS
740 * this api is ugly since we miss [-22..-2] range of valid
741 * counter values, but that's uapi
35578d79 742 */
f91840a3
AS
743 if (err)
744 return err;
745 return value;
35578d79
KX
746}
747
62544ce8 748static const struct bpf_func_proto bpf_perf_event_read_proto = {
35578d79 749 .func = bpf_perf_event_read,
1075ef59 750 .gpl_only = true,
35578d79
KX
751 .ret_type = RET_INTEGER,
752 .arg1_type = ARG_CONST_MAP_PTR,
753 .arg2_type = ARG_ANYTHING,
754};
755
908432ca
YS
756BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
757 struct bpf_perf_event_value *, buf, u32, size)
758{
759 int err = -EINVAL;
760
761 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
762 goto clear;
763 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
764 &buf->running);
765 if (unlikely(err))
766 goto clear;
767 return 0;
768clear:
769 memset(buf, 0, size);
770 return err;
771}
772
773static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
774 .func = bpf_perf_event_read_value,
775 .gpl_only = true,
776 .ret_type = RET_INTEGER,
777 .arg1_type = ARG_CONST_MAP_PTR,
778 .arg2_type = ARG_ANYTHING,
779 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
780 .arg4_type = ARG_CONST_SIZE,
781};
782
8e7a3920
DB
783static __always_inline u64
784__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
283ca526 785 u64 flags, struct perf_sample_data *sd)
a43eec30 786{
a43eec30 787 struct bpf_array *array = container_of(map, struct bpf_array, map);
d7931330 788 unsigned int cpu = smp_processor_id();
1e33759c 789 u64 index = flags & BPF_F_INDEX_MASK;
3b1efb19 790 struct bpf_event_entry *ee;
a43eec30 791 struct perf_event *event;
a43eec30 792
1e33759c 793 if (index == BPF_F_CURRENT_CPU)
d7931330 794 index = cpu;
a43eec30
AS
795 if (unlikely(index >= array->map.max_entries))
796 return -E2BIG;
797
3b1efb19 798 ee = READ_ONCE(array->ptrs[index]);
1ca1cc98 799 if (!ee)
a43eec30
AS
800 return -ENOENT;
801
3b1efb19 802 event = ee->event;
a43eec30
AS
803 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
804 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
805 return -EINVAL;
806
d7931330 807 if (unlikely(event->oncpu != cpu))
a43eec30
AS
808 return -EOPNOTSUPP;
809
56201969 810 return perf_event_output(event, sd, regs);
a43eec30
AS
811}
812
9594dc3c
MM
813/*
814 * Support executing tracepoints in normal, irq, and nmi context that each call
815 * bpf_perf_event_output
816 */
817struct bpf_trace_sample_data {
818 struct perf_sample_data sds[3];
819};
820
821static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
822static DEFINE_PER_CPU(int, bpf_trace_nest_level);
f3694e00
DB
823BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
824 u64, flags, void *, data, u64, size)
8e7a3920 825{
9594dc3c
MM
826 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
827 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
8e7a3920
DB
828 struct perf_raw_record raw = {
829 .frag = {
830 .size = size,
831 .data = data,
832 },
833 };
9594dc3c
MM
834 struct perf_sample_data *sd;
835 int err;
8e7a3920 836
9594dc3c
MM
837 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
838 err = -EBUSY;
839 goto out;
840 }
841
842 sd = &sds->sds[nest_level - 1];
843
844 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
845 err = -EINVAL;
846 goto out;
847 }
8e7a3920 848
283ca526
DB
849 perf_sample_data_init(sd, 0, 0);
850 sd->raw = &raw;
851
9594dc3c
MM
852 err = __bpf_perf_event_output(regs, map, flags, sd);
853
854out:
855 this_cpu_dec(bpf_trace_nest_level);
856 return err;
8e7a3920
DB
857}
858
a43eec30
AS
859static const struct bpf_func_proto bpf_perf_event_output_proto = {
860 .func = bpf_perf_event_output,
1075ef59 861 .gpl_only = true,
a43eec30
AS
862 .ret_type = RET_INTEGER,
863 .arg1_type = ARG_PTR_TO_CTX,
864 .arg2_type = ARG_CONST_MAP_PTR,
865 .arg3_type = ARG_ANYTHING,
39f19ebb 866 .arg4_type = ARG_PTR_TO_MEM,
a60dd35d 867 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
a43eec30
AS
868};
869
768fb61f
AZ
870static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
871struct bpf_nested_pt_regs {
872 struct pt_regs regs[3];
873};
874static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
875static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
bd570ff9 876
555c8a86
DB
877u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
878 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
bd570ff9 879{
768fb61f 880 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
555c8a86
DB
881 struct perf_raw_frag frag = {
882 .copy = ctx_copy,
883 .size = ctx_size,
884 .data = ctx,
885 };
886 struct perf_raw_record raw = {
887 .frag = {
183fc153
AM
888 {
889 .next = ctx_size ? &frag : NULL,
890 },
555c8a86
DB
891 .size = meta_size,
892 .data = meta,
893 },
894 };
768fb61f
AZ
895 struct perf_sample_data *sd;
896 struct pt_regs *regs;
897 u64 ret;
898
899 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
900 ret = -EBUSY;
901 goto out;
902 }
903 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
904 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
bd570ff9
DB
905
906 perf_fetch_caller_regs(regs);
283ca526
DB
907 perf_sample_data_init(sd, 0, 0);
908 sd->raw = &raw;
bd570ff9 909
768fb61f
AZ
910 ret = __bpf_perf_event_output(regs, map, flags, sd);
911out:
912 this_cpu_dec(bpf_event_output_nest_level);
913 return ret;
bd570ff9
DB
914}
915
f3694e00 916BPF_CALL_0(bpf_get_current_task)
606274c5
AS
917{
918 return (long) current;
919}
920
f470378c 921const struct bpf_func_proto bpf_get_current_task_proto = {
606274c5
AS
922 .func = bpf_get_current_task,
923 .gpl_only = true,
924 .ret_type = RET_INTEGER,
925};
926
f3694e00 927BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
60d20f91 928{
60d20f91
SD
929 struct bpf_array *array = container_of(map, struct bpf_array, map);
930 struct cgroup *cgrp;
60d20f91 931
60d20f91
SD
932 if (unlikely(idx >= array->map.max_entries))
933 return -E2BIG;
934
935 cgrp = READ_ONCE(array->ptrs[idx]);
936 if (unlikely(!cgrp))
937 return -EAGAIN;
938
939 return task_under_cgroup_hierarchy(current, cgrp);
940}
941
942static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
943 .func = bpf_current_task_under_cgroup,
944 .gpl_only = false,
945 .ret_type = RET_INTEGER,
946 .arg1_type = ARG_CONST_MAP_PTR,
947 .arg2_type = ARG_ANYTHING,
948};
949
8b401f9e
YS
950struct send_signal_irq_work {
951 struct irq_work irq_work;
952 struct task_struct *task;
953 u32 sig;
8482941f 954 enum pid_type type;
8b401f9e
YS
955};
956
957static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
958
959static void do_bpf_send_signal(struct irq_work *entry)
960{
961 struct send_signal_irq_work *work;
962
963 work = container_of(entry, struct send_signal_irq_work, irq_work);
8482941f 964 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
8b401f9e
YS
965}
966
8482941f 967static int bpf_send_signal_common(u32 sig, enum pid_type type)
8b401f9e
YS
968{
969 struct send_signal_irq_work *work = NULL;
970
971 /* Similar to bpf_probe_write_user, task needs to be
972 * in a sound condition and kernel memory access be
973 * permitted in order to send signal to the current
974 * task.
975 */
976 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
977 return -EPERM;
978 if (unlikely(uaccess_kernel()))
979 return -EPERM;
980 if (unlikely(!nmi_uaccess_okay()))
981 return -EPERM;
982
1bc7896e 983 if (irqs_disabled()) {
e1afb702
YS
984 /* Do an early check on signal validity. Otherwise,
985 * the error is lost in deferred irq_work.
986 */
987 if (unlikely(!valid_signal(sig)))
988 return -EINVAL;
989
8b401f9e 990 work = this_cpu_ptr(&send_signal_work);
153bedba 991 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY)
8b401f9e
YS
992 return -EBUSY;
993
994 /* Add the current task, which is the target of sending signal,
995 * to the irq_work. The current task may change when queued
996 * irq works get executed.
997 */
998 work->task = current;
999 work->sig = sig;
8482941f 1000 work->type = type;
8b401f9e
YS
1001 irq_work_queue(&work->irq_work);
1002 return 0;
1003 }
1004
8482941f
YS
1005 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
1006}
1007
1008BPF_CALL_1(bpf_send_signal, u32, sig)
1009{
1010 return bpf_send_signal_common(sig, PIDTYPE_TGID);
8b401f9e
YS
1011}
1012
1013static const struct bpf_func_proto bpf_send_signal_proto = {
1014 .func = bpf_send_signal,
1015 .gpl_only = false,
1016 .ret_type = RET_INTEGER,
1017 .arg1_type = ARG_ANYTHING,
1018};
1019
8482941f
YS
1020BPF_CALL_1(bpf_send_signal_thread, u32, sig)
1021{
1022 return bpf_send_signal_common(sig, PIDTYPE_PID);
1023}
1024
1025static const struct bpf_func_proto bpf_send_signal_thread_proto = {
1026 .func = bpf_send_signal_thread,
1027 .gpl_only = false,
1028 .ret_type = RET_INTEGER,
1029 .arg1_type = ARG_ANYTHING,
1030};
1031
fc611f47
KS
1032const struct bpf_func_proto *
1033bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2541517c
AS
1034{
1035 switch (func_id) {
1036 case BPF_FUNC_map_lookup_elem:
1037 return &bpf_map_lookup_elem_proto;
1038 case BPF_FUNC_map_update_elem:
1039 return &bpf_map_update_elem_proto;
1040 case BPF_FUNC_map_delete_elem:
1041 return &bpf_map_delete_elem_proto;
02a8c817
AC
1042 case BPF_FUNC_map_push_elem:
1043 return &bpf_map_push_elem_proto;
1044 case BPF_FUNC_map_pop_elem:
1045 return &bpf_map_pop_elem_proto;
1046 case BPF_FUNC_map_peek_elem:
1047 return &bpf_map_peek_elem_proto;
d9847d31
AS
1048 case BPF_FUNC_ktime_get_ns:
1049 return &bpf_ktime_get_ns_proto;
71d19214
MÅ»
1050 case BPF_FUNC_ktime_get_boot_ns:
1051 return &bpf_ktime_get_boot_ns_proto;
04fd61ab
AS
1052 case BPF_FUNC_tail_call:
1053 return &bpf_tail_call_proto;
ffeedafb
AS
1054 case BPF_FUNC_get_current_pid_tgid:
1055 return &bpf_get_current_pid_tgid_proto;
606274c5
AS
1056 case BPF_FUNC_get_current_task:
1057 return &bpf_get_current_task_proto;
ffeedafb
AS
1058 case BPF_FUNC_get_current_uid_gid:
1059 return &bpf_get_current_uid_gid_proto;
1060 case BPF_FUNC_get_current_comm:
1061 return &bpf_get_current_comm_proto;
9c959c86 1062 case BPF_FUNC_trace_printk:
0756ea3e 1063 return bpf_get_trace_printk_proto();
ab1973d3
AS
1064 case BPF_FUNC_get_smp_processor_id:
1065 return &bpf_get_smp_processor_id_proto;
2d0e30c3
DB
1066 case BPF_FUNC_get_numa_node_id:
1067 return &bpf_get_numa_node_id_proto;
35578d79
KX
1068 case BPF_FUNC_perf_event_read:
1069 return &bpf_perf_event_read_proto;
96ae5227
SD
1070 case BPF_FUNC_probe_write_user:
1071 return bpf_get_probe_write_proto();
60d20f91
SD
1072 case BPF_FUNC_current_task_under_cgroup:
1073 return &bpf_current_task_under_cgroup_proto;
8937bd80
AS
1074 case BPF_FUNC_get_prandom_u32:
1075 return &bpf_get_prandom_u32_proto;
6ae08ae3
DB
1076 case BPF_FUNC_probe_read_user:
1077 return &bpf_probe_read_user_proto;
1078 case BPF_FUNC_probe_read_kernel:
1079 return &bpf_probe_read_kernel_proto;
6ae08ae3
DB
1080 case BPF_FUNC_probe_read_user_str:
1081 return &bpf_probe_read_user_str_proto;
1082 case BPF_FUNC_probe_read_kernel_str:
1083 return &bpf_probe_read_kernel_str_proto;
0ebeea8c
DB
1084#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1085 case BPF_FUNC_probe_read:
1086 return &bpf_probe_read_compat_proto;
a5e8c070 1087 case BPF_FUNC_probe_read_str:
6ae08ae3 1088 return &bpf_probe_read_compat_str_proto;
0ebeea8c 1089#endif
34ea38ca 1090#ifdef CONFIG_CGROUPS
bf6fa2c8
YS
1091 case BPF_FUNC_get_current_cgroup_id:
1092 return &bpf_get_current_cgroup_id_proto;
34ea38ca 1093#endif
8b401f9e
YS
1094 case BPF_FUNC_send_signal:
1095 return &bpf_send_signal_proto;
8482941f
YS
1096 case BPF_FUNC_send_signal_thread:
1097 return &bpf_send_signal_thread_proto;
b80b033b
SL
1098 case BPF_FUNC_perf_event_read_value:
1099 return &bpf_perf_event_read_value_proto;
b4490c5c
CN
1100 case BPF_FUNC_get_ns_current_pid_tgid:
1101 return &bpf_get_ns_current_pid_tgid_proto;
457f4436
AN
1102 case BPF_FUNC_ringbuf_output:
1103 return &bpf_ringbuf_output_proto;
1104 case BPF_FUNC_ringbuf_reserve:
1105 return &bpf_ringbuf_reserve_proto;
1106 case BPF_FUNC_ringbuf_submit:
1107 return &bpf_ringbuf_submit_proto;
1108 case BPF_FUNC_ringbuf_discard:
1109 return &bpf_ringbuf_discard_proto;
1110 case BPF_FUNC_ringbuf_query:
1111 return &bpf_ringbuf_query_proto;
9fd82b61
AS
1112 default:
1113 return NULL;
1114 }
1115}
1116
5e43f899
AI
1117static const struct bpf_func_proto *
1118kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
9fd82b61
AS
1119{
1120 switch (func_id) {
a43eec30
AS
1121 case BPF_FUNC_perf_event_output:
1122 return &bpf_perf_event_output_proto;
d5a3b1f6
AS
1123 case BPF_FUNC_get_stackid:
1124 return &bpf_get_stackid_proto;
c195651e
YS
1125 case BPF_FUNC_get_stack:
1126 return &bpf_get_stack_proto;
9802d865
JB
1127#ifdef CONFIG_BPF_KPROBE_OVERRIDE
1128 case BPF_FUNC_override_return:
1129 return &bpf_override_return_proto;
1130#endif
2541517c 1131 default:
fc611f47 1132 return bpf_tracing_func_proto(func_id, prog);
2541517c
AS
1133 }
1134}
1135
1136/* bpf+kprobe programs can access fields of 'struct pt_regs' */
19de99f7 1137static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
5e43f899 1138 const struct bpf_prog *prog,
23994631 1139 struct bpf_insn_access_aux *info)
2541517c 1140{
2541517c
AS
1141 if (off < 0 || off >= sizeof(struct pt_regs))
1142 return false;
2541517c
AS
1143 if (type != BPF_READ)
1144 return false;
2541517c
AS
1145 if (off % size != 0)
1146 return false;
2d071c64
DB
1147 /*
1148 * Assertion for 32 bit to make sure last 8 byte access
1149 * (BPF_DW) to the last 4 byte member is disallowed.
1150 */
1151 if (off + size > sizeof(struct pt_regs))
1152 return false;
1153
2541517c
AS
1154 return true;
1155}
1156
7de16e3a 1157const struct bpf_verifier_ops kprobe_verifier_ops = {
2541517c
AS
1158 .get_func_proto = kprobe_prog_func_proto,
1159 .is_valid_access = kprobe_prog_is_valid_access,
1160};
1161
7de16e3a
JK
1162const struct bpf_prog_ops kprobe_prog_ops = {
1163};
1164
f3694e00
DB
1165BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1166 u64, flags, void *, data, u64, size)
9940d67c 1167{
f3694e00
DB
1168 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1169
9940d67c
AS
1170 /*
1171 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1172 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
f3694e00 1173 * from there and call the same bpf_perf_event_output() helper inline.
9940d67c 1174 */
f3694e00 1175 return ____bpf_perf_event_output(regs, map, flags, data, size);
9940d67c
AS
1176}
1177
1178static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1179 .func = bpf_perf_event_output_tp,
1180 .gpl_only = true,
1181 .ret_type = RET_INTEGER,
1182 .arg1_type = ARG_PTR_TO_CTX,
1183 .arg2_type = ARG_CONST_MAP_PTR,
1184 .arg3_type = ARG_ANYTHING,
39f19ebb 1185 .arg4_type = ARG_PTR_TO_MEM,
a60dd35d 1186 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
9940d67c
AS
1187};
1188
f3694e00
DB
1189BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1190 u64, flags)
9940d67c 1191{
f3694e00 1192 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
9940d67c 1193
f3694e00
DB
1194 /*
1195 * Same comment as in bpf_perf_event_output_tp(), only that this time
1196 * the other helper's function body cannot be inlined due to being
1197 * external, thus we need to call raw helper function.
1198 */
1199 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1200 flags, 0, 0);
9940d67c
AS
1201}
1202
1203static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1204 .func = bpf_get_stackid_tp,
1205 .gpl_only = true,
1206 .ret_type = RET_INTEGER,
1207 .arg1_type = ARG_PTR_TO_CTX,
1208 .arg2_type = ARG_CONST_MAP_PTR,
1209 .arg3_type = ARG_ANYTHING,
1210};
1211
c195651e
YS
1212BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1213 u64, flags)
1214{
1215 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1216
1217 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1218 (unsigned long) size, flags, 0);
1219}
1220
1221static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1222 .func = bpf_get_stack_tp,
1223 .gpl_only = true,
1224 .ret_type = RET_INTEGER,
1225 .arg1_type = ARG_PTR_TO_CTX,
1226 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1227 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1228 .arg4_type = ARG_ANYTHING,
1229};
1230
5e43f899
AI
1231static const struct bpf_func_proto *
1232tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
f005afed
YS
1233{
1234 switch (func_id) {
1235 case BPF_FUNC_perf_event_output:
1236 return &bpf_perf_event_output_proto_tp;
1237 case BPF_FUNC_get_stackid:
1238 return &bpf_get_stackid_proto_tp;
c195651e
YS
1239 case BPF_FUNC_get_stack:
1240 return &bpf_get_stack_proto_tp;
f005afed 1241 default:
fc611f47 1242 return bpf_tracing_func_proto(func_id, prog);
f005afed
YS
1243 }
1244}
1245
1246static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
5e43f899 1247 const struct bpf_prog *prog,
f005afed
YS
1248 struct bpf_insn_access_aux *info)
1249{
1250 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1251 return false;
1252 if (type != BPF_READ)
1253 return false;
1254 if (off % size != 0)
1255 return false;
1256
1257 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1258 return true;
1259}
1260
1261const struct bpf_verifier_ops tracepoint_verifier_ops = {
1262 .get_func_proto = tp_prog_func_proto,
1263 .is_valid_access = tp_prog_is_valid_access,
1264};
1265
1266const struct bpf_prog_ops tracepoint_prog_ops = {
1267};
1268
1269BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
4bebdc7a
YS
1270 struct bpf_perf_event_value *, buf, u32, size)
1271{
1272 int err = -EINVAL;
1273
1274 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1275 goto clear;
1276 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1277 &buf->running);
1278 if (unlikely(err))
1279 goto clear;
1280 return 0;
1281clear:
1282 memset(buf, 0, size);
1283 return err;
1284}
1285
f005afed
YS
1286static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1287 .func = bpf_perf_prog_read_value,
4bebdc7a
YS
1288 .gpl_only = true,
1289 .ret_type = RET_INTEGER,
1290 .arg1_type = ARG_PTR_TO_CTX,
1291 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1292 .arg3_type = ARG_CONST_SIZE,
1293};
1294
fff7b643
DX
1295BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1296 void *, buf, u32, size, u64, flags)
1297{
1298#ifndef CONFIG_X86
1299 return -ENOENT;
1300#else
1301 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1302 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1303 u32 to_copy;
1304
1305 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1306 return -EINVAL;
1307
1308 if (unlikely(!br_stack))
1309 return -EINVAL;
1310
1311 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1312 return br_stack->nr * br_entry_size;
1313
1314 if (!buf || (size % br_entry_size != 0))
1315 return -EINVAL;
1316
1317 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1318 memcpy(buf, br_stack->entries, to_copy);
1319
1320 return to_copy;
1321#endif
1322}
1323
1324static const struct bpf_func_proto bpf_read_branch_records_proto = {
1325 .func = bpf_read_branch_records,
1326 .gpl_only = true,
1327 .ret_type = RET_INTEGER,
1328 .arg1_type = ARG_PTR_TO_CTX,
1329 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1330 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1331 .arg4_type = ARG_ANYTHING,
1332};
1333
5e43f899
AI
1334static const struct bpf_func_proto *
1335pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
9fd82b61
AS
1336{
1337 switch (func_id) {
1338 case BPF_FUNC_perf_event_output:
9940d67c 1339 return &bpf_perf_event_output_proto_tp;
9fd82b61 1340 case BPF_FUNC_get_stackid:
9940d67c 1341 return &bpf_get_stackid_proto_tp;
c195651e
YS
1342 case BPF_FUNC_get_stack:
1343 return &bpf_get_stack_proto_tp;
4bebdc7a 1344 case BPF_FUNC_perf_prog_read_value:
f005afed 1345 return &bpf_perf_prog_read_value_proto;
fff7b643
DX
1346 case BPF_FUNC_read_branch_records:
1347 return &bpf_read_branch_records_proto;
9fd82b61 1348 default:
fc611f47 1349 return bpf_tracing_func_proto(func_id, prog);
9fd82b61
AS
1350 }
1351}
1352
c4f6699d
AS
1353/*
1354 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1355 * to avoid potential recursive reuse issue when/if tracepoints are added
9594dc3c
MM
1356 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1357 *
1358 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1359 * in normal, irq, and nmi context.
c4f6699d 1360 */
9594dc3c
MM
1361struct bpf_raw_tp_regs {
1362 struct pt_regs regs[3];
1363};
1364static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1365static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1366static struct pt_regs *get_bpf_raw_tp_regs(void)
1367{
1368 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1369 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1370
1371 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1372 this_cpu_dec(bpf_raw_tp_nest_level);
1373 return ERR_PTR(-EBUSY);
1374 }
1375
1376 return &tp_regs->regs[nest_level - 1];
1377}
1378
1379static void put_bpf_raw_tp_regs(void)
1380{
1381 this_cpu_dec(bpf_raw_tp_nest_level);
1382}
1383
c4f6699d
AS
1384BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1385 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1386{
9594dc3c
MM
1387 struct pt_regs *regs = get_bpf_raw_tp_regs();
1388 int ret;
1389
1390 if (IS_ERR(regs))
1391 return PTR_ERR(regs);
c4f6699d
AS
1392
1393 perf_fetch_caller_regs(regs);
9594dc3c
MM
1394 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1395
1396 put_bpf_raw_tp_regs();
1397 return ret;
c4f6699d
AS
1398}
1399
1400static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1401 .func = bpf_perf_event_output_raw_tp,
1402 .gpl_only = true,
1403 .ret_type = RET_INTEGER,
1404 .arg1_type = ARG_PTR_TO_CTX,
1405 .arg2_type = ARG_CONST_MAP_PTR,
1406 .arg3_type = ARG_ANYTHING,
1407 .arg4_type = ARG_PTR_TO_MEM,
1408 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1409};
1410
a7658e1a 1411extern const struct bpf_func_proto bpf_skb_output_proto;
d831ee84 1412extern const struct bpf_func_proto bpf_xdp_output_proto;
a7658e1a 1413
c4f6699d
AS
1414BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1415 struct bpf_map *, map, u64, flags)
1416{
9594dc3c
MM
1417 struct pt_regs *regs = get_bpf_raw_tp_regs();
1418 int ret;
1419
1420 if (IS_ERR(regs))
1421 return PTR_ERR(regs);
c4f6699d
AS
1422
1423 perf_fetch_caller_regs(regs);
1424 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
9594dc3c
MM
1425 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1426 flags, 0, 0);
1427 put_bpf_raw_tp_regs();
1428 return ret;
c4f6699d
AS
1429}
1430
1431static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1432 .func = bpf_get_stackid_raw_tp,
1433 .gpl_only = true,
1434 .ret_type = RET_INTEGER,
1435 .arg1_type = ARG_PTR_TO_CTX,
1436 .arg2_type = ARG_CONST_MAP_PTR,
1437 .arg3_type = ARG_ANYTHING,
1438};
1439
c195651e
YS
1440BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1441 void *, buf, u32, size, u64, flags)
1442{
9594dc3c
MM
1443 struct pt_regs *regs = get_bpf_raw_tp_regs();
1444 int ret;
1445
1446 if (IS_ERR(regs))
1447 return PTR_ERR(regs);
c195651e
YS
1448
1449 perf_fetch_caller_regs(regs);
9594dc3c
MM
1450 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1451 (unsigned long) size, flags, 0);
1452 put_bpf_raw_tp_regs();
1453 return ret;
c195651e
YS
1454}
1455
1456static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1457 .func = bpf_get_stack_raw_tp,
1458 .gpl_only = true,
1459 .ret_type = RET_INTEGER,
1460 .arg1_type = ARG_PTR_TO_CTX,
1461 .arg2_type = ARG_PTR_TO_MEM,
1462 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1463 .arg4_type = ARG_ANYTHING,
1464};
1465
5e43f899
AI
1466static const struct bpf_func_proto *
1467raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
c4f6699d
AS
1468{
1469 switch (func_id) {
1470 case BPF_FUNC_perf_event_output:
1471 return &bpf_perf_event_output_proto_raw_tp;
1472 case BPF_FUNC_get_stackid:
1473 return &bpf_get_stackid_proto_raw_tp;
c195651e
YS
1474 case BPF_FUNC_get_stack:
1475 return &bpf_get_stack_proto_raw_tp;
c4f6699d 1476 default:
fc611f47 1477 return bpf_tracing_func_proto(func_id, prog);
c4f6699d
AS
1478 }
1479}
1480
958a3f2d 1481const struct bpf_func_proto *
f1b9509c
AS
1482tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1483{
1484 switch (func_id) {
1485#ifdef CONFIG_NET
1486 case BPF_FUNC_skb_output:
1487 return &bpf_skb_output_proto;
d831ee84
EC
1488 case BPF_FUNC_xdp_output:
1489 return &bpf_xdp_output_proto;
f1b9509c 1490#endif
492e639f
YS
1491 case BPF_FUNC_seq_printf:
1492 return prog->expected_attach_type == BPF_TRACE_ITER ?
1493 &bpf_seq_printf_proto :
1494 NULL;
1495 case BPF_FUNC_seq_write:
1496 return prog->expected_attach_type == BPF_TRACE_ITER ?
1497 &bpf_seq_write_proto :
1498 NULL;
f1b9509c
AS
1499 default:
1500 return raw_tp_prog_func_proto(func_id, prog);
1501 }
1502}
1503
c4f6699d
AS
1504static bool raw_tp_prog_is_valid_access(int off, int size,
1505 enum bpf_access_type type,
5e43f899 1506 const struct bpf_prog *prog,
c4f6699d
AS
1507 struct bpf_insn_access_aux *info)
1508{
f1b9509c
AS
1509 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1510 return false;
1511 if (type != BPF_READ)
1512 return false;
1513 if (off % size != 0)
1514 return false;
1515 return true;
1516}
1517
1518static bool tracing_prog_is_valid_access(int off, int size,
1519 enum bpf_access_type type,
1520 const struct bpf_prog *prog,
1521 struct bpf_insn_access_aux *info)
1522{
1523 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
c4f6699d
AS
1524 return false;
1525 if (type != BPF_READ)
1526 return false;
1527 if (off % size != 0)
1528 return false;
9e15db66 1529 return btf_ctx_access(off, size, type, prog, info);
c4f6699d
AS
1530}
1531
3e7c67d9
KS
1532int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1533 const union bpf_attr *kattr,
1534 union bpf_attr __user *uattr)
1535{
1536 return -ENOTSUPP;
1537}
1538
c4f6699d
AS
1539const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1540 .get_func_proto = raw_tp_prog_func_proto,
1541 .is_valid_access = raw_tp_prog_is_valid_access,
1542};
1543
1544const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1545};
1546
f1b9509c
AS
1547const struct bpf_verifier_ops tracing_verifier_ops = {
1548 .get_func_proto = tracing_prog_func_proto,
1549 .is_valid_access = tracing_prog_is_valid_access,
1550};
1551
1552const struct bpf_prog_ops tracing_prog_ops = {
da00d2f1 1553 .test_run = bpf_prog_test_run_tracing,
f1b9509c
AS
1554};
1555
9df1c28b
MM
1556static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1557 enum bpf_access_type type,
1558 const struct bpf_prog *prog,
1559 struct bpf_insn_access_aux *info)
1560{
1561 if (off == 0) {
1562 if (size != sizeof(u64) || type != BPF_READ)
1563 return false;
1564 info->reg_type = PTR_TO_TP_BUFFER;
1565 }
1566 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1567}
1568
1569const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1570 .get_func_proto = raw_tp_prog_func_proto,
1571 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1572};
1573
1574const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1575};
1576
0515e599 1577static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
5e43f899 1578 const struct bpf_prog *prog,
23994631 1579 struct bpf_insn_access_aux *info)
0515e599 1580{
95da0cdb 1581 const int size_u64 = sizeof(u64);
31fd8581 1582
0515e599
AS
1583 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1584 return false;
1585 if (type != BPF_READ)
1586 return false;
bc23105c
DB
1587 if (off % size != 0) {
1588 if (sizeof(unsigned long) != 4)
1589 return false;
1590 if (size != 8)
1591 return false;
1592 if (off % size != 4)
1593 return false;
1594 }
31fd8581 1595
f96da094
DB
1596 switch (off) {
1597 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
95da0cdb
TQ
1598 bpf_ctx_record_field_size(info, size_u64);
1599 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1600 return false;
1601 break;
1602 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1603 bpf_ctx_record_field_size(info, size_u64);
1604 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
23994631 1605 return false;
f96da094
DB
1606 break;
1607 default:
0515e599
AS
1608 if (size != sizeof(long))
1609 return false;
1610 }
f96da094 1611
0515e599
AS
1612 return true;
1613}
1614
6b8cc1d1
DB
1615static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1616 const struct bpf_insn *si,
0515e599 1617 struct bpf_insn *insn_buf,
f96da094 1618 struct bpf_prog *prog, u32 *target_size)
0515e599
AS
1619{
1620 struct bpf_insn *insn = insn_buf;
1621
6b8cc1d1 1622 switch (si->off) {
0515e599 1623 case offsetof(struct bpf_perf_event_data, sample_period):
f035a515 1624 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
6b8cc1d1 1625 data), si->dst_reg, si->src_reg,
0515e599 1626 offsetof(struct bpf_perf_event_data_kern, data));
6b8cc1d1 1627 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
f96da094
DB
1628 bpf_target_off(struct perf_sample_data, period, 8,
1629 target_size));
0515e599 1630 break;
95da0cdb
TQ
1631 case offsetof(struct bpf_perf_event_data, addr):
1632 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1633 data), si->dst_reg, si->src_reg,
1634 offsetof(struct bpf_perf_event_data_kern, data));
1635 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1636 bpf_target_off(struct perf_sample_data, addr, 8,
1637 target_size));
1638 break;
0515e599 1639 default:
f035a515 1640 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
6b8cc1d1 1641 regs), si->dst_reg, si->src_reg,
0515e599 1642 offsetof(struct bpf_perf_event_data_kern, regs));
6b8cc1d1
DB
1643 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1644 si->off);
0515e599
AS
1645 break;
1646 }
1647
1648 return insn - insn_buf;
1649}
1650
7de16e3a 1651const struct bpf_verifier_ops perf_event_verifier_ops = {
f005afed 1652 .get_func_proto = pe_prog_func_proto,
0515e599
AS
1653 .is_valid_access = pe_prog_is_valid_access,
1654 .convert_ctx_access = pe_prog_convert_ctx_access,
1655};
7de16e3a
JK
1656
1657const struct bpf_prog_ops perf_event_prog_ops = {
1658};
e87c6bc3
YS
1659
1660static DEFINE_MUTEX(bpf_event_mutex);
1661
c8c088ba
YS
1662#define BPF_TRACE_MAX_PROGS 64
1663
e87c6bc3
YS
1664int perf_event_attach_bpf_prog(struct perf_event *event,
1665 struct bpf_prog *prog)
1666{
e672db03 1667 struct bpf_prog_array *old_array;
e87c6bc3
YS
1668 struct bpf_prog_array *new_array;
1669 int ret = -EEXIST;
1670
9802d865 1671 /*
b4da3340
MH
1672 * Kprobe override only works if they are on the function entry,
1673 * and only if they are on the opt-in list.
9802d865
JB
1674 */
1675 if (prog->kprobe_override &&
b4da3340 1676 (!trace_kprobe_on_func_entry(event->tp_event) ||
9802d865
JB
1677 !trace_kprobe_error_injectable(event->tp_event)))
1678 return -EINVAL;
1679
e87c6bc3
YS
1680 mutex_lock(&bpf_event_mutex);
1681
1682 if (event->prog)
07c41a29 1683 goto unlock;
e87c6bc3 1684
e672db03 1685 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
c8c088ba
YS
1686 if (old_array &&
1687 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1688 ret = -E2BIG;
1689 goto unlock;
1690 }
1691
e87c6bc3
YS
1692 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1693 if (ret < 0)
07c41a29 1694 goto unlock;
e87c6bc3
YS
1695
1696 /* set the new array to event->tp_event and set event->prog */
1697 event->prog = prog;
1698 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1699 bpf_prog_array_free(old_array);
1700
07c41a29 1701unlock:
e87c6bc3
YS
1702 mutex_unlock(&bpf_event_mutex);
1703 return ret;
1704}
1705
1706void perf_event_detach_bpf_prog(struct perf_event *event)
1707{
e672db03 1708 struct bpf_prog_array *old_array;
e87c6bc3
YS
1709 struct bpf_prog_array *new_array;
1710 int ret;
1711
1712 mutex_lock(&bpf_event_mutex);
1713
1714 if (!event->prog)
07c41a29 1715 goto unlock;
e87c6bc3 1716
e672db03 1717 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
e87c6bc3 1718 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
170a7e3e
SY
1719 if (ret == -ENOENT)
1720 goto unlock;
e87c6bc3
YS
1721 if (ret < 0) {
1722 bpf_prog_array_delete_safe(old_array, event->prog);
1723 } else {
1724 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1725 bpf_prog_array_free(old_array);
1726 }
1727
1728 bpf_prog_put(event->prog);
1729 event->prog = NULL;
1730
07c41a29 1731unlock:
e87c6bc3
YS
1732 mutex_unlock(&bpf_event_mutex);
1733}
f371b304 1734
f4e2298e 1735int perf_event_query_prog_array(struct perf_event *event, void __user *info)
f371b304
YS
1736{
1737 struct perf_event_query_bpf __user *uquery = info;
1738 struct perf_event_query_bpf query = {};
e672db03 1739 struct bpf_prog_array *progs;
3a38bb98 1740 u32 *ids, prog_cnt, ids_len;
f371b304
YS
1741 int ret;
1742
031258da 1743 if (!perfmon_capable())
f371b304
YS
1744 return -EPERM;
1745 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1746 return -EINVAL;
1747 if (copy_from_user(&query, uquery, sizeof(query)))
1748 return -EFAULT;
3a38bb98
YS
1749
1750 ids_len = query.ids_len;
1751 if (ids_len > BPF_TRACE_MAX_PROGS)
9c481b90 1752 return -E2BIG;
3a38bb98
YS
1753 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1754 if (!ids)
1755 return -ENOMEM;
1756 /*
1757 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1758 * is required when user only wants to check for uquery->prog_cnt.
1759 * There is no need to check for it since the case is handled
1760 * gracefully in bpf_prog_array_copy_info.
1761 */
f371b304
YS
1762
1763 mutex_lock(&bpf_event_mutex);
e672db03
SF
1764 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
1765 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
f371b304
YS
1766 mutex_unlock(&bpf_event_mutex);
1767
3a38bb98
YS
1768 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1769 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1770 ret = -EFAULT;
1771
1772 kfree(ids);
f371b304
YS
1773 return ret;
1774}
c4f6699d
AS
1775
1776extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1777extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1778
a38d1107 1779struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
c4f6699d
AS
1780{
1781 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1782
1783 for (; btp < __stop__bpf_raw_tp; btp++) {
1784 if (!strcmp(btp->tp->name, name))
1785 return btp;
1786 }
a38d1107
MM
1787
1788 return bpf_get_raw_tracepoint_module(name);
1789}
1790
1791void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
1792{
1793 struct module *mod = __module_address((unsigned long)btp);
1794
1795 if (mod)
1796 module_put(mod);
c4f6699d
AS
1797}
1798
1799static __always_inline
1800void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1801{
f03efe49 1802 cant_sleep();
c4f6699d 1803 rcu_read_lock();
c4f6699d 1804 (void) BPF_PROG_RUN(prog, args);
c4f6699d
AS
1805 rcu_read_unlock();
1806}
1807
1808#define UNPACK(...) __VA_ARGS__
1809#define REPEAT_1(FN, DL, X, ...) FN(X)
1810#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1811#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1812#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1813#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1814#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1815#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1816#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1817#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1818#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1819#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1820#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1821#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1822
1823#define SARG(X) u64 arg##X
1824#define COPY(X) args[X] = arg##X
1825
1826#define __DL_COM (,)
1827#define __DL_SEM (;)
1828
1829#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1830
1831#define BPF_TRACE_DEFN_x(x) \
1832 void bpf_trace_run##x(struct bpf_prog *prog, \
1833 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1834 { \
1835 u64 args[x]; \
1836 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1837 __bpf_trace_run(prog, args); \
1838 } \
1839 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1840BPF_TRACE_DEFN_x(1);
1841BPF_TRACE_DEFN_x(2);
1842BPF_TRACE_DEFN_x(3);
1843BPF_TRACE_DEFN_x(4);
1844BPF_TRACE_DEFN_x(5);
1845BPF_TRACE_DEFN_x(6);
1846BPF_TRACE_DEFN_x(7);
1847BPF_TRACE_DEFN_x(8);
1848BPF_TRACE_DEFN_x(9);
1849BPF_TRACE_DEFN_x(10);
1850BPF_TRACE_DEFN_x(11);
1851BPF_TRACE_DEFN_x(12);
1852
1853static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1854{
1855 struct tracepoint *tp = btp->tp;
1856
1857 /*
1858 * check that program doesn't access arguments beyond what's
1859 * available in this tracepoint
1860 */
1861 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1862 return -EINVAL;
1863
9df1c28b
MM
1864 if (prog->aux->max_tp_access > btp->writable_size)
1865 return -EINVAL;
1866
c4f6699d
AS
1867 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1868}
1869
1870int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1871{
e16ec340 1872 return __bpf_probe_register(btp, prog);
c4f6699d
AS
1873}
1874
1875int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1876{
e16ec340 1877 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
c4f6699d 1878}
41bdc4b4
YS
1879
1880int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1881 u32 *fd_type, const char **buf,
1882 u64 *probe_offset, u64 *probe_addr)
1883{
1884 bool is_tracepoint, is_syscall_tp;
1885 struct bpf_prog *prog;
1886 int flags, err = 0;
1887
1888 prog = event->prog;
1889 if (!prog)
1890 return -ENOENT;
1891
1892 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1893 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1894 return -EOPNOTSUPP;
1895
1896 *prog_id = prog->aux->id;
1897 flags = event->tp_event->flags;
1898 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1899 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1900
1901 if (is_tracepoint || is_syscall_tp) {
1902 *buf = is_tracepoint ? event->tp_event->tp->name
1903 : event->tp_event->name;
1904 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1905 *probe_offset = 0x0;
1906 *probe_addr = 0x0;
1907 } else {
1908 /* kprobe/uprobe */
1909 err = -EOPNOTSUPP;
1910#ifdef CONFIG_KPROBE_EVENTS
1911 if (flags & TRACE_EVENT_FL_KPROBE)
1912 err = bpf_get_kprobe_info(event, fd_type, buf,
1913 probe_offset, probe_addr,
1914 event->attr.type == PERF_TYPE_TRACEPOINT);
1915#endif
1916#ifdef CONFIG_UPROBE_EVENTS
1917 if (flags & TRACE_EVENT_FL_UPROBE)
1918 err = bpf_get_uprobe_info(event, fd_type, buf,
1919 probe_offset,
1920 event->attr.type == PERF_TYPE_TRACEPOINT);
1921#endif
1922 }
1923
1924 return err;
1925}
a38d1107 1926
9db1ff0a
YS
1927static int __init send_signal_irq_work_init(void)
1928{
1929 int cpu;
1930 struct send_signal_irq_work *work;
1931
1932 for_each_possible_cpu(cpu) {
1933 work = per_cpu_ptr(&send_signal_work, cpu);
1934 init_irq_work(&work->irq_work, do_bpf_send_signal);
1935 }
1936 return 0;
1937}
1938
1939subsys_initcall(send_signal_irq_work_init);
1940
a38d1107 1941#ifdef CONFIG_MODULES
390e99cf
SF
1942static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
1943 void *module)
a38d1107
MM
1944{
1945 struct bpf_trace_module *btm, *tmp;
1946 struct module *mod = module;
1947
1948 if (mod->num_bpf_raw_events == 0 ||
1949 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
1950 return 0;
1951
1952 mutex_lock(&bpf_module_mutex);
1953
1954 switch (op) {
1955 case MODULE_STATE_COMING:
1956 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
1957 if (btm) {
1958 btm->module = module;
1959 list_add(&btm->list, &bpf_trace_modules);
1960 }
1961 break;
1962 case MODULE_STATE_GOING:
1963 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
1964 if (btm->module == module) {
1965 list_del(&btm->list);
1966 kfree(btm);
1967 break;
1968 }
1969 }
1970 break;
1971 }
1972
1973 mutex_unlock(&bpf_module_mutex);
1974
1975 return 0;
1976}
1977
1978static struct notifier_block bpf_module_nb = {
1979 .notifier_call = bpf_event_notify,
1980};
1981
390e99cf 1982static int __init bpf_event_init(void)
a38d1107
MM
1983{
1984 register_module_notifier(&bpf_module_nb);
1985 return 0;
1986}
1987
1988fs_initcall(bpf_event_init);
1989#endif /* CONFIG_MODULES */