1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst
4  *
5  * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
6  * Copyright (c) 2022 Tejun Heo <tj@kernel.org>
7  * Copyright (c) 2022 David Vernet <dvernet@meta.com>
8  */
9 #include <linux/btf_ids.h>
10 #include "ext_idle.h"
11 
12 #define SCX_OP_IDX(op)		(offsetof(struct sched_ext_ops, op) / sizeof(void (*)(void)))
13 
14 enum scx_consts {
15 	SCX_DSP_DFL_MAX_BATCH		= 32,
16 	SCX_DSP_MAX_LOOPS		= 32,
17 	SCX_WATCHDOG_MAX_TIMEOUT	= 30 * HZ,
18 
19 	SCX_EXIT_BT_LEN			= 64,
20 	SCX_EXIT_MSG_LEN		= 1024,
21 	SCX_EXIT_DUMP_DFL_LEN		= 32768,
22 
23 	SCX_CPUPERF_ONE			= SCHED_CAPACITY_SCALE,
24 
25 	/*
26 	 * Iterating all tasks may take a while. Periodically drop
27 	 * scx_tasks_lock to avoid causing e.g. CSD and RCU stalls.
28 	 */
29 	SCX_TASK_ITER_BATCH		= 32,
30 };
31 
32 enum scx_exit_kind {
33 	SCX_EXIT_NONE,
34 	SCX_EXIT_DONE,
35 
36 	SCX_EXIT_UNREG = 64,	/* user-space initiated unregistration */
37 	SCX_EXIT_UNREG_BPF,	/* BPF-initiated unregistration */
38 	SCX_EXIT_UNREG_KERN,	/* kernel-initiated unregistration */
39 	SCX_EXIT_SYSRQ,		/* requested by 'S' sysrq */
40 
41 	SCX_EXIT_ERROR = 1024,	/* runtime error, error msg contains details */
42 	SCX_EXIT_ERROR_BPF,	/* ERROR but triggered through scx_bpf_error() */
43 	SCX_EXIT_ERROR_STALL,	/* watchdog detected stalled runnable tasks */
44 };
45 
46 /*
47  * An exit code can be specified when exiting with scx_bpf_exit() or scx_exit(),
48  * corresponding to exit_kind UNREG_BPF and UNREG_KERN respectively. The codes
49  * are 64bit of the format:
50  *
51  *   Bits: [63  ..  48 47   ..  32 31 .. 0]
52  *         [ SYS ACT ] [ SYS RSN ] [ USR  ]
53  *
54  *   SYS ACT: System-defined exit actions
55  *   SYS RSN: System-defined exit reasons
56  *   USR    : User-defined exit codes and reasons
57  *
58  * Using the above, users may communicate intention and context by ORing system
59  * actions and/or system reasons with a user-defined exit code.
60  */
61 enum scx_exit_code {
62 	/* Reasons */
63 	SCX_ECODE_RSN_HOTPLUG	= 1LLU << 32,
64 
65 	/* Actions */
66 	SCX_ECODE_ACT_RESTART	= 1LLU << 48,
67 };
68 
69 /*
70  * scx_exit_info is passed to ops.exit() to describe why the BPF scheduler is
71  * being disabled.
72  */
73 struct scx_exit_info {
74 	/* %SCX_EXIT_* - broad category of the exit reason */
75 	enum scx_exit_kind	kind;
76 
77 	/* exit code if gracefully exiting */
78 	s64			exit_code;
79 
80 	/* textual representation of the above */
81 	const char		*reason;
82 
83 	/* backtrace if exiting due to an error */
84 	unsigned long		*bt;
85 	u32			bt_len;
86 
87 	/* informational message */
88 	char			*msg;
89 
90 	/* debug dump */
91 	char			*dump;
92 };
93 
94 /* sched_ext_ops.flags */
95 enum scx_ops_flags {
96 	/*
97 	 * Keep built-in idle tracking even if ops.update_idle() is implemented.
98 	 */
99 	SCX_OPS_KEEP_BUILTIN_IDLE	= 1LLU << 0,
100 
101 	/*
102 	 * By default, if there are no other task to run on the CPU, ext core
103 	 * keeps running the current task even after its slice expires. If this
104 	 * flag is specified, such tasks are passed to ops.enqueue() with
105 	 * %SCX_ENQ_LAST. See the comment above %SCX_ENQ_LAST for more info.
106 	 */
107 	SCX_OPS_ENQ_LAST		= 1LLU << 1,
108 
109 	/*
110 	 * An exiting task may schedule after PF_EXITING is set. In such cases,
111 	 * bpf_task_from_pid() may not be able to find the task and if the BPF
112 	 * scheduler depends on pid lookup for dispatching, the task will be
113 	 * lost leading to various issues including RCU grace period stalls.
114 	 *
115 	 * To mask this problem, by default, unhashed tasks are automatically
116 	 * dispatched to the local DSQ on enqueue. If the BPF scheduler doesn't
117 	 * depend on pid lookups and wants to handle these tasks directly, the
118 	 * following flag can be used.
119 	 */
120 	SCX_OPS_ENQ_EXITING		= 1LLU << 2,
121 
122 	/*
123 	 * If set, only tasks with policy set to SCHED_EXT are attached to
124 	 * sched_ext. If clear, SCHED_NORMAL tasks are also included.
125 	 */
126 	SCX_OPS_SWITCH_PARTIAL		= 1LLU << 3,
127 
128 	/*
129 	 * A migration disabled task can only execute on its current CPU. By
130 	 * default, such tasks are automatically put on the CPU's local DSQ with
131 	 * the default slice on enqueue. If this ops flag is set, they also go
132 	 * through ops.enqueue().
133 	 *
134 	 * A migration disabled task never invokes ops.select_cpu() as it can
135 	 * only select the current CPU. Also, p->cpus_ptr will only contain its
136 	 * current CPU while p->nr_cpus_allowed keeps tracking p->user_cpus_ptr
137 	 * and thus may disagree with cpumask_weight(p->cpus_ptr).
138 	 */
139 	SCX_OPS_ENQ_MIGRATION_DISABLED	= 1LLU << 4,
140 
141 	/*
142 	 * Queued wakeup (ttwu_queue) is a wakeup optimization that invokes
143 	 * ops.enqueue() on the ops.select_cpu() selected or the wakee's
144 	 * previous CPU via IPI (inter-processor interrupt) to reduce cacheline
145 	 * transfers. When this optimization is enabled, ops.select_cpu() is
146 	 * skipped in some cases (when racing against the wakee switching out).
147 	 * As the BPF scheduler may depend on ops.select_cpu() being invoked
148 	 * during wakeups, queued wakeup is disabled by default.
149 	 *
150 	 * If this ops flag is set, queued wakeup optimization is enabled and
151 	 * the BPF scheduler must be able to handle ops.enqueue() invoked on the
152 	 * wakee's CPU without preceding ops.select_cpu() even for tasks which
153 	 * may be executed on multiple CPUs.
154 	 */
155 	SCX_OPS_ALLOW_QUEUED_WAKEUP	= 1LLU << 5,
156 
157 	/*
158 	 * If set, enable per-node idle cpumasks. If clear, use a single global
159 	 * flat idle cpumask.
160 	 */
161 	SCX_OPS_BUILTIN_IDLE_PER_NODE	= 1LLU << 6,
162 
163 	/*
164 	 * CPU cgroup support flags
165 	 */
166 	SCX_OPS_HAS_CGROUP_WEIGHT	= 1LLU << 16,	/* DEPRECATED, will be removed on 6.18 */
167 
168 	SCX_OPS_ALL_FLAGS		= SCX_OPS_KEEP_BUILTIN_IDLE |
169 					  SCX_OPS_ENQ_LAST |
170 					  SCX_OPS_ENQ_EXITING |
171 					  SCX_OPS_ENQ_MIGRATION_DISABLED |
172 					  SCX_OPS_ALLOW_QUEUED_WAKEUP |
173 					  SCX_OPS_SWITCH_PARTIAL |
174 					  SCX_OPS_BUILTIN_IDLE_PER_NODE |
175 					  SCX_OPS_HAS_CGROUP_WEIGHT,
176 
177 	/* high 8 bits are internal, don't include in SCX_OPS_ALL_FLAGS */
178 	__SCX_OPS_INTERNAL_MASK		= 0xffLLU << 56,
179 
180 	SCX_OPS_HAS_CPU_PREEMPT		= 1LLU << 56,
181 };
182 
183 /* argument container for ops.init_task() */
184 struct scx_init_task_args {
185 	/*
186 	 * Set if ops.init_task() is being invoked on the fork path, as opposed
187 	 * to the scheduler transition path.
188 	 */
189 	bool			fork;
190 #ifdef CONFIG_EXT_GROUP_SCHED
191 	/* the cgroup the task is joining */
192 	struct cgroup		*cgroup;
193 #endif
194 };
195 
196 /* argument container for ops.exit_task() */
197 struct scx_exit_task_args {
198 	/* Whether the task exited before running on sched_ext. */
199 	bool cancelled;
200 };
201 
202 /* argument container for ops->cgroup_init() */
203 struct scx_cgroup_init_args {
204 	/* the weight of the cgroup [1..10000] */
205 	u32			weight;
206 };
207 
208 enum scx_cpu_preempt_reason {
209 	/* next task is being scheduled by &sched_class_rt */
210 	SCX_CPU_PREEMPT_RT,
211 	/* next task is being scheduled by &sched_class_dl */
212 	SCX_CPU_PREEMPT_DL,
213 	/* next task is being scheduled by &sched_class_stop */
214 	SCX_CPU_PREEMPT_STOP,
215 	/* unknown reason for SCX being preempted */
216 	SCX_CPU_PREEMPT_UNKNOWN,
217 };
218 
219 /*
220  * Argument container for ops->cpu_acquire(). Currently empty, but may be
221  * expanded in the future.
222  */
223 struct scx_cpu_acquire_args {};
224 
225 /* argument container for ops->cpu_release() */
226 struct scx_cpu_release_args {
227 	/* the reason the CPU was preempted */
228 	enum scx_cpu_preempt_reason reason;
229 
230 	/* the task that's going to be scheduled on the CPU */
231 	struct task_struct	*task;
232 };
233 
234 /*
235  * Informational context provided to dump operations.
236  */
237 struct scx_dump_ctx {
238 	enum scx_exit_kind	kind;
239 	s64			exit_code;
240 	const char		*reason;
241 	u64			at_ns;
242 	u64			at_jiffies;
243 };
244 
245 /**
246  * struct sched_ext_ops - Operation table for BPF scheduler implementation
247  *
248  * A BPF scheduler can implement an arbitrary scheduling policy by
249  * implementing and loading operations in this table. Note that a userland
250  * scheduling policy can also be implemented using the BPF scheduler
251  * as a shim layer.
252  */
253 struct sched_ext_ops {
254 	/**
255 	 * @select_cpu: Pick the target CPU for a task which is being woken up
256 	 * @p: task being woken up
257 	 * @prev_cpu: the cpu @p was on before sleeping
258 	 * @wake_flags: SCX_WAKE_*
259 	 *
260 	 * Decision made here isn't final. @p may be moved to any CPU while it
261 	 * is getting dispatched for execution later. However, as @p is not on
262 	 * the rq at this point, getting the eventual execution CPU right here
263 	 * saves a small bit of overhead down the line.
264 	 *
265 	 * If an idle CPU is returned, the CPU is kicked and will try to
266 	 * dispatch. While an explicit custom mechanism can be added,
267 	 * select_cpu() serves as the default way to wake up idle CPUs.
268 	 *
269 	 * @p may be inserted into a DSQ directly by calling
270 	 * scx_bpf_dsq_insert(). If so, the ops.enqueue() will be skipped.
271 	 * Directly inserting into %SCX_DSQ_LOCAL will put @p in the local DSQ
272 	 * of the CPU returned by this operation.
273 	 *
274 	 * Note that select_cpu() is never called for tasks that can only run
275 	 * on a single CPU or tasks with migration disabled, as they don't have
276 	 * the option to select a different CPU. See select_task_rq() for
277 	 * details.
278 	 */
279 	s32 (*select_cpu)(struct task_struct *p, s32 prev_cpu, u64 wake_flags);
280 
281 	/**
282 	 * @enqueue: Enqueue a task on the BPF scheduler
283 	 * @p: task being enqueued
284 	 * @enq_flags: %SCX_ENQ_*
285 	 *
286 	 * @p is ready to run. Insert directly into a DSQ by calling
287 	 * scx_bpf_dsq_insert() or enqueue on the BPF scheduler. If not directly
288 	 * inserted, the bpf scheduler owns @p and if it fails to dispatch @p,
289 	 * the task will stall.
290 	 *
291 	 * If @p was inserted into a DSQ from ops.select_cpu(), this callback is
292 	 * skipped.
293 	 */
294 	void (*enqueue)(struct task_struct *p, u64 enq_flags);
295 
296 	/**
297 	 * @dequeue: Remove a task from the BPF scheduler
298 	 * @p: task being dequeued
299 	 * @deq_flags: %SCX_DEQ_*
300 	 *
301 	 * Remove @p from the BPF scheduler. This is usually called to isolate
302 	 * the task while updating its scheduling properties (e.g. priority).
303 	 *
304 	 * The ext core keeps track of whether the BPF side owns a given task or
305 	 * not and can gracefully ignore spurious dispatches from BPF side,
306 	 * which makes it safe to not implement this method. However, depending
307 	 * on the scheduling logic, this can lead to confusing behaviors - e.g.
308 	 * scheduling position not being updated across a priority change.
309 	 */
310 	void (*dequeue)(struct task_struct *p, u64 deq_flags);
311 
312 	/**
313 	 * @dispatch: Dispatch tasks from the BPF scheduler and/or user DSQs
314 	 * @cpu: CPU to dispatch tasks for
315 	 * @prev: previous task being switched out
316 	 *
317 	 * Called when a CPU's local dsq is empty. The operation should dispatch
318 	 * one or more tasks from the BPF scheduler into the DSQs using
319 	 * scx_bpf_dsq_insert() and/or move from user DSQs into the local DSQ
320 	 * using scx_bpf_dsq_move_to_local().
321 	 *
322 	 * The maximum number of times scx_bpf_dsq_insert() can be called
323 	 * without an intervening scx_bpf_dsq_move_to_local() is specified by
324 	 * ops.dispatch_max_batch. See the comments on top of the two functions
325 	 * for more details.
326 	 *
327 	 * When not %NULL, @prev is an SCX task with its slice depleted. If
328 	 * @prev is still runnable as indicated by set %SCX_TASK_QUEUED in
329 	 * @prev->scx.flags, it is not enqueued yet and will be enqueued after
330 	 * ops.dispatch() returns. To keep executing @prev, return without
331 	 * dispatching or moving any tasks. Also see %SCX_OPS_ENQ_LAST.
332 	 */
333 	void (*dispatch)(s32 cpu, struct task_struct *prev);
334 
335 	/**
336 	 * @tick: Periodic tick
337 	 * @p: task running currently
338 	 *
339 	 * This operation is called every 1/HZ seconds on CPUs which are
340 	 * executing an SCX task. Setting @p->scx.slice to 0 will trigger an
341 	 * immediate dispatch cycle on the CPU.
342 	 */
343 	void (*tick)(struct task_struct *p);
344 
345 	/**
346 	 * @runnable: A task is becoming runnable on its associated CPU
347 	 * @p: task becoming runnable
348 	 * @enq_flags: %SCX_ENQ_*
349 	 *
350 	 * This and the following three functions can be used to track a task's
351 	 * execution state transitions. A task becomes ->runnable() on a CPU,
352 	 * and then goes through one or more ->running() and ->stopping() pairs
353 	 * as it runs on the CPU, and eventually becomes ->quiescent() when it's
354 	 * done running on the CPU.
355 	 *
356 	 * @p is becoming runnable on the CPU because it's
357 	 *
358 	 * - waking up (%SCX_ENQ_WAKEUP)
359 	 * - being moved from another CPU
360 	 * - being restored after temporarily taken off the queue for an
361 	 *   attribute change.
362 	 *
363 	 * This and ->enqueue() are related but not coupled. This operation
364 	 * notifies @p's state transition and may not be followed by ->enqueue()
365 	 * e.g. when @p is being dispatched to a remote CPU, or when @p is
366 	 * being enqueued on a CPU experiencing a hotplug event. Likewise, a
367 	 * task may be ->enqueue()'d without being preceded by this operation
368 	 * e.g. after exhausting its slice.
369 	 */
370 	void (*runnable)(struct task_struct *p, u64 enq_flags);
371 
372 	/**
373 	 * @running: A task is starting to run on its associated CPU
374 	 * @p: task starting to run
375 	 *
376 	 * Note that this callback may be called from a CPU other than the
377 	 * one the task is going to run on. This can happen when a task
378 	 * property is changed (i.e., affinity), since scx_next_task_scx(),
379 	 * which triggers this callback, may run on a CPU different from
380 	 * the task's assigned CPU.
381 	 *
382 	 * Therefore, always use scx_bpf_task_cpu(@p) to determine the
383 	 * target CPU the task is going to use.
384 	 *
385 	 * See ->runnable() for explanation on the task state notifiers.
386 	 */
387 	void (*running)(struct task_struct *p);
388 
389 	/**
390 	 * @stopping: A task is stopping execution
391 	 * @p: task stopping to run
392 	 * @runnable: is task @p still runnable?
393 	 *
394 	 * Note that this callback may be called from a CPU other than the
395 	 * one the task was running on. This can happen when a task
396 	 * property is changed (i.e., affinity), since dequeue_task_scx(),
397 	 * which triggers this callback, may run on a CPU different from
398 	 * the task's assigned CPU.
399 	 *
400 	 * Therefore, always use scx_bpf_task_cpu(@p) to retrieve the CPU
401 	 * the task was running on.
402 	 *
403 	 * See ->runnable() for explanation on the task state notifiers. If
404 	 * !@runnable, ->quiescent() will be invoked after this operation
405 	 * returns.
406 	 */
407 	void (*stopping)(struct task_struct *p, bool runnable);
408 
409 	/**
410 	 * @quiescent: A task is becoming not runnable on its associated CPU
411 	 * @p: task becoming not runnable
412 	 * @deq_flags: %SCX_DEQ_*
413 	 *
414 	 * See ->runnable() for explanation on the task state notifiers.
415 	 *
416 	 * @p is becoming quiescent on the CPU because it's
417 	 *
418 	 * - sleeping (%SCX_DEQ_SLEEP)
419 	 * - being moved to another CPU
420 	 * - being temporarily taken off the queue for an attribute change
421 	 *   (%SCX_DEQ_SAVE)
422 	 *
423 	 * This and ->dequeue() are related but not coupled. This operation
424 	 * notifies @p's state transition and may not be preceded by ->dequeue()
425 	 * e.g. when @p is being dispatched to a remote CPU.
426 	 */
427 	void (*quiescent)(struct task_struct *p, u64 deq_flags);
428 
429 	/**
430 	 * @yield: Yield CPU
431 	 * @from: yielding task
432 	 * @to: optional yield target task
433 	 *
434 	 * If @to is NULL, @from is yielding the CPU to other runnable tasks.
435 	 * The BPF scheduler should ensure that other available tasks are
436 	 * dispatched before the yielding task. Return value is ignored in this
437 	 * case.
438 	 *
439 	 * If @to is not-NULL, @from wants to yield the CPU to @to. If the bpf
440 	 * scheduler can implement the request, return %true; otherwise, %false.
441 	 */
442 	bool (*yield)(struct task_struct *from, struct task_struct *to);
443 
444 	/**
445 	 * @core_sched_before: Task ordering for core-sched
446 	 * @a: task A
447 	 * @b: task B
448 	 *
449 	 * Used by core-sched to determine the ordering between two tasks. See
450 	 * Documentation/admin-guide/hw-vuln/core-scheduling.rst for details on
451 	 * core-sched.
452 	 *
453 	 * Both @a and @b are runnable and may or may not currently be queued on
454 	 * the BPF scheduler. Should return %true if @a should run before @b.
455 	 * %false if there's no required ordering or @b should run before @a.
456 	 *
457 	 * If not specified, the default is ordering them according to when they
458 	 * became runnable.
459 	 */
460 	bool (*core_sched_before)(struct task_struct *a, struct task_struct *b);
461 
462 	/**
463 	 * @set_weight: Set task weight
464 	 * @p: task to set weight for
465 	 * @weight: new weight [1..10000]
466 	 *
467 	 * Update @p's weight to @weight.
468 	 */
469 	void (*set_weight)(struct task_struct *p, u32 weight);
470 
471 	/**
472 	 * @set_cpumask: Set CPU affinity
473 	 * @p: task to set CPU affinity for
474 	 * @cpumask: cpumask of cpus that @p can run on
475 	 *
476 	 * Update @p's CPU affinity to @cpumask.
477 	 */
478 	void (*set_cpumask)(struct task_struct *p,
479 			    const struct cpumask *cpumask);
480 
481 	/**
482 	 * @update_idle: Update the idle state of a CPU
483 	 * @cpu: CPU to update the idle state for
484 	 * @idle: whether entering or exiting the idle state
485 	 *
486 	 * This operation is called when @rq's CPU goes or leaves the idle
487 	 * state. By default, implementing this operation disables the built-in
488 	 * idle CPU tracking and the following helpers become unavailable:
489 	 *
490 	 * - scx_bpf_select_cpu_dfl()
491 	 * - scx_bpf_select_cpu_and()
492 	 * - scx_bpf_test_and_clear_cpu_idle()
493 	 * - scx_bpf_pick_idle_cpu()
494 	 *
495 	 * The user also must implement ops.select_cpu() as the default
496 	 * implementation relies on scx_bpf_select_cpu_dfl().
497 	 *
498 	 * Specify the %SCX_OPS_KEEP_BUILTIN_IDLE flag to keep the built-in idle
499 	 * tracking.
500 	 */
501 	void (*update_idle)(s32 cpu, bool idle);
502 
503 	/**
504 	 * @cpu_acquire: A CPU is becoming available to the BPF scheduler
505 	 * @cpu: The CPU being acquired by the BPF scheduler.
506 	 * @args: Acquire arguments, see the struct definition.
507 	 *
508 	 * A CPU that was previously released from the BPF scheduler is now once
509 	 * again under its control.
510 	 */
511 	void (*cpu_acquire)(s32 cpu, struct scx_cpu_acquire_args *args);
512 
513 	/**
514 	 * @cpu_release: A CPU is taken away from the BPF scheduler
515 	 * @cpu: The CPU being released by the BPF scheduler.
516 	 * @args: Release arguments, see the struct definition.
517 	 *
518 	 * The specified CPU is no longer under the control of the BPF
519 	 * scheduler. This could be because it was preempted by a higher
520 	 * priority sched_class, though there may be other reasons as well. The
521 	 * caller should consult @args->reason to determine the cause.
522 	 */
523 	void (*cpu_release)(s32 cpu, struct scx_cpu_release_args *args);
524 
525 	/**
526 	 * @init_task: Initialize a task to run in a BPF scheduler
527 	 * @p: task to initialize for BPF scheduling
528 	 * @args: init arguments, see the struct definition
529 	 *
530 	 * Either we're loading a BPF scheduler or a new task is being forked.
531 	 * Initialize @p for BPF scheduling. This operation may block and can
532 	 * be used for allocations, and is called exactly once for a task.
533 	 *
534 	 * Return 0 for success, -errno for failure. An error return while
535 	 * loading will abort loading of the BPF scheduler. During a fork, it
536 	 * will abort that specific fork.
537 	 */
538 	s32 (*init_task)(struct task_struct *p, struct scx_init_task_args *args);
539 
540 	/**
541 	 * @exit_task: Exit a previously-running task from the system
542 	 * @p: task to exit
543 	 * @args: exit arguments, see the struct definition
544 	 *
545 	 * @p is exiting or the BPF scheduler is being unloaded. Perform any
546 	 * necessary cleanup for @p.
547 	 */
548 	void (*exit_task)(struct task_struct *p, struct scx_exit_task_args *args);
549 
550 	/**
551 	 * @enable: Enable BPF scheduling for a task
552 	 * @p: task to enable BPF scheduling for
553 	 *
554 	 * Enable @p for BPF scheduling. enable() is called on @p any time it
555 	 * enters SCX, and is always paired with a matching disable().
556 	 */
557 	void (*enable)(struct task_struct *p);
558 
559 	/**
560 	 * @disable: Disable BPF scheduling for a task
561 	 * @p: task to disable BPF scheduling for
562 	 *
563 	 * @p is exiting, leaving SCX or the BPF scheduler is being unloaded.
564 	 * Disable BPF scheduling for @p. A disable() call is always matched
565 	 * with a prior enable() call.
566 	 */
567 	void (*disable)(struct task_struct *p);
568 
569 	/**
570 	 * @dump: Dump BPF scheduler state on error
571 	 * @ctx: debug dump context
572 	 *
573 	 * Use scx_bpf_dump() to generate BPF scheduler specific debug dump.
574 	 */
575 	void (*dump)(struct scx_dump_ctx *ctx);
576 
577 	/**
578 	 * @dump_cpu: Dump BPF scheduler state for a CPU on error
579 	 * @ctx: debug dump context
580 	 * @cpu: CPU to generate debug dump for
581 	 * @idle: @cpu is currently idle without any runnable tasks
582 	 *
583 	 * Use scx_bpf_dump() to generate BPF scheduler specific debug dump for
584 	 * @cpu. If @idle is %true and this operation doesn't produce any
585 	 * output, @cpu is skipped for dump.
586 	 */
587 	void (*dump_cpu)(struct scx_dump_ctx *ctx, s32 cpu, bool idle);
588 
589 	/**
590 	 * @dump_task: Dump BPF scheduler state for a runnable task on error
591 	 * @ctx: debug dump context
592 	 * @p: runnable task to generate debug dump for
593 	 *
594 	 * Use scx_bpf_dump() to generate BPF scheduler specific debug dump for
595 	 * @p.
596 	 */
597 	void (*dump_task)(struct scx_dump_ctx *ctx, struct task_struct *p);
598 
599 #ifdef CONFIG_EXT_GROUP_SCHED
600 	/**
601 	 * @cgroup_init: Initialize a cgroup
602 	 * @cgrp: cgroup being initialized
603 	 * @args: init arguments, see the struct definition
604 	 *
605 	 * Either the BPF scheduler is being loaded or @cgrp created, initialize
606 	 * @cgrp for sched_ext. This operation may block.
607 	 *
608 	 * Return 0 for success, -errno for failure. An error return while
609 	 * loading will abort loading of the BPF scheduler. During cgroup
610 	 * creation, it will abort the specific cgroup creation.
611 	 */
612 	s32 (*cgroup_init)(struct cgroup *cgrp,
613 			   struct scx_cgroup_init_args *args);
614 
615 	/**
616 	 * @cgroup_exit: Exit a cgroup
617 	 * @cgrp: cgroup being exited
618 	 *
619 	 * Either the BPF scheduler is being unloaded or @cgrp destroyed, exit
620 	 * @cgrp for sched_ext. This operation my block.
621 	 */
622 	void (*cgroup_exit)(struct cgroup *cgrp);
623 
624 	/**
625 	 * @cgroup_prep_move: Prepare a task to be moved to a different cgroup
626 	 * @p: task being moved
627 	 * @from: cgroup @p is being moved from
628 	 * @to: cgroup @p is being moved to
629 	 *
630 	 * Prepare @p for move from cgroup @from to @to. This operation may
631 	 * block and can be used for allocations.
632 	 *
633 	 * Return 0 for success, -errno for failure. An error return aborts the
634 	 * migration.
635 	 */
636 	s32 (*cgroup_prep_move)(struct task_struct *p,
637 				struct cgroup *from, struct cgroup *to);
638 
639 	/**
640 	 * @cgroup_move: Commit cgroup move
641 	 * @p: task being moved
642 	 * @from: cgroup @p is being moved from
643 	 * @to: cgroup @p is being moved to
644 	 *
645 	 * Commit the move. @p is dequeued during this operation.
646 	 */
647 	void (*cgroup_move)(struct task_struct *p,
648 			    struct cgroup *from, struct cgroup *to);
649 
650 	/**
651 	 * @cgroup_cancel_move: Cancel cgroup move
652 	 * @p: task whose cgroup move is being canceled
653 	 * @from: cgroup @p was being moved from
654 	 * @to: cgroup @p was being moved to
655 	 *
656 	 * @p was cgroup_prep_move()'d but failed before reaching cgroup_move().
657 	 * Undo the preparation.
658 	 */
659 	void (*cgroup_cancel_move)(struct task_struct *p,
660 				   struct cgroup *from, struct cgroup *to);
661 
662 	/**
663 	 * @cgroup_set_weight: A cgroup's weight is being changed
664 	 * @cgrp: cgroup whose weight is being updated
665 	 * @weight: new weight [1..10000]
666 	 *
667 	 * Update @tg's weight to @weight.
668 	 */
669 	void (*cgroup_set_weight)(struct cgroup *cgrp, u32 weight);
670 #endif	/* CONFIG_EXT_GROUP_SCHED */
671 
672 	/*
673 	 * All online ops must come before ops.cpu_online().
674 	 */
675 
676 	/**
677 	 * @cpu_online: A CPU became online
678 	 * @cpu: CPU which just came up
679 	 *
680 	 * @cpu just came online. @cpu will not call ops.enqueue() or
681 	 * ops.dispatch(), nor run tasks associated with other CPUs beforehand.
682 	 */
683 	void (*cpu_online)(s32 cpu);
684 
685 	/**
686 	 * @cpu_offline: A CPU is going offline
687 	 * @cpu: CPU which is going offline
688 	 *
689 	 * @cpu is going offline. @cpu will not call ops.enqueue() or
690 	 * ops.dispatch(), nor run tasks associated with other CPUs afterwards.
691 	 */
692 	void (*cpu_offline)(s32 cpu);
693 
694 	/*
695 	 * All CPU hotplug ops must come before ops.init().
696 	 */
697 
698 	/**
699 	 * @init: Initialize the BPF scheduler
700 	 */
701 	s32 (*init)(void);
702 
703 	/**
704 	 * @exit: Clean up after the BPF scheduler
705 	 * @info: Exit info
706 	 *
707 	 * ops.exit() is also called on ops.init() failure, which is a bit
708 	 * unusual. This is to allow rich reporting through @info on how
709 	 * ops.init() failed.
710 	 */
711 	void (*exit)(struct scx_exit_info *info);
712 
713 	/**
714 	 * @dispatch_max_batch: Max nr of tasks that dispatch() can dispatch
715 	 */
716 	u32 dispatch_max_batch;
717 
718 	/**
719 	 * @flags: %SCX_OPS_* flags
720 	 */
721 	u64 flags;
722 
723 	/**
724 	 * @timeout_ms: The maximum amount of time, in milliseconds, that a
725 	 * runnable task should be able to wait before being scheduled. The
726 	 * maximum timeout may not exceed the default timeout of 30 seconds.
727 	 *
728 	 * Defaults to the maximum allowed timeout value of 30 seconds.
729 	 */
730 	u32 timeout_ms;
731 
732 	/**
733 	 * @exit_dump_len: scx_exit_info.dump buffer length. If 0, the default
734 	 * value of 32768 is used.
735 	 */
736 	u32 exit_dump_len;
737 
738 	/**
739 	 * @hotplug_seq: A sequence number that may be set by the scheduler to
740 	 * detect when a hotplug event has occurred during the loading process.
741 	 * If 0, no detection occurs. Otherwise, the scheduler will fail to
742 	 * load if the sequence number does not match @scx_hotplug_seq on the
743 	 * enable path.
744 	 */
745 	u64 hotplug_seq;
746 
747 	/**
748 	 * @name: BPF scheduler's name
749 	 *
750 	 * Must be a non-zero valid BPF object name including only isalnum(),
751 	 * '_' and '.' chars. Shows up in kernel.sched_ext_ops sysctl while the
752 	 * BPF scheduler is enabled.
753 	 */
754 	char name[SCX_OPS_NAME_LEN];
755 
756 	/* internal use only, must be NULL */
757 	void *priv;
758 };
759 
760 enum scx_opi {
761 	SCX_OPI_BEGIN			= 0,
762 	SCX_OPI_NORMAL_BEGIN		= 0,
763 	SCX_OPI_NORMAL_END		= SCX_OP_IDX(cpu_online),
764 	SCX_OPI_CPU_HOTPLUG_BEGIN	= SCX_OP_IDX(cpu_online),
765 	SCX_OPI_CPU_HOTPLUG_END		= SCX_OP_IDX(init),
766 	SCX_OPI_END			= SCX_OP_IDX(init),
767 };
768 
769 /*
770  * Collection of event counters. Event types are placed in descending order.
771  */
772 struct scx_event_stats {
773 	/*
774 	 * If ops.select_cpu() returns a CPU which can't be used by the task,
775 	 * the core scheduler code silently picks a fallback CPU.
776 	 */
777 	s64		SCX_EV_SELECT_CPU_FALLBACK;
778 
779 	/*
780 	 * When dispatching to a local DSQ, the CPU may have gone offline in
781 	 * the meantime. In this case, the task is bounced to the global DSQ.
782 	 */
783 	s64		SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE;
784 
785 	/*
786 	 * If SCX_OPS_ENQ_LAST is not set, the number of times that a task
787 	 * continued to run because there were no other tasks on the CPU.
788 	 */
789 	s64		SCX_EV_DISPATCH_KEEP_LAST;
790 
791 	/*
792 	 * If SCX_OPS_ENQ_EXITING is not set, the number of times that a task
793 	 * is dispatched to a local DSQ when exiting.
794 	 */
795 	s64		SCX_EV_ENQ_SKIP_EXITING;
796 
797 	/*
798 	 * If SCX_OPS_ENQ_MIGRATION_DISABLED is not set, the number of times a
799 	 * migration disabled task skips ops.enqueue() and is dispatched to its
800 	 * local DSQ.
801 	 */
802 	s64		SCX_EV_ENQ_SKIP_MIGRATION_DISABLED;
803 
804 	/*
805 	 * Total number of times a task's time slice was refilled with the
806 	 * default value (SCX_SLICE_DFL).
807 	 */
808 	s64		SCX_EV_REFILL_SLICE_DFL;
809 
810 	/*
811 	 * The total duration of bypass modes in nanoseconds.
812 	 */
813 	s64		SCX_EV_BYPASS_DURATION;
814 
815 	/*
816 	 * The number of tasks dispatched in the bypassing mode.
817 	 */
818 	s64		SCX_EV_BYPASS_DISPATCH;
819 
820 	/*
821 	 * The number of times the bypassing mode has been activated.
822 	 */
823 	s64		SCX_EV_BYPASS_ACTIVATE;
824 };
825 
826 struct scx_sched {
827 	struct sched_ext_ops	ops;
828 	DECLARE_BITMAP(has_op, SCX_OPI_END);
829 
830 	/*
831 	 * Dispatch queues.
832 	 *
833 	 * The global DSQ (%SCX_DSQ_GLOBAL) is split per-node for scalability.
834 	 * This is to avoid live-locking in bypass mode where all tasks are
835 	 * dispatched to %SCX_DSQ_GLOBAL and all CPUs consume from it. If
836 	 * per-node split isn't sufficient, it can be further split.
837 	 */
838 	struct rhashtable	dsq_hash;
839 	struct scx_dispatch_q	**global_dsqs;
840 
841 	/*
842 	 * The event counters are in a per-CPU variable to minimize the
843 	 * accounting overhead. A system-wide view on the event counter is
844 	 * constructed when requested by scx_bpf_events().
845 	 */
846 	struct scx_event_stats __percpu *event_stats_cpu;
847 
848 	bool			warned_zero_slice;
849 
850 	atomic_t		exit_kind;
851 	struct scx_exit_info	*exit_info;
852 
853 	struct kobject		kobj;
854 
855 	struct kthread_worker	*helper;
856 	struct irq_work		error_irq_work;
857 	struct kthread_work	disable_work;
858 	struct rcu_work		rcu_work;
859 };
860 
861 enum scx_wake_flags {
862 	/* expose select WF_* flags as enums */
863 	SCX_WAKE_FORK		= WF_FORK,
864 	SCX_WAKE_TTWU		= WF_TTWU,
865 	SCX_WAKE_SYNC		= WF_SYNC,
866 };
867 
868 enum scx_enq_flags {
869 	/* expose select ENQUEUE_* flags as enums */
870 	SCX_ENQ_WAKEUP		= ENQUEUE_WAKEUP,
871 	SCX_ENQ_HEAD		= ENQUEUE_HEAD,
872 	SCX_ENQ_CPU_SELECTED	= ENQUEUE_RQ_SELECTED,
873 
874 	/* high 32bits are SCX specific */
875 
876 	/*
877 	 * Set the following to trigger preemption when calling
878 	 * scx_bpf_dsq_insert() with a local dsq as the target. The slice of the
879 	 * current task is cleared to zero and the CPU is kicked into the
880 	 * scheduling path. Implies %SCX_ENQ_HEAD.
881 	 */
882 	SCX_ENQ_PREEMPT		= 1LLU << 32,
883 
884 	/*
885 	 * The task being enqueued was previously enqueued on the current CPU's
886 	 * %SCX_DSQ_LOCAL, but was removed from it in a call to the
887 	 * bpf_scx_reenqueue_local() kfunc. If bpf_scx_reenqueue_local() was
888 	 * invoked in a ->cpu_release() callback, and the task is again
889 	 * dispatched back to %SCX_LOCAL_DSQ by this current ->enqueue(), the
890 	 * task will not be scheduled on the CPU until at least the next invocation
891 	 * of the ->cpu_acquire() callback.
892 	 */
893 	SCX_ENQ_REENQ		= 1LLU << 40,
894 
895 	/*
896 	 * The task being enqueued is the only task available for the cpu. By
897 	 * default, ext core keeps executing such tasks but when
898 	 * %SCX_OPS_ENQ_LAST is specified, they're ops.enqueue()'d with the
899 	 * %SCX_ENQ_LAST flag set.
900 	 *
901 	 * The BPF scheduler is responsible for triggering a follow-up
902 	 * scheduling event. Otherwise, Execution may stall.
903 	 */
904 	SCX_ENQ_LAST		= 1LLU << 41,
905 
906 	/* high 8 bits are internal */
907 	__SCX_ENQ_INTERNAL_MASK	= 0xffLLU << 56,
908 
909 	SCX_ENQ_CLEAR_OPSS	= 1LLU << 56,
910 	SCX_ENQ_DSQ_PRIQ	= 1LLU << 57,
911 };
912 
913 enum scx_deq_flags {
914 	/* expose select DEQUEUE_* flags as enums */
915 	SCX_DEQ_SLEEP		= DEQUEUE_SLEEP,
916 
917 	/* high 32bits are SCX specific */
918 
919 	/*
920 	 * The generic core-sched layer decided to execute the task even though
921 	 * it hasn't been dispatched yet. Dequeue from the BPF side.
922 	 */
923 	SCX_DEQ_CORE_SCHED_EXEC	= 1LLU << 32,
924 };
925 
926 enum scx_pick_idle_cpu_flags {
927 	SCX_PICK_IDLE_CORE	= 1LLU << 0,	/* pick a CPU whose SMT siblings are also idle */
928 	SCX_PICK_IDLE_IN_NODE	= 1LLU << 1,	/* pick a CPU in the same target NUMA node */
929 };
930 
931 enum scx_kick_flags {
932 	/*
933 	 * Kick the target CPU if idle. Guarantees that the target CPU goes
934 	 * through at least one full scheduling cycle before going idle. If the
935 	 * target CPU can be determined to be currently not idle and going to go
936 	 * through a scheduling cycle before going idle, noop.
937 	 */
938 	SCX_KICK_IDLE		= 1LLU << 0,
939 
940 	/*
941 	 * Preempt the current task and execute the dispatch path. If the
942 	 * current task of the target CPU is an SCX task, its ->scx.slice is
943 	 * cleared to zero before the scheduling path is invoked so that the
944 	 * task expires and the dispatch path is invoked.
945 	 */
946 	SCX_KICK_PREEMPT	= 1LLU << 1,
947 
948 	/*
949 	 * Wait for the CPU to be rescheduled. The scx_bpf_kick_cpu() call will
950 	 * return after the target CPU finishes picking the next task.
951 	 */
952 	SCX_KICK_WAIT		= 1LLU << 2,
953 };
954 
955 enum scx_tg_flags {
956 	SCX_TG_ONLINE		= 1U << 0,
957 	SCX_TG_INITED		= 1U << 1,
958 };
959 
960 enum scx_enable_state {
961 	SCX_ENABLING,
962 	SCX_ENABLED,
963 	SCX_DISABLING,
964 	SCX_DISABLED,
965 };
966 
967 static const char *scx_enable_state_str[] = {
968 	[SCX_ENABLING]		= "enabling",
969 	[SCX_ENABLED]		= "enabled",
970 	[SCX_DISABLING]		= "disabling",
971 	[SCX_DISABLED]		= "disabled",
972 };
973 
974 /*
975  * sched_ext_entity->ops_state
976  *
977  * Used to track the task ownership between the SCX core and the BPF scheduler.
978  * State transitions look as follows:
979  *
980  * NONE -> QUEUEING -> QUEUED -> DISPATCHING
981  *   ^              |                 |
982  *   |              v                 v
983  *   \-------------------------------/
984  *
985  * QUEUEING and DISPATCHING states can be waited upon. See wait_ops_state() call
986  * sites for explanations on the conditions being waited upon and why they are
987  * safe. Transitions out of them into NONE or QUEUED must store_release and the
988  * waiters should load_acquire.
989  *
990  * Tracking scx_ops_state enables sched_ext core to reliably determine whether
991  * any given task can be dispatched by the BPF scheduler at all times and thus
992  * relaxes the requirements on the BPF scheduler. This allows the BPF scheduler
993  * to try to dispatch any task anytime regardless of its state as the SCX core
994  * can safely reject invalid dispatches.
995  */
996 enum scx_ops_state {
997 	SCX_OPSS_NONE,		/* owned by the SCX core */
998 	SCX_OPSS_QUEUEING,	/* in transit to the BPF scheduler */
999 	SCX_OPSS_QUEUED,	/* owned by the BPF scheduler */
1000 	SCX_OPSS_DISPATCHING,	/* in transit back to the SCX core */
1001 
1002 	/*
1003 	 * QSEQ brands each QUEUED instance so that, when dispatch races
1004 	 * dequeue/requeue, the dispatcher can tell whether it still has a claim
1005 	 * on the task being dispatched.
1006 	 *
1007 	 * As some 32bit archs can't do 64bit store_release/load_acquire,
1008 	 * p->scx.ops_state is atomic_long_t which leaves 30 bits for QSEQ on
1009 	 * 32bit machines. The dispatch race window QSEQ protects is very narrow
1010 	 * and runs with IRQ disabled. 30 bits should be sufficient.
1011 	 */
1012 	SCX_OPSS_QSEQ_SHIFT	= 2,
1013 };
1014 
1015 /* Use macros to ensure that the type is unsigned long for the masks */
1016 #define SCX_OPSS_STATE_MASK	((1LU << SCX_OPSS_QSEQ_SHIFT) - 1)
1017 #define SCX_OPSS_QSEQ_MASK	(~SCX_OPSS_STATE_MASK)
1018 
1019 /*
1020  * NOTE: sched_ext is in the process of growing multiple scheduler support and
1021  * scx_root usage is in a transitional state. Naked dereferences are safe if the
1022  * caller is one of the tasks attached to SCX and explicit RCU dereference is
1023  * necessary otherwise. Naked scx_root dereferences trigger sparse warnings but
1024  * are used as temporary markers to indicate that the dereferences need to be
1025  * updated to point to the associated scheduler instances rather than scx_root.
1026  */
1027 static struct scx_sched __rcu *scx_root;
1028 
1029 /*
1030  * During exit, a task may schedule after losing its PIDs. When disabling the
1031  * BPF scheduler, we need to be able to iterate tasks in every state to
1032  * guarantee system safety. Maintain a dedicated task list which contains every
1033  * task between its fork and eventual free.
1034  */
1035 static DEFINE_SPINLOCK(scx_tasks_lock);
1036 static LIST_HEAD(scx_tasks);
1037 
1038 /* ops enable/disable */
1039 static DEFINE_MUTEX(scx_enable_mutex);
1040 DEFINE_STATIC_KEY_FALSE(__scx_enabled);
1041 DEFINE_STATIC_PERCPU_RWSEM(scx_fork_rwsem);
1042 static atomic_t scx_enable_state_var = ATOMIC_INIT(SCX_DISABLED);
1043 static unsigned long scx_in_softlockup;
1044 static atomic_t scx_breather_depth = ATOMIC_INIT(0);
1045 static int scx_bypass_depth;
1046 static bool scx_init_task_enabled;
1047 static bool scx_switching_all;
1048 DEFINE_STATIC_KEY_FALSE(__scx_switched_all);
1049 
1050 static atomic_long_t scx_nr_rejected = ATOMIC_LONG_INIT(0);
1051 static atomic_long_t scx_hotplug_seq = ATOMIC_LONG_INIT(0);
1052 
1053 /*
1054  * A monotically increasing sequence number that is incremented every time a
1055  * scheduler is enabled. This can be used by to check if any custom sched_ext
1056  * scheduler has ever been used in the system.
1057  */
1058 static atomic_long_t scx_enable_seq = ATOMIC_LONG_INIT(0);
1059 
1060 /*
1061  * The maximum amount of time in jiffies that a task may be runnable without
1062  * being scheduled on a CPU. If this timeout is exceeded, it will trigger
1063  * scx_error().
1064  */
1065 static unsigned long scx_watchdog_timeout;
1066 
1067 /*
1068  * The last time the delayed work was run. This delayed work relies on
1069  * ksoftirqd being able to run to service timer interrupts, so it's possible
1070  * that this work itself could get wedged. To account for this, we check that
1071  * it's not stalled in the timer tick, and trigger an error if it is.
1072  */
1073 static unsigned long scx_watchdog_timestamp = INITIAL_JIFFIES;
1074 
1075 static struct delayed_work scx_watchdog_work;
1076 
1077 /* for %SCX_KICK_WAIT */
1078 static unsigned long __percpu *scx_kick_cpus_pnt_seqs;
1079 
1080 /*
1081  * Direct dispatch marker.
1082  *
1083  * Non-NULL values are used for direct dispatch from enqueue path. A valid
1084  * pointer points to the task currently being enqueued. An ERR_PTR value is used
1085  * to indicate that direct dispatch has already happened.
1086  */
1087 static DEFINE_PER_CPU(struct task_struct *, direct_dispatch_task);
1088 
1089 static const struct rhashtable_params dsq_hash_params = {
1090 	.key_len		= sizeof_field(struct scx_dispatch_q, id),
1091 	.key_offset		= offsetof(struct scx_dispatch_q, id),
1092 	.head_offset		= offsetof(struct scx_dispatch_q, hash_node),
1093 };
1094 
1095 static LLIST_HEAD(dsqs_to_free);
1096 
1097 /* dispatch buf */
1098 struct scx_dsp_buf_ent {
1099 	struct task_struct	*task;
1100 	unsigned long		qseq;
1101 	u64			dsq_id;
1102 	u64			enq_flags;
1103 };
1104 
1105 static u32 scx_dsp_max_batch;
1106 
1107 struct scx_dsp_ctx {
1108 	struct rq		*rq;
1109 	u32			cursor;
1110 	u32			nr_tasks;
1111 	struct scx_dsp_buf_ent	buf[];
1112 };
1113 
1114 static struct scx_dsp_ctx __percpu *scx_dsp_ctx;
1115 
1116 /* string formatting from BPF */
1117 struct scx_bstr_buf {
1118 	u64			data[MAX_BPRINTF_VARARGS];
1119 	char			line[SCX_EXIT_MSG_LEN];
1120 };
1121 
1122 static DEFINE_RAW_SPINLOCK(scx_exit_bstr_buf_lock);
1123 static struct scx_bstr_buf scx_exit_bstr_buf;
1124 
1125 /* ops debug dump */
1126 struct scx_dump_data {
1127 	s32			cpu;
1128 	bool			first;
1129 	s32			cursor;
1130 	struct seq_buf		*s;
1131 	const char		*prefix;
1132 	struct scx_bstr_buf	buf;
1133 };
1134 
1135 static struct scx_dump_data scx_dump_data = {
1136 	.cpu			= -1,
1137 };
1138 
1139 /* /sys/kernel/sched_ext interface */
1140 static struct kset *scx_kset;
1141 
1142 #define CREATE_TRACE_POINTS
1143 #include <trace/events/sched_ext.h>
1144 
1145 static void process_ddsp_deferred_locals(struct rq *rq);
1146 static void scx_bpf_kick_cpu(s32 cpu, u64 flags);
1147 static void scx_vexit(struct scx_sched *sch, enum scx_exit_kind kind,
1148 		      s64 exit_code, const char *fmt, va_list args);
1149 
1150 static __printf(4, 5) void scx_exit(struct scx_sched *sch,
1151 				    enum scx_exit_kind kind, s64 exit_code,
1152 				    const char *fmt, ...)
1153 {
1154 	va_list args;
1155 
1156 	va_start(args, fmt);
1157 	scx_vexit(sch, kind, exit_code, fmt, args);
1158 	va_end(args);
1159 }
1160 
1161 static __printf(3, 4) void scx_kf_exit(enum scx_exit_kind kind, s64 exit_code,
1162 				       const char *fmt, ...)
1163 {
1164 	struct scx_sched *sch;
1165 	va_list args;
1166 
1167 	rcu_read_lock();
1168 	sch = rcu_dereference(scx_root);
1169 	if (sch) {
1170 		va_start(args, fmt);
1171 		scx_vexit(sch, kind, exit_code, fmt, args);
1172 		va_end(args);
1173 	}
1174 	rcu_read_unlock();
1175 }
1176 
1177 #define scx_error(sch, fmt, args...)	scx_exit((sch), SCX_EXIT_ERROR, 0, fmt, ##args)
1178 #define scx_kf_error(fmt, args...)	scx_kf_exit(SCX_EXIT_ERROR, 0, fmt, ##args)
1179 
1180 #define SCX_HAS_OP(sch, op)	test_bit(SCX_OP_IDX(op), (sch)->has_op)
1181 
1182 static long jiffies_delta_msecs(unsigned long at, unsigned long now)
1183 {
1184 	if (time_after(at, now))
1185 		return jiffies_to_msecs(at - now);
1186 	else
1187 		return -(long)jiffies_to_msecs(now - at);
1188 }
1189 
1190 /* if the highest set bit is N, return a mask with bits [N+1, 31] set */
1191 static u32 higher_bits(u32 flags)
1192 {
1193 	return ~((1 << fls(flags)) - 1);
1194 }
1195 
1196 /* return the mask with only the highest bit set */
1197 static u32 highest_bit(u32 flags)
1198 {
1199 	int bit = fls(flags);
1200 	return ((u64)1 << bit) >> 1;
1201 }
1202 
1203 static bool u32_before(u32 a, u32 b)
1204 {
1205 	return (s32)(a - b) < 0;
1206 }
1207 
1208 static struct scx_dispatch_q *find_global_dsq(struct task_struct *p)
1209 {
1210 	struct scx_sched *sch = scx_root;
1211 
1212 	return sch->global_dsqs[cpu_to_node(task_cpu(p))];
1213 }
1214 
1215 static struct scx_dispatch_q *find_user_dsq(struct scx_sched *sch, u64 dsq_id)
1216 {
1217 	return rhashtable_lookup_fast(&sch->dsq_hash, &dsq_id, dsq_hash_params);
1218 }
1219 
1220 /*
1221  * scx_kf_mask enforcement. Some kfuncs can only be called from specific SCX
1222  * ops. When invoking SCX ops, SCX_CALL_OP[_RET]() should be used to indicate
1223  * the allowed kfuncs and those kfuncs should use scx_kf_allowed() to check
1224  * whether it's running from an allowed context.
1225  *
1226  * @mask is constant, always inline to cull the mask calculations.
1227  */
1228 static __always_inline void scx_kf_allow(u32 mask)
1229 {
1230 	/* nesting is allowed only in increasing scx_kf_mask order */
1231 	WARN_ONCE((mask | higher_bits(mask)) & current->scx.kf_mask,
1232 		  "invalid nesting current->scx.kf_mask=0x%x mask=0x%x\n",
1233 		  current->scx.kf_mask, mask);
1234 	current->scx.kf_mask |= mask;
1235 	barrier();
1236 }
1237 
1238 static void scx_kf_disallow(u32 mask)
1239 {
1240 	barrier();
1241 	current->scx.kf_mask &= ~mask;
1242 }
1243 
1244 /*
1245  * Track the rq currently locked.
1246  *
1247  * This allows kfuncs to safely operate on rq from any scx ops callback,
1248  * knowing which rq is already locked.
1249  */
1250 static DEFINE_PER_CPU(struct rq *, locked_rq);
1251 
1252 static inline void update_locked_rq(struct rq *rq)
1253 {
1254 	/*
1255 	 * Check whether @rq is actually locked. This can help expose bugs
1256 	 * or incorrect assumptions about the context in which a kfunc or
1257 	 * callback is executed.
1258 	 */
1259 	if (rq)
1260 		lockdep_assert_rq_held(rq);
1261 	__this_cpu_write(locked_rq, rq);
1262 }
1263 
1264 /*
1265  * Return the rq currently locked from an scx callback, or NULL if no rq is
1266  * locked.
1267  */
1268 static inline struct rq *scx_locked_rq(void)
1269 {
1270 	return __this_cpu_read(locked_rq);
1271 }
1272 
1273 #define SCX_CALL_OP(sch, mask, op, rq, args...)					\
1274 do {										\
1275 	update_locked_rq(rq);							\
1276 	if (mask) {								\
1277 		scx_kf_allow(mask);						\
1278 		(sch)->ops.op(args);						\
1279 		scx_kf_disallow(mask);						\
1280 	} else {								\
1281 		(sch)->ops.op(args);						\
1282 	}									\
1283 	update_locked_rq(NULL);							\
1284 } while (0)
1285 
1286 #define SCX_CALL_OP_RET(sch, mask, op, rq, args...)				\
1287 ({										\
1288 	__typeof__((sch)->ops.op(args)) __ret;					\
1289 										\
1290 	update_locked_rq(rq);							\
1291 	if (mask) {								\
1292 		scx_kf_allow(mask);						\
1293 		__ret = (sch)->ops.op(args);					\
1294 		scx_kf_disallow(mask);						\
1295 	} else {								\
1296 		__ret = (sch)->ops.op(args);					\
1297 	}									\
1298 	update_locked_rq(NULL);							\
1299 	__ret;									\
1300 })
1301 
1302 /*
1303  * Some kfuncs are allowed only on the tasks that are subjects of the
1304  * in-progress scx_ops operation for, e.g., locking guarantees. To enforce such
1305  * restrictions, the following SCX_CALL_OP_*() variants should be used when
1306  * invoking scx_ops operations that take task arguments. These can only be used
1307  * for non-nesting operations due to the way the tasks are tracked.
1308  *
1309  * kfuncs which can only operate on such tasks can in turn use
1310  * scx_kf_allowed_on_arg_tasks() to test whether the invocation is allowed on
1311  * the specific task.
1312  */
1313 #define SCX_CALL_OP_TASK(sch, mask, op, rq, task, args...)			\
1314 do {										\
1315 	BUILD_BUG_ON((mask) & ~__SCX_KF_TERMINAL);				\
1316 	current->scx.kf_tasks[0] = task;					\
1317 	SCX_CALL_OP((sch), mask, op, rq, task, ##args);				\
1318 	current->scx.kf_tasks[0] = NULL;					\
1319 } while (0)
1320 
1321 #define SCX_CALL_OP_TASK_RET(sch, mask, op, rq, task, args...)			\
1322 ({										\
1323 	__typeof__((sch)->ops.op(task, ##args)) __ret;				\
1324 	BUILD_BUG_ON((mask) & ~__SCX_KF_TERMINAL);				\
1325 	current->scx.kf_tasks[0] = task;					\
1326 	__ret = SCX_CALL_OP_RET((sch), mask, op, rq, task, ##args);		\
1327 	current->scx.kf_tasks[0] = NULL;					\
1328 	__ret;									\
1329 })
1330 
1331 #define SCX_CALL_OP_2TASKS_RET(sch, mask, op, rq, task0, task1, args...)	\
1332 ({										\
1333 	__typeof__((sch)->ops.op(task0, task1, ##args)) __ret;			\
1334 	BUILD_BUG_ON((mask) & ~__SCX_KF_TERMINAL);				\
1335 	current->scx.kf_tasks[0] = task0;					\
1336 	current->scx.kf_tasks[1] = task1;					\
1337 	__ret = SCX_CALL_OP_RET((sch), mask, op, rq, task0, task1, ##args);	\
1338 	current->scx.kf_tasks[0] = NULL;					\
1339 	current->scx.kf_tasks[1] = NULL;					\
1340 	__ret;									\
1341 })
1342 
1343 /* @mask is constant, always inline to cull unnecessary branches */
1344 static __always_inline bool scx_kf_allowed(u32 mask)
1345 {
1346 	if (unlikely(!(current->scx.kf_mask & mask))) {
1347 		scx_kf_error("kfunc with mask 0x%x called from an operation only allowing 0x%x",
1348 			     mask, current->scx.kf_mask);
1349 		return false;
1350 	}
1351 
1352 	/*
1353 	 * Enforce nesting boundaries. e.g. A kfunc which can be called from
1354 	 * DISPATCH must not be called if we're running DEQUEUE which is nested
1355 	 * inside ops.dispatch(). We don't need to check boundaries for any
1356 	 * blocking kfuncs as the verifier ensures they're only called from
1357 	 * sleepable progs.
1358 	 */
1359 	if (unlikely(highest_bit(mask) == SCX_KF_CPU_RELEASE &&
1360 		     (current->scx.kf_mask & higher_bits(SCX_KF_CPU_RELEASE)))) {
1361 		scx_kf_error("cpu_release kfunc called from a nested operation");
1362 		return false;
1363 	}
1364 
1365 	if (unlikely(highest_bit(mask) == SCX_KF_DISPATCH &&
1366 		     (current->scx.kf_mask & higher_bits(SCX_KF_DISPATCH)))) {
1367 		scx_kf_error("dispatch kfunc called from a nested operation");
1368 		return false;
1369 	}
1370 
1371 	return true;
1372 }
1373 
1374 /* see SCX_CALL_OP_TASK() */
1375 static __always_inline bool scx_kf_allowed_on_arg_tasks(u32 mask,
1376 							struct task_struct *p)
1377 {
1378 	if (!scx_kf_allowed(mask))
1379 		return false;
1380 
1381 	if (unlikely((p != current->scx.kf_tasks[0] &&
1382 		      p != current->scx.kf_tasks[1]))) {
1383 		scx_kf_error("called on a task not being operated on");
1384 		return false;
1385 	}
1386 
1387 	return true;
1388 }
1389 
1390 /**
1391  * nldsq_next_task - Iterate to the next task in a non-local DSQ
1392  * @dsq: user dsq being iterated
1393  * @cur: current position, %NULL to start iteration
1394  * @rev: walk backwards
1395  *
1396  * Returns %NULL when iteration is finished.
1397  */
1398 static struct task_struct *nldsq_next_task(struct scx_dispatch_q *dsq,
1399 					   struct task_struct *cur, bool rev)
1400 {
1401 	struct list_head *list_node;
1402 	struct scx_dsq_list_node *dsq_lnode;
1403 
1404 	lockdep_assert_held(&dsq->lock);
1405 
1406 	if (cur)
1407 		list_node = &cur->scx.dsq_list.node;
1408 	else
1409 		list_node = &dsq->list;
1410 
1411 	/* find the next task, need to skip BPF iteration cursors */
1412 	do {
1413 		if (rev)
1414 			list_node = list_node->prev;
1415 		else
1416 			list_node = list_node->next;
1417 
1418 		if (list_node == &dsq->list)
1419 			return NULL;
1420 
1421 		dsq_lnode = container_of(list_node, struct scx_dsq_list_node,
1422 					 node);
1423 	} while (dsq_lnode->flags & SCX_DSQ_LNODE_ITER_CURSOR);
1424 
1425 	return container_of(dsq_lnode, struct task_struct, scx.dsq_list);
1426 }
1427 
1428 #define nldsq_for_each_task(p, dsq)						\
1429 	for ((p) = nldsq_next_task((dsq), NULL, false); (p);			\
1430 	     (p) = nldsq_next_task((dsq), (p), false))
1431 
1432 
1433 /*
1434  * BPF DSQ iterator. Tasks in a non-local DSQ can be iterated in [reverse]
1435  * dispatch order. BPF-visible iterator is opaque and larger to allow future
1436  * changes without breaking backward compatibility. Can be used with
1437  * bpf_for_each(). See bpf_iter_scx_dsq_*().
1438  */
1439 enum scx_dsq_iter_flags {
1440 	/* iterate in the reverse dispatch order */
1441 	SCX_DSQ_ITER_REV		= 1U << 16,
1442 
1443 	__SCX_DSQ_ITER_HAS_SLICE	= 1U << 30,
1444 	__SCX_DSQ_ITER_HAS_VTIME	= 1U << 31,
1445 
1446 	__SCX_DSQ_ITER_USER_FLAGS	= SCX_DSQ_ITER_REV,
1447 	__SCX_DSQ_ITER_ALL_FLAGS	= __SCX_DSQ_ITER_USER_FLAGS |
1448 					  __SCX_DSQ_ITER_HAS_SLICE |
1449 					  __SCX_DSQ_ITER_HAS_VTIME,
1450 };
1451 
1452 struct bpf_iter_scx_dsq_kern {
1453 	struct scx_dsq_list_node	cursor;
1454 	struct scx_dispatch_q		*dsq;
1455 	u64				slice;
1456 	u64				vtime;
1457 } __attribute__((aligned(8)));
1458 
1459 struct bpf_iter_scx_dsq {
1460 	u64				__opaque[6];
1461 } __attribute__((aligned(8)));
1462 
1463 
1464 /*
1465  * SCX task iterator.
1466  */
1467 struct scx_task_iter {
1468 	struct sched_ext_entity		cursor;
1469 	struct task_struct		*locked;
1470 	struct rq			*rq;
1471 	struct rq_flags			rf;
1472 	u32				cnt;
1473 };
1474 
1475 /**
1476  * scx_task_iter_start - Lock scx_tasks_lock and start a task iteration
1477  * @iter: iterator to init
1478  *
1479  * Initialize @iter and return with scx_tasks_lock held. Once initialized, @iter
1480  * must eventually be stopped with scx_task_iter_stop().
1481  *
1482  * scx_tasks_lock and the rq lock may be released using scx_task_iter_unlock()
1483  * between this and the first next() call or between any two next() calls. If
1484  * the locks are released between two next() calls, the caller is responsible
1485  * for ensuring that the task being iterated remains accessible either through
1486  * RCU read lock or obtaining a reference count.
1487  *
1488  * All tasks which existed when the iteration started are guaranteed to be
1489  * visited as long as they still exist.
1490  */
1491 static void scx_task_iter_start(struct scx_task_iter *iter)
1492 {
1493 	BUILD_BUG_ON(__SCX_DSQ_ITER_ALL_FLAGS &
1494 		     ((1U << __SCX_DSQ_LNODE_PRIV_SHIFT) - 1));
1495 
1496 	spin_lock_irq(&scx_tasks_lock);
1497 
1498 	iter->cursor = (struct sched_ext_entity){ .flags = SCX_TASK_CURSOR };
1499 	list_add(&iter->cursor.tasks_node, &scx_tasks);
1500 	iter->locked = NULL;
1501 	iter->cnt = 0;
1502 }
1503 
1504 static void __scx_task_iter_rq_unlock(struct scx_task_iter *iter)
1505 {
1506 	if (iter->locked) {
1507 		task_rq_unlock(iter->rq, iter->locked, &iter->rf);
1508 		iter->locked = NULL;
1509 	}
1510 }
1511 
1512 /**
1513  * scx_task_iter_unlock - Unlock rq and scx_tasks_lock held by a task iterator
1514  * @iter: iterator to unlock
1515  *
1516  * If @iter is in the middle of a locked iteration, it may be locking the rq of
1517  * the task currently being visited in addition to scx_tasks_lock. Unlock both.
1518  * This function can be safely called anytime during an iteration.
1519  */
1520 static void scx_task_iter_unlock(struct scx_task_iter *iter)
1521 {
1522 	__scx_task_iter_rq_unlock(iter);
1523 	spin_unlock_irq(&scx_tasks_lock);
1524 }
1525 
1526 /**
1527  * scx_task_iter_relock - Lock scx_tasks_lock released by scx_task_iter_unlock()
1528  * @iter: iterator to re-lock
1529  *
1530  * Re-lock scx_tasks_lock unlocked by scx_task_iter_unlock(). Note that it
1531  * doesn't re-lock the rq lock. Must be called before other iterator operations.
1532  */
1533 static void scx_task_iter_relock(struct scx_task_iter *iter)
1534 {
1535 	spin_lock_irq(&scx_tasks_lock);
1536 }
1537 
1538 /**
1539  * scx_task_iter_stop - Stop a task iteration and unlock scx_tasks_lock
1540  * @iter: iterator to exit
1541  *
1542  * Exit a previously initialized @iter. Must be called with scx_tasks_lock held
1543  * which is released on return. If the iterator holds a task's rq lock, that rq
1544  * lock is also released. See scx_task_iter_start() for details.
1545  */
1546 static void scx_task_iter_stop(struct scx_task_iter *iter)
1547 {
1548 	list_del_init(&iter->cursor.tasks_node);
1549 	scx_task_iter_unlock(iter);
1550 }
1551 
1552 /**
1553  * scx_task_iter_next - Next task
1554  * @iter: iterator to walk
1555  *
1556  * Visit the next task. See scx_task_iter_start() for details. Locks are dropped
1557  * and re-acquired every %SCX_TASK_ITER_BATCH iterations to avoid causing stalls
1558  * by holding scx_tasks_lock for too long.
1559  */
1560 static struct task_struct *scx_task_iter_next(struct scx_task_iter *iter)
1561 {
1562 	struct list_head *cursor = &iter->cursor.tasks_node;
1563 	struct sched_ext_entity *pos;
1564 
1565 	if (!(++iter->cnt % SCX_TASK_ITER_BATCH)) {
1566 		scx_task_iter_unlock(iter);
1567 		cond_resched();
1568 		scx_task_iter_relock(iter);
1569 	}
1570 
1571 	list_for_each_entry(pos, cursor, tasks_node) {
1572 		if (&pos->tasks_node == &scx_tasks)
1573 			return NULL;
1574 		if (!(pos->flags & SCX_TASK_CURSOR)) {
1575 			list_move(cursor, &pos->tasks_node);
1576 			return container_of(pos, struct task_struct, scx);
1577 		}
1578 	}
1579 
1580 	/* can't happen, should always terminate at scx_tasks above */
1581 	BUG();
1582 }
1583 
1584 /**
1585  * scx_task_iter_next_locked - Next non-idle task with its rq locked
1586  * @iter: iterator to walk
1587  *
1588  * Visit the non-idle task with its rq lock held. Allows callers to specify
1589  * whether they would like to filter out dead tasks. See scx_task_iter_start()
1590  * for details.
1591  */
1592 static struct task_struct *scx_task_iter_next_locked(struct scx_task_iter *iter)
1593 {
1594 	struct task_struct *p;
1595 
1596 	__scx_task_iter_rq_unlock(iter);
1597 
1598 	while ((p = scx_task_iter_next(iter))) {
1599 		/*
1600 		 * scx_task_iter is used to prepare and move tasks into SCX
1601 		 * while loading the BPF scheduler and vice-versa while
1602 		 * unloading. The init_tasks ("swappers") should be excluded
1603 		 * from the iteration because:
1604 		 *
1605 		 * - It's unsafe to use __setschduler_prio() on an init_task to
1606 		 *   determine the sched_class to use as it won't preserve its
1607 		 *   idle_sched_class.
1608 		 *
1609 		 * - ops.init/exit_task() can easily be confused if called with
1610 		 *   init_tasks as they, e.g., share PID 0.
1611 		 *
1612 		 * As init_tasks are never scheduled through SCX, they can be
1613 		 * skipped safely. Note that is_idle_task() which tests %PF_IDLE
1614 		 * doesn't work here:
1615 		 *
1616 		 * - %PF_IDLE may not be set for an init_task whose CPU hasn't
1617 		 *   yet been onlined.
1618 		 *
1619 		 * - %PF_IDLE can be set on tasks that are not init_tasks. See
1620 		 *   play_idle_precise() used by CONFIG_IDLE_INJECT.
1621 		 *
1622 		 * Test for idle_sched_class as only init_tasks are on it.
1623 		 */
1624 		if (p->sched_class != &idle_sched_class)
1625 			break;
1626 	}
1627 	if (!p)
1628 		return NULL;
1629 
1630 	iter->rq = task_rq_lock(p, &iter->rf);
1631 	iter->locked = p;
1632 
1633 	return p;
1634 }
1635 
1636 /**
1637  * scx_add_event - Increase an event counter for 'name' by 'cnt'
1638  * @sch: scx_sched to account events for
1639  * @name: an event name defined in struct scx_event_stats
1640  * @cnt: the number of the event occured
1641  *
1642  * This can be used when preemption is not disabled.
1643  */
1644 #define scx_add_event(sch, name, cnt) do {					\
1645 	this_cpu_add((sch)->event_stats_cpu->name, (cnt));			\
1646 	trace_sched_ext_event(#name, (cnt));					\
1647 } while(0)
1648 
1649 /**
1650  * __scx_add_event - Increase an event counter for 'name' by 'cnt'
1651  * @sch: scx_sched to account events for
1652  * @name: an event name defined in struct scx_event_stats
1653  * @cnt: the number of the event occured
1654  *
1655  * This should be used only when preemption is disabled.
1656  */
1657 #define __scx_add_event(sch, name, cnt) do {					\
1658 	__this_cpu_add((sch)->event_stats_cpu->name, (cnt));			\
1659 	trace_sched_ext_event(#name, cnt);					\
1660 } while(0)
1661 
1662 /**
1663  * scx_agg_event - Aggregate an event counter 'kind' from 'src_e' to 'dst_e'
1664  * @dst_e: destination event stats
1665  * @src_e: source event stats
1666  * @kind: a kind of event to be aggregated
1667  */
1668 #define scx_agg_event(dst_e, src_e, kind) do {					\
1669 	(dst_e)->kind += READ_ONCE((src_e)->kind);				\
1670 } while(0)
1671 
1672 /**
1673  * scx_dump_event - Dump an event 'kind' in 'events' to 's'
1674  * @s: output seq_buf
1675  * @events: event stats
1676  * @kind: a kind of event to dump
1677  */
1678 #define scx_dump_event(s, events, kind) do {					\
1679 	dump_line(&(s), "%40s: %16lld", #kind, (events)->kind);			\
1680 } while (0)
1681 
1682 
1683 static void scx_read_events(struct scx_sched *sch,
1684 			    struct scx_event_stats *events);
1685 
1686 static enum scx_enable_state scx_enable_state(void)
1687 {
1688 	return atomic_read(&scx_enable_state_var);
1689 }
1690 
1691 static enum scx_enable_state scx_set_enable_state(enum scx_enable_state to)
1692 {
1693 	return atomic_xchg(&scx_enable_state_var, to);
1694 }
1695 
1696 static bool scx_tryset_enable_state(enum scx_enable_state to,
1697 				    enum scx_enable_state from)
1698 {
1699 	int from_v = from;
1700 
1701 	return atomic_try_cmpxchg(&scx_enable_state_var, &from_v, to);
1702 }
1703 
1704 static bool scx_rq_bypassing(struct rq *rq)
1705 {
1706 	return unlikely(rq->scx.flags & SCX_RQ_BYPASSING);
1707 }
1708 
1709 /**
1710  * wait_ops_state - Busy-wait the specified ops state to end
1711  * @p: target task
1712  * @opss: state to wait the end of
1713  *
1714  * Busy-wait for @p to transition out of @opss. This can only be used when the
1715  * state part of @opss is %SCX_QUEUEING or %SCX_DISPATCHING. This function also
1716  * has load_acquire semantics to ensure that the caller can see the updates made
1717  * in the enqueueing and dispatching paths.
1718  */
1719 static void wait_ops_state(struct task_struct *p, unsigned long opss)
1720 {
1721 	do {
1722 		cpu_relax();
1723 	} while (atomic_long_read_acquire(&p->scx.ops_state) == opss);
1724 }
1725 
1726 static inline bool __cpu_valid(s32 cpu)
1727 {
1728 	return likely(cpu >= 0 && cpu < nr_cpu_ids && cpu_possible(cpu));
1729 }
1730 
1731 /**
1732  * ops_cpu_valid - Verify a cpu number, to be used on ops input args
1733  * @sch: scx_sched to abort on error
1734  * @cpu: cpu number which came from a BPF ops
1735  * @where: extra information reported on error
1736  *
1737  * @cpu is a cpu number which came from the BPF scheduler and can be any value.
1738  * Verify that it is in range and one of the possible cpus. If invalid, trigger
1739  * an ops error.
1740  */
1741 static bool ops_cpu_valid(struct scx_sched *sch, s32 cpu, const char *where)
1742 {
1743 	if (__cpu_valid(cpu)) {
1744 		return true;
1745 	} else {
1746 		scx_error(sch, "invalid CPU %d%s%s", cpu, where ? " " : "", where ?: "");
1747 		return false;
1748 	}
1749 }
1750 
1751 /**
1752  * kf_cpu_valid - Verify a CPU number, to be used on kfunc input args
1753  * @cpu: cpu number which came from a BPF ops
1754  * @where: extra information reported on error
1755  *
1756  * The same as ops_cpu_valid() but @sch is implicit.
1757  */
1758 static bool kf_cpu_valid(u32 cpu, const char *where)
1759 {
1760 	if (__cpu_valid(cpu)) {
1761 		return true;
1762 	} else {
1763 		scx_kf_error("invalid CPU %d%s%s", cpu, where ? " " : "", where ?: "");
1764 		return false;
1765 	}
1766 }
1767 
1768 /**
1769  * ops_sanitize_err - Sanitize a -errno value
1770  * @sch: scx_sched to error out on error
1771  * @ops_name: operation to blame on failure
1772  * @err: -errno value to sanitize
1773  *
1774  * Verify @err is a valid -errno. If not, trigger scx_error() and return
1775  * -%EPROTO. This is necessary because returning a rogue -errno up the chain can
1776  * cause misbehaviors. For an example, a large negative return from
1777  * ops.init_task() triggers an oops when passed up the call chain because the
1778  * value fails IS_ERR() test after being encoded with ERR_PTR() and then is
1779  * handled as a pointer.
1780  */
1781 static int ops_sanitize_err(struct scx_sched *sch, const char *ops_name, s32 err)
1782 {
1783 	if (err < 0 && err >= -MAX_ERRNO)
1784 		return err;
1785 
1786 	scx_error(sch, "ops.%s() returned an invalid errno %d", ops_name, err);
1787 	return -EPROTO;
1788 }
1789 
1790 static void run_deferred(struct rq *rq)
1791 {
1792 	process_ddsp_deferred_locals(rq);
1793 }
1794 
1795 #ifdef CONFIG_SMP
1796 static void deferred_bal_cb_workfn(struct rq *rq)
1797 {
1798 	run_deferred(rq);
1799 }
1800 #endif
1801 
1802 static void deferred_irq_workfn(struct irq_work *irq_work)
1803 {
1804 	struct rq *rq = container_of(irq_work, struct rq, scx.deferred_irq_work);
1805 
1806 	raw_spin_rq_lock(rq);
1807 	run_deferred(rq);
1808 	raw_spin_rq_unlock(rq);
1809 }
1810 
1811 /**
1812  * schedule_deferred - Schedule execution of deferred actions on an rq
1813  * @rq: target rq
1814  *
1815  * Schedule execution of deferred actions on @rq. Must be called with @rq
1816  * locked. Deferred actions are executed with @rq locked but unpinned, and thus
1817  * can unlock @rq to e.g. migrate tasks to other rqs.
1818  */
1819 static void schedule_deferred(struct rq *rq)
1820 {
1821 	lockdep_assert_rq_held(rq);
1822 
1823 #ifdef CONFIG_SMP
1824 	/*
1825 	 * If in the middle of waking up a task, task_woken_scx() will be called
1826 	 * afterwards which will then run the deferred actions, no need to
1827 	 * schedule anything.
1828 	 */
1829 	if (rq->scx.flags & SCX_RQ_IN_WAKEUP)
1830 		return;
1831 
1832 	/*
1833 	 * If in balance, the balance callbacks will be called before rq lock is
1834 	 * released. Schedule one.
1835 	 */
1836 	if (rq->scx.flags & SCX_RQ_IN_BALANCE) {
1837 		queue_balance_callback(rq, &rq->scx.deferred_bal_cb,
1838 				       deferred_bal_cb_workfn);
1839 		return;
1840 	}
1841 #endif
1842 	/*
1843 	 * No scheduler hooks available. Queue an irq work. They are executed on
1844 	 * IRQ re-enable which may take a bit longer than the scheduler hooks.
1845 	 * The above WAKEUP and BALANCE paths should cover most of the cases and
1846 	 * the time to IRQ re-enable shouldn't be long.
1847 	 */
1848 	irq_work_queue(&rq->scx.deferred_irq_work);
1849 }
1850 
1851 /**
1852  * touch_core_sched - Update timestamp used for core-sched task ordering
1853  * @rq: rq to read clock from, must be locked
1854  * @p: task to update the timestamp for
1855  *
1856  * Update @p->scx.core_sched_at timestamp. This is used by scx_prio_less() to
1857  * implement global or local-DSQ FIFO ordering for core-sched. Should be called
1858  * when a task becomes runnable and its turn on the CPU ends (e.g. slice
1859  * exhaustion).
1860  */
1861 static void touch_core_sched(struct rq *rq, struct task_struct *p)
1862 {
1863 	lockdep_assert_rq_held(rq);
1864 
1865 #ifdef CONFIG_SCHED_CORE
1866 	/*
1867 	 * It's okay to update the timestamp spuriously. Use
1868 	 * sched_core_disabled() which is cheaper than enabled().
1869 	 *
1870 	 * As this is used to determine ordering between tasks of sibling CPUs,
1871 	 * it may be better to use per-core dispatch sequence instead.
1872 	 */
1873 	if (!sched_core_disabled())
1874 		p->scx.core_sched_at = sched_clock_cpu(cpu_of(rq));
1875 #endif
1876 }
1877 
1878 /**
1879  * touch_core_sched_dispatch - Update core-sched timestamp on dispatch
1880  * @rq: rq to read clock from, must be locked
1881  * @p: task being dispatched
1882  *
1883  * If the BPF scheduler implements custom core-sched ordering via
1884  * ops.core_sched_before(), @p->scx.core_sched_at is used to implement FIFO
1885  * ordering within each local DSQ. This function is called from dispatch paths
1886  * and updates @p->scx.core_sched_at if custom core-sched ordering is in effect.
1887  */
1888 static void touch_core_sched_dispatch(struct rq *rq, struct task_struct *p)
1889 {
1890 	lockdep_assert_rq_held(rq);
1891 
1892 #ifdef CONFIG_SCHED_CORE
1893 	if (unlikely(SCX_HAS_OP(scx_root, core_sched_before)))
1894 		touch_core_sched(rq, p);
1895 #endif
1896 }
1897 
1898 static void update_curr_scx(struct rq *rq)
1899 {
1900 	struct task_struct *curr = rq->curr;
1901 	s64 delta_exec;
1902 
1903 	delta_exec = update_curr_common(rq);
1904 	if (unlikely(delta_exec <= 0))
1905 		return;
1906 
1907 	if (curr->scx.slice != SCX_SLICE_INF) {
1908 		curr->scx.slice -= min_t(u64, curr->scx.slice, delta_exec);
1909 		if (!curr->scx.slice)
1910 			touch_core_sched(rq, curr);
1911 	}
1912 }
1913 
1914 static bool scx_dsq_priq_less(struct rb_node *node_a,
1915 			      const struct rb_node *node_b)
1916 {
1917 	const struct task_struct *a =
1918 		container_of(node_a, struct task_struct, scx.dsq_priq);
1919 	const struct task_struct *b =
1920 		container_of(node_b, struct task_struct, scx.dsq_priq);
1921 
1922 	return time_before64(a->scx.dsq_vtime, b->scx.dsq_vtime);
1923 }
1924 
1925 static void dsq_mod_nr(struct scx_dispatch_q *dsq, s32 delta)
1926 {
1927 	/* scx_bpf_dsq_nr_queued() reads ->nr without locking, use WRITE_ONCE() */
1928 	WRITE_ONCE(dsq->nr, dsq->nr + delta);
1929 }
1930 
1931 static void refill_task_slice_dfl(struct task_struct *p)
1932 {
1933 	p->scx.slice = SCX_SLICE_DFL;
1934 	__scx_add_event(scx_root, SCX_EV_REFILL_SLICE_DFL, 1);
1935 }
1936 
1937 static void dispatch_enqueue(struct scx_sched *sch, struct scx_dispatch_q *dsq,
1938 			     struct task_struct *p, u64 enq_flags)
1939 {
1940 	bool is_local = dsq->id == SCX_DSQ_LOCAL;
1941 
1942 	WARN_ON_ONCE(p->scx.dsq || !list_empty(&p->scx.dsq_list.node));
1943 	WARN_ON_ONCE((p->scx.dsq_flags & SCX_TASK_DSQ_ON_PRIQ) ||
1944 		     !RB_EMPTY_NODE(&p->scx.dsq_priq));
1945 
1946 	if (!is_local) {
1947 		raw_spin_lock(&dsq->lock);
1948 		if (unlikely(dsq->id == SCX_DSQ_INVALID)) {
1949 			scx_error(sch, "attempting to dispatch to a destroyed dsq");
1950 			/* fall back to the global dsq */
1951 			raw_spin_unlock(&dsq->lock);
1952 			dsq = find_global_dsq(p);
1953 			raw_spin_lock(&dsq->lock);
1954 		}
1955 	}
1956 
1957 	if (unlikely((dsq->id & SCX_DSQ_FLAG_BUILTIN) &&
1958 		     (enq_flags & SCX_ENQ_DSQ_PRIQ))) {
1959 		/*
1960 		 * SCX_DSQ_LOCAL and SCX_DSQ_GLOBAL DSQs always consume from
1961 		 * their FIFO queues. To avoid confusion and accidentally
1962 		 * starving vtime-dispatched tasks by FIFO-dispatched tasks, we
1963 		 * disallow any internal DSQ from doing vtime ordering of
1964 		 * tasks.
1965 		 */
1966 		scx_error(sch, "cannot use vtime ordering for built-in DSQs");
1967 		enq_flags &= ~SCX_ENQ_DSQ_PRIQ;
1968 	}
1969 
1970 	if (enq_flags & SCX_ENQ_DSQ_PRIQ) {
1971 		struct rb_node *rbp;
1972 
1973 		/*
1974 		 * A PRIQ DSQ shouldn't be using FIFO enqueueing. As tasks are
1975 		 * linked to both the rbtree and list on PRIQs, this can only be
1976 		 * tested easily when adding the first task.
1977 		 */
1978 		if (unlikely(RB_EMPTY_ROOT(&dsq->priq) &&
1979 			     nldsq_next_task(dsq, NULL, false)))
1980 			scx_error(sch, "DSQ ID 0x%016llx already had FIFO-enqueued tasks",
1981 				  dsq->id);
1982 
1983 		p->scx.dsq_flags |= SCX_TASK_DSQ_ON_PRIQ;
1984 		rb_add(&p->scx.dsq_priq, &dsq->priq, scx_dsq_priq_less);
1985 
1986 		/*
1987 		 * Find the previous task and insert after it on the list so
1988 		 * that @dsq->list is vtime ordered.
1989 		 */
1990 		rbp = rb_prev(&p->scx.dsq_priq);
1991 		if (rbp) {
1992 			struct task_struct *prev =
1993 				container_of(rbp, struct task_struct,
1994 					     scx.dsq_priq);
1995 			list_add(&p->scx.dsq_list.node, &prev->scx.dsq_list.node);
1996 		} else {
1997 			list_add(&p->scx.dsq_list.node, &dsq->list);
1998 		}
1999 	} else {
2000 		/* a FIFO DSQ shouldn't be using PRIQ enqueuing */
2001 		if (unlikely(!RB_EMPTY_ROOT(&dsq->priq)))
2002 			scx_error(sch, "DSQ ID 0x%016llx already had PRIQ-enqueued tasks",
2003 				  dsq->id);
2004 
2005 		if (enq_flags & (SCX_ENQ_HEAD | SCX_ENQ_PREEMPT))
2006 			list_add(&p->scx.dsq_list.node, &dsq->list);
2007 		else
2008 			list_add_tail(&p->scx.dsq_list.node, &dsq->list);
2009 	}
2010 
2011 	/* seq records the order tasks are queued, used by BPF DSQ iterator */
2012 	dsq->seq++;
2013 	p->scx.dsq_seq = dsq->seq;
2014 
2015 	dsq_mod_nr(dsq, 1);
2016 	p->scx.dsq = dsq;
2017 
2018 	/*
2019 	 * scx.ddsp_dsq_id and scx.ddsp_enq_flags are only relevant on the
2020 	 * direct dispatch path, but we clear them here because the direct
2021 	 * dispatch verdict may be overridden on the enqueue path during e.g.
2022 	 * bypass.
2023 	 */
2024 	p->scx.ddsp_dsq_id = SCX_DSQ_INVALID;
2025 	p->scx.ddsp_enq_flags = 0;
2026 
2027 	/*
2028 	 * We're transitioning out of QUEUEING or DISPATCHING. store_release to
2029 	 * match waiters' load_acquire.
2030 	 */
2031 	if (enq_flags & SCX_ENQ_CLEAR_OPSS)
2032 		atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE);
2033 
2034 	if (is_local) {
2035 		struct rq *rq = container_of(dsq, struct rq, scx.local_dsq);
2036 		bool preempt = false;
2037 
2038 		if ((enq_flags & SCX_ENQ_PREEMPT) && p != rq->curr &&
2039 		    rq->curr->sched_class == &ext_sched_class) {
2040 			rq->curr->scx.slice = 0;
2041 			preempt = true;
2042 		}
2043 
2044 		if (preempt || sched_class_above(&ext_sched_class,
2045 						 rq->curr->sched_class))
2046 			resched_curr(rq);
2047 	} else {
2048 		raw_spin_unlock(&dsq->lock);
2049 	}
2050 }
2051 
2052 static void task_unlink_from_dsq(struct task_struct *p,
2053 				 struct scx_dispatch_q *dsq)
2054 {
2055 	WARN_ON_ONCE(list_empty(&p->scx.dsq_list.node));
2056 
2057 	if (p->scx.dsq_flags & SCX_TASK_DSQ_ON_PRIQ) {
2058 		rb_erase(&p->scx.dsq_priq, &dsq->priq);
2059 		RB_CLEAR_NODE(&p->scx.dsq_priq);
2060 		p->scx.dsq_flags &= ~SCX_TASK_DSQ_ON_PRIQ;
2061 	}
2062 
2063 	list_del_init(&p->scx.dsq_list.node);
2064 	dsq_mod_nr(dsq, -1);
2065 }
2066 
2067 static void dispatch_dequeue(struct rq *rq, struct task_struct *p)
2068 {
2069 	struct scx_dispatch_q *dsq = p->scx.dsq;
2070 	bool is_local = dsq == &rq->scx.local_dsq;
2071 
2072 	if (!dsq) {
2073 		/*
2074 		 * If !dsq && on-list, @p is on @rq's ddsp_deferred_locals.
2075 		 * Unlinking is all that's needed to cancel.
2076 		 */
2077 		if (unlikely(!list_empty(&p->scx.dsq_list.node)))
2078 			list_del_init(&p->scx.dsq_list.node);
2079 
2080 		/*
2081 		 * When dispatching directly from the BPF scheduler to a local
2082 		 * DSQ, the task isn't associated with any DSQ but
2083 		 * @p->scx.holding_cpu may be set under the protection of
2084 		 * %SCX_OPSS_DISPATCHING.
2085 		 */
2086 		if (p->scx.holding_cpu >= 0)
2087 			p->scx.holding_cpu = -1;
2088 
2089 		return;
2090 	}
2091 
2092 	if (!is_local)
2093 		raw_spin_lock(&dsq->lock);
2094 
2095 	/*
2096 	 * Now that we hold @dsq->lock, @p->holding_cpu and @p->scx.dsq_* can't
2097 	 * change underneath us.
2098 	*/
2099 	if (p->scx.holding_cpu < 0) {
2100 		/* @p must still be on @dsq, dequeue */
2101 		task_unlink_from_dsq(p, dsq);
2102 	} else {
2103 		/*
2104 		 * We're racing against dispatch_to_local_dsq() which already
2105 		 * removed @p from @dsq and set @p->scx.holding_cpu. Clear the
2106 		 * holding_cpu which tells dispatch_to_local_dsq() that it lost
2107 		 * the race.
2108 		 */
2109 		WARN_ON_ONCE(!list_empty(&p->scx.dsq_list.node));
2110 		p->scx.holding_cpu = -1;
2111 	}
2112 	p->scx.dsq = NULL;
2113 
2114 	if (!is_local)
2115 		raw_spin_unlock(&dsq->lock);
2116 }
2117 
2118 static struct scx_dispatch_q *find_dsq_for_dispatch(struct scx_sched *sch,
2119 						    struct rq *rq, u64 dsq_id,
2120 						    struct task_struct *p)
2121 {
2122 	struct scx_dispatch_q *dsq;
2123 
2124 	if (dsq_id == SCX_DSQ_LOCAL)
2125 		return &rq->scx.local_dsq;
2126 
2127 	if ((dsq_id & SCX_DSQ_LOCAL_ON) == SCX_DSQ_LOCAL_ON) {
2128 		s32 cpu = dsq_id & SCX_DSQ_LOCAL_CPU_MASK;
2129 
2130 		if (!ops_cpu_valid(sch, cpu, "in SCX_DSQ_LOCAL_ON dispatch verdict"))
2131 			return find_global_dsq(p);
2132 
2133 		return &cpu_rq(cpu)->scx.local_dsq;
2134 	}
2135 
2136 	if (dsq_id == SCX_DSQ_GLOBAL)
2137 		dsq = find_global_dsq(p);
2138 	else
2139 		dsq = find_user_dsq(sch, dsq_id);
2140 
2141 	if (unlikely(!dsq)) {
2142 		scx_error(sch, "non-existent DSQ 0x%llx for %s[%d]",
2143 			  dsq_id, p->comm, p->pid);
2144 		return find_global_dsq(p);
2145 	}
2146 
2147 	return dsq;
2148 }
2149 
2150 static void mark_direct_dispatch(struct task_struct *ddsp_task,
2151 				 struct task_struct *p, u64 dsq_id,
2152 				 u64 enq_flags)
2153 {
2154 	/*
2155 	 * Mark that dispatch already happened from ops.select_cpu() or
2156 	 * ops.enqueue() by spoiling direct_dispatch_task with a non-NULL value
2157 	 * which can never match a valid task pointer.
2158 	 */
2159 	__this_cpu_write(direct_dispatch_task, ERR_PTR(-ESRCH));
2160 
2161 	/* @p must match the task on the enqueue path */
2162 	if (unlikely(p != ddsp_task)) {
2163 		if (IS_ERR(ddsp_task))
2164 			scx_kf_error("%s[%d] already direct-dispatched",
2165 				  p->comm, p->pid);
2166 		else
2167 			scx_kf_error("scheduling for %s[%d] but trying to direct-dispatch %s[%d]",
2168 				  ddsp_task->comm, ddsp_task->pid,
2169 				  p->comm, p->pid);
2170 		return;
2171 	}
2172 
2173 	WARN_ON_ONCE(p->scx.ddsp_dsq_id != SCX_DSQ_INVALID);
2174 	WARN_ON_ONCE(p->scx.ddsp_enq_flags);
2175 
2176 	p->scx.ddsp_dsq_id = dsq_id;
2177 	p->scx.ddsp_enq_flags = enq_flags;
2178 }
2179 
2180 static void direct_dispatch(struct scx_sched *sch, struct task_struct *p,
2181 			    u64 enq_flags)
2182 {
2183 	struct rq *rq = task_rq(p);
2184 	struct scx_dispatch_q *dsq =
2185 		find_dsq_for_dispatch(sch, rq, p->scx.ddsp_dsq_id, p);
2186 
2187 	touch_core_sched_dispatch(rq, p);
2188 
2189 	p->scx.ddsp_enq_flags |= enq_flags;
2190 
2191 	/*
2192 	 * We are in the enqueue path with @rq locked and pinned, and thus can't
2193 	 * double lock a remote rq and enqueue to its local DSQ. For
2194 	 * DSQ_LOCAL_ON verdicts targeting the local DSQ of a remote CPU, defer
2195 	 * the enqueue so that it's executed when @rq can be unlocked.
2196 	 */
2197 	if (dsq->id == SCX_DSQ_LOCAL && dsq != &rq->scx.local_dsq) {
2198 		unsigned long opss;
2199 
2200 		opss = atomic_long_read(&p->scx.ops_state) & SCX_OPSS_STATE_MASK;
2201 
2202 		switch (opss & SCX_OPSS_STATE_MASK) {
2203 		case SCX_OPSS_NONE:
2204 			break;
2205 		case SCX_OPSS_QUEUEING:
2206 			/*
2207 			 * As @p was never passed to the BPF side, _release is
2208 			 * not strictly necessary. Still do it for consistency.
2209 			 */
2210 			atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE);
2211 			break;
2212 		default:
2213 			WARN_ONCE(true, "sched_ext: %s[%d] has invalid ops state 0x%lx in direct_dispatch()",
2214 				  p->comm, p->pid, opss);
2215 			atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE);
2216 			break;
2217 		}
2218 
2219 		WARN_ON_ONCE(p->scx.dsq || !list_empty(&p->scx.dsq_list.node));
2220 		list_add_tail(&p->scx.dsq_list.node,
2221 			      &rq->scx.ddsp_deferred_locals);
2222 		schedule_deferred(rq);
2223 		return;
2224 	}
2225 
2226 	dispatch_enqueue(sch, dsq, p,
2227 			 p->scx.ddsp_enq_flags | SCX_ENQ_CLEAR_OPSS);
2228 }
2229 
2230 static bool scx_rq_online(struct rq *rq)
2231 {
2232 	/*
2233 	 * Test both cpu_active() and %SCX_RQ_ONLINE. %SCX_RQ_ONLINE indicates
2234 	 * the online state as seen from the BPF scheduler. cpu_active() test
2235 	 * guarantees that, if this function returns %true, %SCX_RQ_ONLINE will
2236 	 * stay set until the current scheduling operation is complete even if
2237 	 * we aren't locking @rq.
2238 	 */
2239 	return likely((rq->scx.flags & SCX_RQ_ONLINE) && cpu_active(cpu_of(rq)));
2240 }
2241 
2242 static void do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,
2243 			    int sticky_cpu)
2244 {
2245 	struct scx_sched *sch = scx_root;
2246 	struct task_struct **ddsp_taskp;
2247 	unsigned long qseq;
2248 
2249 	WARN_ON_ONCE(!(p->scx.flags & SCX_TASK_QUEUED));
2250 
2251 	/* rq migration */
2252 	if (sticky_cpu == cpu_of(rq))
2253 		goto local_norefill;
2254 
2255 	/*
2256 	 * If !scx_rq_online(), we already told the BPF scheduler that the CPU
2257 	 * is offline and are just running the hotplug path. Don't bother the
2258 	 * BPF scheduler.
2259 	 */
2260 	if (!scx_rq_online(rq))
2261 		goto local;
2262 
2263 	if (scx_rq_bypassing(rq)) {
2264 		__scx_add_event(sch, SCX_EV_BYPASS_DISPATCH, 1);
2265 		goto global;
2266 	}
2267 
2268 	if (p->scx.ddsp_dsq_id != SCX_DSQ_INVALID)
2269 		goto direct;
2270 
2271 	/* see %SCX_OPS_ENQ_EXITING */
2272 	if (!(sch->ops.flags & SCX_OPS_ENQ_EXITING) &&
2273 	    unlikely(p->flags & PF_EXITING)) {
2274 		__scx_add_event(sch, SCX_EV_ENQ_SKIP_EXITING, 1);
2275 		goto local;
2276 	}
2277 
2278 	/* see %SCX_OPS_ENQ_MIGRATION_DISABLED */
2279 	if (!(sch->ops.flags & SCX_OPS_ENQ_MIGRATION_DISABLED) &&
2280 	    is_migration_disabled(p)) {
2281 		__scx_add_event(sch, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED, 1);
2282 		goto local;
2283 	}
2284 
2285 	if (unlikely(!SCX_HAS_OP(sch, enqueue)))
2286 		goto global;
2287 
2288 	/* DSQ bypass didn't trigger, enqueue on the BPF scheduler */
2289 	qseq = rq->scx.ops_qseq++ << SCX_OPSS_QSEQ_SHIFT;
2290 
2291 	WARN_ON_ONCE(atomic_long_read(&p->scx.ops_state) != SCX_OPSS_NONE);
2292 	atomic_long_set(&p->scx.ops_state, SCX_OPSS_QUEUEING | qseq);
2293 
2294 	ddsp_taskp = this_cpu_ptr(&direct_dispatch_task);
2295 	WARN_ON_ONCE(*ddsp_taskp);
2296 	*ddsp_taskp = p;
2297 
2298 	SCX_CALL_OP_TASK(sch, SCX_KF_ENQUEUE, enqueue, rq, p, enq_flags);
2299 
2300 	*ddsp_taskp = NULL;
2301 	if (p->scx.ddsp_dsq_id != SCX_DSQ_INVALID)
2302 		goto direct;
2303 
2304 	/*
2305 	 * If not directly dispatched, QUEUEING isn't clear yet and dispatch or
2306 	 * dequeue may be waiting. The store_release matches their load_acquire.
2307 	 */
2308 	atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_QUEUED | qseq);
2309 	return;
2310 
2311 direct:
2312 	direct_dispatch(sch, p, enq_flags);
2313 	return;
2314 
2315 local:
2316 	/*
2317 	 * For task-ordering, slice refill must be treated as implying the end
2318 	 * of the current slice. Otherwise, the longer @p stays on the CPU, the
2319 	 * higher priority it becomes from scx_prio_less()'s POV.
2320 	 */
2321 	touch_core_sched(rq, p);
2322 	refill_task_slice_dfl(p);
2323 local_norefill:
2324 	dispatch_enqueue(sch, &rq->scx.local_dsq, p, enq_flags);
2325 	return;
2326 
2327 global:
2328 	touch_core_sched(rq, p);	/* see the comment in local: */
2329 	refill_task_slice_dfl(p);
2330 	dispatch_enqueue(sch, find_global_dsq(p), p, enq_flags);
2331 }
2332 
2333 static bool task_runnable(const struct task_struct *p)
2334 {
2335 	return !list_empty(&p->scx.runnable_node);
2336 }
2337 
2338 static void set_task_runnable(struct rq *rq, struct task_struct *p)
2339 {
2340 	lockdep_assert_rq_held(rq);
2341 
2342 	if (p->scx.flags & SCX_TASK_RESET_RUNNABLE_AT) {
2343 		p->scx.runnable_at = jiffies;
2344 		p->scx.flags &= ~SCX_TASK_RESET_RUNNABLE_AT;
2345 	}
2346 
2347 	/*
2348 	 * list_add_tail() must be used. scx_bypass() depends on tasks being
2349 	 * appended to the runnable_list.
2350 	 */
2351 	list_add_tail(&p->scx.runnable_node, &rq->scx.runnable_list);
2352 }
2353 
2354 static void clr_task_runnable(struct task_struct *p, bool reset_runnable_at)
2355 {
2356 	list_del_init(&p->scx.runnable_node);
2357 	if (reset_runnable_at)
2358 		p->scx.flags |= SCX_TASK_RESET_RUNNABLE_AT;
2359 }
2360 
2361 static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int enq_flags)
2362 {
2363 	struct scx_sched *sch = scx_root;
2364 	int sticky_cpu = p->scx.sticky_cpu;
2365 
2366 	if (enq_flags & ENQUEUE_WAKEUP)
2367 		rq->scx.flags |= SCX_RQ_IN_WAKEUP;
2368 
2369 	enq_flags |= rq->scx.extra_enq_flags;
2370 
2371 	if (sticky_cpu >= 0)
2372 		p->scx.sticky_cpu = -1;
2373 
2374 	/*
2375 	 * Restoring a running task will be immediately followed by
2376 	 * set_next_task_scx() which expects the task to not be on the BPF
2377 	 * scheduler as tasks can only start running through local DSQs. Force
2378 	 * direct-dispatch into the local DSQ by setting the sticky_cpu.
2379 	 */
2380 	if (unlikely(enq_flags & ENQUEUE_RESTORE) && task_current(rq, p))
2381 		sticky_cpu = cpu_of(rq);
2382 
2383 	if (p->scx.flags & SCX_TASK_QUEUED) {
2384 		WARN_ON_ONCE(!task_runnable(p));
2385 		goto out;
2386 	}
2387 
2388 	set_task_runnable(rq, p);
2389 	p->scx.flags |= SCX_TASK_QUEUED;
2390 	rq->scx.nr_running++;
2391 	add_nr_running(rq, 1);
2392 
2393 	if (SCX_HAS_OP(sch, runnable) && !task_on_rq_migrating(p))
2394 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, runnable, rq, p, enq_flags);
2395 
2396 	if (enq_flags & SCX_ENQ_WAKEUP)
2397 		touch_core_sched(rq, p);
2398 
2399 	do_enqueue_task(rq, p, enq_flags, sticky_cpu);
2400 out:
2401 	rq->scx.flags &= ~SCX_RQ_IN_WAKEUP;
2402 
2403 	if ((enq_flags & SCX_ENQ_CPU_SELECTED) &&
2404 	    unlikely(cpu_of(rq) != p->scx.selected_cpu))
2405 		__scx_add_event(sch, SCX_EV_SELECT_CPU_FALLBACK, 1);
2406 }
2407 
2408 static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
2409 {
2410 	struct scx_sched *sch = scx_root;
2411 	unsigned long opss;
2412 
2413 	/* dequeue is always temporary, don't reset runnable_at */
2414 	clr_task_runnable(p, false);
2415 
2416 	/* acquire ensures that we see the preceding updates on QUEUED */
2417 	opss = atomic_long_read_acquire(&p->scx.ops_state);
2418 
2419 	switch (opss & SCX_OPSS_STATE_MASK) {
2420 	case SCX_OPSS_NONE:
2421 		break;
2422 	case SCX_OPSS_QUEUEING:
2423 		/*
2424 		 * QUEUEING is started and finished while holding @p's rq lock.
2425 		 * As we're holding the rq lock now, we shouldn't see QUEUEING.
2426 		 */
2427 		BUG();
2428 	case SCX_OPSS_QUEUED:
2429 		if (SCX_HAS_OP(sch, dequeue))
2430 			SCX_CALL_OP_TASK(sch, SCX_KF_REST, dequeue, rq,
2431 					 p, deq_flags);
2432 
2433 		if (atomic_long_try_cmpxchg(&p->scx.ops_state, &opss,
2434 					    SCX_OPSS_NONE))
2435 			break;
2436 		fallthrough;
2437 	case SCX_OPSS_DISPATCHING:
2438 		/*
2439 		 * If @p is being dispatched from the BPF scheduler to a DSQ,
2440 		 * wait for the transfer to complete so that @p doesn't get
2441 		 * added to its DSQ after dequeueing is complete.
2442 		 *
2443 		 * As we're waiting on DISPATCHING with the rq locked, the
2444 		 * dispatching side shouldn't try to lock the rq while
2445 		 * DISPATCHING is set. See dispatch_to_local_dsq().
2446 		 *
2447 		 * DISPATCHING shouldn't have qseq set and control can reach
2448 		 * here with NONE @opss from the above QUEUED case block.
2449 		 * Explicitly wait on %SCX_OPSS_DISPATCHING instead of @opss.
2450 		 */
2451 		wait_ops_state(p, SCX_OPSS_DISPATCHING);
2452 		BUG_ON(atomic_long_read(&p->scx.ops_state) != SCX_OPSS_NONE);
2453 		break;
2454 	}
2455 }
2456 
2457 static bool dequeue_task_scx(struct rq *rq, struct task_struct *p, int deq_flags)
2458 {
2459 	struct scx_sched *sch = scx_root;
2460 
2461 	if (!(p->scx.flags & SCX_TASK_QUEUED)) {
2462 		WARN_ON_ONCE(task_runnable(p));
2463 		return true;
2464 	}
2465 
2466 	ops_dequeue(rq, p, deq_flags);
2467 
2468 	/*
2469 	 * A currently running task which is going off @rq first gets dequeued
2470 	 * and then stops running. As we want running <-> stopping transitions
2471 	 * to be contained within runnable <-> quiescent transitions, trigger
2472 	 * ->stopping() early here instead of in put_prev_task_scx().
2473 	 *
2474 	 * @p may go through multiple stopping <-> running transitions between
2475 	 * here and put_prev_task_scx() if task attribute changes occur while
2476 	 * balance_scx() leaves @rq unlocked. However, they don't contain any
2477 	 * information meaningful to the BPF scheduler and can be suppressed by
2478 	 * skipping the callbacks if the task is !QUEUED.
2479 	 */
2480 	if (SCX_HAS_OP(sch, stopping) && task_current(rq, p)) {
2481 		update_curr_scx(rq);
2482 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, stopping, rq, p, false);
2483 	}
2484 
2485 	if (SCX_HAS_OP(sch, quiescent) && !task_on_rq_migrating(p))
2486 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, quiescent, rq, p, deq_flags);
2487 
2488 	if (deq_flags & SCX_DEQ_SLEEP)
2489 		p->scx.flags |= SCX_TASK_DEQD_FOR_SLEEP;
2490 	else
2491 		p->scx.flags &= ~SCX_TASK_DEQD_FOR_SLEEP;
2492 
2493 	p->scx.flags &= ~SCX_TASK_QUEUED;
2494 	rq->scx.nr_running--;
2495 	sub_nr_running(rq, 1);
2496 
2497 	dispatch_dequeue(rq, p);
2498 	return true;
2499 }
2500 
2501 static void yield_task_scx(struct rq *rq)
2502 {
2503 	struct scx_sched *sch = scx_root;
2504 	struct task_struct *p = rq->curr;
2505 
2506 	if (SCX_HAS_OP(sch, yield))
2507 		SCX_CALL_OP_2TASKS_RET(sch, SCX_KF_REST, yield, rq, p, NULL);
2508 	else
2509 		p->scx.slice = 0;
2510 }
2511 
2512 static bool yield_to_task_scx(struct rq *rq, struct task_struct *to)
2513 {
2514 	struct scx_sched *sch = scx_root;
2515 	struct task_struct *from = rq->curr;
2516 
2517 	if (SCX_HAS_OP(sch, yield))
2518 		return SCX_CALL_OP_2TASKS_RET(sch, SCX_KF_REST, yield, rq,
2519 					      from, to);
2520 	else
2521 		return false;
2522 }
2523 
2524 static void move_local_task_to_local_dsq(struct task_struct *p, u64 enq_flags,
2525 					 struct scx_dispatch_q *src_dsq,
2526 					 struct rq *dst_rq)
2527 {
2528 	struct scx_dispatch_q *dst_dsq = &dst_rq->scx.local_dsq;
2529 
2530 	/* @dsq is locked and @p is on @dst_rq */
2531 	lockdep_assert_held(&src_dsq->lock);
2532 	lockdep_assert_rq_held(dst_rq);
2533 
2534 	WARN_ON_ONCE(p->scx.holding_cpu >= 0);
2535 
2536 	if (enq_flags & (SCX_ENQ_HEAD | SCX_ENQ_PREEMPT))
2537 		list_add(&p->scx.dsq_list.node, &dst_dsq->list);
2538 	else
2539 		list_add_tail(&p->scx.dsq_list.node, &dst_dsq->list);
2540 
2541 	dsq_mod_nr(dst_dsq, 1);
2542 	p->scx.dsq = dst_dsq;
2543 }
2544 
2545 #ifdef CONFIG_SMP
2546 /**
2547  * move_remote_task_to_local_dsq - Move a task from a foreign rq to a local DSQ
2548  * @p: task to move
2549  * @enq_flags: %SCX_ENQ_*
2550  * @src_rq: rq to move the task from, locked on entry, released on return
2551  * @dst_rq: rq to move the task into, locked on return
2552  *
2553  * Move @p which is currently on @src_rq to @dst_rq's local DSQ.
2554  */
2555 static void move_remote_task_to_local_dsq(struct task_struct *p, u64 enq_flags,
2556 					  struct rq *src_rq, struct rq *dst_rq)
2557 {
2558 	lockdep_assert_rq_held(src_rq);
2559 
2560 	/* the following marks @p MIGRATING which excludes dequeue */
2561 	deactivate_task(src_rq, p, 0);
2562 	set_task_cpu(p, cpu_of(dst_rq));
2563 	p->scx.sticky_cpu = cpu_of(dst_rq);
2564 
2565 	raw_spin_rq_unlock(src_rq);
2566 	raw_spin_rq_lock(dst_rq);
2567 
2568 	/*
2569 	 * We want to pass scx-specific enq_flags but activate_task() will
2570 	 * truncate the upper 32 bit. As we own @rq, we can pass them through
2571 	 * @rq->scx.extra_enq_flags instead.
2572 	 */
2573 	WARN_ON_ONCE(!cpumask_test_cpu(cpu_of(dst_rq), p->cpus_ptr));
2574 	WARN_ON_ONCE(dst_rq->scx.extra_enq_flags);
2575 	dst_rq->scx.extra_enq_flags = enq_flags;
2576 	activate_task(dst_rq, p, 0);
2577 	dst_rq->scx.extra_enq_flags = 0;
2578 }
2579 
2580 /*
2581  * Similar to kernel/sched/core.c::is_cpu_allowed(). However, there are two
2582  * differences:
2583  *
2584  * - is_cpu_allowed() asks "Can this task run on this CPU?" while
2585  *   task_can_run_on_remote_rq() asks "Can the BPF scheduler migrate the task to
2586  *   this CPU?".
2587  *
2588  *   While migration is disabled, is_cpu_allowed() has to say "yes" as the task
2589  *   must be allowed to finish on the CPU that it's currently on regardless of
2590  *   the CPU state. However, task_can_run_on_remote_rq() must say "no" as the
2591  *   BPF scheduler shouldn't attempt to migrate a task which has migration
2592  *   disabled.
2593  *
2594  * - The BPF scheduler is bypassed while the rq is offline and we can always say
2595  *   no to the BPF scheduler initiated migrations while offline.
2596  *
2597  * The caller must ensure that @p and @rq are on different CPUs.
2598  */
2599 static bool task_can_run_on_remote_rq(struct scx_sched *sch,
2600 				      struct task_struct *p, struct rq *rq,
2601 				      bool enforce)
2602 {
2603 	int cpu = cpu_of(rq);
2604 
2605 	WARN_ON_ONCE(task_cpu(p) == cpu);
2606 
2607 	/*
2608 	 * If @p has migration disabled, @p->cpus_ptr is updated to contain only
2609 	 * the pinned CPU in migrate_disable_switch() while @p is being switched
2610 	 * out. However, put_prev_task_scx() is called before @p->cpus_ptr is
2611 	 * updated and thus another CPU may see @p on a DSQ inbetween leading to
2612 	 * @p passing the below task_allowed_on_cpu() check while migration is
2613 	 * disabled.
2614 	 *
2615 	 * Test the migration disabled state first as the race window is narrow
2616 	 * and the BPF scheduler failing to check migration disabled state can
2617 	 * easily be masked if task_allowed_on_cpu() is done first.
2618 	 */
2619 	if (unlikely(is_migration_disabled(p))) {
2620 		if (enforce)
2621 			scx_error(sch, "SCX_DSQ_LOCAL[_ON] cannot move migration disabled %s[%d] from CPU %d to %d",
2622 				  p->comm, p->pid, task_cpu(p), cpu);
2623 		return false;
2624 	}
2625 
2626 	/*
2627 	 * We don't require the BPF scheduler to avoid dispatching to offline
2628 	 * CPUs mostly for convenience but also because CPUs can go offline
2629 	 * between scx_bpf_dsq_insert() calls and here. Trigger error iff the
2630 	 * picked CPU is outside the allowed mask.
2631 	 */
2632 	if (!task_allowed_on_cpu(p, cpu)) {
2633 		if (enforce)
2634 			scx_error(sch, "SCX_DSQ_LOCAL[_ON] target CPU %d not allowed for %s[%d]",
2635 				  cpu, p->comm, p->pid);
2636 		return false;
2637 	}
2638 
2639 	if (!scx_rq_online(rq)) {
2640 		if (enforce)
2641 			__scx_add_event(scx_root,
2642 					SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE, 1);
2643 		return false;
2644 	}
2645 
2646 	return true;
2647 }
2648 
2649 /**
2650  * unlink_dsq_and_lock_src_rq() - Unlink task from its DSQ and lock its task_rq
2651  * @p: target task
2652  * @dsq: locked DSQ @p is currently on
2653  * @src_rq: rq @p is currently on, stable with @dsq locked
2654  *
2655  * Called with @dsq locked but no rq's locked. We want to move @p to a different
2656  * DSQ, including any local DSQ, but are not locking @src_rq. Locking @src_rq is
2657  * required when transferring into a local DSQ. Even when transferring into a
2658  * non-local DSQ, it's better to use the same mechanism to protect against
2659  * dequeues and maintain the invariant that @p->scx.dsq can only change while
2660  * @src_rq is locked, which e.g. scx_dump_task() depends on.
2661  *
2662  * We want to grab @src_rq but that can deadlock if we try while locking @dsq,
2663  * so we want to unlink @p from @dsq, drop its lock and then lock @src_rq. As
2664  * this may race with dequeue, which can't drop the rq lock or fail, do a little
2665  * dancing from our side.
2666  *
2667  * @p->scx.holding_cpu is set to this CPU before @dsq is unlocked. If @p gets
2668  * dequeued after we unlock @dsq but before locking @src_rq, the holding_cpu
2669  * would be cleared to -1. While other cpus may have updated it to different
2670  * values afterwards, as this operation can't be preempted or recurse, the
2671  * holding_cpu can never become this CPU again before we're done. Thus, we can
2672  * tell whether we lost to dequeue by testing whether the holding_cpu still
2673  * points to this CPU. See dispatch_dequeue() for the counterpart.
2674  *
2675  * On return, @dsq is unlocked and @src_rq is locked. Returns %true if @p is
2676  * still valid. %false if lost to dequeue.
2677  */
2678 static bool unlink_dsq_and_lock_src_rq(struct task_struct *p,
2679 				       struct scx_dispatch_q *dsq,
2680 				       struct rq *src_rq)
2681 {
2682 	s32 cpu = raw_smp_processor_id();
2683 
2684 	lockdep_assert_held(&dsq->lock);
2685 
2686 	WARN_ON_ONCE(p->scx.holding_cpu >= 0);
2687 	task_unlink_from_dsq(p, dsq);
2688 	p->scx.holding_cpu = cpu;
2689 
2690 	raw_spin_unlock(&dsq->lock);
2691 	raw_spin_rq_lock(src_rq);
2692 
2693 	/* task_rq couldn't have changed if we're still the holding cpu */
2694 	return likely(p->scx.holding_cpu == cpu) &&
2695 		!WARN_ON_ONCE(src_rq != task_rq(p));
2696 }
2697 
2698 static bool consume_remote_task(struct rq *this_rq, struct task_struct *p,
2699 				struct scx_dispatch_q *dsq, struct rq *src_rq)
2700 {
2701 	raw_spin_rq_unlock(this_rq);
2702 
2703 	if (unlink_dsq_and_lock_src_rq(p, dsq, src_rq)) {
2704 		move_remote_task_to_local_dsq(p, 0, src_rq, this_rq);
2705 		return true;
2706 	} else {
2707 		raw_spin_rq_unlock(src_rq);
2708 		raw_spin_rq_lock(this_rq);
2709 		return false;
2710 	}
2711 }
2712 #else	/* CONFIG_SMP */
2713 static inline void move_remote_task_to_local_dsq(struct task_struct *p, u64 enq_flags, struct rq *src_rq, struct rq *dst_rq) { WARN_ON_ONCE(1); }
2714 static inline bool task_can_run_on_remote_rq(struct scx_sched *sch, struct task_struct *p, struct rq *rq, bool enforce) { return false; }
2715 static inline bool consume_remote_task(struct rq *this_rq, struct task_struct *p, struct scx_dispatch_q *dsq, struct rq *task_rq) { return false; }
2716 #endif	/* CONFIG_SMP */
2717 
2718 /**
2719  * move_task_between_dsqs() - Move a task from one DSQ to another
2720  * @sch: scx_sched being operated on
2721  * @p: target task
2722  * @enq_flags: %SCX_ENQ_*
2723  * @src_dsq: DSQ @p is currently on, must not be a local DSQ
2724  * @dst_dsq: DSQ @p is being moved to, can be any DSQ
2725  *
2726  * Must be called with @p's task_rq and @src_dsq locked. If @dst_dsq is a local
2727  * DSQ and @p is on a different CPU, @p will be migrated and thus its task_rq
2728  * will change. As @p's task_rq is locked, this function doesn't need to use the
2729  * holding_cpu mechanism.
2730  *
2731  * On return, @src_dsq is unlocked and only @p's new task_rq, which is the
2732  * return value, is locked.
2733  */
2734 static struct rq *move_task_between_dsqs(struct scx_sched *sch,
2735 					 struct task_struct *p, u64 enq_flags,
2736 					 struct scx_dispatch_q *src_dsq,
2737 					 struct scx_dispatch_q *dst_dsq)
2738 {
2739 	struct rq *src_rq = task_rq(p), *dst_rq;
2740 
2741 	BUG_ON(src_dsq->id == SCX_DSQ_LOCAL);
2742 	lockdep_assert_held(&src_dsq->lock);
2743 	lockdep_assert_rq_held(src_rq);
2744 
2745 	if (dst_dsq->id == SCX_DSQ_LOCAL) {
2746 		dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq);
2747 		if (src_rq != dst_rq &&
2748 		    unlikely(!task_can_run_on_remote_rq(sch, p, dst_rq, true))) {
2749 			dst_dsq = find_global_dsq(p);
2750 			dst_rq = src_rq;
2751 		}
2752 	} else {
2753 		/* no need to migrate if destination is a non-local DSQ */
2754 		dst_rq = src_rq;
2755 	}
2756 
2757 	/*
2758 	 * Move @p into $dst_dsq. If $dst_dsq is the local DSQ of a different
2759 	 * CPU, @p will be migrated.
2760 	 */
2761 	if (dst_dsq->id == SCX_DSQ_LOCAL) {
2762 		/* @p is going from a non-local DSQ to a local DSQ */
2763 		if (src_rq == dst_rq) {
2764 			task_unlink_from_dsq(p, src_dsq);
2765 			move_local_task_to_local_dsq(p, enq_flags,
2766 						     src_dsq, dst_rq);
2767 			raw_spin_unlock(&src_dsq->lock);
2768 		} else {
2769 			raw_spin_unlock(&src_dsq->lock);
2770 			move_remote_task_to_local_dsq(p, enq_flags,
2771 						      src_rq, dst_rq);
2772 		}
2773 	} else {
2774 		/*
2775 		 * @p is going from a non-local DSQ to a non-local DSQ. As
2776 		 * $src_dsq is already locked, do an abbreviated dequeue.
2777 		 */
2778 		task_unlink_from_dsq(p, src_dsq);
2779 		p->scx.dsq = NULL;
2780 		raw_spin_unlock(&src_dsq->lock);
2781 
2782 		dispatch_enqueue(sch, dst_dsq, p, enq_flags);
2783 	}
2784 
2785 	return dst_rq;
2786 }
2787 
2788 /*
2789  * A poorly behaving BPF scheduler can live-lock the system by e.g. incessantly
2790  * banging on the same DSQ on a large NUMA system to the point where switching
2791  * to the bypass mode can take a long time. Inject artificial delays while the
2792  * bypass mode is switching to guarantee timely completion.
2793  */
2794 static void scx_breather(struct rq *rq)
2795 {
2796 	u64 until;
2797 
2798 	lockdep_assert_rq_held(rq);
2799 
2800 	if (likely(!atomic_read(&scx_breather_depth)))
2801 		return;
2802 
2803 	raw_spin_rq_unlock(rq);
2804 
2805 	until = ktime_get_ns() + NSEC_PER_MSEC;
2806 
2807 	do {
2808 		int cnt = 1024;
2809 		while (atomic_read(&scx_breather_depth) && --cnt)
2810 			cpu_relax();
2811 	} while (atomic_read(&scx_breather_depth) &&
2812 		 time_before64(ktime_get_ns(), until));
2813 
2814 	raw_spin_rq_lock(rq);
2815 }
2816 
2817 static bool consume_dispatch_q(struct scx_sched *sch, struct rq *rq,
2818 			       struct scx_dispatch_q *dsq)
2819 {
2820 	struct task_struct *p;
2821 retry:
2822 	/*
2823 	 * This retry loop can repeatedly race against scx_bypass() dequeueing
2824 	 * tasks from @dsq trying to put the system into the bypass mode. On
2825 	 * some multi-socket machines (e.g. 2x Intel 8480c), this can live-lock
2826 	 * the machine into soft lockups. Give a breather.
2827 	 */
2828 	scx_breather(rq);
2829 
2830 	/*
2831 	 * The caller can't expect to successfully consume a task if the task's
2832 	 * addition to @dsq isn't guaranteed to be visible somehow. Test
2833 	 * @dsq->list without locking and skip if it seems empty.
2834 	 */
2835 	if (list_empty(&dsq->list))
2836 		return false;
2837 
2838 	raw_spin_lock(&dsq->lock);
2839 
2840 	nldsq_for_each_task(p, dsq) {
2841 		struct rq *task_rq = task_rq(p);
2842 
2843 		if (rq == task_rq) {
2844 			task_unlink_from_dsq(p, dsq);
2845 			move_local_task_to_local_dsq(p, 0, dsq, rq);
2846 			raw_spin_unlock(&dsq->lock);
2847 			return true;
2848 		}
2849 
2850 		if (task_can_run_on_remote_rq(sch, p, rq, false)) {
2851 			if (likely(consume_remote_task(rq, p, dsq, task_rq)))
2852 				return true;
2853 			goto retry;
2854 		}
2855 	}
2856 
2857 	raw_spin_unlock(&dsq->lock);
2858 	return false;
2859 }
2860 
2861 static bool consume_global_dsq(struct scx_sched *sch, struct rq *rq)
2862 {
2863 	int node = cpu_to_node(cpu_of(rq));
2864 
2865 	return consume_dispatch_q(sch, rq, sch->global_dsqs[node]);
2866 }
2867 
2868 /**
2869  * dispatch_to_local_dsq - Dispatch a task to a local dsq
2870  * @sch: scx_sched being operated on
2871  * @rq: current rq which is locked
2872  * @dst_dsq: destination DSQ
2873  * @p: task to dispatch
2874  * @enq_flags: %SCX_ENQ_*
2875  *
2876  * We're holding @rq lock and want to dispatch @p to @dst_dsq which is a local
2877  * DSQ. This function performs all the synchronization dancing needed because
2878  * local DSQs are protected with rq locks.
2879  *
2880  * The caller must have exclusive ownership of @p (e.g. through
2881  * %SCX_OPSS_DISPATCHING).
2882  */
2883 static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq,
2884 				  struct scx_dispatch_q *dst_dsq,
2885 				  struct task_struct *p, u64 enq_flags)
2886 {
2887 	struct rq *src_rq = task_rq(p);
2888 	struct rq *dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq);
2889 #ifdef CONFIG_SMP
2890 	struct rq *locked_rq = rq;
2891 #endif
2892 
2893 	/*
2894 	 * We're synchronized against dequeue through DISPATCHING. As @p can't
2895 	 * be dequeued, its task_rq and cpus_allowed are stable too.
2896 	 *
2897 	 * If dispatching to @rq that @p is already on, no lock dancing needed.
2898 	 */
2899 	if (rq == src_rq && rq == dst_rq) {
2900 		dispatch_enqueue(sch, dst_dsq, p,
2901 				 enq_flags | SCX_ENQ_CLEAR_OPSS);
2902 		return;
2903 	}
2904 
2905 #ifdef CONFIG_SMP
2906 	if (src_rq != dst_rq &&
2907 	    unlikely(!task_can_run_on_remote_rq(sch, p, dst_rq, true))) {
2908 		dispatch_enqueue(sch, find_global_dsq(p), p,
2909 				 enq_flags | SCX_ENQ_CLEAR_OPSS);
2910 		return;
2911 	}
2912 
2913 	/*
2914 	 * @p is on a possibly remote @src_rq which we need to lock to move the
2915 	 * task. If dequeue is in progress, it'd be locking @src_rq and waiting
2916 	 * on DISPATCHING, so we can't grab @src_rq lock while holding
2917 	 * DISPATCHING.
2918 	 *
2919 	 * As DISPATCHING guarantees that @p is wholly ours, we can pretend that
2920 	 * we're moving from a DSQ and use the same mechanism - mark the task
2921 	 * under transfer with holding_cpu, release DISPATCHING and then follow
2922 	 * the same protocol. See unlink_dsq_and_lock_src_rq().
2923 	 */
2924 	p->scx.holding_cpu = raw_smp_processor_id();
2925 
2926 	/* store_release ensures that dequeue sees the above */
2927 	atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE);
2928 
2929 	/* switch to @src_rq lock */
2930 	if (locked_rq != src_rq) {
2931 		raw_spin_rq_unlock(locked_rq);
2932 		locked_rq = src_rq;
2933 		raw_spin_rq_lock(src_rq);
2934 	}
2935 
2936 	/* task_rq couldn't have changed if we're still the holding cpu */
2937 	if (likely(p->scx.holding_cpu == raw_smp_processor_id()) &&
2938 	    !WARN_ON_ONCE(src_rq != task_rq(p))) {
2939 		/*
2940 		 * If @p is staying on the same rq, there's no need to go
2941 		 * through the full deactivate/activate cycle. Optimize by
2942 		 * abbreviating move_remote_task_to_local_dsq().
2943 		 */
2944 		if (src_rq == dst_rq) {
2945 			p->scx.holding_cpu = -1;
2946 			dispatch_enqueue(sch, &dst_rq->scx.local_dsq, p,
2947 					 enq_flags);
2948 		} else {
2949 			move_remote_task_to_local_dsq(p, enq_flags,
2950 						      src_rq, dst_rq);
2951 			/* task has been moved to dst_rq, which is now locked */
2952 			locked_rq = dst_rq;
2953 		}
2954 
2955 		/* if the destination CPU is idle, wake it up */
2956 		if (sched_class_above(p->sched_class, dst_rq->curr->sched_class))
2957 			resched_curr(dst_rq);
2958 	}
2959 
2960 	/* switch back to @rq lock */
2961 	if (locked_rq != rq) {
2962 		raw_spin_rq_unlock(locked_rq);
2963 		raw_spin_rq_lock(rq);
2964 	}
2965 #else	/* CONFIG_SMP */
2966 	BUG();	/* control can not reach here on UP */
2967 #endif	/* CONFIG_SMP */
2968 }
2969 
2970 /**
2971  * finish_dispatch - Asynchronously finish dispatching a task
2972  * @rq: current rq which is locked
2973  * @p: task to finish dispatching
2974  * @qseq_at_dispatch: qseq when @p started getting dispatched
2975  * @dsq_id: destination DSQ ID
2976  * @enq_flags: %SCX_ENQ_*
2977  *
2978  * Dispatching to local DSQs may need to wait for queueing to complete or
2979  * require rq lock dancing. As we don't wanna do either while inside
2980  * ops.dispatch() to avoid locking order inversion, we split dispatching into
2981  * two parts. scx_bpf_dsq_insert() which is called by ops.dispatch() records the
2982  * task and its qseq. Once ops.dispatch() returns, this function is called to
2983  * finish up.
2984  *
2985  * There is no guarantee that @p is still valid for dispatching or even that it
2986  * was valid in the first place. Make sure that the task is still owned by the
2987  * BPF scheduler and claim the ownership before dispatching.
2988  */
2989 static void finish_dispatch(struct scx_sched *sch, struct rq *rq,
2990 			    struct task_struct *p,
2991 			    unsigned long qseq_at_dispatch,
2992 			    u64 dsq_id, u64 enq_flags)
2993 {
2994 	struct scx_dispatch_q *dsq;
2995 	unsigned long opss;
2996 
2997 	touch_core_sched_dispatch(rq, p);
2998 retry:
2999 	/*
3000 	 * No need for _acquire here. @p is accessed only after a successful
3001 	 * try_cmpxchg to DISPATCHING.
3002 	 */
3003 	opss = atomic_long_read(&p->scx.ops_state);
3004 
3005 	switch (opss & SCX_OPSS_STATE_MASK) {
3006 	case SCX_OPSS_DISPATCHING:
3007 	case SCX_OPSS_NONE:
3008 		/* someone else already got to it */
3009 		return;
3010 	case SCX_OPSS_QUEUED:
3011 		/*
3012 		 * If qseq doesn't match, @p has gone through at least one
3013 		 * dispatch/dequeue and re-enqueue cycle between
3014 		 * scx_bpf_dsq_insert() and here and we have no claim on it.
3015 		 */
3016 		if ((opss & SCX_OPSS_QSEQ_MASK) != qseq_at_dispatch)
3017 			return;
3018 
3019 		/*
3020 		 * While we know @p is accessible, we don't yet have a claim on
3021 		 * it - the BPF scheduler is allowed to dispatch tasks
3022 		 * spuriously and there can be a racing dequeue attempt. Let's
3023 		 * claim @p by atomically transitioning it from QUEUED to
3024 		 * DISPATCHING.
3025 		 */
3026 		if (likely(atomic_long_try_cmpxchg(&p->scx.ops_state, &opss,
3027 						   SCX_OPSS_DISPATCHING)))
3028 			break;
3029 		goto retry;
3030 	case SCX_OPSS_QUEUEING:
3031 		/*
3032 		 * do_enqueue_task() is in the process of transferring the task
3033 		 * to the BPF scheduler while holding @p's rq lock. As we aren't
3034 		 * holding any kernel or BPF resource that the enqueue path may
3035 		 * depend upon, it's safe to wait.
3036 		 */
3037 		wait_ops_state(p, opss);
3038 		goto retry;
3039 	}
3040 
3041 	BUG_ON(!(p->scx.flags & SCX_TASK_QUEUED));
3042 
3043 	dsq = find_dsq_for_dispatch(sch, this_rq(), dsq_id, p);
3044 
3045 	if (dsq->id == SCX_DSQ_LOCAL)
3046 		dispatch_to_local_dsq(sch, rq, dsq, p, enq_flags);
3047 	else
3048 		dispatch_enqueue(sch, dsq, p, enq_flags | SCX_ENQ_CLEAR_OPSS);
3049 }
3050 
3051 static void flush_dispatch_buf(struct scx_sched *sch, struct rq *rq)
3052 {
3053 	struct scx_dsp_ctx *dspc = this_cpu_ptr(scx_dsp_ctx);
3054 	u32 u;
3055 
3056 	for (u = 0; u < dspc->cursor; u++) {
3057 		struct scx_dsp_buf_ent *ent = &dspc->buf[u];
3058 
3059 		finish_dispatch(sch, rq, ent->task, ent->qseq, ent->dsq_id,
3060 				ent->enq_flags);
3061 	}
3062 
3063 	dspc->nr_tasks += dspc->cursor;
3064 	dspc->cursor = 0;
3065 }
3066 
3067 static int balance_one(struct rq *rq, struct task_struct *prev)
3068 {
3069 	struct scx_sched *sch = scx_root;
3070 	struct scx_dsp_ctx *dspc = this_cpu_ptr(scx_dsp_ctx);
3071 	bool prev_on_scx = prev->sched_class == &ext_sched_class;
3072 	bool prev_on_rq = prev->scx.flags & SCX_TASK_QUEUED;
3073 	int nr_loops = SCX_DSP_MAX_LOOPS;
3074 
3075 	lockdep_assert_rq_held(rq);
3076 	rq->scx.flags |= SCX_RQ_IN_BALANCE;
3077 	rq->scx.flags &= ~(SCX_RQ_BAL_PENDING | SCX_RQ_BAL_KEEP);
3078 
3079 	if ((sch->ops.flags & SCX_OPS_HAS_CPU_PREEMPT) &&
3080 	    unlikely(rq->scx.cpu_released)) {
3081 		/*
3082 		 * If the previous sched_class for the current CPU was not SCX,
3083 		 * notify the BPF scheduler that it again has control of the
3084 		 * core. This callback complements ->cpu_release(), which is
3085 		 * emitted in switch_class().
3086 		 */
3087 		if (SCX_HAS_OP(sch, cpu_acquire))
3088 			SCX_CALL_OP(sch, SCX_KF_REST, cpu_acquire, rq,
3089 				    cpu_of(rq), NULL);
3090 		rq->scx.cpu_released = false;
3091 	}
3092 
3093 	if (prev_on_scx) {
3094 		update_curr_scx(rq);
3095 
3096 		/*
3097 		 * If @prev is runnable & has slice left, it has priority and
3098 		 * fetching more just increases latency for the fetched tasks.
3099 		 * Tell pick_task_scx() to keep running @prev. If the BPF
3100 		 * scheduler wants to handle this explicitly, it should
3101 		 * implement ->cpu_release().
3102 		 *
3103 		 * See scx_disable_workfn() for the explanation on the bypassing
3104 		 * test.
3105 		 */
3106 		if (prev_on_rq && prev->scx.slice && !scx_rq_bypassing(rq)) {
3107 			rq->scx.flags |= SCX_RQ_BAL_KEEP;
3108 			goto has_tasks;
3109 		}
3110 	}
3111 
3112 	/* if there already are tasks to run, nothing to do */
3113 	if (rq->scx.local_dsq.nr)
3114 		goto has_tasks;
3115 
3116 	if (consume_global_dsq(sch, rq))
3117 		goto has_tasks;
3118 
3119 	if (unlikely(!SCX_HAS_OP(sch, dispatch)) ||
3120 	    scx_rq_bypassing(rq) || !scx_rq_online(rq))
3121 		goto no_tasks;
3122 
3123 	dspc->rq = rq;
3124 
3125 	/*
3126 	 * The dispatch loop. Because flush_dispatch_buf() may drop the rq lock,
3127 	 * the local DSQ might still end up empty after a successful
3128 	 * ops.dispatch(). If the local DSQ is empty even after ops.dispatch()
3129 	 * produced some tasks, retry. The BPF scheduler may depend on this
3130 	 * looping behavior to simplify its implementation.
3131 	 */
3132 	do {
3133 		dspc->nr_tasks = 0;
3134 
3135 		SCX_CALL_OP(sch, SCX_KF_DISPATCH, dispatch, rq,
3136 			    cpu_of(rq), prev_on_scx ? prev : NULL);
3137 
3138 		flush_dispatch_buf(sch, rq);
3139 
3140 		if (prev_on_rq && prev->scx.slice) {
3141 			rq->scx.flags |= SCX_RQ_BAL_KEEP;
3142 			goto has_tasks;
3143 		}
3144 		if (rq->scx.local_dsq.nr)
3145 			goto has_tasks;
3146 		if (consume_global_dsq(sch, rq))
3147 			goto has_tasks;
3148 
3149 		/*
3150 		 * ops.dispatch() can trap us in this loop by repeatedly
3151 		 * dispatching ineligible tasks. Break out once in a while to
3152 		 * allow the watchdog to run. As IRQ can't be enabled in
3153 		 * balance(), we want to complete this scheduling cycle and then
3154 		 * start a new one. IOW, we want to call resched_curr() on the
3155 		 * next, most likely idle, task, not the current one. Use
3156 		 * scx_bpf_kick_cpu() for deferred kicking.
3157 		 */
3158 		if (unlikely(!--nr_loops)) {
3159 			scx_bpf_kick_cpu(cpu_of(rq), 0);
3160 			break;
3161 		}
3162 	} while (dspc->nr_tasks);
3163 
3164 no_tasks:
3165 	/*
3166 	 * Didn't find another task to run. Keep running @prev unless
3167 	 * %SCX_OPS_ENQ_LAST is in effect.
3168 	 */
3169 	if (prev_on_rq &&
3170 	    (!(sch->ops.flags & SCX_OPS_ENQ_LAST) || scx_rq_bypassing(rq))) {
3171 		rq->scx.flags |= SCX_RQ_BAL_KEEP;
3172 		__scx_add_event(sch, SCX_EV_DISPATCH_KEEP_LAST, 1);
3173 		goto has_tasks;
3174 	}
3175 	rq->scx.flags &= ~SCX_RQ_IN_BALANCE;
3176 	return false;
3177 
3178 has_tasks:
3179 	rq->scx.flags &= ~SCX_RQ_IN_BALANCE;
3180 	return true;
3181 }
3182 
3183 static int balance_scx(struct rq *rq, struct task_struct *prev,
3184 		       struct rq_flags *rf)
3185 {
3186 	int ret;
3187 
3188 	rq_unpin_lock(rq, rf);
3189 
3190 	ret = balance_one(rq, prev);
3191 
3192 #ifdef CONFIG_SCHED_SMT
3193 	/*
3194 	 * When core-sched is enabled, this ops.balance() call will be followed
3195 	 * by pick_task_scx() on this CPU and the SMT siblings. Balance the
3196 	 * siblings too.
3197 	 */
3198 	if (sched_core_enabled(rq)) {
3199 		const struct cpumask *smt_mask = cpu_smt_mask(cpu_of(rq));
3200 		int scpu;
3201 
3202 		for_each_cpu_andnot(scpu, smt_mask, cpumask_of(cpu_of(rq))) {
3203 			struct rq *srq = cpu_rq(scpu);
3204 			struct task_struct *sprev = srq->curr;
3205 
3206 			WARN_ON_ONCE(__rq_lockp(rq) != __rq_lockp(srq));
3207 			update_rq_clock(srq);
3208 			balance_one(srq, sprev);
3209 		}
3210 	}
3211 #endif
3212 	rq_repin_lock(rq, rf);
3213 
3214 	return ret;
3215 }
3216 
3217 static void process_ddsp_deferred_locals(struct rq *rq)
3218 {
3219 	struct task_struct *p;
3220 
3221 	lockdep_assert_rq_held(rq);
3222 
3223 	/*
3224 	 * Now that @rq can be unlocked, execute the deferred enqueueing of
3225 	 * tasks directly dispatched to the local DSQs of other CPUs. See
3226 	 * direct_dispatch(). Keep popping from the head instead of using
3227 	 * list_for_each_entry_safe() as dispatch_local_dsq() may unlock @rq
3228 	 * temporarily.
3229 	 */
3230 	while ((p = list_first_entry_or_null(&rq->scx.ddsp_deferred_locals,
3231 				struct task_struct, scx.dsq_list.node))) {
3232 		struct scx_sched *sch = scx_root;
3233 		struct scx_dispatch_q *dsq;
3234 
3235 		list_del_init(&p->scx.dsq_list.node);
3236 
3237 		dsq = find_dsq_for_dispatch(sch, rq, p->scx.ddsp_dsq_id, p);
3238 		if (!WARN_ON_ONCE(dsq->id != SCX_DSQ_LOCAL))
3239 			dispatch_to_local_dsq(sch, rq, dsq, p,
3240 					      p->scx.ddsp_enq_flags);
3241 	}
3242 }
3243 
3244 static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
3245 {
3246 	struct scx_sched *sch = scx_root;
3247 
3248 	if (p->scx.flags & SCX_TASK_QUEUED) {
3249 		/*
3250 		 * Core-sched might decide to execute @p before it is
3251 		 * dispatched. Call ops_dequeue() to notify the BPF scheduler.
3252 		 */
3253 		ops_dequeue(rq, p, SCX_DEQ_CORE_SCHED_EXEC);
3254 		dispatch_dequeue(rq, p);
3255 	}
3256 
3257 	p->se.exec_start = rq_clock_task(rq);
3258 
3259 	/* see dequeue_task_scx() on why we skip when !QUEUED */
3260 	if (SCX_HAS_OP(sch, running) && (p->scx.flags & SCX_TASK_QUEUED))
3261 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, running, rq, p);
3262 
3263 	clr_task_runnable(p, true);
3264 
3265 	/*
3266 	 * @p is getting newly scheduled or got kicked after someone updated its
3267 	 * slice. Refresh whether tick can be stopped. See scx_can_stop_tick().
3268 	 */
3269 	if ((p->scx.slice == SCX_SLICE_INF) !=
3270 	    (bool)(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) {
3271 		if (p->scx.slice == SCX_SLICE_INF)
3272 			rq->scx.flags |= SCX_RQ_CAN_STOP_TICK;
3273 		else
3274 			rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
3275 
3276 		sched_update_tick_dependency(rq);
3277 
3278 		/*
3279 		 * For now, let's refresh the load_avgs just when transitioning
3280 		 * in and out of nohz. In the future, we might want to add a
3281 		 * mechanism which calls the following periodically on
3282 		 * tick-stopped CPUs.
3283 		 */
3284 		update_other_load_avgs(rq);
3285 	}
3286 }
3287 
3288 static enum scx_cpu_preempt_reason
3289 preempt_reason_from_class(const struct sched_class *class)
3290 {
3291 #ifdef CONFIG_SMP
3292 	if (class == &stop_sched_class)
3293 		return SCX_CPU_PREEMPT_STOP;
3294 #endif
3295 	if (class == &dl_sched_class)
3296 		return SCX_CPU_PREEMPT_DL;
3297 	if (class == &rt_sched_class)
3298 		return SCX_CPU_PREEMPT_RT;
3299 	return SCX_CPU_PREEMPT_UNKNOWN;
3300 }
3301 
3302 static void switch_class(struct rq *rq, struct task_struct *next)
3303 {
3304 	struct scx_sched *sch = scx_root;
3305 	const struct sched_class *next_class = next->sched_class;
3306 
3307 #ifdef CONFIG_SMP
3308 	/*
3309 	 * Pairs with the smp_load_acquire() issued by a CPU in
3310 	 * kick_cpus_irq_workfn() who is waiting for this CPU to perform a
3311 	 * resched.
3312 	 */
3313 	smp_store_release(&rq->scx.pnt_seq, rq->scx.pnt_seq + 1);
3314 #endif
3315 	if (!(sch->ops.flags & SCX_OPS_HAS_CPU_PREEMPT))
3316 		return;
3317 
3318 	/*
3319 	 * The callback is conceptually meant to convey that the CPU is no
3320 	 * longer under the control of SCX. Therefore, don't invoke the callback
3321 	 * if the next class is below SCX (in which case the BPF scheduler has
3322 	 * actively decided not to schedule any tasks on the CPU).
3323 	 */
3324 	if (sched_class_above(&ext_sched_class, next_class))
3325 		return;
3326 
3327 	/*
3328 	 * At this point we know that SCX was preempted by a higher priority
3329 	 * sched_class, so invoke the ->cpu_release() callback if we have not
3330 	 * done so already. We only send the callback once between SCX being
3331 	 * preempted, and it regaining control of the CPU.
3332 	 *
3333 	 * ->cpu_release() complements ->cpu_acquire(), which is emitted the
3334 	 *  next time that balance_scx() is invoked.
3335 	 */
3336 	if (!rq->scx.cpu_released) {
3337 		if (SCX_HAS_OP(sch, cpu_release)) {
3338 			struct scx_cpu_release_args args = {
3339 				.reason = preempt_reason_from_class(next_class),
3340 				.task = next,
3341 			};
3342 
3343 			SCX_CALL_OP(sch, SCX_KF_CPU_RELEASE, cpu_release, rq,
3344 				    cpu_of(rq), &args);
3345 		}
3346 		rq->scx.cpu_released = true;
3347 	}
3348 }
3349 
3350 static void put_prev_task_scx(struct rq *rq, struct task_struct *p,
3351 			      struct task_struct *next)
3352 {
3353 	struct scx_sched *sch = scx_root;
3354 	update_curr_scx(rq);
3355 
3356 	/* see dequeue_task_scx() on why we skip when !QUEUED */
3357 	if (SCX_HAS_OP(sch, stopping) && (p->scx.flags & SCX_TASK_QUEUED))
3358 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, stopping, rq, p, true);
3359 
3360 	if (p->scx.flags & SCX_TASK_QUEUED) {
3361 		set_task_runnable(rq, p);
3362 
3363 		/*
3364 		 * If @p has slice left and is being put, @p is getting
3365 		 * preempted by a higher priority scheduler class or core-sched
3366 		 * forcing a different task. Leave it at the head of the local
3367 		 * DSQ.
3368 		 */
3369 		if (p->scx.slice && !scx_rq_bypassing(rq)) {
3370 			dispatch_enqueue(sch, &rq->scx.local_dsq, p,
3371 					 SCX_ENQ_HEAD);
3372 			goto switch_class;
3373 		}
3374 
3375 		/*
3376 		 * If @p is runnable but we're about to enter a lower
3377 		 * sched_class, %SCX_OPS_ENQ_LAST must be set. Tell
3378 		 * ops.enqueue() that @p is the only one available for this cpu,
3379 		 * which should trigger an explicit follow-up scheduling event.
3380 		 */
3381 		if (sched_class_above(&ext_sched_class, next->sched_class)) {
3382 			WARN_ON_ONCE(!(sch->ops.flags & SCX_OPS_ENQ_LAST));
3383 			do_enqueue_task(rq, p, SCX_ENQ_LAST, -1);
3384 		} else {
3385 			do_enqueue_task(rq, p, 0, -1);
3386 		}
3387 	}
3388 
3389 switch_class:
3390 	if (next && next->sched_class != &ext_sched_class)
3391 		switch_class(rq, next);
3392 }
3393 
3394 static struct task_struct *first_local_task(struct rq *rq)
3395 {
3396 	return list_first_entry_or_null(&rq->scx.local_dsq.list,
3397 					struct task_struct, scx.dsq_list.node);
3398 }
3399 
3400 static struct task_struct *pick_task_scx(struct rq *rq)
3401 {
3402 	struct task_struct *prev = rq->curr;
3403 	struct task_struct *p;
3404 	bool keep_prev = rq->scx.flags & SCX_RQ_BAL_KEEP;
3405 	bool kick_idle = false;
3406 
3407 	/*
3408 	 * WORKAROUND:
3409 	 *
3410 	 * %SCX_RQ_BAL_KEEP should be set iff $prev is on SCX as it must just
3411 	 * have gone through balance_scx(). Unfortunately, there currently is a
3412 	 * bug where fair could say yes on balance() but no on pick_task(),
3413 	 * which then ends up calling pick_task_scx() without preceding
3414 	 * balance_scx().
3415 	 *
3416 	 * Keep running @prev if possible and avoid stalling from entering idle
3417 	 * without balancing.
3418 	 *
3419 	 * Once fair is fixed, remove the workaround and trigger WARN_ON_ONCE()
3420 	 * if pick_task_scx() is called without preceding balance_scx().
3421 	 */
3422 	if (unlikely(rq->scx.flags & SCX_RQ_BAL_PENDING)) {
3423 		if (prev->scx.flags & SCX_TASK_QUEUED) {
3424 			keep_prev = true;
3425 		} else {
3426 			keep_prev = false;
3427 			kick_idle = true;
3428 		}
3429 	} else if (unlikely(keep_prev &&
3430 			    prev->sched_class != &ext_sched_class)) {
3431 		/*
3432 		 * Can happen while enabling as SCX_RQ_BAL_PENDING assertion is
3433 		 * conditional on scx_enabled() and may have been skipped.
3434 		 */
3435 		WARN_ON_ONCE(scx_enable_state() == SCX_ENABLED);
3436 		keep_prev = false;
3437 	}
3438 
3439 	/*
3440 	 * If balance_scx() is telling us to keep running @prev, replenish slice
3441 	 * if necessary and keep running @prev. Otherwise, pop the first one
3442 	 * from the local DSQ.
3443 	 */
3444 	if (keep_prev) {
3445 		p = prev;
3446 		if (!p->scx.slice)
3447 			refill_task_slice_dfl(p);
3448 	} else {
3449 		p = first_local_task(rq);
3450 		if (!p) {
3451 			if (kick_idle)
3452 				scx_bpf_kick_cpu(cpu_of(rq), SCX_KICK_IDLE);
3453 			return NULL;
3454 		}
3455 
3456 		if (unlikely(!p->scx.slice)) {
3457 			struct scx_sched *sch = scx_root;
3458 
3459 			if (!scx_rq_bypassing(rq) && !sch->warned_zero_slice) {
3460 				printk_deferred(KERN_WARNING "sched_ext: %s[%d] has zero slice in %s()\n",
3461 						p->comm, p->pid, __func__);
3462 				sch->warned_zero_slice = true;
3463 			}
3464 			refill_task_slice_dfl(p);
3465 		}
3466 	}
3467 
3468 	return p;
3469 }
3470 
3471 #ifdef CONFIG_SCHED_CORE
3472 /**
3473  * scx_prio_less - Task ordering for core-sched
3474  * @a: task A
3475  * @b: task B
3476  * @in_fi: in forced idle state
3477  *
3478  * Core-sched is implemented as an additional scheduling layer on top of the
3479  * usual sched_class'es and needs to find out the expected task ordering. For
3480  * SCX, core-sched calls this function to interrogate the task ordering.
3481  *
3482  * Unless overridden by ops.core_sched_before(), @p->scx.core_sched_at is used
3483  * to implement the default task ordering. The older the timestamp, the higher
3484  * priority the task - the global FIFO ordering matching the default scheduling
3485  * behavior.
3486  *
3487  * When ops.core_sched_before() is enabled, @p->scx.core_sched_at is used to
3488  * implement FIFO ordering within each local DSQ. See pick_task_scx().
3489  */
3490 bool scx_prio_less(const struct task_struct *a, const struct task_struct *b,
3491 		   bool in_fi)
3492 {
3493 	struct scx_sched *sch = scx_root;
3494 
3495 	/*
3496 	 * The const qualifiers are dropped from task_struct pointers when
3497 	 * calling ops.core_sched_before(). Accesses are controlled by the
3498 	 * verifier.
3499 	 */
3500 	if (SCX_HAS_OP(sch, core_sched_before) &&
3501 	    !scx_rq_bypassing(task_rq(a)))
3502 		return SCX_CALL_OP_2TASKS_RET(sch, SCX_KF_REST, core_sched_before,
3503 					      NULL,
3504 					      (struct task_struct *)a,
3505 					      (struct task_struct *)b);
3506 	else
3507 		return time_after64(a->scx.core_sched_at, b->scx.core_sched_at);
3508 }
3509 #endif	/* CONFIG_SCHED_CORE */
3510 
3511 #ifdef CONFIG_SMP
3512 
3513 static int select_task_rq_scx(struct task_struct *p, int prev_cpu, int wake_flags)
3514 {
3515 	struct scx_sched *sch = scx_root;
3516 	bool rq_bypass;
3517 
3518 	/*
3519 	 * sched_exec() calls with %WF_EXEC when @p is about to exec(2) as it
3520 	 * can be a good migration opportunity with low cache and memory
3521 	 * footprint. Returning a CPU different than @prev_cpu triggers
3522 	 * immediate rq migration. However, for SCX, as the current rq
3523 	 * association doesn't dictate where the task is going to run, this
3524 	 * doesn't fit well. If necessary, we can later add a dedicated method
3525 	 * which can decide to preempt self to force it through the regular
3526 	 * scheduling path.
3527 	 */
3528 	if (unlikely(wake_flags & WF_EXEC))
3529 		return prev_cpu;
3530 
3531 	rq_bypass = scx_rq_bypassing(task_rq(p));
3532 	if (likely(SCX_HAS_OP(sch, select_cpu)) && !rq_bypass) {
3533 		s32 cpu;
3534 		struct task_struct **ddsp_taskp;
3535 
3536 		ddsp_taskp = this_cpu_ptr(&direct_dispatch_task);
3537 		WARN_ON_ONCE(*ddsp_taskp);
3538 		*ddsp_taskp = p;
3539 
3540 		cpu = SCX_CALL_OP_TASK_RET(sch,
3541 					   SCX_KF_ENQUEUE | SCX_KF_SELECT_CPU,
3542 					   select_cpu, NULL, p, prev_cpu,
3543 					   wake_flags);
3544 		p->scx.selected_cpu = cpu;
3545 		*ddsp_taskp = NULL;
3546 		if (ops_cpu_valid(sch, cpu, "from ops.select_cpu()"))
3547 			return cpu;
3548 		else
3549 			return prev_cpu;
3550 	} else {
3551 		s32 cpu;
3552 
3553 		cpu = scx_select_cpu_dfl(p, prev_cpu, wake_flags, NULL, 0);
3554 		if (cpu >= 0) {
3555 			refill_task_slice_dfl(p);
3556 			p->scx.ddsp_dsq_id = SCX_DSQ_LOCAL;
3557 		} else {
3558 			cpu = prev_cpu;
3559 		}
3560 		p->scx.selected_cpu = cpu;
3561 
3562 		if (rq_bypass)
3563 			__scx_add_event(sch, SCX_EV_BYPASS_DISPATCH, 1);
3564 		return cpu;
3565 	}
3566 }
3567 
3568 static void task_woken_scx(struct rq *rq, struct task_struct *p)
3569 {
3570 	run_deferred(rq);
3571 }
3572 
3573 static void set_cpus_allowed_scx(struct task_struct *p,
3574 				 struct affinity_context *ac)
3575 {
3576 	struct scx_sched *sch = scx_root;
3577 
3578 	set_cpus_allowed_common(p, ac);
3579 
3580 	/*
3581 	 * The effective cpumask is stored in @p->cpus_ptr which may temporarily
3582 	 * differ from the configured one in @p->cpus_mask. Always tell the bpf
3583 	 * scheduler the effective one.
3584 	 *
3585 	 * Fine-grained memory write control is enforced by BPF making the const
3586 	 * designation pointless. Cast it away when calling the operation.
3587 	 */
3588 	if (SCX_HAS_OP(sch, set_cpumask))
3589 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, set_cpumask, NULL,
3590 				 p, (struct cpumask *)p->cpus_ptr);
3591 }
3592 
3593 static void handle_hotplug(struct rq *rq, bool online)
3594 {
3595 	struct scx_sched *sch = scx_root;
3596 	int cpu = cpu_of(rq);
3597 
3598 	atomic_long_inc(&scx_hotplug_seq);
3599 
3600 	/*
3601 	 * scx_root updates are protected by cpus_read_lock() and will stay
3602 	 * stable here. Note that we can't depend on scx_enabled() test as the
3603 	 * hotplug ops need to be enabled before __scx_enabled is set.
3604 	 */
3605 	if (unlikely(!sch))
3606 		return;
3607 
3608 	if (scx_enabled())
3609 		scx_idle_update_selcpu_topology(&sch->ops);
3610 
3611 	if (online && SCX_HAS_OP(sch, cpu_online))
3612 		SCX_CALL_OP(sch, SCX_KF_UNLOCKED, cpu_online, NULL, cpu);
3613 	else if (!online && SCX_HAS_OP(sch, cpu_offline))
3614 		SCX_CALL_OP(sch, SCX_KF_UNLOCKED, cpu_offline, NULL, cpu);
3615 	else
3616 		scx_exit(sch, SCX_EXIT_UNREG_KERN,
3617 			 SCX_ECODE_ACT_RESTART | SCX_ECODE_RSN_HOTPLUG,
3618 			 "cpu %d going %s, exiting scheduler", cpu,
3619 			 online ? "online" : "offline");
3620 }
3621 
3622 void scx_rq_activate(struct rq *rq)
3623 {
3624 	handle_hotplug(rq, true);
3625 }
3626 
3627 void scx_rq_deactivate(struct rq *rq)
3628 {
3629 	handle_hotplug(rq, false);
3630 }
3631 
3632 static void rq_online_scx(struct rq *rq)
3633 {
3634 	rq->scx.flags |= SCX_RQ_ONLINE;
3635 }
3636 
3637 static void rq_offline_scx(struct rq *rq)
3638 {
3639 	rq->scx.flags &= ~SCX_RQ_ONLINE;
3640 }
3641 
3642 #endif	/* CONFIG_SMP */
3643 
3644 static bool check_rq_for_timeouts(struct rq *rq)
3645 {
3646 	struct scx_sched *sch;
3647 	struct task_struct *p;
3648 	struct rq_flags rf;
3649 	bool timed_out = false;
3650 
3651 	rq_lock_irqsave(rq, &rf);
3652 	sch = rcu_dereference_bh(scx_root);
3653 	if (unlikely(!sch))
3654 		goto out_unlock;
3655 
3656 	list_for_each_entry(p, &rq->scx.runnable_list, scx.runnable_node) {
3657 		unsigned long last_runnable = p->scx.runnable_at;
3658 
3659 		if (unlikely(time_after(jiffies,
3660 					last_runnable + scx_watchdog_timeout))) {
3661 			u32 dur_ms = jiffies_to_msecs(jiffies - last_runnable);
3662 
3663 			scx_exit(sch, SCX_EXIT_ERROR_STALL, 0,
3664 				 "%s[%d] failed to run for %u.%03us",
3665 				 p->comm, p->pid, dur_ms / 1000, dur_ms % 1000);
3666 			timed_out = true;
3667 			break;
3668 		}
3669 	}
3670 out_unlock:
3671 	rq_unlock_irqrestore(rq, &rf);
3672 	return timed_out;
3673 }
3674 
3675 static void scx_watchdog_workfn(struct work_struct *work)
3676 {
3677 	int cpu;
3678 
3679 	WRITE_ONCE(scx_watchdog_timestamp, jiffies);
3680 
3681 	for_each_online_cpu(cpu) {
3682 		if (unlikely(check_rq_for_timeouts(cpu_rq(cpu))))
3683 			break;
3684 
3685 		cond_resched();
3686 	}
3687 	queue_delayed_work(system_unbound_wq, to_delayed_work(work),
3688 			   scx_watchdog_timeout / 2);
3689 }
3690 
3691 void scx_tick(struct rq *rq)
3692 {
3693 	struct scx_sched *sch;
3694 	unsigned long last_check;
3695 
3696 	if (!scx_enabled())
3697 		return;
3698 
3699 	sch = rcu_dereference_bh(scx_root);
3700 	if (unlikely(!sch))
3701 		return;
3702 
3703 	last_check = READ_ONCE(scx_watchdog_timestamp);
3704 	if (unlikely(time_after(jiffies,
3705 				last_check + READ_ONCE(scx_watchdog_timeout)))) {
3706 		u32 dur_ms = jiffies_to_msecs(jiffies - last_check);
3707 
3708 		scx_exit(sch, SCX_EXIT_ERROR_STALL, 0,
3709 			 "watchdog failed to check in for %u.%03us",
3710 			 dur_ms / 1000, dur_ms % 1000);
3711 	}
3712 
3713 	update_other_load_avgs(rq);
3714 }
3715 
3716 static void task_tick_scx(struct rq *rq, struct task_struct *curr, int queued)
3717 {
3718 	struct scx_sched *sch = scx_root;
3719 
3720 	update_curr_scx(rq);
3721 
3722 	/*
3723 	 * While disabling, always resched and refresh core-sched timestamp as
3724 	 * we can't trust the slice management or ops.core_sched_before().
3725 	 */
3726 	if (scx_rq_bypassing(rq)) {
3727 		curr->scx.slice = 0;
3728 		touch_core_sched(rq, curr);
3729 	} else if (SCX_HAS_OP(sch, tick)) {
3730 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, tick, rq, curr);
3731 	}
3732 
3733 	if (!curr->scx.slice)
3734 		resched_curr(rq);
3735 }
3736 
3737 #ifdef CONFIG_EXT_GROUP_SCHED
3738 static struct cgroup *tg_cgrp(struct task_group *tg)
3739 {
3740 	/*
3741 	 * If CGROUP_SCHED is disabled, @tg is NULL. If @tg is an autogroup,
3742 	 * @tg->css.cgroup is NULL. In both cases, @tg can be treated as the
3743 	 * root cgroup.
3744 	 */
3745 	if (tg && tg->css.cgroup)
3746 		return tg->css.cgroup;
3747 	else
3748 		return &cgrp_dfl_root.cgrp;
3749 }
3750 
3751 #define SCX_INIT_TASK_ARGS_CGROUP(tg)		.cgroup = tg_cgrp(tg),
3752 
3753 #else	/* CONFIG_EXT_GROUP_SCHED */
3754 
3755 #define SCX_INIT_TASK_ARGS_CGROUP(tg)
3756 
3757 #endif	/* CONFIG_EXT_GROUP_SCHED */
3758 
3759 static enum scx_task_state scx_get_task_state(const struct task_struct *p)
3760 {
3761 	return (p->scx.flags & SCX_TASK_STATE_MASK) >> SCX_TASK_STATE_SHIFT;
3762 }
3763 
3764 static void scx_set_task_state(struct task_struct *p, enum scx_task_state state)
3765 {
3766 	enum scx_task_state prev_state = scx_get_task_state(p);
3767 	bool warn = false;
3768 
3769 	BUILD_BUG_ON(SCX_TASK_NR_STATES > (1 << SCX_TASK_STATE_BITS));
3770 
3771 	switch (state) {
3772 	case SCX_TASK_NONE:
3773 		break;
3774 	case SCX_TASK_INIT:
3775 		warn = prev_state != SCX_TASK_NONE;
3776 		break;
3777 	case SCX_TASK_READY:
3778 		warn = prev_state == SCX_TASK_NONE;
3779 		break;
3780 	case SCX_TASK_ENABLED:
3781 		warn = prev_state != SCX_TASK_READY;
3782 		break;
3783 	default:
3784 		warn = true;
3785 		return;
3786 	}
3787 
3788 	WARN_ONCE(warn, "sched_ext: Invalid task state transition %d -> %d for %s[%d]",
3789 		  prev_state, state, p->comm, p->pid);
3790 
3791 	p->scx.flags &= ~SCX_TASK_STATE_MASK;
3792 	p->scx.flags |= state << SCX_TASK_STATE_SHIFT;
3793 }
3794 
3795 static int scx_init_task(struct task_struct *p, struct task_group *tg, bool fork)
3796 {
3797 	struct scx_sched *sch = scx_root;
3798 	int ret;
3799 
3800 	p->scx.disallow = false;
3801 
3802 	if (SCX_HAS_OP(sch, init_task)) {
3803 		struct scx_init_task_args args = {
3804 			SCX_INIT_TASK_ARGS_CGROUP(tg)
3805 			.fork = fork,
3806 		};
3807 
3808 		ret = SCX_CALL_OP_RET(sch, SCX_KF_UNLOCKED, init_task, NULL,
3809 				      p, &args);
3810 		if (unlikely(ret)) {
3811 			ret = ops_sanitize_err(sch, "init_task", ret);
3812 			return ret;
3813 		}
3814 	}
3815 
3816 	scx_set_task_state(p, SCX_TASK_INIT);
3817 
3818 	if (p->scx.disallow) {
3819 		if (!fork) {
3820 			struct rq *rq;
3821 			struct rq_flags rf;
3822 
3823 			rq = task_rq_lock(p, &rf);
3824 
3825 			/*
3826 			 * We're in the load path and @p->policy will be applied
3827 			 * right after. Reverting @p->policy here and rejecting
3828 			 * %SCHED_EXT transitions from scx_check_setscheduler()
3829 			 * guarantees that if ops.init_task() sets @p->disallow,
3830 			 * @p can never be in SCX.
3831 			 */
3832 			if (p->policy == SCHED_EXT) {
3833 				p->policy = SCHED_NORMAL;
3834 				atomic_long_inc(&scx_nr_rejected);
3835 			}
3836 
3837 			task_rq_unlock(rq, p, &rf);
3838 		} else if (p->policy == SCHED_EXT) {
3839 			scx_error(sch, "ops.init_task() set task->scx.disallow for %s[%d] during fork",
3840 				  p->comm, p->pid);
3841 		}
3842 	}
3843 
3844 	p->scx.flags |= SCX_TASK_RESET_RUNNABLE_AT;
3845 	return 0;
3846 }
3847 
3848 static void scx_enable_task(struct task_struct *p)
3849 {
3850 	struct scx_sched *sch = scx_root;
3851 	struct rq *rq = task_rq(p);
3852 	u32 weight;
3853 
3854 	lockdep_assert_rq_held(rq);
3855 
3856 	/*
3857 	 * Set the weight before calling ops.enable() so that the scheduler
3858 	 * doesn't see a stale value if they inspect the task struct.
3859 	 */
3860 	if (task_has_idle_policy(p))
3861 		weight = WEIGHT_IDLEPRIO;
3862 	else
3863 		weight = sched_prio_to_weight[p->static_prio - MAX_RT_PRIO];
3864 
3865 	p->scx.weight = sched_weight_to_cgroup(weight);
3866 
3867 	if (SCX_HAS_OP(sch, enable))
3868 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, enable, rq, p);
3869 	scx_set_task_state(p, SCX_TASK_ENABLED);
3870 
3871 	if (SCX_HAS_OP(sch, set_weight))
3872 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, set_weight, rq,
3873 				 p, p->scx.weight);
3874 }
3875 
3876 static void scx_disable_task(struct task_struct *p)
3877 {
3878 	struct scx_sched *sch = scx_root;
3879 	struct rq *rq = task_rq(p);
3880 
3881 	lockdep_assert_rq_held(rq);
3882 	WARN_ON_ONCE(scx_get_task_state(p) != SCX_TASK_ENABLED);
3883 
3884 	if (SCX_HAS_OP(sch, disable))
3885 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, disable, rq, p);
3886 	scx_set_task_state(p, SCX_TASK_READY);
3887 }
3888 
3889 static void scx_exit_task(struct task_struct *p)
3890 {
3891 	struct scx_sched *sch = scx_root;
3892 	struct scx_exit_task_args args = {
3893 		.cancelled = false,
3894 	};
3895 
3896 	lockdep_assert_rq_held(task_rq(p));
3897 
3898 	switch (scx_get_task_state(p)) {
3899 	case SCX_TASK_NONE:
3900 		return;
3901 	case SCX_TASK_INIT:
3902 		args.cancelled = true;
3903 		break;
3904 	case SCX_TASK_READY:
3905 		break;
3906 	case SCX_TASK_ENABLED:
3907 		scx_disable_task(p);
3908 		break;
3909 	default:
3910 		WARN_ON_ONCE(true);
3911 		return;
3912 	}
3913 
3914 	if (SCX_HAS_OP(sch, exit_task))
3915 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, exit_task, task_rq(p),
3916 				 p, &args);
3917 	scx_set_task_state(p, SCX_TASK_NONE);
3918 }
3919 
3920 void init_scx_entity(struct sched_ext_entity *scx)
3921 {
3922 	memset(scx, 0, sizeof(*scx));
3923 	INIT_LIST_HEAD(&scx->dsq_list.node);
3924 	RB_CLEAR_NODE(&scx->dsq_priq);
3925 	scx->sticky_cpu = -1;
3926 	scx->holding_cpu = -1;
3927 	INIT_LIST_HEAD(&scx->runnable_node);
3928 	scx->runnable_at = jiffies;
3929 	scx->ddsp_dsq_id = SCX_DSQ_INVALID;
3930 	scx->slice = SCX_SLICE_DFL;
3931 }
3932 
3933 void scx_pre_fork(struct task_struct *p)
3934 {
3935 	/*
3936 	 * BPF scheduler enable/disable paths want to be able to iterate and
3937 	 * update all tasks which can become complex when racing forks. As
3938 	 * enable/disable are very cold paths, let's use a percpu_rwsem to
3939 	 * exclude forks.
3940 	 */
3941 	percpu_down_read(&scx_fork_rwsem);
3942 }
3943 
3944 int scx_fork(struct task_struct *p)
3945 {
3946 	percpu_rwsem_assert_held(&scx_fork_rwsem);
3947 
3948 	if (scx_init_task_enabled)
3949 		return scx_init_task(p, task_group(p), true);
3950 	else
3951 		return 0;
3952 }
3953 
3954 void scx_post_fork(struct task_struct *p)
3955 {
3956 	if (scx_init_task_enabled) {
3957 		scx_set_task_state(p, SCX_TASK_READY);
3958 
3959 		/*
3960 		 * Enable the task immediately if it's running on sched_ext.
3961 		 * Otherwise, it'll be enabled in switching_to_scx() if and
3962 		 * when it's ever configured to run with a SCHED_EXT policy.
3963 		 */
3964 		if (p->sched_class == &ext_sched_class) {
3965 			struct rq_flags rf;
3966 			struct rq *rq;
3967 
3968 			rq = task_rq_lock(p, &rf);
3969 			scx_enable_task(p);
3970 			task_rq_unlock(rq, p, &rf);
3971 		}
3972 	}
3973 
3974 	spin_lock_irq(&scx_tasks_lock);
3975 	list_add_tail(&p->scx.tasks_node, &scx_tasks);
3976 	spin_unlock_irq(&scx_tasks_lock);
3977 
3978 	percpu_up_read(&scx_fork_rwsem);
3979 }
3980 
3981 void scx_cancel_fork(struct task_struct *p)
3982 {
3983 	if (scx_enabled()) {
3984 		struct rq *rq;
3985 		struct rq_flags rf;
3986 
3987 		rq = task_rq_lock(p, &rf);
3988 		WARN_ON_ONCE(scx_get_task_state(p) >= SCX_TASK_READY);
3989 		scx_exit_task(p);
3990 		task_rq_unlock(rq, p, &rf);
3991 	}
3992 
3993 	percpu_up_read(&scx_fork_rwsem);
3994 }
3995 
3996 void sched_ext_free(struct task_struct *p)
3997 {
3998 	unsigned long flags;
3999 
4000 	spin_lock_irqsave(&scx_tasks_lock, flags);
4001 	list_del_init(&p->scx.tasks_node);
4002 	spin_unlock_irqrestore(&scx_tasks_lock, flags);
4003 
4004 	/*
4005 	 * @p is off scx_tasks and wholly ours. scx_enable()'s READY -> ENABLED
4006 	 * transitions can't race us. Disable ops for @p.
4007 	 */
4008 	if (scx_get_task_state(p) != SCX_TASK_NONE) {
4009 		struct rq_flags rf;
4010 		struct rq *rq;
4011 
4012 		rq = task_rq_lock(p, &rf);
4013 		scx_exit_task(p);
4014 		task_rq_unlock(rq, p, &rf);
4015 	}
4016 }
4017 
4018 static void reweight_task_scx(struct rq *rq, struct task_struct *p,
4019 			      const struct load_weight *lw)
4020 {
4021 	struct scx_sched *sch = scx_root;
4022 
4023 	lockdep_assert_rq_held(task_rq(p));
4024 
4025 	p->scx.weight = sched_weight_to_cgroup(scale_load_down(lw->weight));
4026 	if (SCX_HAS_OP(sch, set_weight))
4027 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, set_weight, rq,
4028 				 p, p->scx.weight);
4029 }
4030 
4031 static void prio_changed_scx(struct rq *rq, struct task_struct *p, int oldprio)
4032 {
4033 }
4034 
4035 static void switching_to_scx(struct rq *rq, struct task_struct *p)
4036 {
4037 	struct scx_sched *sch = scx_root;
4038 
4039 	scx_enable_task(p);
4040 
4041 	/*
4042 	 * set_cpus_allowed_scx() is not called while @p is associated with a
4043 	 * different scheduler class. Keep the BPF scheduler up-to-date.
4044 	 */
4045 	if (SCX_HAS_OP(sch, set_cpumask))
4046 		SCX_CALL_OP_TASK(sch, SCX_KF_REST, set_cpumask, rq,
4047 				 p, (struct cpumask *)p->cpus_ptr);
4048 }
4049 
4050 static void switched_from_scx(struct rq *rq, struct task_struct *p)
4051 {
4052 	scx_disable_task(p);
4053 }
4054 
4055 static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p,int wake_flags) {}
4056 static void switched_to_scx(struct rq *rq, struct task_struct *p) {}
4057 
4058 int scx_check_setscheduler(struct task_struct *p, int policy)
4059 {
4060 	lockdep_assert_rq_held(task_rq(p));
4061 
4062 	/* if disallow, reject transitioning into SCX */
4063 	if (scx_enabled() && READ_ONCE(p->scx.disallow) &&
4064 	    p->policy != policy && policy == SCHED_EXT)
4065 		return -EACCES;
4066 
4067 	return 0;
4068 }
4069 
4070 #ifdef CONFIG_NO_HZ_FULL
4071 bool scx_can_stop_tick(struct rq *rq)
4072 {
4073 	struct task_struct *p = rq->curr;
4074 
4075 	if (scx_rq_bypassing(rq))
4076 		return false;
4077 
4078 	if (p->sched_class != &ext_sched_class)
4079 		return true;
4080 
4081 	/*
4082 	 * @rq can dispatch from different DSQs, so we can't tell whether it
4083 	 * needs the tick or not by looking at nr_running. Allow stopping ticks
4084 	 * iff the BPF scheduler indicated so. See set_next_task_scx().
4085 	 */
4086 	return rq->scx.flags & SCX_RQ_CAN_STOP_TICK;
4087 }
4088 #endif
4089 
4090 #ifdef CONFIG_EXT_GROUP_SCHED
4091 
4092 DEFINE_STATIC_PERCPU_RWSEM(scx_cgroup_rwsem);
4093 static bool scx_cgroup_enabled;
4094 
4095 int scx_tg_online(struct task_group *tg)
4096 {
4097 	struct scx_sched *sch = scx_root;
4098 	int ret = 0;
4099 
4100 	WARN_ON_ONCE(tg->scx_flags & (SCX_TG_ONLINE | SCX_TG_INITED));
4101 
4102 	percpu_down_read(&scx_cgroup_rwsem);
4103 
4104 	if (scx_cgroup_enabled) {
4105 		if (SCX_HAS_OP(sch, cgroup_init)) {
4106 			struct scx_cgroup_init_args args =
4107 				{ .weight = tg->scx_weight };
4108 
4109 			ret = SCX_CALL_OP_RET(sch, SCX_KF_UNLOCKED, cgroup_init,
4110 					      NULL, tg->css.cgroup, &args);
4111 			if (ret)
4112 				ret = ops_sanitize_err(sch, "cgroup_init", ret);
4113 		}
4114 		if (ret == 0)
4115 			tg->scx_flags |= SCX_TG_ONLINE | SCX_TG_INITED;
4116 	} else {
4117 		tg->scx_flags |= SCX_TG_ONLINE;
4118 	}
4119 
4120 	percpu_up_read(&scx_cgroup_rwsem);
4121 	return ret;
4122 }
4123 
4124 void scx_tg_offline(struct task_group *tg)
4125 {
4126 	struct scx_sched *sch = scx_root;
4127 
4128 	WARN_ON_ONCE(!(tg->scx_flags & SCX_TG_ONLINE));
4129 
4130 	percpu_down_read(&scx_cgroup_rwsem);
4131 
4132 	if (scx_cgroup_enabled && SCX_HAS_OP(sch, cgroup_exit) &&
4133 	    (tg->scx_flags & SCX_TG_INITED))
4134 		SCX_CALL_OP(sch, SCX_KF_UNLOCKED, cgroup_exit, NULL,
4135 			    tg->css.cgroup);
4136 	tg->scx_flags &= ~(SCX_TG_ONLINE | SCX_TG_INITED);
4137 
4138 	percpu_up_read(&scx_cgroup_rwsem);
4139 }
4140 
4141 int scx_cgroup_can_attach(struct cgroup_taskset *tset)
4142 {
4143 	struct scx_sched *sch = scx_root;
4144 	struct cgroup_subsys_state *css;
4145 	struct task_struct *p;
4146 	int ret;
4147 
4148 	/* released in scx_finish/cancel_attach() */
4149 	percpu_down_read(&scx_cgroup_rwsem);
4150 
4151 	if (!scx_cgroup_enabled)
4152 		return 0;
4153 
4154 	cgroup_taskset_for_each(p, css, tset) {
4155 		struct cgroup *from = tg_cgrp(task_group(p));
4156 		struct cgroup *to = tg_cgrp(css_tg(css));
4157 
4158 		WARN_ON_ONCE(p->scx.cgrp_moving_from);
4159 
4160 		/*
4161 		 * sched_move_task() omits identity migrations. Let's match the
4162 		 * behavior so that ops.cgroup_prep_move() and ops.cgroup_move()
4163 		 * always match one-to-one.
4164 		 */
4165 		if (from == to)
4166 			continue;
4167 
4168 		if (SCX_HAS_OP(sch, cgroup_prep_move)) {
4169 			ret = SCX_CALL_OP_RET(sch, SCX_KF_UNLOCKED,
4170 					      cgroup_prep_move, NULL,
4171 					      p, from, css->cgroup);
4172 			if (ret)
4173 				goto err;
4174 		}
4175 
4176 		p->scx.cgrp_moving_from = from;
4177 	}
4178 
4179 	return 0;
4180 
4181 err:
4182 	cgroup_taskset_for_each(p, css, tset) {
4183 		if (SCX_HAS_OP(sch, cgroup_cancel_move) &&
4184 		    p->scx.cgrp_moving_from)
4185 			SCX_CALL_OP(sch, SCX_KF_UNLOCKED, cgroup_cancel_move, NULL,
4186 				    p, p->scx.cgrp_moving_from, css->cgroup);
4187 		p->scx.cgrp_moving_from = NULL;
4188 	}
4189 
4190 	percpu_up_read(&scx_cgroup_rwsem);
4191 	return ops_sanitize_err(sch, "cgroup_prep_move", ret);
4192 }
4193 
4194 void scx_cgroup_move_task(struct task_struct *p)
4195 {
4196 	struct scx_sched *sch = scx_root;
4197 
4198 	if (!scx_cgroup_enabled)
4199 		return;
4200 
4201 	/*
4202 	 * @p must have ops.cgroup_prep_move() called on it and thus
4203 	 * cgrp_moving_from set.
4204 	 */
4205 	if (SCX_HAS_OP(sch, cgroup_move) &&
4206 	    !WARN_ON_ONCE(!p->scx.cgrp_moving_from))
4207 		SCX_CALL_OP_TASK(sch, SCX_KF_UNLOCKED, cgroup_move, NULL,
4208 				 p, p->scx.cgrp_moving_from,
4209 				 tg_cgrp(task_group(p)));
4210 	p->scx.cgrp_moving_from = NULL;
4211 }
4212 
4213 void scx_cgroup_finish_attach(void)
4214 {
4215 	percpu_up_read(&scx_cgroup_rwsem);
4216 }
4217 
4218 void scx_cgroup_cancel_attach(struct cgroup_taskset *tset)
4219 {
4220 	struct scx_sched *sch = scx_root;
4221 	struct cgroup_subsys_state *css;
4222 	struct task_struct *p;
4223 
4224 	if (!scx_cgroup_enabled)
4225 		goto out_unlock;
4226 
4227 	cgroup_taskset_for_each(p, css, tset) {
4228 		if (SCX_HAS_OP(sch, cgroup_cancel_move) &&
4229 		    p->scx.cgrp_moving_from)
4230 			SCX_CALL_OP(sch, SCX_KF_UNLOCKED, cgroup_cancel_move, NULL,
4231 				    p, p->scx.cgrp_moving_from, css->cgroup);
4232 		p->scx.cgrp_moving_from = NULL;
4233 	}
4234 out_unlock:
4235 	percpu_up_read(&scx_cgroup_rwsem);
4236 }
4237 
4238 void scx_group_set_weight(struct task_group *tg, unsigned long weight)
4239 {
4240 	struct scx_sched *sch = scx_root;
4241 
4242 	percpu_down_read(&scx_cgroup_rwsem);
4243 
4244 	if (scx_cgroup_enabled && tg->scx_weight != weight) {
4245 		if (SCX_HAS_OP(sch, cgroup_set_weight))
4246 			SCX_CALL_OP(sch, SCX_KF_UNLOCKED, cgroup_set_weight, NULL,
4247 				    tg_cgrp(tg), weight);
4248 		tg->scx_weight = weight;
4249 	}
4250 
4251 	percpu_up_read(&scx_cgroup_rwsem);
4252 }
4253 
4254 void scx_group_set_idle(struct task_group *tg, bool idle)
4255 {
4256 	/* TODO: Implement ops->cgroup_set_idle() */
4257 }
4258 
4259 static void scx_cgroup_lock(void)
4260 {
4261 	percpu_down_write(&scx_cgroup_rwsem);
4262 }
4263 
4264 static void scx_cgroup_unlock(void)
4265 {
4266 	percpu_up_write(&scx_cgroup_rwsem);
4267 }
4268 
4269 #else	/* CONFIG_EXT_GROUP_SCHED */
4270 
4271 static inline void scx_cgroup_lock(void) {}
4272 static inline void scx_cgroup_unlock(void) {}
4273 
4274 #endif	/* CONFIG_EXT_GROUP_SCHED */
4275 
4276 /*
4277  * Omitted operations:
4278  *
4279  * - wakeup_preempt: NOOP as it isn't useful in the wakeup path because the task
4280  *   isn't tied to the CPU at that point. Preemption is implemented by resetting
4281  *   the victim task's slice to 0 and triggering reschedule on the target CPU.
4282  *
4283  * - migrate_task_rq: Unnecessary as task to cpu mapping is transient.
4284  *
4285  * - task_fork/dead: We need fork/dead notifications for all tasks regardless of
4286  *   their current sched_class. Call them directly from sched core instead.
4287  */
4288 DEFINE_SCHED_CLASS(ext) = {
4289 	.enqueue_task		= enqueue_task_scx,
4290 	.dequeue_task		= dequeue_task_scx,
4291 	.yield_task		= yield_task_scx,
4292 	.yield_to_task		= yield_to_task_scx,
4293 
4294 	.wakeup_preempt		= wakeup_preempt_scx,
4295 
4296 	.balance		= balance_scx,
4297 	.pick_task		= pick_task_scx,
4298 
4299 	.put_prev_task		= put_prev_task_scx,
4300 	.set_next_task		= set_next_task_scx,
4301 
4302 #ifdef CONFIG_SMP
4303 	.select_task_rq		= select_task_rq_scx,
4304 	.task_woken		= task_woken_scx,
4305 	.set_cpus_allowed	= set_cpus_allowed_scx,
4306 
4307 	.rq_online		= rq_online_scx,
4308 	.rq_offline		= rq_offline_scx,
4309 #endif
4310 
4311 	.task_tick		= task_tick_scx,
4312 
4313 	.switching_to		= switching_to_scx,
4314 	.switched_from		= switched_from_scx,
4315 	.switched_to		= switched_to_scx,
4316 	.reweight_task		= reweight_task_scx,
4317 	.prio_changed		= prio_changed_scx,
4318 
4319 	.update_curr		= update_curr_scx,
4320 
4321 #ifdef CONFIG_UCLAMP_TASK
4322 	.uclamp_enabled		= 1,
4323 #endif
4324 };
4325 
4326 static void init_dsq(struct scx_dispatch_q *dsq, u64 dsq_id)
4327 {
4328 	memset(dsq, 0, sizeof(*dsq));
4329 
4330 	raw_spin_lock_init(&dsq->lock);
4331 	INIT_LIST_HEAD(&dsq->list);
4332 	dsq->id = dsq_id;
4333 }
4334 
4335 static void free_dsq_irq_workfn(struct irq_work *irq_work)
4336 {
4337 	struct llist_node *to_free = llist_del_all(&dsqs_to_free);
4338 	struct scx_dispatch_q *dsq, *tmp_dsq;
4339 
4340 	llist_for_each_entry_safe(dsq, tmp_dsq, to_free, free_node)
4341 		kfree_rcu(dsq, rcu);
4342 }
4343 
4344 static DEFINE_IRQ_WORK(free_dsq_irq_work, free_dsq_irq_workfn);
4345 
4346 static void destroy_dsq(struct scx_sched *sch, u64 dsq_id)
4347 {
4348 	struct scx_dispatch_q *dsq;
4349 	unsigned long flags;
4350 
4351 	rcu_read_lock();
4352 
4353 	dsq = find_user_dsq(sch, dsq_id);
4354 	if (!dsq)
4355 		goto out_unlock_rcu;
4356 
4357 	raw_spin_lock_irqsave(&dsq->lock, flags);
4358 
4359 	if (dsq->nr) {
4360 		scx_error(sch, "attempting to destroy in-use dsq 0x%016llx (nr=%u)",
4361 			  dsq->id, dsq->nr);
4362 		goto out_unlock_dsq;
4363 	}
4364 
4365 	if (rhashtable_remove_fast(&sch->dsq_hash, &dsq->hash_node,
4366 				   dsq_hash_params))
4367 		goto out_unlock_dsq;
4368 
4369 	/*
4370 	 * Mark dead by invalidating ->id to prevent dispatch_enqueue() from
4371 	 * queueing more tasks. As this function can be called from anywhere,
4372 	 * freeing is bounced through an irq work to avoid nesting RCU
4373 	 * operations inside scheduler locks.
4374 	 */
4375 	dsq->id = SCX_DSQ_INVALID;
4376 	llist_add(&dsq->free_node, &dsqs_to_free);
4377 	irq_work_queue(&free_dsq_irq_work);
4378 
4379 out_unlock_dsq:
4380 	raw_spin_unlock_irqrestore(&dsq->lock, flags);
4381 out_unlock_rcu:
4382 	rcu_read_unlock();
4383 }
4384 
4385 #ifdef CONFIG_EXT_GROUP_SCHED
4386 static void scx_cgroup_exit(struct scx_sched *sch)
4387 {
4388 	struct cgroup_subsys_state *css;
4389 
4390 	percpu_rwsem_assert_held(&scx_cgroup_rwsem);
4391 
4392 	scx_cgroup_enabled = false;
4393 
4394 	/*
4395 	 * scx_tg_on/offline() are excluded through scx_cgroup_rwsem. If we walk
4396 	 * cgroups and exit all the inited ones, all online cgroups are exited.
4397 	 */
4398 	rcu_read_lock();
4399 	css_for_each_descendant_post(css, &root_task_group.css) {
4400 		struct task_group *tg = css_tg(css);
4401 
4402 		if (!(tg->scx_flags & SCX_TG_INITED))
4403 			continue;
4404 		tg->scx_flags &= ~SCX_TG_INITED;
4405 
4406 		if (!sch->ops.cgroup_exit)
4407 			continue;
4408 
4409 		if (WARN_ON_ONCE(!css_tryget(css)))
4410 			continue;
4411 		rcu_read_unlock();
4412 
4413 		SCX_CALL_OP(sch, SCX_KF_UNLOCKED, cgroup_exit, NULL,
4414 			    css->cgroup);
4415 
4416 		rcu_read_lock();
4417 		css_put(css);
4418 	}
4419 	rcu_read_unlock();
4420 }
4421 
4422 static int scx_cgroup_init(struct scx_sched *sch)
4423 {
4424 	struct cgroup_subsys_state *css;
4425 	int ret;
4426 
4427 	percpu_rwsem_assert_held(&scx_cgroup_rwsem);
4428 
4429 	/*
4430 	 * scx_tg_on/offline() are excluded through scx_cgroup_rwsem. If we walk
4431 	 * cgroups and init, all online cgroups are initialized.
4432 	 */
4433 	rcu_read_lock();
4434 	css_for_each_descendant_pre(css, &root_task_group.css) {
4435 		struct task_group *tg = css_tg(css);
4436 		struct scx_cgroup_init_args args = { .weight = tg->scx_weight };
4437 
4438 		if ((tg->scx_flags &
4439 		     (SCX_TG_ONLINE | SCX_TG_INITED)) != SCX_TG_ONLINE)
4440 			continue;
4441 
4442 		if (!sch->ops.cgroup_init) {
4443 			tg->scx_flags |= SCX_TG_INITED;
4444 			continue;
4445 		}
4446 
4447 		if (WARN_ON_ONCE(!css_tryget(css)))
4448 			continue;
4449 		rcu_read_unlock();
4450 
4451 		ret = SCX_CALL_OP_RET(sch, SCX_KF_UNLOCKED, cgroup_init, NULL,
4452 				      css->cgroup, &args);
4453 		if (ret) {
4454 			css_put(css);
4455 			scx_error(sch, "ops.cgroup_init() failed (%d)", ret);
4456 			return ret;
4457 		}
4458 		tg->scx_flags |= SCX_TG_INITED;
4459 
4460 		rcu_read_lock();
4461 		css_put(css);
4462 	}
4463 	rcu_read_unlock();
4464 
4465 	WARN_ON_ONCE(scx_cgroup_enabled);
4466 	scx_cgroup_enabled = true;
4467 
4468 	return 0;
4469 }
4470 
4471 #else
4472 static void scx_cgroup_exit(struct scx_sched *sch) {}
4473 static int scx_cgroup_init(struct scx_sched *sch) { return 0; }
4474 #endif
4475 
4476 
4477 /********************************************************************************
4478  * Sysfs interface and ops enable/disable.
4479  */
4480 
4481 #define SCX_ATTR(_name)								\
4482 	static struct kobj_attribute scx_attr_##_name = {			\
4483 		.attr = { .name = __stringify(_name), .mode = 0444 },		\
4484 		.show = scx_attr_##_name##_show,				\
4485 	}
4486 
4487 static ssize_t scx_attr_state_show(struct kobject *kobj,
4488 				   struct kobj_attribute *ka, char *buf)
4489 {
4490 	return sysfs_emit(buf, "%s\n", scx_enable_state_str[scx_enable_state()]);
4491 }
4492 SCX_ATTR(state);
4493 
4494 static ssize_t scx_attr_switch_all_show(struct kobject *kobj,
4495 					struct kobj_attribute *ka, char *buf)
4496 {
4497 	return sysfs_emit(buf, "%d\n", READ_ONCE(scx_switching_all));
4498 }
4499 SCX_ATTR(switch_all);
4500 
4501 static ssize_t scx_attr_nr_rejected_show(struct kobject *kobj,
4502 					 struct kobj_attribute *ka, char *buf)
4503 {
4504 	return sysfs_emit(buf, "%ld\n", atomic_long_read(&scx_nr_rejected));
4505 }
4506 SCX_ATTR(nr_rejected);
4507 
4508 static ssize_t scx_attr_hotplug_seq_show(struct kobject *kobj,
4509 					 struct kobj_attribute *ka, char *buf)
4510 {
4511 	return sysfs_emit(buf, "%ld\n", atomic_long_read(&scx_hotplug_seq));
4512 }
4513 SCX_ATTR(hotplug_seq);
4514 
4515 static ssize_t scx_attr_enable_seq_show(struct kobject *kobj,
4516 					struct kobj_attribute *ka, char *buf)
4517 {
4518 	return sysfs_emit(buf, "%ld\n", atomic_long_read(&scx_enable_seq));
4519 }
4520 SCX_ATTR(enable_seq);
4521 
4522 static struct attribute *scx_global_attrs[] = {
4523 	&scx_attr_state.attr,
4524 	&scx_attr_switch_all.attr,
4525 	&scx_attr_nr_rejected.attr,
4526 	&scx_attr_hotplug_seq.attr,
4527 	&scx_attr_enable_seq.attr,
4528 	NULL,
4529 };
4530 
4531 static const struct attribute_group scx_global_attr_group = {
4532 	.attrs = scx_global_attrs,
4533 };
4534 
4535 static void free_exit_info(struct scx_exit_info *ei);
4536 
4537 static void scx_sched_free_rcu_work(struct work_struct *work)
4538 {
4539 	struct rcu_work *rcu_work = to_rcu_work(work);
4540 	struct scx_sched *sch = container_of(rcu_work, struct scx_sched, rcu_work);
4541 	struct rhashtable_iter rht_iter;
4542 	struct scx_dispatch_q *dsq;
4543 	int node;
4544 
4545 	kthread_stop(sch->helper->task);
4546 	free_percpu(sch->event_stats_cpu);
4547 
4548 	for_each_node_state(node, N_POSSIBLE)
4549 		kfree(sch->global_dsqs[node]);
4550 	kfree(sch->global_dsqs);
4551 
4552 	rhashtable_walk_enter(&sch->dsq_hash, &rht_iter);
4553 	do {
4554 		rhashtable_walk_start(&rht_iter);
4555 
4556 		while ((dsq = rhashtable_walk_next(&rht_iter)) && !IS_ERR(dsq))
4557 			destroy_dsq(sch, dsq->id);
4558 
4559 		rhashtable_walk_stop(&rht_iter);
4560 	} while (dsq == ERR_PTR(-EAGAIN));
4561 	rhashtable_walk_exit(&rht_iter);
4562 
4563 	rhashtable_free_and_destroy(&sch->dsq_hash, NULL, NULL);
4564 	free_exit_info(sch->exit_info);
4565 	kfree(sch);
4566 }
4567 
4568 static void scx_kobj_release(struct kobject *kobj)
4569 {
4570 	struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);
4571 
4572 	INIT_RCU_WORK(&sch->rcu_work, scx_sched_free_rcu_work);
4573 	queue_rcu_work(system_unbound_wq, &sch->rcu_work);
4574 }
4575 
4576 static ssize_t scx_attr_ops_show(struct kobject *kobj,
4577 				 struct kobj_attribute *ka, char *buf)
4578 {
4579 	return sysfs_emit(buf, "%s\n", scx_root->ops.name);
4580 }
4581 SCX_ATTR(ops);
4582 
4583 #define scx_attr_event_show(buf, at, events, kind) ({				\
4584 	sysfs_emit_at(buf, at, "%s %llu\n", #kind, (events)->kind);		\
4585 })
4586 
4587 static ssize_t scx_attr_events_show(struct kobject *kobj,
4588 				    struct kobj_attribute *ka, char *buf)
4589 {
4590 	struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);
4591 	struct scx_event_stats events;
4592 	int at = 0;
4593 
4594 	scx_read_events(sch, &events);
4595 	at += scx_attr_event_show(buf, at, &events, SCX_EV_SELECT_CPU_FALLBACK);
4596 	at += scx_attr_event_show(buf, at, &events, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE);
4597 	at += scx_attr_event_show(buf, at, &events, SCX_EV_DISPATCH_KEEP_LAST);
4598 	at += scx_attr_event_show(buf, at, &events, SCX_EV_ENQ_SKIP_EXITING);
4599 	at += scx_attr_event_show(buf, at, &events, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED);
4600 	at += scx_attr_event_show(buf, at, &events, SCX_EV_REFILL_SLICE_DFL);
4601 	at += scx_attr_event_show(buf, at, &events, SCX_EV_BYPASS_DURATION);
4602 	at += scx_attr_event_show(buf, at, &events, SCX_EV_BYPASS_DISPATCH);
4603 	at += scx_attr_event_show(buf, at, &events, SCX_EV_BYPASS_ACTIVATE);
4604 	return at;
4605 }
4606 SCX_ATTR(events);
4607 
4608 static struct attribute *scx_sched_attrs[] = {
4609 	&scx_attr_ops.attr,
4610 	&scx_attr_events.attr,
4611 	NULL,
4612 };
4613 ATTRIBUTE_GROUPS(scx_sched);
4614 
4615 static const struct kobj_type scx_ktype = {
4616 	.release = scx_kobj_release,
4617 	.sysfs_ops = &kobj_sysfs_ops,
4618 	.default_groups = scx_sched_groups,
4619 };
4620 
4621 static int scx_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
4622 {
4623 	return add_uevent_var(env, "SCXOPS=%s", scx_root->ops.name);
4624 }
4625 
4626 static const struct kset_uevent_ops scx_uevent_ops = {
4627 	.uevent = scx_uevent,
4628 };
4629 
4630 /*
4631  * Used by sched_fork() and __setscheduler_prio() to pick the matching
4632  * sched_class. dl/rt are already handled.
4633  */
4634 bool task_should_scx(int policy)
4635 {
4636 	if (!scx_enabled() || unlikely(scx_enable_state() == SCX_DISABLING))
4637 		return false;
4638 	if (READ_ONCE(scx_switching_all))
4639 		return true;
4640 	return policy == SCHED_EXT;
4641 }
4642 
4643 bool scx_allow_ttwu_queue(const struct task_struct *p)
4644 {
4645 	return !scx_enabled() ||
4646 		(scx_root->ops.flags & SCX_OPS_ALLOW_QUEUED_WAKEUP) ||
4647 		p->sched_class != &ext_sched_class;
4648 }
4649 
4650 /**
4651  * scx_softlockup - sched_ext softlockup handler
4652  * @dur_s: number of seconds of CPU stuck due to soft lockup
4653  *
4654  * On some multi-socket setups (e.g. 2x Intel 8480c), the BPF scheduler can
4655  * live-lock the system by making many CPUs target the same DSQ to the point
4656  * where soft-lockup detection triggers. This function is called from
4657  * soft-lockup watchdog when the triggering point is close and tries to unjam
4658  * the system by enabling the breather and aborting the BPF scheduler.
4659  */
4660 void scx_softlockup(u32 dur_s)
4661 {
4662 	struct scx_sched *sch;
4663 
4664 	rcu_read_lock();
4665 
4666 	sch = rcu_dereference(scx_root);
4667 	if (unlikely(!sch))
4668 		goto out_unlock;
4669 
4670 	switch (scx_enable_state()) {
4671 	case SCX_ENABLING:
4672 	case SCX_ENABLED:
4673 		break;
4674 	default:
4675 		goto out_unlock;
4676 	}
4677 
4678 	/* allow only one instance, cleared at the end of scx_bypass() */
4679 	if (test_and_set_bit(0, &scx_in_softlockup))
4680 		goto out_unlock;
4681 
4682 	printk_deferred(KERN_ERR "sched_ext: Soft lockup - CPU%d stuck for %us, disabling \"%s\"\n",
4683 			smp_processor_id(), dur_s, scx_root->ops.name);
4684 
4685 	/*
4686 	 * Some CPUs may be trapped in the dispatch paths. Enable breather
4687 	 * immediately; otherwise, we might even be able to get to scx_bypass().
4688 	 */
4689 	atomic_inc(&scx_breather_depth);
4690 
4691 	scx_error(sch, "soft lockup - CPU#%d stuck for %us", smp_processor_id(), dur_s);
4692 out_unlock:
4693 	rcu_read_unlock();
4694 }
4695 
4696 static void scx_clear_softlockup(void)
4697 {
4698 	if (test_and_clear_bit(0, &scx_in_softlockup))
4699 		atomic_dec(&scx_breather_depth);
4700 }
4701 
4702 /**
4703  * scx_bypass - [Un]bypass scx_ops and guarantee forward progress
4704  * @bypass: true for bypass, false for unbypass
4705  *
4706  * Bypassing guarantees that all runnable tasks make forward progress without
4707  * trusting the BPF scheduler. We can't grab any mutexes or rwsems as they might
4708  * be held by tasks that the BPF scheduler is forgetting to run, which
4709  * unfortunately also excludes toggling the static branches.
4710  *
4711  * Let's work around by overriding a couple ops and modifying behaviors based on
4712  * the DISABLING state and then cycling the queued tasks through dequeue/enqueue
4713  * to force global FIFO scheduling.
4714  *
4715  * - ops.select_cpu() is ignored and the default select_cpu() is used.
4716  *
4717  * - ops.enqueue() is ignored and tasks are queued in simple global FIFO order.
4718  *   %SCX_OPS_ENQ_LAST is also ignored.
4719  *
4720  * - ops.dispatch() is ignored.
4721  *
4722  * - balance_scx() does not set %SCX_RQ_BAL_KEEP on non-zero slice as slice
4723  *   can't be trusted. Whenever a tick triggers, the running task is rotated to
4724  *   the tail of the queue with core_sched_at touched.
4725  *
4726  * - pick_next_task() suppresses zero slice warning.
4727  *
4728  * - scx_bpf_kick_cpu() is disabled to avoid irq_work malfunction during PM
4729  *   operations.
4730  *
4731  * - scx_prio_less() reverts to the default core_sched_at order.
4732  */
4733 static void scx_bypass(bool bypass)
4734 {
4735 	static DEFINE_RAW_SPINLOCK(bypass_lock);
4736 	static unsigned long bypass_timestamp;
4737 	struct scx_sched *sch;
4738 	unsigned long flags;
4739 	int cpu;
4740 
4741 	raw_spin_lock_irqsave(&bypass_lock, flags);
4742 	sch = rcu_dereference_bh(scx_root);
4743 
4744 	if (bypass) {
4745 		scx_bypass_depth++;
4746 		WARN_ON_ONCE(scx_bypass_depth <= 0);
4747 		if (scx_bypass_depth != 1)
4748 			goto unlock;
4749 		bypass_timestamp = ktime_get_ns();
4750 		if (sch)
4751 			scx_add_event(sch, SCX_EV_BYPASS_ACTIVATE, 1);
4752 	} else {
4753 		scx_bypass_depth--;
4754 		WARN_ON_ONCE(scx_bypass_depth < 0);
4755 		if (scx_bypass_depth != 0)
4756 			goto unlock;
4757 		if (sch)
4758 			scx_add_event(sch, SCX_EV_BYPASS_DURATION,
4759 				      ktime_get_ns() - bypass_timestamp);
4760 	}
4761 
4762 	atomic_inc(&scx_breather_depth);
4763 
4764 	/*
4765 	 * No task property is changing. We just need to make sure all currently
4766 	 * queued tasks are re-queued according to the new scx_rq_bypassing()
4767 	 * state. As an optimization, walk each rq's runnable_list instead of
4768 	 * the scx_tasks list.
4769 	 *
4770 	 * This function can't trust the scheduler and thus can't use
4771 	 * cpus_read_lock(). Walk all possible CPUs instead of online.
4772 	 */
4773 	for_each_possible_cpu(cpu) {
4774 		struct rq *rq = cpu_rq(cpu);
4775 		struct task_struct *p, *n;
4776 
4777 		raw_spin_rq_lock(rq);
4778 
4779 		if (bypass) {
4780 			WARN_ON_ONCE(rq->scx.flags & SCX_RQ_BYPASSING);
4781 			rq->scx.flags |= SCX_RQ_BYPASSING;
4782 		} else {
4783 			WARN_ON_ONCE(!(rq->scx.flags & SCX_RQ_BYPASSING));
4784 			rq->scx.flags &= ~SCX_RQ_BYPASSING;
4785 		}
4786 
4787 		/*
4788 		 * We need to guarantee that no tasks are on the BPF scheduler
4789 		 * while bypassing. Either we see enabled or the enable path
4790 		 * sees scx_rq_bypassing() before moving tasks to SCX.
4791 		 */
4792 		if (!scx_enabled()) {
4793 			raw_spin_rq_unlock(rq);
4794 			continue;
4795 		}
4796 
4797 		/*
4798 		 * The use of list_for_each_entry_safe_reverse() is required
4799 		 * because each task is going to be removed from and added back
4800 		 * to the runnable_list during iteration. Because they're added
4801 		 * to the tail of the list, safe reverse iteration can still
4802 		 * visit all nodes.
4803 		 */
4804 		list_for_each_entry_safe_reverse(p, n, &rq->scx.runnable_list,
4805 						 scx.runnable_node) {
4806 			struct sched_enq_and_set_ctx ctx;
4807 
4808 			/* cycling deq/enq is enough, see the function comment */
4809 			sched_deq_and_put_task(p, DEQUEUE_SAVE | DEQUEUE_MOVE, &ctx);
4810 			sched_enq_and_set_task(&ctx);
4811 		}
4812 
4813 		/* resched to restore ticks and idle state */
4814 		if (cpu_online(cpu) || cpu == smp_processor_id())
4815 			resched_curr(rq);
4816 
4817 		raw_spin_rq_unlock(rq);
4818 	}
4819 
4820 	atomic_dec(&scx_breather_depth);
4821 unlock:
4822 	raw_spin_unlock_irqrestore(&bypass_lock, flags);
4823 	scx_clear_softlockup();
4824 }
4825 
4826 static void free_exit_info(struct scx_exit_info *ei)
4827 {
4828 	kvfree(ei->dump);
4829 	kfree(ei->msg);
4830 	kfree(ei->bt);
4831 	kfree(ei);
4832 }
4833 
4834 static struct scx_exit_info *alloc_exit_info(size_t exit_dump_len)
4835 {
4836 	struct scx_exit_info *ei;
4837 
4838 	ei = kzalloc(sizeof(*ei), GFP_KERNEL);
4839 	if (!ei)
4840 		return NULL;
4841 
4842 	ei->bt = kcalloc(SCX_EXIT_BT_LEN, sizeof(ei->bt[0]), GFP_KERNEL);
4843 	ei->msg = kzalloc(SCX_EXIT_MSG_LEN, GFP_KERNEL);
4844 	ei->dump = kvzalloc(exit_dump_len, GFP_KERNEL);
4845 
4846 	if (!ei->bt || !ei->msg || !ei->dump) {
4847 		free_exit_info(ei);
4848 		return NULL;
4849 	}
4850 
4851 	return ei;
4852 }
4853 
4854 static const char *scx_exit_reason(enum scx_exit_kind kind)
4855 {
4856 	switch (kind) {
4857 	case SCX_EXIT_UNREG:
4858 		return "unregistered from user space";
4859 	case SCX_EXIT_UNREG_BPF:
4860 		return "unregistered from BPF";
4861 	case SCX_EXIT_UNREG_KERN:
4862 		return "unregistered from the main kernel";
4863 	case SCX_EXIT_SYSRQ:
4864 		return "disabled by sysrq-S";
4865 	case SCX_EXIT_ERROR:
4866 		return "runtime error";
4867 	case SCX_EXIT_ERROR_BPF:
4868 		return "scx_bpf_error";
4869 	case SCX_EXIT_ERROR_STALL:
4870 		return "runnable task stall";
4871 	default:
4872 		return "<UNKNOWN>";
4873 	}
4874 }
4875 
4876 static void scx_disable_workfn(struct kthread_work *work)
4877 {
4878 	struct scx_sched *sch = container_of(work, struct scx_sched, disable_work);
4879 	struct scx_exit_info *ei = sch->exit_info;
4880 	struct scx_task_iter sti;
4881 	struct task_struct *p;
4882 	int kind, cpu;
4883 
4884 	kind = atomic_read(&sch->exit_kind);
4885 	while (true) {
4886 		if (kind == SCX_EXIT_DONE)	/* already disabled? */
4887 			return;
4888 		WARN_ON_ONCE(kind == SCX_EXIT_NONE);
4889 		if (atomic_try_cmpxchg(&sch->exit_kind, &kind, SCX_EXIT_DONE))
4890 			break;
4891 	}
4892 	ei->kind = kind;
4893 	ei->reason = scx_exit_reason(ei->kind);
4894 
4895 	/* guarantee forward progress by bypassing scx_ops */
4896 	scx_bypass(true);
4897 
4898 	switch (scx_set_enable_state(SCX_DISABLING)) {
4899 	case SCX_DISABLING:
4900 		WARN_ONCE(true, "sched_ext: duplicate disabling instance?");
4901 		break;
4902 	case SCX_DISABLED:
4903 		pr_warn("sched_ext: ops error detected without ops (%s)\n",
4904 			sch->exit_info->msg);
4905 		WARN_ON_ONCE(scx_set_enable_state(SCX_DISABLED) != SCX_DISABLING);
4906 		goto done;
4907 	default:
4908 		break;
4909 	}
4910 
4911 	/*
4912 	 * Here, every runnable task is guaranteed to make forward progress and
4913 	 * we can safely use blocking synchronization constructs. Actually
4914 	 * disable ops.
4915 	 */
4916 	mutex_lock(&scx_enable_mutex);
4917 
4918 	static_branch_disable(&__scx_switched_all);
4919 	WRITE_ONCE(scx_switching_all, false);
4920 
4921 	/*
4922 	 * Shut down cgroup support before tasks so that the cgroup attach path
4923 	 * doesn't race against scx_exit_task().
4924 	 */
4925 	scx_cgroup_lock();
4926 	scx_cgroup_exit(sch);
4927 	scx_cgroup_unlock();
4928 
4929 	/*
4930 	 * The BPF scheduler is going away. All tasks including %TASK_DEAD ones
4931 	 * must be switched out and exited synchronously.
4932 	 */
4933 	percpu_down_write(&scx_fork_rwsem);
4934 
4935 	scx_init_task_enabled = false;
4936 
4937 	scx_task_iter_start(&sti);
4938 	while ((p = scx_task_iter_next_locked(&sti))) {
4939 		const struct sched_class *old_class = p->sched_class;
4940 		const struct sched_class *new_class =
4941 			__setscheduler_class(p->policy, p->prio);
4942 		struct sched_enq_and_set_ctx ctx;
4943 
4944 		if (old_class != new_class && p->se.sched_delayed)
4945 			dequeue_task(task_rq(p), p, DEQUEUE_SLEEP | DEQUEUE_DELAYED);
4946 
4947 		sched_deq_and_put_task(p, DEQUEUE_SAVE | DEQUEUE_MOVE, &ctx);
4948 
4949 		p->sched_class = new_class;
4950 		check_class_changing(task_rq(p), p, old_class);
4951 
4952 		sched_enq_and_set_task(&ctx);
4953 
4954 		check_class_changed(task_rq(p), p, old_class, p->prio);
4955 		scx_exit_task(p);
4956 	}
4957 	scx_task_iter_stop(&sti);
4958 	percpu_up_write(&scx_fork_rwsem);
4959 
4960 	/*
4961 	 * Invalidate all the rq clocks to prevent getting outdated
4962 	 * rq clocks from a previous scx scheduler.
4963 	 */
4964 	for_each_possible_cpu(cpu) {
4965 		struct rq *rq = cpu_rq(cpu);
4966 		scx_rq_clock_invalidate(rq);
4967 	}
4968 
4969 	/* no task is on scx, turn off all the switches and flush in-progress calls */
4970 	static_branch_disable(&__scx_enabled);
4971 	bitmap_zero(sch->has_op, SCX_OPI_END);
4972 	scx_idle_disable();
4973 	synchronize_rcu();
4974 
4975 	if (ei->kind >= SCX_EXIT_ERROR) {
4976 		pr_err("sched_ext: BPF scheduler \"%s\" disabled (%s)\n",
4977 		       sch->ops.name, ei->reason);
4978 
4979 		if (ei->msg[0] != '\0')
4980 			pr_err("sched_ext: %s: %s\n", sch->ops.name, ei->msg);
4981 #ifdef CONFIG_STACKTRACE
4982 		stack_trace_print(ei->bt, ei->bt_len, 2);
4983 #endif
4984 	} else {
4985 		pr_info("sched_ext: BPF scheduler \"%s\" disabled (%s)\n",
4986 			sch->ops.name, ei->reason);
4987 	}
4988 
4989 	if (sch->ops.exit)
4990 		SCX_CALL_OP(sch, SCX_KF_UNLOCKED, exit, NULL, ei);
4991 
4992 	cancel_delayed_work_sync(&scx_watchdog_work);
4993 
4994 	/*
4995 	 * scx_root clearing must be inside cpus_read_lock(). See
4996 	 * handle_hotplug().
4997 	 */
4998 	cpus_read_lock();
4999 	RCU_INIT_POINTER(scx_root, NULL);
5000 	cpus_read_unlock();
5001 
5002 	/*
5003 	 * Delete the kobject from the hierarchy synchronously. Otherwise, sysfs
5004 	 * could observe an object of the same name still in the hierarchy when
5005 	 * the next scheduler is loaded.
5006 	 */
5007 	kobject_del(&sch->kobj);
5008 
5009 	free_percpu(scx_dsp_ctx);
5010 	scx_dsp_ctx = NULL;
5011 	scx_dsp_max_batch = 0;
5012 
5013 	mutex_unlock(&scx_enable_mutex);
5014 
5015 	WARN_ON_ONCE(scx_set_enable_state(SCX_DISABLED) != SCX_DISABLING);
5016 done:
5017 	scx_bypass(false);
5018 }
5019 
5020 static void scx_disable(enum scx_exit_kind kind)
5021 {
5022 	int none = SCX_EXIT_NONE;
5023 	struct scx_sched *sch;
5024 
5025 	if (WARN_ON_ONCE(kind == SCX_EXIT_NONE || kind == SCX_EXIT_DONE))
5026 		kind = SCX_EXIT_ERROR;
5027 
5028 	rcu_read_lock();
5029 	sch = rcu_dereference(scx_root);
5030 	if (sch) {
5031 		atomic_try_cmpxchg(&sch->exit_kind, &none, kind);
5032 		kthread_queue_work(sch->helper, &sch->disable_work);
5033 	}
5034 	rcu_read_unlock();
5035 }
5036 
5037 static void dump_newline(struct seq_buf *s)
5038 {
5039 	trace_sched_ext_dump("");
5040 
5041 	/* @s may be zero sized and seq_buf triggers WARN if so */
5042 	if (s->size)
5043 		seq_buf_putc(s, '\n');
5044 }
5045 
5046 static __printf(2, 3) void dump_line(struct seq_buf *s, const char *fmt, ...)
5047 {
5048 	va_list args;
5049 
5050 #ifdef CONFIG_TRACEPOINTS
5051 	if (trace_sched_ext_dump_enabled()) {
5052 		/* protected by scx_dump_state()::dump_lock */
5053 		static char line_buf[SCX_EXIT_MSG_LEN];
5054 
5055 		va_start(args, fmt);
5056 		vscnprintf(line_buf, sizeof(line_buf), fmt, args);
5057 		va_end(args);
5058 
5059 		trace_sched_ext_dump(line_buf);
5060 	}
5061 #endif
5062 	/* @s may be zero sized and seq_buf triggers WARN if so */
5063 	if (s->size) {
5064 		va_start(args, fmt);
5065 		seq_buf_vprintf(s, fmt, args);
5066 		va_end(args);
5067 
5068 		seq_buf_putc(s, '\n');
5069 	}
5070 }
5071 
5072 static void dump_stack_trace(struct seq_buf *s, const char *prefix,
5073 			     const unsigned long *bt, unsigned int len)
5074 {
5075 	unsigned int i;
5076 
5077 	for (i = 0; i < len; i++)
5078 		dump_line(s, "%s%pS", prefix, (void *)bt[i]);
5079 }
5080 
5081 static void ops_dump_init(struct seq_buf *s, const char *prefix)
5082 {
5083 	struct scx_dump_data *dd = &scx_dump_data;
5084 
5085 	lockdep_assert_irqs_disabled();
5086 
5087 	dd->cpu = smp_processor_id();		/* allow scx_bpf_dump() */
5088 	dd->first = true;
5089 	dd->cursor = 0;
5090 	dd->s = s;
5091 	dd->prefix = prefix;
5092 }
5093 
5094 static void ops_dump_flush(void)
5095 {
5096 	struct scx_dump_data *dd = &scx_dump_data;
5097 	char *line = dd->buf.line;
5098 
5099 	if (!dd->cursor)
5100 		return;
5101 
5102 	/*
5103 	 * There's something to flush and this is the first line. Insert a blank
5104 	 * line to distinguish ops dump.
5105 	 */
5106 	if (dd->first) {
5107 		dump_newline(dd->s);
5108 		dd->first = false;
5109 	}
5110 
5111 	/*
5112 	 * There may be multiple lines in $line. Scan and emit each line
5113 	 * separately.
5114 	 */
5115 	while (true) {
5116 		char *end = line;
5117 		char c;
5118 
5119 		while (*end != '\n' && *end != '\0')
5120 			end++;
5121 
5122 		/*
5123 		 * If $line overflowed, it may not have newline at the end.
5124 		 * Always emit with a newline.
5125 		 */
5126 		c = *end;
5127 		*end = '\0';
5128 		dump_line(dd->s, "%s%s", dd->prefix, line);
5129 		if (c == '\0')
5130 			break;
5131 
5132 		/* move to the next line */
5133 		end++;
5134 		if (*end == '\0')
5135 			break;
5136 		line = end;
5137 	}
5138 
5139 	dd->cursor = 0;
5140 }
5141 
5142 static void ops_dump_exit(void)
5143 {
5144 	ops_dump_flush();
5145 	scx_dump_data.cpu = -1;
5146 }
5147 
5148 static void scx_dump_task(struct seq_buf *s, struct scx_dump_ctx *dctx,
5149 			  struct task_struct *p, char marker)
5150 {
5151 	static unsigned long bt[SCX_EXIT_BT_LEN];
5152 	struct scx_sched *sch = scx_root;
5153 	char dsq_id_buf[19] = "(n/a)";
5154 	unsigned long ops_state = atomic_long_read(&p->scx.ops_state);
5155 	unsigned int bt_len = 0;
5156 
5157 	if (p->scx.dsq)
5158 		scnprintf(dsq_id_buf, sizeof(dsq_id_buf), "0x%llx",
5159 			  (unsigned long long)p->scx.dsq->id);
5160 
5161 	dump_newline(s);
5162 	dump_line(s, " %c%c %s[%d] %+ldms",
5163 		  marker, task_state_to_char(p), p->comm, p->pid,
5164 		  jiffies_delta_msecs(p->scx.runnable_at, dctx->at_jiffies));
5165 	dump_line(s, "      scx_state/flags=%u/0x%x dsq_flags=0x%x ops_state/qseq=%lu/%lu",
5166 		  scx_get_task_state(p), p->scx.flags & ~SCX_TASK_STATE_MASK,
5167 		  p->scx.dsq_flags, ops_state & SCX_OPSS_STATE_MASK,
5168 		  ops_state >> SCX_OPSS_QSEQ_SHIFT);
5169 	dump_line(s, "      sticky/holding_cpu=%d/%d dsq_id=%s",
5170 		  p->scx.sticky_cpu, p->scx.holding_cpu, dsq_id_buf);
5171 	dump_line(s, "      dsq_vtime=%llu slice=%llu weight=%u",
5172 		  p->scx.dsq_vtime, p->scx.slice, p->scx.weight);
5173 	dump_line(s, "      cpus=%*pb", cpumask_pr_args(p->cpus_ptr));
5174 
5175 	if (SCX_HAS_OP(sch, dump_task)) {
5176 		ops_dump_init(s, "    ");
5177 		SCX_CALL_OP(sch, SCX_KF_REST, dump_task, NULL, dctx, p);
5178 		ops_dump_exit();
5179 	}
5180 
5181 #ifdef CONFIG_STACKTRACE
5182 	bt_len = stack_trace_save_tsk(p, bt, SCX_EXIT_BT_LEN, 1);
5183 #endif
5184 	if (bt_len) {
5185 		dump_newline(s);
5186 		dump_stack_trace(s, "    ", bt, bt_len);
5187 	}
5188 }
5189 
5190 static void scx_dump_state(struct scx_exit_info *ei, size_t dump_len)
5191 {
5192 	static DEFINE_SPINLOCK(dump_lock);
5193 	static const char trunc_marker[] = "\n\n~~~~ TRUNCATED ~~~~\n";
5194 	struct scx_sched *sch = scx_root;
5195 	struct scx_dump_ctx dctx = {
5196 		.kind = ei->kind,
5197 		.exit_code = ei->exit_code,
5198 		.reason = ei->reason,
5199 		.at_ns = ktime_get_ns(),
5200 		.at_jiffies = jiffies,
5201 	};
5202 	struct seq_buf s;
5203 	struct scx_event_stats events;
5204 	unsigned long flags;
5205 	char *buf;
5206 	int cpu;
5207 
5208 	spin_lock_irqsave(&dump_lock, flags);
5209 
5210 	seq_buf_init(&s, ei->dump, dump_len);
5211 
5212 	if (ei->kind == SCX_EXIT_NONE) {
5213 		dump_line(&s, "Debug dump triggered by %s", ei->reason);
5214 	} else {
5215 		dump_line(&s, "%s[%d] triggered exit kind %d:",
5216 			  current->comm, current->pid, ei->kind);
5217 		dump_line(&s, "  %s (%s)", ei->reason, ei->msg);
5218 		dump_newline(&s);
5219 		dump_line(&s, "Backtrace:");
5220 		dump_stack_trace(&s, "  ", ei->bt, ei->bt_len);
5221 	}
5222 
5223 	if (SCX_HAS_OP(sch, dump)) {
5224 		ops_dump_init(&s, "");
5225 		SCX_CALL_OP(sch, SCX_KF_UNLOCKED, dump, NULL, &dctx);
5226 		ops_dump_exit();
5227 	}
5228 
5229 	dump_newline(&s);
5230 	dump_line(&s, "CPU states");
5231 	dump_line(&s, "----------");
5232 
5233 	for_each_possible_cpu(cpu) {
5234 		struct rq *rq = cpu_rq(cpu);
5235 		struct rq_flags rf;
5236 		struct task_struct *p;
5237 		struct seq_buf ns;
5238 		size_t avail, used;
5239 		bool idle;
5240 
5241 		rq_lock(rq, &rf);
5242 
5243 		idle = list_empty(&rq->scx.runnable_list) &&
5244 			rq->curr->sched_class == &idle_sched_class;
5245 
5246 		if (idle && !SCX_HAS_OP(sch, dump_cpu))
5247 			goto next;
5248 
5249 		/*
5250 		 * We don't yet know whether ops.dump_cpu() will produce output
5251 		 * and we may want to skip the default CPU dump if it doesn't.
5252 		 * Use a nested seq_buf to generate the standard dump so that we
5253 		 * can decide whether to commit later.
5254 		 */
5255 		avail = seq_buf_get_buf(&s, &buf);
5256 		seq_buf_init(&ns, buf, avail);
5257 
5258 		dump_newline(&ns);
5259 		dump_line(&ns, "CPU %-4d: nr_run=%u flags=0x%x cpu_rel=%d ops_qseq=%lu pnt_seq=%lu",
5260 			  cpu, rq->scx.nr_running, rq->scx.flags,
5261 			  rq->scx.cpu_released, rq->scx.ops_qseq,
5262 			  rq->scx.pnt_seq);
5263 		dump_line(&ns, "          curr=%s[%d] class=%ps",
5264 			  rq->curr->comm, rq->curr->pid,
5265 			  rq->curr->sched_class);
5266 		if (!cpumask_empty(rq->scx.cpus_to_kick))
5267 			dump_line(&ns, "  cpus_to_kick   : %*pb",
5268 				  cpumask_pr_args(rq->scx.cpus_to_kick));
5269 		if (!cpumask_empty(rq->scx.cpus_to_kick_if_idle))
5270 			dump_line(&ns, "  idle_to_kick   : %*pb",
5271 				  cpumask_pr_args(rq->scx.cpus_to_kick_if_idle));
5272 		if (!cpumask_empty(rq->scx.cpus_to_preempt))
5273 			dump_line(&ns, "  cpus_to_preempt: %*pb",
5274 				  cpumask_pr_args(rq->scx.cpus_to_preempt));
5275 		if (!cpumask_empty(rq->scx.cpus_to_wait))
5276 			dump_line(&ns, "  cpus_to_wait   : %*pb",
5277 				  cpumask_pr_args(rq->scx.cpus_to_wait));
5278 
5279 		used = seq_buf_used(&ns);
5280 		if (SCX_HAS_OP(sch, dump_cpu)) {
5281 			ops_dump_init(&ns, "  ");
5282 			SCX_CALL_OP(sch, SCX_KF_REST, dump_cpu, NULL,
5283 				    &dctx, cpu, idle);
5284 			ops_dump_exit();
5285 		}
5286 
5287 		/*
5288 		 * If idle && nothing generated by ops.dump_cpu(), there's
5289 		 * nothing interesting. Skip.
5290 		 */
5291 		if (idle && used == seq_buf_used(&ns))
5292 			goto next;
5293 
5294 		/*
5295 		 * $s may already have overflowed when $ns was created. If so,
5296 		 * calling commit on it will trigger BUG.
5297 		 */
5298 		if (avail) {
5299 			seq_buf_commit(&s, seq_buf_used(&ns));
5300 			if (seq_buf_has_overflowed(&ns))
5301 				seq_buf_set_overflow(&s);
5302 		}
5303 
5304 		if (rq->curr->sched_class == &ext_sched_class)
5305 			scx_dump_task(&s, &dctx, rq->curr, '*');
5306 
5307 		list_for_each_entry(p, &rq->scx.runnable_list, scx.runnable_node)
5308 			scx_dump_task(&s, &dctx, p, ' ');
5309 	next:
5310 		rq_unlock(rq, &rf);
5311 	}
5312 
5313 	dump_newline(&s);
5314 	dump_line(&s, "Event counters");
5315 	dump_line(&s, "--------------");
5316 
5317 	scx_read_events(sch, &events);
5318 	scx_dump_event(s, &events, SCX_EV_SELECT_CPU_FALLBACK);
5319 	scx_dump_event(s, &events, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE);
5320 	scx_dump_event(s, &events, SCX_EV_DISPATCH_KEEP_LAST);
5321 	scx_dump_event(s, &events, SCX_EV_ENQ_SKIP_EXITING);
5322 	scx_dump_event(s, &events, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED);
5323 	scx_dump_event(s, &events, SCX_EV_REFILL_SLICE_DFL);
5324 	scx_dump_event(s, &events, SCX_EV_BYPASS_DURATION);
5325 	scx_dump_event(s, &events, SCX_EV_BYPASS_DISPATCH);
5326 	scx_dump_event(s, &events, SCX_EV_BYPASS_ACTIVATE);
5327 
5328 	if (seq_buf_has_overflowed(&s) && dump_len >= sizeof(trunc_marker))
5329 		memcpy(ei->dump + dump_len - sizeof(trunc_marker),
5330 		       trunc_marker, sizeof(trunc_marker));
5331 
5332 	spin_unlock_irqrestore(&dump_lock, flags);
5333 }
5334 
5335 static void scx_error_irq_workfn(struct irq_work *irq_work)
5336 {
5337 	struct scx_sched *sch = container_of(irq_work, struct scx_sched, error_irq_work);
5338 	struct scx_exit_info *ei = sch->exit_info;
5339 
5340 	if (ei->kind >= SCX_EXIT_ERROR)
5341 		scx_dump_state(ei, sch->ops.exit_dump_len);
5342 
5343 	kthread_queue_work(sch->helper, &sch->disable_work);
5344 }
5345 
5346 static void scx_vexit(struct scx_sched *sch,
5347 		      enum scx_exit_kind kind, s64 exit_code,
5348 		      const char *fmt, va_list args)
5349 {
5350 	struct scx_exit_info *ei = sch->exit_info;
5351 	int none = SCX_EXIT_NONE;
5352 
5353 	if (!atomic_try_cmpxchg(&sch->exit_kind, &none, kind))
5354 		return;
5355 
5356 	ei->exit_code = exit_code;
5357 #ifdef CONFIG_STACKTRACE
5358 	if (kind >= SCX_EXIT_ERROR)
5359 		ei->bt_len = stack_trace_save(ei->bt, SCX_EXIT_BT_LEN, 1);
5360 #endif
5361 	vscnprintf(ei->msg, SCX_EXIT_MSG_LEN, fmt, args);
5362 
5363 	/*
5364 	 * Set ei->kind and ->reason for scx_dump_state(). They'll be set again
5365 	 * in scx_disable_workfn().
5366 	 */
5367 	ei->kind = kind;
5368 	ei->reason = scx_exit_reason(ei->kind);
5369 
5370 	irq_work_queue(&sch->error_irq_work);
5371 }
5372 
5373 static struct scx_sched *scx_alloc_and_add_sched(struct sched_ext_ops *ops)
5374 {
5375 	struct scx_sched *sch;
5376 	int node, ret;
5377 
5378 	sch = kzalloc(sizeof(*sch), GFP_KERNEL);
5379 	if (!sch)
5380 		return ERR_PTR(-ENOMEM);
5381 
5382 	sch->exit_info = alloc_exit_info(ops->exit_dump_len);
5383 	if (!sch->exit_info) {
5384 		ret = -ENOMEM;
5385 		goto err_free_sch;
5386 	}
5387 
5388 	ret = rhashtable_init(&sch->dsq_hash, &dsq_hash_params);
5389 	if (ret < 0)
5390 		goto err_free_ei;
5391 
5392 	sch->global_dsqs = kcalloc(nr_node_ids, sizeof(sch->global_dsqs[0]),
5393 				   GFP_KERNEL);
5394 	if (!sch->global_dsqs) {
5395 		ret = -ENOMEM;
5396 		goto err_free_hash;
5397 	}
5398 
5399 	for_each_node_state(node, N_POSSIBLE) {
5400 		struct scx_dispatch_q *dsq;
5401 
5402 		dsq = kzalloc_node(sizeof(*dsq), GFP_KERNEL, node);
5403 		if (!dsq) {
5404 			ret = -ENOMEM;
5405 			goto err_free_gdsqs;
5406 		}
5407 
5408 		init_dsq(dsq, SCX_DSQ_GLOBAL);
5409 		sch->global_dsqs[node] = dsq;
5410 	}
5411 
5412 	sch->event_stats_cpu = alloc_percpu(struct scx_event_stats);
5413 	if (!sch->event_stats_cpu)
5414 		goto err_free_gdsqs;
5415 
5416 	sch->helper = kthread_run_worker(0, "sched_ext_helper");
5417 	if (!sch->helper)
5418 		goto err_free_event_stats;
5419 	sched_set_fifo(sch->helper->task);
5420 
5421 	atomic_set(&sch->exit_kind, SCX_EXIT_NONE);
5422 	init_irq_work(&sch->error_irq_work, scx_error_irq_workfn);
5423 	kthread_init_work(&sch->disable_work, scx_disable_workfn);
5424 	sch->ops = *ops;
5425 	ops->priv = sch;
5426 
5427 	sch->kobj.kset = scx_kset;
5428 	ret = kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root");
5429 	if (ret < 0)
5430 		goto err_stop_helper;
5431 
5432 	return sch;
5433 
5434 err_stop_helper:
5435 	kthread_stop(sch->helper->task);
5436 err_free_event_stats:
5437 	free_percpu(sch->event_stats_cpu);
5438 err_free_gdsqs:
5439 	for_each_node_state(node, N_POSSIBLE)
5440 		kfree(sch->global_dsqs[node]);
5441 	kfree(sch->global_dsqs);
5442 err_free_hash:
5443 	rhashtable_free_and_destroy(&sch->dsq_hash, NULL, NULL);
5444 err_free_ei:
5445 	free_exit_info(sch->exit_info);
5446 err_free_sch:
5447 	kfree(sch);
5448 	return ERR_PTR(ret);
5449 }
5450 
5451 static void check_hotplug_seq(struct scx_sched *sch,
5452 			      const struct sched_ext_ops *ops)
5453 {
5454 	unsigned long long global_hotplug_seq;
5455 
5456 	/*
5457 	 * If a hotplug event has occurred between when a scheduler was
5458 	 * initialized, and when we were able to attach, exit and notify user
5459 	 * space about it.
5460 	 */
5461 	if (ops->hotplug_seq) {
5462 		global_hotplug_seq = atomic_long_read(&scx_hotplug_seq);
5463 		if (ops->hotplug_seq != global_hotplug_seq) {
5464 			scx_exit(sch, SCX_EXIT_UNREG_KERN,
5465 				 SCX_ECODE_ACT_RESTART | SCX_ECODE_RSN_HOTPLUG,
5466 				 "expected hotplug seq %llu did not match actual %llu",
5467 				 ops->hotplug_seq, global_hotplug_seq);
5468 		}
5469 	}
5470 }
5471 
5472 static int validate_ops(struct scx_sched *sch, const struct sched_ext_ops *ops)
5473 {
5474 	/*
5475 	 * It doesn't make sense to specify the SCX_OPS_ENQ_LAST flag if the
5476 	 * ops.enqueue() callback isn't implemented.
5477 	 */
5478 	if ((ops->flags & SCX_OPS_ENQ_LAST) && !ops->enqueue) {
5479 		scx_error(sch, "SCX_OPS_ENQ_LAST requires ops.enqueue() to be implemented");
5480 		return -EINVAL;
5481 	}
5482 
5483 	/*
5484 	 * SCX_OPS_BUILTIN_IDLE_PER_NODE requires built-in CPU idle
5485 	 * selection policy to be enabled.
5486 	 */
5487 	if ((ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE) &&
5488 	    (ops->update_idle && !(ops->flags & SCX_OPS_KEEP_BUILTIN_IDLE))) {
5489 		scx_error(sch, "SCX_OPS_BUILTIN_IDLE_PER_NODE requires CPU idle selection enabled");
5490 		return -EINVAL;
5491 	}
5492 
5493 	if (ops->flags & SCX_OPS_HAS_CGROUP_WEIGHT)
5494 		pr_warn("SCX_OPS_HAS_CGROUP_WEIGHT is deprecated and a noop\n");
5495 
5496 	return 0;
5497 }
5498 
5499 static int scx_enable(struct sched_ext_ops *ops, struct bpf_link *link)
5500 {
5501 	struct scx_sched *sch;
5502 	struct scx_task_iter sti;
5503 	struct task_struct *p;
5504 	unsigned long timeout;
5505 	int i, cpu, ret;
5506 
5507 	if (!cpumask_equal(housekeeping_cpumask(HK_TYPE_DOMAIN),
5508 			   cpu_possible_mask)) {
5509 		pr_err("sched_ext: Not compatible with \"isolcpus=\" domain isolation\n");
5510 		return -EINVAL;
5511 	}
5512 
5513 	mutex_lock(&scx_enable_mutex);
5514 
5515 	if (scx_enable_state() != SCX_DISABLED) {
5516 		ret = -EBUSY;
5517 		goto err_unlock;
5518 	}
5519 
5520 	sch = scx_alloc_and_add_sched(ops);
5521 	if (IS_ERR(sch)) {
5522 		ret = PTR_ERR(sch);
5523 		goto err_unlock;
5524 	}
5525 
5526 	/*
5527 	 * Transition to ENABLING and clear exit info to arm the disable path.
5528 	 * Failure triggers full disabling from here on.
5529 	 */
5530 	WARN_ON_ONCE(scx_set_enable_state(SCX_ENABLING) != SCX_DISABLED);
5531 	WARN_ON_ONCE(scx_root);
5532 
5533 	atomic_long_set(&scx_nr_rejected, 0);
5534 
5535 	for_each_possible_cpu(cpu)
5536 		cpu_rq(cpu)->scx.cpuperf_target = SCX_CPUPERF_ONE;
5537 
5538 	/*
5539 	 * Keep CPUs stable during enable so that the BPF scheduler can track
5540 	 * online CPUs by watching ->on/offline_cpu() after ->init().
5541 	 */
5542 	cpus_read_lock();
5543 
5544 	/*
5545 	 * Make the scheduler instance visible. Must be inside cpus_read_lock().
5546 	 * See handle_hotplug().
5547 	 */
5548 	rcu_assign_pointer(scx_root, sch);
5549 
5550 	scx_idle_enable(ops);
5551 
5552 	if (sch->ops.init) {
5553 		ret = SCX_CALL_OP_RET(sch, SCX_KF_UNLOCKED, init, NULL);
5554 		if (ret) {
5555 			ret = ops_sanitize_err(sch, "init", ret);
5556 			cpus_read_unlock();
5557 			scx_error(sch, "ops.init() failed (%d)", ret);
5558 			goto err_disable;
5559 		}
5560 	}
5561 
5562 	for (i = SCX_OPI_CPU_HOTPLUG_BEGIN; i < SCX_OPI_CPU_HOTPLUG_END; i++)
5563 		if (((void (**)(void))ops)[i])
5564 			set_bit(i, sch->has_op);
5565 
5566 	check_hotplug_seq(sch, ops);
5567 	scx_idle_update_selcpu_topology(ops);
5568 
5569 	cpus_read_unlock();
5570 
5571 	ret = validate_ops(sch, ops);
5572 	if (ret)
5573 		goto err_disable;
5574 
5575 	WARN_ON_ONCE(scx_dsp_ctx);
5576 	scx_dsp_max_batch = ops->dispatch_max_batch ?: SCX_DSP_DFL_MAX_BATCH;
5577 	scx_dsp_ctx = __alloc_percpu(struct_size_t(struct scx_dsp_ctx, buf,
5578 						   scx_dsp_max_batch),
5579 				     __alignof__(struct scx_dsp_ctx));
5580 	if (!scx_dsp_ctx) {
5581 		ret = -ENOMEM;
5582 		goto err_disable;
5583 	}
5584 
5585 	if (ops->timeout_ms)
5586 		timeout = msecs_to_jiffies(ops->timeout_ms);
5587 	else
5588 		timeout = SCX_WATCHDOG_MAX_TIMEOUT;
5589 
5590 	WRITE_ONCE(scx_watchdog_timeout, timeout);
5591 	WRITE_ONCE(scx_watchdog_timestamp, jiffies);
5592 	queue_delayed_work(system_unbound_wq, &scx_watchdog_work,
5593 			   scx_watchdog_timeout / 2);
5594 
5595 	/*
5596 	 * Once __scx_enabled is set, %current can be switched to SCX anytime.
5597 	 * This can lead to stalls as some BPF schedulers (e.g. userspace
5598 	 * scheduling) may not function correctly before all tasks are switched.
5599 	 * Init in bypass mode to guarantee forward progress.
5600 	 */
5601 	scx_bypass(true);
5602 
5603 	for (i = SCX_OPI_NORMAL_BEGIN; i < SCX_OPI_NORMAL_END; i++)
5604 		if (((void (**)(void))ops)[i])
5605 			set_bit(i, sch->has_op);
5606 
5607 	if (sch->ops.cpu_acquire || sch->ops.cpu_release)
5608 		sch->ops.flags |= SCX_OPS_HAS_CPU_PREEMPT;
5609 
5610 	/*
5611 	 * Lock out forks, cgroup on/offlining and moves before opening the
5612 	 * floodgate so that they don't wander into the operations prematurely.
5613 	 */
5614 	percpu_down_write(&scx_fork_rwsem);
5615 
5616 	WARN_ON_ONCE(scx_init_task_enabled);
5617 	scx_init_task_enabled = true;
5618 
5619 	/*
5620 	 * Enable ops for every task. Fork is excluded by scx_fork_rwsem
5621 	 * preventing new tasks from being added. No need to exclude tasks
5622 	 * leaving as sched_ext_free() can handle both prepped and enabled
5623 	 * tasks. Prep all tasks first and then enable them with preemption
5624 	 * disabled.
5625 	 *
5626 	 * All cgroups should be initialized before scx_init_task() so that the
5627 	 * BPF scheduler can reliably track each task's cgroup membership from
5628 	 * scx_init_task(). Lock out cgroup on/offlining and task migrations
5629 	 * while tasks are being initialized so that scx_cgroup_can_attach()
5630 	 * never sees uninitialized tasks.
5631 	 */
5632 	scx_cgroup_lock();
5633 	ret = scx_cgroup_init(sch);
5634 	if (ret)
5635 		goto err_disable_unlock_all;
5636 
5637 	scx_task_iter_start(&sti);
5638 	while ((p = scx_task_iter_next_locked(&sti))) {
5639 		/*
5640 		 * @p may already be dead, have lost all its usages counts and
5641 		 * be waiting for RCU grace period before being freed. @p can't
5642 		 * be initialized for SCX in such cases and should be ignored.
5643 		 */
5644 		if (!tryget_task_struct(p))
5645 			continue;
5646 
5647 		scx_task_iter_unlock(&sti);
5648 
5649 		ret = scx_init_task(p, task_group(p), false);
5650 		if (ret) {
5651 			put_task_struct(p);
5652 			scx_task_iter_relock(&sti);
5653 			scx_task_iter_stop(&sti);
5654 			scx_error(sch, "ops.init_task() failed (%d) for %s[%d]",
5655 				  ret, p->comm, p->pid);
5656 			goto err_disable_unlock_all;
5657 		}
5658 
5659 		scx_set_task_state(p, SCX_TASK_READY);
5660 
5661 		put_task_struct(p);
5662 		scx_task_iter_relock(&sti);
5663 	}
5664 	scx_task_iter_stop(&sti);
5665 	scx_cgroup_unlock();
5666 	percpu_up_write(&scx_fork_rwsem);
5667 
5668 	/*
5669 	 * All tasks are READY. It's safe to turn on scx_enabled() and switch
5670 	 * all eligible tasks.
5671 	 */
5672 	WRITE_ONCE(scx_switching_all, !(ops->flags & SCX_OPS_SWITCH_PARTIAL));
5673 	static_branch_enable(&__scx_enabled);
5674 
5675 	/*
5676 	 * We're fully committed and can't fail. The task READY -> ENABLED
5677 	 * transitions here are synchronized against sched_ext_free() through
5678 	 * scx_tasks_lock.
5679 	 */
5680 	percpu_down_write(&scx_fork_rwsem);
5681 	scx_task_iter_start(&sti);
5682 	while ((p = scx_task_iter_next_locked(&sti))) {
5683 		const struct sched_class *old_class = p->sched_class;
5684 		const struct sched_class *new_class =
5685 			__setscheduler_class(p->policy, p->prio);
5686 		struct sched_enq_and_set_ctx ctx;
5687 
5688 		if (old_class != new_class && p->se.sched_delayed)
5689 			dequeue_task(task_rq(p), p, DEQUEUE_SLEEP | DEQUEUE_DELAYED);
5690 
5691 		sched_deq_and_put_task(p, DEQUEUE_SAVE | DEQUEUE_MOVE, &ctx);
5692 
5693 		p->scx.slice = SCX_SLICE_DFL;
5694 		p->sched_class = new_class;
5695 		check_class_changing(task_rq(p), p, old_class);
5696 
5697 		sched_enq_and_set_task(&ctx);
5698 
5699 		check_class_changed(task_rq(p), p, old_class, p->prio);
5700 	}
5701 	scx_task_iter_stop(&sti);
5702 	percpu_up_write(&scx_fork_rwsem);
5703 
5704 	scx_bypass(false);
5705 
5706 	if (!scx_tryset_enable_state(SCX_ENABLED, SCX_ENABLING)) {
5707 		WARN_ON_ONCE(atomic_read(&sch->exit_kind) == SCX_EXIT_NONE);
5708 		goto err_disable;
5709 	}
5710 
5711 	if (!(ops->flags & SCX_OPS_SWITCH_PARTIAL))
5712 		static_branch_enable(&__scx_switched_all);
5713 
5714 	pr_info("sched_ext: BPF scheduler \"%s\" enabled%s\n",
5715 		sch->ops.name, scx_switched_all() ? "" : " (partial)");
5716 	kobject_uevent(&sch->kobj, KOBJ_ADD);
5717 	mutex_unlock(&scx_enable_mutex);
5718 
5719 	atomic_long_inc(&scx_enable_seq);
5720 
5721 	return 0;
5722 
5723 err_unlock:
5724 	mutex_unlock(&scx_enable_mutex);
5725 	return ret;
5726 
5727 err_disable_unlock_all:
5728 	scx_cgroup_unlock();
5729 	percpu_up_write(&scx_fork_rwsem);
5730 	scx_bypass(false);
5731 err_disable:
5732 	mutex_unlock(&scx_enable_mutex);
5733 	/*
5734 	 * Returning an error code here would not pass all the error information
5735 	 * to userspace. Record errno using scx_error() for cases scx_error()
5736 	 * wasn't already invoked and exit indicating success so that the error
5737 	 * is notified through ops.exit() with all the details.
5738 	 *
5739 	 * Flush scx_disable_work to ensure that error is reported before init
5740 	 * completion. sch's base reference will be put by bpf_scx_unreg().
5741 	 */
5742 	scx_error(sch, "scx_enable() failed (%d)", ret);
5743 	kthread_flush_work(&sch->disable_work);
5744 	return 0;
5745 }
5746 
5747 
5748 /********************************************************************************
5749  * bpf_struct_ops plumbing.
5750  */
5751 #include <linux/bpf_verifier.h>
5752 #include <linux/bpf.h>
5753 #include <linux/btf.h>
5754 
5755 static const struct btf_type *task_struct_type;
5756 
5757 static bool bpf_scx_is_valid_access(int off, int size,
5758 				    enum bpf_access_type type,
5759 				    const struct bpf_prog *prog,
5760 				    struct bpf_insn_access_aux *info)
5761 {
5762 	if (type != BPF_READ)
5763 		return false;
5764 	if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
5765 		return false;
5766 	if (off % size != 0)
5767 		return false;
5768 
5769 	return btf_ctx_access(off, size, type, prog, info);
5770 }
5771 
5772 static int bpf_scx_btf_struct_access(struct bpf_verifier_log *log,
5773 				     const struct bpf_reg_state *reg, int off,
5774 				     int size)
5775 {
5776 	const struct btf_type *t;
5777 
5778 	t = btf_type_by_id(reg->btf, reg->btf_id);
5779 	if (t == task_struct_type) {
5780 		if (off >= offsetof(struct task_struct, scx.slice) &&
5781 		    off + size <= offsetofend(struct task_struct, scx.slice))
5782 			return SCALAR_VALUE;
5783 		if (off >= offsetof(struct task_struct, scx.dsq_vtime) &&
5784 		    off + size <= offsetofend(struct task_struct, scx.dsq_vtime))
5785 			return SCALAR_VALUE;
5786 		if (off >= offsetof(struct task_struct, scx.disallow) &&
5787 		    off + size <= offsetofend(struct task_struct, scx.disallow))
5788 			return SCALAR_VALUE;
5789 	}
5790 
5791 	return -EACCES;
5792 }
5793 
5794 static const struct bpf_verifier_ops bpf_scx_verifier_ops = {
5795 	.get_func_proto = bpf_base_func_proto,
5796 	.is_valid_access = bpf_scx_is_valid_access,
5797 	.btf_struct_access = bpf_scx_btf_struct_access,
5798 };
5799 
5800 static int bpf_scx_init_member(const struct btf_type *t,
5801 			       const struct btf_member *member,
5802 			       void *kdata, const void *udata)
5803 {
5804 	const struct sched_ext_ops *uops = udata;
5805 	struct sched_ext_ops *ops = kdata;
5806 	u32 moff = __btf_member_bit_offset(t, member) / 8;
5807 	int ret;
5808 
5809 	switch (moff) {
5810 	case offsetof(struct sched_ext_ops, dispatch_max_batch):
5811 		if (*(u32 *)(udata + moff) > INT_MAX)
5812 			return -E2BIG;
5813 		ops->dispatch_max_batch = *(u32 *)(udata + moff);
5814 		return 1;
5815 	case offsetof(struct sched_ext_ops, flags):
5816 		if (*(u64 *)(udata + moff) & ~SCX_OPS_ALL_FLAGS)
5817 			return -EINVAL;
5818 		ops->flags = *(u64 *)(udata + moff);
5819 		return 1;
5820 	case offsetof(struct sched_ext_ops, name):
5821 		ret = bpf_obj_name_cpy(ops->name, uops->name,
5822 				       sizeof(ops->name));
5823 		if (ret < 0)
5824 			return ret;
5825 		if (ret == 0)
5826 			return -EINVAL;
5827 		return 1;
5828 	case offsetof(struct sched_ext_ops, timeout_ms):
5829 		if (msecs_to_jiffies(*(u32 *)(udata + moff)) >
5830 		    SCX_WATCHDOG_MAX_TIMEOUT)
5831 			return -E2BIG;
5832 		ops->timeout_ms = *(u32 *)(udata + moff);
5833 		return 1;
5834 	case offsetof(struct sched_ext_ops, exit_dump_len):
5835 		ops->exit_dump_len =
5836 			*(u32 *)(udata + moff) ?: SCX_EXIT_DUMP_DFL_LEN;
5837 		return 1;
5838 	case offsetof(struct sched_ext_ops, hotplug_seq):
5839 		ops->hotplug_seq = *(u64 *)(udata + moff);
5840 		return 1;
5841 	}
5842 
5843 	return 0;
5844 }
5845 
5846 static int bpf_scx_check_member(const struct btf_type *t,
5847 				const struct btf_member *member,
5848 				const struct bpf_prog *prog)
5849 {
5850 	u32 moff = __btf_member_bit_offset(t, member) / 8;
5851 
5852 	switch (moff) {
5853 	case offsetof(struct sched_ext_ops, init_task):
5854 #ifdef CONFIG_EXT_GROUP_SCHED
5855 	case offsetof(struct sched_ext_ops, cgroup_init):
5856 	case offsetof(struct sched_ext_ops, cgroup_exit):
5857 	case offsetof(struct sched_ext_ops, cgroup_prep_move):
5858 #endif
5859 	case offsetof(struct sched_ext_ops, cpu_online):
5860 	case offsetof(struct sched_ext_ops, cpu_offline):
5861 	case offsetof(struct sched_ext_ops, init):
5862 	case offsetof(struct sched_ext_ops, exit):
5863 		break;
5864 	default:
5865 		if (prog->sleepable)
5866 			return -EINVAL;
5867 	}
5868 
5869 	return 0;
5870 }
5871 
5872 static int bpf_scx_reg(void *kdata, struct bpf_link *link)
5873 {
5874 	return scx_enable(kdata, link);
5875 }
5876 
5877 static void bpf_scx_unreg(void *kdata, struct bpf_link *link)
5878 {
5879 	struct sched_ext_ops *ops = kdata;
5880 	struct scx_sched *sch = ops->priv;
5881 
5882 	scx_disable(SCX_EXIT_UNREG);
5883 	kthread_flush_work(&sch->disable_work);
5884 	kobject_put(&sch->kobj);
5885 }
5886 
5887 static int bpf_scx_init(struct btf *btf)
5888 {
5889 	task_struct_type = btf_type_by_id(btf, btf_tracing_ids[BTF_TRACING_TYPE_TASK]);
5890 
5891 	return 0;
5892 }
5893 
5894 static int bpf_scx_update(void *kdata, void *old_kdata, struct bpf_link *link)
5895 {
5896 	/*
5897 	 * sched_ext does not support updating the actively-loaded BPF
5898 	 * scheduler, as registering a BPF scheduler can always fail if the
5899 	 * scheduler returns an error code for e.g. ops.init(), ops.init_task(),
5900 	 * etc. Similarly, we can always race with unregistration happening
5901 	 * elsewhere, such as with sysrq.
5902 	 */
5903 	return -EOPNOTSUPP;
5904 }
5905 
5906 static int bpf_scx_validate(void *kdata)
5907 {
5908 	return 0;
5909 }
5910 
5911 static s32 sched_ext_ops__select_cpu(struct task_struct *p, s32 prev_cpu, u64 wake_flags) { return -EINVAL; }
5912 static void sched_ext_ops__enqueue(struct task_struct *p, u64 enq_flags) {}
5913 static void sched_ext_ops__dequeue(struct task_struct *p, u64 enq_flags) {}
5914 static void sched_ext_ops__dispatch(s32 prev_cpu, struct task_struct *prev__nullable) {}
5915 static void sched_ext_ops__tick(struct task_struct *p) {}
5916 static void sched_ext_ops__runnable(struct task_struct *p, u64 enq_flags) {}
5917 static void sched_ext_ops__running(struct task_struct *p) {}
5918 static void sched_ext_ops__stopping(struct task_struct *p, bool runnable) {}
5919 static void sched_ext_ops__quiescent(struct task_struct *p, u64 deq_flags) {}
5920 static bool sched_ext_ops__yield(struct task_struct *from, struct task_struct *to__nullable) { return false; }
5921 static bool sched_ext_ops__core_sched_before(struct task_struct *a, struct task_struct *b) { return false; }
5922 static void sched_ext_ops__set_weight(struct task_struct *p, u32 weight) {}
5923 static void sched_ext_ops__set_cpumask(struct task_struct *p, const struct cpumask *mask) {}
5924 static void sched_ext_ops__update_idle(s32 cpu, bool idle) {}
5925 static void sched_ext_ops__cpu_acquire(s32 cpu, struct scx_cpu_acquire_args *args) {}
5926 static void sched_ext_ops__cpu_release(s32 cpu, struct scx_cpu_release_args *args) {}
5927 static s32 sched_ext_ops__init_task(struct task_struct *p, struct scx_init_task_args *args) { return -EINVAL; }
5928 static void sched_ext_ops__exit_task(struct task_struct *p, struct scx_exit_task_args *args) {}
5929 static void sched_ext_ops__enable(struct task_struct *p) {}
5930 static void sched_ext_ops__disable(struct task_struct *p) {}
5931 #ifdef CONFIG_EXT_GROUP_SCHED
5932 static s32 sched_ext_ops__cgroup_init(struct cgroup *cgrp, struct scx_cgroup_init_args *args) { return -EINVAL; }
5933 static void sched_ext_ops__cgroup_exit(struct cgroup *cgrp) {}
5934 static s32 sched_ext_ops__cgroup_prep_move(struct task_struct *p, struct cgroup *from, struct cgroup *to) { return -EINVAL; }
5935 static void sched_ext_ops__cgroup_move(struct task_struct *p, struct cgroup *from, struct cgroup *to) {}
5936 static void sched_ext_ops__cgroup_cancel_move(struct task_struct *p, struct cgroup *from, struct cgroup *to) {}
5937 static void sched_ext_ops__cgroup_set_weight(struct cgroup *cgrp, u32 weight) {}
5938 #endif
5939 static void sched_ext_ops__cpu_online(s32 cpu) {}
5940 static void sched_ext_ops__cpu_offline(s32 cpu) {}
5941 static s32 sched_ext_ops__init(void) { return -EINVAL; }
5942 static void sched_ext_ops__exit(struct scx_exit_info *info) {}
5943 static void sched_ext_ops__dump(struct scx_dump_ctx *ctx) {}
5944 static void sched_ext_ops__dump_cpu(struct scx_dump_ctx *ctx, s32 cpu, bool idle) {}
5945 static void sched_ext_ops__dump_task(struct scx_dump_ctx *ctx, struct task_struct *p) {}
5946 
5947 static struct sched_ext_ops __bpf_ops_sched_ext_ops = {
5948 	.select_cpu		= sched_ext_ops__select_cpu,
5949 	.enqueue		= sched_ext_ops__enqueue,
5950 	.dequeue		= sched_ext_ops__dequeue,
5951 	.dispatch		= sched_ext_ops__dispatch,
5952 	.tick			= sched_ext_ops__tick,
5953 	.runnable		= sched_ext_ops__runnable,
5954 	.running		= sched_ext_ops__running,
5955 	.stopping		= sched_ext_ops__stopping,
5956 	.quiescent		= sched_ext_ops__quiescent,
5957 	.yield			= sched_ext_ops__yield,
5958 	.core_sched_before	= sched_ext_ops__core_sched_before,
5959 	.set_weight		= sched_ext_ops__set_weight,
5960 	.set_cpumask		= sched_ext_ops__set_cpumask,
5961 	.update_idle		= sched_ext_ops__update_idle,
5962 	.cpu_acquire		= sched_ext_ops__cpu_acquire,
5963 	.cpu_release		= sched_ext_ops__cpu_release,
5964 	.init_task		= sched_ext_ops__init_task,
5965 	.exit_task		= sched_ext_ops__exit_task,
5966 	.enable			= sched_ext_ops__enable,
5967 	.disable		= sched_ext_ops__disable,
5968 #ifdef CONFIG_EXT_GROUP_SCHED
5969 	.cgroup_init		= sched_ext_ops__cgroup_init,
5970 	.cgroup_exit		= sched_ext_ops__cgroup_exit,
5971 	.cgroup_prep_move	= sched_ext_ops__cgroup_prep_move,
5972 	.cgroup_move		= sched_ext_ops__cgroup_move,
5973 	.cgroup_cancel_move	= sched_ext_ops__cgroup_cancel_move,
5974 	.cgroup_set_weight	= sched_ext_ops__cgroup_set_weight,
5975 #endif
5976 	.cpu_online		= sched_ext_ops__cpu_online,
5977 	.cpu_offline		= sched_ext_ops__cpu_offline,
5978 	.init			= sched_ext_ops__init,
5979 	.exit			= sched_ext_ops__exit,
5980 	.dump			= sched_ext_ops__dump,
5981 	.dump_cpu		= sched_ext_ops__dump_cpu,
5982 	.dump_task		= sched_ext_ops__dump_task,
5983 };
5984 
5985 static struct bpf_struct_ops bpf_sched_ext_ops = {
5986 	.verifier_ops = &bpf_scx_verifier_ops,
5987 	.reg = bpf_scx_reg,
5988 	.unreg = bpf_scx_unreg,
5989 	.check_member = bpf_scx_check_member,
5990 	.init_member = bpf_scx_init_member,
5991 	.init = bpf_scx_init,
5992 	.update = bpf_scx_update,
5993 	.validate = bpf_scx_validate,
5994 	.name = "sched_ext_ops",
5995 	.owner = THIS_MODULE,
5996 	.cfi_stubs = &__bpf_ops_sched_ext_ops
5997 };
5998 
5999 
6000 /********************************************************************************
6001  * System integration and init.
6002  */
6003 
6004 static void sysrq_handle_sched_ext_reset(u8 key)
6005 {
6006 	scx_disable(SCX_EXIT_SYSRQ);
6007 }
6008 
6009 static const struct sysrq_key_op sysrq_sched_ext_reset_op = {
6010 	.handler	= sysrq_handle_sched_ext_reset,
6011 	.help_msg	= "reset-sched-ext(S)",
6012 	.action_msg	= "Disable sched_ext and revert all tasks to CFS",
6013 	.enable_mask	= SYSRQ_ENABLE_RTNICE,
6014 };
6015 
6016 static void sysrq_handle_sched_ext_dump(u8 key)
6017 {
6018 	struct scx_exit_info ei = { .kind = SCX_EXIT_NONE, .reason = "SysRq-D" };
6019 
6020 	if (scx_enabled())
6021 		scx_dump_state(&ei, 0);
6022 }
6023 
6024 static const struct sysrq_key_op sysrq_sched_ext_dump_op = {
6025 	.handler	= sysrq_handle_sched_ext_dump,
6026 	.help_msg	= "dump-sched-ext(D)",
6027 	.action_msg	= "Trigger sched_ext debug dump",
6028 	.enable_mask	= SYSRQ_ENABLE_RTNICE,
6029 };
6030 
6031 static bool can_skip_idle_kick(struct rq *rq)
6032 {
6033 	lockdep_assert_rq_held(rq);
6034 
6035 	/*
6036 	 * We can skip idle kicking if @rq is going to go through at least one
6037 	 * full SCX scheduling cycle before going idle. Just checking whether
6038 	 * curr is not idle is insufficient because we could be racing
6039 	 * balance_one() trying to pull the next task from a remote rq, which
6040 	 * may fail, and @rq may become idle afterwards.
6041 	 *
6042 	 * The race window is small and we don't and can't guarantee that @rq is
6043 	 * only kicked while idle anyway. Skip only when sure.
6044 	 */
6045 	return !is_idle_task(rq->curr) && !(rq->scx.flags & SCX_RQ_IN_BALANCE);
6046 }
6047 
6048 static bool kick_one_cpu(s32 cpu, struct rq *this_rq, unsigned long *pseqs)
6049 {
6050 	struct rq *rq = cpu_rq(cpu);
6051 	struct scx_rq *this_scx = &this_rq->scx;
6052 	bool should_wait = false;
6053 	unsigned long flags;
6054 
6055 	raw_spin_rq_lock_irqsave(rq, flags);
6056 
6057 	/*
6058 	 * During CPU hotplug, a CPU may depend on kicking itself to make
6059 	 * forward progress. Allow kicking self regardless of online state.
6060 	 */
6061 	if (cpu_online(cpu) || cpu == cpu_of(this_rq)) {
6062 		if (cpumask_test_cpu(cpu, this_scx->cpus_to_preempt)) {
6063 			if (rq->curr->sched_class == &ext_sched_class)
6064 				rq->curr->scx.slice = 0;
6065 			cpumask_clear_cpu(cpu, this_scx->cpus_to_preempt);
6066 		}
6067 
6068 		if (cpumask_test_cpu(cpu, this_scx->cpus_to_wait)) {
6069 			pseqs[cpu] = rq->scx.pnt_seq;
6070 			should_wait = true;
6071 		}
6072 
6073 		resched_curr(rq);
6074 	} else {
6075 		cpumask_clear_cpu(cpu, this_scx->cpus_to_preempt);
6076 		cpumask_clear_cpu(cpu, this_scx->cpus_to_wait);
6077 	}
6078 
6079 	raw_spin_rq_unlock_irqrestore(rq, flags);
6080 
6081 	return should_wait;
6082 }
6083 
6084 static void kick_one_cpu_if_idle(s32 cpu, struct rq *this_rq)
6085 {
6086 	struct rq *rq = cpu_rq(cpu);
6087 	unsigned long flags;
6088 
6089 	raw_spin_rq_lock_irqsave(rq, flags);
6090 
6091 	if (!can_skip_idle_kick(rq) &&
6092 	    (cpu_online(cpu) || cpu == cpu_of(this_rq)))
6093 		resched_curr(rq);
6094 
6095 	raw_spin_rq_unlock_irqrestore(rq, flags);
6096 }
6097 
6098 static void kick_cpus_irq_workfn(struct irq_work *irq_work)
6099 {
6100 	struct rq *this_rq = this_rq();
6101 	struct scx_rq *this_scx = &this_rq->scx;
6102 	unsigned long *pseqs = this_cpu_ptr(scx_kick_cpus_pnt_seqs);
6103 	bool should_wait = false;
6104 	s32 cpu;
6105 
6106 	for_each_cpu(cpu, this_scx->cpus_to_kick) {
6107 		should_wait |= kick_one_cpu(cpu, this_rq, pseqs);
6108 		cpumask_clear_cpu(cpu, this_scx->cpus_to_kick);
6109 		cpumask_clear_cpu(cpu, this_scx->cpus_to_kick_if_idle);
6110 	}
6111 
6112 	for_each_cpu(cpu, this_scx->cpus_to_kick_if_idle) {
6113 		kick_one_cpu_if_idle(cpu, this_rq);
6114 		cpumask_clear_cpu(cpu, this_scx->cpus_to_kick_if_idle);
6115 	}
6116 
6117 	if (!should_wait)
6118 		return;
6119 
6120 	for_each_cpu(cpu, this_scx->cpus_to_wait) {
6121 		unsigned long *wait_pnt_seq = &cpu_rq(cpu)->scx.pnt_seq;
6122 
6123 		if (cpu != cpu_of(this_rq)) {
6124 			/*
6125 			 * Pairs with smp_store_release() issued by this CPU in
6126 			 * switch_class() on the resched path.
6127 			 *
6128 			 * We busy-wait here to guarantee that no other task can
6129 			 * be scheduled on our core before the target CPU has
6130 			 * entered the resched path.
6131 			 */
6132 			while (smp_load_acquire(wait_pnt_seq) == pseqs[cpu])
6133 				cpu_relax();
6134 		}
6135 
6136 		cpumask_clear_cpu(cpu, this_scx->cpus_to_wait);
6137 	}
6138 }
6139 
6140 /**
6141  * print_scx_info - print out sched_ext scheduler state
6142  * @log_lvl: the log level to use when printing
6143  * @p: target task
6144  *
6145  * If a sched_ext scheduler is enabled, print the name and state of the
6146  * scheduler. If @p is on sched_ext, print further information about the task.
6147  *
6148  * This function can be safely called on any task as long as the task_struct
6149  * itself is accessible. While safe, this function isn't synchronized and may
6150  * print out mixups or garbages of limited length.
6151  */
6152 void print_scx_info(const char *log_lvl, struct task_struct *p)
6153 {
6154 	struct scx_sched *sch = scx_root;
6155 	enum scx_enable_state state = scx_enable_state();
6156 	const char *all = READ_ONCE(scx_switching_all) ? "+all" : "";
6157 	char runnable_at_buf[22] = "?";
6158 	struct sched_class *class;
6159 	unsigned long runnable_at;
6160 
6161 	if (state == SCX_DISABLED)
6162 		return;
6163 
6164 	/*
6165 	 * Carefully check if the task was running on sched_ext, and then
6166 	 * carefully copy the time it's been runnable, and its state.
6167 	 */
6168 	if (copy_from_kernel_nofault(&class, &p->sched_class, sizeof(class)) ||
6169 	    class != &ext_sched_class) {
6170 		printk("%sSched_ext: %s (%s%s)", log_lvl, sch->ops.name,
6171 		       scx_enable_state_str[state], all);
6172 		return;
6173 	}
6174 
6175 	if (!copy_from_kernel_nofault(&runnable_at, &p->scx.runnable_at,
6176 				      sizeof(runnable_at)))
6177 		scnprintf(runnable_at_buf, sizeof(runnable_at_buf), "%+ldms",
6178 			  jiffies_delta_msecs(runnable_at, jiffies));
6179 
6180 	/* print everything onto one line to conserve console space */
6181 	printk("%sSched_ext: %s (%s%s), task: runnable_at=%s",
6182 	       log_lvl, sch->ops.name, scx_enable_state_str[state], all,
6183 	       runnable_at_buf);
6184 }
6185 
6186 static int scx_pm_handler(struct notifier_block *nb, unsigned long event, void *ptr)
6187 {
6188 	/*
6189 	 * SCX schedulers often have userspace components which are sometimes
6190 	 * involved in critial scheduling paths. PM operations involve freezing
6191 	 * userspace which can lead to scheduling misbehaviors including stalls.
6192 	 * Let's bypass while PM operations are in progress.
6193 	 */
6194 	switch (event) {
6195 	case PM_HIBERNATION_PREPARE:
6196 	case PM_SUSPEND_PREPARE:
6197 	case PM_RESTORE_PREPARE:
6198 		scx_bypass(true);
6199 		break;
6200 	case PM_POST_HIBERNATION:
6201 	case PM_POST_SUSPEND:
6202 	case PM_POST_RESTORE:
6203 		scx_bypass(false);
6204 		break;
6205 	}
6206 
6207 	return NOTIFY_OK;
6208 }
6209 
6210 static struct notifier_block scx_pm_notifier = {
6211 	.notifier_call = scx_pm_handler,
6212 };
6213 
6214 void __init init_sched_ext_class(void)
6215 {
6216 	s32 cpu, v;
6217 
6218 	/*
6219 	 * The following is to prevent the compiler from optimizing out the enum
6220 	 * definitions so that BPF scheduler implementations can use them
6221 	 * through the generated vmlinux.h.
6222 	 */
6223 	WRITE_ONCE(v, SCX_ENQ_WAKEUP | SCX_DEQ_SLEEP | SCX_KICK_PREEMPT |
6224 		   SCX_TG_ONLINE);
6225 
6226 	scx_idle_init_masks();
6227 
6228 	scx_kick_cpus_pnt_seqs =
6229 		__alloc_percpu(sizeof(scx_kick_cpus_pnt_seqs[0]) * nr_cpu_ids,
6230 			       __alignof__(scx_kick_cpus_pnt_seqs[0]));
6231 	BUG_ON(!scx_kick_cpus_pnt_seqs);
6232 
6233 	for_each_possible_cpu(cpu) {
6234 		struct rq *rq = cpu_rq(cpu);
6235 		int  n = cpu_to_node(cpu);
6236 
6237 		init_dsq(&rq->scx.local_dsq, SCX_DSQ_LOCAL);
6238 		INIT_LIST_HEAD(&rq->scx.runnable_list);
6239 		INIT_LIST_HEAD(&rq->scx.ddsp_deferred_locals);
6240 
6241 		BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_kick, GFP_KERNEL, n));
6242 		BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_kick_if_idle, GFP_KERNEL, n));
6243 		BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_preempt, GFP_KERNEL, n));
6244 		BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_wait, GFP_KERNEL, n));
6245 		init_irq_work(&rq->scx.deferred_irq_work, deferred_irq_workfn);
6246 		init_irq_work(&rq->scx.kick_cpus_irq_work, kick_cpus_irq_workfn);
6247 
6248 		if (cpu_online(cpu))
6249 			cpu_rq(cpu)->scx.flags |= SCX_RQ_ONLINE;
6250 	}
6251 
6252 	register_sysrq_key('S', &sysrq_sched_ext_reset_op);
6253 	register_sysrq_key('D', &sysrq_sched_ext_dump_op);
6254 	INIT_DELAYED_WORK(&scx_watchdog_work, scx_watchdog_workfn);
6255 }
6256 
6257 
6258 /********************************************************************************
6259  * Helpers that can be called from the BPF scheduler.
6260  */
6261 static bool scx_dsq_insert_preamble(struct task_struct *p, u64 enq_flags)
6262 {
6263 	if (!scx_kf_allowed(SCX_KF_ENQUEUE | SCX_KF_DISPATCH))
6264 		return false;
6265 
6266 	lockdep_assert_irqs_disabled();
6267 
6268 	if (unlikely(!p)) {
6269 		scx_kf_error("called with NULL task");
6270 		return false;
6271 	}
6272 
6273 	if (unlikely(enq_flags & __SCX_ENQ_INTERNAL_MASK)) {
6274 		scx_kf_error("invalid enq_flags 0x%llx", enq_flags);
6275 		return false;
6276 	}
6277 
6278 	return true;
6279 }
6280 
6281 static void scx_dsq_insert_commit(struct task_struct *p, u64 dsq_id,
6282 				  u64 enq_flags)
6283 {
6284 	struct scx_dsp_ctx *dspc = this_cpu_ptr(scx_dsp_ctx);
6285 	struct task_struct *ddsp_task;
6286 
6287 	ddsp_task = __this_cpu_read(direct_dispatch_task);
6288 	if (ddsp_task) {
6289 		mark_direct_dispatch(ddsp_task, p, dsq_id, enq_flags);
6290 		return;
6291 	}
6292 
6293 	if (unlikely(dspc->cursor >= scx_dsp_max_batch)) {
6294 		scx_kf_error("dispatch buffer overflow");
6295 		return;
6296 	}
6297 
6298 	dspc->buf[dspc->cursor++] = (struct scx_dsp_buf_ent){
6299 		.task = p,
6300 		.qseq = atomic_long_read(&p->scx.ops_state) & SCX_OPSS_QSEQ_MASK,
6301 		.dsq_id = dsq_id,
6302 		.enq_flags = enq_flags,
6303 	};
6304 }
6305 
6306 __bpf_kfunc_start_defs();
6307 
6308 /**
6309  * scx_bpf_dsq_insert - Insert a task into the FIFO queue of a DSQ
6310  * @p: task_struct to insert
6311  * @dsq_id: DSQ to insert into
6312  * @slice: duration @p can run for in nsecs, 0 to keep the current value
6313  * @enq_flags: SCX_ENQ_*
6314  *
6315  * Insert @p into the FIFO queue of the DSQ identified by @dsq_id. It is safe to
6316  * call this function spuriously. Can be called from ops.enqueue(),
6317  * ops.select_cpu(), and ops.dispatch().
6318  *
6319  * When called from ops.select_cpu() or ops.enqueue(), it's for direct dispatch
6320  * and @p must match the task being enqueued.
6321  *
6322  * When called from ops.select_cpu(), @enq_flags and @dsp_id are stored, and @p
6323  * will be directly inserted into the corresponding dispatch queue after
6324  * ops.select_cpu() returns. If @p is inserted into SCX_DSQ_LOCAL, it will be
6325  * inserted into the local DSQ of the CPU returned by ops.select_cpu().
6326  * @enq_flags are OR'd with the enqueue flags on the enqueue path before the
6327  * task is inserted.
6328  *
6329  * When called from ops.dispatch(), there are no restrictions on @p or @dsq_id
6330  * and this function can be called upto ops.dispatch_max_batch times to insert
6331  * multiple tasks. scx_bpf_dispatch_nr_slots() returns the number of the
6332  * remaining slots. scx_bpf_consume() flushes the batch and resets the counter.
6333  *
6334  * This function doesn't have any locking restrictions and may be called under
6335  * BPF locks (in the future when BPF introduces more flexible locking).
6336  *
6337  * @p is allowed to run for @slice. The scheduling path is triggered on slice
6338  * exhaustion. If zero, the current residual slice is maintained. If
6339  * %SCX_SLICE_INF, @p never expires and the BPF scheduler must kick the CPU with
6340  * scx_bpf_kick_cpu() to trigger scheduling.
6341  */
6342 __bpf_kfunc void scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, u64 slice,
6343 				    u64 enq_flags)
6344 {
6345 	if (!scx_dsq_insert_preamble(p, enq_flags))
6346 		return;
6347 
6348 	if (slice)
6349 		p->scx.slice = slice;
6350 	else
6351 		p->scx.slice = p->scx.slice ?: 1;
6352 
6353 	scx_dsq_insert_commit(p, dsq_id, enq_flags);
6354 }
6355 
6356 /* for backward compatibility, will be removed in v6.15 */
6357 __bpf_kfunc void scx_bpf_dispatch(struct task_struct *p, u64 dsq_id, u64 slice,
6358 				  u64 enq_flags)
6359 {
6360 	printk_deferred_once(KERN_WARNING "sched_ext: scx_bpf_dispatch() renamed to scx_bpf_dsq_insert()");
6361 	scx_bpf_dsq_insert(p, dsq_id, slice, enq_flags);
6362 }
6363 
6364 /**
6365  * scx_bpf_dsq_insert_vtime - Insert a task into the vtime priority queue of a DSQ
6366  * @p: task_struct to insert
6367  * @dsq_id: DSQ to insert into
6368  * @slice: duration @p can run for in nsecs, 0 to keep the current value
6369  * @vtime: @p's ordering inside the vtime-sorted queue of the target DSQ
6370  * @enq_flags: SCX_ENQ_*
6371  *
6372  * Insert @p into the vtime priority queue of the DSQ identified by @dsq_id.
6373  * Tasks queued into the priority queue are ordered by @vtime. All other aspects
6374  * are identical to scx_bpf_dsq_insert().
6375  *
6376  * @vtime ordering is according to time_before64() which considers wrapping. A
6377  * numerically larger vtime may indicate an earlier position in the ordering and
6378  * vice-versa.
6379  *
6380  * A DSQ can only be used as a FIFO or priority queue at any given time and this
6381  * function must not be called on a DSQ which already has one or more FIFO tasks
6382  * queued and vice-versa. Also, the built-in DSQs (SCX_DSQ_LOCAL and
6383  * SCX_DSQ_GLOBAL) cannot be used as priority queues.
6384  */
6385 __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id,
6386 					  u64 slice, u64 vtime, u64 enq_flags)
6387 {
6388 	if (!scx_dsq_insert_preamble(p, enq_flags))
6389 		return;
6390 
6391 	if (slice)
6392 		p->scx.slice = slice;
6393 	else
6394 		p->scx.slice = p->scx.slice ?: 1;
6395 
6396 	p->scx.dsq_vtime = vtime;
6397 
6398 	scx_dsq_insert_commit(p, dsq_id, enq_flags | SCX_ENQ_DSQ_PRIQ);
6399 }
6400 
6401 /* for backward compatibility, will be removed in v6.15 */
6402 __bpf_kfunc void scx_bpf_dispatch_vtime(struct task_struct *p, u64 dsq_id,
6403 					u64 slice, u64 vtime, u64 enq_flags)
6404 {
6405 	printk_deferred_once(KERN_WARNING "sched_ext: scx_bpf_dispatch_vtime() renamed to scx_bpf_dsq_insert_vtime()");
6406 	scx_bpf_dsq_insert_vtime(p, dsq_id, slice, vtime, enq_flags);
6407 }
6408 
6409 __bpf_kfunc_end_defs();
6410 
6411 BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch)
6412 BTF_ID_FLAGS(func, scx_bpf_dsq_insert, KF_RCU)
6413 BTF_ID_FLAGS(func, scx_bpf_dsq_insert_vtime, KF_RCU)
6414 BTF_ID_FLAGS(func, scx_bpf_dispatch, KF_RCU)
6415 BTF_ID_FLAGS(func, scx_bpf_dispatch_vtime, KF_RCU)
6416 BTF_KFUNCS_END(scx_kfunc_ids_enqueue_dispatch)
6417 
6418 static const struct btf_kfunc_id_set scx_kfunc_set_enqueue_dispatch = {
6419 	.owner			= THIS_MODULE,
6420 	.set			= &scx_kfunc_ids_enqueue_dispatch,
6421 };
6422 
6423 static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,
6424 			 struct task_struct *p, u64 dsq_id, u64 enq_flags)
6425 {
6426 	struct scx_sched *sch = scx_root;
6427 	struct scx_dispatch_q *src_dsq = kit->dsq, *dst_dsq;
6428 	struct rq *this_rq, *src_rq, *locked_rq;
6429 	bool dispatched = false;
6430 	bool in_balance;
6431 	unsigned long flags;
6432 
6433 	if (!scx_kf_allowed_if_unlocked() && !scx_kf_allowed(SCX_KF_DISPATCH))
6434 		return false;
6435 
6436 	/*
6437 	 * Can be called from either ops.dispatch() locking this_rq() or any
6438 	 * context where no rq lock is held. If latter, lock @p's task_rq which
6439 	 * we'll likely need anyway.
6440 	 */
6441 	src_rq = task_rq(p);
6442 
6443 	local_irq_save(flags);
6444 	this_rq = this_rq();
6445 	in_balance = this_rq->scx.flags & SCX_RQ_IN_BALANCE;
6446 
6447 	if (in_balance) {
6448 		if (this_rq != src_rq) {
6449 			raw_spin_rq_unlock(this_rq);
6450 			raw_spin_rq_lock(src_rq);
6451 		}
6452 	} else {
6453 		raw_spin_rq_lock(src_rq);
6454 	}
6455 
6456 	/*
6457 	 * If the BPF scheduler keeps calling this function repeatedly, it can
6458 	 * cause similar live-lock conditions as consume_dispatch_q(). Insert a
6459 	 * breather if necessary.
6460 	 */
6461 	scx_breather(src_rq);
6462 
6463 	locked_rq = src_rq;
6464 	raw_spin_lock(&src_dsq->lock);
6465 
6466 	/*
6467 	 * Did someone else get to it? @p could have already left $src_dsq, got
6468 	 * re-enqueud, or be in the process of being consumed by someone else.
6469 	 */
6470 	if (unlikely(p->scx.dsq != src_dsq ||
6471 		     u32_before(kit->cursor.priv, p->scx.dsq_seq) ||
6472 		     p->scx.holding_cpu >= 0) ||
6473 	    WARN_ON_ONCE(src_rq != task_rq(p))) {
6474 		raw_spin_unlock(&src_dsq->lock);
6475 		goto out;
6476 	}
6477 
6478 	/* @p is still on $src_dsq and stable, determine the destination */
6479 	dst_dsq = find_dsq_for_dispatch(sch, this_rq, dsq_id, p);
6480 
6481 	/*
6482 	 * Apply vtime and slice updates before moving so that the new time is
6483 	 * visible before inserting into $dst_dsq. @p is still on $src_dsq but
6484 	 * this is safe as we're locking it.
6485 	 */
6486 	if (kit->cursor.flags & __SCX_DSQ_ITER_HAS_VTIME)
6487 		p->scx.dsq_vtime = kit->vtime;
6488 	if (kit->cursor.flags & __SCX_DSQ_ITER_HAS_SLICE)
6489 		p->scx.slice = kit->slice;
6490 
6491 	/* execute move */
6492 	locked_rq = move_task_between_dsqs(sch, p, enq_flags, src_dsq, dst_dsq);
6493 	dispatched = true;
6494 out:
6495 	if (in_balance) {
6496 		if (this_rq != locked_rq) {
6497 			raw_spin_rq_unlock(locked_rq);
6498 			raw_spin_rq_lock(this_rq);
6499 		}
6500 	} else {
6501 		raw_spin_rq_unlock_irqrestore(locked_rq, flags);
6502 	}
6503 
6504 	kit->cursor.flags &= ~(__SCX_DSQ_ITER_HAS_SLICE |
6505 			       __SCX_DSQ_ITER_HAS_VTIME);
6506 	return dispatched;
6507 }
6508 
6509 __bpf_kfunc_start_defs();
6510 
6511 /**
6512  * scx_bpf_dispatch_nr_slots - Return the number of remaining dispatch slots
6513  *
6514  * Can only be called from ops.dispatch().
6515  */
6516 __bpf_kfunc u32 scx_bpf_dispatch_nr_slots(void)
6517 {
6518 	if (!scx_kf_allowed(SCX_KF_DISPATCH))
6519 		return 0;
6520 
6521 	return scx_dsp_max_batch - __this_cpu_read(scx_dsp_ctx->cursor);
6522 }
6523 
6524 /**
6525  * scx_bpf_dispatch_cancel - Cancel the latest dispatch
6526  *
6527  * Cancel the latest dispatch. Can be called multiple times to cancel further
6528  * dispatches. Can only be called from ops.dispatch().
6529  */
6530 __bpf_kfunc void scx_bpf_dispatch_cancel(void)
6531 {
6532 	struct scx_dsp_ctx *dspc = this_cpu_ptr(scx_dsp_ctx);
6533 
6534 	if (!scx_kf_allowed(SCX_KF_DISPATCH))
6535 		return;
6536 
6537 	if (dspc->cursor > 0)
6538 		dspc->cursor--;
6539 	else
6540 		scx_kf_error("dispatch buffer underflow");
6541 }
6542 
6543 /**
6544  * scx_bpf_dsq_move_to_local - move a task from a DSQ to the current CPU's local DSQ
6545  * @dsq_id: DSQ to move task from
6546  *
6547  * Move a task from the non-local DSQ identified by @dsq_id to the current CPU's
6548  * local DSQ for execution. Can only be called from ops.dispatch().
6549  *
6550  * This function flushes the in-flight dispatches from scx_bpf_dsq_insert()
6551  * before trying to move from the specified DSQ. It may also grab rq locks and
6552  * thus can't be called under any BPF locks.
6553  *
6554  * Returns %true if a task has been moved, %false if there isn't any task to
6555  * move.
6556  */
6557 __bpf_kfunc bool scx_bpf_dsq_move_to_local(u64 dsq_id)
6558 {
6559 	struct scx_sched *sch = scx_root;
6560 	struct scx_dsp_ctx *dspc = this_cpu_ptr(scx_dsp_ctx);
6561 	struct scx_dispatch_q *dsq;
6562 
6563 	if (!scx_kf_allowed(SCX_KF_DISPATCH))
6564 		return false;
6565 
6566 	flush_dispatch_buf(sch, dspc->rq);
6567 
6568 	dsq = find_user_dsq(sch, dsq_id);
6569 	if (unlikely(!dsq)) {
6570 		scx_error(sch, "invalid DSQ ID 0x%016llx", dsq_id);
6571 		return false;
6572 	}
6573 
6574 	if (consume_dispatch_q(sch, dspc->rq, dsq)) {
6575 		/*
6576 		 * A successfully consumed task can be dequeued before it starts
6577 		 * running while the CPU is trying to migrate other dispatched
6578 		 * tasks. Bump nr_tasks to tell balance_scx() to retry on empty
6579 		 * local DSQ.
6580 		 */
6581 		dspc->nr_tasks++;
6582 		return true;
6583 	} else {
6584 		return false;
6585 	}
6586 }
6587 
6588 /* for backward compatibility, will be removed in v6.15 */
6589 __bpf_kfunc bool scx_bpf_consume(u64 dsq_id)
6590 {
6591 	printk_deferred_once(KERN_WARNING "sched_ext: scx_bpf_consume() renamed to scx_bpf_dsq_move_to_local()");
6592 	return scx_bpf_dsq_move_to_local(dsq_id);
6593 }
6594 
6595 /**
6596  * scx_bpf_dsq_move_set_slice - Override slice when moving between DSQs
6597  * @it__iter: DSQ iterator in progress
6598  * @slice: duration the moved task can run for in nsecs
6599  *
6600  * Override the slice of the next task that will be moved from @it__iter using
6601  * scx_bpf_dsq_move[_vtime](). If this function is not called, the previous
6602  * slice duration is kept.
6603  */
6604 __bpf_kfunc void scx_bpf_dsq_move_set_slice(struct bpf_iter_scx_dsq *it__iter,
6605 					    u64 slice)
6606 {
6607 	struct bpf_iter_scx_dsq_kern *kit = (void *)it__iter;
6608 
6609 	kit->slice = slice;
6610 	kit->cursor.flags |= __SCX_DSQ_ITER_HAS_SLICE;
6611 }
6612 
6613 /* for backward compatibility, will be removed in v6.15 */
6614 __bpf_kfunc void scx_bpf_dispatch_from_dsq_set_slice(
6615 			struct bpf_iter_scx_dsq *it__iter, u64 slice)
6616 {
6617 	printk_deferred_once(KERN_WARNING "sched_ext: scx_bpf_dispatch_from_dsq_set_slice() renamed to scx_bpf_dsq_move_set_slice()");
6618 	scx_bpf_dsq_move_set_slice(it__iter, slice);
6619 }
6620 
6621 /**
6622  * scx_bpf_dsq_move_set_vtime - Override vtime when moving between DSQs
6623  * @it__iter: DSQ iterator in progress
6624  * @vtime: task's ordering inside the vtime-sorted queue of the target DSQ
6625  *
6626  * Override the vtime of the next task that will be moved from @it__iter using
6627  * scx_bpf_dsq_move_vtime(). If this function is not called, the previous slice
6628  * vtime is kept. If scx_bpf_dsq_move() is used to dispatch the next task, the
6629  * override is ignored and cleared.
6630  */
6631 __bpf_kfunc void scx_bpf_dsq_move_set_vtime(struct bpf_iter_scx_dsq *it__iter,
6632 					    u64 vtime)
6633 {
6634 	struct bpf_iter_scx_dsq_kern *kit = (void *)it__iter;
6635 
6636 	kit->vtime = vtime;
6637 	kit->cursor.flags |= __SCX_DSQ_ITER_HAS_VTIME;
6638 }
6639 
6640 /* for backward compatibility, will be removed in v6.15 */
6641 __bpf_kfunc void scx_bpf_dispatch_from_dsq_set_vtime(
6642 			struct bpf_iter_scx_dsq *it__iter, u64 vtime)
6643 {
6644 	printk_deferred_once(KERN_WARNING "sched_ext: scx_bpf_dispatch_from_dsq_set_vtime() renamed to scx_bpf_dsq_move_set_vtime()");
6645 	scx_bpf_dsq_move_set_vtime(it__iter, vtime);
6646 }
6647 
6648 /**
6649  * scx_bpf_dsq_move - Move a task from DSQ iteration to a DSQ
6650  * @it__iter: DSQ iterator in progress
6651  * @p: task to transfer
6652  * @dsq_id: DSQ to move @p to
6653  * @enq_flags: SCX_ENQ_*
6654  *
6655  * Transfer @p which is on the DSQ currently iterated by @it__iter to the DSQ
6656  * specified by @dsq_id. All DSQs - local DSQs, global DSQ and user DSQs - can
6657  * be the destination.
6658  *
6659  * For the transfer to be successful, @p must still be on the DSQ and have been
6660  * queued before the DSQ iteration started. This function doesn't care whether
6661  * @p was obtained from the DSQ iteration. @p just has to be on the DSQ and have
6662  * been queued before the iteration started.
6663  *
6664  * @p's slice is kept by default. Use scx_bpf_dsq_move_set_slice() to update.
6665  *
6666  * Can be called from ops.dispatch() or any BPF context which doesn't hold a rq
6667  * lock (e.g. BPF timers or SYSCALL programs).
6668  *
6669  * Returns %true if @p has been consumed, %false if @p had already been consumed
6670  * or dequeued.
6671  */
6672 __bpf_kfunc bool scx_bpf_dsq_move(struct bpf_iter_scx_dsq *it__iter,
6673 				  struct task_struct *p, u64 dsq_id,
6674 				  u64 enq_flags)
6675 {
6676 	return scx_dsq_move((struct bpf_iter_scx_dsq_kern *)it__iter,
6677 			    p, dsq_id, enq_flags);
6678 }
6679 
6680 /* for backward compatibility, will be removed in v6.15 */
6681 __bpf_kfunc bool scx_bpf_dispatch_from_dsq(struct bpf_iter_scx_dsq *it__iter,
6682 					   struct task_struct *p, u64 dsq_id,
6683 					   u64 enq_flags)
6684 {
6685 	printk_deferred_once(KERN_WARNING "sched_ext: scx_bpf_dispatch_from_dsq() renamed to scx_bpf_dsq_move()");
6686 	return scx_bpf_dsq_move(it__iter, p, dsq_id, enq_flags);
6687 }
6688 
6689 /**
6690  * scx_bpf_dsq_move_vtime - Move a task from DSQ iteration to a PRIQ DSQ
6691  * @it__iter: DSQ iterator in progress
6692  * @p: task to transfer
6693  * @dsq_id: DSQ to move @p to
6694  * @enq_flags: SCX_ENQ_*
6695  *
6696  * Transfer @p which is on the DSQ currently iterated by @it__iter to the
6697  * priority queue of the DSQ specified by @dsq_id. The destination must be a
6698  * user DSQ as only user DSQs support priority queue.
6699  *
6700  * @p's slice and vtime are kept by default. Use scx_bpf_dsq_move_set_slice()
6701  * and scx_bpf_dsq_move_set_vtime() to update.
6702  *
6703  * All other aspects are identical to scx_bpf_dsq_move(). See
6704  * scx_bpf_dsq_insert_vtime() for more information on @vtime.
6705  */
6706 __bpf_kfunc bool scx_bpf_dsq_move_vtime(struct bpf_iter_scx_dsq *it__iter,
6707 					struct task_struct *p, u64 dsq_id,
6708 					u64 enq_flags)
6709 {
6710 	return scx_dsq_move((struct bpf_iter_scx_dsq_kern *)it__iter,
6711 			    p, dsq_id, enq_flags | SCX_ENQ_DSQ_PRIQ);
6712 }
6713 
6714 /* for backward compatibility, will be removed in v6.15 */
6715 __bpf_kfunc bool scx_bpf_dispatch_vtime_from_dsq(struct bpf_iter_scx_dsq *it__iter,
6716 						 struct task_struct *p, u64 dsq_id,
6717 						 u64 enq_flags)
6718 {
6719 	printk_deferred_once(KERN_WARNING "sched_ext: scx_bpf_dispatch_from_dsq_vtime() renamed to scx_bpf_dsq_move_vtime()");
6720 	return scx_bpf_dsq_move_vtime(it__iter, p, dsq_id, enq_flags);
6721 }
6722 
6723 __bpf_kfunc_end_defs();
6724 
6725 BTF_KFUNCS_START(scx_kfunc_ids_dispatch)
6726 BTF_ID_FLAGS(func, scx_bpf_dispatch_nr_slots)
6727 BTF_ID_FLAGS(func, scx_bpf_dispatch_cancel)
6728 BTF_ID_FLAGS(func, scx_bpf_dsq_move_to_local)
6729 BTF_ID_FLAGS(func, scx_bpf_consume)
6730 BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_slice)
6731 BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_vtime)
6732 BTF_ID_FLAGS(func, scx_bpf_dsq_move, KF_RCU)
6733 BTF_ID_FLAGS(func, scx_bpf_dsq_move_vtime, KF_RCU)
6734 BTF_ID_FLAGS(func, scx_bpf_dispatch_from_dsq_set_slice)
6735 BTF_ID_FLAGS(func, scx_bpf_dispatch_from_dsq_set_vtime)
6736 BTF_ID_FLAGS(func, scx_bpf_dispatch_from_dsq, KF_RCU)
6737 BTF_ID_FLAGS(func, scx_bpf_dispatch_vtime_from_dsq, KF_RCU)
6738 BTF_KFUNCS_END(scx_kfunc_ids_dispatch)
6739 
6740 static const struct btf_kfunc_id_set scx_kfunc_set_dispatch = {
6741 	.owner			= THIS_MODULE,
6742 	.set			= &scx_kfunc_ids_dispatch,
6743 };
6744 
6745 __bpf_kfunc_start_defs();
6746 
6747 /**
6748  * scx_bpf_reenqueue_local - Re-enqueue tasks on a local DSQ
6749  *
6750  * Iterate over all of the tasks currently enqueued on the local DSQ of the
6751  * caller's CPU, and re-enqueue them in the BPF scheduler. Returns the number of
6752  * processed tasks. Can only be called from ops.cpu_release().
6753  */
6754 __bpf_kfunc u32 scx_bpf_reenqueue_local(void)
6755 {
6756 	LIST_HEAD(tasks);
6757 	u32 nr_enqueued = 0;
6758 	struct rq *rq;
6759 	struct task_struct *p, *n;
6760 
6761 	if (!scx_kf_allowed(SCX_KF_CPU_RELEASE))
6762 		return 0;
6763 
6764 	rq = cpu_rq(smp_processor_id());
6765 	lockdep_assert_rq_held(rq);
6766 
6767 	/*
6768 	 * The BPF scheduler may choose to dispatch tasks back to
6769 	 * @rq->scx.local_dsq. Move all candidate tasks off to a private list
6770 	 * first to avoid processing the same tasks repeatedly.
6771 	 */
6772 	list_for_each_entry_safe(p, n, &rq->scx.local_dsq.list,
6773 				 scx.dsq_list.node) {
6774 		/*
6775 		 * If @p is being migrated, @p's current CPU may not agree with
6776 		 * its allowed CPUs and the migration_cpu_stop is about to
6777 		 * deactivate and re-activate @p anyway. Skip re-enqueueing.
6778 		 *
6779 		 * While racing sched property changes may also dequeue and
6780 		 * re-enqueue a migrating task while its current CPU and allowed
6781 		 * CPUs disagree, they use %ENQUEUE_RESTORE which is bypassed to
6782 		 * the current local DSQ for running tasks and thus are not
6783 		 * visible to the BPF scheduler.
6784 		 *
6785 		 * Also skip re-enqueueing tasks that can only run on this
6786 		 * CPU, as they would just be re-added to the same local
6787 		 * DSQ without any benefit.
6788 		 */
6789 		if (p->migration_pending || is_migration_disabled(p) || p->nr_cpus_allowed == 1)
6790 			continue;
6791 
6792 		dispatch_dequeue(rq, p);
6793 		list_add_tail(&p->scx.dsq_list.node, &tasks);
6794 	}
6795 
6796 	list_for_each_entry_safe(p, n, &tasks, scx.dsq_list.node) {
6797 		list_del_init(&p->scx.dsq_list.node);
6798 		do_enqueue_task(rq, p, SCX_ENQ_REENQ, -1);
6799 		nr_enqueued++;
6800 	}
6801 
6802 	return nr_enqueued;
6803 }
6804 
6805 __bpf_kfunc_end_defs();
6806 
6807 BTF_KFUNCS_START(scx_kfunc_ids_cpu_release)
6808 BTF_ID_FLAGS(func, scx_bpf_reenqueue_local)
6809 BTF_KFUNCS_END(scx_kfunc_ids_cpu_release)
6810 
6811 static const struct btf_kfunc_id_set scx_kfunc_set_cpu_release = {
6812 	.owner			= THIS_MODULE,
6813 	.set			= &scx_kfunc_ids_cpu_release,
6814 };
6815 
6816 __bpf_kfunc_start_defs();
6817 
6818 /**
6819  * scx_bpf_create_dsq - Create a custom DSQ
6820  * @dsq_id: DSQ to create
6821  * @node: NUMA node to allocate from
6822  *
6823  * Create a custom DSQ identified by @dsq_id. Can be called from any sleepable
6824  * scx callback, and any BPF_PROG_TYPE_SYSCALL prog.
6825  */
6826 __bpf_kfunc s32 scx_bpf_create_dsq(u64 dsq_id, s32 node)
6827 {
6828 	struct scx_dispatch_q *dsq;
6829 	struct scx_sched *sch;
6830 	s32 ret;
6831 
6832 	if (unlikely(node >= (int)nr_node_ids ||
6833 		     (node < 0 && node != NUMA_NO_NODE)))
6834 		return -EINVAL;
6835 
6836 	if (unlikely(dsq_id & SCX_DSQ_FLAG_BUILTIN))
6837 		return -EINVAL;
6838 
6839 	dsq = kmalloc_node(sizeof(*dsq), GFP_KERNEL, node);
6840 	if (!dsq)
6841 		return -ENOMEM;
6842 
6843 	init_dsq(dsq, dsq_id);
6844 
6845 	rcu_read_lock();
6846 
6847 	sch = rcu_dereference(scx_root);
6848 	if (sch)
6849 		ret = rhashtable_lookup_insert_fast(&sch->dsq_hash, &dsq->hash_node,
6850 						    dsq_hash_params);
6851 	else
6852 		ret = -ENODEV;
6853 
6854 	rcu_read_unlock();
6855 	if (ret)
6856 		kfree(dsq);
6857 	return ret;
6858 }
6859 
6860 __bpf_kfunc_end_defs();
6861 
6862 BTF_KFUNCS_START(scx_kfunc_ids_unlocked)
6863 BTF_ID_FLAGS(func, scx_bpf_create_dsq, KF_SLEEPABLE)
6864 BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_slice)
6865 BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_vtime)
6866 BTF_ID_FLAGS(func, scx_bpf_dsq_move, KF_RCU)
6867 BTF_ID_FLAGS(func, scx_bpf_dsq_move_vtime, KF_RCU)
6868 BTF_ID_FLAGS(func, scx_bpf_dispatch_from_dsq_set_slice)
6869 BTF_ID_FLAGS(func, scx_bpf_dispatch_from_dsq_set_vtime)
6870 BTF_ID_FLAGS(func, scx_bpf_dispatch_from_dsq, KF_RCU)
6871 BTF_ID_FLAGS(func, scx_bpf_dispatch_vtime_from_dsq, KF_RCU)
6872 BTF_KFUNCS_END(scx_kfunc_ids_unlocked)
6873 
6874 static const struct btf_kfunc_id_set scx_kfunc_set_unlocked = {
6875 	.owner			= THIS_MODULE,
6876 	.set			= &scx_kfunc_ids_unlocked,
6877 };
6878 
6879 __bpf_kfunc_start_defs();
6880 
6881 /**
6882  * scx_bpf_kick_cpu - Trigger reschedule on a CPU
6883  * @cpu: cpu to kick
6884  * @flags: %SCX_KICK_* flags
6885  *
6886  * Kick @cpu into rescheduling. This can be used to wake up an idle CPU or
6887  * trigger rescheduling on a busy CPU. This can be called from any online
6888  * scx_ops operation and the actual kicking is performed asynchronously through
6889  * an irq work.
6890  */
6891 __bpf_kfunc void scx_bpf_kick_cpu(s32 cpu, u64 flags)
6892 {
6893 	struct rq *this_rq;
6894 	unsigned long irq_flags;
6895 
6896 	if (!kf_cpu_valid(cpu, NULL))
6897 		return;
6898 
6899 	local_irq_save(irq_flags);
6900 
6901 	this_rq = this_rq();
6902 
6903 	/*
6904 	 * While bypassing for PM ops, IRQ handling may not be online which can
6905 	 * lead to irq_work_queue() malfunction such as infinite busy wait for
6906 	 * IRQ status update. Suppress kicking.
6907 	 */
6908 	if (scx_rq_bypassing(this_rq))
6909 		goto out;
6910 
6911 	/*
6912 	 * Actual kicking is bounced to kick_cpus_irq_workfn() to avoid nesting
6913 	 * rq locks. We can probably be smarter and avoid bouncing if called
6914 	 * from ops which don't hold a rq lock.
6915 	 */
6916 	if (flags & SCX_KICK_IDLE) {
6917 		struct rq *target_rq = cpu_rq(cpu);
6918 
6919 		if (unlikely(flags & (SCX_KICK_PREEMPT | SCX_KICK_WAIT)))
6920 			scx_kf_error("PREEMPT/WAIT cannot be used with SCX_KICK_IDLE");
6921 
6922 		if (raw_spin_rq_trylock(target_rq)) {
6923 			if (can_skip_idle_kick(target_rq)) {
6924 				raw_spin_rq_unlock(target_rq);
6925 				goto out;
6926 			}
6927 			raw_spin_rq_unlock(target_rq);
6928 		}
6929 		cpumask_set_cpu(cpu, this_rq->scx.cpus_to_kick_if_idle);
6930 	} else {
6931 		cpumask_set_cpu(cpu, this_rq->scx.cpus_to_kick);
6932 
6933 		if (flags & SCX_KICK_PREEMPT)
6934 			cpumask_set_cpu(cpu, this_rq->scx.cpus_to_preempt);
6935 		if (flags & SCX_KICK_WAIT)
6936 			cpumask_set_cpu(cpu, this_rq->scx.cpus_to_wait);
6937 	}
6938 
6939 	irq_work_queue(&this_rq->scx.kick_cpus_irq_work);
6940 out:
6941 	local_irq_restore(irq_flags);
6942 }
6943 
6944 /**
6945  * scx_bpf_dsq_nr_queued - Return the number of queued tasks
6946  * @dsq_id: id of the DSQ
6947  *
6948  * Return the number of tasks in the DSQ matching @dsq_id. If not found,
6949  * -%ENOENT is returned.
6950  */
6951 __bpf_kfunc s32 scx_bpf_dsq_nr_queued(u64 dsq_id)
6952 {
6953 	struct scx_sched *sch;
6954 	struct scx_dispatch_q *dsq;
6955 	s32 ret;
6956 
6957 	preempt_disable();
6958 
6959 	sch = rcu_dereference_sched(scx_root);
6960 	if (unlikely(!sch)) {
6961 		ret = -ENODEV;
6962 		goto out;
6963 	}
6964 
6965 	if (dsq_id == SCX_DSQ_LOCAL) {
6966 		ret = READ_ONCE(this_rq()->scx.local_dsq.nr);
6967 		goto out;
6968 	} else if ((dsq_id & SCX_DSQ_LOCAL_ON) == SCX_DSQ_LOCAL_ON) {
6969 		s32 cpu = dsq_id & SCX_DSQ_LOCAL_CPU_MASK;
6970 
6971 		if (ops_cpu_valid(sch, cpu, NULL)) {
6972 			ret = READ_ONCE(cpu_rq(cpu)->scx.local_dsq.nr);
6973 			goto out;
6974 		}
6975 	} else {
6976 		dsq = find_user_dsq(sch, dsq_id);
6977 		if (dsq) {
6978 			ret = READ_ONCE(dsq->nr);
6979 			goto out;
6980 		}
6981 	}
6982 	ret = -ENOENT;
6983 out:
6984 	preempt_enable();
6985 	return ret;
6986 }
6987 
6988 /**
6989  * scx_bpf_destroy_dsq - Destroy a custom DSQ
6990  * @dsq_id: DSQ to destroy
6991  *
6992  * Destroy the custom DSQ identified by @dsq_id. Only DSQs created with
6993  * scx_bpf_create_dsq() can be destroyed. The caller must ensure that the DSQ is
6994  * empty and no further tasks are dispatched to it. Ignored if called on a DSQ
6995  * which doesn't exist. Can be called from any online scx_ops operations.
6996  */
6997 __bpf_kfunc void scx_bpf_destroy_dsq(u64 dsq_id)
6998 {
6999 	struct scx_sched *sch;
7000 
7001 	rcu_read_lock();
7002 	sch = rcu_dereference(scx_root);
7003 	if (sch)
7004 		destroy_dsq(sch, dsq_id);
7005 	rcu_read_unlock();
7006 }
7007 
7008 /**
7009  * bpf_iter_scx_dsq_new - Create a DSQ iterator
7010  * @it: iterator to initialize
7011  * @dsq_id: DSQ to iterate
7012  * @flags: %SCX_DSQ_ITER_*
7013  *
7014  * Initialize BPF iterator @it which can be used with bpf_for_each() to walk
7015  * tasks in the DSQ specified by @dsq_id. Iteration using @it only includes
7016  * tasks which are already queued when this function is invoked.
7017  */
7018 __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id,
7019 				     u64 flags)
7020 {
7021 	struct bpf_iter_scx_dsq_kern *kit = (void *)it;
7022 	struct scx_sched *sch;
7023 
7024 	BUILD_BUG_ON(sizeof(struct bpf_iter_scx_dsq_kern) >
7025 		     sizeof(struct bpf_iter_scx_dsq));
7026 	BUILD_BUG_ON(__alignof__(struct bpf_iter_scx_dsq_kern) !=
7027 		     __alignof__(struct bpf_iter_scx_dsq));
7028 
7029 	/*
7030 	 * next() and destroy() will be called regardless of the return value.
7031 	 * Always clear $kit->dsq.
7032 	 */
7033 	kit->dsq = NULL;
7034 
7035 	sch = rcu_dereference_check(scx_root, rcu_read_lock_bh_held());
7036 	if (unlikely(!sch))
7037 		return -ENODEV;
7038 
7039 	if (flags & ~__SCX_DSQ_ITER_USER_FLAGS)
7040 		return -EINVAL;
7041 
7042 	kit->dsq = find_user_dsq(sch, dsq_id);
7043 	if (!kit->dsq)
7044 		return -ENOENT;
7045 
7046 	INIT_LIST_HEAD(&kit->cursor.node);
7047 	kit->cursor.flags = SCX_DSQ_LNODE_ITER_CURSOR | flags;
7048 	kit->cursor.priv = READ_ONCE(kit->dsq->seq);
7049 
7050 	return 0;
7051 }
7052 
7053 /**
7054  * bpf_iter_scx_dsq_next - Progress a DSQ iterator
7055  * @it: iterator to progress
7056  *
7057  * Return the next task. See bpf_iter_scx_dsq_new().
7058  */
7059 __bpf_kfunc struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it)
7060 {
7061 	struct bpf_iter_scx_dsq_kern *kit = (void *)it;
7062 	bool rev = kit->cursor.flags & SCX_DSQ_ITER_REV;
7063 	struct task_struct *p;
7064 	unsigned long flags;
7065 
7066 	if (!kit->dsq)
7067 		return NULL;
7068 
7069 	raw_spin_lock_irqsave(&kit->dsq->lock, flags);
7070 
7071 	if (list_empty(&kit->cursor.node))
7072 		p = NULL;
7073 	else
7074 		p = container_of(&kit->cursor, struct task_struct, scx.dsq_list);
7075 
7076 	/*
7077 	 * Only tasks which were queued before the iteration started are
7078 	 * visible. This bounds BPF iterations and guarantees that vtime never
7079 	 * jumps in the other direction while iterating.
7080 	 */
7081 	do {
7082 		p = nldsq_next_task(kit->dsq, p, rev);
7083 	} while (p && unlikely(u32_before(kit->cursor.priv, p->scx.dsq_seq)));
7084 
7085 	if (p) {
7086 		if (rev)
7087 			list_move_tail(&kit->cursor.node, &p->scx.dsq_list.node);
7088 		else
7089 			list_move(&kit->cursor.node, &p->scx.dsq_list.node);
7090 	} else {
7091 		list_del_init(&kit->cursor.node);
7092 	}
7093 
7094 	raw_spin_unlock_irqrestore(&kit->dsq->lock, flags);
7095 
7096 	return p;
7097 }
7098 
7099 /**
7100  * bpf_iter_scx_dsq_destroy - Destroy a DSQ iterator
7101  * @it: iterator to destroy
7102  *
7103  * Undo scx_iter_scx_dsq_new().
7104  */
7105 __bpf_kfunc void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it)
7106 {
7107 	struct bpf_iter_scx_dsq_kern *kit = (void *)it;
7108 
7109 	if (!kit->dsq)
7110 		return;
7111 
7112 	if (!list_empty(&kit->cursor.node)) {
7113 		unsigned long flags;
7114 
7115 		raw_spin_lock_irqsave(&kit->dsq->lock, flags);
7116 		list_del_init(&kit->cursor.node);
7117 		raw_spin_unlock_irqrestore(&kit->dsq->lock, flags);
7118 	}
7119 	kit->dsq = NULL;
7120 }
7121 
7122 __bpf_kfunc_end_defs();
7123 
7124 static s32 __bstr_format(u64 *data_buf, char *line_buf, size_t line_size,
7125 			 char *fmt, unsigned long long *data, u32 data__sz)
7126 {
7127 	struct bpf_bprintf_data bprintf_data = { .get_bin_args = true };
7128 	s32 ret;
7129 
7130 	if (data__sz % 8 || data__sz > MAX_BPRINTF_VARARGS * 8 ||
7131 	    (data__sz && !data)) {
7132 		scx_kf_error("invalid data=%p and data__sz=%u", (void *)data, data__sz);
7133 		return -EINVAL;
7134 	}
7135 
7136 	ret = copy_from_kernel_nofault(data_buf, data, data__sz);
7137 	if (ret < 0) {
7138 		scx_kf_error("failed to read data fields (%d)", ret);
7139 		return ret;
7140 	}
7141 
7142 	ret = bpf_bprintf_prepare(fmt, UINT_MAX, data_buf, data__sz / 8,
7143 				  &bprintf_data);
7144 	if (ret < 0) {
7145 		scx_kf_error("format preparation failed (%d)", ret);
7146 		return ret;
7147 	}
7148 
7149 	ret = bstr_printf(line_buf, line_size, fmt,
7150 			  bprintf_data.bin_args);
7151 	bpf_bprintf_cleanup(&bprintf_data);
7152 	if (ret < 0) {
7153 		scx_kf_error("(\"%s\", %p, %u) failed to format", fmt, data, data__sz);
7154 		return ret;
7155 	}
7156 
7157 	return ret;
7158 }
7159 
7160 static s32 bstr_format(struct scx_bstr_buf *buf,
7161 		       char *fmt, unsigned long long *data, u32 data__sz)
7162 {
7163 	return __bstr_format(buf->data, buf->line, sizeof(buf->line),
7164 			     fmt, data, data__sz);
7165 }
7166 
7167 __bpf_kfunc_start_defs();
7168 
7169 /**
7170  * scx_bpf_exit_bstr - Gracefully exit the BPF scheduler.
7171  * @exit_code: Exit value to pass to user space via struct scx_exit_info.
7172  * @fmt: error message format string
7173  * @data: format string parameters packaged using ___bpf_fill() macro
7174  * @data__sz: @data len, must end in '__sz' for the verifier
7175  *
7176  * Indicate that the BPF scheduler wants to exit gracefully, and initiate ops
7177  * disabling.
7178  */
7179 __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
7180 				   unsigned long long *data, u32 data__sz)
7181 {
7182 	unsigned long flags;
7183 
7184 	raw_spin_lock_irqsave(&scx_exit_bstr_buf_lock, flags);
7185 	if (bstr_format(&scx_exit_bstr_buf, fmt, data, data__sz) >= 0)
7186 		scx_kf_exit(SCX_EXIT_UNREG_BPF, exit_code, "%s", scx_exit_bstr_buf.line);
7187 	raw_spin_unlock_irqrestore(&scx_exit_bstr_buf_lock, flags);
7188 }
7189 
7190 /**
7191  * scx_bpf_error_bstr - Indicate fatal error
7192  * @fmt: error message format string
7193  * @data: format string parameters packaged using ___bpf_fill() macro
7194  * @data__sz: @data len, must end in '__sz' for the verifier
7195  *
7196  * Indicate that the BPF scheduler encountered a fatal error and initiate ops
7197  * disabling.
7198  */
7199 __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
7200 				    u32 data__sz)
7201 {
7202 	unsigned long flags;
7203 
7204 	raw_spin_lock_irqsave(&scx_exit_bstr_buf_lock, flags);
7205 	if (bstr_format(&scx_exit_bstr_buf, fmt, data, data__sz) >= 0)
7206 		scx_kf_exit(SCX_EXIT_ERROR_BPF, 0, "%s", scx_exit_bstr_buf.line);
7207 	raw_spin_unlock_irqrestore(&scx_exit_bstr_buf_lock, flags);
7208 }
7209 
7210 /**
7211  * scx_bpf_dump_bstr - Generate extra debug dump specific to the BPF scheduler
7212  * @fmt: format string
7213  * @data: format string parameters packaged using ___bpf_fill() macro
7214  * @data__sz: @data len, must end in '__sz' for the verifier
7215  *
7216  * To be called through scx_bpf_dump() helper from ops.dump(), dump_cpu() and
7217  * dump_task() to generate extra debug dump specific to the BPF scheduler.
7218  *
7219  * The extra dump may be multiple lines. A single line may be split over
7220  * multiple calls. The last line is automatically terminated.
7221  */
7222 __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data,
7223 				   u32 data__sz)
7224 {
7225 	struct scx_dump_data *dd = &scx_dump_data;
7226 	struct scx_bstr_buf *buf = &dd->buf;
7227 	s32 ret;
7228 
7229 	if (raw_smp_processor_id() != dd->cpu) {
7230 		scx_kf_error("scx_bpf_dump() must only be called from ops.dump() and friends");
7231 		return;
7232 	}
7233 
7234 	/* append the formatted string to the line buf */
7235 	ret = __bstr_format(buf->data, buf->line + dd->cursor,
7236 			    sizeof(buf->line) - dd->cursor, fmt, data, data__sz);
7237 	if (ret < 0) {
7238 		dump_line(dd->s, "%s[!] (\"%s\", %p, %u) failed to format (%d)",
7239 			  dd->prefix, fmt, data, data__sz, ret);
7240 		return;
7241 	}
7242 
7243 	dd->cursor += ret;
7244 	dd->cursor = min_t(s32, dd->cursor, sizeof(buf->line));
7245 
7246 	if (!dd->cursor)
7247 		return;
7248 
7249 	/*
7250 	 * If the line buf overflowed or ends in a newline, flush it into the
7251 	 * dump. This is to allow the caller to generate a single line over
7252 	 * multiple calls. As ops_dump_flush() can also handle multiple lines in
7253 	 * the line buf, the only case which can lead to an unexpected
7254 	 * truncation is when the caller keeps generating newlines in the middle
7255 	 * instead of the end consecutively. Don't do that.
7256 	 */
7257 	if (dd->cursor >= sizeof(buf->line) || buf->line[dd->cursor - 1] == '\n')
7258 		ops_dump_flush();
7259 }
7260 
7261 /**
7262  * scx_bpf_cpuperf_cap - Query the maximum relative capacity of a CPU
7263  * @cpu: CPU of interest
7264  *
7265  * Return the maximum relative capacity of @cpu in relation to the most
7266  * performant CPU in the system. The return value is in the range [1,
7267  * %SCX_CPUPERF_ONE]. See scx_bpf_cpuperf_cur().
7268  */
7269 __bpf_kfunc u32 scx_bpf_cpuperf_cap(s32 cpu)
7270 {
7271 	if (kf_cpu_valid(cpu, NULL))
7272 		return arch_scale_cpu_capacity(cpu);
7273 	else
7274 		return SCX_CPUPERF_ONE;
7275 }
7276 
7277 /**
7278  * scx_bpf_cpuperf_cur - Query the current relative performance of a CPU
7279  * @cpu: CPU of interest
7280  *
7281  * Return the current relative performance of @cpu in relation to its maximum.
7282  * The return value is in the range [1, %SCX_CPUPERF_ONE].
7283  *
7284  * The current performance level of a CPU in relation to the maximum performance
7285  * available in the system can be calculated as follows:
7286  *
7287  *   scx_bpf_cpuperf_cap() * scx_bpf_cpuperf_cur() / %SCX_CPUPERF_ONE
7288  *
7289  * The result is in the range [1, %SCX_CPUPERF_ONE].
7290  */
7291 __bpf_kfunc u32 scx_bpf_cpuperf_cur(s32 cpu)
7292 {
7293 	if (kf_cpu_valid(cpu, NULL))
7294 		return arch_scale_freq_capacity(cpu);
7295 	else
7296 		return SCX_CPUPERF_ONE;
7297 }
7298 
7299 /**
7300  * scx_bpf_cpuperf_set - Set the relative performance target of a CPU
7301  * @cpu: CPU of interest
7302  * @perf: target performance level [0, %SCX_CPUPERF_ONE]
7303  *
7304  * Set the target performance level of @cpu to @perf. @perf is in linear
7305  * relative scale between 0 and %SCX_CPUPERF_ONE. This determines how the
7306  * schedutil cpufreq governor chooses the target frequency.
7307  *
7308  * The actual performance level chosen, CPU grouping, and the overhead and
7309  * latency of the operations are dependent on the hardware and cpufreq driver in
7310  * use. Consult hardware and cpufreq documentation for more information. The
7311  * current performance level can be monitored using scx_bpf_cpuperf_cur().
7312  */
7313 __bpf_kfunc void scx_bpf_cpuperf_set(s32 cpu, u32 perf)
7314 {
7315 	if (unlikely(perf > SCX_CPUPERF_ONE)) {
7316 		scx_kf_error("Invalid cpuperf target %u for CPU %d", perf, cpu);
7317 		return;
7318 	}
7319 
7320 	if (kf_cpu_valid(cpu, NULL)) {
7321 		struct rq *rq = cpu_rq(cpu), *locked_rq = scx_locked_rq();
7322 		struct rq_flags rf;
7323 
7324 		/*
7325 		 * When called with an rq lock held, restrict the operation
7326 		 * to the corresponding CPU to prevent ABBA deadlocks.
7327 		 */
7328 		if (locked_rq && rq != locked_rq) {
7329 			scx_kf_error("Invalid target CPU %d", cpu);
7330 			return;
7331 		}
7332 
7333 		/*
7334 		 * If no rq lock is held, allow to operate on any CPU by
7335 		 * acquiring the corresponding rq lock.
7336 		 */
7337 		if (!locked_rq) {
7338 			rq_lock_irqsave(rq, &rf);
7339 			update_rq_clock(rq);
7340 		}
7341 
7342 		rq->scx.cpuperf_target = perf;
7343 		cpufreq_update_util(rq, 0);
7344 
7345 		if (!locked_rq)
7346 			rq_unlock_irqrestore(rq, &rf);
7347 	}
7348 }
7349 
7350 /**
7351  * scx_bpf_nr_node_ids - Return the number of possible node IDs
7352  *
7353  * All valid node IDs in the system are smaller than the returned value.
7354  */
7355 __bpf_kfunc u32 scx_bpf_nr_node_ids(void)
7356 {
7357 	return nr_node_ids;
7358 }
7359 
7360 /**
7361  * scx_bpf_nr_cpu_ids - Return the number of possible CPU IDs
7362  *
7363  * All valid CPU IDs in the system are smaller than the returned value.
7364  */
7365 __bpf_kfunc u32 scx_bpf_nr_cpu_ids(void)
7366 {
7367 	return nr_cpu_ids;
7368 }
7369 
7370 /**
7371  * scx_bpf_get_possible_cpumask - Get a referenced kptr to cpu_possible_mask
7372  */
7373 __bpf_kfunc const struct cpumask *scx_bpf_get_possible_cpumask(void)
7374 {
7375 	return cpu_possible_mask;
7376 }
7377 
7378 /**
7379  * scx_bpf_get_online_cpumask - Get a referenced kptr to cpu_online_mask
7380  */
7381 __bpf_kfunc const struct cpumask *scx_bpf_get_online_cpumask(void)
7382 {
7383 	return cpu_online_mask;
7384 }
7385 
7386 /**
7387  * scx_bpf_put_cpumask - Release a possible/online cpumask
7388  * @cpumask: cpumask to release
7389  */
7390 __bpf_kfunc void scx_bpf_put_cpumask(const struct cpumask *cpumask)
7391 {
7392 	/*
7393 	 * Empty function body because we aren't actually acquiring or releasing
7394 	 * a reference to a global cpumask, which is read-only in the caller and
7395 	 * is never released. The acquire / release semantics here are just used
7396 	 * to make the cpumask is a trusted pointer in the caller.
7397 	 */
7398 }
7399 
7400 /**
7401  * scx_bpf_task_running - Is task currently running?
7402  * @p: task of interest
7403  */
7404 __bpf_kfunc bool scx_bpf_task_running(const struct task_struct *p)
7405 {
7406 	return task_rq(p)->curr == p;
7407 }
7408 
7409 /**
7410  * scx_bpf_task_cpu - CPU a task is currently associated with
7411  * @p: task of interest
7412  */
7413 __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p)
7414 {
7415 	return task_cpu(p);
7416 }
7417 
7418 /**
7419  * scx_bpf_cpu_rq - Fetch the rq of a CPU
7420  * @cpu: CPU of the rq
7421  */
7422 __bpf_kfunc struct rq *scx_bpf_cpu_rq(s32 cpu)
7423 {
7424 	if (!kf_cpu_valid(cpu, NULL))
7425 		return NULL;
7426 
7427 	return cpu_rq(cpu);
7428 }
7429 
7430 /**
7431  * scx_bpf_task_cgroup - Return the sched cgroup of a task
7432  * @p: task of interest
7433  *
7434  * @p->sched_task_group->css.cgroup represents the cgroup @p is associated with
7435  * from the scheduler's POV. SCX operations should use this function to
7436  * determine @p's current cgroup as, unlike following @p->cgroups,
7437  * @p->sched_task_group is protected by @p's rq lock and thus atomic w.r.t. all
7438  * rq-locked operations. Can be called on the parameter tasks of rq-locked
7439  * operations. The restriction guarantees that @p's rq is locked by the caller.
7440  */
7441 #ifdef CONFIG_CGROUP_SCHED
7442 __bpf_kfunc struct cgroup *scx_bpf_task_cgroup(struct task_struct *p)
7443 {
7444 	struct task_group *tg = p->sched_task_group;
7445 	struct cgroup *cgrp = &cgrp_dfl_root.cgrp;
7446 
7447 	if (!scx_kf_allowed_on_arg_tasks(__SCX_KF_RQ_LOCKED, p))
7448 		goto out;
7449 
7450 	cgrp = tg_cgrp(tg);
7451 
7452 out:
7453 	cgroup_get(cgrp);
7454 	return cgrp;
7455 }
7456 #endif
7457 
7458 /**
7459  * scx_bpf_now - Returns a high-performance monotonically non-decreasing
7460  * clock for the current CPU. The clock returned is in nanoseconds.
7461  *
7462  * It provides the following properties:
7463  *
7464  * 1) High performance: Many BPF schedulers call bpf_ktime_get_ns() frequently
7465  *  to account for execution time and track tasks' runtime properties.
7466  *  Unfortunately, in some hardware platforms, bpf_ktime_get_ns() -- which
7467  *  eventually reads a hardware timestamp counter -- is neither performant nor
7468  *  scalable. scx_bpf_now() aims to provide a high-performance clock by
7469  *  using the rq clock in the scheduler core whenever possible.
7470  *
7471  * 2) High enough resolution for the BPF scheduler use cases: In most BPF
7472  *  scheduler use cases, the required clock resolution is lower than the most
7473  *  accurate hardware clock (e.g., rdtsc in x86). scx_bpf_now() basically
7474  *  uses the rq clock in the scheduler core whenever it is valid. It considers
7475  *  that the rq clock is valid from the time the rq clock is updated
7476  *  (update_rq_clock) until the rq is unlocked (rq_unpin_lock).
7477  *
7478  * 3) Monotonically non-decreasing clock for the same CPU: scx_bpf_now()
7479  *  guarantees the clock never goes backward when comparing them in the same
7480  *  CPU. On the other hand, when comparing clocks in different CPUs, there
7481  *  is no such guarantee -- the clock can go backward. It provides a
7482  *  monotonically *non-decreasing* clock so that it would provide the same
7483  *  clock values in two different scx_bpf_now() calls in the same CPU
7484  *  during the same period of when the rq clock is valid.
7485  */
7486 __bpf_kfunc u64 scx_bpf_now(void)
7487 {
7488 	struct rq *rq;
7489 	u64 clock;
7490 
7491 	preempt_disable();
7492 
7493 	rq = this_rq();
7494 	if (smp_load_acquire(&rq->scx.flags) & SCX_RQ_CLK_VALID) {
7495 		/*
7496 		 * If the rq clock is valid, use the cached rq clock.
7497 		 *
7498 		 * Note that scx_bpf_now() is re-entrant between a process
7499 		 * context and an interrupt context (e.g., timer interrupt).
7500 		 * However, we don't need to consider the race between them
7501 		 * because such race is not observable from a caller.
7502 		 */
7503 		clock = READ_ONCE(rq->scx.clock);
7504 	} else {
7505 		/*
7506 		 * Otherwise, return a fresh rq clock.
7507 		 *
7508 		 * The rq clock is updated outside of the rq lock.
7509 		 * In this case, keep the updated rq clock invalid so the next
7510 		 * kfunc call outside the rq lock gets a fresh rq clock.
7511 		 */
7512 		clock = sched_clock_cpu(cpu_of(rq));
7513 	}
7514 
7515 	preempt_enable();
7516 
7517 	return clock;
7518 }
7519 
7520 static void scx_read_events(struct scx_sched *sch, struct scx_event_stats *events)
7521 {
7522 	struct scx_event_stats *e_cpu;
7523 	int cpu;
7524 
7525 	/* Aggregate per-CPU event counters into @events. */
7526 	memset(events, 0, sizeof(*events));
7527 	for_each_possible_cpu(cpu) {
7528 		e_cpu = per_cpu_ptr(sch->event_stats_cpu, cpu);
7529 		scx_agg_event(events, e_cpu, SCX_EV_SELECT_CPU_FALLBACK);
7530 		scx_agg_event(events, e_cpu, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE);
7531 		scx_agg_event(events, e_cpu, SCX_EV_DISPATCH_KEEP_LAST);
7532 		scx_agg_event(events, e_cpu, SCX_EV_ENQ_SKIP_EXITING);
7533 		scx_agg_event(events, e_cpu, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED);
7534 		scx_agg_event(events, e_cpu, SCX_EV_REFILL_SLICE_DFL);
7535 		scx_agg_event(events, e_cpu, SCX_EV_BYPASS_DURATION);
7536 		scx_agg_event(events, e_cpu, SCX_EV_BYPASS_DISPATCH);
7537 		scx_agg_event(events, e_cpu, SCX_EV_BYPASS_ACTIVATE);
7538 	}
7539 }
7540 
7541 /*
7542  * scx_bpf_events - Get a system-wide event counter to
7543  * @events: output buffer from a BPF program
7544  * @events__sz: @events len, must end in '__sz'' for the verifier
7545  */
7546 __bpf_kfunc void scx_bpf_events(struct scx_event_stats *events,
7547 				size_t events__sz)
7548 {
7549 	struct scx_sched *sch;
7550 	struct scx_event_stats e_sys;
7551 
7552 	rcu_read_lock();
7553 	sch = rcu_dereference(scx_root);
7554 	if (sch)
7555 		scx_read_events(sch, &e_sys);
7556 	else
7557 		memset(&e_sys, 0, sizeof(e_sys));
7558 	rcu_read_unlock();
7559 
7560 	/*
7561 	 * We cannot entirely trust a BPF-provided size since a BPF program
7562 	 * might be compiled against a different vmlinux.h, of which
7563 	 * scx_event_stats would be larger (a newer vmlinux.h) or smaller
7564 	 * (an older vmlinux.h). Hence, we use the smaller size to avoid
7565 	 * memory corruption.
7566 	 */
7567 	events__sz = min(events__sz, sizeof(*events));
7568 	memcpy(events, &e_sys, events__sz);
7569 }
7570 
7571 __bpf_kfunc_end_defs();
7572 
7573 BTF_KFUNCS_START(scx_kfunc_ids_any)
7574 BTF_ID_FLAGS(func, scx_bpf_kick_cpu)
7575 BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued)
7576 BTF_ID_FLAGS(func, scx_bpf_destroy_dsq)
7577 BTF_ID_FLAGS(func, bpf_iter_scx_dsq_new, KF_ITER_NEW | KF_RCU_PROTECTED)
7578 BTF_ID_FLAGS(func, bpf_iter_scx_dsq_next, KF_ITER_NEXT | KF_RET_NULL)
7579 BTF_ID_FLAGS(func, bpf_iter_scx_dsq_destroy, KF_ITER_DESTROY)
7580 BTF_ID_FLAGS(func, scx_bpf_exit_bstr, KF_TRUSTED_ARGS)
7581 BTF_ID_FLAGS(func, scx_bpf_error_bstr, KF_TRUSTED_ARGS)
7582 BTF_ID_FLAGS(func, scx_bpf_dump_bstr, KF_TRUSTED_ARGS)
7583 BTF_ID_FLAGS(func, scx_bpf_cpuperf_cap)
7584 BTF_ID_FLAGS(func, scx_bpf_cpuperf_cur)
7585 BTF_ID_FLAGS(func, scx_bpf_cpuperf_set)
7586 BTF_ID_FLAGS(func, scx_bpf_nr_node_ids)
7587 BTF_ID_FLAGS(func, scx_bpf_nr_cpu_ids)
7588 BTF_ID_FLAGS(func, scx_bpf_get_possible_cpumask, KF_ACQUIRE)
7589 BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE)
7590 BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE)
7591 BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU)
7592 BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU)
7593 BTF_ID_FLAGS(func, scx_bpf_cpu_rq)
7594 #ifdef CONFIG_CGROUP_SCHED
7595 BTF_ID_FLAGS(func, scx_bpf_task_cgroup, KF_RCU | KF_ACQUIRE)
7596 #endif
7597 BTF_ID_FLAGS(func, scx_bpf_now)
7598 BTF_ID_FLAGS(func, scx_bpf_events, KF_TRUSTED_ARGS)
7599 BTF_KFUNCS_END(scx_kfunc_ids_any)
7600 
7601 static const struct btf_kfunc_id_set scx_kfunc_set_any = {
7602 	.owner			= THIS_MODULE,
7603 	.set			= &scx_kfunc_ids_any,
7604 };
7605 
7606 static int __init scx_init(void)
7607 {
7608 	int ret;
7609 
7610 	/*
7611 	 * kfunc registration can't be done from init_sched_ext_class() as
7612 	 * register_btf_kfunc_id_set() needs most of the system to be up.
7613 	 *
7614 	 * Some kfuncs are context-sensitive and can only be called from
7615 	 * specific SCX ops. They are grouped into BTF sets accordingly.
7616 	 * Unfortunately, BPF currently doesn't have a way of enforcing such
7617 	 * restrictions. Eventually, the verifier should be able to enforce
7618 	 * them. For now, register them the same and make each kfunc explicitly
7619 	 * check using scx_kf_allowed().
7620 	 */
7621 	if ((ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
7622 					     &scx_kfunc_set_enqueue_dispatch)) ||
7623 	    (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
7624 					     &scx_kfunc_set_dispatch)) ||
7625 	    (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
7626 					     &scx_kfunc_set_cpu_release)) ||
7627 	    (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
7628 					     &scx_kfunc_set_unlocked)) ||
7629 	    (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
7630 					     &scx_kfunc_set_unlocked)) ||
7631 	    (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
7632 					     &scx_kfunc_set_any)) ||
7633 	    (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
7634 					     &scx_kfunc_set_any)) ||
7635 	    (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
7636 					     &scx_kfunc_set_any))) {
7637 		pr_err("sched_ext: Failed to register kfunc sets (%d)\n", ret);
7638 		return ret;
7639 	}
7640 
7641 	ret = scx_idle_init();
7642 	if (ret) {
7643 		pr_err("sched_ext: Failed to initialize idle tracking (%d)\n", ret);
7644 		return ret;
7645 	}
7646 
7647 	ret = register_bpf_struct_ops(&bpf_sched_ext_ops, sched_ext_ops);
7648 	if (ret) {
7649 		pr_err("sched_ext: Failed to register struct_ops (%d)\n", ret);
7650 		return ret;
7651 	}
7652 
7653 	ret = register_pm_notifier(&scx_pm_notifier);
7654 	if (ret) {
7655 		pr_err("sched_ext: Failed to register PM notifier (%d)\n", ret);
7656 		return ret;
7657 	}
7658 
7659 	scx_kset = kset_create_and_add("sched_ext", &scx_uevent_ops, kernel_kobj);
7660 	if (!scx_kset) {
7661 		pr_err("sched_ext: Failed to create /sys/kernel/sched_ext\n");
7662 		return -ENOMEM;
7663 	}
7664 
7665 	ret = sysfs_create_group(&scx_kset->kobj, &scx_global_attr_group);
7666 	if (ret < 0) {
7667 		pr_err("sched_ext: Failed to add global attributes\n");
7668 		return ret;
7669 	}
7670 
7671 	return 0;
7672 }
7673 __initcall(scx_init);
7674