PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/trace/bpf_trace.c

http://github.com/mirrors/linux-2.6
C | 1717 lines | 1336 code | 240 blank | 141 comment | 213 complexity | 667001aab35cdae7e1b45ddb0aa9d593 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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 <asm/tlb.h>
  17. #include "trace_probe.h"
  18. #include "trace.h"
  19. #define bpf_event_rcu_dereference(p) \
  20. rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
  21. #ifdef CONFIG_MODULES
  22. struct bpf_trace_module {
  23. struct module *module;
  24. struct list_head list;
  25. };
  26. static LIST_HEAD(bpf_trace_modules);
  27. static DEFINE_MUTEX(bpf_module_mutex);
  28. static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
  29. {
  30. struct bpf_raw_event_map *btp, *ret = NULL;
  31. struct bpf_trace_module *btm;
  32. unsigned int i;
  33. mutex_lock(&bpf_module_mutex);
  34. list_for_each_entry(btm, &bpf_trace_modules, list) {
  35. for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
  36. btp = &btm->module->bpf_raw_events[i];
  37. if (!strcmp(btp->tp->name, name)) {
  38. if (try_module_get(btm->module))
  39. ret = btp;
  40. goto out;
  41. }
  42. }
  43. }
  44. out:
  45. mutex_unlock(&bpf_module_mutex);
  46. return ret;
  47. }
  48. #else
  49. static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
  50. {
  51. return NULL;
  52. }
  53. #endif /* CONFIG_MODULES */
  54. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  55. u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  56. /**
  57. * trace_call_bpf - invoke BPF program
  58. * @call: tracepoint event
  59. * @ctx: opaque context pointer
  60. *
  61. * kprobe handlers execute BPF programs via this helper.
  62. * Can be used from static tracepoints in the future.
  63. *
  64. * Return: BPF programs always return an integer which is interpreted by
  65. * kprobe handler as:
  66. * 0 - return from kprobe (event is filtered out)
  67. * 1 - store kprobe event into ring buffer
  68. * Other values are reserved and currently alias to 1
  69. */
  70. unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
  71. {
  72. unsigned int ret;
  73. if (in_nmi()) /* not supported yet */
  74. return 1;
  75. cant_sleep();
  76. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
  77. /*
  78. * since some bpf program is already running on this cpu,
  79. * don't call into another bpf program (same or different)
  80. * and don't send kprobe event into ring-buffer,
  81. * so return zero here
  82. */
  83. ret = 0;
  84. goto out;
  85. }
  86. /*
  87. * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
  88. * to all call sites, we did a bpf_prog_array_valid() there to check
  89. * whether call->prog_array is empty or not, which is
  90. * a heurisitc to speed up execution.
  91. *
  92. * If bpf_prog_array_valid() fetched prog_array was
  93. * non-NULL, we go into trace_call_bpf() and do the actual
  94. * proper rcu_dereference() under RCU lock.
  95. * If it turns out that prog_array is NULL then, we bail out.
  96. * For the opposite, if the bpf_prog_array_valid() fetched pointer
  97. * was NULL, you'll skip the prog_array with the risk of missing
  98. * out of events when it was updated in between this and the
  99. * rcu_dereference() which is accepted risk.
  100. */
  101. ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
  102. out:
  103. __this_cpu_dec(bpf_prog_active);
  104. return ret;
  105. }
  106. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  107. BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
  108. {
  109. regs_set_return_value(regs, rc);
  110. override_function_with_return(regs);
  111. return 0;
  112. }
  113. static const struct bpf_func_proto bpf_override_return_proto = {
  114. .func = bpf_override_return,
  115. .gpl_only = true,
  116. .ret_type = RET_INTEGER,
  117. .arg1_type = ARG_PTR_TO_CTX,
  118. .arg2_type = ARG_ANYTHING,
  119. };
  120. #endif
  121. BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
  122. const void __user *, unsafe_ptr)
  123. {
  124. int ret = probe_user_read(dst, unsafe_ptr, size);
  125. if (unlikely(ret < 0))
  126. memset(dst, 0, size);
  127. return ret;
  128. }
  129. static const struct bpf_func_proto bpf_probe_read_user_proto = {
  130. .func = bpf_probe_read_user,
  131. .gpl_only = true,
  132. .ret_type = RET_INTEGER,
  133. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  134. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  135. .arg3_type = ARG_ANYTHING,
  136. };
  137. BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
  138. const void __user *, unsafe_ptr)
  139. {
  140. int ret = strncpy_from_unsafe_user(dst, unsafe_ptr, size);
  141. if (unlikely(ret < 0))
  142. memset(dst, 0, size);
  143. return ret;
  144. }
  145. static const struct bpf_func_proto bpf_probe_read_user_str_proto = {
  146. .func = bpf_probe_read_user_str,
  147. .gpl_only = true,
  148. .ret_type = RET_INTEGER,
  149. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  150. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  151. .arg3_type = ARG_ANYTHING,
  152. };
  153. static __always_inline int
  154. bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr,
  155. const bool compat)
  156. {
  157. int ret = security_locked_down(LOCKDOWN_BPF_READ);
  158. if (unlikely(ret < 0))
  159. goto out;
  160. ret = compat ? probe_kernel_read(dst, unsafe_ptr, size) :
  161. probe_kernel_read_strict(dst, unsafe_ptr, size);
  162. if (unlikely(ret < 0))
  163. out:
  164. memset(dst, 0, size);
  165. return ret;
  166. }
  167. BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
  168. const void *, unsafe_ptr)
  169. {
  170. return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false);
  171. }
  172. static const struct bpf_func_proto bpf_probe_read_kernel_proto = {
  173. .func = bpf_probe_read_kernel,
  174. .gpl_only = true,
  175. .ret_type = RET_INTEGER,
  176. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  177. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  178. .arg3_type = ARG_ANYTHING,
  179. };
  180. BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
  181. const void *, unsafe_ptr)
  182. {
  183. return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, true);
  184. }
  185. static const struct bpf_func_proto bpf_probe_read_compat_proto = {
  186. .func = bpf_probe_read_compat,
  187. .gpl_only = true,
  188. .ret_type = RET_INTEGER,
  189. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  190. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  191. .arg3_type = ARG_ANYTHING,
  192. };
  193. static __always_inline int
  194. bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,
  195. const bool compat)
  196. {
  197. int ret = security_locked_down(LOCKDOWN_BPF_READ);
  198. if (unlikely(ret < 0))
  199. goto out;
  200. /*
  201. * The strncpy_from_unsafe_*() call will likely not fill the entire
  202. * buffer, but that's okay in this circumstance as we're probing
  203. * arbitrary memory anyway similar to bpf_probe_read_*() and might
  204. * as well probe the stack. Thus, memory is explicitly cleared
  205. * only in error case, so that improper users ignoring return
  206. * code altogether don't copy garbage; otherwise length of string
  207. * is returned that can be used for bpf_perf_event_output() et al.
  208. */
  209. ret = compat ? strncpy_from_unsafe(dst, unsafe_ptr, size) :
  210. strncpy_from_unsafe_strict(dst, unsafe_ptr, size);
  211. if (unlikely(ret < 0))
  212. out:
  213. memset(dst, 0, size);
  214. return ret;
  215. }
  216. BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
  217. const void *, unsafe_ptr)
  218. {
  219. return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false);
  220. }
  221. static const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
  222. .func = bpf_probe_read_kernel_str,
  223. .gpl_only = true,
  224. .ret_type = RET_INTEGER,
  225. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  226. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  227. .arg3_type = ARG_ANYTHING,
  228. };
  229. BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
  230. const void *, unsafe_ptr)
  231. {
  232. return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, true);
  233. }
  234. static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
  235. .func = bpf_probe_read_compat_str,
  236. .gpl_only = true,
  237. .ret_type = RET_INTEGER,
  238. .arg1_type = ARG_PTR_TO_UNINIT_MEM,
  239. .arg2_type = ARG_CONST_SIZE_OR_ZERO,
  240. .arg3_type = ARG_ANYTHING,
  241. };
  242. BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
  243. u32, size)
  244. {
  245. /*
  246. * Ensure we're in user context which is safe for the helper to
  247. * run. This helper has no business in a kthread.
  248. *
  249. * access_ok() should prevent writing to non-user memory, but in
  250. * some situations (nommu, temporary switch, etc) access_ok() does
  251. * not provide enough validation, hence the check on KERNEL_DS.
  252. *
  253. * nmi_uaccess_okay() ensures the probe is not run in an interim
  254. * state, when the task or mm are switched. This is specifically
  255. * required to prevent the use of temporary mm.
  256. */
  257. if (unlikely(in_interrupt() ||
  258. current->flags & (PF_KTHREAD | PF_EXITING)))
  259. return -EPERM;
  260. if (unlikely(uaccess_kernel()))
  261. return -EPERM;
  262. if (unlikely(!nmi_uaccess_okay()))
  263. return -EPERM;
  264. return probe_user_write(unsafe_ptr, src, size);
  265. }
  266. static const struct bpf_func_proto bpf_probe_write_user_proto = {
  267. .func = bpf_probe_write_user,
  268. .gpl_only = true,
  269. .ret_type = RET_INTEGER,
  270. .arg1_type = ARG_ANYTHING,
  271. .arg2_type = ARG_PTR_TO_MEM,
  272. .arg3_type = ARG_CONST_SIZE,
  273. };
  274. static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
  275. {
  276. pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
  277. current->comm, task_pid_nr(current));
  278. return &bpf_probe_write_user_proto;
  279. }
  280. /*
  281. * Only limited trace_printk() conversion specifiers allowed:
  282. * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
  283. */
  284. BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
  285. u64, arg2, u64, arg3)
  286. {
  287. bool str_seen = false;
  288. int mod[3] = {};
  289. int fmt_cnt = 0;
  290. u64 unsafe_addr;
  291. char buf[64];
  292. int i;
  293. /*
  294. * bpf_check()->check_func_arg()->check_stack_boundary()
  295. * guarantees that fmt points to bpf program stack,
  296. * fmt_size bytes of it were initialized and fmt_size > 0
  297. */
  298. if (fmt[--fmt_size] != 0)
  299. return -EINVAL;
  300. /* check format string for allowed specifiers */
  301. for (i = 0; i < fmt_size; i++) {
  302. if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
  303. return -EINVAL;
  304. if (fmt[i] != '%')
  305. continue;
  306. if (fmt_cnt >= 3)
  307. return -EINVAL;
  308. /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
  309. i++;
  310. if (fmt[i] == 'l') {
  311. mod[fmt_cnt]++;
  312. i++;
  313. } else if (fmt[i] == 'p' || fmt[i] == 's') {
  314. mod[fmt_cnt]++;
  315. /* disallow any further format extensions */
  316. if (fmt[i + 1] != 0 &&
  317. !isspace(fmt[i + 1]) &&
  318. !ispunct(fmt[i + 1]))
  319. return -EINVAL;
  320. fmt_cnt++;
  321. if (fmt[i] == 's') {
  322. if (str_seen)
  323. /* allow only one '%s' per fmt string */
  324. return -EINVAL;
  325. str_seen = true;
  326. switch (fmt_cnt) {
  327. case 1:
  328. unsafe_addr = arg1;
  329. arg1 = (long) buf;
  330. break;
  331. case 2:
  332. unsafe_addr = arg2;
  333. arg2 = (long) buf;
  334. break;
  335. case 3:
  336. unsafe_addr = arg3;
  337. arg3 = (long) buf;
  338. break;
  339. }
  340. buf[0] = 0;
  341. strncpy_from_unsafe(buf,
  342. (void *) (long) unsafe_addr,
  343. sizeof(buf));
  344. }
  345. continue;
  346. }
  347. if (fmt[i] == 'l') {
  348. mod[fmt_cnt]++;
  349. i++;
  350. }
  351. if (fmt[i] != 'i' && fmt[i] != 'd' &&
  352. fmt[i] != 'u' && fmt[i] != 'x')
  353. return -EINVAL;
  354. fmt_cnt++;
  355. }
  356. /* Horrid workaround for getting va_list handling working with different
  357. * argument type combinations generically for 32 and 64 bit archs.
  358. */
  359. #define __BPF_TP_EMIT() __BPF_ARG3_TP()
  360. #define __BPF_TP(...) \
  361. __trace_printk(0 /* Fake ip */, \
  362. fmt, ##__VA_ARGS__)
  363. #define __BPF_ARG1_TP(...) \
  364. ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
  365. ? __BPF_TP(arg1, ##__VA_ARGS__) \
  366. : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
  367. ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
  368. : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
  369. #define __BPF_ARG2_TP(...) \
  370. ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
  371. ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
  372. : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
  373. ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
  374. : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
  375. #define __BPF_ARG3_TP(...) \
  376. ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
  377. ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
  378. : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
  379. ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
  380. : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
  381. return __BPF_TP_EMIT();
  382. }
  383. static const struct bpf_func_proto bpf_trace_printk_proto = {
  384. .func = bpf_trace_printk,
  385. .gpl_only = true,
  386. .ret_type = RET_INTEGER,
  387. .arg1_type = ARG_PTR_TO_MEM,
  388. .arg2_type = ARG_CONST_SIZE,
  389. };
  390. const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
  391. {
  392. /*
  393. * this program might be calling bpf_trace_printk,
  394. * so allocate per-cpu printk buffers
  395. */
  396. trace_printk_init_buffers();
  397. return &bpf_trace_printk_proto;
  398. }
  399. static __always_inline int
  400. get_map_perf_counter(struct bpf_map *map, u64 flags,
  401. u64 *value, u64 *enabled, u64 *running)
  402. {
  403. struct bpf_array *array = container_of(map, struct bpf_array, map);
  404. unsigned int cpu = smp_processor_id();
  405. u64 index = flags & BPF_F_INDEX_MASK;
  406. struct bpf_event_entry *ee;
  407. if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
  408. return -EINVAL;
  409. if (index == BPF_F_CURRENT_CPU)
  410. index = cpu;
  411. if (unlikely(index >= array->map.max_entries))
  412. return -E2BIG;
  413. ee = READ_ONCE(array->ptrs[index]);
  414. if (!ee)
  415. return -ENOENT;
  416. return perf_event_read_local(ee->event, value, enabled, running);
  417. }
  418. BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
  419. {
  420. u64 value = 0;
  421. int err;
  422. err = get_map_perf_counter(map, flags, &value, NULL, NULL);
  423. /*
  424. * this api is ugly since we miss [-22..-2] range of valid
  425. * counter values, but that's uapi
  426. */
  427. if (err)
  428. return err;
  429. return value;
  430. }
  431. static const struct bpf_func_proto bpf_perf_event_read_proto = {
  432. .func = bpf_perf_event_read,
  433. .gpl_only = true,
  434. .ret_type = RET_INTEGER,
  435. .arg1_type = ARG_CONST_MAP_PTR,
  436. .arg2_type = ARG_ANYTHING,
  437. };
  438. BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
  439. struct bpf_perf_event_value *, buf, u32, size)
  440. {
  441. int err = -EINVAL;
  442. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  443. goto clear;
  444. err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
  445. &buf->running);
  446. if (unlikely(err))
  447. goto clear;
  448. return 0;
  449. clear:
  450. memset(buf, 0, size);
  451. return err;
  452. }
  453. static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
  454. .func = bpf_perf_event_read_value,
  455. .gpl_only = true,
  456. .ret_type = RET_INTEGER,
  457. .arg1_type = ARG_CONST_MAP_PTR,
  458. .arg2_type = ARG_ANYTHING,
  459. .arg3_type = ARG_PTR_TO_UNINIT_MEM,
  460. .arg4_type = ARG_CONST_SIZE,
  461. };
  462. static __always_inline u64
  463. __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
  464. u64 flags, struct perf_sample_data *sd)
  465. {
  466. struct bpf_array *array = container_of(map, struct bpf_array, map);
  467. unsigned int cpu = smp_processor_id();
  468. u64 index = flags & BPF_F_INDEX_MASK;
  469. struct bpf_event_entry *ee;
  470. struct perf_event *event;
  471. if (index == BPF_F_CURRENT_CPU)
  472. index = cpu;
  473. if (unlikely(index >= array->map.max_entries))
  474. return -E2BIG;
  475. ee = READ_ONCE(array->ptrs[index]);
  476. if (!ee)
  477. return -ENOENT;
  478. event = ee->event;
  479. if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
  480. event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
  481. return -EINVAL;
  482. if (unlikely(event->oncpu != cpu))
  483. return -EOPNOTSUPP;
  484. return perf_event_output(event, sd, regs);
  485. }
  486. /*
  487. * Support executing tracepoints in normal, irq, and nmi context that each call
  488. * bpf_perf_event_output
  489. */
  490. struct bpf_trace_sample_data {
  491. struct perf_sample_data sds[3];
  492. };
  493. static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
  494. static DEFINE_PER_CPU(int, bpf_trace_nest_level);
  495. BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
  496. u64, flags, void *, data, u64, size)
  497. {
  498. struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
  499. int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
  500. struct perf_raw_record raw = {
  501. .frag = {
  502. .size = size,
  503. .data = data,
  504. },
  505. };
  506. struct perf_sample_data *sd;
  507. int err;
  508. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
  509. err = -EBUSY;
  510. goto out;
  511. }
  512. sd = &sds->sds[nest_level - 1];
  513. if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
  514. err = -EINVAL;
  515. goto out;
  516. }
  517. perf_sample_data_init(sd, 0, 0);
  518. sd->raw = &raw;
  519. err = __bpf_perf_event_output(regs, map, flags, sd);
  520. out:
  521. this_cpu_dec(bpf_trace_nest_level);
  522. return err;
  523. }
  524. static const struct bpf_func_proto bpf_perf_event_output_proto = {
  525. .func = bpf_perf_event_output,
  526. .gpl_only = true,
  527. .ret_type = RET_INTEGER,
  528. .arg1_type = ARG_PTR_TO_CTX,
  529. .arg2_type = ARG_CONST_MAP_PTR,
  530. .arg3_type = ARG_ANYTHING,
  531. .arg4_type = ARG_PTR_TO_MEM,
  532. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  533. };
  534. static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
  535. struct bpf_nested_pt_regs {
  536. struct pt_regs regs[3];
  537. };
  538. static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
  539. static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
  540. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  541. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  542. {
  543. int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
  544. struct perf_raw_frag frag = {
  545. .copy = ctx_copy,
  546. .size = ctx_size,
  547. .data = ctx,
  548. };
  549. struct perf_raw_record raw = {
  550. .frag = {
  551. {
  552. .next = ctx_size ? &frag : NULL,
  553. },
  554. .size = meta_size,
  555. .data = meta,
  556. },
  557. };
  558. struct perf_sample_data *sd;
  559. struct pt_regs *regs;
  560. u64 ret;
  561. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
  562. ret = -EBUSY;
  563. goto out;
  564. }
  565. sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
  566. regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
  567. perf_fetch_caller_regs(regs);
  568. perf_sample_data_init(sd, 0, 0);
  569. sd->raw = &raw;
  570. ret = __bpf_perf_event_output(regs, map, flags, sd);
  571. out:
  572. this_cpu_dec(bpf_event_output_nest_level);
  573. return ret;
  574. }
  575. BPF_CALL_0(bpf_get_current_task)
  576. {
  577. return (long) current;
  578. }
  579. static const struct bpf_func_proto bpf_get_current_task_proto = {
  580. .func = bpf_get_current_task,
  581. .gpl_only = true,
  582. .ret_type = RET_INTEGER,
  583. };
  584. BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
  585. {
  586. struct bpf_array *array = container_of(map, struct bpf_array, map);
  587. struct cgroup *cgrp;
  588. if (unlikely(idx >= array->map.max_entries))
  589. return -E2BIG;
  590. cgrp = READ_ONCE(array->ptrs[idx]);
  591. if (unlikely(!cgrp))
  592. return -EAGAIN;
  593. return task_under_cgroup_hierarchy(current, cgrp);
  594. }
  595. static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
  596. .func = bpf_current_task_under_cgroup,
  597. .gpl_only = false,
  598. .ret_type = RET_INTEGER,
  599. .arg1_type = ARG_CONST_MAP_PTR,
  600. .arg2_type = ARG_ANYTHING,
  601. };
  602. struct send_signal_irq_work {
  603. struct irq_work irq_work;
  604. struct task_struct *task;
  605. u32 sig;
  606. enum pid_type type;
  607. };
  608. static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
  609. static void do_bpf_send_signal(struct irq_work *entry)
  610. {
  611. struct send_signal_irq_work *work;
  612. work = container_of(entry, struct send_signal_irq_work, irq_work);
  613. group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
  614. }
  615. static int bpf_send_signal_common(u32 sig, enum pid_type type)
  616. {
  617. struct send_signal_irq_work *work = NULL;
  618. /* Similar to bpf_probe_write_user, task needs to be
  619. * in a sound condition and kernel memory access be
  620. * permitted in order to send signal to the current
  621. * task.
  622. */
  623. if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
  624. return -EPERM;
  625. if (unlikely(uaccess_kernel()))
  626. return -EPERM;
  627. if (unlikely(!nmi_uaccess_okay()))
  628. return -EPERM;
  629. if (irqs_disabled()) {
  630. /* Do an early check on signal validity. Otherwise,
  631. * the error is lost in deferred irq_work.
  632. */
  633. if (unlikely(!valid_signal(sig)))
  634. return -EINVAL;
  635. work = this_cpu_ptr(&send_signal_work);
  636. if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY)
  637. return -EBUSY;
  638. /* Add the current task, which is the target of sending signal,
  639. * to the irq_work. The current task may change when queued
  640. * irq works get executed.
  641. */
  642. work->task = current;
  643. work->sig = sig;
  644. work->type = type;
  645. irq_work_queue(&work->irq_work);
  646. return 0;
  647. }
  648. return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
  649. }
  650. BPF_CALL_1(bpf_send_signal, u32, sig)
  651. {
  652. return bpf_send_signal_common(sig, PIDTYPE_TGID);
  653. }
  654. static const struct bpf_func_proto bpf_send_signal_proto = {
  655. .func = bpf_send_signal,
  656. .gpl_only = false,
  657. .ret_type = RET_INTEGER,
  658. .arg1_type = ARG_ANYTHING,
  659. };
  660. BPF_CALL_1(bpf_send_signal_thread, u32, sig)
  661. {
  662. return bpf_send_signal_common(sig, PIDTYPE_PID);
  663. }
  664. static const struct bpf_func_proto bpf_send_signal_thread_proto = {
  665. .func = bpf_send_signal_thread,
  666. .gpl_only = false,
  667. .ret_type = RET_INTEGER,
  668. .arg1_type = ARG_ANYTHING,
  669. };
  670. const struct bpf_func_proto *
  671. bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  672. {
  673. switch (func_id) {
  674. case BPF_FUNC_map_lookup_elem:
  675. return &bpf_map_lookup_elem_proto;
  676. case BPF_FUNC_map_update_elem:
  677. return &bpf_map_update_elem_proto;
  678. case BPF_FUNC_map_delete_elem:
  679. return &bpf_map_delete_elem_proto;
  680. case BPF_FUNC_map_push_elem:
  681. return &bpf_map_push_elem_proto;
  682. case BPF_FUNC_map_pop_elem:
  683. return &bpf_map_pop_elem_proto;
  684. case BPF_FUNC_map_peek_elem:
  685. return &bpf_map_peek_elem_proto;
  686. case BPF_FUNC_ktime_get_ns:
  687. return &bpf_ktime_get_ns_proto;
  688. case BPF_FUNC_tail_call:
  689. return &bpf_tail_call_proto;
  690. case BPF_FUNC_get_current_pid_tgid:
  691. return &bpf_get_current_pid_tgid_proto;
  692. case BPF_FUNC_get_current_task:
  693. return &bpf_get_current_task_proto;
  694. case BPF_FUNC_get_current_uid_gid:
  695. return &bpf_get_current_uid_gid_proto;
  696. case BPF_FUNC_get_current_comm:
  697. return &bpf_get_current_comm_proto;
  698. case BPF_FUNC_trace_printk:
  699. return bpf_get_trace_printk_proto();
  700. case BPF_FUNC_get_smp_processor_id:
  701. return &bpf_get_smp_processor_id_proto;
  702. case BPF_FUNC_get_numa_node_id:
  703. return &bpf_get_numa_node_id_proto;
  704. case BPF_FUNC_perf_event_read:
  705. return &bpf_perf_event_read_proto;
  706. case BPF_FUNC_probe_write_user:
  707. return bpf_get_probe_write_proto();
  708. case BPF_FUNC_current_task_under_cgroup:
  709. return &bpf_current_task_under_cgroup_proto;
  710. case BPF_FUNC_get_prandom_u32:
  711. return &bpf_get_prandom_u32_proto;
  712. case BPF_FUNC_probe_read_user:
  713. return &bpf_probe_read_user_proto;
  714. case BPF_FUNC_probe_read_kernel:
  715. return &bpf_probe_read_kernel_proto;
  716. case BPF_FUNC_probe_read:
  717. return &bpf_probe_read_compat_proto;
  718. case BPF_FUNC_probe_read_user_str:
  719. return &bpf_probe_read_user_str_proto;
  720. case BPF_FUNC_probe_read_kernel_str:
  721. return &bpf_probe_read_kernel_str_proto;
  722. case BPF_FUNC_probe_read_str:
  723. return &bpf_probe_read_compat_str_proto;
  724. #ifdef CONFIG_CGROUPS
  725. case BPF_FUNC_get_current_cgroup_id:
  726. return &bpf_get_current_cgroup_id_proto;
  727. #endif
  728. case BPF_FUNC_send_signal:
  729. return &bpf_send_signal_proto;
  730. case BPF_FUNC_send_signal_thread:
  731. return &bpf_send_signal_thread_proto;
  732. case BPF_FUNC_perf_event_read_value:
  733. return &bpf_perf_event_read_value_proto;
  734. case BPF_FUNC_get_ns_current_pid_tgid:
  735. return &bpf_get_ns_current_pid_tgid_proto;
  736. default:
  737. return NULL;
  738. }
  739. }
  740. static const struct bpf_func_proto *
  741. kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  742. {
  743. switch (func_id) {
  744. case BPF_FUNC_perf_event_output:
  745. return &bpf_perf_event_output_proto;
  746. case BPF_FUNC_get_stackid:
  747. return &bpf_get_stackid_proto;
  748. case BPF_FUNC_get_stack:
  749. return &bpf_get_stack_proto;
  750. #ifdef CONFIG_BPF_KPROBE_OVERRIDE
  751. case BPF_FUNC_override_return:
  752. return &bpf_override_return_proto;
  753. #endif
  754. default:
  755. return bpf_tracing_func_proto(func_id, prog);
  756. }
  757. }
  758. /* bpf+kprobe programs can access fields of 'struct pt_regs' */
  759. static bool kprobe_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. if (off < 0 || off >= sizeof(struct pt_regs))
  764. return false;
  765. if (type != BPF_READ)
  766. return false;
  767. if (off % size != 0)
  768. return false;
  769. /*
  770. * Assertion for 32 bit to make sure last 8 byte access
  771. * (BPF_DW) to the last 4 byte member is disallowed.
  772. */
  773. if (off + size > sizeof(struct pt_regs))
  774. return false;
  775. return true;
  776. }
  777. const struct bpf_verifier_ops kprobe_verifier_ops = {
  778. .get_func_proto = kprobe_prog_func_proto,
  779. .is_valid_access = kprobe_prog_is_valid_access,
  780. };
  781. const struct bpf_prog_ops kprobe_prog_ops = {
  782. };
  783. BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
  784. u64, flags, void *, data, u64, size)
  785. {
  786. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  787. /*
  788. * r1 points to perf tracepoint buffer where first 8 bytes are hidden
  789. * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
  790. * from there and call the same bpf_perf_event_output() helper inline.
  791. */
  792. return ____bpf_perf_event_output(regs, map, flags, data, size);
  793. }
  794. static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
  795. .func = bpf_perf_event_output_tp,
  796. .gpl_only = true,
  797. .ret_type = RET_INTEGER,
  798. .arg1_type = ARG_PTR_TO_CTX,
  799. .arg2_type = ARG_CONST_MAP_PTR,
  800. .arg3_type = ARG_ANYTHING,
  801. .arg4_type = ARG_PTR_TO_MEM,
  802. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  803. };
  804. BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
  805. u64, flags)
  806. {
  807. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  808. /*
  809. * Same comment as in bpf_perf_event_output_tp(), only that this time
  810. * the other helper's function body cannot be inlined due to being
  811. * external, thus we need to call raw helper function.
  812. */
  813. return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  814. flags, 0, 0);
  815. }
  816. static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
  817. .func = bpf_get_stackid_tp,
  818. .gpl_only = true,
  819. .ret_type = RET_INTEGER,
  820. .arg1_type = ARG_PTR_TO_CTX,
  821. .arg2_type = ARG_CONST_MAP_PTR,
  822. .arg3_type = ARG_ANYTHING,
  823. };
  824. BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
  825. u64, flags)
  826. {
  827. struct pt_regs *regs = *(struct pt_regs **)tp_buff;
  828. return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  829. (unsigned long) size, flags, 0);
  830. }
  831. static const struct bpf_func_proto bpf_get_stack_proto_tp = {
  832. .func = bpf_get_stack_tp,
  833. .gpl_only = true,
  834. .ret_type = RET_INTEGER,
  835. .arg1_type = ARG_PTR_TO_CTX,
  836. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  837. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  838. .arg4_type = ARG_ANYTHING,
  839. };
  840. static const struct bpf_func_proto *
  841. tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  842. {
  843. switch (func_id) {
  844. case BPF_FUNC_perf_event_output:
  845. return &bpf_perf_event_output_proto_tp;
  846. case BPF_FUNC_get_stackid:
  847. return &bpf_get_stackid_proto_tp;
  848. case BPF_FUNC_get_stack:
  849. return &bpf_get_stack_proto_tp;
  850. default:
  851. return bpf_tracing_func_proto(func_id, prog);
  852. }
  853. }
  854. static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  855. const struct bpf_prog *prog,
  856. struct bpf_insn_access_aux *info)
  857. {
  858. if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
  859. return false;
  860. if (type != BPF_READ)
  861. return false;
  862. if (off % size != 0)
  863. return false;
  864. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
  865. return true;
  866. }
  867. const struct bpf_verifier_ops tracepoint_verifier_ops = {
  868. .get_func_proto = tp_prog_func_proto,
  869. .is_valid_access = tp_prog_is_valid_access,
  870. };
  871. const struct bpf_prog_ops tracepoint_prog_ops = {
  872. };
  873. BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
  874. struct bpf_perf_event_value *, buf, u32, size)
  875. {
  876. int err = -EINVAL;
  877. if (unlikely(size != sizeof(struct bpf_perf_event_value)))
  878. goto clear;
  879. err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
  880. &buf->running);
  881. if (unlikely(err))
  882. goto clear;
  883. return 0;
  884. clear:
  885. memset(buf, 0, size);
  886. return err;
  887. }
  888. static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
  889. .func = bpf_perf_prog_read_value,
  890. .gpl_only = true,
  891. .ret_type = RET_INTEGER,
  892. .arg1_type = ARG_PTR_TO_CTX,
  893. .arg2_type = ARG_PTR_TO_UNINIT_MEM,
  894. .arg3_type = ARG_CONST_SIZE,
  895. };
  896. BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
  897. void *, buf, u32, size, u64, flags)
  898. {
  899. #ifndef CONFIG_X86
  900. return -ENOENT;
  901. #else
  902. static const u32 br_entry_size = sizeof(struct perf_branch_entry);
  903. struct perf_branch_stack *br_stack = ctx->data->br_stack;
  904. u32 to_copy;
  905. if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
  906. return -EINVAL;
  907. if (unlikely(!br_stack))
  908. return -EINVAL;
  909. if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
  910. return br_stack->nr * br_entry_size;
  911. if (!buf || (size % br_entry_size != 0))
  912. return -EINVAL;
  913. to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
  914. memcpy(buf, br_stack->entries, to_copy);
  915. return to_copy;
  916. #endif
  917. }
  918. static const struct bpf_func_proto bpf_read_branch_records_proto = {
  919. .func = bpf_read_branch_records,
  920. .gpl_only = true,
  921. .ret_type = RET_INTEGER,
  922. .arg1_type = ARG_PTR_TO_CTX,
  923. .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
  924. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  925. .arg4_type = ARG_ANYTHING,
  926. };
  927. static const struct bpf_func_proto *
  928. pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  929. {
  930. switch (func_id) {
  931. case BPF_FUNC_perf_event_output:
  932. return &bpf_perf_event_output_proto_tp;
  933. case BPF_FUNC_get_stackid:
  934. return &bpf_get_stackid_proto_tp;
  935. case BPF_FUNC_get_stack:
  936. return &bpf_get_stack_proto_tp;
  937. case BPF_FUNC_perf_prog_read_value:
  938. return &bpf_perf_prog_read_value_proto;
  939. case BPF_FUNC_read_branch_records:
  940. return &bpf_read_branch_records_proto;
  941. default:
  942. return bpf_tracing_func_proto(func_id, prog);
  943. }
  944. }
  945. /*
  946. * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
  947. * to avoid potential recursive reuse issue when/if tracepoints are added
  948. * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
  949. *
  950. * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
  951. * in normal, irq, and nmi context.
  952. */
  953. struct bpf_raw_tp_regs {
  954. struct pt_regs regs[3];
  955. };
  956. static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
  957. static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
  958. static struct pt_regs *get_bpf_raw_tp_regs(void)
  959. {
  960. struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
  961. int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
  962. if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
  963. this_cpu_dec(bpf_raw_tp_nest_level);
  964. return ERR_PTR(-EBUSY);
  965. }
  966. return &tp_regs->regs[nest_level - 1];
  967. }
  968. static void put_bpf_raw_tp_regs(void)
  969. {
  970. this_cpu_dec(bpf_raw_tp_nest_level);
  971. }
  972. BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
  973. struct bpf_map *, map, u64, flags, void *, data, u64, size)
  974. {
  975. struct pt_regs *regs = get_bpf_raw_tp_regs();
  976. int ret;
  977. if (IS_ERR(regs))
  978. return PTR_ERR(regs);
  979. perf_fetch_caller_regs(regs);
  980. ret = ____bpf_perf_event_output(regs, map, flags, data, size);
  981. put_bpf_raw_tp_regs();
  982. return ret;
  983. }
  984. static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
  985. .func = bpf_perf_event_output_raw_tp,
  986. .gpl_only = true,
  987. .ret_type = RET_INTEGER,
  988. .arg1_type = ARG_PTR_TO_CTX,
  989. .arg2_type = ARG_CONST_MAP_PTR,
  990. .arg3_type = ARG_ANYTHING,
  991. .arg4_type = ARG_PTR_TO_MEM,
  992. .arg5_type = ARG_CONST_SIZE_OR_ZERO,
  993. };
  994. extern const struct bpf_func_proto bpf_skb_output_proto;
  995. extern const struct bpf_func_proto bpf_xdp_output_proto;
  996. BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
  997. struct bpf_map *, map, u64, flags)
  998. {
  999. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1000. int ret;
  1001. if (IS_ERR(regs))
  1002. return PTR_ERR(regs);
  1003. perf_fetch_caller_regs(regs);
  1004. /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
  1005. ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
  1006. flags, 0, 0);
  1007. put_bpf_raw_tp_regs();
  1008. return ret;
  1009. }
  1010. static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
  1011. .func = bpf_get_stackid_raw_tp,
  1012. .gpl_only = true,
  1013. .ret_type = RET_INTEGER,
  1014. .arg1_type = ARG_PTR_TO_CTX,
  1015. .arg2_type = ARG_CONST_MAP_PTR,
  1016. .arg3_type = ARG_ANYTHING,
  1017. };
  1018. BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
  1019. void *, buf, u32, size, u64, flags)
  1020. {
  1021. struct pt_regs *regs = get_bpf_raw_tp_regs();
  1022. int ret;
  1023. if (IS_ERR(regs))
  1024. return PTR_ERR(regs);
  1025. perf_fetch_caller_regs(regs);
  1026. ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
  1027. (unsigned long) size, flags, 0);
  1028. put_bpf_raw_tp_regs();
  1029. return ret;
  1030. }
  1031. static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
  1032. .func = bpf_get_stack_raw_tp,
  1033. .gpl_only = true,
  1034. .ret_type = RET_INTEGER,
  1035. .arg1_type = ARG_PTR_TO_CTX,
  1036. .arg2_type = ARG_PTR_TO_MEM,
  1037. .arg3_type = ARG_CONST_SIZE_OR_ZERO,
  1038. .arg4_type = ARG_ANYTHING,
  1039. };
  1040. static const struct bpf_func_proto *
  1041. raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1042. {
  1043. switch (func_id) {
  1044. case BPF_FUNC_perf_event_output:
  1045. return &bpf_perf_event_output_proto_raw_tp;
  1046. case BPF_FUNC_get_stackid:
  1047. return &bpf_get_stackid_proto_raw_tp;
  1048. case BPF_FUNC_get_stack:
  1049. return &bpf_get_stack_proto_raw_tp;
  1050. default:
  1051. return bpf_tracing_func_proto(func_id, prog);
  1052. }
  1053. }
  1054. static const struct bpf_func_proto *
  1055. tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  1056. {
  1057. switch (func_id) {
  1058. #ifdef CONFIG_NET
  1059. case BPF_FUNC_skb_output:
  1060. return &bpf_skb_output_proto;
  1061. case BPF_FUNC_xdp_output:
  1062. return &bpf_xdp_output_proto;
  1063. #endif
  1064. default:
  1065. return raw_tp_prog_func_proto(func_id, prog);
  1066. }
  1067. }
  1068. static bool raw_tp_prog_is_valid_access(int off, int size,
  1069. enum bpf_access_type type,
  1070. const struct bpf_prog *prog,
  1071. struct bpf_insn_access_aux *info)
  1072. {
  1073. if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
  1074. return false;
  1075. if (type != BPF_READ)
  1076. return false;
  1077. if (off % size != 0)
  1078. return false;
  1079. return true;
  1080. }
  1081. static bool tracing_prog_is_valid_access(int off, int size,
  1082. enum bpf_access_type type,
  1083. const struct bpf_prog *prog,
  1084. struct bpf_insn_access_aux *info)
  1085. {
  1086. if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
  1087. return false;
  1088. if (type != BPF_READ)
  1089. return false;
  1090. if (off % size != 0)
  1091. return false;
  1092. return btf_ctx_access(off, size, type, prog, info);
  1093. }
  1094. int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
  1095. const union bpf_attr *kattr,
  1096. union bpf_attr __user *uattr)
  1097. {
  1098. return -ENOTSUPP;
  1099. }
  1100. const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
  1101. .get_func_proto = raw_tp_prog_func_proto,
  1102. .is_valid_access = raw_tp_prog_is_valid_access,
  1103. };
  1104. const struct bpf_prog_ops raw_tracepoint_prog_ops = {
  1105. };
  1106. const struct bpf_verifier_ops tracing_verifier_ops = {
  1107. .get_func_proto = tracing_prog_func_proto,
  1108. .is_valid_access = tracing_prog_is_valid_access,
  1109. };
  1110. const struct bpf_prog_ops tracing_prog_ops = {
  1111. .test_run = bpf_prog_test_run_tracing,
  1112. };
  1113. static bool raw_tp_writable_prog_is_valid_access(int off, int size,
  1114. enum bpf_access_type type,
  1115. const struct bpf_prog *prog,
  1116. struct bpf_insn_access_aux *info)
  1117. {
  1118. if (off == 0) {
  1119. if (size != sizeof(u64) || type != BPF_READ)
  1120. return false;
  1121. info->reg_type = PTR_TO_TP_BUFFER;
  1122. }
  1123. return raw_tp_prog_is_valid_access(off, size, type, prog, info);
  1124. }
  1125. const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
  1126. .get_func_proto = raw_tp_prog_func_proto,
  1127. .is_valid_access = raw_tp_writable_prog_is_valid_access,
  1128. };
  1129. const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
  1130. };
  1131. static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
  1132. const struct bpf_prog *prog,
  1133. struct bpf_insn_access_aux *info)
  1134. {
  1135. const int size_u64 = sizeof(u64);
  1136. if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
  1137. return false;
  1138. if (type != BPF_READ)
  1139. return false;
  1140. if (off % size != 0) {
  1141. if (sizeof(unsigned long) != 4)
  1142. return false;
  1143. if (size != 8)
  1144. return false;
  1145. if (off % size != 4)
  1146. return false;
  1147. }
  1148. switch (off) {
  1149. case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
  1150. bpf_ctx_record_field_size(info, size_u64);
  1151. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  1152. return false;
  1153. break;
  1154. case bpf_ctx_range(struct bpf_perf_event_data, addr):
  1155. bpf_ctx_record_field_size(info, size_u64);
  1156. if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
  1157. return false;
  1158. break;
  1159. default:
  1160. if (size != sizeof(long))
  1161. return false;
  1162. }
  1163. return true;
  1164. }
  1165. static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
  1166. const struct bpf_insn *si,
  1167. struct bpf_insn *insn_buf,
  1168. struct bpf_prog *prog, u32 *target_size)
  1169. {
  1170. struct bpf_insn *insn = insn_buf;
  1171. switch (si->off) {
  1172. case offsetof(struct bpf_perf_event_data, sample_period):
  1173. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1174. data), si->dst_reg, si->src_reg,
  1175. offsetof(struct bpf_perf_event_data_kern, data));
  1176. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  1177. bpf_target_off(struct perf_sample_data, period, 8,
  1178. target_size));
  1179. break;
  1180. case offsetof(struct bpf_perf_event_data, addr):
  1181. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1182. data), si->dst_reg, si->src_reg,
  1183. offsetof(struct bpf_perf_event_data_kern, data));
  1184. *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
  1185. bpf_target_off(struct perf_sample_data, addr, 8,
  1186. target_size));
  1187. break;
  1188. default:
  1189. *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
  1190. regs), si->dst_reg, si->src_reg,
  1191. offsetof(struct bpf_perf_event_data_kern, regs));
  1192. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
  1193. si->off);
  1194. break;
  1195. }
  1196. return insn - insn_buf;
  1197. }
  1198. const struct bpf_verifier_ops perf_event_verifier_ops = {
  1199. .get_func_proto = pe_prog_func_proto,
  1200. .is_valid_access = pe_prog_is_valid_access,
  1201. .convert_ctx_access = pe_prog_convert_ctx_access,
  1202. };
  1203. const struct bpf_prog_ops perf_event_prog_ops = {
  1204. };
  1205. static DEFINE_MUTEX(bpf_event_mutex);
  1206. #define BPF_TRACE_MAX_PROGS 64
  1207. int perf_event_attach_bpf_prog(struct perf_event *event,
  1208. struct bpf_prog *prog)
  1209. {
  1210. struct bpf_prog_array *old_array;
  1211. struct bpf_prog_array *new_array;
  1212. int ret = -EEXIST;
  1213. /*
  1214. * Kprobe override only works if they are on the function entry,
  1215. * and only if they are on the opt-in list.
  1216. */
  1217. if (prog->kprobe_override &&
  1218. (!trace_kprobe_on_func_entry(event->tp_event) ||
  1219. !trace_kprobe_error_injectable(event->tp_event)))
  1220. return -EINVAL;
  1221. mutex_lock(&bpf_event_mutex);
  1222. if (event->prog)
  1223. goto unlock;
  1224. old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1225. if (old_array &&
  1226. bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
  1227. ret = -E2BIG;
  1228. goto unlock;
  1229. }
  1230. ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
  1231. if (ret < 0)
  1232. goto unlock;
  1233. /* set the new array to event->tp_event and set event->prog */
  1234. event->prog = prog;
  1235. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  1236. bpf_prog_array_free(old_array);
  1237. unlock:
  1238. mutex_unlock(&bpf_event_mutex);
  1239. return ret;
  1240. }
  1241. void perf_event_detach_bpf_prog(struct perf_event *event)
  1242. {
  1243. struct bpf_prog_array *old_array;
  1244. struct bpf_prog_array *new_array;
  1245. int ret;
  1246. mutex_lock(&bpf_event_mutex);
  1247. if (!event->prog)
  1248. goto unlock;
  1249. old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1250. ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
  1251. if (ret == -ENOENT)
  1252. goto unlock;
  1253. if (ret < 0) {
  1254. bpf_prog_array_delete_safe(old_array, event->prog);
  1255. } else {
  1256. rcu_assign_pointer(event->tp_event->prog_array, new_array);
  1257. bpf_prog_array_free(old_array);
  1258. }
  1259. bpf_prog_put(event->prog);
  1260. event->prog = NULL;
  1261. unlock:
  1262. mutex_unlock(&bpf_event_mutex);
  1263. }
  1264. int perf_event_query_prog_array(struct perf_event *event, void __user *info)
  1265. {
  1266. struct perf_event_query_bpf __user *uquery = info;
  1267. struct perf_event_query_bpf query = {};
  1268. struct bpf_prog_array *progs;
  1269. u32 *ids, prog_cnt, ids_len;
  1270. int ret;
  1271. if (!capable(CAP_SYS_ADMIN))
  1272. return -EPERM;
  1273. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  1274. return -EINVAL;
  1275. if (copy_from_user(&query, uquery, sizeof(query)))
  1276. return -EFAULT;
  1277. ids_len = query.ids_len;
  1278. if (ids_len > BPF_TRACE_MAX_PROGS)
  1279. return -E2BIG;
  1280. ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
  1281. if (!ids)
  1282. return -ENOMEM;
  1283. /*
  1284. * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
  1285. * is required when user only wants to check for uquery->prog_cnt.
  1286. * There is no need to check for it since the case is handled
  1287. * gracefully in bpf_prog_array_copy_info.
  1288. */
  1289. mutex_lock(&bpf_event_mutex);
  1290. progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
  1291. ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
  1292. mutex_unlock(&bpf_event_mutex);
  1293. if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
  1294. copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
  1295. ret = -EFAULT;
  1296. kfree(ids);
  1297. return ret;
  1298. }
  1299. extern struct bpf_raw_event_map __start__bpf_raw_tp[];
  1300. extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
  1301. struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
  1302. {
  1303. struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
  1304. for (; btp < __stop__bpf_raw_tp; btp++) {
  1305. if (!strcmp(btp->tp->name, name))
  1306. return btp;
  1307. }
  1308. return bpf_get_raw_tracepoint_module(name);
  1309. }
  1310. void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
  1311. {
  1312. struct module *mod = __module_address((unsigned long)btp);
  1313. if (mod)
  1314. module_put(mod);
  1315. }
  1316. static __always_inline
  1317. void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
  1318. {
  1319. cant_sleep();
  1320. rcu_read_lock();
  1321. (void) BPF_PROG_RUN(prog, args);
  1322. rcu_read_unlock();
  1323. }
  1324. #define UNPACK(...) __VA_ARGS__
  1325. #define REPEAT_1(FN, DL, X, ...) FN(X)
  1326. #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
  1327. #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
  1328. #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
  1329. #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
  1330. #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
  1331. #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
  1332. #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
  1333. #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
  1334. #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
  1335. #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
  1336. #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
  1337. #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
  1338. #define SARG(X) u64 arg##X
  1339. #define COPY(X) args[X] = arg##X
  1340. #define __DL_COM (,)
  1341. #define __DL_SEM (;)
  1342. #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  1343. #define BPF_TRACE_DEFN_x(x) \
  1344. void bpf_trace_run##x(struct bpf_prog *prog, \
  1345. REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
  1346. { \
  1347. u64 args[x]; \
  1348. REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
  1349. __bpf_trace_run(prog, args); \
  1350. } \
  1351. EXPORT_SYMBOL_GPL(bpf_trace_run##x)
  1352. BPF_TRACE_DEFN_x(1);
  1353. BPF_TRACE_DEFN_x(2);
  1354. BPF_TRACE_DEFN_x(3);
  1355. BPF_TRACE_DEFN_x(4);
  1356. BPF_TRACE_DEFN_x(5);
  1357. BPF_TRACE_DEFN_x(6);
  1358. BPF_TRACE_DEFN_x(7);
  1359. BPF_TRACE_DEFN_x(8);
  1360. BPF_TRACE_DEFN_x(9);
  1361. BPF_TRACE_DEFN_x(10);
  1362. BPF_TRACE_DEFN_x(11);
  1363. BPF_TRACE_DEFN_x(12);
  1364. static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  1365. {
  1366. struct tracepoint *tp = btp->tp;
  1367. /*
  1368. * check that program doesn't access arguments beyond what's
  1369. * available in this tracepoint
  1370. */
  1371. if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
  1372. return -EINVAL;
  1373. if (prog->aux->max_tp_access > btp->writable_size)
  1374. return -EINVAL;
  1375. return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
  1376. }
  1377. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  1378. {
  1379. return __bpf_probe_register(btp, prog);
  1380. }
  1381. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
  1382. {
  1383. return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
  1384. }
  1385. int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
  1386. u32 *fd_type, const char **buf,
  1387. u64 *probe_offset, u64 *probe_addr)
  1388. {
  1389. bool is_tracepoint, is_syscall_tp;
  1390. struct bpf_prog *prog;
  1391. int flags, err = 0;
  1392. prog = event->prog;
  1393. if (!prog)
  1394. return -ENOENT;
  1395. /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
  1396. if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
  1397. return -EOPNOTSUPP;
  1398. *prog_id = prog->aux->id;
  1399. flags = event->tp_event->flags;
  1400. is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
  1401. is_syscall_tp = is_syscall_trace_event(event->tp_event);
  1402. if (is_tracepoint || is_syscall_tp) {
  1403. *buf = is_tracepoint ? event->tp_event->tp->name
  1404. : event->tp_event->name;
  1405. *fd_type = BPF_FD_TYPE_TRACEPOINT;
  1406. *probe_offset = 0x0;
  1407. *probe_addr = 0x0;
  1408. } else {
  1409. /* kprobe/uprobe */
  1410. err = -EOPNOTSUPP;
  1411. #ifdef CONFIG_KPROBE_EVENTS
  1412. if (flags & TRACE_EVENT_FL_KPROBE)
  1413. err = bpf_get_kprobe_info(event, fd_type, buf,
  1414. probe_offset, probe_addr,
  1415. event->attr.type == PERF_TYPE_TRACEPOINT);
  1416. #endif
  1417. #ifdef CONFIG_UPROBE_EVENTS
  1418. if (flags & TRACE_EVENT_FL_UPROBE)
  1419. err = bpf_get_uprobe_info(event, fd_type, buf,
  1420. probe_offset,
  1421. event->attr.type == PERF_TYPE_TRACEPOINT);
  1422. #endif
  1423. }
  1424. return err;
  1425. }
  1426. static int __init send_signal_irq_work_init(void)
  1427. {
  1428. int cpu;
  1429. struct send_signal_irq_work *work;
  1430. for_each_possible_cpu(cpu) {
  1431. work = per_cpu_ptr(&send_signal_work, cpu);
  1432. init_irq_work(&work->irq_work, do_bpf_send_signal);
  1433. }
  1434. return 0;
  1435. }
  1436. subsys_initcall(send_signal_irq_work_init);
  1437. #ifdef CONFIG_MODULES
  1438. static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
  1439. void *module)
  1440. {
  1441. struct bpf_trace_module *btm, *tmp;
  1442. struct module *mod = module;
  1443. if (mod->num_bpf_raw_events == 0 ||
  1444. (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
  1445. return 0;
  1446. mutex_lock(&bpf_module_mutex);
  1447. switch (op) {
  1448. case MODULE_STATE_COMING:
  1449. btm = kzalloc(sizeof(*btm), GFP_KERNEL);
  1450. if (btm) {
  1451. btm->module = module;
  1452. list_add(&btm->list, &bpf_trace_modules);
  1453. }
  1454. break;
  1455. case MODULE_STATE_GOING:
  1456. list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
  1457. if (btm->module == module) {
  1458. list_del(&btm->list);
  1459. kfree(btm);
  1460. break;
  1461. }
  1462. }
  1463. break;
  1464. }
  1465. mutex_unlock(&bpf_module_mutex);
  1466. return 0;
  1467. }
  1468. static struct notifier_block bpf_module_nb = {
  1469. .notifier_call = bpf_event_notify,
  1470. };
  1471. static int __init bpf_event_init(void)
  1472. {
  1473. register_module_notifier(&bpf_module_nb);
  1474. return 0;
  1475. }
  1476. fs_initcall(bpf_event_init);
  1477. #endif /* CONFIG_MODULES */