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