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