1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst 4 * 5 * Sub-scheduler hierarchy support. 6 * 7 * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. 8 * Copyright (c) 2026 Tejun Heo <tj@kernel.org> 9 */ 10 #ifndef _KERNEL_SCHED_EXT_SUB_H 11 #define _KERNEL_SCHED_EXT_SUB_H 12 13 #include "internal.h" 14 15 #ifdef CONFIG_EXT_SUB_SCHED 16 17 struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root); 18 struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root); 19 void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch); 20 struct cgroup *sch_cgroup(struct scx_sched *sch); 21 void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch); 22 void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog); 23 void scx_pstack_recursion_on_caps_updated(struct bpf_prog *prog); 24 void drain_descendants(struct scx_sched *sch); 25 void scx_sub_disable(struct scx_sched *sch); 26 void scx_sub_enable_workfn(struct kthread_work *work); 27 bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux); 28 void scx_free_pshards(struct scx_sched *sch); 29 s32 scx_alloc_pshards(struct scx_sched *sch); 30 void scx_init_root_caps(struct scx_sched *sch); 31 void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev); 32 void scx_unbypass_replay_ecaps(struct rq *rq, struct scx_sched *sch); 33 void scx_online_ecaps(struct rq *rq); 34 void scx_offline_ecaps(struct rq *rq); 35 void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu); 36 void scx_discard_stale_ecaps_syncs(void); 37 struct scx_dispatch_q *scx_local_or_reject_dsq(struct scx_sched *sch, struct rq *rq, 38 struct task_struct *p, u64 *enq_flags); 39 bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p); 40 void scx_reenq_reject(struct rq *rq); 41 42 static inline const char *sch_cgrp_path(struct scx_sched *sch) 43 { 44 return sch->cgrp_path; 45 } 46 47 #else /* CONFIG_EXT_SUB_SCHED */ 48 49 static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; } 50 static inline struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root) { return NULL; } 51 static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {} 52 static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; } 53 static inline const char *sch_cgrp_path(struct scx_sched *sch) { return "/"; } 54 static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {} 55 static inline void drain_descendants(struct scx_sched *sch) { } 56 static inline void scx_sub_disable(struct scx_sched *sch) { } 57 static inline void scx_free_pshards(struct scx_sched *sch) {} 58 static inline s32 scx_alloc_pshards(struct scx_sched *sch) { return 0; } 59 static inline void scx_init_root_caps(struct scx_sched *sch) {} 60 static inline void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev) {} 61 static inline void scx_unbypass_replay_ecaps(struct rq *rq, struct scx_sched *sch) {} 62 static inline void scx_online_ecaps(struct rq *rq) {} 63 static inline void scx_offline_ecaps(struct rq *rq) {} 64 static inline void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu) {} 65 static inline void scx_discard_stale_ecaps_syncs(void) {} 66 static inline struct scx_dispatch_q *scx_local_or_reject_dsq(struct scx_sched *sch, struct rq *rq, struct task_struct *p, u64 *enq_flags) { return &rq->scx.local_dsq; } 67 static inline bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p) { return false; } 68 static inline void scx_reenq_reject(struct rq *rq) {} 69 70 #endif /* CONFIG_EXT_SUB_SCHED */ 71 72 /** 73 * scx_for_each_descendant_pre - pre-order walk of a sched's descendants 74 * @pos: iteration cursor 75 * @root: sched to walk the descendants of 76 * 77 * Walk @root's descendants. @root is included in the iteration and the first 78 * node to be visited. Must be called with scx_enable_mutex, scx_sched_lock, or 79 * RCU read lock. 80 */ 81 #define scx_for_each_descendant_pre(pos, root) \ 82 for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos); \ 83 (pos) = scx_next_descendant_pre((pos), (root))) 84 85 #ifdef CONFIG_EXT_SUB_SCHED 86 87 /** 88 * scx_missing_caps - The caps in @needed that @sch lacks on @cpu 89 * @sch: sched to test 90 * @cpu: cpu to test on 91 * @needed: bitmask of SCX_CAP_* values 92 * 93 * Return the caps in @needed that @sch lacks for @cpu, 0 if it holds them all. 94 */ 95 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) 96 { 97 u64 ecaps; 98 99 /* root holds every cap on every cpu */ 100 if (!sch->level) 101 return 0; 102 103 ecaps = READ_ONCE(per_cpu_ptr(sch->pcpu, cpu)->ecaps); 104 105 return needed & ~ecaps; 106 } 107 108 /* 109 * Cap semantics: which caps an action requires, and which caps a cap implies. 110 * Keep all such mappings collected here. 111 */ 112 113 /* map @enq_flags to the SCX_CAP_* bit required for the local-DSQ insert */ 114 static inline u64 scx_caps_for_enq(u64 enq_flags) 115 { 116 /* a restored task must be put into the local DSQ regardless of caps */ 117 if (enq_flags & SCX_ENQ_IGNORE_CAPS) 118 return 0; 119 if (enq_flags & SCX_ENQ_IMMED) 120 return SCX_CAP_ENQ_IMMED; 121 return SCX_CAP_ENQ; 122 } 123 124 /* map queued @p to the SCX_CAP_* bit required to stay on its local DSQ */ 125 static inline u64 scx_caps_for_task(struct task_struct *p) 126 { 127 if (p->scx.flags & SCX_TASK_IMMED) 128 return SCX_CAP_ENQ_IMMED; 129 return SCX_CAP_ENQ; 130 } 131 132 /* the cap @sch needs to preempt @rq's current task, 0 if none */ 133 static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq) 134 { 135 struct task_struct *curr = rq->curr; 136 137 /* a non-ext task can't be preempted by ext, own-subtree needs no cap */ 138 if (curr->sched_class != &ext_sched_class || 139 scx_is_descendant(scx_task_sched(curr), sch)) 140 return 0; 141 return SCX_CAP_PREEMPT; 142 } 143 144 /* caps implied by holding @cap */ 145 static inline u64 scx_caps_implied(u64 cap) 146 { 147 switch (cap) { 148 case SCX_CAP_PREEMPT: 149 return SCX_CAP_ENQ | SCX_CAP_ENQ_IMMED; 150 case SCX_CAP_ENQ: 151 return SCX_CAP_ENQ_IMMED; 152 } 153 return 0; 154 } 155 156 /* may @p keep running on @rq's cpu? requires baseline cpu access */ 157 static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p) 158 { 159 /* a migration-disabled task is let in without caps, keep it likewise */ 160 if (unlikely(is_migration_disabled(p))) 161 return true; 162 163 return likely(!scx_missing_caps(scx_task_sched(p), cpu_of(rq), SCX_CAP_BASE)); 164 } 165 166 #else /* CONFIG_EXT_SUB_SCHED */ 167 168 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) { return 0; } 169 static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq) { return 0; } 170 static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p) { return true; } 171 172 #endif /* CONFIG_EXT_SUB_SCHED */ 173 174 #endif /* _KERNEL_SCHED_EXT_SUB_H */ 175