bpf_trace.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
  3. * Copyright (c) 2016 Facebook
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/slab.h>
  8. #include <linux/bpf.h>
  9. #include <linux/bpf_perf_event.h>
  10. #include <linux/filter.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/ctype.h>
  13. #include <linux/kprobes.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/error-injection.h>
  16. #include "trace_probe.h"
  17. #include "trace.h"
  18. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  19. u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  20. /**
  21. * trace_call_bpf - invoke BPF program
  22. * @call: tracepoint event
  23. * @ctx: opaque context pointer
  24. *
  25. * kprobe handlers execute BPF programs via this helper.
  26. * Can be used from static tracepoints in the future.
  27. *
  28. * Return: BPF programs always return an integer which is interpreted by
  29. * kprobe handler as:
  30. * 0 - return from kprobe (event is filtered out)
  31. * 1 - store kprobe event into ring buffer
  32. * Other values are reserved and currently alias to 1
  33. */
  34. unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
  35. {
  36. unsigned int ret;
  37. if (in_nmi()) /* not supported yet */
  38. return 1;
  39. preempt_disable();
  40. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  41. /*
  42. * since some bpf program is already running on this cpu,
  43. * don't call into another bpf program (same or different)
  44. * and don't send kprobe event into ring-buffer,
  45. * so return zero here
  46. */
  47. ret = 0;
  48. goto out;
  49. }
  50. /*
  51. * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
  52. * to all call sites, we did a bpf_prog_array_valid() there to check
  53. * whether call->prog_array is empty or not, which is
  54. * a heurisitc to speed up execution.
  55. *
  56. * If bpf_prog_array_valid() fetched prog_array was
  57. * non-NULL, we go into trace_call_bpf() and do the actual
  58. * proper rcu_dereference() under RCU lock.
  59. * If it turns out that prog_array is NULL then, we bail out.
  60. * For the opposite, if the bpf_prog_array_valid() fetched pointer
  61. * was NULL, you'll skip the prog_array with the risk of missing
  62. * out of events when it was updated in between this and the
  63. * rcu_dereference() which is accepted risk.
  64. */
  65. ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
  66. out:
  67. __this_cpu_dec(bpf_prog_active);
  68. preempt_enable();
  69. return ret;
  70. }
  71. EXPORT_SYMBOL_GPL(trace_call_bpf);
  72. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  73. BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
  74. {
  75. regs_set_return_value(regs, rc);
  76. override_function_with_return(regs);
  77. return 0;
  78. }
  79. static const struct bpf_func_proto bpf_override_return_proto = {
  80. .func = bpf_override_return,
  81. .gpl_only = true,
  82. .ret_type = RET_INTEGER,
  83. .arg1_type = ARG_PTR_TO_CTX,
  84. .arg2_type = ARG_ANYTHING,
  85. };
  86. #endif
  87. BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
  88. {
  89. int ret;
  90. ret = probe_kernel_read(dst, unsafe_ptr, size);
  91. if (unlikely(ret < 0))
  92. memset(dst, 0, size);
  93. return ret;
  94. }
  95. static const struct bpf_func_proto bpf_probe_read_proto = {
  96. .func = bpf_probe_read,
  97. .gpl_only = true,
  98. .ret_type = RET_INTEGER,
  99. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  100. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  101. .arg3_type = ARG_ANYTHING,
  102. };
  103. BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
  104. u32, size)
  105. {
  106. /*
  107. * Ensure we're in user context which is safe for the helper to
  108. * run. This helper has no business in a kthread.
  109. *
  110. * access_ok() should prevent writing to non-user memory, but in
  111. * some situations (nommu, temporary switch, etc) access_ok() does
  112. * not provide enough validation, hence the check on KERNEL_DS.
  113. */
  114. if (unlikely(in_interrupt() ||
  115. current->flags & (PF_KTHREAD | PF_EXITING)))
  116. return -EPERM;
  117. if (unlikely(uaccess_kernel()))
  118. return -EPERM;
  119. if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
  120. return -EPERM;
  121. return probe_kernel_write(unsafe_ptr, src, size);
  122. }
  123. static const struct bpf_func_proto bpf_probe_write_user_proto = {
  124. .func = bpf_probe_write_user,
  125. .gpl_only = true,
  126. .ret_type = RET_INTEGER,
  127. .arg1_type = ARG_ANYTHING,
  128. .arg2_type = ARG_PTR_TO_MEM,
  129. .arg3_type = ARG_CONST_SIZE,
  130. };
  131. static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
  132. {
  133. pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
  134. current->comm, task_pid_nr(current));
  135. return &bpf_probe_write_user_proto;
  136. }
  137. /*
  138. * Only limited trace_printk() conversion specifiers allowed:
  139. * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
  140. */
  141. BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
  142. u64, arg2, u64, arg3)
  143. {
  144. bool str_seen = false;
  145. int mod[3] = {};
  146. int fmt_cnt = 0;
  147. u64 unsafe_addr;
  148. char buf[64];
  149. int i;
  150. /*
  151. * bpf_check()->check_func_arg()->check_stack_boundary()
  152. * guarantees that fmt points to bpf program stack,
  153. * fmt_size bytes of it were initialized and fmt_size > 0
  154. */
  155. if (fmt[--fmt_size] != 0)
  156. return -EINVAL;
  157. /* check format string for allowed specifiers */
  158. for (i = 0; i < fmt_size; i++) {
  159. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  160. return -EINVAL;
  161. if (fmt[i] != '%')
  162. continue;
  163. if (fmt_cnt >= 3)
  164. return -EINVAL;
  165. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  166. i++;
  167. if (fmt[i] == 'l') {
  168. mod[fmt_cnt]++;
  169. i++;
  170. } else if (fmt[i] == 'p' || fmt[i] == 's') {
  171. mod[fmt_cnt]++;
  172. i++;
  173. if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
  174. return -EINVAL;
  175. fmt_cnt++;
  176. if (fmt[i - 1] == 's') {
  177. if (str_seen)
  178. /* allow only one '%s' per fmt string */
  179. return -EINVAL;
  180. str_seen = true;
  181. switch (fmt_cnt) {
  182. case 1:
  183. unsafe_addr = arg1;
  184. arg1 = (long) buf;
  185. break;
  186. case 2:
  187. unsafe_addr = arg2;
  188. arg2 = (long) buf;
  189. break;
  190. case 3:
  191. unsafe_addr = arg3;
  192. arg3 = (long) buf;
  193. break;
  194. }
  195. buf[0] = 0;
  196. strncpy_from_unsafe(buf,
  197. (void *) (long) unsafe_addr,
  198. sizeof(buf));
  199. }
  200. continue;
  201. }
  202. if (fmt[i] == 'l') {
  203. mod[fmt_cnt]++;
  204. i++;
  205. }
  206. if (fmt[i] != 'i' && fmt[i] != 'd' &&
  207. fmt[i] != 'u' && fmt[i] != 'x')
  208. return -EINVAL;
  209. fmt_cnt++;
  210. }
  211. /* Horrid workaround for getting va_list handling working with different
  212. * argument type combinations generically for 32 and 64 bit archs.
  213. */
  214. #define __BPF_TP_EMIT() __BPF_ARG3_TP()
  215. #define __BPF_TP(...) \
  216. __trace_printk(0 /* Fake ip */, \
  217. fmt, ##__VA_ARGS__)
  218. #define __BPF_ARG1_TP(...) \
  219. ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
  220. ? __BPF_TP(arg1, ##__VA_ARGS__) \
  221. : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
  222. ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
  223. : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
  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. #define __BPF_ARG3_TP(...) \
  231. ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
  232. ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
  233. : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
  234. ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
  235. : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
  236. return __BPF_TP_EMIT();
  237. }
  238. static const struct bpf_func_proto bpf_trace_printk_proto = {
  239. .func = bpf_trace_printk,
  240. .gpl_only = true,
  241. .ret_type = RET_INTEGER,
  242. .arg1_type = ARG_PTR_TO_MEM,
  243. .arg2_type = ARG_CONST_SIZE,
  244. };
  245. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  246. {
  247. /*
  248. * this program might be calling bpf_trace_printk,
  249. * so allocate per-cpu printk buffers
  250. */
  251. trace_printk_init_buffers();
  252. return &bpf_trace_printk_proto;
  253. }
  254. static __always_inline int
  255. get_map_perf_counter(struct bpf_map *map, u64 flags,
  256. u64 *value, u64 *enabled, u64 *running)
  257. {
  258. struct bpf_array *array = container_of(map, struct bpf_array, map);
  259. unsigned int cpu = smp_processor_id();
  260. u64 index = flags & BPF_F_INDEX_MASK;
  261. struct bpf_event_entry *ee;
  262. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  263. return -EINVAL;
  264. if (index == BPF_F_CURRENT_CPU)
  265. index = cpu;
  266. if (unlikely(index >= array->map.max_entries))
  267. return -E2BIG;
  268. ee = READ_ONCE(array->ptrs[index]);
  269. if (!ee)
  270. return -ENOENT;
  271. return perf_event_read_local(ee->event, value, enabled, running);
  272. }
  273. BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
  274. {
  275. u64 value = 0;
  276. int err;
  277. err = get_map_perf_counter(map, flags, &value, NULL, NULL);
  278. /*
  279. * this api is ugly since we miss [-22..-2] range of valid
  280. * counter values, but that's uapi
  281. */
  282. if (err)
  283. return err;
  284. return value;
  285. }
  286. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  287. .func = bpf_perf_event_read,
  288. .gpl_only = true,
  289. .ret_type = RET_INTEGER,
  290. .arg1_type = ARG_CONST_MAP_PTR,
  291. .arg2_type = ARG_ANYTHING,
  292. };
  293. BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
  294. struct bpf_perf_event_value *, buf, u32, size)
  295. {
  296. int err = -EINVAL;
  297. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  298. goto clear;
  299. err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
  300. &buf->running);
  301. if (unlikely(err))
  302. goto clear;
  303. return 0;
  304. clear:
  305. memset(buf, 0, size);
  306. return err;
  307. }
  308. static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
  309. .func = bpf_perf_event_read_value,
  310. .gpl_only = true,
  311. .ret_type = RET_INTEGER,
  312. .arg1_type = ARG_CONST_MAP_PTR,
  313. .arg2_type = ARG_ANYTHING,
  314. .arg3_type = ARG_PTR_TO_UNINIT_MEM,
  315. .arg4_type = ARG_CONST_SIZE,
  316. };
  317. static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
  318. static __always_inline u64
  319. __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
  320. u64 flags, struct perf_sample_data *sd)
  321. {
  322. struct bpf_array *array = container_of(map, struct bpf_array, map);
  323. unsigned int cpu = smp_processor_id();
  324. u64 index = flags & BPF_F_INDEX_MASK;
  325. struct bpf_event_entry *ee;
  326. struct perf_event *event;
  327. if (index == BPF_F_CURRENT_CPU)
  328. index = cpu;
  329. if (unlikely(index >= array->map.max_entries))
  330. return -E2BIG;
  331. ee = READ_ONCE(array->ptrs[index]);
  332. if (!ee)
  333. return -ENOENT;
  334. event = ee->event;
  335. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  336. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  337. return -EINVAL;
  338. if (unlikely(event->oncpu != cpu))
  339. return -EOPNOTSUPP;
  340. perf_event_output(event, sd, regs);
  341. return 0;
  342. }
  343. BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
  344. u64, flags, void *, data, u64, size)
  345. {
  346. struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
  347. struct perf_raw_record raw = {
  348. .frag = {
  349. .size = size,
  350. .data = data,
  351. },
  352. };
  353. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  354. return -EINVAL;
  355. perf_sample_data_init(sd, 0, 0);
  356. sd->raw = &raw;
  357. return __bpf_perf_event_output(regs, map, flags, sd);
  358. }
  359. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  360. .func = bpf_perf_event_output,
  361. .gpl_only = true,
  362. .ret_type = RET_INTEGER,
  363. .arg1_type = ARG_PTR_TO_CTX,
  364. .arg2_type = ARG_CONST_MAP_PTR,
  365. .arg3_type = ARG_ANYTHING,
  366. .arg4_type = ARG_PTR_TO_MEM,
  367. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  368. };
  369. static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
  370. static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
  371. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  372. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  373. {
  374. struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
  375. struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
  376. struct perf_raw_frag frag = {
  377. .copy = ctx_copy,
  378. .size = ctx_size,
  379. .data = ctx,
  380. };
  381. struct perf_raw_record raw = {
  382. .frag = {
  383. {
  384. .next = ctx_size ? &frag : NULL,
  385. },
  386. .size = meta_size,
  387. .data = meta,
  388. },
  389. };
  390. perf_fetch_caller_regs(regs);
  391. perf_sample_data_init(sd, 0, 0);
  392. sd->raw = &raw;
  393. return __bpf_perf_event_output(regs, map, flags, sd);
  394. }
  395. BPF_CALL_0(bpf_get_current_task)
  396. {
  397. return (long) current;
  398. }
  399. static const struct bpf_func_proto bpf_get_current_task_proto = {
  400. .func = bpf_get_current_task,
  401. .gpl_only = true,
  402. .ret_type = RET_INTEGER,
  403. };
  404. BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
  405. {
  406. struct bpf_array *array = container_of(map, struct bpf_array, map);
  407. struct cgroup *cgrp;
  408. if (unlikely(idx >= array->map.max_entries))
  409. return -E2BIG;
  410. cgrp = READ_ONCE(array->ptrs[idx]);
  411. if (unlikely(!cgrp))
  412. return -EAGAIN;
  413. return task_under_cgroup_hierarchy(current, cgrp);
  414. }
  415. static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
  416. .func = bpf_current_task_under_cgroup,
  417. .gpl_only = false,
  418. .ret_type = RET_INTEGER,
  419. .arg1_type = ARG_CONST_MAP_PTR,
  420. .arg2_type = ARG_ANYTHING,
  421. };
  422. BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
  423. const void *, unsafe_ptr)
  424. {
  425. int ret;
  426. /*
  427. * The strncpy_from_unsafe() call will likely not fill the entire
  428. * buffer, but that's okay in this circumstance as we're probing
  429. * arbitrary memory anyway similar to bpf_probe_read() and might
  430. * as well probe the stack. Thus, memory is explicitly cleared
  431. * only in error case, so that improper users ignoring return
  432. * code altogether don't copy garbage; otherwise length of string
  433. * is returned that can be used for bpf_perf_event_output() et al.
  434. */
  435. ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
  436. if (unlikely(ret < 0))
  437. memset(dst, 0, size);
  438. return ret;
  439. }
  440. static const struct bpf_func_proto bpf_probe_read_str_proto = {
  441. .func = bpf_probe_read_str,
  442. .gpl_only = true,
  443. .ret_type = RET_INTEGER,
  444. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  445. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  446. .arg3_type = ARG_ANYTHING,
  447. };
  448. static const struct bpf_func_proto *
  449. tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  450. {
  451. switch (func_id) {
  452. case BPF_FUNC_map_lookup_elem:
  453. return &bpf_map_lookup_elem_proto;
  454. case BPF_FUNC_map_update_elem:
  455. return &bpf_map_update_elem_proto;
  456. case BPF_FUNC_map_delete_elem:
  457. return &bpf_map_delete_elem_proto;
  458. case BPF_FUNC_probe_read:
  459. return &bpf_probe_read_proto;
  460. case BPF_FUNC_ktime_get_ns:
  461. return &bpf_ktime_get_ns_proto;
  462. case BPF_FUNC_tail_call:
  463. return &bpf_tail_call_proto;
  464. case BPF_FUNC_get_current_pid_tgid:
  465. return &bpf_get_current_pid_tgid_proto;
  466. case BPF_FUNC_get_current_task:
  467. return &bpf_get_current_task_proto;
  468. case BPF_FUNC_get_current_uid_gid:
  469. return &bpf_get_current_uid_gid_proto;
  470. case BPF_FUNC_get_current_comm:
  471. return &bpf_get_current_comm_proto;
  472. case BPF_FUNC_trace_printk:
  473. return bpf_get_trace_printk_proto();
  474. case BPF_FUNC_get_smp_processor_id:
  475. return &bpf_get_smp_processor_id_proto;
  476. case BPF_FUNC_get_numa_node_id:
  477. return &bpf_get_numa_node_id_proto;
  478. case BPF_FUNC_perf_event_read:
  479. return &bpf_perf_event_read_proto;
  480. case BPF_FUNC_probe_write_user:
  481. return bpf_get_probe_write_proto();
  482. case BPF_FUNC_current_task_under_cgroup:
  483. return &bpf_current_task_under_cgroup_proto;
  484. case BPF_FUNC_get_prandom_u32:
  485. return &bpf_get_prandom_u32_proto;
  486. case BPF_FUNC_probe_read_str:
  487. return &bpf_probe_read_str_proto;
  488. #ifdef CONFIG_CGROUPS
  489. case BPF_FUNC_get_current_cgroup_id:
  490. return &bpf_get_current_cgroup_id_proto;
  491. #endif
  492. default:
  493. return NULL;
  494. }
  495. }
  496. static const struct bpf_func_proto *
  497. kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  498. {
  499. switch (func_id) {
  500. case BPF_FUNC_perf_event_output:
  501. return &bpf_perf_event_output_proto;
  502. case BPF_FUNC_get_stackid:
  503. return &bpf_get_stackid_proto;
  504. case BPF_FUNC_get_stack:
  505. return &bpf_get_stack_proto;
  506. case BPF_FUNC_perf_event_read_value:
  507. return &bpf_perf_event_read_value_proto;
  508. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  509. case BPF_FUNC_override_return:
  510. return &bpf_override_return_proto;
  511. #endif
  512. default:
  513. return tracing_func_proto(func_id, prog);
  514. }
  515. }
  516. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  517. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  518. const struct bpf_prog *prog,
  519. struct bpf_insn_access_aux *info)
  520. {
  521. if (off < 0 || off >= sizeof(struct pt_regs))
  522. return false;
  523. if (type != BPF_READ)
  524. return false;
  525. if (off % size != 0)
  526. return false;
  527. /*
  528. * Assertion for 32 bit to make sure last 8 byte access
  529. * (BPF_DW) to the last 4 byte member is disallowed.
  530. */
  531. if (off + size > sizeof(struct pt_regs))
  532. return false;
  533. return true;
  534. }
  535. const struct bpf_verifier_ops kprobe_verifier_ops = {
  536. .get_func_proto = kprobe_prog_func_proto,
  537. .is_valid_access = kprobe_prog_is_valid_access,
  538. };
  539. const struct bpf_prog_ops kprobe_prog_ops = {
  540. };
  541. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  542. u64, flags, void *, data, u64, size)
  543. {
  544. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  545. /*
  546. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  547. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  548. * from there and call the same bpf_perf_event_output() helper inline.
  549. */
  550. return ____bpf_perf_event_output(regs, map, flags, data, size);
  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_OR_ZERO,
  561. };
  562. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  563. u64, flags)
  564. {
  565. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  566. /*
  567. * Same comment as in bpf_perf_event_output_tp(), only that this time
  568. * the other helper's function body cannot be inlined due to being
  569. * external, thus we need to call raw helper function.
  570. */
  571. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  572. flags, 0, 0);
  573. }
  574. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  575. .func = bpf_get_stackid_tp,
  576. .gpl_only = true,
  577. .ret_type = RET_INTEGER,
  578. .arg1_type = ARG_PTR_TO_CTX,
  579. .arg2_type = ARG_CONST_MAP_PTR,
  580. .arg3_type = ARG_ANYTHING,
  581. };
  582. BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
  583. u64, flags)
  584. {
  585. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  586. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  587. (unsigned long) size, flags, 0);
  588. }
  589. static const struct bpf_func_proto bpf_get_stack_proto_tp = {
  590. .func = bpf_get_stack_tp,
  591. .gpl_only = true,
  592. .ret_type = RET_INTEGER,
  593. .arg1_type = ARG_PTR_TO_CTX,
  594. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  595. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  596. .arg4_type = ARG_ANYTHING,
  597. };
  598. static const struct bpf_func_proto *
  599. tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  600. {
  601. switch (func_id) {
  602. case BPF_FUNC_perf_event_output:
  603. return &bpf_perf_event_output_proto_tp;
  604. case BPF_FUNC_get_stackid:
  605. return &bpf_get_stackid_proto_tp;
  606. case BPF_FUNC_get_stack:
  607. return &bpf_get_stack_proto_tp;
  608. default:
  609. return tracing_func_proto(func_id, prog);
  610. }
  611. }
  612. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  613. const struct bpf_prog *prog,
  614. struct bpf_insn_access_aux *info)
  615. {
  616. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  617. return false;
  618. if (type != BPF_READ)
  619. return false;
  620. if (off % size != 0)
  621. return false;
  622. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  623. return true;
  624. }
  625. const struct bpf_verifier_ops tracepoint_verifier_ops = {
  626. .get_func_proto = tp_prog_func_proto,
  627. .is_valid_access = tp_prog_is_valid_access,
  628. };
  629. const struct bpf_prog_ops tracepoint_prog_ops = {
  630. };
  631. BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
  632. struct bpf_perf_event_value *, buf, u32, size)
  633. {
  634. int err = -EINVAL;
  635. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  636. goto clear;
  637. err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
  638. &buf->running);
  639. if (unlikely(err))
  640. goto clear;
  641. return 0;
  642. clear:
  643. memset(buf, 0, size);
  644. return err;
  645. }
  646. static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  647. .func = bpf_perf_prog_read_value,
  648. .gpl_only = true,
  649. .ret_type = RET_INTEGER,
  650. .arg1_type = ARG_PTR_TO_CTX,
  651. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  652. .arg3_type = ARG_CONST_SIZE,
  653. };
  654. static const struct bpf_func_proto *
  655. pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  656. {
  657. switch (func_id) {
  658. case BPF_FUNC_perf_event_output:
  659. return &bpf_perf_event_output_proto_tp;
  660. case BPF_FUNC_get_stackid:
  661. return &bpf_get_stackid_proto_tp;
  662. case BPF_FUNC_get_stack:
  663. return &bpf_get_stack_proto_tp;
  664. case BPF_FUNC_perf_prog_read_value:
  665. return &bpf_perf_prog_read_value_proto;
  666. default:
  667. return tracing_func_proto(func_id, prog);
  668. }
  669. }
  670. /*
  671. * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
  672. * to avoid potential recursive reuse issue when/if tracepoints are added
  673. * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
  674. */
  675. static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
  676. BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
  677. struct bpf_map *, map, u64, flags, void *, data, u64, size)
  678. {
  679. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  680. perf_fetch_caller_regs(regs);
  681. return ____bpf_perf_event_output(regs, map, flags, data, size);
  682. }
  683. static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
  684. .func = bpf_perf_event_output_raw_tp,
  685. .gpl_only = true,
  686. .ret_type = RET_INTEGER,
  687. .arg1_type = ARG_PTR_TO_CTX,
  688. .arg2_type = ARG_CONST_MAP_PTR,
  689. .arg3_type = ARG_ANYTHING,
  690. .arg4_type = ARG_PTR_TO_MEM,
  691. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  692. };
  693. BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
  694. struct bpf_map *, map, u64, flags)
  695. {
  696. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  697. perf_fetch_caller_regs(regs);
  698. /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
  699. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  700. flags, 0, 0);
  701. }
  702. static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
  703. .func = bpf_get_stackid_raw_tp,
  704. .gpl_only = true,
  705. .ret_type = RET_INTEGER,
  706. .arg1_type = ARG_PTR_TO_CTX,
  707. .arg2_type = ARG_CONST_MAP_PTR,
  708. .arg3_type = ARG_ANYTHING,
  709. };
  710. BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
  711. void *, buf, u32, size, u64, flags)
  712. {
  713. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  714. perf_fetch_caller_regs(regs);
  715. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  716. (unsigned long) size, flags, 0);
  717. }
  718. static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
  719. .func = bpf_get_stack_raw_tp,
  720. .gpl_only = true,
  721. .ret_type = RET_INTEGER,
  722. .arg1_type = ARG_PTR_TO_CTX,
  723. .arg2_type = ARG_PTR_TO_MEM,
  724. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  725. .arg4_type = ARG_ANYTHING,
  726. };
  727. static const struct bpf_func_proto *
  728. raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  729. {
  730. switch (func_id) {
  731. case BPF_FUNC_perf_event_output:
  732. return &bpf_perf_event_output_proto_raw_tp;
  733. case BPF_FUNC_get_stackid:
  734. return &bpf_get_stackid_proto_raw_tp;
  735. case BPF_FUNC_get_stack:
  736. return &bpf_get_stack_proto_raw_tp;
  737. default:
  738. return tracing_func_proto(func_id, prog);
  739. }
  740. }
  741. static bool raw_tp_prog_is_valid_access(int off, int size,
  742. enum bpf_access_type type,
  743. const struct bpf_prog *prog,
  744. struct bpf_insn_access_aux *info)
  745. {
  746. /* largest tracepoint in the kernel has 12 args */
  747. if (off < 0 || off >= sizeof(__u64) * 12)
  748. return false;
  749. if (type != BPF_READ)
  750. return false;
  751. if (off % size != 0)
  752. return false;
  753. return true;
  754. }
  755. const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
  756. .get_func_proto = raw_tp_prog_func_proto,
  757. .is_valid_access = raw_tp_prog_is_valid_access,
  758. };
  759. const struct bpf_prog_ops raw_tracepoint_prog_ops = {
  760. };
  761. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  762. const struct bpf_prog *prog,
  763. struct bpf_insn_access_aux *info)
  764. {
  765. const int size_u64 = sizeof(u64);
  766. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  767. return false;
  768. if (type != BPF_READ)
  769. return false;
  770. if (off % size != 0) {
  771. if (sizeof(unsigned long) != 4)
  772. return false;
  773. if (size != 8)
  774. return false;
  775. if (off % size != 4)
  776. return false;
  777. }
  778. switch (off) {
  779. case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
  780. bpf_ctx_record_field_size(info, size_u64);
  781. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  782. return false;
  783. break;
  784. case bpf_ctx_range(struct bpf_perf_event_data, addr):
  785. bpf_ctx_record_field_size(info, size_u64);
  786. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  787. return false;
  788. break;
  789. default:
  790. if (size != sizeof(long))
  791. return false;
  792. }
  793. return true;
  794. }
  795. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  796. const struct bpf_insn *si,
  797. struct bpf_insn *insn_buf,
  798. struct bpf_prog *prog, u32 *target_size)
  799. {
  800. struct bpf_insn *insn = insn_buf;
  801. switch (si->off) {
  802. case offsetof(struct bpf_perf_event_data, sample_period):
  803. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  804. data), si->dst_reg, si->src_reg,
  805. offsetof(struct bpf_perf_event_data_kern, data));
  806. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  807. bpf_target_off(struct perf_sample_data, period, 8,
  808. target_size));
  809. break;
  810. case offsetof(struct bpf_perf_event_data, addr):
  811. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  812. data), si->dst_reg, si->src_reg,
  813. offsetof(struct bpf_perf_event_data_kern, data));
  814. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  815. bpf_target_off(struct perf_sample_data, addr, 8,
  816. target_size));
  817. break;
  818. default:
  819. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  820. regs), si->dst_reg, si->src_reg,
  821. offsetof(struct bpf_perf_event_data_kern, regs));
  822. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  823. si->off);
  824. break;
  825. }
  826. return insn - insn_buf;
  827. }
  828. const struct bpf_verifier_ops perf_event_verifier_ops = {
  829. .get_func_proto = pe_prog_func_proto,
  830. .is_valid_access = pe_prog_is_valid_access,
  831. .convert_ctx_access = pe_prog_convert_ctx_access,
  832. };
  833. const struct bpf_prog_ops perf_event_prog_ops = {
  834. };
  835. static DEFINE_MUTEX(bpf_event_mutex);
  836. #define BPF_TRACE_MAX_PROGS 64
  837. int perf_event_attach_bpf_prog(struct perf_event *event,
  838. struct bpf_prog *prog)
  839. {
  840. struct bpf_prog_array __rcu *old_array;
  841. struct bpf_prog_array *new_array;
  842. int ret = -EEXIST;
  843. /*
  844. * Kprobe override only works if they are on the function entry,
  845. * and only if they are on the opt-in list.
  846. */
  847. if (prog->kprobe_override &&
  848. (!trace_kprobe_on_func_entry(event->tp_event) ||
  849. !trace_kprobe_error_injectable(event->tp_event)))
  850. return -EINVAL;
  851. mutex_lock(&bpf_event_mutex);
  852. if (event->prog)
  853. goto unlock;
  854. old_array = event->tp_event->prog_array;
  855. if (old_array &&
  856. bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
  857. ret = -E2BIG;
  858. goto unlock;
  859. }
  860. ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
  861. if (ret < 0)
  862. goto unlock;
  863. /* set the new array to event->tp_event and set event->prog */
  864. event->prog = prog;
  865. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  866. bpf_prog_array_free(old_array);
  867. unlock:
  868. mutex_unlock(&bpf_event_mutex);
  869. return ret;
  870. }
  871. void perf_event_detach_bpf_prog(struct perf_event *event)
  872. {
  873. struct bpf_prog_array __rcu *old_array;
  874. struct bpf_prog_array *new_array;
  875. int ret;
  876. mutex_lock(&bpf_event_mutex);
  877. if (!event->prog)
  878. goto unlock;
  879. old_array = event->tp_event->prog_array;
  880. ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
  881. if (ret == -ENOENT)
  882. goto unlock;
  883. if (ret < 0) {
  884. bpf_prog_array_delete_safe(old_array, event->prog);
  885. } else {
  886. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  887. bpf_prog_array_free(old_array);
  888. }
  889. bpf_prog_put(event->prog);
  890. event->prog = NULL;
  891. unlock:
  892. mutex_unlock(&bpf_event_mutex);
  893. }
  894. int perf_event_query_prog_array(struct perf_event *event, void __user *info)
  895. {
  896. struct perf_event_query_bpf __user *uquery = info;
  897. struct perf_event_query_bpf query = {};
  898. u32 *ids, prog_cnt, ids_len;
  899. int ret;
  900. if (!capable(CAP_SYS_ADMIN))
  901. return -EPERM;
  902. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  903. return -EINVAL;
  904. if (copy_from_user(&query, uquery, sizeof(query)))
  905. return -EFAULT;
  906. ids_len = query.ids_len;
  907. if (ids_len > BPF_TRACE_MAX_PROGS)
  908. return -E2BIG;
  909. ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
  910. if (!ids)
  911. return -ENOMEM;
  912. /*
  913. * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
  914. * is required when user only wants to check for uquery->prog_cnt.
  915. * There is no need to check for it since the case is handled
  916. * gracefully in bpf_prog_array_copy_info.
  917. */
  918. mutex_lock(&bpf_event_mutex);
  919. ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
  920. ids,
  921. ids_len,
  922. &prog_cnt);
  923. mutex_unlock(&bpf_event_mutex);
  924. if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
  925. copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
  926. ret = -EFAULT;
  927. kfree(ids);
  928. return ret;
  929. }
  930. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  931. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  932. struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
  933. {
  934. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  935. for (; btp < __stop__bpf_raw_tp; btp++) {
  936. if (!strcmp(btp->tp->name, name))
  937. return btp;
  938. }
  939. return NULL;
  940. }
  941. static __always_inline
  942. void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
  943. {
  944. rcu_read_lock();
  945. preempt_disable();
  946. (void) BPF_PROG_RUN(prog, args);
  947. preempt_enable();
  948. rcu_read_unlock();
  949. }
  950. #define UNPACK(...) __VA_ARGS__
  951. #define REPEAT_1(FN, DL, X, ...) FN(X)
  952. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  953. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  954. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  955. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  956. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  957. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  958. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  959. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  960. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  961. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  962. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  963. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  964. #define SARG(X) u64 arg##X
  965. #define COPY(X) args[X] = arg##X
  966. #define __DL_COM (,)
  967. #define __DL_SEM (;)
  968. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  969. #define BPF_TRACE_DEFN_x(x) \
  970. void bpf_trace_run##x(struct bpf_prog *prog, \
  971. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  972. { \
  973. u64 args[x]; \
  974. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  975. __bpf_trace_run(prog, args); \
  976. } \
  977. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  978. BPF_TRACE_DEFN_x(1);
  979. BPF_TRACE_DEFN_x(2);
  980. BPF_TRACE_DEFN_x(3);
  981. BPF_TRACE_DEFN_x(4);
  982. BPF_TRACE_DEFN_x(5);
  983. BPF_TRACE_DEFN_x(6);
  984. BPF_TRACE_DEFN_x(7);
  985. BPF_TRACE_DEFN_x(8);
  986. BPF_TRACE_DEFN_x(9);
  987. BPF_TRACE_DEFN_x(10);
  988. BPF_TRACE_DEFN_x(11);
  989. BPF_TRACE_DEFN_x(12);
  990. static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  991. {
  992. struct tracepoint *tp = btp->tp;
  993. /*
  994. * check that program doesn't access arguments beyond what's
  995. * available in this tracepoint
  996. */
  997. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  998. return -EINVAL;
  999. return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
  1000. }
  1001. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  1002. {
  1003. int err;
  1004. mutex_lock(&bpf_event_mutex);
  1005. err = __bpf_probe_register(btp, prog);
  1006. mutex_unlock(&bpf_event_mutex);
  1007. return err;
  1008. }
  1009. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  1010. {
  1011. int err;
  1012. mutex_lock(&bpf_event_mutex);
  1013. err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
  1014. mutex_unlock(&bpf_event_mutex);
  1015. return err;
  1016. }
  1017. int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
  1018. u32 *fd_type, const char **buf,
  1019. u64 *probe_offset, u64 *probe_addr)
  1020. {
  1021. bool is_tracepoint, is_syscall_tp;
  1022. struct bpf_prog *prog;
  1023. int flags, err = 0;
  1024. prog = event->prog;
  1025. if (!prog)
  1026. return -ENOENT;
  1027. /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
  1028. if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
  1029. return -EOPNOTSUPP;
  1030. *prog_id = prog->aux->id;
  1031. flags = event->tp_event->flags;
  1032. is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
  1033. is_syscall_tp = is_syscall_trace_event(event->tp_event);
  1034. if (is_tracepoint || is_syscall_tp) {
  1035. *buf = is_tracepoint ? event->tp_event->tp->name
  1036. : event->tp_event->name;
  1037. *fd_type = BPF_FD_TYPE_TRACEPOINT;
  1038. *probe_offset = 0x0;
  1039. *probe_addr = 0x0;
  1040. } else {
  1041. /* kprobe/uprobe */
  1042. err = -EOPNOTSUPP;
  1043. #ifdef CONFIG_KPROBE_EVENTS
  1044. if (flags & TRACE_EVENT_FL_KPROBE)
  1045. err = bpf_get_kprobe_info(event, fd_type, buf,
  1046. probe_offset, probe_addr,
  1047. event->attr.type == PERF_TYPE_TRACEPOINT);
  1048. #endif
  1049. #ifdef CONFIG_UPROBE_EVENTS
  1050. if (flags & TRACE_EVENT_FL_UPROBE)
  1051. err = bpf_get_uprobe_info(event, fd_type, buf,
  1052. probe_offset,
  1053. event->attr.type == PERF_TYPE_TRACEPOINT);
  1054. #endif
  1055. }
  1056. return err;
  1057. }