xref: /linux/kernel/sched/ext/internal.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1*bba2c361STejun Heo /* SPDX-License-Identifier: GPL-2.0 */
2*bba2c361STejun Heo /*
3*bba2c361STejun Heo  * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst
4*bba2c361STejun Heo  *
5*bba2c361STejun Heo  * Copyright (c) 2025 Meta Platforms, Inc. and affiliates.
6*bba2c361STejun Heo  * Copyright (c) 2025 Tejun Heo <tj@kernel.org>
7*bba2c361STejun Heo  */
8*bba2c361STejun Heo #define SCX_OP_IDX(op)		(offsetof(struct sched_ext_ops, op) / sizeof(void (*)(void)))
9*bba2c361STejun Heo #define SCX_MOFF_IDX(moff)	((moff) / sizeof(void (*)(void)))
10*bba2c361STejun Heo 
11*bba2c361STejun Heo enum scx_exit_kind {
12*bba2c361STejun Heo 	SCX_EXIT_NONE,
13*bba2c361STejun Heo 	SCX_EXIT_DONE,
14*bba2c361STejun Heo 
15*bba2c361STejun Heo 	SCX_EXIT_UNREG = 64,	/* user-space initiated unregistration */
16*bba2c361STejun Heo 	SCX_EXIT_UNREG_BPF,	/* BPF-initiated unregistration */
17*bba2c361STejun Heo 	SCX_EXIT_UNREG_KERN,	/* kernel-initiated unregistration */
18*bba2c361STejun Heo 	SCX_EXIT_SYSRQ,		/* requested by 'S' sysrq */
19*bba2c361STejun Heo 	SCX_EXIT_PARENT,	/* parent exiting */
20*bba2c361STejun Heo 
21*bba2c361STejun Heo 	SCX_EXIT_ERROR = 1024,	/* runtime error, error msg contains details */
22*bba2c361STejun Heo 	SCX_EXIT_ERROR_BPF,	/* ERROR but triggered through scx_bpf_error() */
23*bba2c361STejun Heo 	SCX_EXIT_ERROR_STALL,	/* watchdog detected stalled runnable tasks */
24*bba2c361STejun Heo };
25*bba2c361STejun Heo 
26*bba2c361STejun Heo /*
27*bba2c361STejun Heo  * An exit code can be specified when exiting with scx_bpf_exit() or scx_exit(),
28*bba2c361STejun Heo  * corresponding to exit_kind UNREG_BPF and UNREG_KERN respectively. The codes
29*bba2c361STejun Heo  * are 64bit of the format:
30*bba2c361STejun Heo  *
31*bba2c361STejun Heo  *   Bits: [63  ..  48 47   ..  32 31 .. 0]
32*bba2c361STejun Heo  *         [ SYS ACT ] [ SYS RSN ] [ USR  ]
33*bba2c361STejun Heo  *
34*bba2c361STejun Heo  *   SYS ACT: System-defined exit actions
35*bba2c361STejun Heo  *   SYS RSN: System-defined exit reasons
36*bba2c361STejun Heo  *   USR    : User-defined exit codes and reasons
37*bba2c361STejun Heo  *
38*bba2c361STejun Heo  * Using the above, users may communicate intention and context by ORing system
39*bba2c361STejun Heo  * actions and/or system reasons with a user-defined exit code.
40*bba2c361STejun Heo  */
41*bba2c361STejun Heo enum scx_exit_code {
42*bba2c361STejun Heo 	/* Reasons */
43*bba2c361STejun Heo 	SCX_ECODE_RSN_HOTPLUG	= 1LLU << 32,
44*bba2c361STejun Heo 	SCX_ECODE_RSN_CGROUP_OFFLINE = 2LLU << 32,
45*bba2c361STejun Heo 
46*bba2c361STejun Heo 	/* Actions */
47*bba2c361STejun Heo 	SCX_ECODE_ACT_RESTART	= 1LLU << 48,
48*bba2c361STejun Heo };
49*bba2c361STejun Heo 
50*bba2c361STejun Heo enum scx_exit_flags {
51*bba2c361STejun Heo 	/*
52*bba2c361STejun Heo 	 * ops.exit() may be called even if the loading failed before ops.init()
53*bba2c361STejun Heo 	 * finishes successfully. This is because ops.exit() allows rich exit
54*bba2c361STejun Heo 	 * info communication. The following flag indicates whether ops.init()
55*bba2c361STejun Heo 	 * finished successfully.
56*bba2c361STejun Heo 	 */
57*bba2c361STejun Heo 	SCX_EFLAG_INITIALIZED   = 1LLU << 0,
58*bba2c361STejun Heo };
59*bba2c361STejun Heo 
60*bba2c361STejun Heo /*
61*bba2c361STejun Heo  * scx_exit_info is passed to ops.exit() to describe why the BPF scheduler is
62*bba2c361STejun Heo  * being disabled.
63*bba2c361STejun Heo  */
64*bba2c361STejun Heo struct scx_exit_info {
65*bba2c361STejun Heo 	/* %SCX_EXIT_* - broad category of the exit reason */
66*bba2c361STejun Heo 	enum scx_exit_kind	kind;
67*bba2c361STejun Heo 
68*bba2c361STejun Heo 	/*
69*bba2c361STejun Heo 	 * CPU that initiated the exit, valid once @kind has been set.
70*bba2c361STejun Heo 	 * Negative if the exit path didn't identify a CPU.
71*bba2c361STejun Heo 	 */
72*bba2c361STejun Heo 	s32			exit_cpu;
73*bba2c361STejun Heo 
74*bba2c361STejun Heo 	/* exit code if gracefully exiting */
75*bba2c361STejun Heo 	s64			exit_code;
76*bba2c361STejun Heo 
77*bba2c361STejun Heo 	/* %SCX_EFLAG_* */
78*bba2c361STejun Heo 	u64			flags;
79*bba2c361STejun Heo 
80*bba2c361STejun Heo 	/* textual representation of the above */
81*bba2c361STejun Heo 	const char		*reason;
82*bba2c361STejun Heo 
83*bba2c361STejun Heo 	/* backtrace if exiting due to an error */
84*bba2c361STejun Heo 	unsigned long		*bt;
85*bba2c361STejun Heo 	u32			bt_len;
86*bba2c361STejun Heo 
87*bba2c361STejun Heo 	/* informational message */
88*bba2c361STejun Heo 	char			*msg;
89*bba2c361STejun Heo 
90*bba2c361STejun Heo 	/* debug dump */
91*bba2c361STejun Heo 	char			*dump;
92*bba2c361STejun Heo };
93*bba2c361STejun Heo 
94*bba2c361STejun Heo /* sched_ext_ops.flags */
95*bba2c361STejun Heo enum scx_ops_flags {
96*bba2c361STejun Heo 	/*
97*bba2c361STejun Heo 	 * Keep built-in idle tracking even if ops.update_idle() is implemented.
98*bba2c361STejun Heo 	 */
99*bba2c361STejun Heo 	SCX_OPS_KEEP_BUILTIN_IDLE	= 1LLU << 0,
100*bba2c361STejun Heo 
101*bba2c361STejun Heo 	/*
102*bba2c361STejun Heo 	 * By default, if there are no other task to run on the CPU, ext core
103*bba2c361STejun Heo 	 * keeps running the current task even after its slice expires. If this
104*bba2c361STejun Heo 	 * flag is specified, such tasks are passed to ops.enqueue() with
105*bba2c361STejun Heo 	 * %SCX_ENQ_LAST. See the comment above %SCX_ENQ_LAST for more info.
106*bba2c361STejun Heo 	 */
107*bba2c361STejun Heo 	SCX_OPS_ENQ_LAST		= 1LLU << 1,
108*bba2c361STejun Heo 
109*bba2c361STejun Heo 	/*
110*bba2c361STejun Heo 	 * An exiting task may schedule after PF_EXITING is set. In such cases,
111*bba2c361STejun Heo 	 * bpf_task_from_pid() may not be able to find the task and if the BPF
112*bba2c361STejun Heo 	 * scheduler depends on pid lookup for dispatching, the task will be
113*bba2c361STejun Heo 	 * lost leading to various issues including RCU grace period stalls.
114*bba2c361STejun Heo 	 *
115*bba2c361STejun Heo 	 * To mask this problem, by default, unhashed tasks are automatically
116*bba2c361STejun Heo 	 * dispatched to the local DSQ on enqueue. If the BPF scheduler doesn't
117*bba2c361STejun Heo 	 * depend on pid lookups and wants to handle these tasks directly, the
118*bba2c361STejun Heo 	 * following flag can be used. With %SCX_OPS_TID_TO_TASK,
119*bba2c361STejun Heo 	 * scx_bpf_tid_to_task() can find exiting tasks reliably.
120*bba2c361STejun Heo 	 */
121*bba2c361STejun Heo 	SCX_OPS_ENQ_EXITING		= 1LLU << 2,
122*bba2c361STejun Heo 
123*bba2c361STejun Heo 	/*
124*bba2c361STejun Heo 	 * If set, only tasks with policy set to SCHED_EXT are attached to
125*bba2c361STejun Heo 	 * sched_ext. If clear, SCHED_NORMAL tasks are also included.
126*bba2c361STejun Heo 	 */
127*bba2c361STejun Heo 	SCX_OPS_SWITCH_PARTIAL		= 1LLU << 3,
128*bba2c361STejun Heo 
129*bba2c361STejun Heo 	/*
130*bba2c361STejun Heo 	 * A migration disabled task can only execute on its current CPU. By
131*bba2c361STejun Heo 	 * default, such tasks are automatically put on the CPU's local DSQ with
132*bba2c361STejun Heo 	 * the default slice on enqueue. If this ops flag is set, they also go
133*bba2c361STejun Heo 	 * through ops.enqueue().
134*bba2c361STejun Heo 	 *
135*bba2c361STejun Heo 	 * A migration disabled task never invokes ops.select_cpu() as it can
136*bba2c361STejun Heo 	 * only select the current CPU. Also, p->cpus_ptr will only contain its
137*bba2c361STejun Heo 	 * current CPU while p->nr_cpus_allowed keeps tracking p->user_cpus_ptr
138*bba2c361STejun Heo 	 * and thus may disagree with cpumask_weight(p->cpus_ptr).
139*bba2c361STejun Heo 	 */
140*bba2c361STejun Heo 	SCX_OPS_ENQ_MIGRATION_DISABLED	= 1LLU << 4,
141*bba2c361STejun Heo 
142*bba2c361STejun Heo 	/*
143*bba2c361STejun Heo 	 * Queued wakeup (ttwu_queue) is a wakeup optimization that invokes
144*bba2c361STejun Heo 	 * ops.enqueue() on the ops.select_cpu() selected or the wakee's
145*bba2c361STejun Heo 	 * previous CPU via IPI (inter-processor interrupt) to reduce cacheline
146*bba2c361STejun Heo 	 * transfers. When this optimization is enabled, ops.select_cpu() is
147*bba2c361STejun Heo 	 * skipped in some cases (when racing against the wakee switching out).
148*bba2c361STejun Heo 	 * As the BPF scheduler may depend on ops.select_cpu() being invoked
149*bba2c361STejun Heo 	 * during wakeups, queued wakeup is disabled by default.
150*bba2c361STejun Heo 	 *
151*bba2c361STejun Heo 	 * If this ops flag is set, queued wakeup optimization is enabled and
152*bba2c361STejun Heo 	 * the BPF scheduler must be able to handle ops.enqueue() invoked on the
153*bba2c361STejun Heo 	 * wakee's CPU without preceding ops.select_cpu() even for tasks which
154*bba2c361STejun Heo 	 * may be executed on multiple CPUs.
155*bba2c361STejun Heo 	 */
156*bba2c361STejun Heo 	SCX_OPS_ALLOW_QUEUED_WAKEUP	= 1LLU << 5,
157*bba2c361STejun Heo 
158*bba2c361STejun Heo 	/*
159*bba2c361STejun Heo 	 * If set, enable per-node idle cpumasks. If clear, use a single global
160*bba2c361STejun Heo 	 * flat idle cpumask.
161*bba2c361STejun Heo 	 */
162*bba2c361STejun Heo 	SCX_OPS_BUILTIN_IDLE_PER_NODE	= 1LLU << 6,
163*bba2c361STejun Heo 
164*bba2c361STejun Heo 	/*
165*bba2c361STejun Heo 	 * If set, %SCX_ENQ_IMMED is assumed to be set on all local DSQ
166*bba2c361STejun Heo 	 * enqueues.
167*bba2c361STejun Heo 	 */
168*bba2c361STejun Heo 	SCX_OPS_ALWAYS_ENQ_IMMED	= 1LLU << 7,
169*bba2c361STejun Heo 
170*bba2c361STejun Heo 	/*
171*bba2c361STejun Heo 	 * Maintain a mapping from p->scx.tid to task_struct so the BPF
172*bba2c361STejun Heo 	 * scheduler can recover task pointers from stored tids via
173*bba2c361STejun Heo 	 * scx_bpf_tid_to_task().
174*bba2c361STejun Heo 	 *
175*bba2c361STejun Heo 	 * Only the root scheduler turns this on. A sub-sched may set the flag
176*bba2c361STejun Heo 	 * to declare a dependency on the lookup; if the root scheduler hasn't
177*bba2c361STejun Heo 	 * enabled it, attaching the sub-sched is rejected.
178*bba2c361STejun Heo 	 */
179*bba2c361STejun Heo 	SCX_OPS_TID_TO_TASK		= 1LLU << 8,
180*bba2c361STejun Heo 
181*bba2c361STejun Heo 	SCX_OPS_ALL_FLAGS		= SCX_OPS_KEEP_BUILTIN_IDLE |
182*bba2c361STejun Heo 					  SCX_OPS_ENQ_LAST |
183*bba2c361STejun Heo 					  SCX_OPS_ENQ_EXITING |
184*bba2c361STejun Heo 					  SCX_OPS_ENQ_MIGRATION_DISABLED |
185*bba2c361STejun Heo 					  SCX_OPS_ALLOW_QUEUED_WAKEUP |
186*bba2c361STejun Heo 					  SCX_OPS_SWITCH_PARTIAL |
187*bba2c361STejun Heo 					  SCX_OPS_BUILTIN_IDLE_PER_NODE |
188*bba2c361STejun Heo 					  SCX_OPS_ALWAYS_ENQ_IMMED |
189*bba2c361STejun Heo 					  SCX_OPS_TID_TO_TASK,
190*bba2c361STejun Heo 
191*bba2c361STejun Heo 	/* high 8 bits are internal, don't include in SCX_OPS_ALL_FLAGS */
192*bba2c361STejun Heo 	__SCX_OPS_INTERNAL_MASK		= 0xffLLU << 56,
193*bba2c361STejun Heo 
194*bba2c361STejun Heo 	SCX_OPS_HAS_CPU_PREEMPT		= 1LLU << 56,
195*bba2c361STejun Heo };
196*bba2c361STejun Heo 
197*bba2c361STejun Heo /* argument container for ops.init_task() */
198*bba2c361STejun Heo struct scx_init_task_args {
199*bba2c361STejun Heo 	/*
200*bba2c361STejun Heo 	 * Set if ops.init_task() is being invoked on the fork path, as opposed
201*bba2c361STejun Heo 	 * to the scheduler transition path.
202*bba2c361STejun Heo 	 */
203*bba2c361STejun Heo 	bool			fork;
204*bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED
205*bba2c361STejun Heo 	/* the cgroup the task is joining */
206*bba2c361STejun Heo 	struct cgroup		*cgroup;
207*bba2c361STejun Heo #endif
208*bba2c361STejun Heo };
209*bba2c361STejun Heo 
210*bba2c361STejun Heo /* argument container for ops.exit_task() */
211*bba2c361STejun Heo struct scx_exit_task_args {
212*bba2c361STejun Heo 	/* Whether the task exited before running on sched_ext. */
213*bba2c361STejun Heo 	bool cancelled;
214*bba2c361STejun Heo };
215*bba2c361STejun Heo 
216*bba2c361STejun Heo /* argument container for ops.cgroup_init() */
217*bba2c361STejun Heo struct scx_cgroup_init_args {
218*bba2c361STejun Heo 	/* the weight of the cgroup [1..10000] */
219*bba2c361STejun Heo 	u32			weight;
220*bba2c361STejun Heo 
221*bba2c361STejun Heo 	/* bandwidth control parameters from cpu.max and cpu.max.burst */
222*bba2c361STejun Heo 	u64			bw_period_us;
223*bba2c361STejun Heo 	u64			bw_quota_us;
224*bba2c361STejun Heo 	u64			bw_burst_us;
225*bba2c361STejun Heo };
226*bba2c361STejun Heo 
227*bba2c361STejun Heo enum scx_cpu_preempt_reason {
228*bba2c361STejun Heo 	/* next task is being scheduled by &sched_class_rt */
229*bba2c361STejun Heo 	SCX_CPU_PREEMPT_RT,
230*bba2c361STejun Heo 	/* next task is being scheduled by &sched_class_dl */
231*bba2c361STejun Heo 	SCX_CPU_PREEMPT_DL,
232*bba2c361STejun Heo 	/* next task is being scheduled by &sched_class_stop */
233*bba2c361STejun Heo 	SCX_CPU_PREEMPT_STOP,
234*bba2c361STejun Heo 	/* unknown reason for SCX being preempted */
235*bba2c361STejun Heo 	SCX_CPU_PREEMPT_UNKNOWN,
236*bba2c361STejun Heo };
237*bba2c361STejun Heo 
238*bba2c361STejun Heo /*
239*bba2c361STejun Heo  * Argument container for ops.cpu_acquire(). Currently empty, but may be
240*bba2c361STejun Heo  * expanded in the future.
241*bba2c361STejun Heo  */
242*bba2c361STejun Heo struct scx_cpu_acquire_args {};
243*bba2c361STejun Heo 
244*bba2c361STejun Heo /* argument container for ops.cpu_release() */
245*bba2c361STejun Heo struct scx_cpu_release_args {
246*bba2c361STejun Heo 	/* the reason the CPU was preempted */
247*bba2c361STejun Heo 	enum scx_cpu_preempt_reason reason;
248*bba2c361STejun Heo 
249*bba2c361STejun Heo 	/* the task that's going to be scheduled on the CPU */
250*bba2c361STejun Heo 	struct task_struct	*task;
251*bba2c361STejun Heo };
252*bba2c361STejun Heo 
253*bba2c361STejun Heo /* informational context provided to dump operations */
254*bba2c361STejun Heo struct scx_dump_ctx {
255*bba2c361STejun Heo 	enum scx_exit_kind	kind;
256*bba2c361STejun Heo 	s64			exit_code;
257*bba2c361STejun Heo 	const char		*reason;
258*bba2c361STejun Heo 	u64			at_ns;
259*bba2c361STejun Heo 	u64			at_jiffies;
260*bba2c361STejun Heo };
261*bba2c361STejun Heo 
262*bba2c361STejun Heo /* argument container for ops.sub_attach() */
263*bba2c361STejun Heo struct scx_sub_attach_args {
264*bba2c361STejun Heo 	struct sched_ext_ops	*ops;
265*bba2c361STejun Heo 	char			*cgroup_path;
266*bba2c361STejun Heo };
267*bba2c361STejun Heo 
268*bba2c361STejun Heo /* argument container for ops.sub_detach() */
269*bba2c361STejun Heo struct scx_sub_detach_args {
270*bba2c361STejun Heo 	struct sched_ext_ops	*ops;
271*bba2c361STejun Heo 	char			*cgroup_path;
272*bba2c361STejun Heo };
273*bba2c361STejun Heo 
274*bba2c361STejun Heo /**
275*bba2c361STejun Heo  * struct sched_ext_ops - Operation table for BPF scheduler implementation
276*bba2c361STejun Heo  *
277*bba2c361STejun Heo  * A BPF scheduler can implement an arbitrary scheduling policy by
278*bba2c361STejun Heo  * implementing and loading operations in this table. Note that a userland
279*bba2c361STejun Heo  * scheduling policy can also be implemented using the BPF scheduler
280*bba2c361STejun Heo  * as a shim layer.
281*bba2c361STejun Heo  */
282*bba2c361STejun Heo struct sched_ext_ops {
283*bba2c361STejun Heo 	/**
284*bba2c361STejun Heo 	 * @select_cpu: Pick the target CPU for a task which is being woken up
285*bba2c361STejun Heo 	 * @p: task being woken up
286*bba2c361STejun Heo 	 * @prev_cpu: the cpu @p was on before sleeping
287*bba2c361STejun Heo 	 * @wake_flags: SCX_WAKE_*
288*bba2c361STejun Heo 	 *
289*bba2c361STejun Heo 	 * Decision made here isn't final. @p may be moved to any CPU while it
290*bba2c361STejun Heo 	 * is getting dispatched for execution later. However, as @p is not on
291*bba2c361STejun Heo 	 * the rq at this point, getting the eventual execution CPU right here
292*bba2c361STejun Heo 	 * saves a small bit of overhead down the line.
293*bba2c361STejun Heo 	 *
294*bba2c361STejun Heo 	 * If an idle CPU is returned, the CPU is kicked and will try to
295*bba2c361STejun Heo 	 * dispatch. While an explicit custom mechanism can be added,
296*bba2c361STejun Heo 	 * select_cpu() serves as the default way to wake up idle CPUs.
297*bba2c361STejun Heo 	 *
298*bba2c361STejun Heo 	 * @p may be inserted into a DSQ directly by calling
299*bba2c361STejun Heo 	 * scx_bpf_dsq_insert(). If so, the ops.enqueue() will be skipped.
300*bba2c361STejun Heo 	 * Directly inserting into %SCX_DSQ_LOCAL will put @p in the local DSQ
301*bba2c361STejun Heo 	 * of the CPU returned by this operation.
302*bba2c361STejun Heo 	 *
303*bba2c361STejun Heo 	 * Note that select_cpu() is never called for tasks that can only run
304*bba2c361STejun Heo 	 * on a single CPU or tasks with migration disabled, as they don't have
305*bba2c361STejun Heo 	 * the option to select a different CPU. See select_task_rq() for
306*bba2c361STejun Heo 	 * details.
307*bba2c361STejun Heo 	 */
308*bba2c361STejun Heo 	s32 (*select_cpu)(struct task_struct *p, s32 prev_cpu, u64 wake_flags);
309*bba2c361STejun Heo 
310*bba2c361STejun Heo 	/**
311*bba2c361STejun Heo 	 * @enqueue: Enqueue a task on the BPF scheduler
312*bba2c361STejun Heo 	 * @p: task being enqueued
313*bba2c361STejun Heo 	 * @enq_flags: %SCX_ENQ_*
314*bba2c361STejun Heo 	 *
315*bba2c361STejun Heo 	 * @p is ready to run. Insert directly into a DSQ by calling
316*bba2c361STejun Heo 	 * scx_bpf_dsq_insert() or enqueue on the BPF scheduler. If not directly
317*bba2c361STejun Heo 	 * inserted, the bpf scheduler owns @p and if it fails to dispatch @p,
318*bba2c361STejun Heo 	 * the task will stall.
319*bba2c361STejun Heo 	 *
320*bba2c361STejun Heo 	 * If @p was inserted into a DSQ from ops.select_cpu(), this callback is
321*bba2c361STejun Heo 	 * skipped.
322*bba2c361STejun Heo 	 */
323*bba2c361STejun Heo 	void (*enqueue)(struct task_struct *p, u64 enq_flags);
324*bba2c361STejun Heo 
325*bba2c361STejun Heo 	/**
326*bba2c361STejun Heo 	 * @dequeue: Remove a task from the BPF scheduler
327*bba2c361STejun Heo 	 * @p: task being dequeued
328*bba2c361STejun Heo 	 * @deq_flags: %SCX_DEQ_*
329*bba2c361STejun Heo 	 *
330*bba2c361STejun Heo 	 * Remove @p from the BPF scheduler. This is usually called to isolate
331*bba2c361STejun Heo 	 * the task while updating its scheduling properties (e.g. priority).
332*bba2c361STejun Heo 	 *
333*bba2c361STejun Heo 	 * The ext core keeps track of whether the BPF side owns a given task or
334*bba2c361STejun Heo 	 * not and can gracefully ignore spurious dispatches from BPF side,
335*bba2c361STejun Heo 	 * which makes it safe to not implement this method. However, depending
336*bba2c361STejun Heo 	 * on the scheduling logic, this can lead to confusing behaviors - e.g.
337*bba2c361STejun Heo 	 * scheduling position not being updated across a priority change.
338*bba2c361STejun Heo 	 */
339*bba2c361STejun Heo 	void (*dequeue)(struct task_struct *p, u64 deq_flags);
340*bba2c361STejun Heo 
341*bba2c361STejun Heo 	/**
342*bba2c361STejun Heo 	 * @dispatch: Dispatch tasks from the BPF scheduler and/or user DSQs
343*bba2c361STejun Heo 	 * @cpu: CPU to dispatch tasks for
344*bba2c361STejun Heo 	 * @prev: previous task being switched out
345*bba2c361STejun Heo 	 *
346*bba2c361STejun Heo 	 * Called when a CPU's local dsq is empty. The operation should dispatch
347*bba2c361STejun Heo 	 * one or more tasks from the BPF scheduler into the DSQs using
348*bba2c361STejun Heo 	 * scx_bpf_dsq_insert() and/or move from user DSQs into the local DSQ
349*bba2c361STejun Heo 	 * using scx_bpf_dsq_move_to_local().
350*bba2c361STejun Heo 	 *
351*bba2c361STejun Heo 	 * The maximum number of times scx_bpf_dsq_insert() can be called
352*bba2c361STejun Heo 	 * without an intervening scx_bpf_dsq_move_to_local() is specified by
353*bba2c361STejun Heo 	 * ops.dispatch_max_batch. See the comments on top of the two functions
354*bba2c361STejun Heo 	 * for more details.
355*bba2c361STejun Heo 	 *
356*bba2c361STejun Heo 	 * When not %NULL, @prev is an SCX task with its slice depleted. If
357*bba2c361STejun Heo 	 * @prev is still runnable as indicated by set %SCX_TASK_QUEUED in
358*bba2c361STejun Heo 	 * @prev->scx.flags, it is not enqueued yet and will be enqueued after
359*bba2c361STejun Heo 	 * ops.dispatch() returns. To keep executing @prev, return without
360*bba2c361STejun Heo 	 * dispatching or moving any tasks. Also see %SCX_OPS_ENQ_LAST.
361*bba2c361STejun Heo 	 */
362*bba2c361STejun Heo 	void (*dispatch)(s32 cpu, struct task_struct *prev);
363*bba2c361STejun Heo 
364*bba2c361STejun Heo 	/**
365*bba2c361STejun Heo 	 * @tick: Periodic tick
366*bba2c361STejun Heo 	 * @p: task running currently
367*bba2c361STejun Heo 	 *
368*bba2c361STejun Heo 	 * This operation is called every 1/HZ seconds on CPUs which are
369*bba2c361STejun Heo 	 * executing an SCX task. Setting @p->scx.slice to 0 will trigger an
370*bba2c361STejun Heo 	 * immediate dispatch cycle on the CPU.
371*bba2c361STejun Heo 	 */
372*bba2c361STejun Heo 	void (*tick)(struct task_struct *p);
373*bba2c361STejun Heo 
374*bba2c361STejun Heo 	/**
375*bba2c361STejun Heo 	 * @runnable: A task is becoming runnable on its associated CPU
376*bba2c361STejun Heo 	 * @p: task becoming runnable
377*bba2c361STejun Heo 	 * @enq_flags: %SCX_ENQ_*
378*bba2c361STejun Heo 	 *
379*bba2c361STejun Heo 	 * This and the following three functions can be used to track a task's
380*bba2c361STejun Heo 	 * execution state transitions. A task becomes ->runnable() on a CPU,
381*bba2c361STejun Heo 	 * and then goes through one or more ->running() and ->stopping() pairs
382*bba2c361STejun Heo 	 * as it runs on the CPU, and eventually becomes ->quiescent() when it's
383*bba2c361STejun Heo 	 * done running on the CPU.
384*bba2c361STejun Heo 	 *
385*bba2c361STejun Heo 	 * @p is becoming runnable on the CPU because it's
386*bba2c361STejun Heo 	 *
387*bba2c361STejun Heo 	 * - waking up (%SCX_ENQ_WAKEUP)
388*bba2c361STejun Heo 	 * - being moved from another CPU
389*bba2c361STejun Heo 	 * - being restored after temporarily taken off the queue for an
390*bba2c361STejun Heo 	 *   attribute change.
391*bba2c361STejun Heo 	 *
392*bba2c361STejun Heo 	 * This and ->enqueue() are related but not coupled. This operation
393*bba2c361STejun Heo 	 * notifies @p's state transition and may not be followed by ->enqueue()
394*bba2c361STejun Heo 	 * e.g. when @p is being dispatched to a remote CPU, or when @p is
395*bba2c361STejun Heo 	 * being enqueued on a CPU experiencing a hotplug event. Likewise, a
396*bba2c361STejun Heo 	 * task may be ->enqueue()'d without being preceded by this operation
397*bba2c361STejun Heo 	 * e.g. after exhausting its slice.
398*bba2c361STejun Heo 	 */
399*bba2c361STejun Heo 	void (*runnable)(struct task_struct *p, u64 enq_flags);
400*bba2c361STejun Heo 
401*bba2c361STejun Heo 	/**
402*bba2c361STejun Heo 	 * @running: A task is starting to run on its associated CPU
403*bba2c361STejun Heo 	 * @p: task starting to run
404*bba2c361STejun Heo 	 *
405*bba2c361STejun Heo 	 * Note that this callback may be called from a CPU other than the
406*bba2c361STejun Heo 	 * one the task is going to run on. This can happen when a task
407*bba2c361STejun Heo 	 * property is changed (i.e., affinity), since scx_next_task_scx(),
408*bba2c361STejun Heo 	 * which triggers this callback, may run on a CPU different from
409*bba2c361STejun Heo 	 * the task's assigned CPU.
410*bba2c361STejun Heo 	 *
411*bba2c361STejun Heo 	 * Therefore, always use scx_bpf_task_cpu(@p) to determine the
412*bba2c361STejun Heo 	 * target CPU the task is going to use.
413*bba2c361STejun Heo 	 *
414*bba2c361STejun Heo 	 * See ->runnable() for explanation on the task state notifiers.
415*bba2c361STejun Heo 	 */
416*bba2c361STejun Heo 	void (*running)(struct task_struct *p);
417*bba2c361STejun Heo 
418*bba2c361STejun Heo 	/**
419*bba2c361STejun Heo 	 * @stopping: A task is stopping execution
420*bba2c361STejun Heo 	 * @p: task stopping to run
421*bba2c361STejun Heo 	 * @runnable: is task @p still runnable?
422*bba2c361STejun Heo 	 *
423*bba2c361STejun Heo 	 * Note that this callback may be called from a CPU other than the
424*bba2c361STejun Heo 	 * one the task was running on. This can happen when a task
425*bba2c361STejun Heo 	 * property is changed (i.e., affinity), since dequeue_task_scx(),
426*bba2c361STejun Heo 	 * which triggers this callback, may run on a CPU different from
427*bba2c361STejun Heo 	 * the task's assigned CPU.
428*bba2c361STejun Heo 	 *
429*bba2c361STejun Heo 	 * Therefore, always use scx_bpf_task_cpu(@p) to retrieve the CPU
430*bba2c361STejun Heo 	 * the task was running on.
431*bba2c361STejun Heo 	 *
432*bba2c361STejun Heo 	 * See ->runnable() for explanation on the task state notifiers. If
433*bba2c361STejun Heo 	 * !@runnable, ->quiescent() will be invoked after this operation
434*bba2c361STejun Heo 	 * returns.
435*bba2c361STejun Heo 	 */
436*bba2c361STejun Heo 	void (*stopping)(struct task_struct *p, bool runnable);
437*bba2c361STejun Heo 
438*bba2c361STejun Heo 	/**
439*bba2c361STejun Heo 	 * @quiescent: A task is becoming not runnable on its associated CPU
440*bba2c361STejun Heo 	 * @p: task becoming not runnable
441*bba2c361STejun Heo 	 * @deq_flags: %SCX_DEQ_*
442*bba2c361STejun Heo 	 *
443*bba2c361STejun Heo 	 * See ->runnable() for explanation on the task state notifiers.
444*bba2c361STejun Heo 	 *
445*bba2c361STejun Heo 	 * @p is becoming quiescent on the CPU because it's
446*bba2c361STejun Heo 	 *
447*bba2c361STejun Heo 	 * - sleeping (%SCX_DEQ_SLEEP)
448*bba2c361STejun Heo 	 * - being moved to another CPU
449*bba2c361STejun Heo 	 * - being temporarily taken off the queue for an attribute change
450*bba2c361STejun Heo 	 *   (%SCX_DEQ_SAVE)
451*bba2c361STejun Heo 	 *
452*bba2c361STejun Heo 	 * This and ->dequeue() are related but not coupled. This operation
453*bba2c361STejun Heo 	 * notifies @p's state transition and may not be preceded by ->dequeue()
454*bba2c361STejun Heo 	 * e.g. when @p is being dispatched to a remote CPU.
455*bba2c361STejun Heo 	 */
456*bba2c361STejun Heo 	void (*quiescent)(struct task_struct *p, u64 deq_flags);
457*bba2c361STejun Heo 
458*bba2c361STejun Heo 	/**
459*bba2c361STejun Heo 	 * @yield: Yield CPU
460*bba2c361STejun Heo 	 * @from: yielding task
461*bba2c361STejun Heo 	 * @to: optional yield target task
462*bba2c361STejun Heo 	 *
463*bba2c361STejun Heo 	 * If @to is NULL, @from is yielding the CPU to other runnable tasks.
464*bba2c361STejun Heo 	 * The BPF scheduler should ensure that other available tasks are
465*bba2c361STejun Heo 	 * dispatched before the yielding task. Return value is ignored in this
466*bba2c361STejun Heo 	 * case.
467*bba2c361STejun Heo 	 *
468*bba2c361STejun Heo 	 * If @to is not-NULL, @from wants to yield the CPU to @to. If the bpf
469*bba2c361STejun Heo 	 * scheduler can implement the request, return %true; otherwise, %false.
470*bba2c361STejun Heo 	 */
471*bba2c361STejun Heo 	bool (*yield)(struct task_struct *from, struct task_struct *to);
472*bba2c361STejun Heo 
473*bba2c361STejun Heo 	/**
474*bba2c361STejun Heo 	 * @core_sched_before: Task ordering for core-sched
475*bba2c361STejun Heo 	 * @a: task A
476*bba2c361STejun Heo 	 * @b: task B
477*bba2c361STejun Heo 	 *
478*bba2c361STejun Heo 	 * Used by core-sched to determine the ordering between two tasks. See
479*bba2c361STejun Heo 	 * Documentation/admin-guide/hw-vuln/core-scheduling.rst for details on
480*bba2c361STejun Heo 	 * core-sched.
481*bba2c361STejun Heo 	 *
482*bba2c361STejun Heo 	 * Both @a and @b are runnable and may or may not currently be queued on
483*bba2c361STejun Heo 	 * the BPF scheduler. Should return %true if @a should run before @b.
484*bba2c361STejun Heo 	 * %false if there's no required ordering or @b should run before @a.
485*bba2c361STejun Heo 	 *
486*bba2c361STejun Heo 	 * If not specified, the default is ordering them according to when they
487*bba2c361STejun Heo 	 * became runnable.
488*bba2c361STejun Heo 	 */
489*bba2c361STejun Heo 	bool (*core_sched_before)(struct task_struct *a, struct task_struct *b);
490*bba2c361STejun Heo 
491*bba2c361STejun Heo 	/**
492*bba2c361STejun Heo 	 * @set_weight: Set task weight
493*bba2c361STejun Heo 	 * @p: task to set weight for
494*bba2c361STejun Heo 	 * @weight: new weight [1..10000]
495*bba2c361STejun Heo 	 *
496*bba2c361STejun Heo 	 * Update @p's weight to @weight.
497*bba2c361STejun Heo 	 */
498*bba2c361STejun Heo 	void (*set_weight)(struct task_struct *p, u32 weight);
499*bba2c361STejun Heo 
500*bba2c361STejun Heo 	/**
501*bba2c361STejun Heo 	 * @set_cpumask: Set CPU affinity
502*bba2c361STejun Heo 	 * @p: task to set CPU affinity for
503*bba2c361STejun Heo 	 * @cpumask: cpumask of cpus that @p can run on
504*bba2c361STejun Heo 	 *
505*bba2c361STejun Heo 	 * Update @p's CPU affinity to @cpumask.
506*bba2c361STejun Heo 	 */
507*bba2c361STejun Heo 	void (*set_cpumask)(struct task_struct *p,
508*bba2c361STejun Heo 			    const struct cpumask *cpumask);
509*bba2c361STejun Heo 
510*bba2c361STejun Heo 	/**
511*bba2c361STejun Heo 	 * @update_idle: Update the idle state of a CPU
512*bba2c361STejun Heo 	 * @cpu: CPU to update the idle state for
513*bba2c361STejun Heo 	 * @idle: whether entering or exiting the idle state
514*bba2c361STejun Heo 	 *
515*bba2c361STejun Heo 	 * This operation is called when @rq's CPU goes or leaves the idle
516*bba2c361STejun Heo 	 * state. By default, implementing this operation disables the built-in
517*bba2c361STejun Heo 	 * idle CPU tracking and the following helpers become unavailable:
518*bba2c361STejun Heo 	 *
519*bba2c361STejun Heo 	 * - scx_bpf_select_cpu_dfl()
520*bba2c361STejun Heo 	 * - scx_bpf_select_cpu_and()
521*bba2c361STejun Heo 	 * - scx_bpf_test_and_clear_cpu_idle()
522*bba2c361STejun Heo 	 * - scx_bpf_pick_idle_cpu()
523*bba2c361STejun Heo 	 *
524*bba2c361STejun Heo 	 * The user also must implement ops.select_cpu() as the default
525*bba2c361STejun Heo 	 * implementation relies on scx_bpf_select_cpu_dfl().
526*bba2c361STejun Heo 	 *
527*bba2c361STejun Heo 	 * Specify the %SCX_OPS_KEEP_BUILTIN_IDLE flag to keep the built-in idle
528*bba2c361STejun Heo 	 * tracking.
529*bba2c361STejun Heo 	 */
530*bba2c361STejun Heo 	void (*update_idle)(s32 cpu, bool idle);
531*bba2c361STejun Heo 
532*bba2c361STejun Heo 	/**
533*bba2c361STejun Heo 	 * @init_task: Initialize a task to run in a BPF scheduler
534*bba2c361STejun Heo 	 * @p: task to initialize for BPF scheduling
535*bba2c361STejun Heo 	 * @args: init arguments, see the struct definition
536*bba2c361STejun Heo 	 *
537*bba2c361STejun Heo 	 * Either we're loading a BPF scheduler or a new task is being forked.
538*bba2c361STejun Heo 	 * Initialize @p for BPF scheduling. This operation may block and can
539*bba2c361STejun Heo 	 * be used for allocations, and is called exactly once for a task.
540*bba2c361STejun Heo 	 *
541*bba2c361STejun Heo 	 * Return 0 for success, -errno for failure. An error return while
542*bba2c361STejun Heo 	 * loading will abort loading of the BPF scheduler. During a fork, it
543*bba2c361STejun Heo 	 * will abort that specific fork.
544*bba2c361STejun Heo 	 */
545*bba2c361STejun Heo 	s32 (*init_task)(struct task_struct *p, struct scx_init_task_args *args);
546*bba2c361STejun Heo 
547*bba2c361STejun Heo 	/**
548*bba2c361STejun Heo 	 * @exit_task: Exit a previously-running task from the system
549*bba2c361STejun Heo 	 * @p: task to exit
550*bba2c361STejun Heo 	 * @args: exit arguments, see the struct definition
551*bba2c361STejun Heo 	 *
552*bba2c361STejun Heo 	 * @p is exiting or the BPF scheduler is being unloaded. Perform any
553*bba2c361STejun Heo 	 * necessary cleanup for @p.
554*bba2c361STejun Heo 	 */
555*bba2c361STejun Heo 	void (*exit_task)(struct task_struct *p, struct scx_exit_task_args *args);
556*bba2c361STejun Heo 
557*bba2c361STejun Heo 	/**
558*bba2c361STejun Heo 	 * @enable: Enable BPF scheduling for a task
559*bba2c361STejun Heo 	 * @p: task to enable BPF scheduling for
560*bba2c361STejun Heo 	 *
561*bba2c361STejun Heo 	 * Enable @p for BPF scheduling. enable() is called on @p any time it
562*bba2c361STejun Heo 	 * enters SCX, and is always paired with a matching disable().
563*bba2c361STejun Heo 	 */
564*bba2c361STejun Heo 	void (*enable)(struct task_struct *p);
565*bba2c361STejun Heo 
566*bba2c361STejun Heo 	/**
567*bba2c361STejun Heo 	 * @disable: Disable BPF scheduling for a task
568*bba2c361STejun Heo 	 * @p: task to disable BPF scheduling for
569*bba2c361STejun Heo 	 *
570*bba2c361STejun Heo 	 * @p is exiting, leaving SCX or the BPF scheduler is being unloaded.
571*bba2c361STejun Heo 	 * Disable BPF scheduling for @p. A disable() call is always matched
572*bba2c361STejun Heo 	 * with a prior enable() call.
573*bba2c361STejun Heo 	 */
574*bba2c361STejun Heo 	void (*disable)(struct task_struct *p);
575*bba2c361STejun Heo 
576*bba2c361STejun Heo 	/**
577*bba2c361STejun Heo 	 * @dump: Dump BPF scheduler state on error
578*bba2c361STejun Heo 	 * @ctx: debug dump context
579*bba2c361STejun Heo 	 *
580*bba2c361STejun Heo 	 * Use scx_bpf_dump() to generate BPF scheduler specific debug dump.
581*bba2c361STejun Heo 	 */
582*bba2c361STejun Heo 	void (*dump)(struct scx_dump_ctx *ctx);
583*bba2c361STejun Heo 
584*bba2c361STejun Heo 	/**
585*bba2c361STejun Heo 	 * @dump_cpu: Dump BPF scheduler state for a CPU on error
586*bba2c361STejun Heo 	 * @ctx: debug dump context
587*bba2c361STejun Heo 	 * @cpu: CPU to generate debug dump for
588*bba2c361STejun Heo 	 * @idle: @cpu is currently idle without any runnable tasks
589*bba2c361STejun Heo 	 *
590*bba2c361STejun Heo 	 * Use scx_bpf_dump() to generate BPF scheduler specific debug dump for
591*bba2c361STejun Heo 	 * @cpu. If @idle is %true and this operation doesn't produce any
592*bba2c361STejun Heo 	 * output, @cpu is skipped for dump.
593*bba2c361STejun Heo 	 */
594*bba2c361STejun Heo 	void (*dump_cpu)(struct scx_dump_ctx *ctx, s32 cpu, bool idle);
595*bba2c361STejun Heo 
596*bba2c361STejun Heo 	/**
597*bba2c361STejun Heo 	 * @dump_task: Dump BPF scheduler state for a runnable task on error
598*bba2c361STejun Heo 	 * @ctx: debug dump context
599*bba2c361STejun Heo 	 * @p: runnable task to generate debug dump for
600*bba2c361STejun Heo 	 *
601*bba2c361STejun Heo 	 * Use scx_bpf_dump() to generate BPF scheduler specific debug dump for
602*bba2c361STejun Heo 	 * @p.
603*bba2c361STejun Heo 	 */
604*bba2c361STejun Heo 	void (*dump_task)(struct scx_dump_ctx *ctx, struct task_struct *p);
605*bba2c361STejun Heo 
606*bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED
607*bba2c361STejun Heo 	/**
608*bba2c361STejun Heo 	 * @cgroup_init: Initialize a cgroup
609*bba2c361STejun Heo 	 * @cgrp: cgroup being initialized
610*bba2c361STejun Heo 	 * @args: init arguments, see the struct definition
611*bba2c361STejun Heo 	 *
612*bba2c361STejun Heo 	 * Either the BPF scheduler is being loaded or @cgrp created, initialize
613*bba2c361STejun Heo 	 * @cgrp for sched_ext. This operation may block.
614*bba2c361STejun Heo 	 *
615*bba2c361STejun Heo 	 * Return 0 for success, -errno for failure. An error return while
616*bba2c361STejun Heo 	 * loading will abort loading of the BPF scheduler. During cgroup
617*bba2c361STejun Heo 	 * creation, it will abort the specific cgroup creation.
618*bba2c361STejun Heo 	 */
619*bba2c361STejun Heo 	s32 (*cgroup_init)(struct cgroup *cgrp,
620*bba2c361STejun Heo 			   struct scx_cgroup_init_args *args);
621*bba2c361STejun Heo 
622*bba2c361STejun Heo 	/**
623*bba2c361STejun Heo 	 * @cgroup_exit: Exit a cgroup
624*bba2c361STejun Heo 	 * @cgrp: cgroup being exited
625*bba2c361STejun Heo 	 *
626*bba2c361STejun Heo 	 * Either the BPF scheduler is being unloaded or @cgrp destroyed, exit
627*bba2c361STejun Heo 	 * @cgrp for sched_ext. This operation my block.
628*bba2c361STejun Heo 	 */
629*bba2c361STejun Heo 	void (*cgroup_exit)(struct cgroup *cgrp);
630*bba2c361STejun Heo 
631*bba2c361STejun Heo 	/**
632*bba2c361STejun Heo 	 * @cgroup_prep_move: Prepare a task to be moved to a different cgroup
633*bba2c361STejun Heo 	 * @p: task being moved
634*bba2c361STejun Heo 	 * @from: cgroup @p is being moved from
635*bba2c361STejun Heo 	 * @to: cgroup @p is being moved to
636*bba2c361STejun Heo 	 *
637*bba2c361STejun Heo 	 * Prepare @p for move from cgroup @from to @to. This operation may
638*bba2c361STejun Heo 	 * block and can be used for allocations.
639*bba2c361STejun Heo 	 *
640*bba2c361STejun Heo 	 * Return 0 for success, -errno for failure. An error return aborts the
641*bba2c361STejun Heo 	 * migration.
642*bba2c361STejun Heo 	 */
643*bba2c361STejun Heo 	s32 (*cgroup_prep_move)(struct task_struct *p,
644*bba2c361STejun Heo 				struct cgroup *from, struct cgroup *to);
645*bba2c361STejun Heo 
646*bba2c361STejun Heo 	/**
647*bba2c361STejun Heo 	 * @cgroup_move: Commit cgroup move
648*bba2c361STejun Heo 	 * @p: task being moved
649*bba2c361STejun Heo 	 * @from: cgroup @p is being moved from
650*bba2c361STejun Heo 	 * @to: cgroup @p is being moved to
651*bba2c361STejun Heo 	 *
652*bba2c361STejun Heo 	 * Commit the move. @p is dequeued during this operation.
653*bba2c361STejun Heo 	 */
654*bba2c361STejun Heo 	void (*cgroup_move)(struct task_struct *p,
655*bba2c361STejun Heo 			    struct cgroup *from, struct cgroup *to);
656*bba2c361STejun Heo 
657*bba2c361STejun Heo 	/**
658*bba2c361STejun Heo 	 * @cgroup_cancel_move: Cancel cgroup move
659*bba2c361STejun Heo 	 * @p: task whose cgroup move is being canceled
660*bba2c361STejun Heo 	 * @from: cgroup @p was being moved from
661*bba2c361STejun Heo 	 * @to: cgroup @p was being moved to
662*bba2c361STejun Heo 	 *
663*bba2c361STejun Heo 	 * @p was cgroup_prep_move()'d but failed before reaching cgroup_move().
664*bba2c361STejun Heo 	 * Undo the preparation.
665*bba2c361STejun Heo 	 */
666*bba2c361STejun Heo 	void (*cgroup_cancel_move)(struct task_struct *p,
667*bba2c361STejun Heo 				   struct cgroup *from, struct cgroup *to);
668*bba2c361STejun Heo 
669*bba2c361STejun Heo 	/**
670*bba2c361STejun Heo 	 * @cgroup_set_weight: A cgroup's weight is being changed
671*bba2c361STejun Heo 	 * @cgrp: cgroup whose weight is being updated
672*bba2c361STejun Heo 	 * @weight: new weight [1..10000]
673*bba2c361STejun Heo 	 *
674*bba2c361STejun Heo 	 * Update @cgrp's weight to @weight.
675*bba2c361STejun Heo 	 */
676*bba2c361STejun Heo 	void (*cgroup_set_weight)(struct cgroup *cgrp, u32 weight);
677*bba2c361STejun Heo 
678*bba2c361STejun Heo 	/**
679*bba2c361STejun Heo 	 * @cgroup_set_bandwidth: A cgroup's bandwidth is being changed
680*bba2c361STejun Heo 	 * @cgrp: cgroup whose bandwidth is being updated
681*bba2c361STejun Heo 	 * @period_us: bandwidth control period
682*bba2c361STejun Heo 	 * @quota_us: bandwidth control quota
683*bba2c361STejun Heo 	 * @burst_us: bandwidth control burst
684*bba2c361STejun Heo 	 *
685*bba2c361STejun Heo 	 * Update @cgrp's bandwidth control parameters. This is from the cpu.max
686*bba2c361STejun Heo 	 * cgroup interface.
687*bba2c361STejun Heo 	 *
688*bba2c361STejun Heo 	 * @quota_us / @period_us determines the CPU bandwidth @cgrp is entitled
689*bba2c361STejun Heo 	 * to. For example, if @period_us is 1_000_000 and @quota_us is
690*bba2c361STejun Heo 	 * 2_500_000. @cgrp is entitled to 2.5 CPUs. @burst_us can be
691*bba2c361STejun Heo 	 * interpreted in the same fashion and specifies how much @cgrp can
692*bba2c361STejun Heo 	 * burst temporarily. The specific control mechanism and thus the
693*bba2c361STejun Heo 	 * interpretation of @period_us and burstiness is up to the BPF
694*bba2c361STejun Heo 	 * scheduler.
695*bba2c361STejun Heo 	 */
696*bba2c361STejun Heo 	void (*cgroup_set_bandwidth)(struct cgroup *cgrp,
697*bba2c361STejun Heo 				     u64 period_us, u64 quota_us, u64 burst_us);
698*bba2c361STejun Heo 
699*bba2c361STejun Heo 	/**
700*bba2c361STejun Heo 	 * @cgroup_set_idle: A cgroup's idle state is being changed
701*bba2c361STejun Heo 	 * @cgrp: cgroup whose idle state is being updated
702*bba2c361STejun Heo 	 * @idle: whether the cgroup is entering or exiting idle state
703*bba2c361STejun Heo 	 *
704*bba2c361STejun Heo 	 * Update @cgrp's idle state to @idle. This callback is invoked when
705*bba2c361STejun Heo 	 * a cgroup transitions between idle and non-idle states, allowing the
706*bba2c361STejun Heo 	 * BPF scheduler to adjust its behavior accordingly.
707*bba2c361STejun Heo 	 */
708*bba2c361STejun Heo 	void (*cgroup_set_idle)(struct cgroup *cgrp, bool idle);
709*bba2c361STejun Heo 
710*bba2c361STejun Heo #endif	/* CONFIG_EXT_GROUP_SCHED */
711*bba2c361STejun Heo 
712*bba2c361STejun Heo 	/**
713*bba2c361STejun Heo 	 * @sub_attach: Attach a sub-scheduler
714*bba2c361STejun Heo 	 * @args: argument container, see the struct definition
715*bba2c361STejun Heo 	 *
716*bba2c361STejun Heo 	 * Return 0 to accept the sub-scheduler. -errno to reject.
717*bba2c361STejun Heo 	 */
718*bba2c361STejun Heo 	s32 (*sub_attach)(struct scx_sub_attach_args *args);
719*bba2c361STejun Heo 
720*bba2c361STejun Heo 	/**
721*bba2c361STejun Heo 	 * @sub_detach: Detach a sub-scheduler
722*bba2c361STejun Heo 	 * @args: argument container, see the struct definition
723*bba2c361STejun Heo 	 */
724*bba2c361STejun Heo 	void (*sub_detach)(struct scx_sub_detach_args *args);
725*bba2c361STejun Heo 
726*bba2c361STejun Heo 	/*
727*bba2c361STejun Heo 	 * All online ops must come before ops.cpu_online().
728*bba2c361STejun Heo 	 */
729*bba2c361STejun Heo 
730*bba2c361STejun Heo 	/**
731*bba2c361STejun Heo 	 * @cpu_online: A CPU became online
732*bba2c361STejun Heo 	 * @cpu: CPU which just came up
733*bba2c361STejun Heo 	 *
734*bba2c361STejun Heo 	 * @cpu just came online. @cpu will not call ops.enqueue() or
735*bba2c361STejun Heo 	 * ops.dispatch(), nor run tasks associated with other CPUs beforehand.
736*bba2c361STejun Heo 	 */
737*bba2c361STejun Heo 	void (*cpu_online)(s32 cpu);
738*bba2c361STejun Heo 
739*bba2c361STejun Heo 	/**
740*bba2c361STejun Heo 	 * @cpu_offline: A CPU is going offline
741*bba2c361STejun Heo 	 * @cpu: CPU which is going offline
742*bba2c361STejun Heo 	 *
743*bba2c361STejun Heo 	 * @cpu is going offline. @cpu will not call ops.enqueue() or
744*bba2c361STejun Heo 	 * ops.dispatch(), nor run tasks associated with other CPUs afterwards.
745*bba2c361STejun Heo 	 */
746*bba2c361STejun Heo 	void (*cpu_offline)(s32 cpu);
747*bba2c361STejun Heo 
748*bba2c361STejun Heo 	/*
749*bba2c361STejun Heo 	 * All CPU hotplug ops must come before ops.init().
750*bba2c361STejun Heo 	 */
751*bba2c361STejun Heo 
752*bba2c361STejun Heo 	/**
753*bba2c361STejun Heo 	 * @init: Initialize the BPF scheduler
754*bba2c361STejun Heo 	 */
755*bba2c361STejun Heo 	s32 (*init)(void);
756*bba2c361STejun Heo 
757*bba2c361STejun Heo 	/**
758*bba2c361STejun Heo 	 * @exit: Clean up after the BPF scheduler
759*bba2c361STejun Heo 	 * @info: Exit info
760*bba2c361STejun Heo 	 *
761*bba2c361STejun Heo 	 * ops.exit() is also called on ops.init() failure, which is a bit
762*bba2c361STejun Heo 	 * unusual. This is to allow rich reporting through @info on how
763*bba2c361STejun Heo 	 * ops.init() failed.
764*bba2c361STejun Heo 	 */
765*bba2c361STejun Heo 	void (*exit)(struct scx_exit_info *info);
766*bba2c361STejun Heo 
767*bba2c361STejun Heo 	/*
768*bba2c361STejun Heo 	 * Data fields must comes after all ops fields.
769*bba2c361STejun Heo 	 */
770*bba2c361STejun Heo 
771*bba2c361STejun Heo 	/**
772*bba2c361STejun Heo 	 * @dispatch_max_batch: Max nr of tasks that dispatch() can dispatch
773*bba2c361STejun Heo 	 */
774*bba2c361STejun Heo 	u32 dispatch_max_batch;
775*bba2c361STejun Heo 
776*bba2c361STejun Heo 	/**
777*bba2c361STejun Heo 	 * @flags: %SCX_OPS_* flags
778*bba2c361STejun Heo 	 */
779*bba2c361STejun Heo 	u64 flags;
780*bba2c361STejun Heo 
781*bba2c361STejun Heo 	/**
782*bba2c361STejun Heo 	 * @timeout_ms: The maximum amount of time, in milliseconds, that a
783*bba2c361STejun Heo 	 * runnable task should be able to wait before being scheduled. The
784*bba2c361STejun Heo 	 * maximum timeout may not exceed the default timeout of 30 seconds.
785*bba2c361STejun Heo 	 *
786*bba2c361STejun Heo 	 * Defaults to the maximum allowed timeout value of 30 seconds.
787*bba2c361STejun Heo 	 */
788*bba2c361STejun Heo 	u32 timeout_ms;
789*bba2c361STejun Heo 
790*bba2c361STejun Heo 	/**
791*bba2c361STejun Heo 	 * @exit_dump_len: scx_exit_info.dump buffer length. If 0, the default
792*bba2c361STejun Heo 	 * value of 32768 is used.
793*bba2c361STejun Heo 	 */
794*bba2c361STejun Heo 	u32 exit_dump_len;
795*bba2c361STejun Heo 
796*bba2c361STejun Heo 	/**
797*bba2c361STejun Heo 	 * @hotplug_seq: A sequence number that may be set by the scheduler to
798*bba2c361STejun Heo 	 * detect when a hotplug event has occurred during the loading process.
799*bba2c361STejun Heo 	 * If 0, no detection occurs. Otherwise, the scheduler will fail to
800*bba2c361STejun Heo 	 * load if the sequence number does not match @scx_hotplug_seq on the
801*bba2c361STejun Heo 	 * enable path.
802*bba2c361STejun Heo 	 */
803*bba2c361STejun Heo 	u64 hotplug_seq;
804*bba2c361STejun Heo 
805*bba2c361STejun Heo 	/**
806*bba2c361STejun Heo 	 * @cgroup_id: When >1, attach the scheduler as a sub-scheduler on the
807*bba2c361STejun Heo 	 * specified cgroup.
808*bba2c361STejun Heo 	 */
809*bba2c361STejun Heo 	u64 sub_cgroup_id;
810*bba2c361STejun Heo 
811*bba2c361STejun Heo 	/**
812*bba2c361STejun Heo 	 * @name: BPF scheduler's name
813*bba2c361STejun Heo 	 *
814*bba2c361STejun Heo 	 * Must be a non-zero valid BPF object name including only isalnum(),
815*bba2c361STejun Heo 	 * '_' and '.' chars. Shows up in kernel.sched_ext_ops sysctl while the
816*bba2c361STejun Heo 	 * BPF scheduler is enabled.
817*bba2c361STejun Heo 	 */
818*bba2c361STejun Heo 	char name[SCX_OPS_NAME_LEN];
819*bba2c361STejun Heo 
820*bba2c361STejun Heo 	/* internal use only, must be NULL */
821*bba2c361STejun Heo 	void __rcu *priv;
822*bba2c361STejun Heo 
823*bba2c361STejun Heo 	/*
824*bba2c361STejun Heo 	 * Deprecated callbacks. Kept at the end of the struct so the cid-form
825*bba2c361STejun Heo 	 * struct (sched_ext_ops_cid) can omit them without affecting the
826*bba2c361STejun Heo 	 * shared field offsets. Use SCX_ENQ_IMMED instead. Sitting past
827*bba2c361STejun Heo 	 * SCX_OPI_END means has_op doesn't cover them, so SCX_HAS_OP() cannot
828*bba2c361STejun Heo 	 * be used; callers must test sch->ops.cpu_acquire / cpu_release
829*bba2c361STejun Heo 	 * directly.
830*bba2c361STejun Heo 	 */
831*bba2c361STejun Heo 
832*bba2c361STejun Heo 	/**
833*bba2c361STejun Heo 	 * @cpu_acquire: A CPU is becoming available to the BPF scheduler
834*bba2c361STejun Heo 	 * @cpu: The CPU being acquired by the BPF scheduler.
835*bba2c361STejun Heo 	 * @args: Acquire arguments, see the struct definition.
836*bba2c361STejun Heo 	 *
837*bba2c361STejun Heo 	 * A CPU that was previously released from the BPF scheduler is now once
838*bba2c361STejun Heo 	 * again under its control. Deprecated; use SCX_ENQ_IMMED instead.
839*bba2c361STejun Heo 	 */
840*bba2c361STejun Heo 	void (*cpu_acquire)(s32 cpu, struct scx_cpu_acquire_args *args);
841*bba2c361STejun Heo 
842*bba2c361STejun Heo 	/**
843*bba2c361STejun Heo 	 * @cpu_release: A CPU is taken away from the BPF scheduler
844*bba2c361STejun Heo 	 * @cpu: The CPU being released by the BPF scheduler.
845*bba2c361STejun Heo 	 * @args: Release arguments, see the struct definition.
846*bba2c361STejun Heo 	 *
847*bba2c361STejun Heo 	 * The specified CPU is no longer under the control of the BPF
848*bba2c361STejun Heo 	 * scheduler. This could be because it was preempted by a higher
849*bba2c361STejun Heo 	 * priority sched_class, though there may be other reasons as well. The
850*bba2c361STejun Heo 	 * caller should consult @args->reason to determine the cause.
851*bba2c361STejun Heo 	 * Deprecated; use SCX_ENQ_IMMED instead.
852*bba2c361STejun Heo 	 */
853*bba2c361STejun Heo 	void (*cpu_release)(s32 cpu, struct scx_cpu_release_args *args);
854*bba2c361STejun Heo };
855*bba2c361STejun Heo 
856*bba2c361STejun Heo /**
857*bba2c361STejun Heo  * struct sched_ext_ops_cid - cid-form alternative to struct sched_ext_ops
858*bba2c361STejun Heo  *
859*bba2c361STejun Heo  * Mirrors struct sched_ext_ops with cpu/cpumask substituted with cid/cmask
860*bba2c361STejun Heo  * where applicable. Layout up to and including @priv matches sched_ext_ops
861*bba2c361STejun Heo  * byte-for-byte (verified by BUILD_BUG_ON checks at scx_init() time) so
862*bba2c361STejun Heo  * shared field offsets work for both struct types in bpf_scx_init_member()
863*bba2c361STejun Heo  * and bpf_scx_check_member(). The deprecated cpu_acquire/cpu_release
864*bba2c361STejun Heo  * callbacks at the tail of sched_ext_ops are omitted here entirely.
865*bba2c361STejun Heo  *
866*bba2c361STejun Heo  * Differences from sched_ext_ops:
867*bba2c361STejun Heo  *   - select_cpu       -> select_cid (returns cid)
868*bba2c361STejun Heo  *   - dispatch         -> dispatch (cpu arg is now cid)
869*bba2c361STejun Heo  *   - update_idle      -> update_idle (cpu arg is now cid)
870*bba2c361STejun Heo  *   - set_cpumask      -> set_cmask (cmask instead of cpumask)
871*bba2c361STejun Heo  *   - cpu_online       -> cid_online
872*bba2c361STejun Heo  *   - cpu_offline      -> cid_offline
873*bba2c361STejun Heo  *   - dump_cpu         -> dump_cid
874*bba2c361STejun Heo  *   - cpu_acquire/cpu_release  -> not present (deprecated in sched_ext_ops)
875*bba2c361STejun Heo  *
876*bba2c361STejun Heo  * BPF schedulers using this type cannot call cpu-form scx_bpf_* kfuncs;
877*bba2c361STejun Heo  * use the cid-form variants instead. Enforced at BPF verifier time via
878*bba2c361STejun Heo  * scx_kfunc_context_filter() branching on prog->aux->st_ops.
879*bba2c361STejun Heo  *
880*bba2c361STejun Heo  * See sched_ext_ops for callback documentation.
881*bba2c361STejun Heo  */
882*bba2c361STejun Heo struct sched_ext_ops_cid {
883*bba2c361STejun Heo 	s32 (*select_cid)(struct task_struct *p, s32 prev_cid, u64 wake_flags);
884*bba2c361STejun Heo 	void (*enqueue)(struct task_struct *p, u64 enq_flags);
885*bba2c361STejun Heo 	void (*dequeue)(struct task_struct *p, u64 deq_flags);
886*bba2c361STejun Heo 	void (*dispatch)(s32 cid, struct task_struct *prev);
887*bba2c361STejun Heo 	void (*tick)(struct task_struct *p);
888*bba2c361STejun Heo 	void (*runnable)(struct task_struct *p, u64 enq_flags);
889*bba2c361STejun Heo 	void (*running)(struct task_struct *p);
890*bba2c361STejun Heo 	void (*stopping)(struct task_struct *p, bool runnable);
891*bba2c361STejun Heo 	void (*quiescent)(struct task_struct *p, u64 deq_flags);
892*bba2c361STejun Heo 	bool (*yield)(struct task_struct *from, struct task_struct *to);
893*bba2c361STejun Heo 	bool (*core_sched_before)(struct task_struct *a,
894*bba2c361STejun Heo 				   struct task_struct *b);
895*bba2c361STejun Heo 	void (*set_weight)(struct task_struct *p, u32 weight);
896*bba2c361STejun Heo 	void (*set_cmask)(struct task_struct *p,
897*bba2c361STejun Heo 			   const struct scx_cmask *cmask);
898*bba2c361STejun Heo 	void (*update_idle)(s32 cid, bool idle);
899*bba2c361STejun Heo 	s32 (*init_task)(struct task_struct *p,
900*bba2c361STejun Heo 			  struct scx_init_task_args *args);
901*bba2c361STejun Heo 	void (*exit_task)(struct task_struct *p,
902*bba2c361STejun Heo 			   struct scx_exit_task_args *args);
903*bba2c361STejun Heo 	void (*enable)(struct task_struct *p);
904*bba2c361STejun Heo 	void (*disable)(struct task_struct *p);
905*bba2c361STejun Heo 	void (*dump)(struct scx_dump_ctx *ctx);
906*bba2c361STejun Heo 	void (*dump_cid)(struct scx_dump_ctx *ctx, s32 cid, bool idle);
907*bba2c361STejun Heo 	void (*dump_task)(struct scx_dump_ctx *ctx, struct task_struct *p);
908*bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED
909*bba2c361STejun Heo 	s32 (*cgroup_init)(struct cgroup *cgrp,
910*bba2c361STejun Heo 			    struct scx_cgroup_init_args *args);
911*bba2c361STejun Heo 	void (*cgroup_exit)(struct cgroup *cgrp);
912*bba2c361STejun Heo 	s32 (*cgroup_prep_move)(struct task_struct *p,
913*bba2c361STejun Heo 				 struct cgroup *from, struct cgroup *to);
914*bba2c361STejun Heo 	void (*cgroup_move)(struct task_struct *p,
915*bba2c361STejun Heo 			     struct cgroup *from, struct cgroup *to);
916*bba2c361STejun Heo 	void (*cgroup_cancel_move)(struct task_struct *p,
917*bba2c361STejun Heo 				    struct cgroup *from, struct cgroup *to);
918*bba2c361STejun Heo 	void (*cgroup_set_weight)(struct cgroup *cgrp, u32 weight);
919*bba2c361STejun Heo 	void (*cgroup_set_bandwidth)(struct cgroup *cgrp,
920*bba2c361STejun Heo 				      u64 period_us, u64 quota_us, u64 burst_us);
921*bba2c361STejun Heo 	void (*cgroup_set_idle)(struct cgroup *cgrp, bool idle);
922*bba2c361STejun Heo #endif	/* CONFIG_EXT_GROUP_SCHED */
923*bba2c361STejun Heo 	s32 (*sub_attach)(struct scx_sub_attach_args *args);
924*bba2c361STejun Heo 	void (*sub_detach)(struct scx_sub_detach_args *args);
925*bba2c361STejun Heo 	void (*cid_online)(s32 cid);
926*bba2c361STejun Heo 	void (*cid_offline)(s32 cid);
927*bba2c361STejun Heo 	s32 (*init)(void);
928*bba2c361STejun Heo 	void (*exit)(struct scx_exit_info *info);
929*bba2c361STejun Heo 
930*bba2c361STejun Heo 	/* Data fields - must match sched_ext_ops layout exactly */
931*bba2c361STejun Heo 	u32 dispatch_max_batch;
932*bba2c361STejun Heo 	u64 flags;
933*bba2c361STejun Heo 	u32 timeout_ms;
934*bba2c361STejun Heo 	u32 exit_dump_len;
935*bba2c361STejun Heo 	u64 hotplug_seq;
936*bba2c361STejun Heo 	u64 sub_cgroup_id;
937*bba2c361STejun Heo 	char name[SCX_OPS_NAME_LEN];
938*bba2c361STejun Heo 
939*bba2c361STejun Heo 	/* internal use only, must be NULL */
940*bba2c361STejun Heo 	void __rcu *priv;
941*bba2c361STejun Heo 
942*bba2c361STejun Heo 	/* layout end anchor for the BUILD_BUG_ON in scx_init(); keep last */
943*bba2c361STejun Heo 	char __end[0];
944*bba2c361STejun Heo };
945*bba2c361STejun Heo 
946*bba2c361STejun Heo enum scx_opi {
947*bba2c361STejun Heo 	SCX_OPI_BEGIN			= 0,
948*bba2c361STejun Heo 	SCX_OPI_NORMAL_BEGIN		= 0,
949*bba2c361STejun Heo 	SCX_OPI_NORMAL_END		= SCX_OP_IDX(cpu_online),
950*bba2c361STejun Heo 	SCX_OPI_CPU_HOTPLUG_BEGIN	= SCX_OP_IDX(cpu_online),
951*bba2c361STejun Heo 	SCX_OPI_CPU_HOTPLUG_END		= SCX_OP_IDX(init),
952*bba2c361STejun Heo 	SCX_OPI_END			= SCX_OP_IDX(init),
953*bba2c361STejun Heo };
954*bba2c361STejun Heo 
955*bba2c361STejun Heo /*
956*bba2c361STejun Heo  * Collection of event counters. Event types are placed in descending order.
957*bba2c361STejun Heo  */
958*bba2c361STejun Heo struct scx_event_stats {
959*bba2c361STejun Heo 	/*
960*bba2c361STejun Heo 	 * If ops.select_cpu() returns a CPU which can't be used by the task,
961*bba2c361STejun Heo 	 * the core scheduler code silently picks a fallback CPU.
962*bba2c361STejun Heo 	 */
963*bba2c361STejun Heo 	s64		SCX_EV_SELECT_CPU_FALLBACK;
964*bba2c361STejun Heo 
965*bba2c361STejun Heo 	/*
966*bba2c361STejun Heo 	 * When dispatching to a local DSQ, the CPU may have gone offline in
967*bba2c361STejun Heo 	 * the meantime. In this case, the task is bounced to the global DSQ.
968*bba2c361STejun Heo 	 */
969*bba2c361STejun Heo 	s64		SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE;
970*bba2c361STejun Heo 
971*bba2c361STejun Heo 	/*
972*bba2c361STejun Heo 	 * If SCX_OPS_ENQ_LAST is not set, the number of times that a task
973*bba2c361STejun Heo 	 * continued to run because there were no other tasks on the CPU.
974*bba2c361STejun Heo 	 */
975*bba2c361STejun Heo 	s64		SCX_EV_DISPATCH_KEEP_LAST;
976*bba2c361STejun Heo 
977*bba2c361STejun Heo 	/*
978*bba2c361STejun Heo 	 * If SCX_OPS_ENQ_EXITING is not set, the number of times that a task
979*bba2c361STejun Heo 	 * is dispatched to a local DSQ when exiting.
980*bba2c361STejun Heo 	 */
981*bba2c361STejun Heo 	s64		SCX_EV_ENQ_SKIP_EXITING;
982*bba2c361STejun Heo 
983*bba2c361STejun Heo 	/*
984*bba2c361STejun Heo 	 * If SCX_OPS_ENQ_MIGRATION_DISABLED is not set, the number of times a
985*bba2c361STejun Heo 	 * migration disabled task skips ops.enqueue() and is dispatched to its
986*bba2c361STejun Heo 	 * local DSQ.
987*bba2c361STejun Heo 	 */
988*bba2c361STejun Heo 	s64		SCX_EV_ENQ_SKIP_MIGRATION_DISABLED;
989*bba2c361STejun Heo 
990*bba2c361STejun Heo 	/*
991*bba2c361STejun Heo 	 * The number of times a task, enqueued on a local DSQ with
992*bba2c361STejun Heo 	 * SCX_ENQ_IMMED, was re-enqueued because the CPU was not available for
993*bba2c361STejun Heo 	 * immediate execution.
994*bba2c361STejun Heo 	 */
995*bba2c361STejun Heo 	s64		SCX_EV_REENQ_IMMED;
996*bba2c361STejun Heo 
997*bba2c361STejun Heo 	/*
998*bba2c361STejun Heo 	 * The number of times a reenq of local DSQ caused another reenq of
999*bba2c361STejun Heo 	 * local DSQ. This can happen when %SCX_ENQ_IMMED races against a higher
1000*bba2c361STejun Heo 	 * priority class task even if the BPF scheduler always satisfies the
1001*bba2c361STejun Heo 	 * prerequisites for %SCX_ENQ_IMMED at the time of enqueue. However,
1002*bba2c361STejun Heo 	 * that scenario is very unlikely and this count going up regularly
1003*bba2c361STejun Heo 	 * indicates that the BPF scheduler is handling %SCX_ENQ_REENQ
1004*bba2c361STejun Heo 	 * incorrectly causing recursive reenqueues.
1005*bba2c361STejun Heo 	 */
1006*bba2c361STejun Heo 	s64		SCX_EV_REENQ_LOCAL_REPEAT;
1007*bba2c361STejun Heo 
1008*bba2c361STejun Heo 	/*
1009*bba2c361STejun Heo 	 * Total number of times a task's time slice was refilled with the
1010*bba2c361STejun Heo 	 * default value (SCX_SLICE_DFL).
1011*bba2c361STejun Heo 	 */
1012*bba2c361STejun Heo 	s64		SCX_EV_REFILL_SLICE_DFL;
1013*bba2c361STejun Heo 
1014*bba2c361STejun Heo 	/*
1015*bba2c361STejun Heo 	 * The total duration of bypass modes in nanoseconds.
1016*bba2c361STejun Heo 	 */
1017*bba2c361STejun Heo 	s64		SCX_EV_BYPASS_DURATION;
1018*bba2c361STejun Heo 
1019*bba2c361STejun Heo 	/*
1020*bba2c361STejun Heo 	 * The number of tasks dispatched in the bypassing mode.
1021*bba2c361STejun Heo 	 */
1022*bba2c361STejun Heo 	s64		SCX_EV_BYPASS_DISPATCH;
1023*bba2c361STejun Heo 
1024*bba2c361STejun Heo 	/*
1025*bba2c361STejun Heo 	 * The number of times the bypassing mode has been activated.
1026*bba2c361STejun Heo 	 */
1027*bba2c361STejun Heo 	s64		SCX_EV_BYPASS_ACTIVATE;
1028*bba2c361STejun Heo 
1029*bba2c361STejun Heo 	/*
1030*bba2c361STejun Heo 	 * The number of times the scheduler attempted to insert a task that it
1031*bba2c361STejun Heo 	 * doesn't own into a DSQ. Such attempts are ignored.
1032*bba2c361STejun Heo 	 *
1033*bba2c361STejun Heo 	 * As BPF schedulers are allowed to ignore dequeues, it's difficult to
1034*bba2c361STejun Heo 	 * tell whether such an attempt is from a scheduler malfunction or an
1035*bba2c361STejun Heo 	 * ignored dequeue around sub-sched enabling. If this count keeps going
1036*bba2c361STejun Heo 	 * up regardless of sub-sched enabling, it likely indicates a bug in the
1037*bba2c361STejun Heo 	 * scheduler.
1038*bba2c361STejun Heo 	 */
1039*bba2c361STejun Heo 	s64		SCX_EV_INSERT_NOT_OWNED;
1040*bba2c361STejun Heo 
1041*bba2c361STejun Heo 	/*
1042*bba2c361STejun Heo 	 * The number of times tasks from bypassing descendants are scheduled
1043*bba2c361STejun Heo 	 * from sub_bypass_dsq's.
1044*bba2c361STejun Heo 	 */
1045*bba2c361STejun Heo 	s64		SCX_EV_SUB_BYPASS_DISPATCH;
1046*bba2c361STejun Heo };
1047*bba2c361STejun Heo 
1048*bba2c361STejun Heo struct scx_sched;
1049*bba2c361STejun Heo 
1050*bba2c361STejun Heo enum scx_sched_pcpu_flags {
1051*bba2c361STejun Heo 	SCX_SCHED_PCPU_BYPASSING	= 1LLU << 0,
1052*bba2c361STejun Heo };
1053*bba2c361STejun Heo 
1054*bba2c361STejun Heo /* dispatch buf */
1055*bba2c361STejun Heo struct scx_dsp_buf_ent {
1056*bba2c361STejun Heo 	struct task_struct	*task;
1057*bba2c361STejun Heo 	unsigned long		qseq;
1058*bba2c361STejun Heo 	u64			dsq_id;
1059*bba2c361STejun Heo 	u64			enq_flags;
1060*bba2c361STejun Heo };
1061*bba2c361STejun Heo 
1062*bba2c361STejun Heo struct scx_dsp_ctx {
1063*bba2c361STejun Heo 	struct rq		*rq;
1064*bba2c361STejun Heo 	u32			cursor;
1065*bba2c361STejun Heo 	u32			nr_tasks;
1066*bba2c361STejun Heo 	struct scx_dsp_buf_ent	buf[];
1067*bba2c361STejun Heo };
1068*bba2c361STejun Heo 
1069*bba2c361STejun Heo struct scx_deferred_reenq_local {
1070*bba2c361STejun Heo 	struct list_head	node;
1071*bba2c361STejun Heo 	u64			flags;
1072*bba2c361STejun Heo 	u64			seq;
1073*bba2c361STejun Heo 	u32			cnt;
1074*bba2c361STejun Heo };
1075*bba2c361STejun Heo 
1076*bba2c361STejun Heo struct scx_sched_pcpu {
1077*bba2c361STejun Heo 	struct scx_sched	*sch;
1078*bba2c361STejun Heo 	u64			flags;	/* protected by rq lock */
1079*bba2c361STejun Heo 
1080*bba2c361STejun Heo 	/*
1081*bba2c361STejun Heo 	 * The event counters are in a per-CPU variable to minimize the
1082*bba2c361STejun Heo 	 * accounting overhead. A system-wide view on the event counter is
1083*bba2c361STejun Heo 	 * constructed when requested by scx_bpf_events().
1084*bba2c361STejun Heo 	 */
1085*bba2c361STejun Heo 	struct scx_event_stats	event_stats;
1086*bba2c361STejun Heo 
1087*bba2c361STejun Heo 	struct scx_deferred_reenq_local deferred_reenq_local;
1088*bba2c361STejun Heo 	struct scx_dispatch_q	bypass_dsq;
1089*bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED
1090*bba2c361STejun Heo 	u32			bypass_host_seq;
1091*bba2c361STejun Heo #endif
1092*bba2c361STejun Heo 
1093*bba2c361STejun Heo 	/* must be the last entry - contains flex array */
1094*bba2c361STejun Heo 	struct scx_dsp_ctx	dsp_ctx;
1095*bba2c361STejun Heo };
1096*bba2c361STejun Heo 
1097*bba2c361STejun Heo struct scx_sched_pnode {
1098*bba2c361STejun Heo 	struct scx_dispatch_q	global_dsq;
1099*bba2c361STejun Heo };
1100*bba2c361STejun Heo 
1101*bba2c361STejun Heo struct scx_sched {
1102*bba2c361STejun Heo 	/*
1103*bba2c361STejun Heo 	 * cpu-form and cid-form ops share field offsets up to .priv (verified
1104*bba2c361STejun Heo 	 * by BUILD_BUG_ON in scx_init()). The anonymous union lets the kernel
1105*bba2c361STejun Heo 	 * access either view of the same storage without function-pointer
1106*bba2c361STejun Heo 	 * casts: use .ops for cpu-form and shared fields, .ops_cid for the
1107*bba2c361STejun Heo 	 * cid-renamed callbacks (set_cmask, select_cid, cid_online, ...).
1108*bba2c361STejun Heo 	 */
1109*bba2c361STejun Heo 	union {
1110*bba2c361STejun Heo 		struct sched_ext_ops		ops;
1111*bba2c361STejun Heo 		struct sched_ext_ops_cid	ops_cid;
1112*bba2c361STejun Heo 	};
1113*bba2c361STejun Heo 	bool			is_cid_type;	/* true if registered via bpf_sched_ext_ops_cid */
1114*bba2c361STejun Heo 
1115*bba2c361STejun Heo 	/*
1116*bba2c361STejun Heo 	 * Arena map auto-discovered from member progs at struct_ops attach.
1117*bba2c361STejun Heo 	 * cid-form schedulers must use exactly one arena across all member
1118*bba2c361STejun Heo 	 * progs. NULL on cpu-form.
1119*bba2c361STejun Heo 	 *
1120*bba2c361STejun Heo 	 * @arena_pool sub-allocates @arena_map. Each gen_pool chunk is added
1121*bba2c361STejun Heo 	 * at the kernel-side mapping address. @arena_kern_base is the start
1122*bba2c361STejun Heo 	 * of the arena's kern_vm range. See scx_arena_to_kaddr() and
1123*bba2c361STejun Heo 	 * scx_kaddr_to_arena().
1124*bba2c361STejun Heo 	 */
1125*bba2c361STejun Heo 	struct bpf_map		*arena_map;
1126*bba2c361STejun Heo 	struct gen_pool		*arena_pool;
1127*bba2c361STejun Heo 	uintptr_t		arena_kern_base;
1128*bba2c361STejun Heo 
1129*bba2c361STejun Heo 	/*
1130*bba2c361STejun Heo 	 * Per-CPU arena cmask used by scx_call_op_set_cpumask() to hand a cmask
1131*bba2c361STejun Heo 	 * to ops_cid.set_cmask(). The kernel writes through the stored kern_va
1132*bba2c361STejun Heo 	 * and hands BPF its arena pointer via scx_kaddr_to_arena().
1133*bba2c361STejun Heo 	 */
1134*bba2c361STejun Heo 	struct scx_cmask * __percpu *set_cmask_scratch;
1135*bba2c361STejun Heo 
1136*bba2c361STejun Heo 	DECLARE_BITMAP(has_op, SCX_OPI_END);
1137*bba2c361STejun Heo 
1138*bba2c361STejun Heo 	/*
1139*bba2c361STejun Heo 	 * Dispatch queues.
1140*bba2c361STejun Heo 	 *
1141*bba2c361STejun Heo 	 * The global DSQ (%SCX_DSQ_GLOBAL) is split per-node for scalability.
1142*bba2c361STejun Heo 	 * This is to avoid live-locking in bypass mode where all tasks are
1143*bba2c361STejun Heo 	 * dispatched to %SCX_DSQ_GLOBAL and all CPUs consume from it. If
1144*bba2c361STejun Heo 	 * per-node split isn't sufficient, it can be further split.
1145*bba2c361STejun Heo 	 */
1146*bba2c361STejun Heo 	struct rhashtable	dsq_hash;
1147*bba2c361STejun Heo 	struct scx_sched_pnode	**pnode;
1148*bba2c361STejun Heo 	struct scx_sched_pcpu __percpu *pcpu;
1149*bba2c361STejun Heo 
1150*bba2c361STejun Heo 	u64			slice_dfl;
1151*bba2c361STejun Heo 	u64			bypass_timestamp;
1152*bba2c361STejun Heo 	s32			bypass_depth;
1153*bba2c361STejun Heo 
1154*bba2c361STejun Heo 	/* bypass dispatch path enable state, see bypass_dsp_enabled() */
1155*bba2c361STejun Heo 	unsigned long		bypass_dsp_claim;
1156*bba2c361STejun Heo 	atomic_t		bypass_dsp_enable_depth;
1157*bba2c361STejun Heo 
1158*bba2c361STejun Heo 	bool			aborting;
1159*bba2c361STejun Heo 	bool			dump_disabled;	/* protected by scx_dump_lock */
1160*bba2c361STejun Heo 	u32			dsp_max_batch;
1161*bba2c361STejun Heo 	s32			level;
1162*bba2c361STejun Heo 
1163*bba2c361STejun Heo 	/*
1164*bba2c361STejun Heo 	 * Updates to the following warned bitfields can race causing RMW issues
1165*bba2c361STejun Heo 	 * but it doesn't really matter.
1166*bba2c361STejun Heo 	 */
1167*bba2c361STejun Heo 	bool			warned_zero_slice:1;
1168*bba2c361STejun Heo 	bool			warned_deprecated_rq:1;
1169*bba2c361STejun Heo 	bool			warned_unassoc_progs:1;
1170*bba2c361STejun Heo 
1171*bba2c361STejun Heo 	struct list_head	all;
1172*bba2c361STejun Heo 
1173*bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED
1174*bba2c361STejun Heo 	struct rhash_head	hash_node;
1175*bba2c361STejun Heo 
1176*bba2c361STejun Heo 	struct list_head	children;
1177*bba2c361STejun Heo 	struct list_head	sibling;
1178*bba2c361STejun Heo 	struct cgroup		*cgrp;
1179*bba2c361STejun Heo 	char			*cgrp_path;
1180*bba2c361STejun Heo 	struct kset		*sub_kset;
1181*bba2c361STejun Heo 
1182*bba2c361STejun Heo 	bool			sub_attached;
1183*bba2c361STejun Heo #endif	/* CONFIG_EXT_SUB_SCHED */
1184*bba2c361STejun Heo 
1185*bba2c361STejun Heo 	/*
1186*bba2c361STejun Heo 	 * The maximum amount of time in jiffies that a task may be runnable
1187*bba2c361STejun Heo 	 * without being scheduled on a CPU. If this timeout is exceeded, it
1188*bba2c361STejun Heo 	 * will trigger scx_error().
1189*bba2c361STejun Heo 	 */
1190*bba2c361STejun Heo 	unsigned long		watchdog_timeout;
1191*bba2c361STejun Heo 
1192*bba2c361STejun Heo 	atomic_t		exit_kind;
1193*bba2c361STejun Heo 	struct scx_exit_info	*exit_info;
1194*bba2c361STejun Heo 
1195*bba2c361STejun Heo 	struct kobject		kobj;
1196*bba2c361STejun Heo 
1197*bba2c361STejun Heo 	struct kthread_worker	*helper;
1198*bba2c361STejun Heo 	struct irq_work		disable_irq_work;
1199*bba2c361STejun Heo 	struct kthread_work	disable_work;
1200*bba2c361STejun Heo 	struct timer_list	bypass_lb_timer;
1201*bba2c361STejun Heo 	cpumask_var_t		bypass_lb_donee_cpumask;
1202*bba2c361STejun Heo 	cpumask_var_t		bypass_lb_resched_cpumask;
1203*bba2c361STejun Heo 	struct rcu_work		rcu_work;
1204*bba2c361STejun Heo 
1205*bba2c361STejun Heo 	/* all ancestors including self */
1206*bba2c361STejun Heo 	struct scx_sched	*ancestors[];
1207*bba2c361STejun Heo };
1208*bba2c361STejun Heo 
1209*bba2c361STejun Heo /**
1210*bba2c361STejun Heo  * scx_arena_to_kaddr - Translate a BPF-arena pointer to its kernel address
1211*bba2c361STejun Heo  * @sch: scheduler whose arena hosts @bpf_ptr
1212*bba2c361STejun Heo  * @bpf_ptr: BPF-arena pointer, only the low 32 bits are used
1213*bba2c361STejun Heo  *
1214*bba2c361STejun Heo  * The (u32) cast normalizes any input into the arena's 4 GiB kern_vm range,
1215*bba2c361STejun Heo  * which combined with scratch-page fault recovery makes the returned pointer
1216*bba2c361STejun Heo  * safe to dereference up to GUARD_SZ / 2 past the intended object. Accesses
1217*bba2c361STejun Heo  * larger than GUARD_SZ / 2 must be explicitly bounds-checked.
1218*bba2c361STejun Heo  */
1219*bba2c361STejun Heo static inline void *scx_arena_to_kaddr(struct scx_sched *sch, const void *bpf_ptr)
1220*bba2c361STejun Heo {
1221*bba2c361STejun Heo 	return (void *)(sch->arena_kern_base + (u32)(uintptr_t)bpf_ptr);
1222*bba2c361STejun Heo }
1223*bba2c361STejun Heo 
1224*bba2c361STejun Heo /**
1225*bba2c361STejun Heo  * scx_kaddr_to_arena - Translate a kernel arena address to its BPF form
1226*bba2c361STejun Heo  * @sch: scheduler whose arena hosts @kaddr
1227*bba2c361STejun Heo  * @kaddr: kernel-side arena address, supplied by trusted kernel code
1228*bba2c361STejun Heo  */
1229*bba2c361STejun Heo static inline void *scx_kaddr_to_arena(struct scx_sched *sch, const void *kaddr)
1230*bba2c361STejun Heo {
1231*bba2c361STejun Heo 	return (void *)((uintptr_t)kaddr - sch->arena_kern_base);
1232*bba2c361STejun Heo }
1233*bba2c361STejun Heo 
1234*bba2c361STejun Heo enum scx_wake_flags {
1235*bba2c361STejun Heo 	/* expose select WF_* flags as enums */
1236*bba2c361STejun Heo 	SCX_WAKE_FORK		= WF_FORK,
1237*bba2c361STejun Heo 	SCX_WAKE_TTWU		= WF_TTWU,
1238*bba2c361STejun Heo 	SCX_WAKE_SYNC		= WF_SYNC,
1239*bba2c361STejun Heo };
1240*bba2c361STejun Heo 
1241*bba2c361STejun Heo enum scx_enq_flags {
1242*bba2c361STejun Heo 	/* expose select ENQUEUE_* flags as enums */
1243*bba2c361STejun Heo 	SCX_ENQ_WAKEUP		= ENQUEUE_WAKEUP,
1244*bba2c361STejun Heo 	SCX_ENQ_HEAD		= ENQUEUE_HEAD,
1245*bba2c361STejun Heo 	SCX_ENQ_CPU_SELECTED	= ENQUEUE_RQ_SELECTED,
1246*bba2c361STejun Heo 
1247*bba2c361STejun Heo 	/* high 32bits are SCX specific */
1248*bba2c361STejun Heo 
1249*bba2c361STejun Heo 	/*
1250*bba2c361STejun Heo 	 * Set the following to trigger preemption when calling
1251*bba2c361STejun Heo 	 * scx_bpf_dsq_insert() with a local dsq as the target. The slice of the
1252*bba2c361STejun Heo 	 * current task is cleared to zero and the CPU is kicked into the
1253*bba2c361STejun Heo 	 * scheduling path. Implies %SCX_ENQ_HEAD.
1254*bba2c361STejun Heo 	 */
1255*bba2c361STejun Heo 	SCX_ENQ_PREEMPT		= 1LLU << 32,
1256*bba2c361STejun Heo 
1257*bba2c361STejun Heo 	/*
1258*bba2c361STejun Heo 	 * Only allowed on local DSQs. Guarantees that the task either gets
1259*bba2c361STejun Heo 	 * on the CPU immediately and stays on it, or gets reenqueued back
1260*bba2c361STejun Heo 	 * to the BPF scheduler. It will never linger on a local DSQ or be
1261*bba2c361STejun Heo 	 * silently put back after preemption.
1262*bba2c361STejun Heo 	 *
1263*bba2c361STejun Heo 	 * The protection persists until the next fresh enqueue - it
1264*bba2c361STejun Heo 	 * survives SAVE/RESTORE cycles, slice extensions and preemption.
1265*bba2c361STejun Heo 	 * If the task can't stay on the CPU for any reason, it gets
1266*bba2c361STejun Heo 	 * reenqueued back to the BPF scheduler.
1267*bba2c361STejun Heo 	 *
1268*bba2c361STejun Heo 	 * Exiting and migration-disabled tasks bypass ops.enqueue() and
1269*bba2c361STejun Heo 	 * are placed directly on a local DSQ without IMMED protection
1270*bba2c361STejun Heo 	 * unless %SCX_OPS_ENQ_EXITING and %SCX_OPS_ENQ_MIGRATION_DISABLED
1271*bba2c361STejun Heo 	 * are set respectively.
1272*bba2c361STejun Heo 	 */
1273*bba2c361STejun Heo 	SCX_ENQ_IMMED		= 1LLU << 33,
1274*bba2c361STejun Heo 
1275*bba2c361STejun Heo 	/*
1276*bba2c361STejun Heo 	 * The task being enqueued was previously enqueued on a DSQ, but was
1277*bba2c361STejun Heo 	 * removed and is being re-enqueued. See SCX_TASK_REENQ_* flags to find
1278*bba2c361STejun Heo 	 * out why a given task is being reenqueued.
1279*bba2c361STejun Heo 	 */
1280*bba2c361STejun Heo 	SCX_ENQ_REENQ		= 1LLU << 40,
1281*bba2c361STejun Heo 
1282*bba2c361STejun Heo 	/*
1283*bba2c361STejun Heo 	 * The task being enqueued is the only task available for the cpu. By
1284*bba2c361STejun Heo 	 * default, ext core keeps executing such tasks but when
1285*bba2c361STejun Heo 	 * %SCX_OPS_ENQ_LAST is specified, they're ops.enqueue()'d with the
1286*bba2c361STejun Heo 	 * %SCX_ENQ_LAST flag set.
1287*bba2c361STejun Heo 	 *
1288*bba2c361STejun Heo 	 * The BPF scheduler is responsible for triggering a follow-up
1289*bba2c361STejun Heo 	 * scheduling event. Otherwise, Execution may stall.
1290*bba2c361STejun Heo 	 */
1291*bba2c361STejun Heo 	SCX_ENQ_LAST		= 1LLU << 41,
1292*bba2c361STejun Heo 
1293*bba2c361STejun Heo 	/* high 8 bits are internal */
1294*bba2c361STejun Heo 	__SCX_ENQ_INTERNAL_MASK	= 0xffLLU << 56,
1295*bba2c361STejun Heo 
1296*bba2c361STejun Heo 	SCX_ENQ_CLEAR_OPSS	= 1LLU << 56,
1297*bba2c361STejun Heo 	SCX_ENQ_DSQ_PRIQ	= 1LLU << 57,
1298*bba2c361STejun Heo 	SCX_ENQ_NESTED		= 1LLU << 58,
1299*bba2c361STejun Heo 	SCX_ENQ_GDSQ_FALLBACK	= 1LLU << 59,	/* fell back to global DSQ */
1300*bba2c361STejun Heo };
1301*bba2c361STejun Heo 
1302*bba2c361STejun Heo enum scx_deq_flags {
1303*bba2c361STejun Heo 	/* expose select DEQUEUE_* flags as enums */
1304*bba2c361STejun Heo 	SCX_DEQ_SLEEP		= DEQUEUE_SLEEP,
1305*bba2c361STejun Heo 
1306*bba2c361STejun Heo 	/* high 32bits are SCX specific */
1307*bba2c361STejun Heo 
1308*bba2c361STejun Heo 	/*
1309*bba2c361STejun Heo 	 * The generic core-sched layer decided to execute the task even though
1310*bba2c361STejun Heo 	 * it hasn't been dispatched yet. Dequeue from the BPF side.
1311*bba2c361STejun Heo 	 */
1312*bba2c361STejun Heo 	SCX_DEQ_CORE_SCHED_EXEC	= 1LLU << 32,
1313*bba2c361STejun Heo 
1314*bba2c361STejun Heo 	/*
1315*bba2c361STejun Heo 	 * The task is being dequeued due to a property change (e.g.,
1316*bba2c361STejun Heo 	 * sched_setaffinity(), sched_setscheduler(), set_user_nice(),
1317*bba2c361STejun Heo 	 * etc.).
1318*bba2c361STejun Heo 	 */
1319*bba2c361STejun Heo 	SCX_DEQ_SCHED_CHANGE	= 1LLU << 33,
1320*bba2c361STejun Heo };
1321*bba2c361STejun Heo 
1322*bba2c361STejun Heo enum scx_reenq_flags {
1323*bba2c361STejun Heo 	/* low 16bits determine which tasks should be reenqueued */
1324*bba2c361STejun Heo 	SCX_REENQ_ANY		= 1LLU << 0,	/* all tasks */
1325*bba2c361STejun Heo 
1326*bba2c361STejun Heo 	__SCX_REENQ_FILTER_MASK	= 0xffffLLU,
1327*bba2c361STejun Heo 
1328*bba2c361STejun Heo 	__SCX_REENQ_USER_MASK	= SCX_REENQ_ANY,
1329*bba2c361STejun Heo 
1330*bba2c361STejun Heo 	/* bits 32-35 used by task_should_reenq() */
1331*bba2c361STejun Heo 	SCX_REENQ_TSR_RQ_OPEN	= 1LLU << 32,
1332*bba2c361STejun Heo 	SCX_REENQ_TSR_NOT_FIRST	= 1LLU << 33,
1333*bba2c361STejun Heo 
1334*bba2c361STejun Heo 	__SCX_REENQ_TSR_MASK	= 0xfLLU << 32,
1335*bba2c361STejun Heo };
1336*bba2c361STejun Heo 
1337*bba2c361STejun Heo enum scx_pick_idle_cpu_flags {
1338*bba2c361STejun Heo 	SCX_PICK_IDLE_CORE	= 1LLU << 0,	/* pick a CPU whose SMT siblings are also idle */
1339*bba2c361STejun Heo 	SCX_PICK_IDLE_IN_NODE	= 1LLU << 1,	/* pick a CPU in the same target NUMA node */
1340*bba2c361STejun Heo };
1341*bba2c361STejun Heo 
1342*bba2c361STejun Heo enum scx_kick_flags {
1343*bba2c361STejun Heo 	/*
1344*bba2c361STejun Heo 	 * Kick the target CPU if idle. Guarantees that the target CPU goes
1345*bba2c361STejun Heo 	 * through at least one full scheduling cycle before going idle. If the
1346*bba2c361STejun Heo 	 * target CPU can be determined to be currently not idle and going to go
1347*bba2c361STejun Heo 	 * through a scheduling cycle before going idle, noop.
1348*bba2c361STejun Heo 	 */
1349*bba2c361STejun Heo 	SCX_KICK_IDLE		= 1LLU << 0,
1350*bba2c361STejun Heo 
1351*bba2c361STejun Heo 	/*
1352*bba2c361STejun Heo 	 * Preempt the current task and execute the dispatch path. If the
1353*bba2c361STejun Heo 	 * current task of the target CPU is an SCX task, its ->scx.slice is
1354*bba2c361STejun Heo 	 * cleared to zero before the scheduling path is invoked so that the
1355*bba2c361STejun Heo 	 * task expires and the dispatch path is invoked.
1356*bba2c361STejun Heo 	 */
1357*bba2c361STejun Heo 	SCX_KICK_PREEMPT	= 1LLU << 1,
1358*bba2c361STejun Heo 
1359*bba2c361STejun Heo 	/*
1360*bba2c361STejun Heo 	 * The scx_bpf_kick_cpu() call will return after the current SCX task of
1361*bba2c361STejun Heo 	 * the target CPU switches out. This can be used to implement e.g. core
1362*bba2c361STejun Heo 	 * scheduling. This has no effect if the current task on the target CPU
1363*bba2c361STejun Heo 	 * is not on SCX.
1364*bba2c361STejun Heo 	 */
1365*bba2c361STejun Heo 	SCX_KICK_WAIT		= 1LLU << 2,
1366*bba2c361STejun Heo };
1367*bba2c361STejun Heo 
1368*bba2c361STejun Heo enum scx_tg_flags {
1369*bba2c361STejun Heo 	SCX_TG_ONLINE		= 1U << 0,
1370*bba2c361STejun Heo 	SCX_TG_INITED		= 1U << 1,
1371*bba2c361STejun Heo };
1372*bba2c361STejun Heo 
1373*bba2c361STejun Heo enum scx_enable_state {
1374*bba2c361STejun Heo 	SCX_ENABLING,
1375*bba2c361STejun Heo 	SCX_ENABLED,
1376*bba2c361STejun Heo 	SCX_DISABLING,
1377*bba2c361STejun Heo 	SCX_DISABLED,
1378*bba2c361STejun Heo };
1379*bba2c361STejun Heo 
1380*bba2c361STejun Heo static const char *scx_enable_state_str[] = {
1381*bba2c361STejun Heo 	[SCX_ENABLING]		= "enabling",
1382*bba2c361STejun Heo 	[SCX_ENABLED]		= "enabled",
1383*bba2c361STejun Heo 	[SCX_DISABLING]		= "disabling",
1384*bba2c361STejun Heo 	[SCX_DISABLED]		= "disabled",
1385*bba2c361STejun Heo };
1386*bba2c361STejun Heo 
1387*bba2c361STejun Heo /*
1388*bba2c361STejun Heo  * Task Ownership State Machine (sched_ext_entity->ops_state)
1389*bba2c361STejun Heo  *
1390*bba2c361STejun Heo  * The sched_ext core uses this state machine to track task ownership
1391*bba2c361STejun Heo  * between the SCX core and the BPF scheduler. This allows the BPF
1392*bba2c361STejun Heo  * scheduler to dispatch tasks without strict ordering requirements, while
1393*bba2c361STejun Heo  * the SCX core safely rejects invalid dispatches.
1394*bba2c361STejun Heo  *
1395*bba2c361STejun Heo  * State Transitions
1396*bba2c361STejun Heo  *
1397*bba2c361STejun Heo  *       .------------> NONE (owned by SCX core)
1398*bba2c361STejun Heo  *       |               |           ^
1399*bba2c361STejun Heo  *       |       enqueue |           | direct dispatch
1400*bba2c361STejun Heo  *       |               v           |
1401*bba2c361STejun Heo  *       |           QUEUEING -------'
1402*bba2c361STejun Heo  *       |               |
1403*bba2c361STejun Heo  *       |       enqueue |
1404*bba2c361STejun Heo  *       |     completes |
1405*bba2c361STejun Heo  *       |               v
1406*bba2c361STejun Heo  *       |            QUEUED (owned by BPF scheduler)
1407*bba2c361STejun Heo  *       |               |
1408*bba2c361STejun Heo  *       |      dispatch |
1409*bba2c361STejun Heo  *       |               |
1410*bba2c361STejun Heo  *       |               v
1411*bba2c361STejun Heo  *       |          DISPATCHING
1412*bba2c361STejun Heo  *       |               |
1413*bba2c361STejun Heo  *       |      dispatch |
1414*bba2c361STejun Heo  *       |     completes |
1415*bba2c361STejun Heo  *       `---------------'
1416*bba2c361STejun Heo  *
1417*bba2c361STejun Heo  * State Descriptions
1418*bba2c361STejun Heo  *
1419*bba2c361STejun Heo  * - %SCX_OPSS_NONE:
1420*bba2c361STejun Heo  *     Task is owned by the SCX core. It's either on a run queue, running,
1421*bba2c361STejun Heo  *     or being manipulated by the core scheduler. The BPF scheduler has no
1422*bba2c361STejun Heo  *     claim on this task.
1423*bba2c361STejun Heo  *
1424*bba2c361STejun Heo  * - %SCX_OPSS_QUEUEING:
1425*bba2c361STejun Heo  *     Transitional state while transferring a task from the SCX core to
1426*bba2c361STejun Heo  *     the BPF scheduler. The task's rq lock is held during this state.
1427*bba2c361STejun Heo  *     Since QUEUEING is both entered and exited under the rq lock, dequeue
1428*bba2c361STejun Heo  *     can never observe this state (it would be a BUG). When finishing a
1429*bba2c361STejun Heo  *     dispatch, if the task is still in %SCX_OPSS_QUEUEING the completion
1430*bba2c361STejun Heo  *     path busy-waits for it to leave this state (via wait_ops_state())
1431*bba2c361STejun Heo  *     before retrying.
1432*bba2c361STejun Heo  *
1433*bba2c361STejun Heo  * - %SCX_OPSS_QUEUED:
1434*bba2c361STejun Heo  *     Task is owned by the BPF scheduler. It's on a DSQ (dispatch queue)
1435*bba2c361STejun Heo  *     and the BPF scheduler is responsible for dispatching it. A QSEQ
1436*bba2c361STejun Heo  *     (queue sequence number) is embedded in this state to detect
1437*bba2c361STejun Heo  *     dispatch/dequeue races: if a task is dequeued and re-enqueued, the
1438*bba2c361STejun Heo  *     QSEQ changes and any in-flight dispatch operations targeting the old
1439*bba2c361STejun Heo  *     QSEQ are safely ignored.
1440*bba2c361STejun Heo  *
1441*bba2c361STejun Heo  * - %SCX_OPSS_DISPATCHING:
1442*bba2c361STejun Heo  *     Transitional state while transferring a task from the BPF scheduler
1443*bba2c361STejun Heo  *     back to the SCX core. This state indicates the BPF scheduler has
1444*bba2c361STejun Heo  *     selected the task for execution. When dequeue needs to take the task
1445*bba2c361STejun Heo  *     off a DSQ and it is still in %SCX_OPSS_DISPATCHING, the dequeue path
1446*bba2c361STejun Heo  *     busy-waits for it to leave this state (via wait_ops_state()) before
1447*bba2c361STejun Heo  *     proceeding. Exits to %SCX_OPSS_NONE when dispatch completes.
1448*bba2c361STejun Heo  *
1449*bba2c361STejun Heo  * Memory Ordering
1450*bba2c361STejun Heo  *
1451*bba2c361STejun Heo  * Transitions out of %SCX_OPSS_QUEUEING and %SCX_OPSS_DISPATCHING into
1452*bba2c361STejun Heo  * %SCX_OPSS_NONE or %SCX_OPSS_QUEUED must use atomic_long_set_release()
1453*bba2c361STejun Heo  * and waiters must use atomic_long_read_acquire(). This ensures proper
1454*bba2c361STejun Heo  * synchronization between concurrent operations.
1455*bba2c361STejun Heo  *
1456*bba2c361STejun Heo  * Cross-CPU Task Migration
1457*bba2c361STejun Heo  *
1458*bba2c361STejun Heo  * When moving a task in the %SCX_OPSS_DISPATCHING state, we can't simply
1459*bba2c361STejun Heo  * grab the target CPU's rq lock because a concurrent dequeue might be
1460*bba2c361STejun Heo  * waiting on %SCX_OPSS_DISPATCHING while holding the source rq lock
1461*bba2c361STejun Heo  * (deadlock).
1462*bba2c361STejun Heo  *
1463*bba2c361STejun Heo  * The sched_ext core uses a "lock dancing" protocol coordinated by
1464*bba2c361STejun Heo  * p->scx.holding_cpu. When moving a task to a different rq:
1465*bba2c361STejun Heo  *
1466*bba2c361STejun Heo  *   1. Verify task can be moved (CPU affinity, migration_disabled, etc.)
1467*bba2c361STejun Heo  *   2. Set p->scx.holding_cpu to the current CPU
1468*bba2c361STejun Heo  *   3. Set task state to %SCX_OPSS_NONE; dequeue waits while DISPATCHING
1469*bba2c361STejun Heo  *      is set, so clearing DISPATCHING first prevents the circular wait
1470*bba2c361STejun Heo  *      (safe to lock the rq we need)
1471*bba2c361STejun Heo  *   4. Unlock the current CPU's rq
1472*bba2c361STejun Heo  *   5. Lock src_rq (where the task currently lives)
1473*bba2c361STejun Heo  *   6. Verify p->scx.holding_cpu == current CPU, if not, dequeue won the
1474*bba2c361STejun Heo  *      race (dequeue clears holding_cpu to -1 when it takes the task), in
1475*bba2c361STejun Heo  *      this case migration is aborted
1476*bba2c361STejun Heo  *   7. If src_rq == dst_rq: clear holding_cpu and enqueue directly
1477*bba2c361STejun Heo  *      into dst_rq's local DSQ (no lock swap needed)
1478*bba2c361STejun Heo  *   8. Otherwise: call move_remote_task_to_local_dsq(), which releases
1479*bba2c361STejun Heo  *      src_rq, locks dst_rq, and performs the deactivate/activate
1480*bba2c361STejun Heo  *      migration cycle (dst_rq is held on return)
1481*bba2c361STejun Heo  *   9. Unlock dst_rq and re-lock the current CPU's rq to restore
1482*bba2c361STejun Heo  *      the lock state expected by the caller
1483*bba2c361STejun Heo  *
1484*bba2c361STejun Heo  * If any verification fails, abort the migration.
1485*bba2c361STejun Heo  *
1486*bba2c361STejun Heo  * This state tracking allows the BPF scheduler to try to dispatch any task
1487*bba2c361STejun Heo  * at any time regardless of its state. The SCX core can safely
1488*bba2c361STejun Heo  * reject/ignore invalid dispatches, simplifying the BPF scheduler
1489*bba2c361STejun Heo  * implementation.
1490*bba2c361STejun Heo  */
1491*bba2c361STejun Heo enum scx_ops_state {
1492*bba2c361STejun Heo 	SCX_OPSS_NONE,		/* owned by the SCX core */
1493*bba2c361STejun Heo 	SCX_OPSS_QUEUEING,	/* in transit to the BPF scheduler */
1494*bba2c361STejun Heo 	SCX_OPSS_QUEUED,	/* owned by the BPF scheduler */
1495*bba2c361STejun Heo 	SCX_OPSS_DISPATCHING,	/* in transit back to the SCX core */
1496*bba2c361STejun Heo 
1497*bba2c361STejun Heo 	/*
1498*bba2c361STejun Heo 	 * QSEQ brands each QUEUED instance so that, when dispatch races
1499*bba2c361STejun Heo 	 * dequeue/requeue, the dispatcher can tell whether it still has a claim
1500*bba2c361STejun Heo 	 * on the task being dispatched.
1501*bba2c361STejun Heo 	 *
1502*bba2c361STejun Heo 	 * As some 32bit archs can't do 64bit store_release/load_acquire,
1503*bba2c361STejun Heo 	 * p->scx.ops_state is atomic_long_t which leaves 30 bits for QSEQ on
1504*bba2c361STejun Heo 	 * 32bit machines. The dispatch race window QSEQ protects is very narrow
1505*bba2c361STejun Heo 	 * and runs with IRQ disabled. 30 bits should be sufficient.
1506*bba2c361STejun Heo 	 */
1507*bba2c361STejun Heo 	SCX_OPSS_QSEQ_SHIFT	= 2,
1508*bba2c361STejun Heo };
1509*bba2c361STejun Heo 
1510*bba2c361STejun Heo /* Use macros to ensure that the type is unsigned long for the masks */
1511*bba2c361STejun Heo #define SCX_OPSS_STATE_MASK	((1LU << SCX_OPSS_QSEQ_SHIFT) - 1)
1512*bba2c361STejun Heo #define SCX_OPSS_QSEQ_MASK	(~SCX_OPSS_STATE_MASK)
1513*bba2c361STejun Heo 
1514*bba2c361STejun Heo extern struct scx_sched __rcu *scx_root;
1515*bba2c361STejun Heo DECLARE_PER_CPU(struct rq *, scx_locked_rq_state);
1516*bba2c361STejun Heo 
1517*bba2c361STejun Heo /*
1518*bba2c361STejun Heo  * True when the currently loaded scheduler hierarchy is cid-form. All scheds
1519*bba2c361STejun Heo  * in a hierarchy share one form, so this single key tells callsites which
1520*bba2c361STejun Heo  * view to use without per-sch dereferences. Use scx_is_cid_type() to test.
1521*bba2c361STejun Heo  */
1522*bba2c361STejun Heo DECLARE_STATIC_KEY_FALSE(__scx_is_cid_type);
1523*bba2c361STejun Heo 
1524*bba2c361STejun Heo int scx_kfunc_context_filter(const struct bpf_prog *prog, u32 kfunc_id);
1525*bba2c361STejun Heo 
1526*bba2c361STejun Heo bool scx_cpu_valid(struct scx_sched *sch, s32 cpu, const char *where);
1527*bba2c361STejun Heo 
1528*bba2c361STejun Heo __printf(5, 0) bool scx_vexit(struct scx_sched *sch, enum scx_exit_kind kind,
1529*bba2c361STejun Heo 			      s64 exit_code, s32 exit_cpu, const char *fmt,
1530*bba2c361STejun Heo 			      va_list args);
1531*bba2c361STejun Heo __printf(5, 6) bool __scx_exit(struct scx_sched *sch, enum scx_exit_kind kind,
1532*bba2c361STejun Heo 			       s64 exit_code, s32 exit_cpu, const char *fmt, ...);
1533*bba2c361STejun Heo 
1534*bba2c361STejun Heo #define scx_exit(sch, kind, exit_code, fmt, args...)				\
1535*bba2c361STejun Heo 	__scx_exit(sch, kind, exit_code, raw_smp_processor_id(), fmt, ##args)
1536*bba2c361STejun Heo #define scx_error(sch, fmt, args...)						\
1537*bba2c361STejun Heo 	scx_exit((sch), SCX_EXIT_ERROR, 0, fmt, ##args)
1538*bba2c361STejun Heo #define scx_verror(sch, fmt, args)						\
1539*bba2c361STejun Heo 	scx_vexit((sch), SCX_EXIT_ERROR, 0, raw_smp_processor_id(), fmt, args)
1540*bba2c361STejun Heo 
1541*bba2c361STejun Heo /*
1542*bba2c361STejun Heo  * Return the rq currently locked from an scx callback, or NULL if no rq is
1543*bba2c361STejun Heo  * locked.
1544*bba2c361STejun Heo  */
1545*bba2c361STejun Heo static inline struct rq *scx_locked_rq(void)
1546*bba2c361STejun Heo {
1547*bba2c361STejun Heo 	return __this_cpu_read(scx_locked_rq_state);
1548*bba2c361STejun Heo }
1549*bba2c361STejun Heo 
1550*bba2c361STejun Heo static inline bool scx_bypassing(struct scx_sched *sch, s32 cpu)
1551*bba2c361STejun Heo {
1552*bba2c361STejun Heo 	return unlikely(per_cpu_ptr(sch->pcpu, cpu)->flags &
1553*bba2c361STejun Heo 			SCX_SCHED_PCPU_BYPASSING);
1554*bba2c361STejun Heo }
1555*bba2c361STejun Heo 
1556*bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED
1557*bba2c361STejun Heo /**
1558*bba2c361STejun Heo  * scx_task_sched - Find scx_sched scheduling a task
1559*bba2c361STejun Heo  * @p: task of interest
1560*bba2c361STejun Heo  *
1561*bba2c361STejun Heo  * Return @p's scheduler instance. Must be called with @p's pi_lock or rq lock
1562*bba2c361STejun Heo  * held.
1563*bba2c361STejun Heo  */
1564*bba2c361STejun Heo static inline struct scx_sched *scx_task_sched(const struct task_struct *p)
1565*bba2c361STejun Heo {
1566*bba2c361STejun Heo 	return rcu_dereference_protected(p->scx.sched,
1567*bba2c361STejun Heo 					 lockdep_is_held(&p->pi_lock) ||
1568*bba2c361STejun Heo 					 lockdep_is_held(__rq_lockp(task_rq(p))));
1569*bba2c361STejun Heo }
1570*bba2c361STejun Heo 
1571*bba2c361STejun Heo /**
1572*bba2c361STejun Heo  * scx_task_sched_rcu - Find scx_sched scheduling a task
1573*bba2c361STejun Heo  * @p: task of interest
1574*bba2c361STejun Heo  *
1575*bba2c361STejun Heo  * Return @p's scheduler instance. The returned scx_sched is RCU protected.
1576*bba2c361STejun Heo  */
1577*bba2c361STejun Heo static inline struct scx_sched *scx_task_sched_rcu(const struct task_struct *p)
1578*bba2c361STejun Heo {
1579*bba2c361STejun Heo 	return rcu_dereference_all(p->scx.sched);
1580*bba2c361STejun Heo }
1581*bba2c361STejun Heo 
1582*bba2c361STejun Heo /**
1583*bba2c361STejun Heo  * scx_task_on_sched - Is a task on the specified sched?
1584*bba2c361STejun Heo  * @sch: sched to test against
1585*bba2c361STejun Heo  * @p: task of interest
1586*bba2c361STejun Heo  *
1587*bba2c361STejun Heo  * Returns %true if @p is on @sch, %false otherwise.
1588*bba2c361STejun Heo  */
1589*bba2c361STejun Heo static inline bool scx_task_on_sched(struct scx_sched *sch,
1590*bba2c361STejun Heo 				     const struct task_struct *p)
1591*bba2c361STejun Heo {
1592*bba2c361STejun Heo 	return rcu_access_pointer(p->scx.sched) == sch;
1593*bba2c361STejun Heo }
1594*bba2c361STejun Heo 
1595*bba2c361STejun Heo /**
1596*bba2c361STejun Heo  * scx_prog_sched - Find scx_sched associated with a BPF prog
1597*bba2c361STejun Heo  * @aux: aux passed in from BPF to a kfunc
1598*bba2c361STejun Heo  *
1599*bba2c361STejun Heo  * To be called from kfuncs. Return the scheduler instance associated with the
1600*bba2c361STejun Heo  * BPF program given the implicit kfunc argument aux. The returned scx_sched is
1601*bba2c361STejun Heo  * RCU protected.
1602*bba2c361STejun Heo  */
1603*bba2c361STejun Heo static inline struct scx_sched *scx_prog_sched(const struct bpf_prog_aux *aux)
1604*bba2c361STejun Heo {
1605*bba2c361STejun Heo 	struct sched_ext_ops *ops;
1606*bba2c361STejun Heo 	struct scx_sched *root;
1607*bba2c361STejun Heo 
1608*bba2c361STejun Heo 	ops = bpf_prog_get_assoc_struct_ops(aux);
1609*bba2c361STejun Heo 	if (likely(ops))
1610*bba2c361STejun Heo 		return rcu_dereference_all(ops->priv);
1611*bba2c361STejun Heo 
1612*bba2c361STejun Heo 	root = rcu_dereference_all(scx_root);
1613*bba2c361STejun Heo 	if (root) {
1614*bba2c361STejun Heo 		/*
1615*bba2c361STejun Heo 		 * COMPAT-v6.19: Schedulers built before sub-sched support was
1616*bba2c361STejun Heo 		 * introduced may have unassociated non-struct_ops programs.
1617*bba2c361STejun Heo 		 */
1618*bba2c361STejun Heo 		if (!root->ops.sub_attach)
1619*bba2c361STejun Heo 			return root;
1620*bba2c361STejun Heo 
1621*bba2c361STejun Heo 		if (!root->warned_unassoc_progs) {
1622*bba2c361STejun Heo 			printk_deferred(KERN_WARNING "sched_ext: Unassociated program %s (id %d)\n",
1623*bba2c361STejun Heo 					aux->name, aux->id);
1624*bba2c361STejun Heo 			root->warned_unassoc_progs = true;
1625*bba2c361STejun Heo 		}
1626*bba2c361STejun Heo 	}
1627*bba2c361STejun Heo 
1628*bba2c361STejun Heo 	return NULL;
1629*bba2c361STejun Heo }
1630*bba2c361STejun Heo #else	/* CONFIG_EXT_SUB_SCHED */
1631*bba2c361STejun Heo static inline struct scx_sched *scx_task_sched(const struct task_struct *p)
1632*bba2c361STejun Heo {
1633*bba2c361STejun Heo 	return rcu_dereference_protected(scx_root,
1634*bba2c361STejun Heo 					 lockdep_is_held(&p->pi_lock) ||
1635*bba2c361STejun Heo 					 lockdep_is_held(__rq_lockp(task_rq(p))));
1636*bba2c361STejun Heo }
1637*bba2c361STejun Heo 
1638*bba2c361STejun Heo static inline struct scx_sched *scx_task_sched_rcu(const struct task_struct *p)
1639*bba2c361STejun Heo {
1640*bba2c361STejun Heo 	return rcu_dereference_all(scx_root);
1641*bba2c361STejun Heo }
1642*bba2c361STejun Heo 
1643*bba2c361STejun Heo static inline bool scx_task_on_sched(struct scx_sched *sch,
1644*bba2c361STejun Heo 				     const struct task_struct *p)
1645*bba2c361STejun Heo {
1646*bba2c361STejun Heo 	return true;
1647*bba2c361STejun Heo }
1648*bba2c361STejun Heo 
1649*bba2c361STejun Heo static inline struct scx_sched *scx_prog_sched(const struct bpf_prog_aux *aux)
1650*bba2c361STejun Heo {
1651*bba2c361STejun Heo 	return rcu_dereference_all(scx_root);
1652*bba2c361STejun Heo }
1653*bba2c361STejun Heo #endif	/* CONFIG_EXT_SUB_SCHED */
1654