bpf_trace.c 17 KB

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