Path: blob/master/tools/sched_ext/include/scx/common.bpf.h
26292 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (c) 2022 Meta Platforms, Inc. and affiliates.3* Copyright (c) 2022 Tejun Heo <[email protected]>4* Copyright (c) 2022 David Vernet <[email protected]>5*/6#ifndef __SCX_COMMON_BPF_H7#define __SCX_COMMON_BPF_H89/*10* The generated kfunc prototypes in vmlinux.h are missing address space11* attributes which cause build failures. For now, suppress the generated12* prototypes. See https://github.com/sched-ext/scx/issues/1111.13*/14#define BPF_NO_KFUNC_PROTOTYPES1516#ifdef LSP17#define __bpf__18#include "../vmlinux.h"19#else20#include "vmlinux.h"21#endif2223#include <bpf/bpf_helpers.h>24#include <bpf/bpf_tracing.h>25#include <asm-generic/errno.h>26#include "user_exit_info.h"27#include "enum_defs.autogen.h"2829#define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */30#define PF_KTHREAD 0x00200000 /* I am a kernel thread */31#define PF_EXITING 0x0000000432#define CLOCK_MONOTONIC 13334extern int LINUX_KERNEL_VERSION __kconfig;35extern const char CONFIG_CC_VERSION_TEXT[64] __kconfig __weak;36extern const char CONFIG_LOCALVERSION[64] __kconfig __weak;3738/*39* Earlier versions of clang/pahole lost upper 32bits in 64bit enums which can40* lead to really confusing misbehaviors. Let's trigger a build failure.41*/42static inline void ___vmlinux_h_sanity_check___(void)43{44_Static_assert(SCX_DSQ_FLAG_BUILTIN,45"bpftool generated vmlinux.h is missing high bits for 64bit enums, upgrade clang and pahole");46}4748s32 scx_bpf_create_dsq(u64 dsq_id, s32 node) __ksym;49s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags, bool *is_idle) __ksym;50s32 scx_bpf_select_cpu_and(struct task_struct *p, s32 prev_cpu, u64 wake_flags,51const struct cpumask *cpus_allowed, u64 flags) __ksym __weak;52void scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak;53void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak;54u32 scx_bpf_dispatch_nr_slots(void) __ksym;55void scx_bpf_dispatch_cancel(void) __ksym;56bool scx_bpf_dsq_move_to_local(u64 dsq_id) __ksym __weak;57void scx_bpf_dsq_move_set_slice(struct bpf_iter_scx_dsq *it__iter, u64 slice) __ksym __weak;58void scx_bpf_dsq_move_set_vtime(struct bpf_iter_scx_dsq *it__iter, u64 vtime) __ksym __weak;59bool scx_bpf_dsq_move(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;60bool scx_bpf_dsq_move_vtime(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;61u32 scx_bpf_reenqueue_local(void) __ksym;62void scx_bpf_kick_cpu(s32 cpu, u64 flags) __ksym;63s32 scx_bpf_dsq_nr_queued(u64 dsq_id) __ksym;64void scx_bpf_destroy_dsq(u64 dsq_id) __ksym;65int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __ksym __weak;66struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) __ksym __weak;67void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak;68void scx_bpf_exit_bstr(s64 exit_code, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;69void scx_bpf_error_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym;70void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym __weak;71u32 scx_bpf_cpuperf_cap(s32 cpu) __ksym __weak;72u32 scx_bpf_cpuperf_cur(s32 cpu) __ksym __weak;73void scx_bpf_cpuperf_set(s32 cpu, u32 perf) __ksym __weak;74u32 scx_bpf_nr_node_ids(void) __ksym __weak;75u32 scx_bpf_nr_cpu_ids(void) __ksym __weak;76int scx_bpf_cpu_node(s32 cpu) __ksym __weak;77const struct cpumask *scx_bpf_get_possible_cpumask(void) __ksym __weak;78const struct cpumask *scx_bpf_get_online_cpumask(void) __ksym __weak;79void scx_bpf_put_cpumask(const struct cpumask *cpumask) __ksym __weak;80const struct cpumask *scx_bpf_get_idle_cpumask_node(int node) __ksym __weak;81const struct cpumask *scx_bpf_get_idle_cpumask(void) __ksym;82const struct cpumask *scx_bpf_get_idle_smtmask_node(int node) __ksym __weak;83const struct cpumask *scx_bpf_get_idle_smtmask(void) __ksym;84void scx_bpf_put_idle_cpumask(const struct cpumask *cpumask) __ksym;85bool scx_bpf_test_and_clear_cpu_idle(s32 cpu) __ksym;86s32 scx_bpf_pick_idle_cpu_node(const cpumask_t *cpus_allowed, int node, u64 flags) __ksym __weak;87s32 scx_bpf_pick_idle_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym;88s32 scx_bpf_pick_any_cpu_node(const cpumask_t *cpus_allowed, int node, u64 flags) __ksym __weak;89s32 scx_bpf_pick_any_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym;90bool scx_bpf_task_running(const struct task_struct *p) __ksym;91s32 scx_bpf_task_cpu(const struct task_struct *p) __ksym;92struct rq *scx_bpf_cpu_rq(s32 cpu) __ksym;93struct cgroup *scx_bpf_task_cgroup(struct task_struct *p) __ksym __weak;94u64 scx_bpf_now(void) __ksym __weak;95void scx_bpf_events(struct scx_event_stats *events, size_t events__sz) __ksym __weak;9697/*98* Use the following as @it__iter when calling scx_bpf_dsq_move[_vtime]() from99* within bpf_for_each() loops.100*/101#define BPF_FOR_EACH_ITER (&___it)102103#define scx_read_event(e, name) \104(bpf_core_field_exists((e)->name) ? (e)->name : 0)105106static inline __attribute__((format(printf, 1, 2)))107void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {}108109/*110* Helper macro for initializing the fmt and variadic argument inputs to both111* bstr exit kfuncs. Callers to this function should use ___fmt and ___param to112* refer to the initialized list of inputs to the bstr kfunc.113*/114#define scx_bpf_bstr_preamble(fmt, args...) \115static char ___fmt[] = fmt; \116/* \117* Note that __param[] must have at least one \118* element to keep the verifier happy. \119*/ \120unsigned long long ___param[___bpf_narg(args) ?: 1] = {}; \121\122_Pragma("GCC diagnostic push") \123_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \124___bpf_fill(___param, args); \125_Pragma("GCC diagnostic pop")126127/*128* scx_bpf_exit() wraps the scx_bpf_exit_bstr() kfunc with variadic arguments129* instead of an array of u64. Using this macro will cause the scheduler to130* exit cleanly with the specified exit code being passed to user space.131*/132#define scx_bpf_exit(code, fmt, args...) \133({ \134scx_bpf_bstr_preamble(fmt, args) \135scx_bpf_exit_bstr(code, ___fmt, ___param, sizeof(___param)); \136___scx_bpf_bstr_format_checker(fmt, ##args); \137})138139/*140* scx_bpf_error() wraps the scx_bpf_error_bstr() kfunc with variadic arguments141* instead of an array of u64. Invoking this macro will cause the scheduler to142* exit in an erroneous state, with diagnostic information being passed to the143* user.144*/145#define scx_bpf_error(fmt, args...) \146({ \147scx_bpf_bstr_preamble(fmt, args) \148scx_bpf_error_bstr(___fmt, ___param, sizeof(___param)); \149___scx_bpf_bstr_format_checker(fmt, ##args); \150})151152/*153* scx_bpf_dump() wraps the scx_bpf_dump_bstr() kfunc with variadic arguments154* instead of an array of u64. To be used from ops.dump() and friends.155*/156#define scx_bpf_dump(fmt, args...) \157({ \158scx_bpf_bstr_preamble(fmt, args) \159scx_bpf_dump_bstr(___fmt, ___param, sizeof(___param)); \160___scx_bpf_bstr_format_checker(fmt, ##args); \161})162163/*164* scx_bpf_dump_header() is a wrapper around scx_bpf_dump that adds a header165* of system information for debugging.166*/167#define scx_bpf_dump_header() \168({ \169scx_bpf_dump("kernel: %d.%d.%d %s\ncc: %s\n", \170LINUX_KERNEL_VERSION >> 16, \171LINUX_KERNEL_VERSION >> 8 & 0xFF, \172LINUX_KERNEL_VERSION & 0xFF, \173CONFIG_LOCALVERSION, \174CONFIG_CC_VERSION_TEXT); \175})176177#define BPF_STRUCT_OPS(name, args...) \178SEC("struct_ops/"#name) \179BPF_PROG(name, ##args)180181#define BPF_STRUCT_OPS_SLEEPABLE(name, args...) \182SEC("struct_ops.s/"#name) \183BPF_PROG(name, ##args)184185/**186* RESIZABLE_ARRAY - Generates annotations for an array that may be resized187* @elfsec: the data section of the BPF program in which to place the array188* @arr: the name of the array189*190* libbpf has an API for setting map value sizes. Since data sections (i.e.191* bss, data, rodata) themselves are maps, a data section can be resized. If192* a data section has an array as its last element, the BTF info for that193* array will be adjusted so that length of the array is extended to meet the194* new length of the data section. This macro annotates an array to have an195* element count of one with the assumption that this array can be resized196* within the userspace program. It also annotates the section specifier so197* this array exists in a custom sub data section which can be resized198* independently.199*200* See RESIZE_ARRAY() for the userspace convenience macro for resizing an201* array declared with RESIZABLE_ARRAY().202*/203#define RESIZABLE_ARRAY(elfsec, arr) arr[1] SEC("."#elfsec"."#arr)204205/**206* MEMBER_VPTR - Obtain the verified pointer to a struct or array member207* @base: struct or array to index208* @member: dereferenced member (e.g. .field, [idx0][idx1], .field[idx0] ...)209*210* The verifier often gets confused by the instruction sequence the compiler211* generates for indexing struct fields or arrays. This macro forces the212* compiler to generate a code sequence which first calculates the byte offset,213* checks it against the struct or array size and add that byte offset to214* generate the pointer to the member to help the verifier.215*216* Ideally, we want to abort if the calculated offset is out-of-bounds. However,217* BPF currently doesn't support abort, so evaluate to %NULL instead. The caller218* must check for %NULL and take appropriate action to appease the verifier. To219* avoid confusing the verifier, it's best to check for %NULL and dereference220* immediately.221*222* vptr = MEMBER_VPTR(my_array, [i][j]);223* if (!vptr)224* return error;225* *vptr = new_value;226*227* sizeof(@base) should encompass the memory area to be accessed and thus can't228* be a pointer to the area. Use `MEMBER_VPTR(*ptr, .member)` instead of229* `MEMBER_VPTR(ptr, ->member)`.230*/231#define MEMBER_VPTR(base, member) (typeof((base) member) *) \232({ \233u64 __base = (u64)&(base); \234u64 __addr = (u64)&((base) member) - __base; \235_Static_assert(sizeof(base) >= sizeof((base) member), \236"@base is smaller than @member, is @base a pointer?"); \237asm volatile ( \238"if %0 <= %[max] goto +2\n" \239"%0 = 0\n" \240"goto +1\n" \241"%0 += %1\n" \242: "+r"(__addr) \243: "r"(__base), \244[max]"i"(sizeof(base) - sizeof((base) member))); \245__addr; \246})247248/**249* ARRAY_ELEM_PTR - Obtain the verified pointer to an array element250* @arr: array to index into251* @i: array index252* @n: number of elements in array253*254* Similar to MEMBER_VPTR() but is intended for use with arrays where the255* element count needs to be explicit.256* It can be used in cases where a global array is defined with an initial257* size but is intended to be be resized before loading the BPF program.258* Without this version of the macro, MEMBER_VPTR() will use the compile time259* size of the array to compute the max, which will result in rejection by260* the verifier.261*/262#define ARRAY_ELEM_PTR(arr, i, n) (typeof(arr[i]) *) \263({ \264u64 __base = (u64)arr; \265u64 __addr = (u64)&(arr[i]) - __base; \266asm volatile ( \267"if %0 <= %[max] goto +2\n" \268"%0 = 0\n" \269"goto +1\n" \270"%0 += %1\n" \271: "+r"(__addr) \272: "r"(__base), \273[max]"r"(sizeof(arr[0]) * ((n) - 1))); \274__addr; \275})276277278/*279* BPF declarations and helpers280*/281282/* list and rbtree */283#define __contains(name, node) __attribute__((btf_decl_tag("contains:" #name ":" #node)))284#define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))285286void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;287void bpf_obj_drop_impl(void *kptr, void *meta) __ksym;288289#define bpf_obj_new(type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL))290#define bpf_obj_drop(kptr) bpf_obj_drop_impl(kptr, NULL)291292int bpf_list_push_front_impl(struct bpf_list_head *head,293struct bpf_list_node *node,294void *meta, __u64 off) __ksym;295#define bpf_list_push_front(head, node) bpf_list_push_front_impl(head, node, NULL, 0)296297int bpf_list_push_back_impl(struct bpf_list_head *head,298struct bpf_list_node *node,299void *meta, __u64 off) __ksym;300#define bpf_list_push_back(head, node) bpf_list_push_back_impl(head, node, NULL, 0)301302struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head) __ksym;303struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head) __ksym;304struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root,305struct bpf_rb_node *node) __ksym;306int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node,307bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b),308void *meta, __u64 off) __ksym;309#define bpf_rbtree_add(head, node, less) bpf_rbtree_add_impl(head, node, less, NULL, 0)310311struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) __ksym;312313void *bpf_refcount_acquire_impl(void *kptr, void *meta) __ksym;314#define bpf_refcount_acquire(kptr) bpf_refcount_acquire_impl(kptr, NULL)315316/* task */317struct task_struct *bpf_task_from_pid(s32 pid) __ksym;318struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;319void bpf_task_release(struct task_struct *p) __ksym;320321/* cgroup */322struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym;323void bpf_cgroup_release(struct cgroup *cgrp) __ksym;324struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;325326/* css iteration */327struct bpf_iter_css;328struct cgroup_subsys_state;329extern int bpf_iter_css_new(struct bpf_iter_css *it,330struct cgroup_subsys_state *start,331unsigned int flags) __weak __ksym;332extern struct cgroup_subsys_state *333bpf_iter_css_next(struct bpf_iter_css *it) __weak __ksym;334extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;335336/* cpumask */337struct bpf_cpumask *bpf_cpumask_create(void) __ksym;338struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) __ksym;339void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym;340u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym;341u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym;342void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;343void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;344bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym;345bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;346bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym;347void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym;348void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym;349bool bpf_cpumask_and(struct bpf_cpumask *dst, const struct cpumask *src1,350const struct cpumask *src2) __ksym;351void bpf_cpumask_or(struct bpf_cpumask *dst, const struct cpumask *src1,352const struct cpumask *src2) __ksym;353void bpf_cpumask_xor(struct bpf_cpumask *dst, const struct cpumask *src1,354const struct cpumask *src2) __ksym;355bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym;356bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym;357bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym;358bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym;359bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym;360void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym;361u32 bpf_cpumask_any_distribute(const struct cpumask *cpumask) __ksym;362u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,363const struct cpumask *src2) __ksym;364u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym;365366int bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign, u32 nr_words) __ksym;367int *bpf_iter_bits_next(struct bpf_iter_bits *it) __ksym;368void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __ksym;369370#define def_iter_struct(name) \371struct bpf_iter_##name { \372struct bpf_iter_bits it; \373const struct cpumask *bitmap; \374};375376#define def_iter_new(name) \377static inline int bpf_iter_##name##_new( \378struct bpf_iter_##name *it, const u64 *unsafe_ptr__ign, u32 nr_words) \379{ \380it->bitmap = scx_bpf_get_##name##_cpumask(); \381return bpf_iter_bits_new(&it->it, (const u64 *)it->bitmap, \382sizeof(struct cpumask) / 8); \383}384385#define def_iter_next(name) \386static inline int *bpf_iter_##name##_next(struct bpf_iter_##name *it) { \387return bpf_iter_bits_next(&it->it); \388}389390#define def_iter_destroy(name) \391static inline void bpf_iter_##name##_destroy(struct bpf_iter_##name *it) { \392scx_bpf_put_cpumask(it->bitmap); \393bpf_iter_bits_destroy(&it->it); \394}395#define def_for_each_cpu(cpu, name) for_each_##name##_cpu(cpu)396397/// Provides iterator for possible and online cpus.398///399/// # Example400///401/// ```402/// static inline void example_use() {403/// int *cpu;404///405/// for_each_possible_cpu(cpu){406/// bpf_printk("CPU %d is possible", *cpu);407/// }408///409/// for_each_online_cpu(cpu){410/// bpf_printk("CPU %d is online", *cpu);411/// }412/// }413/// ```414def_iter_struct(possible);415def_iter_new(possible);416def_iter_next(possible);417def_iter_destroy(possible);418#define for_each_possible_cpu(cpu) bpf_for_each(possible, cpu, NULL, 0)419420def_iter_struct(online);421def_iter_new(online);422def_iter_next(online);423def_iter_destroy(online);424#define for_each_online_cpu(cpu) bpf_for_each(online, cpu, NULL, 0)425426/*427* Access a cpumask in read-only mode (typically to check bits).428*/429static __always_inline const struct cpumask *cast_mask(struct bpf_cpumask *mask)430{431return (const struct cpumask *)mask;432}433434/*435* Return true if task @p cannot migrate to a different CPU, false436* otherwise.437*/438static inline bool is_migration_disabled(const struct task_struct *p)439{440if (bpf_core_field_exists(p->migration_disabled))441return p->migration_disabled;442return false;443}444445/* rcu */446void bpf_rcu_read_lock(void) __ksym;447void bpf_rcu_read_unlock(void) __ksym;448449/*450* Time helpers, most of which are from jiffies.h.451*/452453/**454* time_delta - Calculate the delta between new and old time stamp455* @after: first comparable as u64456* @before: second comparable as u64457*458* Return: the time difference, which is >= 0459*/460static inline s64 time_delta(u64 after, u64 before)461{462return (s64)(after - before) > 0 ? (s64)(after - before) : 0;463}464465/**466* time_after - returns true if the time a is after time b.467* @a: first comparable as u64468* @b: second comparable as u64469*470* Do this with "<0" and ">=0" to only test the sign of the result. A471* good compiler would generate better code (and a really good compiler472* wouldn't care). Gcc is currently neither.473*474* Return: %true is time a is after time b, otherwise %false.475*/476static inline bool time_after(u64 a, u64 b)477{478return (s64)(b - a) < 0;479}480481/**482* time_before - returns true if the time a is before time b.483* @a: first comparable as u64484* @b: second comparable as u64485*486* Return: %true is time a is before time b, otherwise %false.487*/488static inline bool time_before(u64 a, u64 b)489{490return time_after(b, a);491}492493/**494* time_after_eq - returns true if the time a is after or the same as time b.495* @a: first comparable as u64496* @b: second comparable as u64497*498* Return: %true is time a is after or the same as time b, otherwise %false.499*/500static inline bool time_after_eq(u64 a, u64 b)501{502return (s64)(a - b) >= 0;503}504505/**506* time_before_eq - returns true if the time a is before or the same as time b.507* @a: first comparable as u64508* @b: second comparable as u64509*510* Return: %true is time a is before or the same as time b, otherwise %false.511*/512static inline bool time_before_eq(u64 a, u64 b)513{514return time_after_eq(b, a);515}516517/**518* time_in_range - Calculate whether a is in the range of [b, c].519* @a: time to test520* @b: beginning of the range521* @c: end of the range522*523* Return: %true is time a is in the range [b, c], otherwise %false.524*/525static inline bool time_in_range(u64 a, u64 b, u64 c)526{527return time_after_eq(a, b) && time_before_eq(a, c);528}529530/**531* time_in_range_open - Calculate whether a is in the range of [b, c).532* @a: time to test533* @b: beginning of the range534* @c: end of the range535*536* Return: %true is time a is in the range [b, c), otherwise %false.537*/538static inline bool time_in_range_open(u64 a, u64 b, u64 c)539{540return time_after_eq(a, b) && time_before(a, c);541}542543544/*545* Other helpers546*/547548/* useful compiler attributes */549#define likely(x) __builtin_expect(!!(x), 1)550#define unlikely(x) __builtin_expect(!!(x), 0)551#define __maybe_unused __attribute__((__unused__))552553/*554* READ/WRITE_ONCE() are from kernel (include/asm-generic/rwonce.h). They555* prevent compiler from caching, redoing or reordering reads or writes.556*/557typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;558typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;559typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;560typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;561562static __always_inline void __read_once_size(const volatile void *p, void *res, int size)563{564switch (size) {565case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;566case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;567case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;568case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;569default:570barrier();571__builtin_memcpy((void *)res, (const void *)p, size);572barrier();573}574}575576static __always_inline void __write_once_size(volatile void *p, void *res, int size)577{578switch (size) {579case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;580case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;581case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;582case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;583default:584barrier();585__builtin_memcpy((void *)p, (const void *)res, size);586barrier();587}588}589590/*591* __unqual_typeof(x) - Declare an unqualified scalar type, leaving592* non-scalar types unchanged,593*594* Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'595* is not type-compatible with 'signed char', and we define a separate case.596*597* This is copied verbatim from kernel's include/linux/compiler_types.h, but598* with default expression (for pointers) changed from (x) to (typeof(x)0).599*600* This is because LLVM has a bug where for lvalue (x), it does not get rid of601* an extra address_space qualifier, but does in case of rvalue (typeof(x)0).602* Hence, for pointers, we need to create an rvalue expression to get the603* desired type. See https://github.com/llvm/llvm-project/issues/53400.604*/605#define __scalar_type_to_expr_cases(type) \606unsigned type : (unsigned type)0, signed type : (signed type)0607608#define __unqual_typeof(x) \609typeof(_Generic((x), \610char: (char)0, \611__scalar_type_to_expr_cases(char), \612__scalar_type_to_expr_cases(short), \613__scalar_type_to_expr_cases(int), \614__scalar_type_to_expr_cases(long), \615__scalar_type_to_expr_cases(long long), \616default: (typeof(x))0))617618#define READ_ONCE(x) \619({ \620union { __unqual_typeof(x) __val; char __c[1]; } __u = \621{ .__c = { 0 } }; \622__read_once_size((__unqual_typeof(x) *)&(x), __u.__c, sizeof(x)); \623__u.__val; \624})625626#define WRITE_ONCE(x, val) \627({ \628union { __unqual_typeof(x) __val; char __c[1]; } __u = \629{ .__val = (val) }; \630__write_once_size((__unqual_typeof(x) *)&(x), __u.__c, sizeof(x)); \631__u.__val; \632})633634/*635* log2_u32 - Compute the base 2 logarithm of a 32-bit exponential value.636* @v: The value for which we're computing the base 2 logarithm.637*/638static inline u32 log2_u32(u32 v)639{640u32 r;641u32 shift;642643r = (v > 0xFFFF) << 4; v >>= r;644shift = (v > 0xFF) << 3; v >>= shift; r |= shift;645shift = (v > 0xF) << 2; v >>= shift; r |= shift;646shift = (v > 0x3) << 1; v >>= shift; r |= shift;647r |= (v >> 1);648return r;649}650651/*652* log2_u64 - Compute the base 2 logarithm of a 64-bit exponential value.653* @v: The value for which we're computing the base 2 logarithm.654*/655static inline u32 log2_u64(u64 v)656{657u32 hi = v >> 32;658if (hi)659return log2_u32(hi) + 32 + 1;660else661return log2_u32(v) + 1;662}663664/*665* Return a value proportionally scaled to the task's weight.666*/667static inline u64 scale_by_task_weight(const struct task_struct *p, u64 value)668{669return (value * p->scx.weight) / 100;670}671672/*673* Return a value inversely proportional to the task's weight.674*/675static inline u64 scale_by_task_weight_inverse(const struct task_struct *p, u64 value)676{677return value * 100 / p->scx.weight;678}679680681#include "compat.bpf.h"682#include "enums.bpf.h"683684#endif /* __SCX_COMMON_BPF_H */685686687