bpf_trace.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. * limited trace_printk()
  108. * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
  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] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
  176. return -EINVAL;
  177. fmt_cnt++;
  178. }
  179. return __trace_printk(1/* fake ip will not be printed */, fmt,
  180. mod[0] == 2 ? arg1 : mod[0] == 1 ? (long) arg1 : (u32) arg1,
  181. mod[1] == 2 ? arg2 : mod[1] == 1 ? (long) arg2 : (u32) arg2,
  182. mod[2] == 2 ? arg3 : mod[2] == 1 ? (long) arg3 : (u32) arg3);
  183. }
  184. static const struct bpf_func_proto bpf_trace_printk_proto = {
  185. .func = bpf_trace_printk,
  186. .gpl_only = true,
  187. .ret_type = RET_INTEGER,
  188. .arg1_type = ARG_PTR_TO_MEM,
  189. .arg2_type = ARG_CONST_SIZE,
  190. };
  191. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  192. {
  193. /*
  194. * this program might be calling bpf_trace_printk,
  195. * so allocate per-cpu printk buffers
  196. */
  197. trace_printk_init_buffers();
  198. return &bpf_trace_printk_proto;
  199. }
  200. BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
  201. {
  202. struct bpf_array *array = container_of(map, struct bpf_array, map);
  203. unsigned int cpu = smp_processor_id();
  204. u64 index = flags & BPF_F_INDEX_MASK;
  205. struct bpf_event_entry *ee;
  206. struct perf_event *event;
  207. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  208. return -EINVAL;
  209. if (index == BPF_F_CURRENT_CPU)
  210. index = cpu;
  211. if (unlikely(index >= array->map.max_entries))
  212. return -E2BIG;
  213. ee = READ_ONCE(array->ptrs[index]);
  214. if (!ee)
  215. return -ENOENT;
  216. event = ee->event;
  217. if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
  218. event->attr.type != PERF_TYPE_RAW))
  219. return -EINVAL;
  220. /* make sure event is local and doesn't have pmu::count */
  221. if (unlikely(event->oncpu != cpu || event->pmu->count))
  222. return -EINVAL;
  223. /*
  224. * we don't know if the function is run successfully by the
  225. * return value. It can be judged in other places, such as
  226. * eBPF programs.
  227. */
  228. return perf_event_read_local(event);
  229. }
  230. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  231. .func = bpf_perf_event_read,
  232. .gpl_only = true,
  233. .ret_type = RET_INTEGER,
  234. .arg1_type = ARG_CONST_MAP_PTR,
  235. .arg2_type = ARG_ANYTHING,
  236. };
  237. static __always_inline u64
  238. __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
  239. u64 flags, struct perf_raw_record *raw)
  240. {
  241. struct bpf_array *array = container_of(map, struct bpf_array, map);
  242. unsigned int cpu = smp_processor_id();
  243. u64 index = flags & BPF_F_INDEX_MASK;
  244. struct perf_sample_data sample_data;
  245. struct bpf_event_entry *ee;
  246. struct perf_event *event;
  247. if (index == BPF_F_CURRENT_CPU)
  248. index = cpu;
  249. if (unlikely(index >= array->map.max_entries))
  250. return -E2BIG;
  251. ee = READ_ONCE(array->ptrs[index]);
  252. if (!ee)
  253. return -ENOENT;
  254. event = ee->event;
  255. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  256. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  257. return -EINVAL;
  258. if (unlikely(event->oncpu != cpu))
  259. return -EOPNOTSUPP;
  260. perf_sample_data_init(&sample_data, 0, 0);
  261. sample_data.raw = raw;
  262. perf_event_output(event, &sample_data, regs);
  263. return 0;
  264. }
  265. BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
  266. u64, flags, void *, data, u64, size)
  267. {
  268. struct perf_raw_record raw = {
  269. .frag = {
  270. .size = size,
  271. .data = data,
  272. },
  273. };
  274. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  275. return -EINVAL;
  276. return __bpf_perf_event_output(regs, map, flags, &raw);
  277. }
  278. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  279. .func = bpf_perf_event_output,
  280. .gpl_only = true,
  281. .ret_type = RET_INTEGER,
  282. .arg1_type = ARG_PTR_TO_CTX,
  283. .arg2_type = ARG_CONST_MAP_PTR,
  284. .arg3_type = ARG_ANYTHING,
  285. .arg4_type = ARG_PTR_TO_MEM,
  286. .arg5_type = ARG_CONST_SIZE,
  287. };
  288. static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
  289. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  290. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  291. {
  292. struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
  293. struct perf_raw_frag frag = {
  294. .copy = ctx_copy,
  295. .size = ctx_size,
  296. .data = ctx,
  297. };
  298. struct perf_raw_record raw = {
  299. .frag = {
  300. {
  301. .next = ctx_size ? &frag : NULL,
  302. },
  303. .size = meta_size,
  304. .data = meta,
  305. },
  306. };
  307. perf_fetch_caller_regs(regs);
  308. return __bpf_perf_event_output(regs, map, flags, &raw);
  309. }
  310. BPF_CALL_0(bpf_get_current_task)
  311. {
  312. return (long) current;
  313. }
  314. static const struct bpf_func_proto bpf_get_current_task_proto = {
  315. .func = bpf_get_current_task,
  316. .gpl_only = true,
  317. .ret_type = RET_INTEGER,
  318. };
  319. BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
  320. {
  321. struct bpf_array *array = container_of(map, struct bpf_array, map);
  322. struct cgroup *cgrp;
  323. if (unlikely(in_interrupt()))
  324. return -EINVAL;
  325. if (unlikely(idx >= array->map.max_entries))
  326. return -E2BIG;
  327. cgrp = READ_ONCE(array->ptrs[idx]);
  328. if (unlikely(!cgrp))
  329. return -EAGAIN;
  330. return task_under_cgroup_hierarchy(current, cgrp);
  331. }
  332. static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
  333. .func = bpf_current_task_under_cgroup,
  334. .gpl_only = false,
  335. .ret_type = RET_INTEGER,
  336. .arg1_type = ARG_CONST_MAP_PTR,
  337. .arg2_type = ARG_ANYTHING,
  338. };
  339. BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
  340. const void *, unsafe_ptr)
  341. {
  342. int ret;
  343. /*
  344. * The strncpy_from_unsafe() call will likely not fill the entire
  345. * buffer, but that's okay in this circumstance as we're probing
  346. * arbitrary memory anyway similar to bpf_probe_read() and might
  347. * as well probe the stack. Thus, memory is explicitly cleared
  348. * only in error case, so that improper users ignoring return
  349. * code altogether don't copy garbage; otherwise length of string
  350. * is returned that can be used for bpf_perf_event_output() et al.
  351. */
  352. ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
  353. if (unlikely(ret < 0))
  354. memset(dst, 0, size);
  355. return ret;
  356. }
  357. static const struct bpf_func_proto bpf_probe_read_str_proto = {
  358. .func = bpf_probe_read_str,
  359. .gpl_only = true,
  360. .ret_type = RET_INTEGER,
  361. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  362. .arg2_type = ARG_CONST_SIZE,
  363. .arg3_type = ARG_ANYTHING,
  364. };
  365. static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
  366. {
  367. switch (func_id) {
  368. case BPF_FUNC_map_lookup_elem:
  369. return &bpf_map_lookup_elem_proto;
  370. case BPF_FUNC_map_update_elem:
  371. return &bpf_map_update_elem_proto;
  372. case BPF_FUNC_map_delete_elem:
  373. return &bpf_map_delete_elem_proto;
  374. case BPF_FUNC_probe_read:
  375. return &bpf_probe_read_proto;
  376. case BPF_FUNC_ktime_get_ns:
  377. return &bpf_ktime_get_ns_proto;
  378. case BPF_FUNC_tail_call:
  379. return &bpf_tail_call_proto;
  380. case BPF_FUNC_get_current_pid_tgid:
  381. return &bpf_get_current_pid_tgid_proto;
  382. case BPF_FUNC_get_current_task:
  383. return &bpf_get_current_task_proto;
  384. case BPF_FUNC_get_current_uid_gid:
  385. return &bpf_get_current_uid_gid_proto;
  386. case BPF_FUNC_get_current_comm:
  387. return &bpf_get_current_comm_proto;
  388. case BPF_FUNC_trace_printk:
  389. return bpf_get_trace_printk_proto();
  390. case BPF_FUNC_get_smp_processor_id:
  391. return &bpf_get_smp_processor_id_proto;
  392. case BPF_FUNC_get_numa_node_id:
  393. return &bpf_get_numa_node_id_proto;
  394. case BPF_FUNC_perf_event_read:
  395. return &bpf_perf_event_read_proto;
  396. case BPF_FUNC_probe_write_user:
  397. return bpf_get_probe_write_proto();
  398. case BPF_FUNC_current_task_under_cgroup:
  399. return &bpf_current_task_under_cgroup_proto;
  400. case BPF_FUNC_get_prandom_u32:
  401. return &bpf_get_prandom_u32_proto;
  402. case BPF_FUNC_probe_read_str:
  403. return &bpf_probe_read_str_proto;
  404. default:
  405. return NULL;
  406. }
  407. }
  408. static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
  409. {
  410. switch (func_id) {
  411. case BPF_FUNC_perf_event_output:
  412. return &bpf_perf_event_output_proto;
  413. case BPF_FUNC_get_stackid:
  414. return &bpf_get_stackid_proto;
  415. default:
  416. return tracing_func_proto(func_id);
  417. }
  418. }
  419. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  420. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  421. enum bpf_reg_type *reg_type)
  422. {
  423. if (off < 0 || off >= sizeof(struct pt_regs))
  424. return false;
  425. if (type != BPF_READ)
  426. return false;
  427. if (off % size != 0)
  428. return false;
  429. /*
  430. * Assertion for 32 bit to make sure last 8 byte access
  431. * (BPF_DW) to the last 4 byte member is disallowed.
  432. */
  433. if (off + size > sizeof(struct pt_regs))
  434. return false;
  435. return true;
  436. }
  437. static const struct bpf_verifier_ops kprobe_prog_ops = {
  438. .get_func_proto = kprobe_prog_func_proto,
  439. .is_valid_access = kprobe_prog_is_valid_access,
  440. };
  441. static struct bpf_prog_type_list kprobe_tl __ro_after_init = {
  442. .ops = &kprobe_prog_ops,
  443. .type = BPF_PROG_TYPE_KPROBE,
  444. };
  445. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  446. u64, flags, void *, data, u64, size)
  447. {
  448. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  449. /*
  450. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  451. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  452. * from there and call the same bpf_perf_event_output() helper inline.
  453. */
  454. return ____bpf_perf_event_output(regs, map, flags, data, size);
  455. }
  456. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  457. .func = bpf_perf_event_output_tp,
  458. .gpl_only = true,
  459. .ret_type = RET_INTEGER,
  460. .arg1_type = ARG_PTR_TO_CTX,
  461. .arg2_type = ARG_CONST_MAP_PTR,
  462. .arg3_type = ARG_ANYTHING,
  463. .arg4_type = ARG_PTR_TO_MEM,
  464. .arg5_type = ARG_CONST_SIZE,
  465. };
  466. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  467. u64, flags)
  468. {
  469. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  470. /*
  471. * Same comment as in bpf_perf_event_output_tp(), only that this time
  472. * the other helper's function body cannot be inlined due to being
  473. * external, thus we need to call raw helper function.
  474. */
  475. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  476. flags, 0, 0);
  477. }
  478. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  479. .func = bpf_get_stackid_tp,
  480. .gpl_only = true,
  481. .ret_type = RET_INTEGER,
  482. .arg1_type = ARG_PTR_TO_CTX,
  483. .arg2_type = ARG_CONST_MAP_PTR,
  484. .arg3_type = ARG_ANYTHING,
  485. };
  486. static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
  487. {
  488. switch (func_id) {
  489. case BPF_FUNC_perf_event_output:
  490. return &bpf_perf_event_output_proto_tp;
  491. case BPF_FUNC_get_stackid:
  492. return &bpf_get_stackid_proto_tp;
  493. default:
  494. return tracing_func_proto(func_id);
  495. }
  496. }
  497. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  498. enum bpf_reg_type *reg_type)
  499. {
  500. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  501. return false;
  502. if (type != BPF_READ)
  503. return false;
  504. if (off % size != 0)
  505. return false;
  506. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  507. return true;
  508. }
  509. static const struct bpf_verifier_ops tracepoint_prog_ops = {
  510. .get_func_proto = tp_prog_func_proto,
  511. .is_valid_access = tp_prog_is_valid_access,
  512. };
  513. static struct bpf_prog_type_list tracepoint_tl __ro_after_init = {
  514. .ops = &tracepoint_prog_ops,
  515. .type = BPF_PROG_TYPE_TRACEPOINT,
  516. };
  517. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  518. enum bpf_reg_type *reg_type)
  519. {
  520. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  521. return false;
  522. if (type != BPF_READ)
  523. return false;
  524. if (off % size != 0)
  525. return false;
  526. if (off == offsetof(struct bpf_perf_event_data, sample_period)) {
  527. if (size != sizeof(u64))
  528. return false;
  529. } else {
  530. if (size != sizeof(long))
  531. return false;
  532. }
  533. return true;
  534. }
  535. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  536. const struct bpf_insn *si,
  537. struct bpf_insn *insn_buf,
  538. struct bpf_prog *prog)
  539. {
  540. struct bpf_insn *insn = insn_buf;
  541. switch (si->off) {
  542. case offsetof(struct bpf_perf_event_data, sample_period):
  543. BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64));
  544. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  545. data), si->dst_reg, si->src_reg,
  546. offsetof(struct bpf_perf_event_data_kern, data));
  547. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  548. offsetof(struct perf_sample_data, period));
  549. break;
  550. default:
  551. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  552. regs), si->dst_reg, si->src_reg,
  553. offsetof(struct bpf_perf_event_data_kern, regs));
  554. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  555. si->off);
  556. break;
  557. }
  558. return insn - insn_buf;
  559. }
  560. static const struct bpf_verifier_ops perf_event_prog_ops = {
  561. .get_func_proto = tp_prog_func_proto,
  562. .is_valid_access = pe_prog_is_valid_access,
  563. .convert_ctx_access = pe_prog_convert_ctx_access,
  564. };
  565. static struct bpf_prog_type_list perf_event_tl __ro_after_init = {
  566. .ops = &perf_event_prog_ops,
  567. .type = BPF_PROG_TYPE_PERF_EVENT,
  568. };
  569. static int __init register_kprobe_prog_ops(void)
  570. {
  571. bpf_register_prog_type(&kprobe_tl);
  572. bpf_register_prog_type(&tracepoint_tl);
  573. bpf_register_prog_type(&perf_event_tl);
  574. return 0;
  575. }
  576. late_initcall(register_kprobe_prog_ops);