bpf_trace.c 34 KB

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