Path: blob/master/tools/sched_ext/include/scx/compat.bpf.h
26292 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (c) 2024 Meta Platforms, Inc. and affiliates.3* Copyright (c) 2024 Tejun Heo <[email protected]>4* Copyright (c) 2024 David Vernet <[email protected]>5*/6#ifndef __SCX_COMPAT_BPF_H7#define __SCX_COMPAT_BPF_H89#define __COMPAT_ENUM_OR_ZERO(__type, __ent) \10({ \11__type __ret = 0; \12if (bpf_core_enum_value_exists(__type, __ent)) \13__ret = __ent; \14__ret; \15})1617/* v6.12: 819513666966 ("sched_ext: Add cgroup support") */18#define __COMPAT_scx_bpf_task_cgroup(p) \19(bpf_ksym_exists(scx_bpf_task_cgroup) ? \20scx_bpf_task_cgroup((p)) : NULL)2122/*23* v6.13: The verb `dispatch` was too overloaded and confusing. kfuncs are24* renamed to unload the verb.25*26* Build error is triggered if old names are used. New binaries work with both27* new and old names. The compat macros will be removed on v6.15 release.28*29* scx_bpf_dispatch_from_dsq() and friends were added during v6.12 by30* 4c30f5ce4f7a ("sched_ext: Implement scx_bpf_dispatch[_vtime]_from_dsq()").31* Preserve __COMPAT macros until v6.15.32*/33void scx_bpf_dispatch___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak;34void scx_bpf_dispatch_vtime___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) __ksym __weak;35bool scx_bpf_consume___compat(u64 dsq_id) __ksym __weak;36void scx_bpf_dispatch_from_dsq_set_slice___compat(struct bpf_iter_scx_dsq *it__iter, u64 slice) __ksym __weak;37void scx_bpf_dispatch_from_dsq_set_vtime___compat(struct bpf_iter_scx_dsq *it__iter, u64 vtime) __ksym __weak;38bool scx_bpf_dispatch_from_dsq___compat(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;39bool scx_bpf_dispatch_vtime_from_dsq___compat(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;4041#define scx_bpf_dsq_insert(p, dsq_id, slice, enq_flags) \42(bpf_ksym_exists(scx_bpf_dsq_insert) ? \43scx_bpf_dsq_insert((p), (dsq_id), (slice), (enq_flags)) : \44scx_bpf_dispatch___compat((p), (dsq_id), (slice), (enq_flags)))4546#define scx_bpf_dsq_insert_vtime(p, dsq_id, slice, vtime, enq_flags) \47(bpf_ksym_exists(scx_bpf_dsq_insert_vtime) ? \48scx_bpf_dsq_insert_vtime((p), (dsq_id), (slice), (vtime), (enq_flags)) : \49scx_bpf_dispatch_vtime___compat((p), (dsq_id), (slice), (vtime), (enq_flags)))5051#define scx_bpf_dsq_move_to_local(dsq_id) \52(bpf_ksym_exists(scx_bpf_dsq_move_to_local) ? \53scx_bpf_dsq_move_to_local((dsq_id)) : \54scx_bpf_consume___compat((dsq_id)))5556#define __COMPAT_scx_bpf_dsq_move_set_slice(it__iter, slice) \57(bpf_ksym_exists(scx_bpf_dsq_move_set_slice) ? \58scx_bpf_dsq_move_set_slice((it__iter), (slice)) : \59(bpf_ksym_exists(scx_bpf_dispatch_from_dsq_set_slice___compat) ? \60scx_bpf_dispatch_from_dsq_set_slice___compat((it__iter), (slice)) : \61(void)0))6263#define __COMPAT_scx_bpf_dsq_move_set_vtime(it__iter, vtime) \64(bpf_ksym_exists(scx_bpf_dsq_move_set_vtime) ? \65scx_bpf_dsq_move_set_vtime((it__iter), (vtime)) : \66(bpf_ksym_exists(scx_bpf_dispatch_from_dsq_set_vtime___compat) ? \67scx_bpf_dispatch_from_dsq_set_vtime___compat((it__iter), (vtime)) : \68(void) 0))6970#define __COMPAT_scx_bpf_dsq_move(it__iter, p, dsq_id, enq_flags) \71(bpf_ksym_exists(scx_bpf_dsq_move) ? \72scx_bpf_dsq_move((it__iter), (p), (dsq_id), (enq_flags)) : \73(bpf_ksym_exists(scx_bpf_dispatch_from_dsq___compat) ? \74scx_bpf_dispatch_from_dsq___compat((it__iter), (p), (dsq_id), (enq_flags)) : \75false))7677#define __COMPAT_scx_bpf_dsq_move_vtime(it__iter, p, dsq_id, enq_flags) \78(bpf_ksym_exists(scx_bpf_dsq_move_vtime) ? \79scx_bpf_dsq_move_vtime((it__iter), (p), (dsq_id), (enq_flags)) : \80(bpf_ksym_exists(scx_bpf_dispatch_vtime_from_dsq___compat) ? \81scx_bpf_dispatch_vtime_from_dsq___compat((it__iter), (p), (dsq_id), (enq_flags)) : \82false))8384#define scx_bpf_dispatch(p, dsq_id, slice, enq_flags) \85_Static_assert(false, "scx_bpf_dispatch() renamed to scx_bpf_dsq_insert()")8687#define scx_bpf_dispatch_vtime(p, dsq_id, slice, vtime, enq_flags) \88_Static_assert(false, "scx_bpf_dispatch_vtime() renamed to scx_bpf_dsq_insert_vtime()")8990#define scx_bpf_consume(dsq_id) ({ \91_Static_assert(false, "scx_bpf_consume() renamed to scx_bpf_dsq_move_to_local()"); \92false; \93})9495#define scx_bpf_dispatch_from_dsq_set_slice(it__iter, slice) \96_Static_assert(false, "scx_bpf_dispatch_from_dsq_set_slice() renamed to scx_bpf_dsq_move_set_slice()")9798#define scx_bpf_dispatch_from_dsq_set_vtime(it__iter, vtime) \99_Static_assert(false, "scx_bpf_dispatch_from_dsq_set_vtime() renamed to scx_bpf_dsq_move_set_vtime()")100101#define scx_bpf_dispatch_from_dsq(it__iter, p, dsq_id, enq_flags) ({ \102_Static_assert(false, "scx_bpf_dispatch_from_dsq() renamed to scx_bpf_dsq_move()"); \103false; \104})105106#define scx_bpf_dispatch_vtime_from_dsq(it__iter, p, dsq_id, enq_flags) ({ \107_Static_assert(false, "scx_bpf_dispatch_vtime_from_dsq() renamed to scx_bpf_dsq_move_vtime()"); \108false; \109})110111#define __COMPAT_scx_bpf_dispatch_from_dsq_set_slice(it__iter, slice) \112_Static_assert(false, "__COMPAT_scx_bpf_dispatch_from_dsq_set_slice() renamed to __COMPAT_scx_bpf_dsq_move_set_slice()")113114#define __COMPAT_scx_bpf_dispatch_from_dsq_set_vtime(it__iter, vtime) \115_Static_assert(false, "__COMPAT_scx_bpf_dispatch_from_dsq_set_vtime() renamed to __COMPAT_scx_bpf_dsq_move_set_vtime()")116117#define __COMPAT_scx_bpf_dispatch_from_dsq(it__iter, p, dsq_id, enq_flags) ({ \118_Static_assert(false, "__COMPAT_scx_bpf_dispatch_from_dsq() renamed to __COMPAT_scx_bpf_dsq_move()"); \119false; \120})121122#define __COMPAT_scx_bpf_dispatch_vtime_from_dsq(it__iter, p, dsq_id, enq_flags) ({ \123_Static_assert(false, "__COMPAT_scx_bpf_dispatch_vtime_from_dsq() renamed to __COMPAT_scx_bpf_dsq_move_vtime()"); \124false; \125})126127/**128* __COMPAT_is_enq_cpu_selected - Test if SCX_ENQ_CPU_SELECTED is on129* in a compatible way. We will preserve this __COMPAT helper until v6.16.130*131* @enq_flags: enqueue flags from ops.enqueue()132*133* Return: True if SCX_ENQ_CPU_SELECTED is turned on in @enq_flags134*/135static inline bool __COMPAT_is_enq_cpu_selected(u64 enq_flags)136{137#ifdef HAVE_SCX_ENQ_CPU_SELECTED138/*139* This is the case that a BPF code compiled against vmlinux.h140* where the enum SCX_ENQ_CPU_SELECTED exists.141*/142143/*144* We should temporarily suspend the macro expansion of145* 'SCX_ENQ_CPU_SELECTED'. This avoids 'SCX_ENQ_CPU_SELECTED' being146* rewritten to '__SCX_ENQ_CPU_SELECTED' when 'SCX_ENQ_CPU_SELECTED'147* is defined in 'scripts/gen_enums.py'.148*/149#pragma push_macro("SCX_ENQ_CPU_SELECTED")150#undef SCX_ENQ_CPU_SELECTED151u64 flag;152153/*154* When the kernel did not have SCX_ENQ_CPU_SELECTED,155* select_task_rq_scx() has never been skipped. Thus, this case156* should be considered that the CPU has already been selected.157*/158if (!bpf_core_enum_value_exists(enum scx_enq_flags,159SCX_ENQ_CPU_SELECTED))160return true;161162flag = bpf_core_enum_value(enum scx_enq_flags, SCX_ENQ_CPU_SELECTED);163return enq_flags & flag;164165/*166* Once done, resume the macro expansion of 'SCX_ENQ_CPU_SELECTED'.167*/168#pragma pop_macro("SCX_ENQ_CPU_SELECTED")169#else170/*171* This is the case that a BPF code compiled against vmlinux.h172* where the enum SCX_ENQ_CPU_SELECTED does NOT exist.173*/174return true;175#endif /* HAVE_SCX_ENQ_CPU_SELECTED */176}177178179#define scx_bpf_now() \180(bpf_ksym_exists(scx_bpf_now) ? \181scx_bpf_now() : \182bpf_ktime_get_ns())183184/*185* v6.15: Introduce event counters.186*187* Preserve the following macro until v6.17.188*/189#define __COMPAT_scx_bpf_events(events, size) \190(bpf_ksym_exists(scx_bpf_events) ? \191scx_bpf_events(events, size) : ({}))192193/*194* v6.15: Introduce NUMA-aware kfuncs to operate with per-node idle195* cpumasks.196*197* Preserve the following __COMPAT_scx_*_node macros until v6.17.198*/199#define __COMPAT_scx_bpf_nr_node_ids() \200(bpf_ksym_exists(scx_bpf_nr_node_ids) ? \201scx_bpf_nr_node_ids() : 1U)202203#define __COMPAT_scx_bpf_cpu_node(cpu) \204(bpf_ksym_exists(scx_bpf_cpu_node) ? \205scx_bpf_cpu_node(cpu) : 0)206207#define __COMPAT_scx_bpf_get_idle_cpumask_node(node) \208(bpf_ksym_exists(scx_bpf_get_idle_cpumask_node) ? \209scx_bpf_get_idle_cpumask_node(node) : \210scx_bpf_get_idle_cpumask()) \211212#define __COMPAT_scx_bpf_get_idle_smtmask_node(node) \213(bpf_ksym_exists(scx_bpf_get_idle_smtmask_node) ? \214scx_bpf_get_idle_smtmask_node(node) : \215scx_bpf_get_idle_smtmask())216217#define __COMPAT_scx_bpf_pick_idle_cpu_node(cpus_allowed, node, flags) \218(bpf_ksym_exists(scx_bpf_pick_idle_cpu_node) ? \219scx_bpf_pick_idle_cpu_node(cpus_allowed, node, flags) : \220scx_bpf_pick_idle_cpu(cpus_allowed, flags))221222#define __COMPAT_scx_bpf_pick_any_cpu_node(cpus_allowed, node, flags) \223(bpf_ksym_exists(scx_bpf_pick_any_cpu_node) ? \224scx_bpf_pick_any_cpu_node(cpus_allowed, node, flags) : \225scx_bpf_pick_any_cpu(cpus_allowed, flags))226227/*228* Define sched_ext_ops. This may be expanded to define multiple variants for229* backward compatibility. See compat.h::SCX_OPS_LOAD/ATTACH().230*/231#define SCX_OPS_DEFINE(__name, ...) \232SEC(".struct_ops.link") \233struct sched_ext_ops __name = { \234__VA_ARGS__, \235};236237#endif /* __SCX_COMPAT_BPF_H */238239240