bpf_trace.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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/error-injection.h>
  18. #include "trace_probe.h"
  19. #include "trace.h"
  20. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  21. u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  22. /**
  23. * trace_call_bpf - invoke BPF program
  24. * @call: tracepoint event
  25. * @ctx: opaque context pointer
  26. *
  27. * kprobe handlers execute BPF programs via this helper.
  28. * Can be used from static tracepoints in the future.
  29. *
  30. * Return: BPF programs always return an integer which is interpreted by
  31. * kprobe handler as:
  32. * 0 - return from kprobe (event is filtered out)
  33. * 1 - store kprobe event into ring buffer
  34. * Other values are reserved and currently alias to 1
  35. */
  36. unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
  37. {
  38. unsigned int ret;
  39. if (in_nmi()) /* not supported yet */
  40. return 1;
  41. preempt_disable();
  42. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  43. /*
  44. * since some bpf program is already running on this cpu,
  45. * don't call into another bpf program (same or different)
  46. * and don't send kprobe event into ring-buffer,
  47. * so return zero here
  48. */
  49. ret = 0;
  50. goto out;
  51. }
  52. /*
  53. * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
  54. * to all call sites, we did a bpf_prog_array_valid() there to check
  55. * whether call->prog_array is empty or not, which is
  56. * a heurisitc to speed up execution.
  57. *
  58. * If bpf_prog_array_valid() fetched prog_array was
  59. * non-NULL, we go into trace_call_bpf() and do the actual
  60. * proper rcu_dereference() under RCU lock.
  61. * If it turns out that prog_array is NULL then, we bail out.
  62. * For the opposite, if the bpf_prog_array_valid() fetched pointer
  63. * was NULL, you'll skip the prog_array with the risk of missing
  64. * out of events when it was updated in between this and the
  65. * rcu_dereference() which is accepted risk.
  66. */
  67. ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
  68. out:
  69. __this_cpu_dec(bpf_prog_active);
  70. preempt_enable();
  71. return ret;
  72. }
  73. EXPORT_SYMBOL_GPL(trace_call_bpf);
  74. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  75. BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
  76. {
  77. regs_set_return_value(regs, rc);
  78. override_function_with_return(regs);
  79. return 0;
  80. }
  81. static const struct bpf_func_proto bpf_override_return_proto = {
  82. .func = bpf_override_return,
  83. .gpl_only = true,
  84. .ret_type = RET_INTEGER,
  85. .arg1_type = ARG_PTR_TO_CTX,
  86. .arg2_type = ARG_ANYTHING,
  87. };
  88. #endif
  89. BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
  90. {
  91. int ret;
  92. ret = probe_kernel_read(dst, unsafe_ptr, size);
  93. if (unlikely(ret < 0))
  94. memset(dst, 0, size);
  95. return ret;
  96. }
  97. static const struct bpf_func_proto bpf_probe_read_proto = {
  98. .func = bpf_probe_read,
  99. .gpl_only = true,
  100. .ret_type = RET_INTEGER,
  101. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  102. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  103. .arg3_type = ARG_ANYTHING,
  104. };
  105. BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
  106. u32, size)
  107. {
  108. /*
  109. * Ensure we're in user context which is safe for the helper to
  110. * run. This helper has no business in a kthread.
  111. *
  112. * access_ok() should prevent writing to non-user memory, but in
  113. * some situations (nommu, temporary switch, etc) access_ok() does
  114. * not provide enough validation, hence the check on KERNEL_DS.
  115. */
  116. if (unlikely(in_interrupt() ||
  117. current->flags & (PF_KTHREAD | PF_EXITING)))
  118. return -EPERM;
  119. if (unlikely(uaccess_kernel()))
  120. return -EPERM;
  121. if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
  122. return -EPERM;
  123. return probe_kernel_write(unsafe_ptr, src, size);
  124. }
  125. static const struct bpf_func_proto bpf_probe_write_user_proto = {
  126. .func = bpf_probe_write_user,
  127. .gpl_only = true,
  128. .ret_type = RET_INTEGER,
  129. .arg1_type = ARG_ANYTHING,
  130. .arg2_type = ARG_PTR_TO_MEM,
  131. .arg3_type = ARG_CONST_SIZE,
  132. };
  133. static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
  134. {
  135. pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
  136. current->comm, task_pid_nr(current));
  137. return &bpf_probe_write_user_proto;
  138. }
  139. /*
  140. * Only limited trace_printk() conversion specifiers allowed:
  141. * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
  142. */
  143. BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
  144. u64, arg2, u64, arg3)
  145. {
  146. bool str_seen = false;
  147. int mod[3] = {};
  148. int fmt_cnt = 0;
  149. u64 unsafe_addr;
  150. char buf[64];
  151. int i;
  152. /*
  153. * bpf_check()->check_func_arg()->check_stack_boundary()
  154. * guarantees that fmt points to bpf program stack,
  155. * fmt_size bytes of it were initialized and fmt_size > 0
  156. */
  157. if (fmt[--fmt_size] != 0)
  158. return -EINVAL;
  159. /* check format string for allowed specifiers */
  160. for (i = 0; i < fmt_size; i++) {
  161. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  162. return -EINVAL;
  163. if (fmt[i] != '%')
  164. continue;
  165. if (fmt_cnt >= 3)
  166. return -EINVAL;
  167. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  168. i++;
  169. if (fmt[i] == 'l') {
  170. mod[fmt_cnt]++;
  171. i++;
  172. } else if (fmt[i] == 'p' || fmt[i] == 's') {
  173. mod[fmt_cnt]++;
  174. i++;
  175. if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
  176. return -EINVAL;
  177. fmt_cnt++;
  178. if (fmt[i - 1] == '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. default:
  491. return NULL;
  492. }
  493. }
  494. static const struct bpf_func_proto *
  495. kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  496. {
  497. switch (func_id) {
  498. case BPF_FUNC_perf_event_output:
  499. return &bpf_perf_event_output_proto;
  500. case BPF_FUNC_get_stackid:
  501. return &bpf_get_stackid_proto;
  502. case BPF_FUNC_get_stack:
  503. return &bpf_get_stack_proto;
  504. case BPF_FUNC_perf_event_read_value:
  505. return &bpf_perf_event_read_value_proto;
  506. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  507. case BPF_FUNC_override_return:
  508. return &bpf_override_return_proto;
  509. #endif
  510. default:
  511. return tracing_func_proto(func_id, prog);
  512. }
  513. }
  514. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  515. static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  516. const struct bpf_prog *prog,
  517. struct bpf_insn_access_aux *info)
  518. {
  519. if (off < 0 || off >= sizeof(struct pt_regs))
  520. return false;
  521. if (type != BPF_READ)
  522. return false;
  523. if (off % size != 0)
  524. return false;
  525. /*
  526. * Assertion for 32 bit to make sure last 8 byte access
  527. * (BPF_DW) to the last 4 byte member is disallowed.
  528. */
  529. if (off + size > sizeof(struct pt_regs))
  530. return false;
  531. return true;
  532. }
  533. const struct bpf_verifier_ops kprobe_verifier_ops = {
  534. .get_func_proto = kprobe_prog_func_proto,
  535. .is_valid_access = kprobe_prog_is_valid_access,
  536. };
  537. const struct bpf_prog_ops kprobe_prog_ops = {
  538. };
  539. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  540. u64, flags, void *, data, u64, size)
  541. {
  542. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  543. /*
  544. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  545. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  546. * from there and call the same bpf_perf_event_output() helper inline.
  547. */
  548. return ____bpf_perf_event_output(regs, map, flags, data, size);
  549. }
  550. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  551. .func = bpf_perf_event_output_tp,
  552. .gpl_only = true,
  553. .ret_type = RET_INTEGER,
  554. .arg1_type = ARG_PTR_TO_CTX,
  555. .arg2_type = ARG_CONST_MAP_PTR,
  556. .arg3_type = ARG_ANYTHING,
  557. .arg4_type = ARG_PTR_TO_MEM,
  558. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  559. };
  560. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  561. u64, flags)
  562. {
  563. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  564. /*
  565. * Same comment as in bpf_perf_event_output_tp(), only that this time
  566. * the other helper's function body cannot be inlined due to being
  567. * external, thus we need to call raw helper function.
  568. */
  569. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  570. flags, 0, 0);
  571. }
  572. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  573. .func = bpf_get_stackid_tp,
  574. .gpl_only = true,
  575. .ret_type = RET_INTEGER,
  576. .arg1_type = ARG_PTR_TO_CTX,
  577. .arg2_type = ARG_CONST_MAP_PTR,
  578. .arg3_type = ARG_ANYTHING,
  579. };
  580. BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
  581. u64, flags)
  582. {
  583. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  584. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  585. (unsigned long) size, flags, 0);
  586. }
  587. static const struct bpf_func_proto bpf_get_stack_proto_tp = {
  588. .func = bpf_get_stack_tp,
  589. .gpl_only = true,
  590. .ret_type = RET_INTEGER,
  591. .arg1_type = ARG_PTR_TO_CTX,
  592. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  593. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  594. .arg4_type = ARG_ANYTHING,
  595. };
  596. static const struct bpf_func_proto *
  597. tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  598. {
  599. switch (func_id) {
  600. case BPF_FUNC_perf_event_output:
  601. return &bpf_perf_event_output_proto_tp;
  602. case BPF_FUNC_get_stackid:
  603. return &bpf_get_stackid_proto_tp;
  604. case BPF_FUNC_get_stack:
  605. return &bpf_get_stack_proto_tp;
  606. default:
  607. return tracing_func_proto(func_id, prog);
  608. }
  609. }
  610. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  611. const struct bpf_prog *prog,
  612. struct bpf_insn_access_aux *info)
  613. {
  614. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  615. return false;
  616. if (type != BPF_READ)
  617. return false;
  618. if (off % size != 0)
  619. return false;
  620. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  621. return true;
  622. }
  623. const struct bpf_verifier_ops tracepoint_verifier_ops = {
  624. .get_func_proto = tp_prog_func_proto,
  625. .is_valid_access = tp_prog_is_valid_access,
  626. };
  627. const struct bpf_prog_ops tracepoint_prog_ops = {
  628. };
  629. BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
  630. struct bpf_perf_event_value *, buf, u32, size)
  631. {
  632. int err = -EINVAL;
  633. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  634. goto clear;
  635. err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
  636. &buf->running);
  637. if (unlikely(err))
  638. goto clear;
  639. return 0;
  640. clear:
  641. memset(buf, 0, size);
  642. return err;
  643. }
  644. static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  645. .func = bpf_perf_prog_read_value,
  646. .gpl_only = true,
  647. .ret_type = RET_INTEGER,
  648. .arg1_type = ARG_PTR_TO_CTX,
  649. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  650. .arg3_type = ARG_CONST_SIZE,
  651. };
  652. static const struct bpf_func_proto *
  653. pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  654. {
  655. switch (func_id) {
  656. case BPF_FUNC_perf_event_output:
  657. return &bpf_perf_event_output_proto_tp;
  658. case BPF_FUNC_get_stackid:
  659. return &bpf_get_stackid_proto_tp;
  660. case BPF_FUNC_get_stack:
  661. return &bpf_get_stack_proto_tp;
  662. case BPF_FUNC_perf_prog_read_value:
  663. return &bpf_perf_prog_read_value_proto;
  664. default:
  665. return tracing_func_proto(func_id, prog);
  666. }
  667. }
  668. /*
  669. * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
  670. * to avoid potential recursive reuse issue when/if tracepoints are added
  671. * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
  672. */
  673. static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
  674. BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
  675. struct bpf_map *, map, u64, flags, void *, data, u64, size)
  676. {
  677. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  678. perf_fetch_caller_regs(regs);
  679. return ____bpf_perf_event_output(regs, map, flags, data, size);
  680. }
  681. static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
  682. .func = bpf_perf_event_output_raw_tp,
  683. .gpl_only = true,
  684. .ret_type = RET_INTEGER,
  685. .arg1_type = ARG_PTR_TO_CTX,
  686. .arg2_type = ARG_CONST_MAP_PTR,
  687. .arg3_type = ARG_ANYTHING,
  688. .arg4_type = ARG_PTR_TO_MEM,
  689. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  690. };
  691. BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
  692. struct bpf_map *, map, u64, flags)
  693. {
  694. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  695. perf_fetch_caller_regs(regs);
  696. /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
  697. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  698. flags, 0, 0);
  699. }
  700. static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
  701. .func = bpf_get_stackid_raw_tp,
  702. .gpl_only = true,
  703. .ret_type = RET_INTEGER,
  704. .arg1_type = ARG_PTR_TO_CTX,
  705. .arg2_type = ARG_CONST_MAP_PTR,
  706. .arg3_type = ARG_ANYTHING,
  707. };
  708. BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
  709. void *, buf, u32, size, u64, flags)
  710. {
  711. struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
  712. perf_fetch_caller_regs(regs);
  713. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  714. (unsigned long) size, flags, 0);
  715. }
  716. static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
  717. .func = bpf_get_stack_raw_tp,
  718. .gpl_only = true,
  719. .ret_type = RET_INTEGER,
  720. .arg1_type = ARG_PTR_TO_CTX,
  721. .arg2_type = ARG_PTR_TO_MEM,
  722. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  723. .arg4_type = ARG_ANYTHING,
  724. };
  725. static const struct bpf_func_proto *
  726. raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  727. {
  728. switch (func_id) {
  729. case BPF_FUNC_perf_event_output:
  730. return &bpf_perf_event_output_proto_raw_tp;
  731. case BPF_FUNC_get_stackid:
  732. return &bpf_get_stackid_proto_raw_tp;
  733. case BPF_FUNC_get_stack:
  734. return &bpf_get_stack_proto_raw_tp;
  735. default:
  736. return tracing_func_proto(func_id, prog);
  737. }
  738. }
  739. static bool raw_tp_prog_is_valid_access(int off, int size,
  740. enum bpf_access_type type,
  741. const struct bpf_prog *prog,
  742. struct bpf_insn_access_aux *info)
  743. {
  744. /* largest tracepoint in the kernel has 12 args */
  745. if (off < 0 || off >= sizeof(__u64) * 12)
  746. return false;
  747. if (type != BPF_READ)
  748. return false;
  749. if (off % size != 0)
  750. return false;
  751. return true;
  752. }
  753. const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
  754. .get_func_proto = raw_tp_prog_func_proto,
  755. .is_valid_access = raw_tp_prog_is_valid_access,
  756. };
  757. const struct bpf_prog_ops raw_tracepoint_prog_ops = {
  758. };
  759. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  760. const struct bpf_prog *prog,
  761. struct bpf_insn_access_aux *info)
  762. {
  763. const int size_u64 = sizeof(u64);
  764. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  765. return false;
  766. if (type != BPF_READ)
  767. return false;
  768. if (off % size != 0)
  769. return false;
  770. switch (off) {
  771. case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
  772. bpf_ctx_record_field_size(info, size_u64);
  773. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  774. return false;
  775. break;
  776. case bpf_ctx_range(struct bpf_perf_event_data, addr):
  777. bpf_ctx_record_field_size(info, size_u64);
  778. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  779. return false;
  780. break;
  781. default:
  782. if (size != sizeof(long))
  783. return false;
  784. }
  785. return true;
  786. }
  787. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  788. const struct bpf_insn *si,
  789. struct bpf_insn *insn_buf,
  790. struct bpf_prog *prog, u32 *target_size)
  791. {
  792. struct bpf_insn *insn = insn_buf;
  793. switch (si->off) {
  794. case offsetof(struct bpf_perf_event_data, sample_period):
  795. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  796. data), si->dst_reg, si->src_reg,
  797. offsetof(struct bpf_perf_event_data_kern, data));
  798. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  799. bpf_target_off(struct perf_sample_data, period, 8,
  800. target_size));
  801. break;
  802. case offsetof(struct bpf_perf_event_data, addr):
  803. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  804. data), si->dst_reg, si->src_reg,
  805. offsetof(struct bpf_perf_event_data_kern, data));
  806. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  807. bpf_target_off(struct perf_sample_data, addr, 8,
  808. target_size));
  809. break;
  810. default:
  811. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  812. regs), si->dst_reg, si->src_reg,
  813. offsetof(struct bpf_perf_event_data_kern, regs));
  814. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  815. si->off);
  816. break;
  817. }
  818. return insn - insn_buf;
  819. }
  820. const struct bpf_verifier_ops perf_event_verifier_ops = {
  821. .get_func_proto = pe_prog_func_proto,
  822. .is_valid_access = pe_prog_is_valid_access,
  823. .convert_ctx_access = pe_prog_convert_ctx_access,
  824. };
  825. const struct bpf_prog_ops perf_event_prog_ops = {
  826. };
  827. static DEFINE_MUTEX(bpf_event_mutex);
  828. #define BPF_TRACE_MAX_PROGS 64
  829. int perf_event_attach_bpf_prog(struct perf_event *event,
  830. struct bpf_prog *prog)
  831. {
  832. struct bpf_prog_array __rcu *old_array;
  833. struct bpf_prog_array *new_array;
  834. int ret = -EEXIST;
  835. /*
  836. * Kprobe override only works if they are on the function entry,
  837. * and only if they are on the opt-in list.
  838. */
  839. if (prog->kprobe_override &&
  840. (!trace_kprobe_on_func_entry(event->tp_event) ||
  841. !trace_kprobe_error_injectable(event->tp_event)))
  842. return -EINVAL;
  843. mutex_lock(&bpf_event_mutex);
  844. if (event->prog)
  845. goto unlock;
  846. old_array = event->tp_event->prog_array;
  847. if (old_array &&
  848. bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
  849. ret = -E2BIG;
  850. goto unlock;
  851. }
  852. ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
  853. if (ret < 0)
  854. goto unlock;
  855. /* set the new array to event->tp_event and set event->prog */
  856. event->prog = prog;
  857. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  858. bpf_prog_array_free(old_array);
  859. unlock:
  860. mutex_unlock(&bpf_event_mutex);
  861. return ret;
  862. }
  863. void perf_event_detach_bpf_prog(struct perf_event *event)
  864. {
  865. struct bpf_prog_array __rcu *old_array;
  866. struct bpf_prog_array *new_array;
  867. int ret;
  868. mutex_lock(&bpf_event_mutex);
  869. if (!event->prog)
  870. goto unlock;
  871. old_array = event->tp_event->prog_array;
  872. ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
  873. if (ret < 0) {
  874. bpf_prog_array_delete_safe(old_array, event->prog);
  875. } else {
  876. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  877. bpf_prog_array_free(old_array);
  878. }
  879. bpf_prog_put(event->prog);
  880. event->prog = NULL;
  881. unlock:
  882. mutex_unlock(&bpf_event_mutex);
  883. }
  884. int perf_event_query_prog_array(struct perf_event *event, void __user *info)
  885. {
  886. struct perf_event_query_bpf __user *uquery = info;
  887. struct perf_event_query_bpf query = {};
  888. u32 *ids, prog_cnt, ids_len;
  889. int ret;
  890. if (!capable(CAP_SYS_ADMIN))
  891. return -EPERM;
  892. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  893. return -EINVAL;
  894. if (copy_from_user(&query, uquery, sizeof(query)))
  895. return -EFAULT;
  896. ids_len = query.ids_len;
  897. if (ids_len > BPF_TRACE_MAX_PROGS)
  898. return -E2BIG;
  899. ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
  900. if (!ids)
  901. return -ENOMEM;
  902. /*
  903. * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
  904. * is required when user only wants to check for uquery->prog_cnt.
  905. * There is no need to check for it since the case is handled
  906. * gracefully in bpf_prog_array_copy_info.
  907. */
  908. mutex_lock(&bpf_event_mutex);
  909. ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
  910. ids,
  911. ids_len,
  912. &prog_cnt);
  913. mutex_unlock(&bpf_event_mutex);
  914. if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
  915. copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
  916. ret = -EFAULT;
  917. kfree(ids);
  918. return ret;
  919. }
  920. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  921. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  922. struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
  923. {
  924. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  925. for (; btp < __stop__bpf_raw_tp; btp++) {
  926. if (!strcmp(btp->tp->name, name))
  927. return btp;
  928. }
  929. return NULL;
  930. }
  931. static __always_inline
  932. void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
  933. {
  934. rcu_read_lock();
  935. preempt_disable();
  936. (void) BPF_PROG_RUN(prog, args);
  937. preempt_enable();
  938. rcu_read_unlock();
  939. }
  940. #define UNPACK(...) __VA_ARGS__
  941. #define REPEAT_1(FN, DL, X, ...) FN(X)
  942. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  943. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  944. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  945. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  946. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  947. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  948. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  949. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  950. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  951. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  952. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  953. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  954. #define SARG(X) u64 arg##X
  955. #define COPY(X) args[X] = arg##X
  956. #define __DL_COM (,)
  957. #define __DL_SEM (;)
  958. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  959. #define BPF_TRACE_DEFN_x(x) \
  960. void bpf_trace_run##x(struct bpf_prog *prog, \
  961. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  962. { \
  963. u64 args[x]; \
  964. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  965. __bpf_trace_run(prog, args); \
  966. } \
  967. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  968. BPF_TRACE_DEFN_x(1);
  969. BPF_TRACE_DEFN_x(2);
  970. BPF_TRACE_DEFN_x(3);
  971. BPF_TRACE_DEFN_x(4);
  972. BPF_TRACE_DEFN_x(5);
  973. BPF_TRACE_DEFN_x(6);
  974. BPF_TRACE_DEFN_x(7);
  975. BPF_TRACE_DEFN_x(8);
  976. BPF_TRACE_DEFN_x(9);
  977. BPF_TRACE_DEFN_x(10);
  978. BPF_TRACE_DEFN_x(11);
  979. BPF_TRACE_DEFN_x(12);
  980. static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  981. {
  982. struct tracepoint *tp = btp->tp;
  983. /*
  984. * check that program doesn't access arguments beyond what's
  985. * available in this tracepoint
  986. */
  987. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  988. return -EINVAL;
  989. return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
  990. }
  991. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  992. {
  993. int err;
  994. mutex_lock(&bpf_event_mutex);
  995. err = __bpf_probe_register(btp, prog);
  996. mutex_unlock(&bpf_event_mutex);
  997. return err;
  998. }
  999. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  1000. {
  1001. int err;
  1002. mutex_lock(&bpf_event_mutex);
  1003. err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
  1004. mutex_unlock(&bpf_event_mutex);
  1005. return err;
  1006. }