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