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