xref: /linux/kernel/sched/ext/sub.h (revision 8946dbd3aa91acfb75b1633859290ba248e313b2)
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 /* a dying sub's hot-path influence ends in scx_sched_free_rcu_work() */
48 static inline void scx_dec_has_subs(struct scx_sched *sch)
49 {
50 	if (sch->level)
51 		static_branch_dec(&__scx_has_subs);
52 }
53 
54 #else	/* CONFIG_EXT_SUB_SCHED */
55 
56 static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; }
57 static inline struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root) { return NULL; }
58 static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {}
59 static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; }
60 static inline const char *sch_cgrp_path(struct scx_sched *sch) { return "/"; }
61 static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {}
62 static inline void drain_descendants(struct scx_sched *sch) { }
63 static inline void scx_sub_disable(struct scx_sched *sch) { }
64 static inline void scx_free_pshards(struct scx_sched *sch) {}
65 static inline s32 scx_alloc_pshards(struct scx_sched *sch) { return 0; }
66 static inline void scx_init_root_caps(struct scx_sched *sch) {}
67 static inline void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev) {}
68 static inline void scx_unbypass_replay_ecaps(struct rq *rq, struct scx_sched *sch) {}
69 static inline void scx_online_ecaps(struct rq *rq) {}
70 static inline void scx_offline_ecaps(struct rq *rq) {}
71 static inline void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu) {}
72 static inline void scx_discard_stale_ecaps_syncs(void) {}
73 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; }
74 static inline bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p) { return false; }
75 static inline void scx_reenq_reject(struct rq *rq) {}
76 static inline void scx_dec_has_subs(struct scx_sched *sch) {}
77 
78 #endif	/* CONFIG_EXT_SUB_SCHED */
79 
80 /**
81  * scx_for_each_descendant_pre - pre-order walk of a sched's descendants
82  * @pos: iteration cursor
83  * @root: sched to walk the descendants of
84  *
85  * Walk @root's descendants. @root is included in the iteration and the first
86  * node to be visited. Must be called with scx_enable_mutex, scx_sched_lock, or
87  * RCU read lock.
88  */
89 #define scx_for_each_descendant_pre(pos, root)					\
90 	for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos);		\
91 	     (pos) = scx_next_descendant_pre((pos), (root)))
92 
93 #ifdef CONFIG_EXT_SUB_SCHED
94 
95 /**
96  * scx_missing_caps - The caps in @needed that @sch lacks on @cpu
97  * @sch: sched to test
98  * @cpu: cpu to test on
99  * @needed: bitmask of SCX_CAP_* values
100  *
101  * Return the caps in @needed that @sch lacks for @cpu, 0 if it holds them all.
102  */
103 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed)
104 {
105 	u64 ecaps;
106 
107 	/* no sub-scheds, no missing caps */
108 	if (!scx_has_subs())
109 		return 0;
110 
111 	/* root holds every cap on every cpu */
112 	if (!sch->level)
113 		return 0;
114 
115 	ecaps = READ_ONCE(per_cpu_ptr(sch->pcpu, cpu)->ecaps);
116 
117 	return needed & ~ecaps;
118 }
119 
120 /*
121  * Cap semantics: which caps an action requires, and which caps a cap implies.
122  * Keep all such mappings collected here.
123  */
124 
125 /* map @enq_flags to the SCX_CAP_* bit required for the local-DSQ insert */
126 static inline u64 scx_caps_for_enq(u64 enq_flags)
127 {
128 	/* a restored task must be put into the local DSQ regardless of caps */
129 	if (enq_flags & SCX_ENQ_IGNORE_CAPS)
130 		return 0;
131 	if (enq_flags & SCX_ENQ_IMMED)
132 		return SCX_CAP_ENQ_IMMED;
133 	return SCX_CAP_ENQ;
134 }
135 
136 /* map queued @p to the SCX_CAP_* bit required to stay on its local DSQ */
137 static inline u64 scx_caps_for_task(struct task_struct *p)
138 {
139 	if (p->scx.flags & SCX_TASK_IMMED)
140 		return SCX_CAP_ENQ_IMMED;
141 	return SCX_CAP_ENQ;
142 }
143 
144 /* the cap @sch needs to preempt @rq's current task, 0 if none */
145 static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq)
146 {
147 	struct task_struct *curr = rq->curr;
148 
149 	/* a non-ext task can't be preempted by ext, own-subtree needs no cap */
150 	if (curr->sched_class != &ext_sched_class ||
151 	    scx_is_descendant(scx_task_sched(curr), sch))
152 		return 0;
153 	return SCX_CAP_PREEMPT;
154 }
155 
156 /* caps implied by holding @cap */
157 static inline u64 scx_caps_implied(u64 cap)
158 {
159 	switch (cap) {
160 	case SCX_CAP_PREEMPT:
161 		return SCX_CAP_ENQ | SCX_CAP_ENQ_IMMED;
162 	case SCX_CAP_ENQ:
163 		return SCX_CAP_ENQ_IMMED;
164 	}
165 	return 0;
166 }
167 
168 /* may @p keep running on @rq's cpu? requires baseline cpu access */
169 static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p)
170 {
171 	if (!scx_has_subs())
172 		return true;
173 
174 	/* a migration-disabled task is let in without caps, keep it likewise */
175 	if (unlikely(is_migration_disabled(p)))
176 		return true;
177 
178 	return likely(!scx_missing_caps(scx_task_sched(p), cpu_of(rq), SCX_CAP_BASE));
179 }
180 
181 #else	/* CONFIG_EXT_SUB_SCHED */
182 
183 static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) { return 0; }
184 static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq) { return 0; }
185 static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p) { return true; }
186 
187 #endif	/* CONFIG_EXT_SUB_SCHED */
188 
189 #endif /* _KERNEL_SCHED_EXT_SUB_H */
190