bpf_trace.c 33 KB

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