bpf_trace.c 33 KB

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