xref: /openbmc/linux/kernel/trace/bpf_trace.c (revision 9dae47aba0a055f761176d9297371d5bb24289ec)
1 /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
2  * Copyright (c) 2016 Facebook
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  */
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/bpf.h>
12 #include <linux/bpf_perf_event.h>
13 #include <linux/filter.h>
14 #include <linux/uaccess.h>
15 #include <linux/ctype.h>
16 #include <linux/kprobes.h>
17 #include <asm/kprobes.h>
18 
19 #include "trace_probe.h"
20 #include "trace.h"
21 
22 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
23 
24 /**
25  * trace_call_bpf - invoke BPF program
26  * @call: tracepoint event
27  * @ctx: opaque context pointer
28  *
29  * kprobe handlers execute BPF programs via this helper.
30  * Can be used from static tracepoints in the future.
31  *
32  * Return: BPF programs always return an integer which is interpreted by
33  * kprobe handler as:
34  * 0 - return from kprobe (event is filtered out)
35  * 1 - store kprobe event into ring buffer
36  * Other values are reserved and currently alias to 1
37  */
38 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
39 {
40 	unsigned int ret;
41 
42 	if (in_nmi()) /* not supported yet */
43 		return 1;
44 
45 	preempt_disable();
46 
47 	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
48 		/*
49 		 * since some bpf program is already running on this cpu,
50 		 * don't call into another bpf program (same or different)
51 		 * and don't send kprobe event into ring-buffer,
52 		 * so return zero here
53 		 */
54 		ret = 0;
55 		goto out;
56 	}
57 
58 	/*
59 	 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
60 	 * to all call sites, we did a bpf_prog_array_valid() there to check
61 	 * whether call->prog_array is empty or not, which is
62 	 * a heurisitc to speed up execution.
63 	 *
64 	 * If bpf_prog_array_valid() fetched prog_array was
65 	 * non-NULL, we go into trace_call_bpf() and do the actual
66 	 * proper rcu_dereference() under RCU lock.
67 	 * If it turns out that prog_array is NULL then, we bail out.
68 	 * For the opposite, if the bpf_prog_array_valid() fetched pointer
69 	 * was NULL, you'll skip the prog_array with the risk of missing
70 	 * out of events when it was updated in between this and the
71 	 * rcu_dereference() which is accepted risk.
72 	 */
73 	ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
74 
75  out:
76 	__this_cpu_dec(bpf_prog_active);
77 	preempt_enable();
78 
79 	return ret;
80 }
81 EXPORT_SYMBOL_GPL(trace_call_bpf);
82 
83 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
84 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
85 {
86 	__this_cpu_write(bpf_kprobe_override, 1);
87 	regs_set_return_value(regs, rc);
88 	arch_ftrace_kprobe_override_function(regs);
89 	return 0;
90 }
91 
92 static const struct bpf_func_proto bpf_override_return_proto = {
93 	.func		= bpf_override_return,
94 	.gpl_only	= true,
95 	.ret_type	= RET_INTEGER,
96 	.arg1_type	= ARG_PTR_TO_CTX,
97 	.arg2_type	= ARG_ANYTHING,
98 };
99 #endif
100 
101 BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
102 {
103 	int ret;
104 
105 	ret = probe_kernel_read(dst, unsafe_ptr, size);
106 	if (unlikely(ret < 0))
107 		memset(dst, 0, size);
108 
109 	return ret;
110 }
111 
112 static const struct bpf_func_proto bpf_probe_read_proto = {
113 	.func		= bpf_probe_read,
114 	.gpl_only	= true,
115 	.ret_type	= RET_INTEGER,
116 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
117 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
118 	.arg3_type	= ARG_ANYTHING,
119 };
120 
121 BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
122 	   u32, size)
123 {
124 	/*
125 	 * Ensure we're in user context which is safe for the helper to
126 	 * run. This helper has no business in a kthread.
127 	 *
128 	 * access_ok() should prevent writing to non-user memory, but in
129 	 * some situations (nommu, temporary switch, etc) access_ok() does
130 	 * not provide enough validation, hence the check on KERNEL_DS.
131 	 */
132 
133 	if (unlikely(in_interrupt() ||
134 		     current->flags & (PF_KTHREAD | PF_EXITING)))
135 		return -EPERM;
136 	if (unlikely(uaccess_kernel()))
137 		return -EPERM;
138 	if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
139 		return -EPERM;
140 
141 	return probe_kernel_write(unsafe_ptr, src, size);
142 }
143 
144 static const struct bpf_func_proto bpf_probe_write_user_proto = {
145 	.func		= bpf_probe_write_user,
146 	.gpl_only	= true,
147 	.ret_type	= RET_INTEGER,
148 	.arg1_type	= ARG_ANYTHING,
149 	.arg2_type	= ARG_PTR_TO_MEM,
150 	.arg3_type	= ARG_CONST_SIZE,
151 };
152 
153 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
154 {
155 	pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
156 			    current->comm, task_pid_nr(current));
157 
158 	return &bpf_probe_write_user_proto;
159 }
160 
161 /*
162  * Only limited trace_printk() conversion specifiers allowed:
163  * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
164  */
165 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
166 	   u64, arg2, u64, arg3)
167 {
168 	bool str_seen = false;
169 	int mod[3] = {};
170 	int fmt_cnt = 0;
171 	u64 unsafe_addr;
172 	char buf[64];
173 	int i;
174 
175 	/*
176 	 * bpf_check()->check_func_arg()->check_stack_boundary()
177 	 * guarantees that fmt points to bpf program stack,
178 	 * fmt_size bytes of it were initialized and fmt_size > 0
179 	 */
180 	if (fmt[--fmt_size] != 0)
181 		return -EINVAL;
182 
183 	/* check format string for allowed specifiers */
184 	for (i = 0; i < fmt_size; i++) {
185 		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
186 			return -EINVAL;
187 
188 		if (fmt[i] != '%')
189 			continue;
190 
191 		if (fmt_cnt >= 3)
192 			return -EINVAL;
193 
194 		/* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
195 		i++;
196 		if (fmt[i] == 'l') {
197 			mod[fmt_cnt]++;
198 			i++;
199 		} else if (fmt[i] == 'p' || fmt[i] == 's') {
200 			mod[fmt_cnt]++;
201 			i++;
202 			if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
203 				return -EINVAL;
204 			fmt_cnt++;
205 			if (fmt[i - 1] == 's') {
206 				if (str_seen)
207 					/* allow only one '%s' per fmt string */
208 					return -EINVAL;
209 				str_seen = true;
210 
211 				switch (fmt_cnt) {
212 				case 1:
213 					unsafe_addr = arg1;
214 					arg1 = (long) buf;
215 					break;
216 				case 2:
217 					unsafe_addr = arg2;
218 					arg2 = (long) buf;
219 					break;
220 				case 3:
221 					unsafe_addr = arg3;
222 					arg3 = (long) buf;
223 					break;
224 				}
225 				buf[0] = 0;
226 				strncpy_from_unsafe(buf,
227 						    (void *) (long) unsafe_addr,
228 						    sizeof(buf));
229 			}
230 			continue;
231 		}
232 
233 		if (fmt[i] == 'l') {
234 			mod[fmt_cnt]++;
235 			i++;
236 		}
237 
238 		if (fmt[i] != 'i' && fmt[i] != 'd' &&
239 		    fmt[i] != 'u' && fmt[i] != 'x')
240 			return -EINVAL;
241 		fmt_cnt++;
242 	}
243 
244 /* Horrid workaround for getting va_list handling working with different
245  * argument type combinations generically for 32 and 64 bit archs.
246  */
247 #define __BPF_TP_EMIT()	__BPF_ARG3_TP()
248 #define __BPF_TP(...)							\
249 	__trace_printk(1 /* Fake ip will not be printed. */,		\
250 		       fmt, ##__VA_ARGS__)
251 
252 #define __BPF_ARG1_TP(...)						\
253 	((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64))	\
254 	  ? __BPF_TP(arg1, ##__VA_ARGS__)				\
255 	  : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32))	\
256 	      ? __BPF_TP((long)arg1, ##__VA_ARGS__)			\
257 	      : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
258 
259 #define __BPF_ARG2_TP(...)						\
260 	((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64))	\
261 	  ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__)				\
262 	  : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32))	\
263 	      ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__)		\
264 	      : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
265 
266 #define __BPF_ARG3_TP(...)						\
267 	((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64))	\
268 	  ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__)				\
269 	  : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32))	\
270 	      ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__)		\
271 	      : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
272 
273 	return __BPF_TP_EMIT();
274 }
275 
276 static const struct bpf_func_proto bpf_trace_printk_proto = {
277 	.func		= bpf_trace_printk,
278 	.gpl_only	= true,
279 	.ret_type	= RET_INTEGER,
280 	.arg1_type	= ARG_PTR_TO_MEM,
281 	.arg2_type	= ARG_CONST_SIZE,
282 };
283 
284 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
285 {
286 	/*
287 	 * this program might be calling bpf_trace_printk,
288 	 * so allocate per-cpu printk buffers
289 	 */
290 	trace_printk_init_buffers();
291 
292 	return &bpf_trace_printk_proto;
293 }
294 
295 static __always_inline int
296 get_map_perf_counter(struct bpf_map *map, u64 flags,
297 		     u64 *value, u64 *enabled, u64 *running)
298 {
299 	struct bpf_array *array = container_of(map, struct bpf_array, map);
300 	unsigned int cpu = smp_processor_id();
301 	u64 index = flags & BPF_F_INDEX_MASK;
302 	struct bpf_event_entry *ee;
303 
304 	if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
305 		return -EINVAL;
306 	if (index == BPF_F_CURRENT_CPU)
307 		index = cpu;
308 	if (unlikely(index >= array->map.max_entries))
309 		return -E2BIG;
310 
311 	ee = READ_ONCE(array->ptrs[index]);
312 	if (!ee)
313 		return -ENOENT;
314 
315 	return perf_event_read_local(ee->event, value, enabled, running);
316 }
317 
318 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
319 {
320 	u64 value = 0;
321 	int err;
322 
323 	err = get_map_perf_counter(map, flags, &value, NULL, NULL);
324 	/*
325 	 * this api is ugly since we miss [-22..-2] range of valid
326 	 * counter values, but that's uapi
327 	 */
328 	if (err)
329 		return err;
330 	return value;
331 }
332 
333 static const struct bpf_func_proto bpf_perf_event_read_proto = {
334 	.func		= bpf_perf_event_read,
335 	.gpl_only	= true,
336 	.ret_type	= RET_INTEGER,
337 	.arg1_type	= ARG_CONST_MAP_PTR,
338 	.arg2_type	= ARG_ANYTHING,
339 };
340 
341 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
342 	   struct bpf_perf_event_value *, buf, u32, size)
343 {
344 	int err = -EINVAL;
345 
346 	if (unlikely(size != sizeof(struct bpf_perf_event_value)))
347 		goto clear;
348 	err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
349 				   &buf->running);
350 	if (unlikely(err))
351 		goto clear;
352 	return 0;
353 clear:
354 	memset(buf, 0, size);
355 	return err;
356 }
357 
358 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
359 	.func		= bpf_perf_event_read_value,
360 	.gpl_only	= true,
361 	.ret_type	= RET_INTEGER,
362 	.arg1_type	= ARG_CONST_MAP_PTR,
363 	.arg2_type	= ARG_ANYTHING,
364 	.arg3_type	= ARG_PTR_TO_UNINIT_MEM,
365 	.arg4_type	= ARG_CONST_SIZE,
366 };
367 
368 static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
369 
370 static __always_inline u64
371 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
372 			u64 flags, struct perf_sample_data *sd)
373 {
374 	struct bpf_array *array = container_of(map, struct bpf_array, map);
375 	unsigned int cpu = smp_processor_id();
376 	u64 index = flags & BPF_F_INDEX_MASK;
377 	struct bpf_event_entry *ee;
378 	struct perf_event *event;
379 
380 	if (index == BPF_F_CURRENT_CPU)
381 		index = cpu;
382 	if (unlikely(index >= array->map.max_entries))
383 		return -E2BIG;
384 
385 	ee = READ_ONCE(array->ptrs[index]);
386 	if (!ee)
387 		return -ENOENT;
388 
389 	event = ee->event;
390 	if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
391 		     event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
392 		return -EINVAL;
393 
394 	if (unlikely(event->oncpu != cpu))
395 		return -EOPNOTSUPP;
396 
397 	perf_event_output(event, sd, regs);
398 	return 0;
399 }
400 
401 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
402 	   u64, flags, void *, data, u64, size)
403 {
404 	struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
405 	struct perf_raw_record raw = {
406 		.frag = {
407 			.size = size,
408 			.data = data,
409 		},
410 	};
411 
412 	if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
413 		return -EINVAL;
414 
415 	perf_sample_data_init(sd, 0, 0);
416 	sd->raw = &raw;
417 
418 	return __bpf_perf_event_output(regs, map, flags, sd);
419 }
420 
421 static const struct bpf_func_proto bpf_perf_event_output_proto = {
422 	.func		= bpf_perf_event_output,
423 	.gpl_only	= true,
424 	.ret_type	= RET_INTEGER,
425 	.arg1_type	= ARG_PTR_TO_CTX,
426 	.arg2_type	= ARG_CONST_MAP_PTR,
427 	.arg3_type	= ARG_ANYTHING,
428 	.arg4_type	= ARG_PTR_TO_MEM,
429 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
430 };
431 
432 static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
433 static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
434 
435 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
436 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
437 {
438 	struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
439 	struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
440 	struct perf_raw_frag frag = {
441 		.copy		= ctx_copy,
442 		.size		= ctx_size,
443 		.data		= ctx,
444 	};
445 	struct perf_raw_record raw = {
446 		.frag = {
447 			{
448 				.next	= ctx_size ? &frag : NULL,
449 			},
450 			.size	= meta_size,
451 			.data	= meta,
452 		},
453 	};
454 
455 	perf_fetch_caller_regs(regs);
456 	perf_sample_data_init(sd, 0, 0);
457 	sd->raw = &raw;
458 
459 	return __bpf_perf_event_output(regs, map, flags, sd);
460 }
461 
462 BPF_CALL_0(bpf_get_current_task)
463 {
464 	return (long) current;
465 }
466 
467 static const struct bpf_func_proto bpf_get_current_task_proto = {
468 	.func		= bpf_get_current_task,
469 	.gpl_only	= true,
470 	.ret_type	= RET_INTEGER,
471 };
472 
473 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
474 {
475 	struct bpf_array *array = container_of(map, struct bpf_array, map);
476 	struct cgroup *cgrp;
477 
478 	if (unlikely(in_interrupt()))
479 		return -EINVAL;
480 	if (unlikely(idx >= array->map.max_entries))
481 		return -E2BIG;
482 
483 	cgrp = READ_ONCE(array->ptrs[idx]);
484 	if (unlikely(!cgrp))
485 		return -EAGAIN;
486 
487 	return task_under_cgroup_hierarchy(current, cgrp);
488 }
489 
490 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
491 	.func           = bpf_current_task_under_cgroup,
492 	.gpl_only       = false,
493 	.ret_type       = RET_INTEGER,
494 	.arg1_type      = ARG_CONST_MAP_PTR,
495 	.arg2_type      = ARG_ANYTHING,
496 };
497 
498 BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
499 	   const void *, unsafe_ptr)
500 {
501 	int ret;
502 
503 	/*
504 	 * The strncpy_from_unsafe() call will likely not fill the entire
505 	 * buffer, but that's okay in this circumstance as we're probing
506 	 * arbitrary memory anyway similar to bpf_probe_read() and might
507 	 * as well probe the stack. Thus, memory is explicitly cleared
508 	 * only in error case, so that improper users ignoring return
509 	 * code altogether don't copy garbage; otherwise length of string
510 	 * is returned that can be used for bpf_perf_event_output() et al.
511 	 */
512 	ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
513 	if (unlikely(ret < 0))
514 		memset(dst, 0, size);
515 
516 	return ret;
517 }
518 
519 static const struct bpf_func_proto bpf_probe_read_str_proto = {
520 	.func		= bpf_probe_read_str,
521 	.gpl_only	= true,
522 	.ret_type	= RET_INTEGER,
523 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
524 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
525 	.arg3_type	= ARG_ANYTHING,
526 };
527 
528 static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
529 {
530 	switch (func_id) {
531 	case BPF_FUNC_map_lookup_elem:
532 		return &bpf_map_lookup_elem_proto;
533 	case BPF_FUNC_map_update_elem:
534 		return &bpf_map_update_elem_proto;
535 	case BPF_FUNC_map_delete_elem:
536 		return &bpf_map_delete_elem_proto;
537 	case BPF_FUNC_probe_read:
538 		return &bpf_probe_read_proto;
539 	case BPF_FUNC_ktime_get_ns:
540 		return &bpf_ktime_get_ns_proto;
541 	case BPF_FUNC_tail_call:
542 		return &bpf_tail_call_proto;
543 	case BPF_FUNC_get_current_pid_tgid:
544 		return &bpf_get_current_pid_tgid_proto;
545 	case BPF_FUNC_get_current_task:
546 		return &bpf_get_current_task_proto;
547 	case BPF_FUNC_get_current_uid_gid:
548 		return &bpf_get_current_uid_gid_proto;
549 	case BPF_FUNC_get_current_comm:
550 		return &bpf_get_current_comm_proto;
551 	case BPF_FUNC_trace_printk:
552 		return bpf_get_trace_printk_proto();
553 	case BPF_FUNC_get_smp_processor_id:
554 		return &bpf_get_smp_processor_id_proto;
555 	case BPF_FUNC_get_numa_node_id:
556 		return &bpf_get_numa_node_id_proto;
557 	case BPF_FUNC_perf_event_read:
558 		return &bpf_perf_event_read_proto;
559 	case BPF_FUNC_probe_write_user:
560 		return bpf_get_probe_write_proto();
561 	case BPF_FUNC_current_task_under_cgroup:
562 		return &bpf_current_task_under_cgroup_proto;
563 	case BPF_FUNC_get_prandom_u32:
564 		return &bpf_get_prandom_u32_proto;
565 	case BPF_FUNC_probe_read_str:
566 		return &bpf_probe_read_str_proto;
567 	default:
568 		return NULL;
569 	}
570 }
571 
572 static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
573 {
574 	switch (func_id) {
575 	case BPF_FUNC_perf_event_output:
576 		return &bpf_perf_event_output_proto;
577 	case BPF_FUNC_get_stackid:
578 		return &bpf_get_stackid_proto;
579 	case BPF_FUNC_perf_event_read_value:
580 		return &bpf_perf_event_read_value_proto;
581 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
582 	case BPF_FUNC_override_return:
583 		return &bpf_override_return_proto;
584 #endif
585 	default:
586 		return tracing_func_proto(func_id);
587 	}
588 }
589 
590 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
591 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
592 					struct bpf_insn_access_aux *info)
593 {
594 	if (off < 0 || off >= sizeof(struct pt_regs))
595 		return false;
596 	if (type != BPF_READ)
597 		return false;
598 	if (off % size != 0)
599 		return false;
600 	/*
601 	 * Assertion for 32 bit to make sure last 8 byte access
602 	 * (BPF_DW) to the last 4 byte member is disallowed.
603 	 */
604 	if (off + size > sizeof(struct pt_regs))
605 		return false;
606 
607 	return true;
608 }
609 
610 const struct bpf_verifier_ops kprobe_verifier_ops = {
611 	.get_func_proto  = kprobe_prog_func_proto,
612 	.is_valid_access = kprobe_prog_is_valid_access,
613 };
614 
615 const struct bpf_prog_ops kprobe_prog_ops = {
616 };
617 
618 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
619 	   u64, flags, void *, data, u64, size)
620 {
621 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
622 
623 	/*
624 	 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
625 	 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
626 	 * from there and call the same bpf_perf_event_output() helper inline.
627 	 */
628 	return ____bpf_perf_event_output(regs, map, flags, data, size);
629 }
630 
631 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
632 	.func		= bpf_perf_event_output_tp,
633 	.gpl_only	= true,
634 	.ret_type	= RET_INTEGER,
635 	.arg1_type	= ARG_PTR_TO_CTX,
636 	.arg2_type	= ARG_CONST_MAP_PTR,
637 	.arg3_type	= ARG_ANYTHING,
638 	.arg4_type	= ARG_PTR_TO_MEM,
639 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
640 };
641 
642 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
643 	   u64, flags)
644 {
645 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
646 
647 	/*
648 	 * Same comment as in bpf_perf_event_output_tp(), only that this time
649 	 * the other helper's function body cannot be inlined due to being
650 	 * external, thus we need to call raw helper function.
651 	 */
652 	return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
653 			       flags, 0, 0);
654 }
655 
656 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
657 	.func		= bpf_get_stackid_tp,
658 	.gpl_only	= true,
659 	.ret_type	= RET_INTEGER,
660 	.arg1_type	= ARG_PTR_TO_CTX,
661 	.arg2_type	= ARG_CONST_MAP_PTR,
662 	.arg3_type	= ARG_ANYTHING,
663 };
664 
665 BPF_CALL_3(bpf_perf_prog_read_value_tp, struct bpf_perf_event_data_kern *, ctx,
666 	   struct bpf_perf_event_value *, buf, u32, size)
667 {
668 	int err = -EINVAL;
669 
670 	if (unlikely(size != sizeof(struct bpf_perf_event_value)))
671 		goto clear;
672 	err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
673 				    &buf->running);
674 	if (unlikely(err))
675 		goto clear;
676 	return 0;
677 clear:
678 	memset(buf, 0, size);
679 	return err;
680 }
681 
682 static const struct bpf_func_proto bpf_perf_prog_read_value_proto_tp = {
683          .func           = bpf_perf_prog_read_value_tp,
684          .gpl_only       = true,
685          .ret_type       = RET_INTEGER,
686          .arg1_type      = ARG_PTR_TO_CTX,
687          .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
688          .arg3_type      = ARG_CONST_SIZE,
689 };
690 
691 static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
692 {
693 	switch (func_id) {
694 	case BPF_FUNC_perf_event_output:
695 		return &bpf_perf_event_output_proto_tp;
696 	case BPF_FUNC_get_stackid:
697 		return &bpf_get_stackid_proto_tp;
698 	case BPF_FUNC_perf_prog_read_value:
699 		return &bpf_perf_prog_read_value_proto_tp;
700 	default:
701 		return tracing_func_proto(func_id);
702 	}
703 }
704 
705 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
706 				    struct bpf_insn_access_aux *info)
707 {
708 	if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
709 		return false;
710 	if (type != BPF_READ)
711 		return false;
712 	if (off % size != 0)
713 		return false;
714 
715 	BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
716 	return true;
717 }
718 
719 const struct bpf_verifier_ops tracepoint_verifier_ops = {
720 	.get_func_proto  = tp_prog_func_proto,
721 	.is_valid_access = tp_prog_is_valid_access,
722 };
723 
724 const struct bpf_prog_ops tracepoint_prog_ops = {
725 };
726 
727 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
728 				    struct bpf_insn_access_aux *info)
729 {
730 	const int size_sp = FIELD_SIZEOF(struct bpf_perf_event_data,
731 					 sample_period);
732 
733 	if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
734 		return false;
735 	if (type != BPF_READ)
736 		return false;
737 	if (off % size != 0)
738 		return false;
739 
740 	switch (off) {
741 	case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
742 		bpf_ctx_record_field_size(info, size_sp);
743 		if (!bpf_ctx_narrow_access_ok(off, size, size_sp))
744 			return false;
745 		break;
746 	default:
747 		if (size != sizeof(long))
748 			return false;
749 	}
750 
751 	return true;
752 }
753 
754 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
755 				      const struct bpf_insn *si,
756 				      struct bpf_insn *insn_buf,
757 				      struct bpf_prog *prog, u32 *target_size)
758 {
759 	struct bpf_insn *insn = insn_buf;
760 
761 	switch (si->off) {
762 	case offsetof(struct bpf_perf_event_data, sample_period):
763 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
764 						       data), si->dst_reg, si->src_reg,
765 				      offsetof(struct bpf_perf_event_data_kern, data));
766 		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
767 				      bpf_target_off(struct perf_sample_data, period, 8,
768 						     target_size));
769 		break;
770 	default:
771 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
772 						       regs), si->dst_reg, si->src_reg,
773 				      offsetof(struct bpf_perf_event_data_kern, regs));
774 		*insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
775 				      si->off);
776 		break;
777 	}
778 
779 	return insn - insn_buf;
780 }
781 
782 const struct bpf_verifier_ops perf_event_verifier_ops = {
783 	.get_func_proto		= tp_prog_func_proto,
784 	.is_valid_access	= pe_prog_is_valid_access,
785 	.convert_ctx_access	= pe_prog_convert_ctx_access,
786 };
787 
788 const struct bpf_prog_ops perf_event_prog_ops = {
789 };
790 
791 static DEFINE_MUTEX(bpf_event_mutex);
792 
793 #define BPF_TRACE_MAX_PROGS 64
794 
795 int perf_event_attach_bpf_prog(struct perf_event *event,
796 			       struct bpf_prog *prog)
797 {
798 	struct bpf_prog_array __rcu *old_array;
799 	struct bpf_prog_array *new_array;
800 	int ret = -EEXIST;
801 
802 	/*
803 	 * Kprobe override only works for ftrace based kprobes, and only if they
804 	 * are on the opt-in list.
805 	 */
806 	if (prog->kprobe_override &&
807 	    (!trace_kprobe_ftrace(event->tp_event) ||
808 	     !trace_kprobe_error_injectable(event->tp_event)))
809 		return -EINVAL;
810 
811 	mutex_lock(&bpf_event_mutex);
812 
813 	if (event->prog)
814 		goto unlock;
815 
816 	old_array = event->tp_event->prog_array;
817 	if (old_array &&
818 	    bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
819 		ret = -E2BIG;
820 		goto unlock;
821 	}
822 
823 	ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
824 	if (ret < 0)
825 		goto unlock;
826 
827 	/* set the new array to event->tp_event and set event->prog */
828 	event->prog = prog;
829 	rcu_assign_pointer(event->tp_event->prog_array, new_array);
830 	bpf_prog_array_free(old_array);
831 
832 unlock:
833 	mutex_unlock(&bpf_event_mutex);
834 	return ret;
835 }
836 
837 void perf_event_detach_bpf_prog(struct perf_event *event)
838 {
839 	struct bpf_prog_array __rcu *old_array;
840 	struct bpf_prog_array *new_array;
841 	int ret;
842 
843 	mutex_lock(&bpf_event_mutex);
844 
845 	if (!event->prog)
846 		goto unlock;
847 
848 	old_array = event->tp_event->prog_array;
849 	ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
850 	if (ret < 0) {
851 		bpf_prog_array_delete_safe(old_array, event->prog);
852 	} else {
853 		rcu_assign_pointer(event->tp_event->prog_array, new_array);
854 		bpf_prog_array_free(old_array);
855 	}
856 
857 	bpf_prog_put(event->prog);
858 	event->prog = NULL;
859 
860 unlock:
861 	mutex_unlock(&bpf_event_mutex);
862 }
863 
864 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
865 {
866 	struct perf_event_query_bpf __user *uquery = info;
867 	struct perf_event_query_bpf query = {};
868 	int ret;
869 
870 	if (!capable(CAP_SYS_ADMIN))
871 		return -EPERM;
872 	if (event->attr.type != PERF_TYPE_TRACEPOINT)
873 		return -EINVAL;
874 	if (copy_from_user(&query, uquery, sizeof(query)))
875 		return -EFAULT;
876 
877 	mutex_lock(&bpf_event_mutex);
878 	ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
879 				       uquery->ids,
880 				       query.ids_len,
881 				       &uquery->prog_cnt);
882 	mutex_unlock(&bpf_event_mutex);
883 
884 	return ret;
885 }
886