common.bpf.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2022 Tejun Heo <tj@kernel.org>
  5. * Copyright (c) 2022 David Vernet <dvernet@meta.com>
  6. */
  7. #ifndef __SCX_COMMON_BPF_H
  8. #define __SCX_COMMON_BPF_H
  9. #ifdef LSP
  10. #define __bpf__
  11. #include "../vmlinux/vmlinux.h"
  12. #else
  13. #include "vmlinux.h"
  14. #endif
  15. #include <bpf/bpf_helpers.h>
  16. #include <bpf/bpf_tracing.h>
  17. #include <asm-generic/errno.h>
  18. #include "user_exit_info.h"
  19. #define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
  20. #define PF_KTHREAD 0x00200000 /* I am a kernel thread */
  21. #define PF_EXITING 0x00000004
  22. #define CLOCK_MONOTONIC 1
  23. /*
  24. * Earlier versions of clang/pahole lost upper 32bits in 64bit enums which can
  25. * lead to really confusing misbehaviors. Let's trigger a build failure.
  26. */
  27. static inline void ___vmlinux_h_sanity_check___(void)
  28. {
  29. _Static_assert(SCX_DSQ_FLAG_BUILTIN,
  30. "bpftool generated vmlinux.h is missing high bits for 64bit enums, upgrade clang and pahole");
  31. }
  32. s32 scx_bpf_create_dsq(u64 dsq_id, s32 node) __ksym;
  33. s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags, bool *is_idle) __ksym;
  34. void scx_bpf_dispatch(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym;
  35. void scx_bpf_dispatch_vtime(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym;
  36. u32 scx_bpf_dispatch_nr_slots(void) __ksym;
  37. void scx_bpf_dispatch_cancel(void) __ksym;
  38. bool scx_bpf_consume(u64 dsq_id) __ksym;
  39. void scx_bpf_dispatch_from_dsq_set_slice(struct bpf_iter_scx_dsq *it__iter, u64 slice) __ksym __weak;
  40. void scx_bpf_dispatch_from_dsq_set_vtime(struct bpf_iter_scx_dsq *it__iter, u64 vtime) __ksym __weak;
  41. bool scx_bpf_dispatch_from_dsq(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;
  42. bool scx_bpf_dispatch_vtime_from_dsq(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;
  43. u32 scx_bpf_reenqueue_local(void) __ksym;
  44. void scx_bpf_kick_cpu(s32 cpu, u64 flags) __ksym;
  45. s32 scx_bpf_dsq_nr_queued(u64 dsq_id) __ksym;
  46. void scx_bpf_destroy_dsq(u64 dsq_id) __ksym;
  47. int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __ksym __weak;
  48. struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) __ksym __weak;
  49. void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak;
  50. void scx_bpf_exit_bstr(s64 exit_code, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;
  51. void scx_bpf_error_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym;
  52. void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym __weak;
  53. u32 scx_bpf_cpuperf_cap(s32 cpu) __ksym __weak;
  54. u32 scx_bpf_cpuperf_cur(s32 cpu) __ksym __weak;
  55. void scx_bpf_cpuperf_set(s32 cpu, u32 perf) __ksym __weak;
  56. u32 scx_bpf_nr_cpu_ids(void) __ksym __weak;
  57. const struct cpumask *scx_bpf_get_possible_cpumask(void) __ksym __weak;
  58. const struct cpumask *scx_bpf_get_online_cpumask(void) __ksym __weak;
  59. void scx_bpf_put_cpumask(const struct cpumask *cpumask) __ksym __weak;
  60. const struct cpumask *scx_bpf_get_idle_cpumask(void) __ksym;
  61. const struct cpumask *scx_bpf_get_idle_smtmask(void) __ksym;
  62. void scx_bpf_put_idle_cpumask(const struct cpumask *cpumask) __ksym;
  63. bool scx_bpf_test_and_clear_cpu_idle(s32 cpu) __ksym;
  64. s32 scx_bpf_pick_idle_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym;
  65. s32 scx_bpf_pick_any_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym;
  66. bool scx_bpf_task_running(const struct task_struct *p) __ksym;
  67. s32 scx_bpf_task_cpu(const struct task_struct *p) __ksym;
  68. struct rq *scx_bpf_cpu_rq(s32 cpu) __ksym;
  69. struct cgroup *scx_bpf_task_cgroup(struct task_struct *p) __ksym __weak;
  70. /*
  71. * Use the following as @it__iter when calling
  72. * scx_bpf_dispatch[_vtime]_from_dsq() from within bpf_for_each() loops.
  73. */
  74. #define BPF_FOR_EACH_ITER (&___it)
  75. static inline __attribute__((format(printf, 1, 2)))
  76. void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {}
  77. /*
  78. * Helper macro for initializing the fmt and variadic argument inputs to both
  79. * bstr exit kfuncs. Callers to this function should use ___fmt and ___param to
  80. * refer to the initialized list of inputs to the bstr kfunc.
  81. */
  82. #define scx_bpf_bstr_preamble(fmt, args...) \
  83. static char ___fmt[] = fmt; \
  84. /* \
  85. * Note that __param[] must have at least one \
  86. * element to keep the verifier happy. \
  87. */ \
  88. unsigned long long ___param[___bpf_narg(args) ?: 1] = {}; \
  89. \
  90. _Pragma("GCC diagnostic push") \
  91. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  92. ___bpf_fill(___param, args); \
  93. _Pragma("GCC diagnostic pop") \
  94. /*
  95. * scx_bpf_exit() wraps the scx_bpf_exit_bstr() kfunc with variadic arguments
  96. * instead of an array of u64. Using this macro will cause the scheduler to
  97. * exit cleanly with the specified exit code being passed to user space.
  98. */
  99. #define scx_bpf_exit(code, fmt, args...) \
  100. ({ \
  101. scx_bpf_bstr_preamble(fmt, args) \
  102. scx_bpf_exit_bstr(code, ___fmt, ___param, sizeof(___param)); \
  103. ___scx_bpf_bstr_format_checker(fmt, ##args); \
  104. })
  105. /*
  106. * scx_bpf_error() wraps the scx_bpf_error_bstr() kfunc with variadic arguments
  107. * instead of an array of u64. Invoking this macro will cause the scheduler to
  108. * exit in an erroneous state, with diagnostic information being passed to the
  109. * user.
  110. */
  111. #define scx_bpf_error(fmt, args...) \
  112. ({ \
  113. scx_bpf_bstr_preamble(fmt, args) \
  114. scx_bpf_error_bstr(___fmt, ___param, sizeof(___param)); \
  115. ___scx_bpf_bstr_format_checker(fmt, ##args); \
  116. })
  117. /*
  118. * scx_bpf_dump() wraps the scx_bpf_dump_bstr() kfunc with variadic arguments
  119. * instead of an array of u64. To be used from ops.dump() and friends.
  120. */
  121. #define scx_bpf_dump(fmt, args...) \
  122. ({ \
  123. scx_bpf_bstr_preamble(fmt, args) \
  124. scx_bpf_dump_bstr(___fmt, ___param, sizeof(___param)); \
  125. ___scx_bpf_bstr_format_checker(fmt, ##args); \
  126. })
  127. #define BPF_STRUCT_OPS(name, args...) \
  128. SEC("struct_ops/"#name) \
  129. BPF_PROG(name, ##args)
  130. #define BPF_STRUCT_OPS_SLEEPABLE(name, args...) \
  131. SEC("struct_ops.s/"#name) \
  132. BPF_PROG(name, ##args)
  133. /**
  134. * RESIZABLE_ARRAY - Generates annotations for an array that may be resized
  135. * @elfsec: the data section of the BPF program in which to place the array
  136. * @arr: the name of the array
  137. *
  138. * libbpf has an API for setting map value sizes. Since data sections (i.e.
  139. * bss, data, rodata) themselves are maps, a data section can be resized. If
  140. * a data section has an array as its last element, the BTF info for that
  141. * array will be adjusted so that length of the array is extended to meet the
  142. * new length of the data section. This macro annotates an array to have an
  143. * element count of one with the assumption that this array can be resized
  144. * within the userspace program. It also annotates the section specifier so
  145. * this array exists in a custom sub data section which can be resized
  146. * independently.
  147. *
  148. * See RESIZE_ARRAY() for the userspace convenience macro for resizing an
  149. * array declared with RESIZABLE_ARRAY().
  150. */
  151. #define RESIZABLE_ARRAY(elfsec, arr) arr[1] SEC("."#elfsec"."#arr)
  152. /**
  153. * MEMBER_VPTR - Obtain the verified pointer to a struct or array member
  154. * @base: struct or array to index
  155. * @member: dereferenced member (e.g. .field, [idx0][idx1], .field[idx0] ...)
  156. *
  157. * The verifier often gets confused by the instruction sequence the compiler
  158. * generates for indexing struct fields or arrays. This macro forces the
  159. * compiler to generate a code sequence which first calculates the byte offset,
  160. * checks it against the struct or array size and add that byte offset to
  161. * generate the pointer to the member to help the verifier.
  162. *
  163. * Ideally, we want to abort if the calculated offset is out-of-bounds. However,
  164. * BPF currently doesn't support abort, so evaluate to %NULL instead. The caller
  165. * must check for %NULL and take appropriate action to appease the verifier. To
  166. * avoid confusing the verifier, it's best to check for %NULL and dereference
  167. * immediately.
  168. *
  169. * vptr = MEMBER_VPTR(my_array, [i][j]);
  170. * if (!vptr)
  171. * return error;
  172. * *vptr = new_value;
  173. *
  174. * sizeof(@base) should encompass the memory area to be accessed and thus can't
  175. * be a pointer to the area. Use `MEMBER_VPTR(*ptr, .member)` instead of
  176. * `MEMBER_VPTR(ptr, ->member)`.
  177. */
  178. #define MEMBER_VPTR(base, member) (typeof((base) member) *) \
  179. ({ \
  180. u64 __base = (u64)&(base); \
  181. u64 __addr = (u64)&((base) member) - __base; \
  182. _Static_assert(sizeof(base) >= sizeof((base) member), \
  183. "@base is smaller than @member, is @base a pointer?"); \
  184. asm volatile ( \
  185. "if %0 <= %[max] goto +2\n" \
  186. "%0 = 0\n" \
  187. "goto +1\n" \
  188. "%0 += %1\n" \
  189. : "+r"(__addr) \
  190. : "r"(__base), \
  191. [max]"i"(sizeof(base) - sizeof((base) member))); \
  192. __addr; \
  193. })
  194. /**
  195. * ARRAY_ELEM_PTR - Obtain the verified pointer to an array element
  196. * @arr: array to index into
  197. * @i: array index
  198. * @n: number of elements in array
  199. *
  200. * Similar to MEMBER_VPTR() but is intended for use with arrays where the
  201. * element count needs to be explicit.
  202. * It can be used in cases where a global array is defined with an initial
  203. * size but is intended to be be resized before loading the BPF program.
  204. * Without this version of the macro, MEMBER_VPTR() will use the compile time
  205. * size of the array to compute the max, which will result in rejection by
  206. * the verifier.
  207. */
  208. #define ARRAY_ELEM_PTR(arr, i, n) (typeof(arr[i]) *) \
  209. ({ \
  210. u64 __base = (u64)arr; \
  211. u64 __addr = (u64)&(arr[i]) - __base; \
  212. asm volatile ( \
  213. "if %0 <= %[max] goto +2\n" \
  214. "%0 = 0\n" \
  215. "goto +1\n" \
  216. "%0 += %1\n" \
  217. : "+r"(__addr) \
  218. : "r"(__base), \
  219. [max]"r"(sizeof(arr[0]) * ((n) - 1))); \
  220. __addr; \
  221. })
  222. /*
  223. * BPF declarations and helpers
  224. */
  225. /* list and rbtree */
  226. #define __contains(name, node) __attribute__((btf_decl_tag("contains:" #name ":" #node)))
  227. #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
  228. void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
  229. void bpf_obj_drop_impl(void *kptr, void *meta) __ksym;
  230. #define bpf_obj_new(type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL))
  231. #define bpf_obj_drop(kptr) bpf_obj_drop_impl(kptr, NULL)
  232. int bpf_list_push_front_impl(struct bpf_list_head *head,
  233. struct bpf_list_node *node,
  234. void *meta, __u64 off) __ksym;
  235. #define bpf_list_push_front(head, node) bpf_list_push_front_impl(head, node, NULL, 0)
  236. int bpf_list_push_back_impl(struct bpf_list_head *head,
  237. struct bpf_list_node *node,
  238. void *meta, __u64 off) __ksym;
  239. #define bpf_list_push_back(head, node) bpf_list_push_back_impl(head, node, NULL, 0)
  240. struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head) __ksym;
  241. struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head) __ksym;
  242. struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root,
  243. struct bpf_rb_node *node) __ksym;
  244. int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node,
  245. bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b),
  246. void *meta, __u64 off) __ksym;
  247. #define bpf_rbtree_add(head, node, less) bpf_rbtree_add_impl(head, node, less, NULL, 0)
  248. struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) __ksym;
  249. void *bpf_refcount_acquire_impl(void *kptr, void *meta) __ksym;
  250. #define bpf_refcount_acquire(kptr) bpf_refcount_acquire_impl(kptr, NULL)
  251. /* task */
  252. struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
  253. struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
  254. void bpf_task_release(struct task_struct *p) __ksym;
  255. /* cgroup */
  256. struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym;
  257. void bpf_cgroup_release(struct cgroup *cgrp) __ksym;
  258. struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
  259. /* css iteration */
  260. struct bpf_iter_css;
  261. struct cgroup_subsys_state;
  262. extern int bpf_iter_css_new(struct bpf_iter_css *it,
  263. struct cgroup_subsys_state *start,
  264. unsigned int flags) __weak __ksym;
  265. extern struct cgroup_subsys_state *
  266. bpf_iter_css_next(struct bpf_iter_css *it) __weak __ksym;
  267. extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;
  268. /* cpumask */
  269. struct bpf_cpumask *bpf_cpumask_create(void) __ksym;
  270. struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) __ksym;
  271. void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym;
  272. u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym;
  273. u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym;
  274. void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
  275. void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
  276. bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym;
  277. bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
  278. bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;
  279. void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym;
  280. void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym;
  281. bool bpf_cpumask_and(struct bpf_cpumask *dst, const struct cpumask *src1,
  282. const struct cpumask *src2) __ksym;
  283. void bpf_cpumask_or(struct bpf_cpumask *dst, const struct cpumask *src1,
  284. const struct cpumask *src2) __ksym;
  285. void bpf_cpumask_xor(struct bpf_cpumask *dst, const struct cpumask *src1,
  286. const struct cpumask *src2) __ksym;
  287. bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym;
  288. bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym;
  289. bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym;
  290. bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym;
  291. bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym;
  292. void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym;
  293. u32 bpf_cpumask_any_distribute(const struct cpumask *cpumask) __ksym;
  294. u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
  295. const struct cpumask *src2) __ksym;
  296. u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym;
  297. /*
  298. * Access a cpumask in read-only mode (typically to check bits).
  299. */
  300. static __always_inline const struct cpumask *cast_mask(struct bpf_cpumask *mask)
  301. {
  302. return (const struct cpumask *)mask;
  303. }
  304. /*
  305. * Return true if task @p cannot migrate to a different CPU, false
  306. * otherwise.
  307. */
  308. static inline bool is_migration_disabled(const struct task_struct *p)
  309. {
  310. if (bpf_core_field_exists(p->migration_disabled))
  311. return p->migration_disabled;
  312. return false;
  313. }
  314. /* rcu */
  315. void bpf_rcu_read_lock(void) __ksym;
  316. void bpf_rcu_read_unlock(void) __ksym;
  317. /*
  318. * Other helpers
  319. */
  320. /* useful compiler attributes */
  321. #define likely(x) __builtin_expect(!!(x), 1)
  322. #define unlikely(x) __builtin_expect(!!(x), 0)
  323. #define __maybe_unused __attribute__((__unused__))
  324. /*
  325. * READ/WRITE_ONCE() are from kernel (include/asm-generic/rwonce.h). They
  326. * prevent compiler from caching, redoing or reordering reads or writes.
  327. */
  328. typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;
  329. typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
  330. typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
  331. typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
  332. static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
  333. {
  334. switch (size) {
  335. case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;
  336. case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
  337. case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
  338. case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
  339. default:
  340. barrier();
  341. __builtin_memcpy((void *)res, (const void *)p, size);
  342. barrier();
  343. }
  344. }
  345. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  346. {
  347. switch (size) {
  348. case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;
  349. case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
  350. case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
  351. case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
  352. default:
  353. barrier();
  354. __builtin_memcpy((void *)p, (const void *)res, size);
  355. barrier();
  356. }
  357. }
  358. #define READ_ONCE(x) \
  359. ({ \
  360. union { typeof(x) __val; char __c[1]; } __u = \
  361. { .__c = { 0 } }; \
  362. __read_once_size(&(x), __u.__c, sizeof(x)); \
  363. __u.__val; \
  364. })
  365. #define WRITE_ONCE(x, val) \
  366. ({ \
  367. union { typeof(x) __val; char __c[1]; } __u = \
  368. { .__val = (val) }; \
  369. __write_once_size(&(x), __u.__c, sizeof(x)); \
  370. __u.__val; \
  371. })
  372. /*
  373. * log2_u32 - Compute the base 2 logarithm of a 32-bit exponential value.
  374. * @v: The value for which we're computing the base 2 logarithm.
  375. */
  376. static inline u32 log2_u32(u32 v)
  377. {
  378. u32 r;
  379. u32 shift;
  380. r = (v > 0xFFFF) << 4; v >>= r;
  381. shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
  382. shift = (v > 0xF) << 2; v >>= shift; r |= shift;
  383. shift = (v > 0x3) << 1; v >>= shift; r |= shift;
  384. r |= (v >> 1);
  385. return r;
  386. }
  387. /*
  388. * log2_u64 - Compute the base 2 logarithm of a 64-bit exponential value.
  389. * @v: The value for which we're computing the base 2 logarithm.
  390. */
  391. static inline u32 log2_u64(u64 v)
  392. {
  393. u32 hi = v >> 32;
  394. if (hi)
  395. return log2_u32(hi) + 32 + 1;
  396. else
  397. return log2_u32(v) + 1;
  398. }
  399. #include "compat.bpf.h"
  400. #endif /* __SCX_COMMON_BPF_H */