bpf_trace.c 17 KB

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