GNU Linux-libre 4.14.290-gnu1
[releases.git] / kernel / trace / bpf_trace.c
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                         /* disallow any further format extensions */
165                         if (fmt[i + 1] != 0 &&
166                             !isspace(fmt[i + 1]) &&
167                             !ispunct(fmt[i + 1]))
168                                 return -EINVAL;
169                         fmt_cnt++;
170                         if (fmt[i] == 's') {
171                                 if (str_seen)
172                                         /* allow only one '%s' per fmt string */
173                                         return -EINVAL;
174                                 str_seen = true;
175
176                                 switch (fmt_cnt) {
177                                 case 1:
178                                         unsafe_addr = arg1;
179                                         arg1 = (long) buf;
180                                         break;
181                                 case 2:
182                                         unsafe_addr = arg2;
183                                         arg2 = (long) buf;
184                                         break;
185                                 case 3:
186                                         unsafe_addr = arg3;
187                                         arg3 = (long) buf;
188                                         break;
189                                 }
190                                 buf[0] = 0;
191                                 strncpy_from_unsafe(buf,
192                                                     (void *) (long) unsafe_addr,
193                                                     sizeof(buf));
194                         }
195                         continue;
196                 }
197
198                 if (fmt[i] == 'l') {
199                         mod[fmt_cnt]++;
200                         i++;
201                 }
202
203                 if (fmt[i] != 'i' && fmt[i] != 'd' &&
204                     fmt[i] != 'u' && fmt[i] != 'x')
205                         return -EINVAL;
206                 fmt_cnt++;
207         }
208
209 /* Horrid workaround for getting va_list handling working with different
210  * argument type combinations generically for 32 and 64 bit archs.
211  */
212 #define __BPF_TP_EMIT() __BPF_ARG3_TP()
213 #define __BPF_TP(...)                                                   \
214         __trace_printk(1 /* Fake ip will not be printed. */,            \
215                        fmt, ##__VA_ARGS__)
216
217 #define __BPF_ARG1_TP(...)                                              \
218         ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64))        \
219           ? __BPF_TP(arg1, ##__VA_ARGS__)                               \
220           : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32))    \
221               ? __BPF_TP((long)arg1, ##__VA_ARGS__)                     \
222               : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
223
224 #define __BPF_ARG2_TP(...)                                              \
225         ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64))        \
226           ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__)                          \
227           : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32))    \
228               ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__)                \
229               : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
230
231 #define __BPF_ARG3_TP(...)                                              \
232         ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64))        \
233           ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__)                          \
234           : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32))    \
235               ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__)                \
236               : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
237
238         return __BPF_TP_EMIT();
239 }
240
241 static const struct bpf_func_proto bpf_trace_printk_proto = {
242         .func           = bpf_trace_printk,
243         .gpl_only       = true,
244         .ret_type       = RET_INTEGER,
245         .arg1_type      = ARG_PTR_TO_MEM,
246         .arg2_type      = ARG_CONST_SIZE,
247 };
248
249 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
250 {
251         /*
252          * this program might be calling bpf_trace_printk,
253          * so allocate per-cpu printk buffers
254          */
255         trace_printk_init_buffers();
256
257         return &bpf_trace_printk_proto;
258 }
259
260 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
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         u64 value = 0;
267         int err;
268
269         if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
270                 return -EINVAL;
271         if (index == BPF_F_CURRENT_CPU)
272                 index = cpu;
273         if (unlikely(index >= array->map.max_entries))
274                 return -E2BIG;
275
276         ee = READ_ONCE(array->ptrs[index]);
277         if (!ee)
278                 return -ENOENT;
279
280         err = perf_event_read_local(ee->event, &value);
281         /*
282          * this api is ugly since we miss [-22..-2] range of valid
283          * counter values, but that's uapi
284          */
285         if (err)
286                 return err;
287         return value;
288 }
289
290 static const struct bpf_func_proto bpf_perf_event_read_proto = {
291         .func           = bpf_perf_event_read,
292         .gpl_only       = true,
293         .ret_type       = RET_INTEGER,
294         .arg1_type      = ARG_CONST_MAP_PTR,
295         .arg2_type      = ARG_ANYTHING,
296 };
297
298 static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
299
300 static __always_inline u64
301 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
302                         u64 flags, struct perf_sample_data *sd)
303 {
304         struct bpf_array *array = container_of(map, struct bpf_array, map);
305         unsigned int cpu = smp_processor_id();
306         u64 index = flags & BPF_F_INDEX_MASK;
307         struct bpf_event_entry *ee;
308         struct perf_event *event;
309
310         if (index == BPF_F_CURRENT_CPU)
311                 index = cpu;
312         if (unlikely(index >= array->map.max_entries))
313                 return -E2BIG;
314
315         ee = READ_ONCE(array->ptrs[index]);
316         if (!ee)
317                 return -ENOENT;
318
319         event = ee->event;
320         if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
321                      event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
322                 return -EINVAL;
323
324         if (unlikely(event->oncpu != cpu))
325                 return -EOPNOTSUPP;
326
327         perf_event_output(event, sd, regs);
328         return 0;
329 }
330
331 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
332            u64, flags, void *, data, u64, size)
333 {
334         struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
335         struct perf_raw_record raw = {
336                 .frag = {
337                         .size = size,
338                         .data = data,
339                 },
340         };
341
342         if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
343                 return -EINVAL;
344
345         perf_sample_data_init(sd, 0, 0);
346         sd->raw = &raw;
347
348         return __bpf_perf_event_output(regs, map, flags, sd);
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_MEM,
359         .arg5_type      = ARG_CONST_SIZE,
360 };
361
362 static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
363 static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
364
365 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
366                      void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
367 {
368         struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
369         struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
370         struct perf_raw_frag frag = {
371                 .copy           = ctx_copy,
372                 .size           = ctx_size,
373                 .data           = ctx,
374         };
375         struct perf_raw_record raw = {
376                 .frag = {
377                         {
378                                 .next   = ctx_size ? &frag : NULL,
379                         },
380                         .size   = meta_size,
381                         .data   = meta,
382                 },
383         };
384
385         perf_fetch_caller_regs(regs);
386         perf_sample_data_init(sd, 0, 0);
387         sd->raw = &raw;
388
389         return __bpf_perf_event_output(regs, map, flags, sd);
390 }
391
392 BPF_CALL_0(bpf_get_current_task)
393 {
394         return (long) current;
395 }
396
397 static const struct bpf_func_proto bpf_get_current_task_proto = {
398         .func           = bpf_get_current_task,
399         .gpl_only       = true,
400         .ret_type       = RET_INTEGER,
401 };
402
403 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
404 {
405         struct bpf_array *array = container_of(map, struct bpf_array, map);
406         struct cgroup *cgrp;
407
408         if (unlikely(in_interrupt()))
409                 return -EINVAL;
410         if (unlikely(idx >= array->map.max_entries))
411                 return -E2BIG;
412
413         cgrp = READ_ONCE(array->ptrs[idx]);
414         if (unlikely(!cgrp))
415                 return -EAGAIN;
416
417         return task_under_cgroup_hierarchy(current, cgrp);
418 }
419
420 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
421         .func           = bpf_current_task_under_cgroup,
422         .gpl_only       = false,
423         .ret_type       = RET_INTEGER,
424         .arg1_type      = ARG_CONST_MAP_PTR,
425         .arg2_type      = ARG_ANYTHING,
426 };
427
428 BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
429            const void *, unsafe_ptr)
430 {
431         int ret;
432
433         /*
434          * The strncpy_from_unsafe() call will likely not fill the entire
435          * buffer, but that's okay in this circumstance as we're probing
436          * arbitrary memory anyway similar to bpf_probe_read() and might
437          * as well probe the stack. Thus, memory is explicitly cleared
438          * only in error case, so that improper users ignoring return
439          * code altogether don't copy garbage; otherwise length of string
440          * is returned that can be used for bpf_perf_event_output() et al.
441          */
442         ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
443         if (unlikely(ret < 0))
444                 memset(dst, 0, size);
445
446         return ret;
447 }
448
449 static const struct bpf_func_proto bpf_probe_read_str_proto = {
450         .func           = bpf_probe_read_str,
451         .gpl_only       = true,
452         .ret_type       = RET_INTEGER,
453         .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
454         .arg2_type      = ARG_CONST_SIZE,
455         .arg3_type      = ARG_ANYTHING,
456 };
457
458 static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
459 {
460         switch (func_id) {
461         case BPF_FUNC_map_lookup_elem:
462                 return &bpf_map_lookup_elem_proto;
463         case BPF_FUNC_map_update_elem:
464                 return &bpf_map_update_elem_proto;
465         case BPF_FUNC_map_delete_elem:
466                 return &bpf_map_delete_elem_proto;
467         case BPF_FUNC_probe_read:
468                 return &bpf_probe_read_proto;
469         case BPF_FUNC_ktime_get_ns:
470                 return &bpf_ktime_get_ns_proto;
471         case BPF_FUNC_tail_call:
472                 return &bpf_tail_call_proto;
473         case BPF_FUNC_get_current_pid_tgid:
474                 return &bpf_get_current_pid_tgid_proto;
475         case BPF_FUNC_get_current_task:
476                 return &bpf_get_current_task_proto;
477         case BPF_FUNC_get_current_uid_gid:
478                 return &bpf_get_current_uid_gid_proto;
479         case BPF_FUNC_get_current_comm:
480                 return &bpf_get_current_comm_proto;
481         case BPF_FUNC_trace_printk:
482                 return bpf_get_trace_printk_proto();
483         case BPF_FUNC_get_smp_processor_id:
484                 return &bpf_get_smp_processor_id_proto;
485         case BPF_FUNC_get_numa_node_id:
486                 return &bpf_get_numa_node_id_proto;
487         case BPF_FUNC_perf_event_read:
488                 return &bpf_perf_event_read_proto;
489         case BPF_FUNC_probe_write_user:
490                 return bpf_get_probe_write_proto();
491         case BPF_FUNC_current_task_under_cgroup:
492                 return &bpf_current_task_under_cgroup_proto;
493         case BPF_FUNC_get_prandom_u32:
494                 return &bpf_get_prandom_u32_proto;
495         case BPF_FUNC_probe_read_str:
496                 return &bpf_probe_read_str_proto;
497         default:
498                 return NULL;
499         }
500 }
501
502 static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
503 {
504         switch (func_id) {
505         case BPF_FUNC_perf_event_output:
506                 return &bpf_perf_event_output_proto;
507         case BPF_FUNC_get_stackid:
508                 return &bpf_get_stackid_proto;
509         default:
510                 return tracing_func_proto(func_id);
511         }
512 }
513
514 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
515 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
516                                         struct bpf_insn_access_aux *info)
517 {
518         if (off < 0 || off >= sizeof(struct pt_regs))
519                 return false;
520         if (type != BPF_READ)
521                 return false;
522         if (off % size != 0)
523                 return false;
524         /*
525          * Assertion for 32 bit to make sure last 8 byte access
526          * (BPF_DW) to the last 4 byte member is disallowed.
527          */
528         if (off + size > sizeof(struct pt_regs))
529                 return false;
530
531         return true;
532 }
533
534 const struct bpf_verifier_ops kprobe_prog_ops = {
535         .get_func_proto  = kprobe_prog_func_proto,
536         .is_valid_access = kprobe_prog_is_valid_access,
537 };
538
539 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
540            u64, flags, void *, data, u64, size)
541 {
542         struct pt_regs *regs = *(struct pt_regs **)tp_buff;
543
544         /*
545          * r1 points to perf tracepoint buffer where first 8 bytes are hidden
546          * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
547          * from there and call the same bpf_perf_event_output() helper inline.
548          */
549         return ____bpf_perf_event_output(regs, map, flags, data, size);
550 }
551
552 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
553         .func           = bpf_perf_event_output_tp,
554         .gpl_only       = true,
555         .ret_type       = RET_INTEGER,
556         .arg1_type      = ARG_PTR_TO_CTX,
557         .arg2_type      = ARG_CONST_MAP_PTR,
558         .arg3_type      = ARG_ANYTHING,
559         .arg4_type      = ARG_PTR_TO_MEM,
560         .arg5_type      = ARG_CONST_SIZE,
561 };
562
563 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
564            u64, flags)
565 {
566         struct pt_regs *regs = *(struct pt_regs **)tp_buff;
567
568         /*
569          * Same comment as in bpf_perf_event_output_tp(), only that this time
570          * the other helper's function body cannot be inlined due to being
571          * external, thus we need to call raw helper function.
572          */
573         return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
574                                flags, 0, 0);
575 }
576
577 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
578         .func           = bpf_get_stackid_tp,
579         .gpl_only       = true,
580         .ret_type       = RET_INTEGER,
581         .arg1_type      = ARG_PTR_TO_CTX,
582         .arg2_type      = ARG_CONST_MAP_PTR,
583         .arg3_type      = ARG_ANYTHING,
584 };
585
586 static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
587 {
588         switch (func_id) {
589         case BPF_FUNC_perf_event_output:
590                 return &bpf_perf_event_output_proto_tp;
591         case BPF_FUNC_get_stackid:
592                 return &bpf_get_stackid_proto_tp;
593         default:
594                 return tracing_func_proto(func_id);
595         }
596 }
597
598 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
599                                     struct bpf_insn_access_aux *info)
600 {
601         if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
602                 return false;
603         if (type != BPF_READ)
604                 return false;
605         if (off % size != 0)
606                 return false;
607
608         BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
609         return true;
610 }
611
612 const struct bpf_verifier_ops tracepoint_prog_ops = {
613         .get_func_proto  = tp_prog_func_proto,
614         .is_valid_access = tp_prog_is_valid_access,
615 };
616
617 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
618                                     struct bpf_insn_access_aux *info)
619 {
620         const int size_sp = FIELD_SIZEOF(struct bpf_perf_event_data,
621                                          sample_period);
622
623         if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
624                 return false;
625         if (type != BPF_READ)
626                 return false;
627         if (off % size != 0)
628                 return false;
629
630         switch (off) {
631         case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
632                 bpf_ctx_record_field_size(info, size_sp);
633                 if (!bpf_ctx_narrow_access_ok(off, size, size_sp))
634                         return false;
635                 break;
636         default:
637                 if (size != sizeof(long))
638                         return false;
639         }
640
641         return true;
642 }
643
644 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
645                                       const struct bpf_insn *si,
646                                       struct bpf_insn *insn_buf,
647                                       struct bpf_prog *prog, u32 *target_size)
648 {
649         struct bpf_insn *insn = insn_buf;
650
651         switch (si->off) {
652         case offsetof(struct bpf_perf_event_data, sample_period):
653                 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
654                                                        data), si->dst_reg, si->src_reg,
655                                       offsetof(struct bpf_perf_event_data_kern, data));
656                 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
657                                       bpf_target_off(struct perf_sample_data, period, 8,
658                                                      target_size));
659                 break;
660         default:
661                 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
662                                                        regs), si->dst_reg, si->src_reg,
663                                       offsetof(struct bpf_perf_event_data_kern, regs));
664                 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
665                                       si->off);
666                 break;
667         }
668
669         return insn - insn_buf;
670 }
671
672 const struct bpf_verifier_ops perf_event_prog_ops = {
673         .get_func_proto         = tp_prog_func_proto,
674         .is_valid_access        = pe_prog_is_valid_access,
675         .convert_ctx_access     = pe_prog_convert_ctx_access,
676 };