bpf_trace.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. default:
  492. return NULL;
  493. }
  494. }
  495. static const struct bpf_func_proto *
  496. kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  497. {
  498. switch (func_id) {
  499. case BPF_FUNC_perf_event_output:
  500. return &bpf_perf_event_output_proto;
  501. case BPF_FUNC_get_stackid:
  502. return &bpf_get_stackid_proto;
  503. case BPF_FUNC_get_stack:
  504. return &bpf_get_stack_proto;
  505. case BPF_FUNC_perf_event_read_value:
  506. return &bpf_perf_event_read_value_proto;
  507. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  508. case BPF_FUNC_override_return:
  509. return &bpf_override_return_proto;
  510. #endif
  511. default:
  512. return tracing_func_proto(func_id, prog);
  513. }
  514. }
  515. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  516. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  517. const struct bpf_prog *prog,
  518. struct bpf_insn_access_aux *info)
  519. {
  520. if (off < 0 || off >= sizeof(struct pt_regs))
  521. return false;
  522. if (type != BPF_READ)
  523. return false;
  524. if (off % size != 0)
  525. return false;
  526. /*
  527. * Assertion for 32 bit to make sure last 8 byte access
  528. * (BPF_DW) to the last 4 byte member is disallowed.
  529. */
  530. if (off + size > sizeof(struct pt_regs))
  531. return false;
  532. return true;
  533. }
  534. const struct bpf_verifier_ops kprobe_verifier_ops = {
  535. .get_func_proto = kprobe_prog_func_proto,
  536. .is_valid_access = kprobe_prog_is_valid_access,
  537. };
  538. const struct bpf_prog_ops kprobe_prog_ops = {
  539. };
  540. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  541. u64, flags, void *, data, u64, size)
  542. {
  543. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  544. /*
  545. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  546. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  547. * from there and call the same bpf_perf_event_output() helper inline.
  548. */
  549. return ____bpf_perf_event_output(regs, map, flags, data, size);
  550. }
  551. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  552. .func = bpf_perf_event_output_tp,
  553. .gpl_only = true,
  554. .ret_type = RET_INTEGER,
  555. .arg1_type = ARG_PTR_TO_CTX,
  556. .arg2_type = ARG_CONST_MAP_PTR,
  557. .arg3_type = ARG_ANYTHING,
  558. .arg4_type = ARG_PTR_TO_MEM,
  559. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  560. };
  561. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  562. u64, flags)
  563. {
  564. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  565. /*
  566. * Same comment as in bpf_perf_event_output_tp(), only that this time
  567. * the other helper's function body cannot be inlined due to being
  568. * external, thus we need to call raw helper function.
  569. */
  570. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  571. flags, 0, 0);
  572. }
  573. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  574. .func = bpf_get_stackid_tp,
  575. .gpl_only = true,
  576. .ret_type = RET_INTEGER,
  577. .arg1_type = ARG_PTR_TO_CTX,
  578. .arg2_type = ARG_CONST_MAP_PTR,
  579. .arg3_type = ARG_ANYTHING,
  580. };
  581. BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
  582. u64, flags)
  583. {
  584. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  585. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  586. (unsigned long) size, flags, 0);
  587. }
  588. static const struct bpf_func_proto bpf_get_stack_proto_tp = {
  589. .func = bpf_get_stack_tp,
  590. .gpl_only = true,
  591. .ret_type = RET_INTEGER,
  592. .arg1_type = ARG_PTR_TO_CTX,
  593. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  594. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  595. .arg4_type = ARG_ANYTHING,
  596. };
  597. static const struct bpf_func_proto *
  598. tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  599. {
  600. switch (func_id) {
  601. case BPF_FUNC_perf_event_output:
  602. return &bpf_perf_event_output_proto_tp;
  603. case BPF_FUNC_get_stackid:
  604. return &bpf_get_stackid_proto_tp;
  605. case BPF_FUNC_get_stack:
  606. return &bpf_get_stack_proto_tp;
  607. default:
  608. return tracing_func_proto(func_id, prog);
  609. }
  610. }
  611. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  612. const struct bpf_prog *prog,
  613. struct bpf_insn_access_aux *info)
  614. {
  615. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  616. return false;
  617. if (type != BPF_READ)
  618. return false;
  619. if (off % size != 0)
  620. return false;
  621. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  622. return true;
  623. }
  624. const struct bpf_verifier_ops tracepoint_verifier_ops = {
  625. .get_func_proto = tp_prog_func_proto,
  626. .is_valid_access = tp_prog_is_valid_access,
  627. };
  628. const struct bpf_prog_ops tracepoint_prog_ops = {
  629. };
  630. BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
  631. struct bpf_perf_event_value *, buf, u32, size)
  632. {
  633. int err = -EINVAL;
  634. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  635. goto clear;
  636. err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
  637. &buf->running);
  638. if (unlikely(err))
  639. goto clear;
  640. return 0;
  641. clear:
  642. memset(buf, 0, size);
  643. return err;
  644. }
  645. static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  646. .func = bpf_perf_prog_read_value,
  647. .gpl_only = true,
  648. .ret_type = RET_INTEGER,
  649. .arg1_type = ARG_PTR_TO_CTX,
  650. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  651. .arg3_type = ARG_CONST_SIZE,
  652. };
  653. static const struct bpf_func_proto *
  654. pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  655. {
  656. switch (func_id) {
  657. case BPF_FUNC_perf_event_output:
  658. return &bpf_perf_event_output_proto_tp;
  659. case BPF_FUNC_get_stackid:
  660. return &bpf_get_stackid_proto_tp;
  661. case BPF_FUNC_get_stack:
  662. return &bpf_get_stack_proto_tp;
  663. case BPF_FUNC_perf_prog_read_value:
  664. return &bpf_perf_prog_read_value_proto;
  665. default:
  666. return tracing_func_proto(func_id, prog);
  667. }
  668. }
  669. /*
  670. * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
  671. * to avoid potential recursive reuse issue when/if tracepoints are added
  672. * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
  673. */
  674. static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
  675. BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
  676. struct bpf_map *, map, u64, flags, void *, data, u64, size)
  677. {
  678. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  679. perf_fetch_caller_regs(regs);
  680. return ____bpf_perf_event_output(regs, map, flags, data, size);
  681. }
  682. static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
  683. .func = bpf_perf_event_output_raw_tp,
  684. .gpl_only = true,
  685. .ret_type = RET_INTEGER,
  686. .arg1_type = ARG_PTR_TO_CTX,
  687. .arg2_type = ARG_CONST_MAP_PTR,
  688. .arg3_type = ARG_ANYTHING,
  689. .arg4_type = ARG_PTR_TO_MEM,
  690. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  691. };
  692. BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
  693. struct bpf_map *, map, u64, flags)
  694. {
  695. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  696. perf_fetch_caller_regs(regs);
  697. /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
  698. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  699. flags, 0, 0);
  700. }
  701. static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
  702. .func = bpf_get_stackid_raw_tp,
  703. .gpl_only = true,
  704. .ret_type = RET_INTEGER,
  705. .arg1_type = ARG_PTR_TO_CTX,
  706. .arg2_type = ARG_CONST_MAP_PTR,
  707. .arg3_type = ARG_ANYTHING,
  708. };
  709. BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
  710. void *, buf, u32, size, u64, flags)
  711. {
  712. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  713. perf_fetch_caller_regs(regs);
  714. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  715. (unsigned long) size, flags, 0);
  716. }
  717. static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
  718. .func = bpf_get_stack_raw_tp,
  719. .gpl_only = true,
  720. .ret_type = RET_INTEGER,
  721. .arg1_type = ARG_PTR_TO_CTX,
  722. .arg2_type = ARG_PTR_TO_MEM,
  723. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  724. .arg4_type = ARG_ANYTHING,
  725. };
  726. static const struct bpf_func_proto *
  727. raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  728. {
  729. switch (func_id) {
  730. case BPF_FUNC_perf_event_output:
  731. return &bpf_perf_event_output_proto_raw_tp;
  732. case BPF_FUNC_get_stackid:
  733. return &bpf_get_stackid_proto_raw_tp;
  734. case BPF_FUNC_get_stack:
  735. return &bpf_get_stack_proto_raw_tp;
  736. default:
  737. return tracing_func_proto(func_id, prog);
  738. }
  739. }
  740. static bool raw_tp_prog_is_valid_access(int off, int size,
  741. enum bpf_access_type type,
  742. const struct bpf_prog *prog,
  743. struct bpf_insn_access_aux *info)
  744. {
  745. /* largest tracepoint in the kernel has 12 args */
  746. if (off < 0 || off >= sizeof(__u64) * 12)
  747. return false;
  748. if (type != BPF_READ)
  749. return false;
  750. if (off % size != 0)
  751. return false;
  752. return true;
  753. }
  754. const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
  755. .get_func_proto = raw_tp_prog_func_proto,
  756. .is_valid_access = raw_tp_prog_is_valid_access,
  757. };
  758. const struct bpf_prog_ops raw_tracepoint_prog_ops = {
  759. };
  760. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  761. const struct bpf_prog *prog,
  762. struct bpf_insn_access_aux *info)
  763. {
  764. const int size_u64 = sizeof(u64);
  765. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  766. return false;
  767. if (type != BPF_READ)
  768. return false;
  769. if (off % size != 0)
  770. return false;
  771. switch (off) {
  772. case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
  773. bpf_ctx_record_field_size(info, size_u64);
  774. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  775. return false;
  776. break;
  777. case bpf_ctx_range(struct bpf_perf_event_data, addr):
  778. bpf_ctx_record_field_size(info, size_u64);
  779. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  780. return false;
  781. break;
  782. default:
  783. if (size != sizeof(long))
  784. return false;
  785. }
  786. return true;
  787. }
  788. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  789. const struct bpf_insn *si,
  790. struct bpf_insn *insn_buf,
  791. struct bpf_prog *prog, u32 *target_size)
  792. {
  793. struct bpf_insn *insn = insn_buf;
  794. switch (si->off) {
  795. case offsetof(struct bpf_perf_event_data, sample_period):
  796. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  797. data), si->dst_reg, si->src_reg,
  798. offsetof(struct bpf_perf_event_data_kern, data));
  799. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  800. bpf_target_off(struct perf_sample_data, period, 8,
  801. target_size));
  802. break;
  803. case offsetof(struct bpf_perf_event_data, addr):
  804. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  805. data), si->dst_reg, si->src_reg,
  806. offsetof(struct bpf_perf_event_data_kern, data));
  807. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  808. bpf_target_off(struct perf_sample_data, addr, 8,
  809. target_size));
  810. break;
  811. default:
  812. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  813. regs), si->dst_reg, si->src_reg,
  814. offsetof(struct bpf_perf_event_data_kern, regs));
  815. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  816. si->off);
  817. break;
  818. }
  819. return insn - insn_buf;
  820. }
  821. const struct bpf_verifier_ops perf_event_verifier_ops = {
  822. .get_func_proto = pe_prog_func_proto,
  823. .is_valid_access = pe_prog_is_valid_access,
  824. .convert_ctx_access = pe_prog_convert_ctx_access,
  825. };
  826. const struct bpf_prog_ops perf_event_prog_ops = {
  827. };
  828. static DEFINE_MUTEX(bpf_event_mutex);
  829. #define BPF_TRACE_MAX_PROGS 64
  830. int perf_event_attach_bpf_prog(struct perf_event *event,
  831. struct bpf_prog *prog)
  832. {
  833. struct bpf_prog_array __rcu *old_array;
  834. struct bpf_prog_array *new_array;
  835. int ret = -EEXIST;
  836. /*
  837. * Kprobe override only works if they are on the function entry,
  838. * and only if they are on the opt-in list.
  839. */
  840. if (prog->kprobe_override &&
  841. (!trace_kprobe_on_func_entry(event->tp_event) ||
  842. !trace_kprobe_error_injectable(event->tp_event)))
  843. return -EINVAL;
  844. mutex_lock(&bpf_event_mutex);
  845. if (event->prog)
  846. goto unlock;
  847. old_array = event->tp_event->prog_array;
  848. if (old_array &&
  849. bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
  850. ret = -E2BIG;
  851. goto unlock;
  852. }
  853. ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
  854. if (ret < 0)
  855. goto unlock;
  856. /* set the new array to event->tp_event and set event->prog */
  857. event->prog = prog;
  858. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  859. bpf_prog_array_free(old_array);
  860. unlock:
  861. mutex_unlock(&bpf_event_mutex);
  862. return ret;
  863. }
  864. void perf_event_detach_bpf_prog(struct perf_event *event)
  865. {
  866. struct bpf_prog_array __rcu *old_array;
  867. struct bpf_prog_array *new_array;
  868. int ret;
  869. mutex_lock(&bpf_event_mutex);
  870. if (!event->prog)
  871. goto unlock;
  872. old_array = event->tp_event->prog_array;
  873. ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
  874. if (ret == -ENOENT)
  875. goto unlock;
  876. if (ret < 0) {
  877. bpf_prog_array_delete_safe(old_array, event->prog);
  878. } else {
  879. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  880. bpf_prog_array_free(old_array);
  881. }
  882. bpf_prog_put(event->prog);
  883. event->prog = NULL;
  884. unlock:
  885. mutex_unlock(&bpf_event_mutex);
  886. }
  887. int perf_event_query_prog_array(struct perf_event *event, void __user *info)
  888. {
  889. struct perf_event_query_bpf __user *uquery = info;
  890. struct perf_event_query_bpf query = {};
  891. u32 *ids, prog_cnt, ids_len;
  892. int ret;
  893. if (!capable(CAP_SYS_ADMIN))
  894. return -EPERM;
  895. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  896. return -EINVAL;
  897. if (copy_from_user(&query, uquery, sizeof(query)))
  898. return -EFAULT;
  899. ids_len = query.ids_len;
  900. if (ids_len > BPF_TRACE_MAX_PROGS)
  901. return -E2BIG;
  902. ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
  903. if (!ids)
  904. return -ENOMEM;
  905. /*
  906. * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
  907. * is required when user only wants to check for uquery->prog_cnt.
  908. * There is no need to check for it since the case is handled
  909. * gracefully in bpf_prog_array_copy_info.
  910. */
  911. mutex_lock(&bpf_event_mutex);
  912. ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
  913. ids,
  914. ids_len,
  915. &prog_cnt);
  916. mutex_unlock(&bpf_event_mutex);
  917. if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
  918. copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
  919. ret = -EFAULT;
  920. kfree(ids);
  921. return ret;
  922. }
  923. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  924. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  925. struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
  926. {
  927. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  928. for (; btp < __stop__bpf_raw_tp; btp++) {
  929. if (!strcmp(btp->tp->name, name))
  930. return btp;
  931. }
  932. return NULL;
  933. }
  934. static __always_inline
  935. void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
  936. {
  937. rcu_read_lock();
  938. preempt_disable();
  939. (void) BPF_PROG_RUN(prog, args);
  940. preempt_enable();
  941. rcu_read_unlock();
  942. }
  943. #define UNPACK(...) __VA_ARGS__
  944. #define REPEAT_1(FN, DL, X, ...) FN(X)
  945. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  946. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  947. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  948. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  949. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  950. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  951. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  952. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  953. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  954. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  955. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  956. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  957. #define SARG(X) u64 arg##X
  958. #define COPY(X) args[X] = arg##X
  959. #define __DL_COM (,)
  960. #define __DL_SEM (;)
  961. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  962. #define BPF_TRACE_DEFN_x(x) \
  963. void bpf_trace_run##x(struct bpf_prog *prog, \
  964. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  965. { \
  966. u64 args[x]; \
  967. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  968. __bpf_trace_run(prog, args); \
  969. } \
  970. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  971. BPF_TRACE_DEFN_x(1);
  972. BPF_TRACE_DEFN_x(2);
  973. BPF_TRACE_DEFN_x(3);
  974. BPF_TRACE_DEFN_x(4);
  975. BPF_TRACE_DEFN_x(5);
  976. BPF_TRACE_DEFN_x(6);
  977. BPF_TRACE_DEFN_x(7);
  978. BPF_TRACE_DEFN_x(8);
  979. BPF_TRACE_DEFN_x(9);
  980. BPF_TRACE_DEFN_x(10);
  981. BPF_TRACE_DEFN_x(11);
  982. BPF_TRACE_DEFN_x(12);
  983. static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  984. {
  985. struct tracepoint *tp = btp->tp;
  986. /*
  987. * check that program doesn't access arguments beyond what's
  988. * available in this tracepoint
  989. */
  990. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  991. return -EINVAL;
  992. return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
  993. }
  994. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  995. {
  996. int err;
  997. mutex_lock(&bpf_event_mutex);
  998. err = __bpf_probe_register(btp, prog);
  999. mutex_unlock(&bpf_event_mutex);
  1000. return err;
  1001. }
  1002. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  1003. {
  1004. int err;
  1005. mutex_lock(&bpf_event_mutex);
  1006. err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
  1007. mutex_unlock(&bpf_event_mutex);
  1008. return err;
  1009. }
  1010. int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
  1011. u32 *fd_type, const char **buf,
  1012. u64 *probe_offset, u64 *probe_addr)
  1013. {
  1014. bool is_tracepoint, is_syscall_tp;
  1015. struct bpf_prog *prog;
  1016. int flags, err = 0;
  1017. prog = event->prog;
  1018. if (!prog)
  1019. return -ENOENT;
  1020. /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
  1021. if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
  1022. return -EOPNOTSUPP;
  1023. *prog_id = prog->aux->id;
  1024. flags = event->tp_event->flags;
  1025. is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
  1026. is_syscall_tp = is_syscall_trace_event(event->tp_event);
  1027. if (is_tracepoint || is_syscall_tp) {
  1028. *buf = is_tracepoint ? event->tp_event->tp->name
  1029. : event->tp_event->name;
  1030. *fd_type = BPF_FD_TYPE_TRACEPOINT;
  1031. *probe_offset = 0x0;
  1032. *probe_addr = 0x0;
  1033. } else {
  1034. /* kprobe/uprobe */
  1035. err = -EOPNOTSUPP;
  1036. #ifdef CONFIG_KPROBE_EVENTS
  1037. if (flags & TRACE_EVENT_FL_KPROBE)
  1038. err = bpf_get_kprobe_info(event, fd_type, buf,
  1039. probe_offset, probe_addr,
  1040. event->attr.type == PERF_TYPE_TRACEPOINT);
  1041. #endif
  1042. #ifdef CONFIG_UPROBE_EVENTS
  1043. if (flags & TRACE_EVENT_FL_UPROBE)
  1044. err = bpf_get_uprobe_info(event, fd_type, buf,
  1045. probe_offset,
  1046. event->attr.type == PERF_TYPE_TRACEPOINT);
  1047. #endif
  1048. }
  1049. return err;
  1050. }