xref: /linux/include/linux/sched/ext.h (revision 8197fa420ddd6cd9d3a023b63db34b798d163cc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst
4  *
5  * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
6  * Copyright (c) 2022 Tejun Heo <tj@kernel.org>
7  * Copyright (c) 2022 David Vernet <dvernet@meta.com>
8  */
9 #ifndef _LINUX_SCHED_EXT_H
10 #define _LINUX_SCHED_EXT_H
11 
12 #ifdef CONFIG_SCHED_CLASS_EXT
13 
14 #include <linux/llist.h>
15 #include <linux/rhashtable-types.h>
16 
17 enum scx_public_consts {
18 	SCX_OPS_NAME_LEN	= 128,
19 
20 	/*
21 	 * %SCX_SLICE_DFL is used to refill slices when the BPF scheduler misses
22 	 * to set the slice for a task that is selected for execution.
23 	 * %SCX_EV_REFILL_SLICE_DFL counts the number of times the default slice
24 	 * refill has been triggered.
25 	 *
26 	 * %SCX_SLICE_BYPASS is used as the slice for all tasks in the bypass
27 	 * mode. As making forward progress for all tasks is the main goal of
28 	 * the bypass mode, a shorter slice is used.
29 	 */
30 	SCX_SLICE_DFL		= 20 * 1000000,	/* 20ms */
31 	SCX_SLICE_BYPASS	=  5 * 1000000, /*  5ms */
32 	SCX_SLICE_INF		= U64_MAX,	/* infinite, implies nohz */
33 };
34 
35 /*
36  * DSQ (dispatch queue) IDs are 64bit of the format:
37  *
38  *   Bits: [63] [62 ..  0]
39  *         [ B] [   ID   ]
40  *
41  *    B: 1 for IDs for built-in DSQs, 0 for ops-created user DSQs
42  *   ID: 63 bit ID
43  *
44  * Built-in IDs:
45  *
46  *   Bits: [63] [62] [61..32] [31 ..  0]
47  *         [ 1] [ L] [   R  ] [    V   ]
48  *
49  *    1: 1 for built-in DSQs.
50  *    L: 1 for LOCAL_ON DSQ IDs, 0 for others
51  *    V: For LOCAL_ON DSQ IDs, a CPU number. For others, a pre-defined value.
52  */
53 enum scx_dsq_id_flags {
54 	SCX_DSQ_FLAG_BUILTIN	= 1LLU << 63,
55 	SCX_DSQ_FLAG_LOCAL_ON	= 1LLU << 62,
56 
57 	SCX_DSQ_INVALID		= SCX_DSQ_FLAG_BUILTIN | 0,
58 	SCX_DSQ_GLOBAL		= SCX_DSQ_FLAG_BUILTIN | 1,
59 	SCX_DSQ_LOCAL		= SCX_DSQ_FLAG_BUILTIN | 2,
60 	SCX_DSQ_BYPASS		= SCX_DSQ_FLAG_BUILTIN | 3,
61 	SCX_DSQ_REJECT		= SCX_DSQ_FLAG_BUILTIN | 4,	/* internal - see find_dsq_for_dispatch() */
62 	SCX_DSQ_RESCUE		= SCX_DSQ_FLAG_BUILTIN | 5,	/* internal - see find_dsq_for_dispatch() */
63 	SCX_DSQ_LOCAL_ON	= SCX_DSQ_FLAG_BUILTIN | SCX_DSQ_FLAG_LOCAL_ON,
64 	SCX_DSQ_LOCAL_CPU_MASK	= 0xffffffffLLU,
65 };
66 
67 struct scx_deferred_reenq_user {
68 	struct list_head	node;
69 	u64			flags;
70 };
71 
72 struct scx_dsq_pcpu {
73 	struct scx_dispatch_q	*dsq;
74 	struct scx_deferred_reenq_user deferred_reenq_user;
75 };
76 
77 /*
78  * A dispatch queue (DSQ) can be either a FIFO or p->scx.dsq_vtime ordered
79  * queue. A built-in DSQ is always a FIFO. The built-in local DSQs are used to
80  * buffer between the scheduler core and the BPF scheduler. See the
81  * documentation for more details.
82  */
83 struct scx_dispatch_q {
84 	raw_spinlock_t		lock;
85 	struct task_struct __rcu *first_task; /* lockless peek at head */
86 	struct list_head	list;	/* tasks in dispatch order */
87 	struct rb_root		priq;	/* used to order by p->scx.dsq_vtime */
88 	u32			nr;
89 	u32			seq;	/* used by BPF iter */
90 	u64			id;
91 	struct rhash_head	hash_node;
92 	struct llist_node	free_node;
93 	struct scx_sched	*sched;
94 	struct scx_dsq_pcpu __percpu *pcpu_user;
95 	struct rcu_head		rcu;
96 };
97 
98 /* sched_ext_entity.flags */
99 enum scx_ent_flags {
100 	SCX_TASK_QUEUED		= 1 << 0, /* on ext runqueue */
101 	SCX_TASK_IN_CUSTODY	= 1 << 1, /* in custody, needs ops.dequeue() when leaving */
102 	SCX_TASK_RESET_RUNNABLE_AT = 1 << 2, /* runnable_at should be reset */
103 	SCX_TASK_DEQD_FOR_SLEEP	= 1 << 3, /* last dequeue was for SLEEP */
104 	SCX_TASK_SUB_INIT	= 1 << 4, /* task being initialized for a sub sched */
105 	SCX_TASK_IMMED		= 1 << 5, /* task is on local DSQ with %SCX_ENQ_IMMED */
106 	SCX_TASK_PROTECTED	= 1 << 6, /* slice and DSQ head position protected */
107 	SCX_TASK_RUN_TRACKED	= 1 << 7, /* task is in an ops.running()/stopping() session */
108 
109 	/*
110 	 * The kernel owns the slice, currently the rescue grant. While set:
111 	 *
112 	 * - refill_task_slice_dfl() and expire_task_slice() leave the slice
113 	 *   alone.
114 	 * - Reenqueue scans skip the task.
115 	 * - put_prev_task_scx() keeps a preempted task on the local DSQ.
116 	 * - Schedulers may shorten the slice, never extend it.
117 	 */
118 	SCX_TASK_KERNEL_SLICE	= 1 << 8, /* slice owned by the kernel */
119 
120 	/*
121 	 * The multi-bit fields are packed at the top, below SCX_TASK_CURSOR, so
122 	 * that the single-bit flags stay contiguous from bit 0.
123 	 *
124 	 * Bits 25 to 27 are used to carry task state:
125 	 *
126 	 * NONE		ops.init_task() not called yet
127 	 * INIT_BEGIN	ops.init_task() in flight; see sched_ext_dead()
128 	 * INIT		ops.init_task() succeeded, but task can be cancelled
129 	 * READY	fully initialized, but not in sched_ext
130 	 * ENABLED	fully initialized and in sched_ext
131 	 * DEAD		terminal state set by sched_ext_dead()
132 	 */
133 	SCX_TASK_STATE_SHIFT	= 25,
134 	SCX_TASK_STATE_BITS	= 3,
135 	SCX_TASK_STATE_MASK	= ((1 << SCX_TASK_STATE_BITS) - 1) << SCX_TASK_STATE_SHIFT,
136 
137 	SCX_TASK_NONE		= 0 << SCX_TASK_STATE_SHIFT,
138 	SCX_TASK_INIT_BEGIN	= 1 << SCX_TASK_STATE_SHIFT,
139 	SCX_TASK_INIT		= 2 << SCX_TASK_STATE_SHIFT,
140 	SCX_TASK_READY		= 3 << SCX_TASK_STATE_SHIFT,
141 	SCX_TASK_ENABLED	= 4 << SCX_TASK_STATE_SHIFT,
142 	SCX_TASK_DEAD		= 5 << SCX_TASK_STATE_SHIFT,
143 
144 	/*
145 	 * Bits 28 to 30 are used to carry reenqueue reason. In addition to
146 	 * %SCX_ENQ_REENQ flag, ops.enqueue() can also test for
147 	 * %SCX_TASK_REENQ_REASON_NONE to distinguish reenqueues.
148 	 *
149 	 * NONE		not being reenqueued
150 	 * KFUNC	reenqueued by scx_bpf_dsq_reenq() and friends
151 	 * IMMED	reenqueued due to failed ENQ_IMMED
152 	 * PREEMPTED	preempted while running
153 	 * CAP		sub-sched cap miss, see p->scx.reenq_reason_*
154 	 * PROXY	proxy state prevented a remote DSQ transfer
155 	 */
156 	SCX_TASK_REENQ_REASON_SHIFT = 28,
157 	SCX_TASK_REENQ_REASON_BITS = 3,
158 	SCX_TASK_REENQ_REASON_MASK = ((1 << SCX_TASK_REENQ_REASON_BITS) - 1) << SCX_TASK_REENQ_REASON_SHIFT,
159 
160 	SCX_TASK_REENQ_NONE	= 0 << SCX_TASK_REENQ_REASON_SHIFT,
161 	SCX_TASK_REENQ_KFUNC	= 1 << SCX_TASK_REENQ_REASON_SHIFT,
162 	SCX_TASK_REENQ_IMMED	= 2 << SCX_TASK_REENQ_REASON_SHIFT,
163 	SCX_TASK_REENQ_PREEMPTED = 3 << SCX_TASK_REENQ_REASON_SHIFT,
164 	SCX_TASK_REENQ_CAP	= 4 << SCX_TASK_REENQ_REASON_SHIFT,
165 	SCX_TASK_REENQ_PROXY	= 5 << SCX_TASK_REENQ_REASON_SHIFT,
166 
167 	/* iteration cursor, not a task */
168 	SCX_TASK_CURSOR		= 1 << 31,
169 };
170 
171 /* scx_entity.dsq_flags */
172 enum scx_ent_dsq_flags {
173 	SCX_TASK_DSQ_ON_PRIQ	= 1 << 0, /* task is queued on the priority queue of a dsq */
174 	SCX_TASK_DSQ_REENQ	= 1 << 1, /* marked by scx_bpf_dsq_reenq_task() */
175 };
176 
177 enum scx_dsq_lnode_flags {
178 	SCX_DSQ_LNODE_ITER_CURSOR = 1 << 0,
179 
180 	/* high 16 bits can be for iter cursor flags */
181 	__SCX_DSQ_LNODE_PRIV_SHIFT = 16,
182 };
183 
184 struct scx_dsq_list_node {
185 	struct list_head	node;
186 	u32			flags;
187 	u32			priv;		/* can be used by iter cursor */
188 };
189 
190 #define INIT_DSQ_LIST_CURSOR(__cursor, __dsq, __flags)				\
191 	(struct scx_dsq_list_node) {						\
192 		.node = LIST_HEAD_INIT((__cursor).node),			\
193 		.flags = SCX_DSQ_LNODE_ITER_CURSOR | (__flags),			\
194 		.priv = READ_ONCE((__dsq)->seq),				\
195 	}
196 
197 struct scx_sched;
198 
199 /*
200  * The following is embedded in task_struct and contains all fields necessary
201  * for a task to be scheduled by SCX.
202  */
203 struct sched_ext_entity {
204 #ifdef CONFIG_CGROUPS
205 	/*
206 	 * Associated scx_sched. Updated either during fork or while holding
207 	 * both p->pi_lock and rq lock.
208 	 */
209 	struct scx_sched __rcu	*sched;
210 #endif
211 	struct scx_dispatch_q	*dsq;
212 	atomic_long_t		ops_state;
213 	u64			ddsp_dsq_id;
214 	u64			ddsp_enq_flags;
215 	u64			ddsp_slice;
216 	u64			ddsp_vtime;
217 	struct scx_dsq_list_node dsq_list;	/* dispatch order */
218 	u32			dsq_seq;
219 	u32			dsq_flags;	/* protected by DSQ lock */
220 
221 	/*
222 	 * Used to order tasks when dispatching to the vtime-ordered priority
223 	 * queue of a dsq. This is usually set through
224 	 * scx_bpf_dsq_insert_vtime() but can also be modified directly by the
225 	 * BPF scheduler. Modifying it while a task is queued on a dsq may
226 	 * mangle the ordering and is not recommended. Kept next to @dsq_priq
227 	 * as rbtree insertion reads both on every visited node.
228 	 */
229 	u64			dsq_vtime;
230 	struct rb_node		dsq_priq;	/* p->scx.dsq_vtime order */
231 	u32			flags;		/* protected by rq lock */
232 	u32			weight;
233 	u32			reenq_cnt;	/* reenqueues since last run */
234 	s32			sticky_cpu;
235 	s32			holding_cpu;
236 	s32			selected_cpu;
237 	s32			runnable_cpu;	/* cpu @p is runnable on, -1 if not */
238 	u32			ops_qseq;	/* protected by rq lock */
239 	struct task_struct	*kf_tasks[2];	/* see SCX_CALL_OP_TASK() */
240 
241 	struct list_head	runnable_node;	/* rq->scx.runnable_list */
242 	unsigned long		runnable_at;
243 
244 #ifdef CONFIG_EXT_SUB_SCHED
245 	unsigned long		rescue_at;	/* queued on a rescue DSQ at, jiffies */
246 #endif
247 
248 	/*
249 	 * Unique non-zero task ID assigned at fork. Persists across exec and
250 	 * is never reused. Lets BPF schedulers identify tasks without storing
251 	 * kernel pointers - arena-backed schedulers being one example. See
252 	 * scx_bpf_tid_to_task().
253 	 */
254 	u64			tid;
255 	struct rhash_head	tid_hash_node;	/* see SCX_OPS_TID_TO_TASK */
256 
257 	/* BPF scheduler modifiable fields, along with @dsq_vtime above */
258 
259 	/*
260 	 * Runtime budget in nsecs - how long the task may hold its cpu. Owned
261 	 * by the task's scheduler. Set it when enqueuing via
262 	 * scx_bpf_dsq_insert(), or otherwise via scx_bpf_task_set_slice().
263 	 * Automatically decreased as the task executes. On depletion a
264 	 * scheduling event is triggered.
265 	 *
266 	 * This value is cleared to zero if the task is preempted by
267 	 * %SCX_KICK_PREEMPT and shouldn't be used to determine how long the
268 	 * task ran. Use p->se.sum_exec_runtime instead.
269 	 */
270 	u64			slice;
271 
272 	/*
273 	 * Out-of-band slice request from scx_bpf_task_set_slice() when the
274 	 * caller does not hold the rq lock, applied under the rq lock at the
275 	 * next slice consideration. One atomic64 packs the pending flag, the
276 	 * issuing sch's id, and the requested slice. See scx_slice_oob_consts.
277 	 */
278 	atomic64_t		slice_oob;
279 
280 	/*
281 	 * Sub-sched cap rejected reenq context, valid only while
282 	 * %SCX_TASK_REENQ_CAP is set. @reenq_reason_caps is the SCX_CAP_* bits
283 	 * that were needed but missing. @reenq_reason_cid is the target cid.
284 	 */
285 	u64			reenq_reason_caps;
286 	s32			reenq_reason_cid;
287 
288 	/*
289 	 * If set, reject future sched_setscheduler(2) calls updating the policy
290 	 * to %SCHED_EXT with -%EACCES.
291 	 *
292 	 * Can be set from ops.init_task() while the BPF scheduler is being
293 	 * loaded. If set and the task's policy is already %SCHED_EXT, the
294 	 * task's policy is rejected and forcefully reverted to %SCHED_NORMAL.
295 	 * The number of such events are reported through
296 	 * /sys/kernel/sched_ext/nr_rejected. Setting this flag from any other
297 	 * ops.init_task() invocation, such as during fork, fails the scheduler.
298 	 */
299 	bool			disallow;	/* reject switching into SCX */
300 
301 	/*
302 	 * If set, depletion of this task's slice at the scheduler tick requests
303 	 * lazy instead of immediate rescheduling. Initialized from
304 	 * %SCX_OPS_LAZY_RESCHED immediately before ops.enable() and may be
305 	 * modified afterwards with scx_bpf_task_set_lazy_resched().
306 	 */
307 	bool			lazy_resched;
308 
309 	/* cold fields */
310 #ifdef CONFIG_EXT_GROUP_SCHED
311 	struct cgroup		*cgrp_moving_from;
312 #endif
313 	struct list_head	tasks_node;
314 };
315 
316 void sched_ext_dead(struct task_struct *p);
317 void print_scx_info(const char *log_lvl, struct task_struct *p);
318 void scx_softlockup(u32 dur_s);
319 bool scx_hardlockup(int cpu);
320 bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask);
321 
322 #else	/* !CONFIG_SCHED_CLASS_EXT */
323 
324 static inline void sched_ext_dead(struct task_struct *p) {}
325 static inline void print_scx_info(const char *log_lvl, struct task_struct *p) {}
326 static inline void scx_softlockup(u32 dur_s) {}
327 static inline bool scx_hardlockup(int cpu) { return false; }
328 static inline bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask) { return false; }
329 
330 #endif	/* CONFIG_SCHED_CLASS_EXT */
331 
332 struct scx_task_group {
333 #ifdef CONFIG_EXT_GROUP_SCHED
334 	/*
335 	 * The sched this tg is on, NULL if none. SCX_TG_INITED tracks whether
336 	 * ops.cgroup_init() succeeded on it. When a child sched exits and its
337 	 * tgs move to the parent, a failed init leaves the tg on the parent
338 	 * with INITED clear (see scx_cgroup_return_subtree()).
339 	 *
340 	 * This is tracked separately from cgrp->scx_sched because the tg
341 	 * hierarchy can diverge from the cgroup2 hierarchy in both lifetime and
342 	 * shape. A tg stays online past its cgroup's removal while the
343 	 * cgrp->scx_sched rewrites visit only live cgroups, leaving a removed
344 	 * cgroup's pointer stale. The cpu controller can also be mounted on
345 	 * cgroup1.
346 	 */
347 	struct scx_sched	*sched;
348 
349 	u32			flags;		/* SCX_TG_* */
350 	u32			weight;
351 	u64			bw_period_us;
352 	u64			bw_quota_us;
353 	u64			bw_burst_us;
354 	bool			sched_idle;
355 #endif
356 };
357 
358 #endif	/* _LINUX_SCHED_EXT_H */
359