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