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 */
trace_call_bpf(struct bpf_prog * prog,void * ctx)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
BPF_CALL_3(bpf_probe_read,void *,dst,u32,size,const void *,unsafe_ptr)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_RAW_STACK,
80 .arg2_type = ARG_CONST_STACK_SIZE,
81 .arg3_type = ARG_ANYTHING,
82 };
83
BPF_CALL_3(bpf_probe_write_user,void *,unsafe_ptr,const void *,src,u32,size)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(segment_eq(get_fs(), KERNEL_DS)))
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_STACK,
113 .arg3_type = ARG_CONST_STACK_SIZE,
114 };
115
bpf_get_probe_write_proto(void)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 * limited trace_printk()
126 * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
127 */
BPF_CALL_5(bpf_trace_printk,char *,fmt,u32,fmt_size,u64,arg1,u64,arg2,u64,arg3)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] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
202 return -EINVAL;
203 fmt_cnt++;
204 }
205
206 /* Horrid workaround for getting va_list handling working with different
207 * argument type combinations generically for 32 and 64 bit archs.
208 */
209 #define __BPF_TP_EMIT() __BPF_ARG3_TP()
210 #define __BPF_TP(...) \
211 __trace_printk(1 /* Fake ip will not be printed. */, \
212 fmt, ##__VA_ARGS__)
213
214 #define __BPF_ARG1_TP(...) \
215 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
216 ? __BPF_TP(arg1, ##__VA_ARGS__) \
217 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
218 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
219 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
220
221 #define __BPF_ARG2_TP(...) \
222 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
223 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
224 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
225 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
226 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
227
228 #define __BPF_ARG3_TP(...) \
229 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
230 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
231 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
232 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
233 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
234
235 return __BPF_TP_EMIT();
236 }
237
238 static const struct bpf_func_proto bpf_trace_printk_proto = {
239 .func = bpf_trace_printk,
240 .gpl_only = true,
241 .ret_type = RET_INTEGER,
242 .arg1_type = ARG_PTR_TO_STACK,
243 .arg2_type = ARG_CONST_STACK_SIZE,
244 };
245
bpf_get_trace_printk_proto(void)246 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
247 {
248 /*
249 * this program might be calling bpf_trace_printk,
250 * so allocate per-cpu printk buffers
251 */
252 trace_printk_init_buffers();
253
254 return &bpf_trace_printk_proto;
255 }
256
BPF_CALL_2(bpf_perf_event_read,struct bpf_map *,map,u64,flags)257 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
258 {
259 struct bpf_array *array = container_of(map, struct bpf_array, map);
260 unsigned int cpu = smp_processor_id();
261 u64 index = flags & BPF_F_INDEX_MASK;
262 struct bpf_event_entry *ee;
263 struct perf_event *event;
264
265 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
266 return -EINVAL;
267 if (index == BPF_F_CURRENT_CPU)
268 index = cpu;
269 if (unlikely(index >= array->map.max_entries))
270 return -E2BIG;
271
272 ee = READ_ONCE(array->ptrs[index]);
273 if (!ee)
274 return -ENOENT;
275
276 event = ee->event;
277 if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
278 event->attr.type != PERF_TYPE_RAW))
279 return -EINVAL;
280
281 /* make sure event is local and doesn't have pmu::count */
282 if (unlikely(event->oncpu != cpu || event->pmu->count))
283 return -EINVAL;
284
285 /*
286 * we don't know if the function is run successfully by the
287 * return value. It can be judged in other places, such as
288 * eBPF programs.
289 */
290 return perf_event_read_local(event);
291 }
292
293 static const struct bpf_func_proto bpf_perf_event_read_proto = {
294 .func = bpf_perf_event_read,
295 .gpl_only = true,
296 .ret_type = RET_INTEGER,
297 .arg1_type = ARG_CONST_MAP_PTR,
298 .arg2_type = ARG_ANYTHING,
299 };
300
301 static __always_inline u64
__bpf_perf_event_output(struct pt_regs * regs,struct bpf_map * map,u64 flags,struct perf_raw_record * raw)302 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
303 u64 flags, struct perf_raw_record *raw)
304 {
305 struct bpf_array *array = container_of(map, struct bpf_array, map);
306 unsigned int cpu = smp_processor_id();
307 u64 index = flags & BPF_F_INDEX_MASK;
308 struct perf_sample_data sample_data;
309 struct bpf_event_entry *ee;
310 struct perf_event *event;
311
312 if (index == BPF_F_CURRENT_CPU)
313 index = cpu;
314 if (unlikely(index >= array->map.max_entries))
315 return -E2BIG;
316
317 ee = READ_ONCE(array->ptrs[index]);
318 if (!ee)
319 return -ENOENT;
320
321 event = ee->event;
322 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
323 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
324 return -EINVAL;
325
326 if (unlikely(event->oncpu != cpu))
327 return -EOPNOTSUPP;
328
329 perf_sample_data_init(&sample_data, 0, 0);
330 sample_data.raw = raw;
331 perf_event_output(event, &sample_data, regs);
332 return 0;
333 }
334
BPF_CALL_5(bpf_perf_event_output,struct pt_regs *,regs,struct bpf_map *,map,u64,flags,void *,data,u64,size)335 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
336 u64, flags, void *, data, u64, size)
337 {
338 struct perf_raw_record raw = {
339 .frag = {
340 .size = size,
341 .data = data,
342 },
343 };
344
345 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
346 return -EINVAL;
347
348 return __bpf_perf_event_output(regs, map, flags, &raw);
349 }
350
351 static const struct bpf_func_proto bpf_perf_event_output_proto = {
352 .func = bpf_perf_event_output,
353 .gpl_only = true,
354 .ret_type = RET_INTEGER,
355 .arg1_type = ARG_PTR_TO_CTX,
356 .arg2_type = ARG_CONST_MAP_PTR,
357 .arg3_type = ARG_ANYTHING,
358 .arg4_type = ARG_PTR_TO_STACK,
359 .arg5_type = ARG_CONST_STACK_SIZE,
360 };
361
362 static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
363
bpf_event_output(struct bpf_map * map,u64 flags,void * meta,u64 meta_size,void * ctx,u64 ctx_size,bpf_ctx_copy_t ctx_copy)364 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
365 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
366 {
367 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
368 struct perf_raw_frag frag = {
369 .copy = ctx_copy,
370 .size = ctx_size,
371 .data = ctx,
372 };
373 struct perf_raw_record raw = {
374 .frag = {
375 {
376 .next = ctx_size ? &frag : NULL,
377 },
378 .size = meta_size,
379 .data = meta,
380 },
381 };
382
383 perf_fetch_caller_regs(regs);
384
385 return __bpf_perf_event_output(regs, map, flags, &raw);
386 }
387
BPF_CALL_0(bpf_get_current_task)388 BPF_CALL_0(bpf_get_current_task)
389 {
390 return (long) current;
391 }
392
393 static const struct bpf_func_proto bpf_get_current_task_proto = {
394 .func = bpf_get_current_task,
395 .gpl_only = true,
396 .ret_type = RET_INTEGER,
397 };
398
BPF_CALL_2(bpf_current_task_under_cgroup,struct bpf_map *,map,u32,idx)399 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
400 {
401 struct bpf_array *array = container_of(map, struct bpf_array, map);
402 struct cgroup *cgrp;
403
404 if (unlikely(in_interrupt()))
405 return -EINVAL;
406 if (unlikely(idx >= array->map.max_entries))
407 return -E2BIG;
408
409 cgrp = READ_ONCE(array->ptrs[idx]);
410 if (unlikely(!cgrp))
411 return -EAGAIN;
412
413 return task_under_cgroup_hierarchy(current, cgrp);
414 }
415
416 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
417 .func = bpf_current_task_under_cgroup,
418 .gpl_only = false,
419 .ret_type = RET_INTEGER,
420 .arg1_type = ARG_CONST_MAP_PTR,
421 .arg2_type = ARG_ANYTHING,
422 };
423
tracing_func_proto(enum bpf_func_id func_id)424 static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
425 {
426 switch (func_id) {
427 case BPF_FUNC_map_lookup_elem:
428 return &bpf_map_lookup_elem_proto;
429 case BPF_FUNC_map_update_elem:
430 return &bpf_map_update_elem_proto;
431 case BPF_FUNC_map_delete_elem:
432 return &bpf_map_delete_elem_proto;
433 case BPF_FUNC_probe_read:
434 return &bpf_probe_read_proto;
435 case BPF_FUNC_ktime_get_ns:
436 return &bpf_ktime_get_ns_proto;
437 case BPF_FUNC_tail_call:
438 return &bpf_tail_call_proto;
439 case BPF_FUNC_get_current_pid_tgid:
440 return &bpf_get_current_pid_tgid_proto;
441 case BPF_FUNC_get_current_task:
442 return &bpf_get_current_task_proto;
443 case BPF_FUNC_get_current_uid_gid:
444 return &bpf_get_current_uid_gid_proto;
445 case BPF_FUNC_get_current_comm:
446 return &bpf_get_current_comm_proto;
447 case BPF_FUNC_trace_printk:
448 return bpf_get_trace_printk_proto();
449 case BPF_FUNC_get_smp_processor_id:
450 return &bpf_get_smp_processor_id_proto;
451 case BPF_FUNC_perf_event_read:
452 return &bpf_perf_event_read_proto;
453 case BPF_FUNC_probe_write_user:
454 return bpf_get_probe_write_proto();
455 case BPF_FUNC_current_task_under_cgroup:
456 return &bpf_current_task_under_cgroup_proto;
457 case BPF_FUNC_get_prandom_u32:
458 return &bpf_get_prandom_u32_proto;
459 default:
460 return NULL;
461 }
462 }
463
kprobe_prog_func_proto(enum bpf_func_id func_id)464 static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
465 {
466 switch (func_id) {
467 case BPF_FUNC_perf_event_output:
468 return &bpf_perf_event_output_proto;
469 case BPF_FUNC_get_stackid:
470 return &bpf_get_stackid_proto;
471 default:
472 return tracing_func_proto(func_id);
473 }
474 }
475
476 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
kprobe_prog_is_valid_access(int off,int size,enum bpf_access_type type,enum bpf_reg_type * reg_type)477 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
478 enum bpf_reg_type *reg_type)
479 {
480 if (off < 0 || off >= sizeof(struct pt_regs))
481 return false;
482 if (type != BPF_READ)
483 return false;
484 if (off % size != 0)
485 return false;
486 return true;
487 }
488
489 static const struct bpf_verifier_ops kprobe_prog_ops = {
490 .get_func_proto = kprobe_prog_func_proto,
491 .is_valid_access = kprobe_prog_is_valid_access,
492 };
493
494 static struct bpf_prog_type_list kprobe_tl = {
495 .ops = &kprobe_prog_ops,
496 .type = BPF_PROG_TYPE_KPROBE,
497 };
498
BPF_CALL_5(bpf_perf_event_output_tp,void *,tp_buff,struct bpf_map *,map,u64,flags,void *,data,u64,size)499 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
500 u64, flags, void *, data, u64, size)
501 {
502 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
503
504 /*
505 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
506 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
507 * from there and call the same bpf_perf_event_output() helper inline.
508 */
509 return ____bpf_perf_event_output(regs, map, flags, data, size);
510 }
511
512 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
513 .func = bpf_perf_event_output_tp,
514 .gpl_only = true,
515 .ret_type = RET_INTEGER,
516 .arg1_type = ARG_PTR_TO_CTX,
517 .arg2_type = ARG_CONST_MAP_PTR,
518 .arg3_type = ARG_ANYTHING,
519 .arg4_type = ARG_PTR_TO_STACK,
520 .arg5_type = ARG_CONST_STACK_SIZE,
521 };
522
BPF_CALL_3(bpf_get_stackid_tp,void *,tp_buff,struct bpf_map *,map,u64,flags)523 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
524 u64, flags)
525 {
526 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
527
528 /*
529 * Same comment as in bpf_perf_event_output_tp(), only that this time
530 * the other helper's function body cannot be inlined due to being
531 * external, thus we need to call raw helper function.
532 */
533 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
534 flags, 0, 0);
535 }
536
537 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
538 .func = bpf_get_stackid_tp,
539 .gpl_only = true,
540 .ret_type = RET_INTEGER,
541 .arg1_type = ARG_PTR_TO_CTX,
542 .arg2_type = ARG_CONST_MAP_PTR,
543 .arg3_type = ARG_ANYTHING,
544 };
545
tp_prog_func_proto(enum bpf_func_id func_id)546 static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
547 {
548 switch (func_id) {
549 case BPF_FUNC_perf_event_output:
550 return &bpf_perf_event_output_proto_tp;
551 case BPF_FUNC_get_stackid:
552 return &bpf_get_stackid_proto_tp;
553 default:
554 return tracing_func_proto(func_id);
555 }
556 }
557
tp_prog_is_valid_access(int off,int size,enum bpf_access_type type,enum bpf_reg_type * reg_type)558 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
559 enum bpf_reg_type *reg_type)
560 {
561 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
562 return false;
563 if (type != BPF_READ)
564 return false;
565 if (off % size != 0)
566 return false;
567 return true;
568 }
569
570 static const struct bpf_verifier_ops tracepoint_prog_ops = {
571 .get_func_proto = tp_prog_func_proto,
572 .is_valid_access = tp_prog_is_valid_access,
573 };
574
575 static struct bpf_prog_type_list tracepoint_tl = {
576 .ops = &tracepoint_prog_ops,
577 .type = BPF_PROG_TYPE_TRACEPOINT,
578 };
579
pe_prog_is_valid_access(int off,int size,enum bpf_access_type type,enum bpf_reg_type * reg_type)580 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
581 enum bpf_reg_type *reg_type)
582 {
583 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
584 return false;
585 if (type != BPF_READ)
586 return false;
587 if (off % size != 0)
588 return false;
589 if (off == offsetof(struct bpf_perf_event_data, sample_period)) {
590 if (size != sizeof(u64))
591 return false;
592 } else {
593 if (size != sizeof(long))
594 return false;
595 }
596 return true;
597 }
598
pe_prog_convert_ctx_access(enum bpf_access_type type,int dst_reg,int src_reg,int ctx_off,struct bpf_insn * insn_buf,struct bpf_prog * prog)599 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, int dst_reg,
600 int src_reg, int ctx_off,
601 struct bpf_insn *insn_buf,
602 struct bpf_prog *prog)
603 {
604 struct bpf_insn *insn = insn_buf;
605
606 switch (ctx_off) {
607 case offsetof(struct bpf_perf_event_data, sample_period):
608 BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64));
609
610 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
611 data), dst_reg, src_reg,
612 offsetof(struct bpf_perf_event_data_kern, data));
613 *insn++ = BPF_LDX_MEM(BPF_DW, dst_reg, dst_reg,
614 offsetof(struct perf_sample_data, period));
615 break;
616 default:
617 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
618 regs), dst_reg, src_reg,
619 offsetof(struct bpf_perf_event_data_kern, regs));
620 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), dst_reg, dst_reg, ctx_off);
621 break;
622 }
623
624 return insn - insn_buf;
625 }
626
627 static const struct bpf_verifier_ops perf_event_prog_ops = {
628 .get_func_proto = tp_prog_func_proto,
629 .is_valid_access = pe_prog_is_valid_access,
630 .convert_ctx_access = pe_prog_convert_ctx_access,
631 };
632
633 static struct bpf_prog_type_list perf_event_tl = {
634 .ops = &perf_event_prog_ops,
635 .type = BPF_PROG_TYPE_PERF_EVENT,
636 };
637
register_kprobe_prog_ops(void)638 static int __init register_kprobe_prog_ops(void)
639 {
640 bpf_register_prog_type(&kprobe_tl);
641 bpf_register_prog_type(&tracepoint_tl);
642 bpf_register_prog_type(&perf_event_tl);
643 return 0;
644 }
645 late_initcall(register_kprobe_prog_ops);
646