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) 2022 Meta Platforms, Inc. and affiliates. 6bba2c361STejun Heo * Copyright (c) 2022 Tejun Heo <tj@kernel.org> 7bba2c361STejun Heo * Copyright (c) 2022 David Vernet <dvernet@meta.com> 8bba2c361STejun Heo */ 9*3cd1f76bSTejun Heo #include <linux/bitmap.h> 10*3cd1f76bSTejun Heo #include <linux/btf_ids.h> 11*3cd1f76bSTejun Heo #include <linux/rhashtable.h> 12*3cd1f76bSTejun Heo #include <linux/sched/clock.h> 13*3cd1f76bSTejun Heo #include <linux/sched/isolation.h> 14*3cd1f76bSTejun Heo #include <linux/suspend.h> 15*3cd1f76bSTejun Heo #include <linux/sysrq.h> 16*3cd1f76bSTejun Heo 17*3cd1f76bSTejun Heo #include "../pelt.h" 18*3cd1f76bSTejun Heo #include "internal.h" 19*3cd1f76bSTejun Heo #include "cid.h" 20*3cd1f76bSTejun Heo #include "arena.h" 21*3cd1f76bSTejun Heo #include "idle.h" 22bba2c361STejun Heo 23bba2c361STejun Heo static DEFINE_RAW_SPINLOCK(scx_sched_lock); 24bba2c361STejun Heo 25bba2c361STejun Heo /* 26bba2c361STejun Heo * NOTE: sched_ext is in the process of growing multiple scheduler support and 27bba2c361STejun Heo * scx_root usage is in a transitional state. Naked dereferences are safe if the 28bba2c361STejun Heo * caller is one of the tasks attached to SCX and explicit RCU dereference is 29bba2c361STejun Heo * necessary otherwise. Naked scx_root dereferences trigger sparse warnings but 30bba2c361STejun Heo * are used as temporary markers to indicate that the dereferences need to be 31bba2c361STejun Heo * updated to point to the associated scheduler instances rather than scx_root. 32bba2c361STejun Heo */ 33bba2c361STejun Heo struct scx_sched __rcu *scx_root; 34bba2c361STejun Heo 35bba2c361STejun Heo /* 36bba2c361STejun Heo * All scheds, writers must hold both scx_enable_mutex and scx_sched_lock. 37bba2c361STejun Heo * Readers can hold either or rcu_read_lock(). 38bba2c361STejun Heo */ 39bba2c361STejun Heo static LIST_HEAD(scx_sched_all); 40bba2c361STejun Heo 41bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 42bba2c361STejun Heo static const struct rhashtable_params scx_sched_hash_params = { 43bba2c361STejun Heo .key_len = sizeof_field(struct scx_sched, ops.sub_cgroup_id), 44bba2c361STejun Heo .key_offset = offsetof(struct scx_sched, ops.sub_cgroup_id), 45bba2c361STejun Heo .head_offset = offsetof(struct scx_sched, hash_node), 46bba2c361STejun Heo .insecure_elasticity = true, /* inserted under scx_sched_lock */ 47bba2c361STejun Heo }; 48bba2c361STejun Heo 49bba2c361STejun Heo static struct rhashtable scx_sched_hash; 50bba2c361STejun Heo #endif 51bba2c361STejun Heo 52bba2c361STejun Heo /* see SCX_OPS_TID_TO_TASK */ 53bba2c361STejun Heo static const struct rhashtable_params scx_tid_hash_params = { 54bba2c361STejun Heo .key_len = sizeof_field(struct sched_ext_entity, tid), 55bba2c361STejun Heo .key_offset = offsetof(struct sched_ext_entity, tid), 56bba2c361STejun Heo .head_offset = offsetof(struct sched_ext_entity, tid_hash_node), 57bba2c361STejun Heo .insecure_elasticity = true, /* inserted/removed under scx_tasks_lock */ 58bba2c361STejun Heo }; 59bba2c361STejun Heo static struct rhashtable scx_tid_hash; 60bba2c361STejun Heo 61bba2c361STejun Heo /* 62bba2c361STejun Heo * During exit, a task may schedule after losing its PIDs. When disabling the 63bba2c361STejun Heo * BPF scheduler, we need to be able to iterate tasks in every state to 64bba2c361STejun Heo * guarantee system safety. Maintain a dedicated task list which contains every 65bba2c361STejun Heo * task between its fork and eventual free. 66bba2c361STejun Heo */ 67bba2c361STejun Heo static DEFINE_RAW_SPINLOCK(scx_tasks_lock); 68bba2c361STejun Heo static LIST_HEAD(scx_tasks); 69bba2c361STejun Heo 70bba2c361STejun Heo /* ops enable/disable */ 71bba2c361STejun Heo static DEFINE_MUTEX(scx_enable_mutex); 72bba2c361STejun Heo DEFINE_STATIC_KEY_FALSE(__scx_enabled); 73bba2c361STejun Heo DEFINE_STATIC_PERCPU_RWSEM(scx_fork_rwsem); 74bba2c361STejun Heo static atomic_t scx_enable_state_var = ATOMIC_INIT(SCX_DISABLED); 75bba2c361STejun Heo static DEFINE_RAW_SPINLOCK(scx_bypass_lock); 76bba2c361STejun Heo static bool scx_init_task_enabled; 77bba2c361STejun Heo static bool scx_switching_all; 78bba2c361STejun Heo DEFINE_STATIC_KEY_FALSE(__scx_switched_all); 79bba2c361STejun Heo static DEFINE_STATIC_KEY_FALSE(__scx_tid_to_task_enabled); 80bba2c361STejun Heo 81bba2c361STejun Heo /* 82bba2c361STejun Heo * True once SCX_OPS_TID_TO_TASK has been negotiated with the root scheduler 83bba2c361STejun Heo * and the tid->task table is live. Wraps the static key so callers don't 84bba2c361STejun Heo * take the address, and hints "likely enabled" for the common case where 85bba2c361STejun Heo * the feature is in use. 86bba2c361STejun Heo */ 87bba2c361STejun Heo static inline bool scx_tid_to_task_enabled(void) 88bba2c361STejun Heo { 89bba2c361STejun Heo return static_branch_likely(&__scx_tid_to_task_enabled); 90bba2c361STejun Heo } 91bba2c361STejun Heo 92bba2c361STejun Heo static atomic_long_t scx_nr_rejected = ATOMIC_LONG_INIT(0); 93bba2c361STejun Heo static atomic_long_t scx_hotplug_seq = ATOMIC_LONG_INIT(0); 94bba2c361STejun Heo 95bba2c361STejun Heo /* Global cursor for the per-CPU tid allocator. Starts at 1; tid 0 is reserved. */ 96bba2c361STejun Heo static atomic64_t scx_tid_cursor = ATOMIC64_INIT(1); 97bba2c361STejun Heo 98bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 99bba2c361STejun Heo /* 100bba2c361STejun Heo * The sub sched being enabled. Used by scx_disable_and_exit_task() to exit 101bba2c361STejun Heo * tasks for the sub-sched being enabled. Use a global variable instead of a 102bba2c361STejun Heo * per-task field as all enables are serialized. 103bba2c361STejun Heo */ 104bba2c361STejun Heo static struct scx_sched *scx_enabling_sub_sched; 105bba2c361STejun Heo #else 106bba2c361STejun Heo #define scx_enabling_sub_sched (struct scx_sched *)NULL 107bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 108bba2c361STejun Heo 109bba2c361STejun Heo /* 110bba2c361STejun Heo * A monotonically increasing sequence number that is incremented every time a 111bba2c361STejun Heo * scheduler is enabled. This can be used to check if any custom sched_ext 112bba2c361STejun Heo * scheduler has ever been used in the system. 113bba2c361STejun Heo */ 114bba2c361STejun Heo static atomic_long_t scx_enable_seq = ATOMIC_LONG_INIT(0); 115bba2c361STejun Heo 116bba2c361STejun Heo /* 117bba2c361STejun Heo * Watchdog interval. All scx_sched's share a single watchdog timer and the 118bba2c361STejun Heo * interval is half of the shortest sch->watchdog_timeout. 119bba2c361STejun Heo */ 120bba2c361STejun Heo static unsigned long scx_watchdog_interval; 121bba2c361STejun Heo 122bba2c361STejun Heo /* 123bba2c361STejun Heo * The last time the delayed work was run. This delayed work relies on 124bba2c361STejun Heo * ksoftirqd being able to run to service timer interrupts, so it's possible 125bba2c361STejun Heo * that this work itself could get wedged. To account for this, we check that 126bba2c361STejun Heo * it's not stalled in the timer tick, and trigger an error if it is. 127bba2c361STejun Heo */ 128bba2c361STejun Heo static unsigned long scx_watchdog_timestamp = INITIAL_JIFFIES; 129bba2c361STejun Heo 130bba2c361STejun Heo static struct delayed_work scx_watchdog_work; 131bba2c361STejun Heo 132bba2c361STejun Heo /* 133bba2c361STejun Heo * For %SCX_KICK_WAIT: Each CPU has a pointer to an array of kick_sync sequence 134bba2c361STejun Heo * numbers. The arrays are allocated with kvzalloc() as size can exceed percpu 135bba2c361STejun Heo * allocator limits on large machines. O(nr_cpu_ids^2) allocation, allocated 136bba2c361STejun Heo * lazily when enabling and freed when disabling to avoid waste when sched_ext 137bba2c361STejun Heo * isn't active. 138bba2c361STejun Heo */ 139bba2c361STejun Heo struct scx_kick_syncs { 140bba2c361STejun Heo struct rcu_head rcu; 141bba2c361STejun Heo unsigned long syncs[]; 142bba2c361STejun Heo }; 143bba2c361STejun Heo 144bba2c361STejun Heo static DEFINE_PER_CPU(struct scx_kick_syncs __rcu *, scx_kick_syncs); 145bba2c361STejun Heo 146bba2c361STejun Heo /* 147bba2c361STejun Heo * Per-CPU buffered allocator state for p->scx.tid. Each CPU pulls a chunk of 148bba2c361STejun Heo * SCX_TID_CHUNK ids from scx_tid_cursor and hands them out locally without 149bba2c361STejun Heo * further synchronization. See scx_alloc_tid(). 150bba2c361STejun Heo */ 151bba2c361STejun Heo struct scx_tid_alloc { 152bba2c361STejun Heo u64 next; 153bba2c361STejun Heo u64 end; 154bba2c361STejun Heo }; 155bba2c361STejun Heo static DEFINE_PER_CPU(struct scx_tid_alloc, scx_tid_alloc); 156bba2c361STejun Heo 157bba2c361STejun Heo /* 158bba2c361STejun Heo * Direct dispatch marker. 159bba2c361STejun Heo * 160bba2c361STejun Heo * Non-NULL values are used for direct dispatch from enqueue path. A valid 161bba2c361STejun Heo * pointer points to the task currently being enqueued. An ERR_PTR value is used 162bba2c361STejun Heo * to indicate that direct dispatch has already happened. 163bba2c361STejun Heo */ 164bba2c361STejun Heo static DEFINE_PER_CPU(struct task_struct *, direct_dispatch_task); 165bba2c361STejun Heo 166bba2c361STejun Heo static const struct rhashtable_params dsq_hash_params = { 167bba2c361STejun Heo .key_len = sizeof_field(struct scx_dispatch_q, id), 168bba2c361STejun Heo .key_offset = offsetof(struct scx_dispatch_q, id), 169bba2c361STejun Heo .head_offset = offsetof(struct scx_dispatch_q, hash_node), 170bba2c361STejun Heo }; 171bba2c361STejun Heo 172bba2c361STejun Heo static LLIST_HEAD(dsqs_to_free); 173bba2c361STejun Heo 174bba2c361STejun Heo /* string formatting from BPF */ 175bba2c361STejun Heo struct scx_bstr_buf { 176bba2c361STejun Heo u64 data[MAX_BPRINTF_VARARGS]; 177bba2c361STejun Heo char line[SCX_EXIT_MSG_LEN]; 178bba2c361STejun Heo }; 179bba2c361STejun Heo 180bba2c361STejun Heo static DEFINE_RAW_SPINLOCK(scx_exit_bstr_buf_lock); 181bba2c361STejun Heo static struct scx_bstr_buf scx_exit_bstr_buf; 182bba2c361STejun Heo 183bba2c361STejun Heo /* ops debug dump */ 184bba2c361STejun Heo static DEFINE_RAW_SPINLOCK(scx_dump_lock); 185bba2c361STejun Heo 186bba2c361STejun Heo struct scx_dump_data { 187bba2c361STejun Heo s32 cpu; 188bba2c361STejun Heo bool first; 189bba2c361STejun Heo s32 cursor; 190bba2c361STejun Heo struct seq_buf *s; 191bba2c361STejun Heo const char *prefix; 192bba2c361STejun Heo struct scx_bstr_buf buf; 193bba2c361STejun Heo }; 194bba2c361STejun Heo 195bba2c361STejun Heo static struct scx_dump_data scx_dump_data = { 196bba2c361STejun Heo .cpu = -1, 197bba2c361STejun Heo }; 198bba2c361STejun Heo 199bba2c361STejun Heo /* /sys/kernel/sched_ext interface */ 200bba2c361STejun Heo static struct kset *scx_kset; 201bba2c361STejun Heo 202bba2c361STejun Heo /* 203bba2c361STejun Heo * Parameters that can be adjusted through /sys/module/sched_ext/parameters. 204bba2c361STejun Heo * There usually is no reason to modify these as normal scheduler operation 205bba2c361STejun Heo * shouldn't be affected by them. The knobs are primarily for debugging. 206bba2c361STejun Heo */ 207bba2c361STejun Heo static unsigned int scx_slice_bypass_us = SCX_SLICE_BYPASS / NSEC_PER_USEC; 208bba2c361STejun Heo static unsigned int scx_bypass_lb_intv_us = SCX_BYPASS_LB_DFL_INTV_US; 209bba2c361STejun Heo 210bba2c361STejun Heo static int set_slice_us(const char *val, const struct kernel_param *kp) 211bba2c361STejun Heo { 212bba2c361STejun Heo return param_set_uint_minmax(val, kp, 100, 100 * USEC_PER_MSEC); 213bba2c361STejun Heo } 214bba2c361STejun Heo 215bba2c361STejun Heo static const struct kernel_param_ops slice_us_param_ops = { 216bba2c361STejun Heo .set = set_slice_us, 217bba2c361STejun Heo .get = param_get_uint, 218bba2c361STejun Heo }; 219bba2c361STejun Heo 220bba2c361STejun Heo static int set_bypass_lb_intv_us(const char *val, const struct kernel_param *kp) 221bba2c361STejun Heo { 222bba2c361STejun Heo return param_set_uint_minmax(val, kp, 0, 10 * USEC_PER_SEC); 223bba2c361STejun Heo } 224bba2c361STejun Heo 225bba2c361STejun Heo static const struct kernel_param_ops bypass_lb_intv_us_param_ops = { 226bba2c361STejun Heo .set = set_bypass_lb_intv_us, 227bba2c361STejun Heo .get = param_get_uint, 228bba2c361STejun Heo }; 229bba2c361STejun Heo 230bba2c361STejun Heo #undef MODULE_PARAM_PREFIX 231bba2c361STejun Heo #define MODULE_PARAM_PREFIX "sched_ext." 232bba2c361STejun Heo 233bba2c361STejun Heo module_param_cb(slice_bypass_us, &slice_us_param_ops, &scx_slice_bypass_us, 0600); 234bba2c361STejun Heo MODULE_PARM_DESC(slice_bypass_us, "bypass slice in microseconds, applied on [un]load (100us to 100ms)"); 235bba2c361STejun Heo module_param_cb(bypass_lb_intv_us, &bypass_lb_intv_us_param_ops, &scx_bypass_lb_intv_us, 0600); 236bba2c361STejun Heo MODULE_PARM_DESC(bypass_lb_intv_us, "bypass load balance interval in microseconds (0 (disable) to 10s)"); 237bba2c361STejun Heo 238bba2c361STejun Heo #undef MODULE_PARAM_PREFIX 239bba2c361STejun Heo 240bba2c361STejun Heo #define CREATE_TRACE_POINTS 241bba2c361STejun Heo #include <trace/events/sched_ext.h> 242bba2c361STejun Heo 243bba2c361STejun Heo static void run_deferred(struct rq *rq); 244bba2c361STejun Heo static bool task_dead_and_done(struct task_struct *p); 245bba2c361STejun Heo static void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags); 246bba2c361STejun Heo static void scx_disable(struct scx_sched *sch, enum scx_exit_kind kind); 247bba2c361STejun Heo 248bba2c361STejun Heo __printf(5, 6) bool __scx_exit(struct scx_sched *sch, 249bba2c361STejun Heo enum scx_exit_kind kind, s64 exit_code, 250bba2c361STejun Heo s32 exit_cpu, const char *fmt, ...) 251bba2c361STejun Heo { 252bba2c361STejun Heo va_list args; 253bba2c361STejun Heo bool ret; 254bba2c361STejun Heo 255bba2c361STejun Heo va_start(args, fmt); 256bba2c361STejun Heo ret = scx_vexit(sch, kind, exit_code, exit_cpu, fmt, args); 257bba2c361STejun Heo va_end(args); 258bba2c361STejun Heo 259bba2c361STejun Heo return ret; 260bba2c361STejun Heo } 261bba2c361STejun Heo 262bba2c361STejun Heo static long jiffies_delta_msecs(unsigned long at, unsigned long now) 263bba2c361STejun Heo { 264bba2c361STejun Heo if (time_after(at, now)) 265bba2c361STejun Heo return jiffies_to_msecs(at - now); 266bba2c361STejun Heo else 267bba2c361STejun Heo return -(long)jiffies_to_msecs(now - at); 268bba2c361STejun Heo } 269bba2c361STejun Heo 270bba2c361STejun Heo static bool u32_before(u32 a, u32 b) 271bba2c361STejun Heo { 272bba2c361STejun Heo return (s32)(a - b) < 0; 273bba2c361STejun Heo } 274bba2c361STejun Heo 275bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 276bba2c361STejun Heo /** 277bba2c361STejun Heo * scx_next_descendant_pre - find the next descendant for pre-order walk 278bba2c361STejun Heo * @pos: the current position (%NULL to initiate traversal) 279bba2c361STejun Heo * @root: sched whose descendants to walk 280bba2c361STejun Heo * 281bba2c361STejun Heo * To be used by scx_for_each_descendant_pre(). Find the next descendant to 282bba2c361STejun Heo * visit for pre-order traversal of @root's descendants. @root is included in 283bba2c361STejun Heo * the iteration and the first node to be visited. 284bba2c361STejun Heo */ 285bba2c361STejun Heo static struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, 286bba2c361STejun Heo struct scx_sched *root) 287bba2c361STejun Heo { 288bba2c361STejun Heo struct scx_sched *next; 289bba2c361STejun Heo 290bba2c361STejun Heo lockdep_assert(lockdep_is_held(&scx_enable_mutex) || 291bba2c361STejun Heo lockdep_is_held(&scx_sched_lock)); 292bba2c361STejun Heo 293bba2c361STejun Heo /* if first iteration, visit @root */ 294bba2c361STejun Heo if (!pos) 295bba2c361STejun Heo return root; 296bba2c361STejun Heo 297bba2c361STejun Heo /* visit the first child if exists */ 298bba2c361STejun Heo next = list_first_entry_or_null(&pos->children, struct scx_sched, sibling); 299bba2c361STejun Heo if (next) 300bba2c361STejun Heo return next; 301bba2c361STejun Heo 302bba2c361STejun Heo /* no child, visit my or the closest ancestor's next sibling */ 303bba2c361STejun Heo while (pos != root) { 304bba2c361STejun Heo if (!list_is_last(&pos->sibling, &scx_parent(pos)->children)) 305bba2c361STejun Heo return list_next_entry(pos, sibling); 306bba2c361STejun Heo pos = scx_parent(pos); 307bba2c361STejun Heo } 308bba2c361STejun Heo 309bba2c361STejun Heo return NULL; 310bba2c361STejun Heo } 311bba2c361STejun Heo 312bba2c361STejun Heo static struct scx_sched *scx_find_sub_sched(u64 cgroup_id) 313bba2c361STejun Heo { 314bba2c361STejun Heo return rhashtable_lookup(&scx_sched_hash, &cgroup_id, 315bba2c361STejun Heo scx_sched_hash_params); 316bba2c361STejun Heo } 317bba2c361STejun Heo 318bba2c361STejun Heo static void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) 319bba2c361STejun Heo { 320bba2c361STejun Heo rcu_assign_pointer(p->scx.sched, sch); 321bba2c361STejun Heo } 322bba2c361STejun Heo #else /* CONFIG_EXT_SUB_SCHED */ 323bba2c361STejun Heo static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; } 324bba2c361STejun Heo static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {} 325bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 326bba2c361STejun Heo 327bba2c361STejun Heo /** 328bba2c361STejun Heo * scx_is_descendant - Test whether sched is a descendant 329bba2c361STejun Heo * @sch: sched to test 330bba2c361STejun Heo * @ancestor: ancestor sched to test against 331bba2c361STejun Heo * 332bba2c361STejun Heo * Test whether @sch is a descendant of @ancestor. 333bba2c361STejun Heo */ 334bba2c361STejun Heo static bool scx_is_descendant(struct scx_sched *sch, struct scx_sched *ancestor) 335bba2c361STejun Heo { 336bba2c361STejun Heo if (sch->level < ancestor->level) 337bba2c361STejun Heo return false; 338bba2c361STejun Heo return sch->ancestors[ancestor->level] == ancestor; 339bba2c361STejun Heo } 340bba2c361STejun Heo 341bba2c361STejun Heo /** 342bba2c361STejun Heo * scx_for_each_descendant_pre - pre-order walk of a sched's descendants 343bba2c361STejun Heo * @pos: iteration cursor 344bba2c361STejun Heo * @root: sched to walk the descendants of 345bba2c361STejun Heo * 346bba2c361STejun Heo * Walk @root's descendants. @root is included in the iteration and the first 347bba2c361STejun Heo * node to be visited. Must be called with either scx_enable_mutex or 348bba2c361STejun Heo * scx_sched_lock held. 349bba2c361STejun Heo */ 350bba2c361STejun Heo #define scx_for_each_descendant_pre(pos, root) \ 351bba2c361STejun Heo for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos); \ 352bba2c361STejun Heo (pos) = scx_next_descendant_pre((pos), (root))) 353bba2c361STejun Heo 354bba2c361STejun Heo static struct scx_dispatch_q *find_global_dsq(struct scx_sched *sch, s32 cpu) 355bba2c361STejun Heo { 356bba2c361STejun Heo return &sch->pnode[cpu_to_node(cpu)]->global_dsq; 357bba2c361STejun Heo } 358bba2c361STejun Heo 359bba2c361STejun Heo static struct scx_dispatch_q *find_user_dsq(struct scx_sched *sch, u64 dsq_id) 360bba2c361STejun Heo { 361bba2c361STejun Heo return rhashtable_lookup(&sch->dsq_hash, &dsq_id, dsq_hash_params); 362bba2c361STejun Heo } 363bba2c361STejun Heo 364bba2c361STejun Heo static const struct sched_class *scx_setscheduler_class(struct task_struct *p) 365bba2c361STejun Heo { 366bba2c361STejun Heo if (p->sched_class == &stop_sched_class) 367bba2c361STejun Heo return &stop_sched_class; 368bba2c361STejun Heo 369bba2c361STejun Heo return __setscheduler_class(p->policy, p->prio); 370bba2c361STejun Heo } 371bba2c361STejun Heo 372bba2c361STejun Heo static struct scx_dispatch_q *bypass_dsq(struct scx_sched *sch, s32 cpu) 373bba2c361STejun Heo { 374bba2c361STejun Heo return &per_cpu_ptr(sch->pcpu, cpu)->bypass_dsq; 375bba2c361STejun Heo } 376bba2c361STejun Heo 377bba2c361STejun Heo static struct scx_dispatch_q *bypass_enq_target_dsq(struct scx_sched *sch, s32 cpu) 378bba2c361STejun Heo { 379bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 380bba2c361STejun Heo /* 381bba2c361STejun Heo * If @sch is a sub-sched which is bypassing, its tasks should go into 382bba2c361STejun Heo * the bypass DSQs of the nearest ancestor which is not bypassing. The 383bba2c361STejun Heo * not-bypassing ancestor is responsible for scheduling all tasks from 384bba2c361STejun Heo * bypassing sub-trees. If all ancestors including root are bypassing, 385bba2c361STejun Heo * all tasks should go to the root's bypass DSQs. 386bba2c361STejun Heo * 387bba2c361STejun Heo * Whenever a sched starts bypassing, all runnable tasks in its subtree 388bba2c361STejun Heo * are re-enqueued after scx_bypassing() is turned on, guaranteeing that 389bba2c361STejun Heo * all tasks are transferred to the right DSQs. 390bba2c361STejun Heo */ 391bba2c361STejun Heo while (scx_parent(sch) && scx_bypassing(sch, cpu)) 392bba2c361STejun Heo sch = scx_parent(sch); 393bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 394bba2c361STejun Heo 395bba2c361STejun Heo return bypass_dsq(sch, cpu); 396bba2c361STejun Heo } 397bba2c361STejun Heo 398bba2c361STejun Heo /** 399bba2c361STejun Heo * bypass_dsp_enabled - Check if bypass dispatch path is enabled 400bba2c361STejun Heo * @sch: scheduler to check 401bba2c361STejun Heo * 402bba2c361STejun Heo * When a descendant scheduler enters bypass mode, bypassed tasks are scheduled 403bba2c361STejun Heo * by the nearest non-bypassing ancestor, or the root scheduler if all ancestors 404bba2c361STejun Heo * are bypassing. In the former case, the ancestor is not itself bypassing but 405bba2c361STejun Heo * its bypass DSQs will be populated with bypassed tasks from descendants. Thus, 406bba2c361STejun Heo * the ancestor's bypass dispatch path must be active even though its own 407bba2c361STejun Heo * bypass_depth remains zero. 408bba2c361STejun Heo * 409bba2c361STejun Heo * This function checks bypass_dsp_enable_depth which is managed separately from 410bba2c361STejun Heo * bypass_depth to enable this decoupling. See enable_bypass_dsp() and 411bba2c361STejun Heo * disable_bypass_dsp(). 412bba2c361STejun Heo */ 413bba2c361STejun Heo static bool bypass_dsp_enabled(struct scx_sched *sch) 414bba2c361STejun Heo { 415bba2c361STejun Heo return unlikely(atomic_read(&sch->bypass_dsp_enable_depth)); 416bba2c361STejun Heo } 417bba2c361STejun Heo 418bba2c361STejun Heo /** 419bba2c361STejun Heo * rq_is_open - Is the rq available for immediate execution of an SCX task? 420bba2c361STejun Heo * @rq: rq to test 421bba2c361STejun Heo * @enq_flags: optional %SCX_ENQ_* of the task being enqueued 422bba2c361STejun Heo * 423bba2c361STejun Heo * Returns %true if @rq is currently open for executing an SCX task. After a 424bba2c361STejun Heo * %false return, @rq is guaranteed to invoke SCX dispatch path at least once 425bba2c361STejun Heo * before going to idle and not inserting a task into @rq's local DSQ after a 426bba2c361STejun Heo * %false return doesn't cause @rq to stall. 427bba2c361STejun Heo */ 428bba2c361STejun Heo static bool rq_is_open(struct rq *rq, u64 enq_flags) 429bba2c361STejun Heo { 430bba2c361STejun Heo lockdep_assert_rq_held(rq); 431bba2c361STejun Heo 432bba2c361STejun Heo /* 433bba2c361STejun Heo * A higher-priority class task is either running or in the process of 434bba2c361STejun Heo * waking up on @rq. 435bba2c361STejun Heo */ 436bba2c361STejun Heo if (sched_class_above(rq->next_class, &ext_sched_class)) 437bba2c361STejun Heo return false; 438bba2c361STejun Heo 439bba2c361STejun Heo /* 440bba2c361STejun Heo * @rq is either in transition to or in idle and there is no 441bba2c361STejun Heo * higher-priority class task waking up on it. 442bba2c361STejun Heo */ 443bba2c361STejun Heo if (sched_class_above(&ext_sched_class, rq->next_class)) 444bba2c361STejun Heo return true; 445bba2c361STejun Heo 446bba2c361STejun Heo /* 447bba2c361STejun Heo * @rq is either picking, in transition to, or running an SCX task. 448bba2c361STejun Heo */ 449bba2c361STejun Heo 450bba2c361STejun Heo /* 451bba2c361STejun Heo * If we're in the dispatch path holding rq lock, $curr may or may not 452bba2c361STejun Heo * be ready depending on whether the on-going dispatch decides to extend 453bba2c361STejun Heo * $curr's slice. We say yes here and resolve it at the end of dispatch. 454bba2c361STejun Heo * See balance_one(). 455bba2c361STejun Heo */ 456bba2c361STejun Heo if (rq->scx.flags & SCX_RQ_IN_BALANCE) 457bba2c361STejun Heo return true; 458bba2c361STejun Heo 459bba2c361STejun Heo /* 460bba2c361STejun Heo * %SCX_ENQ_PREEMPT clears $curr's slice if on SCX and kicks dispatch, 461bba2c361STejun Heo * so allow it to avoid spuriously triggering reenq on a combined 462bba2c361STejun Heo * PREEMPT|IMMED insertion. 463bba2c361STejun Heo */ 464bba2c361STejun Heo if (enq_flags & SCX_ENQ_PREEMPT) 465bba2c361STejun Heo return true; 466bba2c361STejun Heo 467bba2c361STejun Heo /* 468bba2c361STejun Heo * @rq is either in transition to or running an SCX task and can't go 469bba2c361STejun Heo * idle without another SCX dispatch cycle. 470bba2c361STejun Heo */ 471bba2c361STejun Heo return false; 472bba2c361STejun Heo } 473bba2c361STejun Heo 474bba2c361STejun Heo /* 475bba2c361STejun Heo * Track the rq currently locked. 476bba2c361STejun Heo * 477bba2c361STejun Heo * This allows kfuncs to safely operate on rq from any scx ops callback, 478bba2c361STejun Heo * knowing which rq is already locked. 479bba2c361STejun Heo */ 480bba2c361STejun Heo DEFINE_PER_CPU(struct rq *, scx_locked_rq_state); 481bba2c361STejun Heo 482bba2c361STejun Heo /* 483bba2c361STejun Heo * Flipped on enable per sch->is_cid_type. Declared in internal.h so 484bba2c361STejun Heo * subsystem inlines can read it. 485bba2c361STejun Heo */ 486bba2c361STejun Heo DEFINE_STATIC_KEY_FALSE(__scx_is_cid_type); 487bba2c361STejun Heo 488bba2c361STejun Heo /** 489bba2c361STejun Heo * scx_call_op_set_cpumask - invoke ops.set_cpumask / ops_cid.set_cmask for @task 490bba2c361STejun Heo * @sch: scx_sched being invoked 491bba2c361STejun Heo * @rq: rq to update as the currently-locked rq, or NULL 492bba2c361STejun Heo * @task: task whose affinity is changing 493bba2c361STejun Heo * @cpumask: new cpumask 494bba2c361STejun Heo * 495bba2c361STejun Heo * For cid-form schedulers, translate @cpumask to a cmask via the per-cpu 496bba2c361STejun Heo * scratch in cid.c and dispatch through the ops_cid union view. Caller 497bba2c361STejun Heo * must hold @rq's rq lock so this_cpu_ptr is stable across the call. 498bba2c361STejun Heo */ 499bba2c361STejun Heo static inline void scx_call_op_set_cpumask(struct scx_sched *sch, struct rq *rq, 500bba2c361STejun Heo struct task_struct *task, 501bba2c361STejun Heo const struct cpumask *cpumask) 502bba2c361STejun Heo { 503bba2c361STejun Heo WARN_ON_ONCE(current->scx.kf_tasks[0]); 504bba2c361STejun Heo current->scx.kf_tasks[0] = task; 505bba2c361STejun Heo if (rq) 506bba2c361STejun Heo update_locked_rq(rq); 507bba2c361STejun Heo 508bba2c361STejun Heo if (scx_is_cid_type()) { 509bba2c361STejun Heo struct scx_cmask *kern_va = *this_cpu_ptr(sch->set_cmask_scratch); 510bba2c361STejun Heo /* 511bba2c361STejun Heo * Build the per-CPU arena cmask and hand BPF its arena address. 512bba2c361STejun Heo * Caller holds the rq lock with IRQs disabled, which makes us 513bba2c361STejun Heo * the sole user of the scratch area. 514bba2c361STejun Heo */ 515bba2c361STejun Heo scx_cpumask_to_cmask(cpumask, kern_va); 516bba2c361STejun Heo sch->ops_cid.set_cmask(task, scx_kaddr_to_arena(sch, kern_va)); 517bba2c361STejun Heo } else { 518bba2c361STejun Heo sch->ops.set_cpumask(task, cpumask); 519bba2c361STejun Heo } 520bba2c361STejun Heo 521bba2c361STejun Heo if (rq) 522bba2c361STejun Heo update_locked_rq(NULL); 523bba2c361STejun Heo current->scx.kf_tasks[0] = NULL; 524bba2c361STejun Heo } 525bba2c361STejun Heo 526bba2c361STejun Heo enum scx_dsq_iter_flags { 527bba2c361STejun Heo /* iterate in the reverse dispatch order */ 528bba2c361STejun Heo SCX_DSQ_ITER_REV = 1U << 16, 529bba2c361STejun Heo 530bba2c361STejun Heo __SCX_DSQ_ITER_HAS_SLICE = 1U << 30, 531bba2c361STejun Heo __SCX_DSQ_ITER_HAS_VTIME = 1U << 31, 532bba2c361STejun Heo 533bba2c361STejun Heo __SCX_DSQ_ITER_USER_FLAGS = SCX_DSQ_ITER_REV, 534bba2c361STejun Heo __SCX_DSQ_ITER_ALL_FLAGS = __SCX_DSQ_ITER_USER_FLAGS | 535bba2c361STejun Heo __SCX_DSQ_ITER_HAS_SLICE | 536bba2c361STejun Heo __SCX_DSQ_ITER_HAS_VTIME, 537bba2c361STejun Heo }; 538bba2c361STejun Heo 539bba2c361STejun Heo /** 540bba2c361STejun Heo * nldsq_next_task - Iterate to the next task in a non-local DSQ 541bba2c361STejun Heo * @dsq: non-local dsq being iterated 542bba2c361STejun Heo * @cur: current position, %NULL to start iteration 543bba2c361STejun Heo * @rev: walk backwards 544bba2c361STejun Heo * 545bba2c361STejun Heo * Returns %NULL when iteration is finished. 546bba2c361STejun Heo */ 547bba2c361STejun Heo static struct task_struct *nldsq_next_task(struct scx_dispatch_q *dsq, 548bba2c361STejun Heo struct task_struct *cur, bool rev) 549bba2c361STejun Heo { 550bba2c361STejun Heo struct list_head *list_node; 551bba2c361STejun Heo struct scx_dsq_list_node *dsq_lnode; 552bba2c361STejun Heo 553bba2c361STejun Heo lockdep_assert_held(&dsq->lock); 554bba2c361STejun Heo 555bba2c361STejun Heo if (cur) 556bba2c361STejun Heo list_node = &cur->scx.dsq_list.node; 557bba2c361STejun Heo else 558bba2c361STejun Heo list_node = &dsq->list; 559bba2c361STejun Heo 560bba2c361STejun Heo /* find the next task, need to skip BPF iteration cursors */ 561bba2c361STejun Heo do { 562bba2c361STejun Heo if (rev) 563bba2c361STejun Heo list_node = list_node->prev; 564bba2c361STejun Heo else 565bba2c361STejun Heo list_node = list_node->next; 566bba2c361STejun Heo 567bba2c361STejun Heo if (list_node == &dsq->list) 568bba2c361STejun Heo return NULL; 569bba2c361STejun Heo 570bba2c361STejun Heo dsq_lnode = container_of(list_node, struct scx_dsq_list_node, 571bba2c361STejun Heo node); 572bba2c361STejun Heo } while (dsq_lnode->flags & SCX_DSQ_LNODE_ITER_CURSOR); 573bba2c361STejun Heo 574bba2c361STejun Heo return container_of(dsq_lnode, struct task_struct, scx.dsq_list); 575bba2c361STejun Heo } 576bba2c361STejun Heo 577bba2c361STejun Heo #define nldsq_for_each_task(p, dsq) \ 578bba2c361STejun Heo for ((p) = nldsq_next_task((dsq), NULL, false); (p); \ 579bba2c361STejun Heo (p) = nldsq_next_task((dsq), (p), false)) 580bba2c361STejun Heo 581bba2c361STejun Heo /** 582bba2c361STejun Heo * nldsq_cursor_next_task - Iterate to the next task given a cursor in a non-local DSQ 583bba2c361STejun Heo * @cursor: scx_dsq_list_node initialized with INIT_DSQ_LIST_CURSOR() 584bba2c361STejun Heo * @dsq: non-local dsq being iterated 585bba2c361STejun Heo * 586bba2c361STejun Heo * Find the next task in a cursor based iteration. The caller must have 587bba2c361STejun Heo * initialized @cursor using INIT_DSQ_LIST_CURSOR() and can release the DSQ lock 588bba2c361STejun Heo * between the iteration steps. 589bba2c361STejun Heo * 590bba2c361STejun Heo * Only tasks which were queued before @cursor was initialized are visible. This 591bba2c361STejun Heo * bounds the iteration and guarantees that vtime never jumps in the other 592bba2c361STejun Heo * direction while iterating. 593bba2c361STejun Heo */ 594bba2c361STejun Heo static struct task_struct *nldsq_cursor_next_task(struct scx_dsq_list_node *cursor, 595bba2c361STejun Heo struct scx_dispatch_q *dsq) 596bba2c361STejun Heo { 597bba2c361STejun Heo bool rev = cursor->flags & SCX_DSQ_ITER_REV; 598bba2c361STejun Heo struct task_struct *p; 599bba2c361STejun Heo 600bba2c361STejun Heo lockdep_assert_held(&dsq->lock); 601bba2c361STejun Heo BUG_ON(!(cursor->flags & SCX_DSQ_LNODE_ITER_CURSOR)); 602bba2c361STejun Heo 603bba2c361STejun Heo if (list_empty(&cursor->node)) 604bba2c361STejun Heo p = NULL; 605bba2c361STejun Heo else 606bba2c361STejun Heo p = container_of(cursor, struct task_struct, scx.dsq_list); 607bba2c361STejun Heo 608bba2c361STejun Heo /* skip cursors and tasks that were queued after @cursor init */ 609bba2c361STejun Heo do { 610bba2c361STejun Heo p = nldsq_next_task(dsq, p, rev); 611bba2c361STejun Heo } while (p && unlikely(u32_before(cursor->priv, p->scx.dsq_seq))); 612bba2c361STejun Heo 613bba2c361STejun Heo if (p) { 614bba2c361STejun Heo if (rev) 615bba2c361STejun Heo list_move_tail(&cursor->node, &p->scx.dsq_list.node); 616bba2c361STejun Heo else 617bba2c361STejun Heo list_move(&cursor->node, &p->scx.dsq_list.node); 618bba2c361STejun Heo } else { 619bba2c361STejun Heo list_del_init(&cursor->node); 620bba2c361STejun Heo } 621bba2c361STejun Heo 622bba2c361STejun Heo return p; 623bba2c361STejun Heo } 624bba2c361STejun Heo 625bba2c361STejun Heo /** 626bba2c361STejun Heo * nldsq_cursor_lost_task - Test whether someone else took the task since iteration 627bba2c361STejun Heo * @cursor: scx_dsq_list_node initialized with INIT_DSQ_LIST_CURSOR() 628bba2c361STejun Heo * @rq: rq @p was on 629bba2c361STejun Heo * @dsq: dsq @p was on 630bba2c361STejun Heo * @p: target task 631bba2c361STejun Heo * 632bba2c361STejun Heo * @p is a task returned by nldsq_cursor_next_task(). The locks may have been 633bba2c361STejun Heo * dropped and re-acquired inbetween. Verify that no one else took or is in the 634bba2c361STejun Heo * process of taking @p from @dsq. 635bba2c361STejun Heo * 636bba2c361STejun Heo * On %false return, the caller can assume full ownership of @p. 637bba2c361STejun Heo */ 638bba2c361STejun Heo static bool nldsq_cursor_lost_task(struct scx_dsq_list_node *cursor, 639bba2c361STejun Heo struct rq *rq, struct scx_dispatch_q *dsq, 640bba2c361STejun Heo struct task_struct *p) 641bba2c361STejun Heo { 642bba2c361STejun Heo lockdep_assert_rq_held(rq); 643bba2c361STejun Heo lockdep_assert_held(&dsq->lock); 644bba2c361STejun Heo 645bba2c361STejun Heo /* 646bba2c361STejun Heo * @p could have already left $src_dsq, got re-enqueud, or be in the 647bba2c361STejun Heo * process of being consumed by someone else. 648bba2c361STejun Heo */ 649bba2c361STejun Heo if (unlikely(p->scx.dsq != dsq || 650bba2c361STejun Heo u32_before(cursor->priv, p->scx.dsq_seq) || 651bba2c361STejun Heo p->scx.holding_cpu >= 0)) 652bba2c361STejun Heo return true; 653bba2c361STejun Heo 654bba2c361STejun Heo /* if @p has stayed on @dsq, its rq couldn't have changed */ 655bba2c361STejun Heo if (WARN_ON_ONCE(rq != task_rq(p))) 656bba2c361STejun Heo return true; 657bba2c361STejun Heo 658bba2c361STejun Heo return false; 659bba2c361STejun Heo } 660bba2c361STejun Heo 661bba2c361STejun Heo /* 662bba2c361STejun Heo * BPF DSQ iterator. Tasks in a non-local DSQ can be iterated in [reverse] 663bba2c361STejun Heo * dispatch order. BPF-visible iterator is opaque and larger to allow future 664bba2c361STejun Heo * changes without breaking backward compatibility. Can be used with 665bba2c361STejun Heo * bpf_for_each(). See bpf_iter_scx_dsq_*(). 666bba2c361STejun Heo */ 667bba2c361STejun Heo struct bpf_iter_scx_dsq_kern { 668bba2c361STejun Heo struct scx_dsq_list_node cursor; 669bba2c361STejun Heo struct scx_dispatch_q *dsq; 670bba2c361STejun Heo u64 slice; 671bba2c361STejun Heo u64 vtime; 672bba2c361STejun Heo } __attribute__((aligned(8))); 673bba2c361STejun Heo 674bba2c361STejun Heo struct bpf_iter_scx_dsq { 675bba2c361STejun Heo u64 __opaque[6]; 676bba2c361STejun Heo } __attribute__((aligned(8))); 677bba2c361STejun Heo 678bba2c361STejun Heo 679bba2c361STejun Heo static u32 scx_get_task_state(const struct task_struct *p) 680bba2c361STejun Heo { 681bba2c361STejun Heo return p->scx.flags & SCX_TASK_STATE_MASK; 682bba2c361STejun Heo } 683bba2c361STejun Heo 684bba2c361STejun Heo static void scx_set_task_state(struct task_struct *p, u32 state) 685bba2c361STejun Heo { 686bba2c361STejun Heo u32 prev_state = scx_get_task_state(p); 687bba2c361STejun Heo bool warn = false; 688bba2c361STejun Heo 689bba2c361STejun Heo switch (state) { 690bba2c361STejun Heo case SCX_TASK_NONE: 691bba2c361STejun Heo warn = prev_state == SCX_TASK_DEAD; 692bba2c361STejun Heo break; 693bba2c361STejun Heo case SCX_TASK_INIT_BEGIN: 694bba2c361STejun Heo warn = prev_state != SCX_TASK_NONE; 695bba2c361STejun Heo break; 696bba2c361STejun Heo case SCX_TASK_INIT: 697bba2c361STejun Heo warn = prev_state != SCX_TASK_INIT_BEGIN; 698bba2c361STejun Heo p->scx.flags |= SCX_TASK_RESET_RUNNABLE_AT; 699bba2c361STejun Heo break; 700bba2c361STejun Heo case SCX_TASK_READY: 701bba2c361STejun Heo warn = !(prev_state == SCX_TASK_INIT || 702bba2c361STejun Heo prev_state == SCX_TASK_ENABLED); 703bba2c361STejun Heo break; 704bba2c361STejun Heo case SCX_TASK_ENABLED: 705bba2c361STejun Heo warn = prev_state != SCX_TASK_READY; 706bba2c361STejun Heo break; 707bba2c361STejun Heo case SCX_TASK_DEAD: 708bba2c361STejun Heo warn = !(prev_state == SCX_TASK_NONE || 709bba2c361STejun Heo prev_state == SCX_TASK_INIT_BEGIN); 710bba2c361STejun Heo break; 711bba2c361STejun Heo default: 712bba2c361STejun Heo WARN_ONCE(1, "sched_ext: Invalid task state %d -> %d for %s[%d]", 713bba2c361STejun Heo prev_state, state, p->comm, p->pid); 714bba2c361STejun Heo return; 715bba2c361STejun Heo } 716bba2c361STejun Heo 717bba2c361STejun Heo WARN_ONCE(warn, "sched_ext: Invalid task state transition 0x%x -> 0x%x for %s[%d]", 718bba2c361STejun Heo prev_state, state, p->comm, p->pid); 719bba2c361STejun Heo 720bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_STATE_MASK; 721bba2c361STejun Heo p->scx.flags |= state; 722bba2c361STejun Heo } 723bba2c361STejun Heo 724bba2c361STejun Heo /* 725bba2c361STejun Heo * SCX task iterator. 726bba2c361STejun Heo */ 727bba2c361STejun Heo struct scx_task_iter { 728bba2c361STejun Heo struct sched_ext_entity cursor; 729bba2c361STejun Heo struct task_struct *locked_task; 730bba2c361STejun Heo struct rq *rq; 731bba2c361STejun Heo struct rq_flags rf; 732bba2c361STejun Heo u32 cnt; 733bba2c361STejun Heo bool list_locked; 734bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 735bba2c361STejun Heo struct cgroup *cgrp; 736bba2c361STejun Heo struct cgroup_subsys_state *css_pos; 737bba2c361STejun Heo struct css_task_iter css_iter; 738bba2c361STejun Heo #endif 739bba2c361STejun Heo }; 740bba2c361STejun Heo 741bba2c361STejun Heo /** 742bba2c361STejun Heo * scx_task_iter_start - Lock scx_tasks_lock and start a task iteration 743bba2c361STejun Heo * @iter: iterator to init 744bba2c361STejun Heo * @cgrp: Optional root of cgroup subhierarchy to iterate 745bba2c361STejun Heo * 746bba2c361STejun Heo * Initialize @iter. Once initialized, @iter must eventually be stopped with 747bba2c361STejun Heo * scx_task_iter_stop(). 748bba2c361STejun Heo * 749bba2c361STejun Heo * If @cgrp is %NULL, scx_tasks is used for iteration and this function returns 750bba2c361STejun Heo * with scx_tasks_lock held and @iter->cursor inserted into scx_tasks. 751bba2c361STejun Heo * 752bba2c361STejun Heo * If @cgrp is not %NULL, @cgrp and its descendants' tasks are walked using 753bba2c361STejun Heo * @iter->css_iter. The caller must be holding cgroup_lock() to prevent cgroup 754bba2c361STejun Heo * task migrations. 755bba2c361STejun Heo * 756bba2c361STejun Heo * The two modes of iterations are largely independent and it's likely that 757bba2c361STejun Heo * scx_tasks can be removed in favor of always using cgroup iteration if 758bba2c361STejun Heo * CONFIG_SCHED_CLASS_EXT depends on CONFIG_CGROUPS. 759bba2c361STejun Heo * 760bba2c361STejun Heo * scx_tasks_lock and the rq lock may be released using scx_task_iter_unlock() 761bba2c361STejun Heo * between this and the first next() call or between any two next() calls. If 762bba2c361STejun Heo * the locks are released between two next() calls, the caller is responsible 763bba2c361STejun Heo * for ensuring that the task being iterated remains accessible either through 764bba2c361STejun Heo * RCU read lock or obtaining a reference count. 765bba2c361STejun Heo * 766bba2c361STejun Heo * All tasks which existed when the iteration started are guaranteed to be 767bba2c361STejun Heo * visited as long as they are not dead. 768bba2c361STejun Heo */ 769bba2c361STejun Heo static void scx_task_iter_start(struct scx_task_iter *iter, struct cgroup *cgrp) 770bba2c361STejun Heo { 771bba2c361STejun Heo memset(iter, 0, sizeof(*iter)); 772bba2c361STejun Heo 773bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 774bba2c361STejun Heo if (cgrp) { 775bba2c361STejun Heo lockdep_assert_held(&cgroup_mutex); 776bba2c361STejun Heo iter->cgrp = cgrp; 777bba2c361STejun Heo iter->css_pos = css_next_descendant_pre(NULL, &iter->cgrp->self); 778bba2c361STejun Heo css_task_iter_start(iter->css_pos, CSS_TASK_ITER_WITH_DEAD, 779bba2c361STejun Heo &iter->css_iter); 780bba2c361STejun Heo return; 781bba2c361STejun Heo } 782bba2c361STejun Heo #endif 783bba2c361STejun Heo raw_spin_lock_irq(&scx_tasks_lock); 784bba2c361STejun Heo 785bba2c361STejun Heo iter->cursor = (struct sched_ext_entity){ .flags = SCX_TASK_CURSOR }; 786bba2c361STejun Heo list_add(&iter->cursor.tasks_node, &scx_tasks); 787bba2c361STejun Heo iter->list_locked = true; 788bba2c361STejun Heo } 789bba2c361STejun Heo 790bba2c361STejun Heo static void __scx_task_iter_rq_unlock(struct scx_task_iter *iter) 791bba2c361STejun Heo { 792bba2c361STejun Heo if (iter->locked_task) { 793bba2c361STejun Heo __balance_callbacks(iter->rq, &iter->rf); 794bba2c361STejun Heo task_rq_unlock(iter->rq, iter->locked_task, &iter->rf); 795bba2c361STejun Heo iter->locked_task = NULL; 796bba2c361STejun Heo } 797bba2c361STejun Heo } 798bba2c361STejun Heo 799bba2c361STejun Heo /** 800bba2c361STejun Heo * scx_task_iter_unlock - Unlock rq and scx_tasks_lock held by a task iterator 801bba2c361STejun Heo * @iter: iterator to unlock 802bba2c361STejun Heo * 803bba2c361STejun Heo * If @iter is in the middle of a locked iteration, it may be locking the rq of 804bba2c361STejun Heo * the task currently being visited in addition to scx_tasks_lock. Unlock both. 805bba2c361STejun Heo * This function can be safely called anytime during an iteration. The next 806bba2c361STejun Heo * iterator operation will automatically restore the necessary locking. 807bba2c361STejun Heo */ 808bba2c361STejun Heo static void scx_task_iter_unlock(struct scx_task_iter *iter) 809bba2c361STejun Heo { 810bba2c361STejun Heo __scx_task_iter_rq_unlock(iter); 811bba2c361STejun Heo if (iter->list_locked) { 812bba2c361STejun Heo iter->list_locked = false; 813bba2c361STejun Heo raw_spin_unlock_irq(&scx_tasks_lock); 814bba2c361STejun Heo } 815bba2c361STejun Heo } 816bba2c361STejun Heo 817bba2c361STejun Heo static void __scx_task_iter_maybe_relock(struct scx_task_iter *iter) 818bba2c361STejun Heo { 819bba2c361STejun Heo if (!iter->list_locked) { 820bba2c361STejun Heo raw_spin_lock_irq(&scx_tasks_lock); 821bba2c361STejun Heo iter->list_locked = true; 822bba2c361STejun Heo } 823bba2c361STejun Heo } 824bba2c361STejun Heo 825bba2c361STejun Heo /** 826bba2c361STejun Heo * scx_task_iter_relock - Re-acquire scx_tasks_lock and, optionally, @p's rq 827bba2c361STejun Heo * @iter: iterator to relock 828bba2c361STejun Heo * @p: task whose rq to lock, or %NULL for scx_tasks_lock only 829bba2c361STejun Heo * 830bba2c361STejun Heo * Counterpart to scx_task_iter_unlock(). Locking @p's rq is optional. Once 831bba2c361STejun Heo * re-acquired, both locks are managed by the iterator from here on. 832bba2c361STejun Heo */ 833bba2c361STejun Heo static void scx_task_iter_relock(struct scx_task_iter *iter, 834bba2c361STejun Heo struct task_struct *p) 835bba2c361STejun Heo { 836bba2c361STejun Heo __scx_task_iter_maybe_relock(iter); 837bba2c361STejun Heo if (p) { 838bba2c361STejun Heo iter->rq = task_rq_lock(p, &iter->rf); 839bba2c361STejun Heo iter->locked_task = p; 840bba2c361STejun Heo } 841bba2c361STejun Heo } 842bba2c361STejun Heo 843bba2c361STejun Heo /** 844bba2c361STejun Heo * scx_task_iter_stop - Stop a task iteration and unlock scx_tasks_lock 845bba2c361STejun Heo * @iter: iterator to exit 846bba2c361STejun Heo * 847bba2c361STejun Heo * Exit a previously initialized @iter. Must be called with scx_tasks_lock held 848bba2c361STejun Heo * which is released on return. If the iterator holds a task's rq lock, that rq 849bba2c361STejun Heo * lock is also released. See scx_task_iter_start() for details. 850bba2c361STejun Heo */ 851bba2c361STejun Heo static void scx_task_iter_stop(struct scx_task_iter *iter) 852bba2c361STejun Heo { 853bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 854bba2c361STejun Heo if (iter->cgrp) { 855bba2c361STejun Heo if (iter->css_pos) 856bba2c361STejun Heo css_task_iter_end(&iter->css_iter); 857bba2c361STejun Heo __scx_task_iter_rq_unlock(iter); 858bba2c361STejun Heo return; 859bba2c361STejun Heo } 860bba2c361STejun Heo #endif 861bba2c361STejun Heo __scx_task_iter_maybe_relock(iter); 862bba2c361STejun Heo list_del_init(&iter->cursor.tasks_node); 863bba2c361STejun Heo scx_task_iter_unlock(iter); 864bba2c361STejun Heo } 865bba2c361STejun Heo 866bba2c361STejun Heo /** 867bba2c361STejun Heo * scx_task_iter_next - Next task 868bba2c361STejun Heo * @iter: iterator to walk 869bba2c361STejun Heo * 870bba2c361STejun Heo * Visit the next task. See scx_task_iter_start() for details. Locks are dropped 871bba2c361STejun Heo * and re-acquired every %SCX_TASK_ITER_BATCH iterations to avoid causing stalls 872bba2c361STejun Heo * by holding scx_tasks_lock for too long. 873bba2c361STejun Heo */ 874bba2c361STejun Heo static struct task_struct *scx_task_iter_next(struct scx_task_iter *iter) 875bba2c361STejun Heo { 876bba2c361STejun Heo struct list_head *cursor = &iter->cursor.tasks_node; 877bba2c361STejun Heo struct sched_ext_entity *pos; 878bba2c361STejun Heo 879bba2c361STejun Heo if (!(++iter->cnt % SCX_TASK_ITER_BATCH)) { 880bba2c361STejun Heo scx_task_iter_unlock(iter); 881bba2c361STejun Heo cond_resched(); 882bba2c361STejun Heo } 883bba2c361STejun Heo 884bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 885bba2c361STejun Heo if (iter->cgrp) { 886bba2c361STejun Heo while (iter->css_pos) { 887bba2c361STejun Heo struct task_struct *p; 888bba2c361STejun Heo 889bba2c361STejun Heo p = css_task_iter_next(&iter->css_iter); 890bba2c361STejun Heo if (p) 891bba2c361STejun Heo return p; 892bba2c361STejun Heo 893bba2c361STejun Heo css_task_iter_end(&iter->css_iter); 894bba2c361STejun Heo iter->css_pos = css_next_descendant_pre(iter->css_pos, 895bba2c361STejun Heo &iter->cgrp->self); 896bba2c361STejun Heo if (iter->css_pos) 897bba2c361STejun Heo css_task_iter_start(iter->css_pos, CSS_TASK_ITER_WITH_DEAD, 898bba2c361STejun Heo &iter->css_iter); 899bba2c361STejun Heo } 900bba2c361STejun Heo return NULL; 901bba2c361STejun Heo } 902bba2c361STejun Heo #endif 903bba2c361STejun Heo __scx_task_iter_maybe_relock(iter); 904bba2c361STejun Heo 905bba2c361STejun Heo list_for_each_entry(pos, cursor, tasks_node) { 906bba2c361STejun Heo if (&pos->tasks_node == &scx_tasks) 907bba2c361STejun Heo return NULL; 908bba2c361STejun Heo if (!(pos->flags & SCX_TASK_CURSOR)) { 909bba2c361STejun Heo list_move(cursor, &pos->tasks_node); 910bba2c361STejun Heo return container_of(pos, struct task_struct, scx); 911bba2c361STejun Heo } 912bba2c361STejun Heo } 913bba2c361STejun Heo 914bba2c361STejun Heo /* can't happen, should always terminate at scx_tasks above */ 915bba2c361STejun Heo BUG(); 916bba2c361STejun Heo } 917bba2c361STejun Heo 918bba2c361STejun Heo /** 919bba2c361STejun Heo * scx_task_iter_next_locked - Next non-idle task with its rq locked 920bba2c361STejun Heo * @iter: iterator to walk 921bba2c361STejun Heo * 922bba2c361STejun Heo * Visit the non-idle task with its rq lock held. Allows callers to specify 923bba2c361STejun Heo * whether they would like to filter out dead tasks. See scx_task_iter_start() 924bba2c361STejun Heo * for details. 925bba2c361STejun Heo */ 926bba2c361STejun Heo static struct task_struct *scx_task_iter_next_locked(struct scx_task_iter *iter) 927bba2c361STejun Heo { 928bba2c361STejun Heo struct task_struct *p; 929bba2c361STejun Heo 930bba2c361STejun Heo __scx_task_iter_rq_unlock(iter); 931bba2c361STejun Heo 932bba2c361STejun Heo while ((p = scx_task_iter_next(iter))) { 933bba2c361STejun Heo /* 934bba2c361STejun Heo * scx_task_iter is used to prepare and move tasks into SCX 935bba2c361STejun Heo * while loading the BPF scheduler and vice-versa while 936bba2c361STejun Heo * unloading. The init_tasks ("swappers") should be excluded 937bba2c361STejun Heo * from the iteration because: 938bba2c361STejun Heo * 939bba2c361STejun Heo * - It's unsafe to use __setschduler_prio() on an init_task to 940bba2c361STejun Heo * determine the sched_class to use as it won't preserve its 941bba2c361STejun Heo * idle_sched_class. 942bba2c361STejun Heo * 943bba2c361STejun Heo * - ops.init/exit_task() can easily be confused if called with 944bba2c361STejun Heo * init_tasks as they, e.g., share PID 0. 945bba2c361STejun Heo * 946bba2c361STejun Heo * As init_tasks are never scheduled through SCX, they can be 947bba2c361STejun Heo * skipped safely. Note that is_idle_task() which tests %PF_IDLE 948bba2c361STejun Heo * doesn't work here: 949bba2c361STejun Heo * 950bba2c361STejun Heo * - %PF_IDLE may not be set for an init_task whose CPU hasn't 951bba2c361STejun Heo * yet been onlined. 952bba2c361STejun Heo * 953bba2c361STejun Heo * - %PF_IDLE can be set on tasks that are not init_tasks. See 954bba2c361STejun Heo * play_idle_precise() used by CONFIG_IDLE_INJECT. 955bba2c361STejun Heo * 956bba2c361STejun Heo * Test for idle_sched_class as only init_tasks are on it. 957bba2c361STejun Heo */ 958bba2c361STejun Heo if (p->sched_class == &idle_sched_class) 959bba2c361STejun Heo continue; 960bba2c361STejun Heo 961bba2c361STejun Heo iter->rq = task_rq_lock(p, &iter->rf); 962bba2c361STejun Heo iter->locked_task = p; 963bba2c361STejun Heo 964bba2c361STejun Heo /* 965bba2c361STejun Heo * cgroup_task_dead() removes the dead tasks from cset->tasks 966bba2c361STejun Heo * after sched_ext_dead() and cgroup iteration may see tasks 967bba2c361STejun Heo * which already finished sched_ext_dead(). %SCX_TASK_DEAD is 968bba2c361STejun Heo * set by sched_ext_dead() under @p's rq lock. Test it to 969bba2c361STejun Heo * avoid visiting tasks which are already dead from SCX POV. 970bba2c361STejun Heo */ 971bba2c361STejun Heo if (scx_get_task_state(p) == SCX_TASK_DEAD) { 972bba2c361STejun Heo __scx_task_iter_rq_unlock(iter); 973bba2c361STejun Heo continue; 974bba2c361STejun Heo } 975bba2c361STejun Heo 976bba2c361STejun Heo return p; 977bba2c361STejun Heo } 978bba2c361STejun Heo return NULL; 979bba2c361STejun Heo } 980bba2c361STejun Heo 981bba2c361STejun Heo /** 982bba2c361STejun Heo * scx_add_event - Increase an event counter for 'name' by 'cnt' 983bba2c361STejun Heo * @sch: scx_sched to account events for 984bba2c361STejun Heo * @name: an event name defined in struct scx_event_stats 985bba2c361STejun Heo * @cnt: the number of the event occurred 986bba2c361STejun Heo * 987bba2c361STejun Heo * This can be used when preemption is not disabled. 988bba2c361STejun Heo */ 989bba2c361STejun Heo #define scx_add_event(sch, name, cnt) do { \ 990bba2c361STejun Heo this_cpu_add((sch)->pcpu->event_stats.name, (cnt)); \ 991bba2c361STejun Heo trace_sched_ext_event(#name, (cnt)); \ 992bba2c361STejun Heo } while(0) 993bba2c361STejun Heo 994bba2c361STejun Heo /** 995bba2c361STejun Heo * __scx_add_event - Increase an event counter for 'name' by 'cnt' 996bba2c361STejun Heo * @sch: scx_sched to account events for 997bba2c361STejun Heo * @name: an event name defined in struct scx_event_stats 998bba2c361STejun Heo * @cnt: the number of the event occurred 999bba2c361STejun Heo * 1000bba2c361STejun Heo * This should be used only when preemption is disabled. 1001bba2c361STejun Heo */ 1002bba2c361STejun Heo #define __scx_add_event(sch, name, cnt) do { \ 1003bba2c361STejun Heo __this_cpu_add((sch)->pcpu->event_stats.name, (cnt)); \ 1004bba2c361STejun Heo trace_sched_ext_event(#name, cnt); \ 1005bba2c361STejun Heo } while(0) 1006bba2c361STejun Heo 1007bba2c361STejun Heo /** 1008bba2c361STejun Heo * scx_agg_event - Aggregate an event counter 'kind' from 'src_e' to 'dst_e' 1009bba2c361STejun Heo * @dst_e: destination event stats 1010bba2c361STejun Heo * @src_e: source event stats 1011bba2c361STejun Heo * @kind: a kind of event to be aggregated 1012bba2c361STejun Heo */ 1013bba2c361STejun Heo #define scx_agg_event(dst_e, src_e, kind) do { \ 1014bba2c361STejun Heo (dst_e)->kind += READ_ONCE((src_e)->kind); \ 1015bba2c361STejun Heo } while(0) 1016bba2c361STejun Heo 1017bba2c361STejun Heo /** 1018bba2c361STejun Heo * scx_dump_event - Dump an event 'kind' in 'events' to 's' 1019bba2c361STejun Heo * @s: output seq_buf 1020bba2c361STejun Heo * @events: event stats 1021bba2c361STejun Heo * @kind: a kind of event to dump 1022bba2c361STejun Heo */ 1023bba2c361STejun Heo #define scx_dump_event(s, events, kind) do { \ 1024bba2c361STejun Heo dump_line(&(s), "%40s: %16lld", #kind, (events)->kind); \ 1025bba2c361STejun Heo } while (0) 1026bba2c361STejun Heo 1027bba2c361STejun Heo 1028bba2c361STejun Heo static void scx_read_events(struct scx_sched *sch, 1029bba2c361STejun Heo struct scx_event_stats *events); 1030bba2c361STejun Heo 1031bba2c361STejun Heo static enum scx_enable_state scx_enable_state(void) 1032bba2c361STejun Heo { 1033bba2c361STejun Heo return atomic_read(&scx_enable_state_var); 1034bba2c361STejun Heo } 1035bba2c361STejun Heo 1036bba2c361STejun Heo static enum scx_enable_state scx_set_enable_state(enum scx_enable_state to) 1037bba2c361STejun Heo { 1038bba2c361STejun Heo return atomic_xchg(&scx_enable_state_var, to); 1039bba2c361STejun Heo } 1040bba2c361STejun Heo 1041bba2c361STejun Heo static bool scx_tryset_enable_state(enum scx_enable_state to, 1042bba2c361STejun Heo enum scx_enable_state from) 1043bba2c361STejun Heo { 1044bba2c361STejun Heo int from_v = from; 1045bba2c361STejun Heo 1046bba2c361STejun Heo return atomic_try_cmpxchg(&scx_enable_state_var, &from_v, to); 1047bba2c361STejun Heo } 1048bba2c361STejun Heo 1049bba2c361STejun Heo /** 1050bba2c361STejun Heo * wait_ops_state - Busy-wait the specified ops state to end 1051bba2c361STejun Heo * @p: target task 1052bba2c361STejun Heo * @opss: state to wait the end of 1053bba2c361STejun Heo * 1054bba2c361STejun Heo * Busy-wait for @p to transition out of @opss. This can only be used when the 1055bba2c361STejun Heo * state part of @opss is %SCX_QUEUEING or %SCX_DISPATCHING. This function also 1056bba2c361STejun Heo * has load_acquire semantics to ensure that the caller can see the updates made 1057bba2c361STejun Heo * in the enqueueing and dispatching paths. 1058bba2c361STejun Heo */ 1059bba2c361STejun Heo static void wait_ops_state(struct task_struct *p, unsigned long opss) 1060bba2c361STejun Heo { 1061bba2c361STejun Heo do { 1062bba2c361STejun Heo cpu_relax(); 1063bba2c361STejun Heo } while (atomic_long_read_acquire(&p->scx.ops_state) == opss); 1064bba2c361STejun Heo } 1065bba2c361STejun Heo 1066bba2c361STejun Heo static inline bool __cpu_valid(s32 cpu) 1067bba2c361STejun Heo { 1068bba2c361STejun Heo return likely(cpu >= 0 && cpu < nr_cpu_ids && cpu_possible(cpu)); 1069bba2c361STejun Heo } 1070bba2c361STejun Heo 1071bba2c361STejun Heo /** 1072bba2c361STejun Heo * scx_cpu_valid - Verify a cpu number, to be used on ops input args 1073bba2c361STejun Heo * @sch: scx_sched to abort on error 1074bba2c361STejun Heo * @cpu: cpu number which came from a BPF ops 1075bba2c361STejun Heo * @where: extra information reported on error 1076bba2c361STejun Heo * 1077bba2c361STejun Heo * @cpu is a cpu number which came from the BPF scheduler and can be any value. 1078bba2c361STejun Heo * Verify that it is in range and one of the possible cpus. If invalid, trigger 1079bba2c361STejun Heo * an ops error. 1080bba2c361STejun Heo */ 1081bba2c361STejun Heo bool scx_cpu_valid(struct scx_sched *sch, s32 cpu, const char *where) 1082bba2c361STejun Heo { 1083bba2c361STejun Heo if (__cpu_valid(cpu)) { 1084bba2c361STejun Heo return true; 1085bba2c361STejun Heo } else { 1086bba2c361STejun Heo scx_error(sch, "invalid CPU %d%s%s", cpu, where ? " " : "", where ?: ""); 1087bba2c361STejun Heo return false; 1088bba2c361STejun Heo } 1089bba2c361STejun Heo } 1090bba2c361STejun Heo 1091bba2c361STejun Heo /** 1092bba2c361STejun Heo * ops_sanitize_err - Sanitize a -errno value 1093bba2c361STejun Heo * @sch: scx_sched to error out on error 1094bba2c361STejun Heo * @ops_name: operation to blame on failure 1095bba2c361STejun Heo * @err: -errno value to sanitize 1096bba2c361STejun Heo * 1097bba2c361STejun Heo * Verify @err is a valid -errno. If not, trigger scx_error() and return 1098bba2c361STejun Heo * -%EPROTO. This is necessary because returning a rogue -errno up the chain can 1099bba2c361STejun Heo * cause misbehaviors. For an example, a large negative return from 1100bba2c361STejun Heo * ops.init_task() triggers an oops when passed up the call chain because the 1101bba2c361STejun Heo * value fails IS_ERR() test after being encoded with ERR_PTR() and then is 1102bba2c361STejun Heo * handled as a pointer. 1103bba2c361STejun Heo */ 1104bba2c361STejun Heo static int ops_sanitize_err(struct scx_sched *sch, const char *ops_name, s32 err) 1105bba2c361STejun Heo { 1106bba2c361STejun Heo if (err < 0 && err >= -MAX_ERRNO) 1107bba2c361STejun Heo return err; 1108bba2c361STejun Heo 1109bba2c361STejun Heo scx_error(sch, "ops.%s() returned an invalid errno %d", ops_name, err); 1110bba2c361STejun Heo return -EPROTO; 1111bba2c361STejun Heo } 1112bba2c361STejun Heo 1113bba2c361STejun Heo static void deferred_bal_cb_workfn(struct rq *rq) 1114bba2c361STejun Heo { 1115bba2c361STejun Heo run_deferred(rq); 1116bba2c361STejun Heo } 1117bba2c361STejun Heo 1118bba2c361STejun Heo static void deferred_irq_workfn(struct irq_work *irq_work) 1119bba2c361STejun Heo { 1120bba2c361STejun Heo struct rq *rq = container_of(irq_work, struct rq, scx.deferred_irq_work); 1121bba2c361STejun Heo 1122bba2c361STejun Heo raw_spin_rq_lock(rq); 1123bba2c361STejun Heo run_deferred(rq); 1124bba2c361STejun Heo raw_spin_rq_unlock(rq); 1125bba2c361STejun Heo } 1126bba2c361STejun Heo 1127bba2c361STejun Heo /** 1128bba2c361STejun Heo * schedule_deferred - Schedule execution of deferred actions on an rq 1129bba2c361STejun Heo * @rq: target rq 1130bba2c361STejun Heo * 1131bba2c361STejun Heo * Schedule execution of deferred actions on @rq. Deferred actions are executed 1132bba2c361STejun Heo * with @rq locked but unpinned, and thus can unlock @rq to e.g. migrate tasks 1133bba2c361STejun Heo * to other rqs. 1134bba2c361STejun Heo */ 1135bba2c361STejun Heo static void schedule_deferred(struct rq *rq) 1136bba2c361STejun Heo { 1137bba2c361STejun Heo /* 1138bba2c361STejun Heo * This is the fallback when schedule_deferred_locked() can't use 1139bba2c361STejun Heo * the cheaper balance callback or wakeup hook paths (the target 1140bba2c361STejun Heo * CPU is not in balance or wakeup). Currently, this is primarily 1141bba2c361STejun Heo * hit by reenqueue operations targeting a remote CPU. 1142bba2c361STejun Heo * 1143bba2c361STejun Heo * Queue on the target CPU. The deferred work can run from any CPU 1144bba2c361STejun Heo * correctly - the _locked() path already processes remote rqs from 1145bba2c361STejun Heo * the calling CPU - but targeting the owning CPU allows IPI delivery 1146bba2c361STejun Heo * without waiting for the calling CPU to re-enable IRQs and is 1147bba2c361STejun Heo * cheaper as the reenqueue runs locally. 1148bba2c361STejun Heo */ 1149bba2c361STejun Heo irq_work_queue_on(&rq->scx.deferred_irq_work, cpu_of(rq)); 1150bba2c361STejun Heo } 1151bba2c361STejun Heo 1152bba2c361STejun Heo /** 1153bba2c361STejun Heo * schedule_deferred_locked - Schedule execution of deferred actions on an rq 1154bba2c361STejun Heo * @rq: target rq 1155bba2c361STejun Heo * 1156bba2c361STejun Heo * Schedule execution of deferred actions on @rq. Equivalent to 1157bba2c361STejun Heo * schedule_deferred() but requires @rq to be locked and can be more efficient. 1158bba2c361STejun Heo */ 1159bba2c361STejun Heo static void schedule_deferred_locked(struct rq *rq) 1160bba2c361STejun Heo { 1161bba2c361STejun Heo lockdep_assert_rq_held(rq); 1162bba2c361STejun Heo 1163bba2c361STejun Heo /* 1164bba2c361STejun Heo * If in the middle of waking up a task, task_woken_scx() will be called 1165bba2c361STejun Heo * afterwards which will then run the deferred actions, no need to 1166bba2c361STejun Heo * schedule anything. 1167bba2c361STejun Heo */ 1168bba2c361STejun Heo if (rq->scx.flags & SCX_RQ_IN_WAKEUP) 1169bba2c361STejun Heo return; 1170bba2c361STejun Heo 1171bba2c361STejun Heo /* Don't do anything if there already is a deferred operation. */ 1172bba2c361STejun Heo if (rq->scx.flags & SCX_RQ_BAL_CB_PENDING) 1173bba2c361STejun Heo return; 1174bba2c361STejun Heo 1175bba2c361STejun Heo /* 1176bba2c361STejun Heo * If in balance, the balance callbacks will be called before rq lock is 1177bba2c361STejun Heo * released. Schedule one. 1178bba2c361STejun Heo * 1179bba2c361STejun Heo * 1180bba2c361STejun Heo * We can't directly insert the callback into the 1181bba2c361STejun Heo * rq's list: The call can drop its lock and make the pending balance 1182bba2c361STejun Heo * callback visible to unrelated code paths that call rq_pin_lock(). 1183bba2c361STejun Heo * 1184bba2c361STejun Heo * Just let balance_one() know that it must do it itself. 1185bba2c361STejun Heo */ 1186bba2c361STejun Heo if (rq->scx.flags & SCX_RQ_IN_BALANCE) { 1187bba2c361STejun Heo rq->scx.flags |= SCX_RQ_BAL_CB_PENDING; 1188bba2c361STejun Heo return; 1189bba2c361STejun Heo } 1190bba2c361STejun Heo 1191bba2c361STejun Heo /* 1192bba2c361STejun Heo * No scheduler hooks available. Use the generic irq_work path. The 1193bba2c361STejun Heo * above WAKEUP and BALANCE paths should cover most of the cases and the 1194bba2c361STejun Heo * time to IRQ re-enable shouldn't be long. 1195bba2c361STejun Heo */ 1196bba2c361STejun Heo schedule_deferred(rq); 1197bba2c361STejun Heo } 1198bba2c361STejun Heo 1199bba2c361STejun Heo static void schedule_dsq_reenq(struct scx_sched *sch, struct scx_dispatch_q *dsq, 1200bba2c361STejun Heo u64 reenq_flags, struct rq *locked_rq) 1201bba2c361STejun Heo { 1202bba2c361STejun Heo struct rq *rq; 1203bba2c361STejun Heo 1204bba2c361STejun Heo /* 1205bba2c361STejun Heo * Allowing reenqueues doesn't make sense while bypassing. This also 1206bba2c361STejun Heo * blocks from new reenqueues to be scheduled on dead scheds. 1207bba2c361STejun Heo */ 1208bba2c361STejun Heo if (unlikely(READ_ONCE(sch->bypass_depth))) 1209bba2c361STejun Heo return; 1210bba2c361STejun Heo 1211bba2c361STejun Heo if (dsq->id == SCX_DSQ_LOCAL) { 1212bba2c361STejun Heo rq = container_of(dsq, struct rq, scx.local_dsq); 1213bba2c361STejun Heo 1214bba2c361STejun Heo struct scx_sched_pcpu *sch_pcpu = per_cpu_ptr(sch->pcpu, cpu_of(rq)); 1215bba2c361STejun Heo struct scx_deferred_reenq_local *drl = &sch_pcpu->deferred_reenq_local; 1216bba2c361STejun Heo 1217bba2c361STejun Heo /* 1218bba2c361STejun Heo * Pairs with smp_mb() in process_deferred_reenq_locals() and 1219bba2c361STejun Heo * guarantees that there is a reenq_local() afterwards. 1220bba2c361STejun Heo */ 1221bba2c361STejun Heo smp_mb(); 1222bba2c361STejun Heo 1223bba2c361STejun Heo if (list_empty(&drl->node) || 1224bba2c361STejun Heo (READ_ONCE(drl->flags) & reenq_flags) != reenq_flags) { 1225bba2c361STejun Heo 1226bba2c361STejun Heo guard(raw_spinlock_irqsave)(&rq->scx.deferred_reenq_lock); 1227bba2c361STejun Heo 1228bba2c361STejun Heo if (list_empty(&drl->node)) 1229bba2c361STejun Heo list_move_tail(&drl->node, &rq->scx.deferred_reenq_locals); 1230bba2c361STejun Heo WRITE_ONCE(drl->flags, drl->flags | reenq_flags); 1231bba2c361STejun Heo } 1232bba2c361STejun Heo } else if (!(dsq->id & SCX_DSQ_FLAG_BUILTIN)) { 1233bba2c361STejun Heo rq = this_rq(); 1234bba2c361STejun Heo 1235bba2c361STejun Heo struct scx_dsq_pcpu *dsq_pcpu = per_cpu_ptr(dsq->pcpu, cpu_of(rq)); 1236bba2c361STejun Heo struct scx_deferred_reenq_user *dru = &dsq_pcpu->deferred_reenq_user; 1237bba2c361STejun Heo 1238bba2c361STejun Heo /* 1239bba2c361STejun Heo * Pairs with smp_mb() in process_deferred_reenq_users() and 1240bba2c361STejun Heo * guarantees that there is a reenq_user() afterwards. 1241bba2c361STejun Heo */ 1242bba2c361STejun Heo smp_mb(); 1243bba2c361STejun Heo 1244bba2c361STejun Heo if (list_empty(&dru->node) || 1245bba2c361STejun Heo (READ_ONCE(dru->flags) & reenq_flags) != reenq_flags) { 1246bba2c361STejun Heo 1247bba2c361STejun Heo guard(raw_spinlock_irqsave)(&rq->scx.deferred_reenq_lock); 1248bba2c361STejun Heo 1249bba2c361STejun Heo if (list_empty(&dru->node)) 1250bba2c361STejun Heo list_move_tail(&dru->node, &rq->scx.deferred_reenq_users); 1251bba2c361STejun Heo WRITE_ONCE(dru->flags, dru->flags | reenq_flags); 1252bba2c361STejun Heo } 1253bba2c361STejun Heo } else { 1254bba2c361STejun Heo scx_error(sch, "DSQ 0x%llx not allowed for reenq", dsq->id); 1255bba2c361STejun Heo return; 1256bba2c361STejun Heo } 1257bba2c361STejun Heo 1258bba2c361STejun Heo if (rq == locked_rq) 1259bba2c361STejun Heo schedule_deferred_locked(rq); 1260bba2c361STejun Heo else 1261bba2c361STejun Heo schedule_deferred(rq); 1262bba2c361STejun Heo } 1263bba2c361STejun Heo 1264bba2c361STejun Heo static void schedule_reenq_local(struct rq *rq, u64 reenq_flags) 1265bba2c361STejun Heo { 1266bba2c361STejun Heo struct scx_sched *root = rcu_dereference_sched(scx_root); 1267bba2c361STejun Heo 1268bba2c361STejun Heo if (WARN_ON_ONCE(!root)) 1269bba2c361STejun Heo return; 1270bba2c361STejun Heo 1271bba2c361STejun Heo schedule_dsq_reenq(root, &rq->scx.local_dsq, reenq_flags, rq); 1272bba2c361STejun Heo } 1273bba2c361STejun Heo 1274bba2c361STejun Heo /** 1275bba2c361STejun Heo * touch_core_sched - Update timestamp used for core-sched task ordering 1276bba2c361STejun Heo * @rq: rq to read clock from, must be locked 1277bba2c361STejun Heo * @p: task to update the timestamp for 1278bba2c361STejun Heo * 1279bba2c361STejun Heo * Update @p->scx.core_sched_at timestamp. This is used by scx_prio_less() to 1280bba2c361STejun Heo * implement global or local-DSQ FIFO ordering for core-sched. Should be called 1281bba2c361STejun Heo * when a task becomes runnable and its turn on the CPU ends (e.g. slice 1282bba2c361STejun Heo * exhaustion). 1283bba2c361STejun Heo */ 1284bba2c361STejun Heo static void touch_core_sched(struct rq *rq, struct task_struct *p) 1285bba2c361STejun Heo { 1286bba2c361STejun Heo lockdep_assert_rq_held(rq); 1287bba2c361STejun Heo 1288bba2c361STejun Heo #ifdef CONFIG_SCHED_CORE 1289bba2c361STejun Heo /* 1290bba2c361STejun Heo * It's okay to update the timestamp spuriously. Use 1291bba2c361STejun Heo * sched_core_disabled() which is cheaper than enabled(). 1292bba2c361STejun Heo * 1293bba2c361STejun Heo * As this is used to determine ordering between tasks of sibling CPUs, 1294bba2c361STejun Heo * it may be better to use per-core dispatch sequence instead. 1295bba2c361STejun Heo */ 1296bba2c361STejun Heo if (!sched_core_disabled()) 1297bba2c361STejun Heo p->scx.core_sched_at = sched_clock_cpu(cpu_of(rq)); 1298bba2c361STejun Heo #endif 1299bba2c361STejun Heo } 1300bba2c361STejun Heo 1301bba2c361STejun Heo /** 1302bba2c361STejun Heo * touch_core_sched_dispatch - Update core-sched timestamp on dispatch 1303bba2c361STejun Heo * @rq: rq to read clock from, must be locked 1304bba2c361STejun Heo * @p: task being dispatched 1305bba2c361STejun Heo * 1306bba2c361STejun Heo * If the BPF scheduler implements custom core-sched ordering via 1307bba2c361STejun Heo * ops.core_sched_before(), @p->scx.core_sched_at is used to implement FIFO 1308bba2c361STejun Heo * ordering within each local DSQ. This function is called from dispatch paths 1309bba2c361STejun Heo * and updates @p->scx.core_sched_at if custom core-sched ordering is in effect. 1310bba2c361STejun Heo */ 1311bba2c361STejun Heo static void touch_core_sched_dispatch(struct rq *rq, struct task_struct *p) 1312bba2c361STejun Heo { 1313bba2c361STejun Heo lockdep_assert_rq_held(rq); 1314bba2c361STejun Heo 1315bba2c361STejun Heo #ifdef CONFIG_SCHED_CORE 1316bba2c361STejun Heo if (unlikely(SCX_HAS_OP(scx_root, core_sched_before))) 1317bba2c361STejun Heo touch_core_sched(rq, p); 1318bba2c361STejun Heo #endif 1319bba2c361STejun Heo } 1320bba2c361STejun Heo 1321bba2c361STejun Heo static void update_curr_scx(struct rq *rq) 1322bba2c361STejun Heo { 1323bba2c361STejun Heo struct task_struct *curr = rq->curr; 1324bba2c361STejun Heo s64 delta_exec; 1325bba2c361STejun Heo 1326bba2c361STejun Heo delta_exec = update_curr_common(rq); 1327bba2c361STejun Heo if (unlikely(delta_exec <= 0)) 1328bba2c361STejun Heo return; 1329bba2c361STejun Heo 1330bba2c361STejun Heo if (curr->scx.slice != SCX_SLICE_INF) { 1331bba2c361STejun Heo curr->scx.slice -= min_t(u64, curr->scx.slice, delta_exec); 1332bba2c361STejun Heo if (!curr->scx.slice) 1333bba2c361STejun Heo touch_core_sched(rq, curr); 1334bba2c361STejun Heo } 1335bba2c361STejun Heo 1336bba2c361STejun Heo dl_server_update(&rq->ext_server, delta_exec); 1337bba2c361STejun Heo } 1338bba2c361STejun Heo 1339bba2c361STejun Heo static bool scx_dsq_priq_less(struct rb_node *node_a, 1340bba2c361STejun Heo const struct rb_node *node_b) 1341bba2c361STejun Heo { 1342bba2c361STejun Heo const struct task_struct *a = 1343bba2c361STejun Heo container_of(node_a, struct task_struct, scx.dsq_priq); 1344bba2c361STejun Heo const struct task_struct *b = 1345bba2c361STejun Heo container_of(node_b, struct task_struct, scx.dsq_priq); 1346bba2c361STejun Heo 1347bba2c361STejun Heo return time_before64(a->scx.dsq_vtime, b->scx.dsq_vtime); 1348bba2c361STejun Heo } 1349bba2c361STejun Heo 1350bba2c361STejun Heo static void dsq_inc_nr(struct scx_dispatch_q *dsq, struct task_struct *p, u64 enq_flags) 1351bba2c361STejun Heo { 1352bba2c361STejun Heo /* scx_bpf_dsq_nr_queued() reads ->nr without locking, use WRITE_ONCE() */ 1353bba2c361STejun Heo WRITE_ONCE(dsq->nr, dsq->nr + 1); 1354bba2c361STejun Heo 1355bba2c361STejun Heo /* 1356bba2c361STejun Heo * Once @p reaches a local DSQ, it can only leave it by being dispatched 1357bba2c361STejun Heo * to the CPU or dequeued. In both cases, the only way @p can go back to 1358bba2c361STejun Heo * the BPF sched is through enqueueing. If being inserted into a local 1359bba2c361STejun Heo * DSQ with IMMED, persist the state until the next enqueueing event in 1360bba2c361STejun Heo * do_enqueue_task() so that we can maintain IMMED protection through 1361bba2c361STejun Heo * e.g. SAVE/RESTORE cycles and slice extensions. 1362bba2c361STejun Heo */ 1363bba2c361STejun Heo if (enq_flags & SCX_ENQ_IMMED) { 1364bba2c361STejun Heo if (unlikely(dsq->id != SCX_DSQ_LOCAL)) { 1365bba2c361STejun Heo WARN_ON_ONCE(!(enq_flags & SCX_ENQ_GDSQ_FALLBACK)); 1366bba2c361STejun Heo return; 1367bba2c361STejun Heo } 1368bba2c361STejun Heo p->scx.flags |= SCX_TASK_IMMED; 1369bba2c361STejun Heo } 1370bba2c361STejun Heo 1371bba2c361STejun Heo if (p->scx.flags & SCX_TASK_IMMED) { 1372bba2c361STejun Heo struct rq *rq = container_of(dsq, struct rq, scx.local_dsq); 1373bba2c361STejun Heo 1374bba2c361STejun Heo if (WARN_ON_ONCE(dsq->id != SCX_DSQ_LOCAL)) 1375bba2c361STejun Heo return; 1376bba2c361STejun Heo 1377bba2c361STejun Heo rq->scx.nr_immed++; 1378bba2c361STejun Heo 1379bba2c361STejun Heo /* 1380bba2c361STejun Heo * If @rq already had other tasks or the current task is not 1381bba2c361STejun Heo * done yet, @p can't go on the CPU immediately. Re-enqueue. 1382bba2c361STejun Heo */ 1383bba2c361STejun Heo if (unlikely(dsq->nr > 1 || !rq_is_open(rq, enq_flags))) 1384bba2c361STejun Heo schedule_reenq_local(rq, 0); 1385bba2c361STejun Heo } 1386bba2c361STejun Heo } 1387bba2c361STejun Heo 1388bba2c361STejun Heo static void dsq_dec_nr(struct scx_dispatch_q *dsq, struct task_struct *p) 1389bba2c361STejun Heo { 1390bba2c361STejun Heo /* see dsq_inc_nr() */ 1391bba2c361STejun Heo WRITE_ONCE(dsq->nr, dsq->nr - 1); 1392bba2c361STejun Heo 1393bba2c361STejun Heo if (p->scx.flags & SCX_TASK_IMMED) { 1394bba2c361STejun Heo struct rq *rq = container_of(dsq, struct rq, scx.local_dsq); 1395bba2c361STejun Heo 1396bba2c361STejun Heo if (WARN_ON_ONCE(dsq->id != SCX_DSQ_LOCAL) || 1397bba2c361STejun Heo WARN_ON_ONCE(rq->scx.nr_immed <= 0)) 1398bba2c361STejun Heo return; 1399bba2c361STejun Heo 1400bba2c361STejun Heo rq->scx.nr_immed--; 1401bba2c361STejun Heo } 1402bba2c361STejun Heo } 1403bba2c361STejun Heo 1404bba2c361STejun Heo static void refill_task_slice_dfl(struct scx_sched *sch, struct task_struct *p) 1405bba2c361STejun Heo { 1406bba2c361STejun Heo p->scx.slice = READ_ONCE(sch->slice_dfl); 1407bba2c361STejun Heo __scx_add_event(sch, SCX_EV_REFILL_SLICE_DFL, 1); 1408bba2c361STejun Heo } 1409bba2c361STejun Heo 1410bba2c361STejun Heo /* 1411bba2c361STejun Heo * Return true if @p is moving due to an internal SCX migration, false 1412bba2c361STejun Heo * otherwise. 1413bba2c361STejun Heo */ 1414bba2c361STejun Heo static inline bool task_scx_migrating(struct task_struct *p) 1415bba2c361STejun Heo { 1416bba2c361STejun Heo /* 1417bba2c361STejun Heo * We only need to check sticky_cpu: it is set to the destination 1418bba2c361STejun Heo * CPU in move_remote_task_to_local_dsq() before deactivate_task() 1419bba2c361STejun Heo * and cleared when the task is enqueued on the destination, so it 1420bba2c361STejun Heo * is only non-negative during an internal SCX migration. 1421bba2c361STejun Heo */ 1422bba2c361STejun Heo return p->scx.sticky_cpu >= 0; 1423bba2c361STejun Heo } 1424bba2c361STejun Heo 1425bba2c361STejun Heo /* 1426bba2c361STejun Heo * Call ops.dequeue() if the task is in BPF custody and not migrating. 1427bba2c361STejun Heo * Clears %SCX_TASK_IN_CUSTODY when the callback is invoked. 1428bba2c361STejun Heo */ 1429bba2c361STejun Heo static void call_task_dequeue(struct scx_sched *sch, struct rq *rq, 1430bba2c361STejun Heo struct task_struct *p, u64 deq_flags) 1431bba2c361STejun Heo { 1432bba2c361STejun Heo if (!(p->scx.flags & SCX_TASK_IN_CUSTODY) || task_scx_migrating(p)) 1433bba2c361STejun Heo return; 1434bba2c361STejun Heo 1435bba2c361STejun Heo if (SCX_HAS_OP(sch, dequeue)) 1436bba2c361STejun Heo SCX_CALL_OP_TASK(sch, dequeue, rq, p, deq_flags); 1437bba2c361STejun Heo 1438bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_IN_CUSTODY; 1439bba2c361STejun Heo } 1440bba2c361STejun Heo 1441bba2c361STejun Heo static void local_dsq_post_enq(struct scx_sched *sch, struct scx_dispatch_q *dsq, 1442bba2c361STejun Heo struct task_struct *p, u64 enq_flags) 1443bba2c361STejun Heo { 1444bba2c361STejun Heo struct rq *rq = container_of(dsq, struct rq, scx.local_dsq); 1445bba2c361STejun Heo 1446bba2c361STejun Heo call_task_dequeue(sch, rq, p, 0); 1447bba2c361STejun Heo 1448bba2c361STejun Heo /* 1449bba2c361STejun Heo * Note that @rq's lock may be dropped between this enqueue and @p 1450bba2c361STejun Heo * actually getting on CPU. This gives higher-class tasks (e.g. RT) 1451bba2c361STejun Heo * an opportunity to wake up on @rq and prevent @p from running. 1452bba2c361STejun Heo * Here are some concrete examples: 1453bba2c361STejun Heo * 1454bba2c361STejun Heo * Example 1: 1455bba2c361STejun Heo * 1456bba2c361STejun Heo * We dispatch two tasks from a single ops.dispatch(): 1457bba2c361STejun Heo * - First, a local task to this CPU's local DSQ; 1458bba2c361STejun Heo * - Second, a local/remote task to a remote CPU's local DSQ. 1459bba2c361STejun Heo * We must drop the local rq lock in order to finish the second 1460bba2c361STejun Heo * dispatch. In that time, an RT task can wake up on the local rq. 1461bba2c361STejun Heo * 1462bba2c361STejun Heo * Example 2: 1463bba2c361STejun Heo * 1464bba2c361STejun Heo * We dispatch a local/remote task to a remote CPU's local DSQ. 1465bba2c361STejun Heo * We must drop the remote rq lock before the dispatched task can run, 1466bba2c361STejun Heo * which gives an RT task an opportunity to wake up on the remote rq. 1467bba2c361STejun Heo * 1468bba2c361STejun Heo * Both examples work the same if we replace dispatching with moving 1469bba2c361STejun Heo * the tasks from a user-created DSQ. 1470bba2c361STejun Heo * 1471bba2c361STejun Heo * We must detect these wakeups so that we can re-enqueue IMMED tasks 1472bba2c361STejun Heo * from @rq's local DSQ. scx_wakeup_preempt() serves exactly this 1473bba2c361STejun Heo * purpose, but for it to be invoked, we must ensure that we bump 1474bba2c361STejun Heo * @rq->next_class to &ext_sched_class if it's currently idle. 1475bba2c361STejun Heo * 1476bba2c361STejun Heo * wakeup_preempt() does the bumping, and since we only invoke it if 1477bba2c361STejun Heo * @rq->next_class is below &ext_sched_class, it will also 1478bba2c361STejun Heo * resched_curr(rq). 1479bba2c361STejun Heo */ 1480bba2c361STejun Heo if (sched_class_above(p->sched_class, rq->next_class)) 1481bba2c361STejun Heo wakeup_preempt(rq, p, 0); 1482bba2c361STejun Heo 1483bba2c361STejun Heo /* 1484bba2c361STejun Heo * If @rq is in balance, the CPU is already vacant and looking for the 1485bba2c361STejun Heo * next task to run. No need to preempt or trigger resched after moving 1486bba2c361STejun Heo * @p into its local DSQ. 1487bba2c361STejun Heo * Note that the wakeup_preempt() above may have already triggered 1488bba2c361STejun Heo * a resched if @rq->next_class was idle. It's harmless, since 1489bba2c361STejun Heo * need_resched is cleared immediately after task pick. 1490bba2c361STejun Heo */ 1491bba2c361STejun Heo if (rq->scx.flags & SCX_RQ_IN_BALANCE) 1492bba2c361STejun Heo return; 1493bba2c361STejun Heo 1494bba2c361STejun Heo if ((enq_flags & SCX_ENQ_PREEMPT) && p != rq->curr && 1495bba2c361STejun Heo rq->curr->sched_class == &ext_sched_class) { 1496bba2c361STejun Heo rq->curr->scx.slice = 0; 1497bba2c361STejun Heo resched_curr(rq); 1498bba2c361STejun Heo } 1499bba2c361STejun Heo } 1500bba2c361STejun Heo 1501bba2c361STejun Heo static void dispatch_enqueue(struct scx_sched *sch, struct rq *rq, 1502bba2c361STejun Heo struct scx_dispatch_q *dsq, struct task_struct *p, 1503bba2c361STejun Heo u64 enq_flags) 1504bba2c361STejun Heo { 1505bba2c361STejun Heo bool is_local = dsq->id == SCX_DSQ_LOCAL; 1506bba2c361STejun Heo 1507bba2c361STejun Heo WARN_ON_ONCE(p->scx.dsq || !list_empty(&p->scx.dsq_list.node)); 1508bba2c361STejun Heo WARN_ON_ONCE((p->scx.dsq_flags & SCX_TASK_DSQ_ON_PRIQ) || 1509bba2c361STejun Heo !RB_EMPTY_NODE(&p->scx.dsq_priq)); 1510bba2c361STejun Heo 1511bba2c361STejun Heo if (!is_local) { 1512bba2c361STejun Heo raw_spin_lock_nested(&dsq->lock, 1513bba2c361STejun Heo (enq_flags & SCX_ENQ_NESTED) ? SINGLE_DEPTH_NESTING : 0); 1514bba2c361STejun Heo 1515bba2c361STejun Heo if (unlikely(dsq->id == SCX_DSQ_INVALID)) { 1516bba2c361STejun Heo scx_error(sch, "attempting to dispatch to a destroyed dsq"); 1517bba2c361STejun Heo /* fall back to the global dsq */ 1518bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 1519bba2c361STejun Heo dsq = find_global_dsq(sch, task_cpu(p)); 1520bba2c361STejun Heo raw_spin_lock(&dsq->lock); 1521bba2c361STejun Heo } 1522bba2c361STejun Heo } 1523bba2c361STejun Heo 1524bba2c361STejun Heo if (unlikely((dsq->id & SCX_DSQ_FLAG_BUILTIN) && 1525bba2c361STejun Heo (enq_flags & SCX_ENQ_DSQ_PRIQ))) { 1526bba2c361STejun Heo /* 1527bba2c361STejun Heo * SCX_DSQ_LOCAL and SCX_DSQ_GLOBAL DSQs always consume from 1528bba2c361STejun Heo * their FIFO queues. To avoid confusion and accidentally 1529bba2c361STejun Heo * starving vtime-dispatched tasks by FIFO-dispatched tasks, we 1530bba2c361STejun Heo * disallow any internal DSQ from doing vtime ordering of 1531bba2c361STejun Heo * tasks. 1532bba2c361STejun Heo */ 1533bba2c361STejun Heo scx_error(sch, "cannot use vtime ordering for built-in DSQs"); 1534bba2c361STejun Heo enq_flags &= ~SCX_ENQ_DSQ_PRIQ; 1535bba2c361STejun Heo } 1536bba2c361STejun Heo 1537bba2c361STejun Heo if (enq_flags & SCX_ENQ_DSQ_PRIQ) { 1538bba2c361STejun Heo struct rb_node *rbp; 1539bba2c361STejun Heo 1540bba2c361STejun Heo /* 1541bba2c361STejun Heo * A PRIQ DSQ shouldn't be using FIFO enqueueing. As tasks are 1542bba2c361STejun Heo * linked to both the rbtree and list on PRIQs, this can only be 1543bba2c361STejun Heo * tested easily when adding the first task. 1544bba2c361STejun Heo */ 1545bba2c361STejun Heo if (unlikely(RB_EMPTY_ROOT(&dsq->priq) && 1546bba2c361STejun Heo nldsq_next_task(dsq, NULL, false))) 1547bba2c361STejun Heo scx_error(sch, "DSQ ID 0x%016llx already had FIFO-enqueued tasks", 1548bba2c361STejun Heo dsq->id); 1549bba2c361STejun Heo 1550bba2c361STejun Heo p->scx.dsq_flags |= SCX_TASK_DSQ_ON_PRIQ; 1551bba2c361STejun Heo rb_add(&p->scx.dsq_priq, &dsq->priq, scx_dsq_priq_less); 1552bba2c361STejun Heo 1553bba2c361STejun Heo /* 1554bba2c361STejun Heo * Find the previous task and insert after it on the list so 1555bba2c361STejun Heo * that @dsq->list is vtime ordered. 1556bba2c361STejun Heo */ 1557bba2c361STejun Heo rbp = rb_prev(&p->scx.dsq_priq); 1558bba2c361STejun Heo if (rbp) { 1559bba2c361STejun Heo struct task_struct *prev = 1560bba2c361STejun Heo container_of(rbp, struct task_struct, 1561bba2c361STejun Heo scx.dsq_priq); 1562bba2c361STejun Heo list_add(&p->scx.dsq_list.node, &prev->scx.dsq_list.node); 1563bba2c361STejun Heo /* first task unchanged - no update needed */ 1564bba2c361STejun Heo } else { 1565bba2c361STejun Heo list_add(&p->scx.dsq_list.node, &dsq->list); 1566bba2c361STejun Heo /* not builtin and new task is at head - use fastpath */ 1567bba2c361STejun Heo rcu_assign_pointer(dsq->first_task, p); 1568bba2c361STejun Heo } 1569bba2c361STejun Heo } else { 1570bba2c361STejun Heo /* a FIFO DSQ shouldn't be using PRIQ enqueuing */ 1571bba2c361STejun Heo if (unlikely(!RB_EMPTY_ROOT(&dsq->priq))) 1572bba2c361STejun Heo scx_error(sch, "DSQ ID 0x%016llx already had PRIQ-enqueued tasks", 1573bba2c361STejun Heo dsq->id); 1574bba2c361STejun Heo 1575bba2c361STejun Heo if (enq_flags & (SCX_ENQ_HEAD | SCX_ENQ_PREEMPT)) { 1576bba2c361STejun Heo list_add(&p->scx.dsq_list.node, &dsq->list); 1577bba2c361STejun Heo /* new task inserted at head - use fastpath */ 1578bba2c361STejun Heo if (!(dsq->id & SCX_DSQ_FLAG_BUILTIN)) 1579bba2c361STejun Heo rcu_assign_pointer(dsq->first_task, p); 1580bba2c361STejun Heo } else { 1581bba2c361STejun Heo /* 1582bba2c361STejun Heo * dsq->list can contain parked BPF iterator cursors, so 1583bba2c361STejun Heo * list_empty() here isn't a reliable proxy for "no real 1584bba2c361STejun Heo * task in the DSQ". Test dsq->first_task directly. 1585bba2c361STejun Heo */ 1586bba2c361STejun Heo list_add_tail(&p->scx.dsq_list.node, &dsq->list); 1587bba2c361STejun Heo if (!dsq->first_task && !(dsq->id & SCX_DSQ_FLAG_BUILTIN)) 1588bba2c361STejun Heo rcu_assign_pointer(dsq->first_task, p); 1589bba2c361STejun Heo } 1590bba2c361STejun Heo } 1591bba2c361STejun Heo 1592bba2c361STejun Heo /* seq records the order tasks are queued, used by BPF DSQ iterator */ 1593bba2c361STejun Heo WRITE_ONCE(dsq->seq, dsq->seq + 1); 1594bba2c361STejun Heo p->scx.dsq_seq = dsq->seq; 1595bba2c361STejun Heo 1596bba2c361STejun Heo dsq_inc_nr(dsq, p, enq_flags); 1597bba2c361STejun Heo p->scx.dsq = dsq; 1598bba2c361STejun Heo 1599bba2c361STejun Heo /* 1600bba2c361STejun Heo * Update custody and call ops.dequeue() before clearing ops_state: 1601bba2c361STejun Heo * once ops_state is cleared, waiters in ops_dequeue() can proceed 1602bba2c361STejun Heo * and dequeue_task_scx() will RMW p->scx.flags. If we clear 1603bba2c361STejun Heo * ops_state first, both sides would modify p->scx.flags 1604bba2c361STejun Heo * concurrently in a non-atomic way. 1605bba2c361STejun Heo */ 1606bba2c361STejun Heo if (is_local) { 1607bba2c361STejun Heo local_dsq_post_enq(sch, dsq, p, enq_flags); 1608bba2c361STejun Heo } else { 1609bba2c361STejun Heo /* 1610bba2c361STejun Heo * Task on global/bypass DSQ: leave custody, task on 1611bba2c361STejun Heo * non-terminal DSQ: enter custody. 1612bba2c361STejun Heo */ 1613bba2c361STejun Heo if (dsq->id == SCX_DSQ_GLOBAL || dsq->id == SCX_DSQ_BYPASS) 1614bba2c361STejun Heo call_task_dequeue(sch, rq, p, 0); 1615bba2c361STejun Heo else 1616bba2c361STejun Heo p->scx.flags |= SCX_TASK_IN_CUSTODY; 1617bba2c361STejun Heo 1618bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 1619bba2c361STejun Heo } 1620bba2c361STejun Heo 1621bba2c361STejun Heo /* 1622bba2c361STejun Heo * We're transitioning out of QUEUEING or DISPATCHING. store_release to 1623bba2c361STejun Heo * match waiters' load_acquire. 1624bba2c361STejun Heo */ 1625bba2c361STejun Heo if (enq_flags & SCX_ENQ_CLEAR_OPSS) 1626bba2c361STejun Heo atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE); 1627bba2c361STejun Heo } 1628bba2c361STejun Heo 1629bba2c361STejun Heo static void task_unlink_from_dsq(struct task_struct *p, 1630bba2c361STejun Heo struct scx_dispatch_q *dsq) 1631bba2c361STejun Heo { 1632bba2c361STejun Heo WARN_ON_ONCE(list_empty(&p->scx.dsq_list.node)); 1633bba2c361STejun Heo 1634bba2c361STejun Heo if (p->scx.dsq_flags & SCX_TASK_DSQ_ON_PRIQ) { 1635bba2c361STejun Heo rb_erase(&p->scx.dsq_priq, &dsq->priq); 1636bba2c361STejun Heo RB_CLEAR_NODE(&p->scx.dsq_priq); 1637bba2c361STejun Heo p->scx.dsq_flags &= ~SCX_TASK_DSQ_ON_PRIQ; 1638bba2c361STejun Heo } 1639bba2c361STejun Heo 1640bba2c361STejun Heo list_del_init(&p->scx.dsq_list.node); 1641bba2c361STejun Heo dsq_dec_nr(dsq, p); 1642bba2c361STejun Heo 1643bba2c361STejun Heo if (!(dsq->id & SCX_DSQ_FLAG_BUILTIN) && dsq->first_task == p) { 1644bba2c361STejun Heo struct task_struct *first_task; 1645bba2c361STejun Heo 1646bba2c361STejun Heo first_task = nldsq_next_task(dsq, NULL, false); 1647bba2c361STejun Heo rcu_assign_pointer(dsq->first_task, first_task); 1648bba2c361STejun Heo } 1649bba2c361STejun Heo } 1650bba2c361STejun Heo 1651bba2c361STejun Heo static void dispatch_dequeue(struct rq *rq, struct task_struct *p) 1652bba2c361STejun Heo { 1653bba2c361STejun Heo struct scx_dispatch_q *dsq = p->scx.dsq; 1654bba2c361STejun Heo bool is_local = dsq == &rq->scx.local_dsq; 1655bba2c361STejun Heo 1656bba2c361STejun Heo lockdep_assert_rq_held(rq); 1657bba2c361STejun Heo 1658bba2c361STejun Heo if (!dsq) { 1659bba2c361STejun Heo /* 1660bba2c361STejun Heo * If !dsq && on-list, @p is on @rq's ddsp_deferred_locals. 1661bba2c361STejun Heo * Unlinking is all that's needed to cancel. 1662bba2c361STejun Heo */ 1663bba2c361STejun Heo if (unlikely(!list_empty(&p->scx.dsq_list.node))) 1664bba2c361STejun Heo list_del_init(&p->scx.dsq_list.node); 1665bba2c361STejun Heo 1666bba2c361STejun Heo /* 1667bba2c361STejun Heo * When dispatching directly from the BPF scheduler to a local 1668bba2c361STejun Heo * DSQ, the task isn't associated with any DSQ but 1669bba2c361STejun Heo * @p->scx.holding_cpu may be set under the protection of 1670bba2c361STejun Heo * %SCX_OPSS_DISPATCHING. 1671bba2c361STejun Heo */ 1672bba2c361STejun Heo if (p->scx.holding_cpu >= 0) 1673bba2c361STejun Heo p->scx.holding_cpu = -1; 1674bba2c361STejun Heo 1675bba2c361STejun Heo return; 1676bba2c361STejun Heo } 1677bba2c361STejun Heo 1678bba2c361STejun Heo if (!is_local) 1679bba2c361STejun Heo raw_spin_lock(&dsq->lock); 1680bba2c361STejun Heo 1681bba2c361STejun Heo /* 1682bba2c361STejun Heo * Now that we hold @dsq->lock, @p->holding_cpu and @p->scx.dsq_* can't 1683bba2c361STejun Heo * change underneath us. 1684bba2c361STejun Heo */ 1685bba2c361STejun Heo if (p->scx.holding_cpu < 0) { 1686bba2c361STejun Heo /* @p must still be on @dsq, dequeue */ 1687bba2c361STejun Heo task_unlink_from_dsq(p, dsq); 1688bba2c361STejun Heo } else { 1689bba2c361STejun Heo /* 1690bba2c361STejun Heo * We're racing against dispatch_to_local_dsq() which already 1691bba2c361STejun Heo * removed @p from @dsq and set @p->scx.holding_cpu. Clear the 1692bba2c361STejun Heo * holding_cpu which tells dispatch_to_local_dsq() that it lost 1693bba2c361STejun Heo * the race. 1694bba2c361STejun Heo */ 1695bba2c361STejun Heo WARN_ON_ONCE(!list_empty(&p->scx.dsq_list.node)); 1696bba2c361STejun Heo p->scx.holding_cpu = -1; 1697bba2c361STejun Heo } 1698bba2c361STejun Heo p->scx.dsq = NULL; 1699bba2c361STejun Heo 1700bba2c361STejun Heo if (!is_local) 1701bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 1702bba2c361STejun Heo } 1703bba2c361STejun Heo 1704bba2c361STejun Heo /* 1705bba2c361STejun Heo * Abbreviated version of dispatch_dequeue() that can be used when both @p's rq 1706bba2c361STejun Heo * and dsq are locked. 1707bba2c361STejun Heo */ 1708bba2c361STejun Heo static void dispatch_dequeue_locked(struct task_struct *p, 1709bba2c361STejun Heo struct scx_dispatch_q *dsq) 1710bba2c361STejun Heo { 1711bba2c361STejun Heo lockdep_assert_rq_held(task_rq(p)); 1712bba2c361STejun Heo lockdep_assert_held(&dsq->lock); 1713bba2c361STejun Heo 1714bba2c361STejun Heo task_unlink_from_dsq(p, dsq); 1715bba2c361STejun Heo p->scx.dsq = NULL; 1716bba2c361STejun Heo } 1717bba2c361STejun Heo 1718bba2c361STejun Heo static struct scx_dispatch_q *find_dsq_for_dispatch(struct scx_sched *sch, 1719bba2c361STejun Heo struct rq *rq, u64 dsq_id, 1720bba2c361STejun Heo s32 tcpu) 1721bba2c361STejun Heo { 1722bba2c361STejun Heo struct scx_dispatch_q *dsq; 1723bba2c361STejun Heo 1724bba2c361STejun Heo if (dsq_id == SCX_DSQ_LOCAL) 1725bba2c361STejun Heo return &rq->scx.local_dsq; 1726bba2c361STejun Heo 1727bba2c361STejun Heo if ((dsq_id & SCX_DSQ_LOCAL_ON) == SCX_DSQ_LOCAL_ON) { 1728bba2c361STejun Heo s32 cpu = scx_cpu_ret(sch, dsq_id & SCX_DSQ_LOCAL_CPU_MASK); 1729bba2c361STejun Heo 1730bba2c361STejun Heo if (!scx_cpu_valid(sch, cpu, "in SCX_DSQ_LOCAL_ON dispatch verdict")) 1731bba2c361STejun Heo return find_global_dsq(sch, tcpu); 1732bba2c361STejun Heo 1733bba2c361STejun Heo return &cpu_rq(cpu)->scx.local_dsq; 1734bba2c361STejun Heo } 1735bba2c361STejun Heo 1736bba2c361STejun Heo if (dsq_id == SCX_DSQ_GLOBAL) 1737bba2c361STejun Heo dsq = find_global_dsq(sch, tcpu); 1738bba2c361STejun Heo else 1739bba2c361STejun Heo dsq = find_user_dsq(sch, dsq_id); 1740bba2c361STejun Heo 1741bba2c361STejun Heo if (unlikely(!dsq)) { 1742bba2c361STejun Heo scx_error(sch, "non-existent DSQ 0x%llx", dsq_id); 1743bba2c361STejun Heo return find_global_dsq(sch, tcpu); 1744bba2c361STejun Heo } 1745bba2c361STejun Heo 1746bba2c361STejun Heo return dsq; 1747bba2c361STejun Heo } 1748bba2c361STejun Heo 1749bba2c361STejun Heo static void mark_direct_dispatch(struct scx_sched *sch, 1750bba2c361STejun Heo struct task_struct *ddsp_task, 1751bba2c361STejun Heo struct task_struct *p, u64 dsq_id, 1752bba2c361STejun Heo u64 enq_flags) 1753bba2c361STejun Heo { 1754bba2c361STejun Heo /* 1755bba2c361STejun Heo * Mark that dispatch already happened from ops.select_cpu() or 1756bba2c361STejun Heo * ops.enqueue() by spoiling direct_dispatch_task with a non-NULL value 1757bba2c361STejun Heo * which can never match a valid task pointer. 1758bba2c361STejun Heo */ 1759bba2c361STejun Heo __this_cpu_write(direct_dispatch_task, ERR_PTR(-ESRCH)); 1760bba2c361STejun Heo 1761bba2c361STejun Heo /* @p must match the task on the enqueue path */ 1762bba2c361STejun Heo if (unlikely(p != ddsp_task)) { 1763bba2c361STejun Heo if (IS_ERR(ddsp_task)) 1764bba2c361STejun Heo scx_error(sch, "%s[%d] already direct-dispatched", 1765bba2c361STejun Heo p->comm, p->pid); 1766bba2c361STejun Heo else 1767bba2c361STejun Heo scx_error(sch, "scheduling for %s[%d] but trying to direct-dispatch %s[%d]", 1768bba2c361STejun Heo ddsp_task->comm, ddsp_task->pid, 1769bba2c361STejun Heo p->comm, p->pid); 1770bba2c361STejun Heo return; 1771bba2c361STejun Heo } 1772bba2c361STejun Heo 1773bba2c361STejun Heo WARN_ON_ONCE(p->scx.ddsp_dsq_id != SCX_DSQ_INVALID); 1774bba2c361STejun Heo WARN_ON_ONCE(p->scx.ddsp_enq_flags); 1775bba2c361STejun Heo 1776bba2c361STejun Heo p->scx.ddsp_dsq_id = dsq_id; 1777bba2c361STejun Heo p->scx.ddsp_enq_flags = enq_flags; 1778bba2c361STejun Heo } 1779bba2c361STejun Heo 1780bba2c361STejun Heo /* 1781bba2c361STejun Heo * Clear @p direct dispatch state when leaving the scheduler. 1782bba2c361STejun Heo * 1783bba2c361STejun Heo * Direct dispatch state must be cleared in the following cases: 1784bba2c361STejun Heo * - direct_dispatch(): cleared on the synchronous enqueue path, deferred 1785bba2c361STejun Heo * dispatch keeps the state until consumed 1786bba2c361STejun Heo * - process_ddsp_deferred_locals(): cleared after consuming deferred state, 1787bba2c361STejun Heo * - do_enqueue_task(): cleared on enqueue fallbacks where the dispatch 1788bba2c361STejun Heo * verdict is ignored (local/global/bypass) 1789bba2c361STejun Heo * - dequeue_task_scx(): cleared after dispatch_dequeue(), covering deferred 1790bba2c361STejun Heo * cancellation and holding_cpu races 1791bba2c361STejun Heo * - scx_disable_task(): cleared for queued wakeup tasks, which are excluded by 1792bba2c361STejun Heo * the scx_bypass() loop, so that stale state is not reused by a subsequent 1793bba2c361STejun Heo * scheduler instance 1794bba2c361STejun Heo */ 1795bba2c361STejun Heo static inline void clear_direct_dispatch(struct task_struct *p) 1796bba2c361STejun Heo { 1797bba2c361STejun Heo p->scx.ddsp_dsq_id = SCX_DSQ_INVALID; 1798bba2c361STejun Heo p->scx.ddsp_enq_flags = 0; 1799bba2c361STejun Heo } 1800bba2c361STejun Heo 1801bba2c361STejun Heo static void direct_dispatch(struct scx_sched *sch, struct task_struct *p, 1802bba2c361STejun Heo u64 enq_flags) 1803bba2c361STejun Heo { 1804bba2c361STejun Heo struct rq *rq = task_rq(p); 1805bba2c361STejun Heo struct scx_dispatch_q *dsq = 1806bba2c361STejun Heo find_dsq_for_dispatch(sch, rq, p->scx.ddsp_dsq_id, task_cpu(p)); 1807bba2c361STejun Heo u64 ddsp_enq_flags; 1808bba2c361STejun Heo 1809bba2c361STejun Heo touch_core_sched_dispatch(rq, p); 1810bba2c361STejun Heo 1811bba2c361STejun Heo p->scx.ddsp_enq_flags |= enq_flags; 1812bba2c361STejun Heo 1813bba2c361STejun Heo /* 1814bba2c361STejun Heo * We are in the enqueue path with @rq locked and pinned, and thus can't 1815bba2c361STejun Heo * double lock a remote rq and enqueue to its local DSQ. For 1816bba2c361STejun Heo * DSQ_LOCAL_ON verdicts targeting the local DSQ of a remote CPU, defer 1817bba2c361STejun Heo * the enqueue so that it's executed when @rq can be unlocked. 1818bba2c361STejun Heo */ 1819bba2c361STejun Heo if (dsq->id == SCX_DSQ_LOCAL && dsq != &rq->scx.local_dsq) { 1820bba2c361STejun Heo unsigned long opss; 1821bba2c361STejun Heo 1822bba2c361STejun Heo opss = atomic_long_read(&p->scx.ops_state) & SCX_OPSS_STATE_MASK; 1823bba2c361STejun Heo 1824bba2c361STejun Heo switch (opss & SCX_OPSS_STATE_MASK) { 1825bba2c361STejun Heo case SCX_OPSS_NONE: 1826bba2c361STejun Heo break; 1827bba2c361STejun Heo case SCX_OPSS_QUEUEING: 1828bba2c361STejun Heo /* 1829bba2c361STejun Heo * As @p was never passed to the BPF side, _release is 1830bba2c361STejun Heo * not strictly necessary. Still do it for consistency. 1831bba2c361STejun Heo */ 1832bba2c361STejun Heo atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE); 1833bba2c361STejun Heo break; 1834bba2c361STejun Heo default: 1835bba2c361STejun Heo WARN_ONCE(true, "sched_ext: %s[%d] has invalid ops state 0x%lx in direct_dispatch()", 1836bba2c361STejun Heo p->comm, p->pid, opss); 1837bba2c361STejun Heo atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE); 1838bba2c361STejun Heo break; 1839bba2c361STejun Heo } 1840bba2c361STejun Heo 1841bba2c361STejun Heo WARN_ON_ONCE(p->scx.dsq || !list_empty(&p->scx.dsq_list.node)); 1842bba2c361STejun Heo list_add_tail(&p->scx.dsq_list.node, 1843bba2c361STejun Heo &rq->scx.ddsp_deferred_locals); 1844bba2c361STejun Heo schedule_deferred_locked(rq); 1845bba2c361STejun Heo return; 1846bba2c361STejun Heo } 1847bba2c361STejun Heo 1848bba2c361STejun Heo ddsp_enq_flags = p->scx.ddsp_enq_flags; 1849bba2c361STejun Heo clear_direct_dispatch(p); 1850bba2c361STejun Heo 1851bba2c361STejun Heo dispatch_enqueue(sch, rq, dsq, p, ddsp_enq_flags | SCX_ENQ_CLEAR_OPSS); 1852bba2c361STejun Heo } 1853bba2c361STejun Heo 1854bba2c361STejun Heo static bool scx_rq_online(struct rq *rq) 1855bba2c361STejun Heo { 1856bba2c361STejun Heo /* 1857bba2c361STejun Heo * Test both cpu_active() and %SCX_RQ_ONLINE. %SCX_RQ_ONLINE indicates 1858bba2c361STejun Heo * the online state as seen from the BPF scheduler. cpu_active() test 1859bba2c361STejun Heo * guarantees that, if this function returns %true, %SCX_RQ_ONLINE will 1860bba2c361STejun Heo * stay set until the current scheduling operation is complete even if 1861bba2c361STejun Heo * we aren't locking @rq. 1862bba2c361STejun Heo */ 1863bba2c361STejun Heo return likely((rq->scx.flags & SCX_RQ_ONLINE) && cpu_active(cpu_of(rq))); 1864bba2c361STejun Heo } 1865bba2c361STejun Heo 1866bba2c361STejun Heo static void do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags, 1867bba2c361STejun Heo int sticky_cpu) 1868bba2c361STejun Heo { 1869bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 1870bba2c361STejun Heo struct task_struct **ddsp_taskp; 1871bba2c361STejun Heo struct scx_dispatch_q *dsq; 1872bba2c361STejun Heo unsigned long qseq; 1873bba2c361STejun Heo 1874bba2c361STejun Heo WARN_ON_ONCE(!(p->scx.flags & SCX_TASK_QUEUED)); 1875bba2c361STejun Heo 1876bba2c361STejun Heo /* internal movements - rq migration / RESTORE */ 1877bba2c361STejun Heo if (sticky_cpu == cpu_of(rq)) 1878bba2c361STejun Heo goto local_norefill; 1879bba2c361STejun Heo 1880bba2c361STejun Heo /* 1881bba2c361STejun Heo * Clear persistent TASK_IMMED for fresh enqueues, see dsq_inc_nr(). 1882bba2c361STejun Heo * Note that exiting and migration-disabled tasks that skip 1883bba2c361STejun Heo * ops.enqueue() below will lose IMMED protection unless 1884bba2c361STejun Heo * %SCX_OPS_ENQ_EXITING / %SCX_OPS_ENQ_MIGRATION_DISABLED are set. 1885bba2c361STejun Heo */ 1886bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_IMMED; 1887bba2c361STejun Heo 1888bba2c361STejun Heo /* 1889bba2c361STejun Heo * If !scx_rq_online(), we already told the BPF scheduler that the CPU 1890bba2c361STejun Heo * is offline and are just running the hotplug path. Don't bother the 1891bba2c361STejun Heo * BPF scheduler. 1892bba2c361STejun Heo */ 1893bba2c361STejun Heo if (!scx_rq_online(rq)) 1894bba2c361STejun Heo goto local; 1895bba2c361STejun Heo 1896bba2c361STejun Heo if (scx_bypassing(sch, cpu_of(rq))) { 1897bba2c361STejun Heo __scx_add_event(sch, SCX_EV_BYPASS_DISPATCH, 1); 1898bba2c361STejun Heo goto bypass; 1899bba2c361STejun Heo } 1900bba2c361STejun Heo 1901bba2c361STejun Heo if (p->scx.ddsp_dsq_id != SCX_DSQ_INVALID) 1902bba2c361STejun Heo goto direct; 1903bba2c361STejun Heo 1904bba2c361STejun Heo /* see %SCX_OPS_ENQ_EXITING */ 1905bba2c361STejun Heo if (!(sch->ops.flags & SCX_OPS_ENQ_EXITING) && 1906bba2c361STejun Heo unlikely(p->flags & PF_EXITING)) { 1907bba2c361STejun Heo __scx_add_event(sch, SCX_EV_ENQ_SKIP_EXITING, 1); 1908bba2c361STejun Heo goto local; 1909bba2c361STejun Heo } 1910bba2c361STejun Heo 1911bba2c361STejun Heo /* see %SCX_OPS_ENQ_MIGRATION_DISABLED */ 1912bba2c361STejun Heo if (!(sch->ops.flags & SCX_OPS_ENQ_MIGRATION_DISABLED) && 1913bba2c361STejun Heo is_migration_disabled(p)) { 1914bba2c361STejun Heo __scx_add_event(sch, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED, 1); 1915bba2c361STejun Heo goto local; 1916bba2c361STejun Heo } 1917bba2c361STejun Heo 1918bba2c361STejun Heo if (unlikely(!SCX_HAS_OP(sch, enqueue))) 1919bba2c361STejun Heo goto global; 1920bba2c361STejun Heo 1921bba2c361STejun Heo /* DSQ bypass didn't trigger, enqueue on the BPF scheduler */ 1922bba2c361STejun Heo qseq = rq->scx.ops_qseq++ << SCX_OPSS_QSEQ_SHIFT; 1923bba2c361STejun Heo 1924bba2c361STejun Heo WARN_ON_ONCE(atomic_long_read(&p->scx.ops_state) != SCX_OPSS_NONE); 1925bba2c361STejun Heo atomic_long_set(&p->scx.ops_state, SCX_OPSS_QUEUEING | qseq); 1926bba2c361STejun Heo 1927bba2c361STejun Heo ddsp_taskp = this_cpu_ptr(&direct_dispatch_task); 1928bba2c361STejun Heo WARN_ON_ONCE(*ddsp_taskp); 1929bba2c361STejun Heo *ddsp_taskp = p; 1930bba2c361STejun Heo 1931bba2c361STejun Heo SCX_CALL_OP_TASK(sch, enqueue, rq, p, enq_flags); 1932bba2c361STejun Heo 1933bba2c361STejun Heo *ddsp_taskp = NULL; 1934bba2c361STejun Heo if (p->scx.ddsp_dsq_id != SCX_DSQ_INVALID) 1935bba2c361STejun Heo goto direct; 1936bba2c361STejun Heo 1937bba2c361STejun Heo /* 1938bba2c361STejun Heo * Task is now in BPF scheduler's custody. Set %SCX_TASK_IN_CUSTODY 1939bba2c361STejun Heo * so ops.dequeue() is called when it leaves custody. 1940bba2c361STejun Heo */ 1941bba2c361STejun Heo p->scx.flags |= SCX_TASK_IN_CUSTODY; 1942bba2c361STejun Heo 1943bba2c361STejun Heo /* 1944bba2c361STejun Heo * If not directly dispatched, QUEUEING isn't clear yet and dispatch or 1945bba2c361STejun Heo * dequeue may be waiting. The store_release matches their load_acquire. 1946bba2c361STejun Heo */ 1947bba2c361STejun Heo atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_QUEUED | qseq); 1948bba2c361STejun Heo return; 1949bba2c361STejun Heo 1950bba2c361STejun Heo direct: 1951bba2c361STejun Heo direct_dispatch(sch, p, enq_flags); 1952bba2c361STejun Heo return; 1953bba2c361STejun Heo local_norefill: 1954bba2c361STejun Heo dispatch_enqueue(sch, rq, &rq->scx.local_dsq, p, enq_flags); 1955bba2c361STejun Heo return; 1956bba2c361STejun Heo local: 1957bba2c361STejun Heo dsq = &rq->scx.local_dsq; 1958bba2c361STejun Heo goto enqueue; 1959bba2c361STejun Heo global: 1960bba2c361STejun Heo dsq = find_global_dsq(sch, task_cpu(p)); 1961bba2c361STejun Heo goto enqueue; 1962bba2c361STejun Heo bypass: 1963bba2c361STejun Heo dsq = bypass_enq_target_dsq(sch, task_cpu(p)); 1964bba2c361STejun Heo goto enqueue; 1965bba2c361STejun Heo 1966bba2c361STejun Heo enqueue: 1967bba2c361STejun Heo /* 1968bba2c361STejun Heo * For task-ordering, slice refill must be treated as implying the end 1969bba2c361STejun Heo * of the current slice. Otherwise, the longer @p stays on the CPU, the 1970bba2c361STejun Heo * higher priority it becomes from scx_prio_less()'s POV. 1971bba2c361STejun Heo */ 1972bba2c361STejun Heo touch_core_sched(rq, p); 1973bba2c361STejun Heo refill_task_slice_dfl(sch, p); 1974bba2c361STejun Heo clear_direct_dispatch(p); 1975bba2c361STejun Heo dispatch_enqueue(sch, rq, dsq, p, enq_flags); 1976bba2c361STejun Heo } 1977bba2c361STejun Heo 1978bba2c361STejun Heo static bool task_runnable(const struct task_struct *p) 1979bba2c361STejun Heo { 1980bba2c361STejun Heo return !list_empty(&p->scx.runnable_node); 1981bba2c361STejun Heo } 1982bba2c361STejun Heo 1983bba2c361STejun Heo static void set_task_runnable(struct rq *rq, struct task_struct *p) 1984bba2c361STejun Heo { 1985bba2c361STejun Heo lockdep_assert_rq_held(rq); 1986bba2c361STejun Heo 1987bba2c361STejun Heo if (p->scx.flags & SCX_TASK_RESET_RUNNABLE_AT) { 1988bba2c361STejun Heo p->scx.runnable_at = jiffies; 1989bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_RESET_RUNNABLE_AT; 1990bba2c361STejun Heo } 1991bba2c361STejun Heo 1992bba2c361STejun Heo /* 1993bba2c361STejun Heo * list_add_tail() must be used. scx_bypass() depends on tasks being 1994bba2c361STejun Heo * appended to the runnable_list. 1995bba2c361STejun Heo */ 1996bba2c361STejun Heo list_add_tail(&p->scx.runnable_node, &rq->scx.runnable_list); 1997bba2c361STejun Heo } 1998bba2c361STejun Heo 1999bba2c361STejun Heo static void clr_task_runnable(struct task_struct *p, bool reset_runnable_at) 2000bba2c361STejun Heo { 2001bba2c361STejun Heo list_del_init(&p->scx.runnable_node); 2002bba2c361STejun Heo if (reset_runnable_at) 2003bba2c361STejun Heo p->scx.flags |= SCX_TASK_RESET_RUNNABLE_AT; 2004bba2c361STejun Heo } 2005bba2c361STejun Heo 2006bba2c361STejun Heo static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_flags) 2007bba2c361STejun Heo { 2008bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 2009bba2c361STejun Heo int sticky_cpu = p->scx.sticky_cpu; 2010bba2c361STejun Heo u64 enq_flags = core_enq_flags | rq->scx.extra_enq_flags; 2011bba2c361STejun Heo 2012bba2c361STejun Heo if (enq_flags & ENQUEUE_WAKEUP) 2013bba2c361STejun Heo rq->scx.flags |= SCX_RQ_IN_WAKEUP; 2014bba2c361STejun Heo 2015bba2c361STejun Heo /* 2016bba2c361STejun Heo * Restoring a running task will be immediately followed by 2017bba2c361STejun Heo * set_next_task_scx() which expects the task to not be on the BPF 2018bba2c361STejun Heo * scheduler as tasks can only start running through local DSQs. Force 2019bba2c361STejun Heo * direct-dispatch into the local DSQ by setting the sticky_cpu. 2020bba2c361STejun Heo */ 2021bba2c361STejun Heo if (unlikely(enq_flags & ENQUEUE_RESTORE) && task_current(rq, p)) 2022bba2c361STejun Heo sticky_cpu = cpu_of(rq); 2023bba2c361STejun Heo 2024bba2c361STejun Heo if (p->scx.flags & SCX_TASK_QUEUED) { 2025bba2c361STejun Heo WARN_ON_ONCE(!task_runnable(p)); 2026bba2c361STejun Heo goto out; 2027bba2c361STejun Heo } 2028bba2c361STejun Heo 2029bba2c361STejun Heo set_task_runnable(rq, p); 2030bba2c361STejun Heo p->scx.flags |= SCX_TASK_QUEUED; 2031bba2c361STejun Heo rq->scx.nr_running++; 2032bba2c361STejun Heo add_nr_running(rq, 1); 2033bba2c361STejun Heo 2034bba2c361STejun Heo if (SCX_HAS_OP(sch, runnable) && !task_on_rq_migrating(p)) 2035bba2c361STejun Heo SCX_CALL_OP_TASK(sch, runnable, rq, p, enq_flags); 2036bba2c361STejun Heo 2037bba2c361STejun Heo if (enq_flags & SCX_ENQ_WAKEUP) 2038bba2c361STejun Heo touch_core_sched(rq, p); 2039bba2c361STejun Heo 2040bba2c361STejun Heo /* Start dl_server if this is the first task being enqueued */ 2041bba2c361STejun Heo if (rq->scx.nr_running == 1) 2042bba2c361STejun Heo dl_server_start(&rq->ext_server); 2043bba2c361STejun Heo 2044bba2c361STejun Heo do_enqueue_task(rq, p, enq_flags, sticky_cpu); 2045bba2c361STejun Heo 2046bba2c361STejun Heo if (sticky_cpu >= 0) 2047bba2c361STejun Heo p->scx.sticky_cpu = -1; 2048bba2c361STejun Heo out: 2049bba2c361STejun Heo rq->scx.flags &= ~SCX_RQ_IN_WAKEUP; 2050bba2c361STejun Heo 2051bba2c361STejun Heo if ((enq_flags & SCX_ENQ_CPU_SELECTED) && 2052bba2c361STejun Heo unlikely(cpu_of(rq) != p->scx.selected_cpu)) 2053bba2c361STejun Heo __scx_add_event(sch, SCX_EV_SELECT_CPU_FALLBACK, 1); 2054bba2c361STejun Heo } 2055bba2c361STejun Heo 2056bba2c361STejun Heo static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags) 2057bba2c361STejun Heo { 2058bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 2059bba2c361STejun Heo unsigned long opss; 2060bba2c361STejun Heo 2061bba2c361STejun Heo /* dequeue is always temporary, don't reset runnable_at */ 2062bba2c361STejun Heo clr_task_runnable(p, false); 2063bba2c361STejun Heo 2064bba2c361STejun Heo retry: 2065bba2c361STejun Heo /* acquire ensures that we see the preceding updates on QUEUED */ 2066bba2c361STejun Heo opss = atomic_long_read_acquire(&p->scx.ops_state); 2067bba2c361STejun Heo 2068bba2c361STejun Heo switch (opss & SCX_OPSS_STATE_MASK) { 2069bba2c361STejun Heo case SCX_OPSS_NONE: 2070bba2c361STejun Heo break; 2071bba2c361STejun Heo case SCX_OPSS_QUEUEING: 2072bba2c361STejun Heo /* 2073bba2c361STejun Heo * QUEUEING is started and finished while holding @p's rq lock. 2074bba2c361STejun Heo * As we're holding the rq lock now, we shouldn't see QUEUEING. 2075bba2c361STejun Heo */ 2076bba2c361STejun Heo BUG(); 2077bba2c361STejun Heo case SCX_OPSS_QUEUED: 2078bba2c361STejun Heo /* 2079bba2c361STejun Heo * A queued task must always be in BPF scheduler's custody. If 2080bba2c361STejun Heo * SCX_TASK_IN_CUSTODY is clear, finish_dispatch() on another 2081bba2c361STejun Heo * CPU has already passed call_task_dequeue() (which clears the 2082bba2c361STejun Heo * flag), but has not yet written SCX_OPSS_NONE. That final 2083bba2c361STejun Heo * store does not require this rq's lock, so retrying with 2084bba2c361STejun Heo * cpu_relax() is bounded: we will observe NONE (or DISPATCHING, 2085bba2c361STejun Heo * handled by the fallthrough) on a subsequent iteration. 2086bba2c361STejun Heo */ 2087bba2c361STejun Heo if (unlikely(!(READ_ONCE(p->scx.flags) & SCX_TASK_IN_CUSTODY))) { 2088bba2c361STejun Heo cpu_relax(); 2089bba2c361STejun Heo goto retry; 2090bba2c361STejun Heo } 2091bba2c361STejun Heo 2092bba2c361STejun Heo if (atomic_long_try_cmpxchg(&p->scx.ops_state, &opss, 2093bba2c361STejun Heo SCX_OPSS_NONE)) 2094bba2c361STejun Heo break; 2095bba2c361STejun Heo fallthrough; 2096bba2c361STejun Heo case SCX_OPSS_DISPATCHING: 2097bba2c361STejun Heo /* 2098bba2c361STejun Heo * If @p is being dispatched from the BPF scheduler to a DSQ, 2099bba2c361STejun Heo * wait for the transfer to complete so that @p doesn't get 2100bba2c361STejun Heo * added to its DSQ after dequeueing is complete. 2101bba2c361STejun Heo * 2102bba2c361STejun Heo * As we're waiting on DISPATCHING with the rq locked, the 2103bba2c361STejun Heo * dispatching side shouldn't try to lock the rq while 2104bba2c361STejun Heo * DISPATCHING is set. See dispatch_to_local_dsq(). 2105bba2c361STejun Heo * 2106bba2c361STejun Heo * DISPATCHING shouldn't have qseq set and control can reach 2107bba2c361STejun Heo * here with NONE @opss from the above QUEUED case block. 2108bba2c361STejun Heo * Explicitly wait on %SCX_OPSS_DISPATCHING instead of @opss. 2109bba2c361STejun Heo */ 2110bba2c361STejun Heo wait_ops_state(p, SCX_OPSS_DISPATCHING); 2111bba2c361STejun Heo BUG_ON(atomic_long_read(&p->scx.ops_state) != SCX_OPSS_NONE); 2112bba2c361STejun Heo break; 2113bba2c361STejun Heo } 2114bba2c361STejun Heo 2115bba2c361STejun Heo /* 2116bba2c361STejun Heo * Call ops.dequeue() if the task is still in BPF custody. 2117bba2c361STejun Heo * 2118bba2c361STejun Heo * The code that clears ops_state to %SCX_OPSS_NONE does not always 2119bba2c361STejun Heo * clear %SCX_TASK_IN_CUSTODY: in dispatch_to_local_dsq(), when 2120bba2c361STejun Heo * we're moving a task that was in %SCX_OPSS_DISPATCHING to a 2121bba2c361STejun Heo * remote CPU's local DSQ, we only set ops_state to %SCX_OPSS_NONE 2122bba2c361STejun Heo * so that a concurrent dequeue can proceed, but we clear 2123bba2c361STejun Heo * %SCX_TASK_IN_CUSTODY only when we later enqueue or move the 2124bba2c361STejun Heo * task. So we can see NONE + IN_CUSTODY here and we must handle 2125bba2c361STejun Heo * it. Similarly, after waiting on %SCX_OPSS_DISPATCHING we see 2126bba2c361STejun Heo * NONE but the task may still have %SCX_TASK_IN_CUSTODY set until 2127bba2c361STejun Heo * it is enqueued on the destination. 2128bba2c361STejun Heo */ 2129bba2c361STejun Heo call_task_dequeue(sch, rq, p, deq_flags); 2130bba2c361STejun Heo } 2131bba2c361STejun Heo 2132bba2c361STejun Heo static bool dequeue_task_scx(struct rq *rq, struct task_struct *p, int core_deq_flags) 2133bba2c361STejun Heo { 2134bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 2135bba2c361STejun Heo u64 deq_flags = core_deq_flags; 2136bba2c361STejun Heo 2137bba2c361STejun Heo /* 2138bba2c361STejun Heo * Set %SCX_DEQ_SCHED_CHANGE when the dequeue is due to a property 2139bba2c361STejun Heo * change (not sleep or core-sched pick). 2140bba2c361STejun Heo */ 2141bba2c361STejun Heo if (!(deq_flags & (DEQUEUE_SLEEP | SCX_DEQ_CORE_SCHED_EXEC))) 2142bba2c361STejun Heo deq_flags |= SCX_DEQ_SCHED_CHANGE; 2143bba2c361STejun Heo 2144bba2c361STejun Heo if (!(p->scx.flags & SCX_TASK_QUEUED)) { 2145bba2c361STejun Heo WARN_ON_ONCE(task_runnable(p)); 2146bba2c361STejun Heo return true; 2147bba2c361STejun Heo } 2148bba2c361STejun Heo 2149bba2c361STejun Heo ops_dequeue(rq, p, deq_flags); 2150bba2c361STejun Heo 2151bba2c361STejun Heo /* 2152bba2c361STejun Heo * A currently running task which is going off @rq first gets dequeued 2153bba2c361STejun Heo * and then stops running. As we want running <-> stopping transitions 2154bba2c361STejun Heo * to be contained within runnable <-> quiescent transitions, trigger 2155bba2c361STejun Heo * ->stopping() early here instead of in put_prev_task_scx(). 2156bba2c361STejun Heo * 2157bba2c361STejun Heo * @p may go through multiple stopping <-> running transitions between 2158bba2c361STejun Heo * here and put_prev_task_scx() if task attribute changes occur while 2159bba2c361STejun Heo * balance_one() leaves @rq unlocked. However, they don't contain any 2160bba2c361STejun Heo * information meaningful to the BPF scheduler and can be suppressed by 2161bba2c361STejun Heo * skipping the callbacks if the task is !QUEUED. 2162bba2c361STejun Heo */ 2163bba2c361STejun Heo if (SCX_HAS_OP(sch, stopping) && task_current(rq, p)) { 2164bba2c361STejun Heo update_curr_scx(rq); 2165bba2c361STejun Heo SCX_CALL_OP_TASK(sch, stopping, rq, p, false); 2166bba2c361STejun Heo } 2167bba2c361STejun Heo 2168bba2c361STejun Heo if (SCX_HAS_OP(sch, quiescent) && !task_on_rq_migrating(p)) 2169bba2c361STejun Heo SCX_CALL_OP_TASK(sch, quiescent, rq, p, deq_flags); 2170bba2c361STejun Heo 2171bba2c361STejun Heo if (deq_flags & SCX_DEQ_SLEEP) 2172bba2c361STejun Heo p->scx.flags |= SCX_TASK_DEQD_FOR_SLEEP; 2173bba2c361STejun Heo else 2174bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_DEQD_FOR_SLEEP; 2175bba2c361STejun Heo 2176bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_QUEUED; 2177bba2c361STejun Heo rq->scx.nr_running--; 2178bba2c361STejun Heo sub_nr_running(rq, 1); 2179bba2c361STejun Heo 2180bba2c361STejun Heo dispatch_dequeue(rq, p); 2181bba2c361STejun Heo clear_direct_dispatch(p); 2182bba2c361STejun Heo return true; 2183bba2c361STejun Heo } 2184bba2c361STejun Heo 2185bba2c361STejun Heo static void yield_task_scx(struct rq *rq) 2186bba2c361STejun Heo { 2187bba2c361STejun Heo struct task_struct *p = rq->donor; 2188bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 2189bba2c361STejun Heo 2190bba2c361STejun Heo if (SCX_HAS_OP(sch, yield)) 2191bba2c361STejun Heo SCX_CALL_OP_2TASKS_RET(sch, yield, rq, p, NULL); 2192bba2c361STejun Heo else 2193bba2c361STejun Heo p->scx.slice = 0; 2194bba2c361STejun Heo } 2195bba2c361STejun Heo 2196bba2c361STejun Heo static bool yield_to_task_scx(struct rq *rq, struct task_struct *to) 2197bba2c361STejun Heo { 2198bba2c361STejun Heo struct task_struct *from = rq->donor; 2199bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(from); 2200bba2c361STejun Heo 2201bba2c361STejun Heo if (SCX_HAS_OP(sch, yield) && sch == scx_task_sched(to)) 2202bba2c361STejun Heo return SCX_CALL_OP_2TASKS_RET(sch, yield, rq, from, to); 2203bba2c361STejun Heo else 2204bba2c361STejun Heo return false; 2205bba2c361STejun Heo } 2206bba2c361STejun Heo 2207bba2c361STejun Heo static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_flags) 2208bba2c361STejun Heo { 2209bba2c361STejun Heo /* 2210bba2c361STejun Heo * Preemption between SCX tasks is implemented by resetting the victim 2211bba2c361STejun Heo * task's slice to 0 and triggering reschedule on the target CPU. 2212bba2c361STejun Heo * Nothing to do. 2213bba2c361STejun Heo */ 2214bba2c361STejun Heo if (p->sched_class == &ext_sched_class) 2215bba2c361STejun Heo return; 2216bba2c361STejun Heo 2217bba2c361STejun Heo /* 2218bba2c361STejun Heo * Getting preempted by a higher-priority class. Reenqueue IMMED tasks. 2219bba2c361STejun Heo * This captures all preemption cases including: 2220bba2c361STejun Heo * 2221bba2c361STejun Heo * - A SCX task is currently running. 2222bba2c361STejun Heo * 2223bba2c361STejun Heo * - @rq is waking from idle due to a SCX task waking to it. 2224bba2c361STejun Heo * 2225bba2c361STejun Heo * - A higher-priority wakes up while SCX dispatch is in progress. 2226bba2c361STejun Heo */ 2227bba2c361STejun Heo if (rq->scx.nr_immed) 2228bba2c361STejun Heo schedule_reenq_local(rq, 0); 2229bba2c361STejun Heo } 2230bba2c361STejun Heo 2231bba2c361STejun Heo static void move_local_task_to_local_dsq(struct scx_sched *sch, 2232bba2c361STejun Heo struct task_struct *p, u64 enq_flags, 2233bba2c361STejun Heo struct scx_dispatch_q *src_dsq, 2234bba2c361STejun Heo struct rq *dst_rq) 2235bba2c361STejun Heo { 2236bba2c361STejun Heo struct scx_dispatch_q *dst_dsq = &dst_rq->scx.local_dsq; 2237bba2c361STejun Heo 2238bba2c361STejun Heo /* @dsq is locked and @p is on @dst_rq */ 2239bba2c361STejun Heo lockdep_assert_held(&src_dsq->lock); 2240bba2c361STejun Heo lockdep_assert_rq_held(dst_rq); 2241bba2c361STejun Heo 2242bba2c361STejun Heo WARN_ON_ONCE(p->scx.holding_cpu >= 0); 2243bba2c361STejun Heo 2244bba2c361STejun Heo if (enq_flags & (SCX_ENQ_HEAD | SCX_ENQ_PREEMPT)) 2245bba2c361STejun Heo list_add(&p->scx.dsq_list.node, &dst_dsq->list); 2246bba2c361STejun Heo else 2247bba2c361STejun Heo list_add_tail(&p->scx.dsq_list.node, &dst_dsq->list); 2248bba2c361STejun Heo 2249bba2c361STejun Heo dsq_inc_nr(dst_dsq, p, enq_flags); 2250bba2c361STejun Heo p->scx.dsq = dst_dsq; 2251bba2c361STejun Heo 2252bba2c361STejun Heo local_dsq_post_enq(sch, dst_dsq, p, enq_flags); 2253bba2c361STejun Heo } 2254bba2c361STejun Heo 2255bba2c361STejun Heo /** 2256bba2c361STejun Heo * move_remote_task_to_local_dsq - Move a task from a foreign rq to a local DSQ 2257bba2c361STejun Heo * @p: task to move 2258bba2c361STejun Heo * @enq_flags: %SCX_ENQ_* 2259bba2c361STejun Heo * @src_rq: rq to move the task from, locked on entry, released on return 2260bba2c361STejun Heo * @dst_rq: rq to move the task into, locked on return 2261bba2c361STejun Heo * 2262bba2c361STejun Heo * Move @p which is currently on @src_rq to @dst_rq's local DSQ. 2263bba2c361STejun Heo */ 2264bba2c361STejun Heo static void move_remote_task_to_local_dsq(struct task_struct *p, u64 enq_flags, 2265bba2c361STejun Heo struct rq *src_rq, struct rq *dst_rq) 2266bba2c361STejun Heo { 2267bba2c361STejun Heo lockdep_assert_rq_held(src_rq); 2268bba2c361STejun Heo 2269bba2c361STejun Heo /* 2270bba2c361STejun Heo * Set sticky_cpu before deactivate_task() to properly mark the 2271bba2c361STejun Heo * beginning of an SCX-internal migration. 2272bba2c361STejun Heo */ 2273bba2c361STejun Heo p->scx.sticky_cpu = cpu_of(dst_rq); 2274bba2c361STejun Heo deactivate_task(src_rq, p, 0); 2275bba2c361STejun Heo set_task_cpu(p, cpu_of(dst_rq)); 2276bba2c361STejun Heo 2277bba2c361STejun Heo raw_spin_rq_unlock(src_rq); 2278bba2c361STejun Heo raw_spin_rq_lock(dst_rq); 2279bba2c361STejun Heo 2280bba2c361STejun Heo /* 2281bba2c361STejun Heo * We want to pass scx-specific enq_flags but activate_task() will 2282bba2c361STejun Heo * truncate the upper 32 bit. As we own @rq, we can pass them through 2283bba2c361STejun Heo * @rq->scx.extra_enq_flags instead. 2284bba2c361STejun Heo */ 2285bba2c361STejun Heo WARN_ON_ONCE(!cpumask_test_cpu(cpu_of(dst_rq), p->cpus_ptr)); 2286bba2c361STejun Heo WARN_ON_ONCE(dst_rq->scx.extra_enq_flags); 2287bba2c361STejun Heo dst_rq->scx.extra_enq_flags = enq_flags; 2288bba2c361STejun Heo activate_task(dst_rq, p, 0); 2289bba2c361STejun Heo dst_rq->scx.extra_enq_flags = 0; 2290bba2c361STejun Heo } 2291bba2c361STejun Heo 2292bba2c361STejun Heo /* 2293bba2c361STejun Heo * Similar to kernel/sched/core.c::is_cpu_allowed(). However, there are two 2294bba2c361STejun Heo * differences: 2295bba2c361STejun Heo * 2296bba2c361STejun Heo * - is_cpu_allowed() asks "Can this task run on this CPU?" while 2297bba2c361STejun Heo * task_can_run_on_remote_rq() asks "Can the BPF scheduler migrate the task to 2298bba2c361STejun Heo * this CPU?". 2299bba2c361STejun Heo * 2300bba2c361STejun Heo * While migration is disabled, is_cpu_allowed() has to say "yes" as the task 2301bba2c361STejun Heo * must be allowed to finish on the CPU that it's currently on regardless of 2302bba2c361STejun Heo * the CPU state. However, task_can_run_on_remote_rq() must say "no" as the 2303bba2c361STejun Heo * BPF scheduler shouldn't attempt to migrate a task which has migration 2304bba2c361STejun Heo * disabled. 2305bba2c361STejun Heo * 2306bba2c361STejun Heo * - The BPF scheduler is bypassed while the rq is offline and we can always say 2307bba2c361STejun Heo * no to the BPF scheduler initiated migrations while offline. 2308bba2c361STejun Heo * 2309bba2c361STejun Heo * The caller must ensure that @p and @rq are on different CPUs. 2310bba2c361STejun Heo */ 2311bba2c361STejun Heo static bool task_can_run_on_remote_rq(struct scx_sched *sch, 2312bba2c361STejun Heo struct task_struct *p, struct rq *rq, 2313bba2c361STejun Heo bool enforce) 2314bba2c361STejun Heo { 2315bba2c361STejun Heo s32 cpu = cpu_of(rq); 2316bba2c361STejun Heo 2317bba2c361STejun Heo WARN_ON_ONCE(task_cpu(p) == cpu); 2318bba2c361STejun Heo 2319bba2c361STejun Heo /* 2320bba2c361STejun Heo * If @p has migration disabled, @p->cpus_ptr is updated to contain only 2321bba2c361STejun Heo * the pinned CPU in migrate_disable_switch() while @p is being switched 2322bba2c361STejun Heo * out. However, put_prev_task_scx() is called before @p->cpus_ptr is 2323bba2c361STejun Heo * updated and thus another CPU may see @p on a DSQ inbetween leading to 2324bba2c361STejun Heo * @p passing the below task_allowed_on_cpu() check while migration is 2325bba2c361STejun Heo * disabled. 2326bba2c361STejun Heo * 2327bba2c361STejun Heo * Test the migration disabled state first as the race window is narrow 2328bba2c361STejun Heo * and the BPF scheduler failing to check migration disabled state can 2329bba2c361STejun Heo * easily be masked if task_allowed_on_cpu() is done first. 2330bba2c361STejun Heo */ 2331bba2c361STejun Heo if (unlikely(is_migration_disabled(p))) { 2332bba2c361STejun Heo if (enforce) 2333bba2c361STejun Heo scx_error(sch, "SCX_DSQ_LOCAL[_ON] cannot move migration disabled %s[%d] from CPU %d to %d", 2334bba2c361STejun Heo p->comm, p->pid, task_cpu(p), cpu); 2335bba2c361STejun Heo return false; 2336bba2c361STejun Heo } 2337bba2c361STejun Heo 2338bba2c361STejun Heo /* 2339bba2c361STejun Heo * We don't require the BPF scheduler to avoid dispatching to offline 2340bba2c361STejun Heo * CPUs mostly for convenience but also because CPUs can go offline 2341bba2c361STejun Heo * between scx_bpf_dsq_insert() calls and here. Trigger error iff the 2342bba2c361STejun Heo * picked CPU is outside the allowed mask. 2343bba2c361STejun Heo */ 2344bba2c361STejun Heo if (!task_allowed_on_cpu(p, cpu)) { 2345bba2c361STejun Heo if (enforce) 2346bba2c361STejun Heo scx_error(sch, "SCX_DSQ_LOCAL[_ON] target CPU %d not allowed for %s[%d]", 2347bba2c361STejun Heo cpu, p->comm, p->pid); 2348bba2c361STejun Heo return false; 2349bba2c361STejun Heo } 2350bba2c361STejun Heo 2351bba2c361STejun Heo if (!scx_rq_online(rq)) { 2352bba2c361STejun Heo if (enforce) 2353bba2c361STejun Heo __scx_add_event(sch, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE, 1); 2354bba2c361STejun Heo return false; 2355bba2c361STejun Heo } 2356bba2c361STejun Heo 2357bba2c361STejun Heo return true; 2358bba2c361STejun Heo } 2359bba2c361STejun Heo 2360bba2c361STejun Heo /** 2361bba2c361STejun Heo * unlink_dsq_and_lock_src_rq() - Unlink task from its DSQ and lock its task_rq 2362bba2c361STejun Heo * @p: target task 2363bba2c361STejun Heo * @dsq: locked DSQ @p is currently on 2364bba2c361STejun Heo * @src_rq: rq @p is currently on, stable with @dsq locked 2365bba2c361STejun Heo * 2366bba2c361STejun Heo * Called with @dsq locked but no rq's locked. We want to move @p to a different 2367bba2c361STejun Heo * DSQ, including any local DSQ, but are not locking @src_rq. Locking @src_rq is 2368bba2c361STejun Heo * required when transferring into a local DSQ. Even when transferring into a 2369bba2c361STejun Heo * non-local DSQ, it's better to use the same mechanism to protect against 2370bba2c361STejun Heo * dequeues and maintain the invariant that @p->scx.dsq can only change while 2371bba2c361STejun Heo * @src_rq is locked, which e.g. scx_dump_task() depends on. 2372bba2c361STejun Heo * 2373bba2c361STejun Heo * We want to grab @src_rq but that can deadlock if we try while locking @dsq, 2374bba2c361STejun Heo * so we want to unlink @p from @dsq, drop its lock and then lock @src_rq. As 2375bba2c361STejun Heo * this may race with dequeue, which can't drop the rq lock or fail, do a little 2376bba2c361STejun Heo * dancing from our side. 2377bba2c361STejun Heo * 2378bba2c361STejun Heo * @p->scx.holding_cpu is set to this CPU before @dsq is unlocked. If @p gets 2379bba2c361STejun Heo * dequeued after we unlock @dsq but before locking @src_rq, the holding_cpu 2380bba2c361STejun Heo * would be cleared to -1. While other cpus may have updated it to different 2381bba2c361STejun Heo * values afterwards, as this operation can't be preempted or recurse, the 2382bba2c361STejun Heo * holding_cpu can never become this CPU again before we're done. Thus, we can 2383bba2c361STejun Heo * tell whether we lost to dequeue by testing whether the holding_cpu still 2384bba2c361STejun Heo * points to this CPU. See dispatch_dequeue() for the counterpart. 2385bba2c361STejun Heo * 2386bba2c361STejun Heo * On return, @dsq is unlocked and @src_rq is locked. Returns %true if @p is 2387bba2c361STejun Heo * still valid. %false if lost to dequeue. 2388bba2c361STejun Heo */ 2389bba2c361STejun Heo static bool unlink_dsq_and_lock_src_rq(struct task_struct *p, 2390bba2c361STejun Heo struct scx_dispatch_q *dsq, 2391bba2c361STejun Heo struct rq *src_rq) 2392bba2c361STejun Heo { 2393bba2c361STejun Heo s32 cpu = raw_smp_processor_id(); 2394bba2c361STejun Heo 2395bba2c361STejun Heo lockdep_assert_held(&dsq->lock); 2396bba2c361STejun Heo 2397bba2c361STejun Heo WARN_ON_ONCE(p->scx.holding_cpu >= 0); 2398bba2c361STejun Heo task_unlink_from_dsq(p, dsq); 2399bba2c361STejun Heo p->scx.holding_cpu = cpu; 2400bba2c361STejun Heo 2401bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 2402bba2c361STejun Heo raw_spin_rq_lock(src_rq); 2403bba2c361STejun Heo 2404bba2c361STejun Heo /* task_rq couldn't have changed if we're still the holding cpu */ 2405bba2c361STejun Heo return likely(p->scx.holding_cpu == cpu) && 2406bba2c361STejun Heo !WARN_ON_ONCE(src_rq != task_rq(p)); 2407bba2c361STejun Heo } 2408bba2c361STejun Heo 2409bba2c361STejun Heo static bool consume_remote_task(struct rq *this_rq, 2410bba2c361STejun Heo struct task_struct *p, u64 enq_flags, 2411bba2c361STejun Heo struct scx_dispatch_q *dsq, struct rq *src_rq) 2412bba2c361STejun Heo { 2413bba2c361STejun Heo raw_spin_rq_unlock(this_rq); 2414bba2c361STejun Heo 2415bba2c361STejun Heo if (unlink_dsq_and_lock_src_rq(p, dsq, src_rq)) { 2416bba2c361STejun Heo move_remote_task_to_local_dsq(p, enq_flags, src_rq, this_rq); 2417bba2c361STejun Heo return true; 2418bba2c361STejun Heo } else { 2419bba2c361STejun Heo raw_spin_rq_unlock(src_rq); 2420bba2c361STejun Heo raw_spin_rq_lock(this_rq); 2421bba2c361STejun Heo return false; 2422bba2c361STejun Heo } 2423bba2c361STejun Heo } 2424bba2c361STejun Heo 2425bba2c361STejun Heo /** 2426bba2c361STejun Heo * move_task_between_dsqs() - Move a task from one DSQ to another 2427bba2c361STejun Heo * @sch: scx_sched being operated on 2428bba2c361STejun Heo * @p: target task 2429bba2c361STejun Heo * @enq_flags: %SCX_ENQ_* 2430bba2c361STejun Heo * @src_dsq: DSQ @p is currently on, must not be a local DSQ 2431bba2c361STejun Heo * @dst_dsq: DSQ @p is being moved to, can be any DSQ 2432bba2c361STejun Heo * 2433bba2c361STejun Heo * Must be called with @p's task_rq and @src_dsq locked. If @dst_dsq is a local 2434bba2c361STejun Heo * DSQ and @p is on a different CPU, @p will be migrated and thus its task_rq 2435bba2c361STejun Heo * will change. As @p's task_rq is locked, this function doesn't need to use the 2436bba2c361STejun Heo * holding_cpu mechanism. 2437bba2c361STejun Heo * 2438bba2c361STejun Heo * On return, @src_dsq is unlocked and only @p's new task_rq, which is the 2439bba2c361STejun Heo * return value, is locked. 2440bba2c361STejun Heo */ 2441bba2c361STejun Heo static struct rq *move_task_between_dsqs(struct scx_sched *sch, 2442bba2c361STejun Heo struct task_struct *p, u64 enq_flags, 2443bba2c361STejun Heo struct scx_dispatch_q *src_dsq, 2444bba2c361STejun Heo struct scx_dispatch_q *dst_dsq) 2445bba2c361STejun Heo { 2446bba2c361STejun Heo struct rq *src_rq = task_rq(p), *dst_rq; 2447bba2c361STejun Heo 2448bba2c361STejun Heo BUG_ON(src_dsq->id == SCX_DSQ_LOCAL); 2449bba2c361STejun Heo lockdep_assert_held(&src_dsq->lock); 2450bba2c361STejun Heo lockdep_assert_rq_held(src_rq); 2451bba2c361STejun Heo 2452bba2c361STejun Heo if (dst_dsq->id == SCX_DSQ_LOCAL) { 2453bba2c361STejun Heo dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq); 2454bba2c361STejun Heo if (src_rq != dst_rq && 2455bba2c361STejun Heo unlikely(!task_can_run_on_remote_rq(sch, p, dst_rq, true))) { 2456bba2c361STejun Heo dst_dsq = find_global_dsq(sch, task_cpu(p)); 2457bba2c361STejun Heo dst_rq = src_rq; 2458bba2c361STejun Heo enq_flags |= SCX_ENQ_GDSQ_FALLBACK; 2459bba2c361STejun Heo } 2460bba2c361STejun Heo } else { 2461bba2c361STejun Heo /* no need to migrate if destination is a non-local DSQ */ 2462bba2c361STejun Heo dst_rq = src_rq; 2463bba2c361STejun Heo } 2464bba2c361STejun Heo 2465bba2c361STejun Heo /* 2466bba2c361STejun Heo * Move @p into $dst_dsq. If $dst_dsq is the local DSQ of a different 2467bba2c361STejun Heo * CPU, @p will be migrated. 2468bba2c361STejun Heo */ 2469bba2c361STejun Heo if (dst_dsq->id == SCX_DSQ_LOCAL) { 2470bba2c361STejun Heo /* @p is going from a non-local DSQ to a local DSQ */ 2471bba2c361STejun Heo if (src_rq == dst_rq) { 2472bba2c361STejun Heo task_unlink_from_dsq(p, src_dsq); 2473bba2c361STejun Heo move_local_task_to_local_dsq(sch, p, enq_flags, 2474bba2c361STejun Heo src_dsq, dst_rq); 2475bba2c361STejun Heo raw_spin_unlock(&src_dsq->lock); 2476bba2c361STejun Heo } else { 2477bba2c361STejun Heo raw_spin_unlock(&src_dsq->lock); 2478bba2c361STejun Heo move_remote_task_to_local_dsq(p, enq_flags, 2479bba2c361STejun Heo src_rq, dst_rq); 2480bba2c361STejun Heo } 2481bba2c361STejun Heo } else { 2482bba2c361STejun Heo /* 2483bba2c361STejun Heo * @p is going from a non-local DSQ to a non-local DSQ. As 2484bba2c361STejun Heo * $src_dsq is already locked, do an abbreviated dequeue. 2485bba2c361STejun Heo */ 2486bba2c361STejun Heo dispatch_dequeue_locked(p, src_dsq); 2487bba2c361STejun Heo raw_spin_unlock(&src_dsq->lock); 2488bba2c361STejun Heo 2489bba2c361STejun Heo dispatch_enqueue(sch, dst_rq, dst_dsq, p, enq_flags); 2490bba2c361STejun Heo } 2491bba2c361STejun Heo 2492bba2c361STejun Heo return dst_rq; 2493bba2c361STejun Heo } 2494bba2c361STejun Heo 2495bba2c361STejun Heo static bool consume_dispatch_q(struct scx_sched *sch, struct rq *rq, 2496bba2c361STejun Heo struct scx_dispatch_q *dsq, u64 enq_flags) 2497bba2c361STejun Heo { 2498bba2c361STejun Heo struct task_struct *p; 2499bba2c361STejun Heo retry: 2500bba2c361STejun Heo /* 2501bba2c361STejun Heo * The caller can't expect to successfully consume a task if the task's 2502bba2c361STejun Heo * addition to @dsq isn't guaranteed to be visible somehow. Test 2503bba2c361STejun Heo * @dsq->list without locking and skip if it seems empty. 2504bba2c361STejun Heo */ 2505bba2c361STejun Heo if (list_empty(&dsq->list)) 2506bba2c361STejun Heo return false; 2507bba2c361STejun Heo 2508bba2c361STejun Heo raw_spin_lock(&dsq->lock); 2509bba2c361STejun Heo 2510bba2c361STejun Heo nldsq_for_each_task(p, dsq) { 2511bba2c361STejun Heo struct rq *task_rq = task_rq(p); 2512bba2c361STejun Heo 2513bba2c361STejun Heo /* 2514bba2c361STejun Heo * This loop can lead to multiple lockup scenarios, e.g. the BPF 2515bba2c361STejun Heo * scheduler can put an enormous number of affinitized tasks into 2516bba2c361STejun Heo * a contended DSQ, or the outer retry loop can repeatedly race 2517bba2c361STejun Heo * against scx_bypass() dequeueing tasks from @dsq trying to put 2518bba2c361STejun Heo * the system into the bypass mode. This can easily live-lock the 2519bba2c361STejun Heo * machine. If aborting, exit from all non-bypass DSQs. 2520bba2c361STejun Heo */ 2521bba2c361STejun Heo if (unlikely(READ_ONCE(sch->aborting)) && dsq->id != SCX_DSQ_BYPASS) 2522bba2c361STejun Heo break; 2523bba2c361STejun Heo 2524bba2c361STejun Heo if (rq == task_rq) { 2525bba2c361STejun Heo task_unlink_from_dsq(p, dsq); 2526bba2c361STejun Heo move_local_task_to_local_dsq(sch, p, enq_flags, dsq, rq); 2527bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 2528bba2c361STejun Heo return true; 2529bba2c361STejun Heo } 2530bba2c361STejun Heo 2531bba2c361STejun Heo if (task_can_run_on_remote_rq(sch, p, rq, false)) { 2532bba2c361STejun Heo if (likely(consume_remote_task(rq, p, enq_flags, dsq, task_rq))) 2533bba2c361STejun Heo return true; 2534bba2c361STejun Heo goto retry; 2535bba2c361STejun Heo } 2536bba2c361STejun Heo } 2537bba2c361STejun Heo 2538bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 2539bba2c361STejun Heo return false; 2540bba2c361STejun Heo } 2541bba2c361STejun Heo 2542bba2c361STejun Heo static bool consume_global_dsq(struct scx_sched *sch, struct rq *rq) 2543bba2c361STejun Heo { 2544bba2c361STejun Heo int node = cpu_to_node(cpu_of(rq)); 2545bba2c361STejun Heo 2546bba2c361STejun Heo return consume_dispatch_q(sch, rq, &sch->pnode[node]->global_dsq, 0); 2547bba2c361STejun Heo } 2548bba2c361STejun Heo 2549bba2c361STejun Heo /** 2550bba2c361STejun Heo * dispatch_to_local_dsq - Dispatch a task to a local dsq 2551bba2c361STejun Heo * @sch: scx_sched being operated on 2552bba2c361STejun Heo * @rq: current rq which is locked 2553bba2c361STejun Heo * @dst_dsq: destination DSQ 2554bba2c361STejun Heo * @p: task to dispatch 2555bba2c361STejun Heo * @enq_flags: %SCX_ENQ_* 2556bba2c361STejun Heo * 2557bba2c361STejun Heo * We're holding @rq lock and want to dispatch @p to @dst_dsq which is a local 2558bba2c361STejun Heo * DSQ. This function performs all the synchronization dancing needed because 2559bba2c361STejun Heo * local DSQs are protected with rq locks. 2560bba2c361STejun Heo * 2561bba2c361STejun Heo * The caller must have exclusive ownership of @p (e.g. through 2562bba2c361STejun Heo * %SCX_OPSS_DISPATCHING). 2563bba2c361STejun Heo */ 2564bba2c361STejun Heo static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, 2565bba2c361STejun Heo struct scx_dispatch_q *dst_dsq, 2566bba2c361STejun Heo struct task_struct *p, u64 enq_flags) 2567bba2c361STejun Heo { 2568bba2c361STejun Heo struct rq *src_rq = task_rq(p); 2569bba2c361STejun Heo struct rq *dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq); 2570bba2c361STejun Heo struct rq *locked_rq = rq; 2571bba2c361STejun Heo 2572bba2c361STejun Heo /* 2573bba2c361STejun Heo * We're synchronized against dequeue through DISPATCHING. As @p can't 2574bba2c361STejun Heo * be dequeued, its task_rq and cpus_allowed are stable too. 2575bba2c361STejun Heo * 2576bba2c361STejun Heo * If dispatching to @rq that @p is already on, no lock dancing needed. 2577bba2c361STejun Heo */ 2578bba2c361STejun Heo if (rq == src_rq && rq == dst_rq) { 2579bba2c361STejun Heo dispatch_enqueue(sch, rq, dst_dsq, p, 2580bba2c361STejun Heo enq_flags | SCX_ENQ_CLEAR_OPSS); 2581bba2c361STejun Heo return; 2582bba2c361STejun Heo } 2583bba2c361STejun Heo 2584bba2c361STejun Heo if (src_rq != dst_rq && 2585bba2c361STejun Heo unlikely(!task_can_run_on_remote_rq(sch, p, dst_rq, true))) { 2586bba2c361STejun Heo dispatch_enqueue(sch, rq, find_global_dsq(sch, task_cpu(p)), p, 2587bba2c361STejun Heo enq_flags | SCX_ENQ_CLEAR_OPSS | SCX_ENQ_GDSQ_FALLBACK); 2588bba2c361STejun Heo return; 2589bba2c361STejun Heo } 2590bba2c361STejun Heo 2591bba2c361STejun Heo /* 2592bba2c361STejun Heo * @p is on a possibly remote @src_rq which we need to lock to move the 2593bba2c361STejun Heo * task. If dequeue is in progress, it'd be locking @src_rq and waiting 2594bba2c361STejun Heo * on DISPATCHING, so we can't grab @src_rq lock while holding 2595bba2c361STejun Heo * DISPATCHING. 2596bba2c361STejun Heo * 2597bba2c361STejun Heo * As DISPATCHING guarantees that @p is wholly ours, we can pretend that 2598bba2c361STejun Heo * we're moving from a DSQ and use the same mechanism - mark the task 2599bba2c361STejun Heo * under transfer with holding_cpu, release DISPATCHING and then follow 2600bba2c361STejun Heo * the same protocol. See unlink_dsq_and_lock_src_rq(). 2601bba2c361STejun Heo */ 2602bba2c361STejun Heo p->scx.holding_cpu = raw_smp_processor_id(); 2603bba2c361STejun Heo 2604bba2c361STejun Heo /* store_release ensures that dequeue sees the above */ 2605bba2c361STejun Heo atomic_long_set_release(&p->scx.ops_state, SCX_OPSS_NONE); 2606bba2c361STejun Heo 2607bba2c361STejun Heo /* switch to @src_rq lock */ 2608bba2c361STejun Heo if (locked_rq != src_rq) { 2609bba2c361STejun Heo raw_spin_rq_unlock(locked_rq); 2610bba2c361STejun Heo locked_rq = src_rq; 2611bba2c361STejun Heo raw_spin_rq_lock(src_rq); 2612bba2c361STejun Heo } 2613bba2c361STejun Heo 2614bba2c361STejun Heo /* task_rq couldn't have changed if we're still the holding cpu */ 2615bba2c361STejun Heo if (likely(p->scx.holding_cpu == raw_smp_processor_id()) && 2616bba2c361STejun Heo !WARN_ON_ONCE(src_rq != task_rq(p))) { 2617bba2c361STejun Heo /* 2618bba2c361STejun Heo * If @p is staying on the same rq, there's no need to go 2619bba2c361STejun Heo * through the full deactivate/activate cycle. Optimize by 2620bba2c361STejun Heo * abbreviating move_remote_task_to_local_dsq(). 2621bba2c361STejun Heo */ 2622bba2c361STejun Heo if (src_rq == dst_rq) { 2623bba2c361STejun Heo p->scx.holding_cpu = -1; 2624bba2c361STejun Heo dispatch_enqueue(sch, dst_rq, &dst_rq->scx.local_dsq, p, 2625bba2c361STejun Heo enq_flags); 2626bba2c361STejun Heo } else { 2627bba2c361STejun Heo move_remote_task_to_local_dsq(p, enq_flags, 2628bba2c361STejun Heo src_rq, dst_rq); 2629bba2c361STejun Heo /* task has been moved to dst_rq, which is now locked */ 2630bba2c361STejun Heo locked_rq = dst_rq; 2631bba2c361STejun Heo } 2632bba2c361STejun Heo 2633bba2c361STejun Heo /* if the destination CPU is idle, wake it up */ 2634bba2c361STejun Heo if (sched_class_above(p->sched_class, dst_rq->curr->sched_class)) 2635bba2c361STejun Heo resched_curr(dst_rq); 2636bba2c361STejun Heo } 2637bba2c361STejun Heo 2638bba2c361STejun Heo /* switch back to @rq lock */ 2639bba2c361STejun Heo if (locked_rq != rq) { 2640bba2c361STejun Heo raw_spin_rq_unlock(locked_rq); 2641bba2c361STejun Heo raw_spin_rq_lock(rq); 2642bba2c361STejun Heo } 2643bba2c361STejun Heo } 2644bba2c361STejun Heo 2645bba2c361STejun Heo /** 2646bba2c361STejun Heo * finish_dispatch - Asynchronously finish dispatching a task 2647bba2c361STejun Heo * @rq: current rq which is locked 2648bba2c361STejun Heo * @p: task to finish dispatching 2649bba2c361STejun Heo * @qseq_at_dispatch: qseq when @p started getting dispatched 2650bba2c361STejun Heo * @dsq_id: destination DSQ ID 2651bba2c361STejun Heo * @enq_flags: %SCX_ENQ_* 2652bba2c361STejun Heo * 2653bba2c361STejun Heo * Dispatching to local DSQs may need to wait for queueing to complete or 2654bba2c361STejun Heo * require rq lock dancing. As we don't wanna do either while inside 2655bba2c361STejun Heo * ops.dispatch() to avoid locking order inversion, we split dispatching into 2656bba2c361STejun Heo * two parts. scx_bpf_dsq_insert() which is called by ops.dispatch() records the 2657bba2c361STejun Heo * task and its qseq. Once ops.dispatch() returns, this function is called to 2658bba2c361STejun Heo * finish up. 2659bba2c361STejun Heo * 2660bba2c361STejun Heo * There is no guarantee that @p is still valid for dispatching or even that it 2661bba2c361STejun Heo * was valid in the first place. Make sure that the task is still owned by the 2662bba2c361STejun Heo * BPF scheduler and claim the ownership before dispatching. 2663bba2c361STejun Heo */ 2664bba2c361STejun Heo static void finish_dispatch(struct scx_sched *sch, struct rq *rq, 2665bba2c361STejun Heo struct task_struct *p, 2666bba2c361STejun Heo unsigned long qseq_at_dispatch, 2667bba2c361STejun Heo u64 dsq_id, u64 enq_flags) 2668bba2c361STejun Heo { 2669bba2c361STejun Heo struct scx_dispatch_q *dsq; 2670bba2c361STejun Heo unsigned long opss; 2671bba2c361STejun Heo 2672bba2c361STejun Heo touch_core_sched_dispatch(rq, p); 2673bba2c361STejun Heo retry: 2674bba2c361STejun Heo /* 2675bba2c361STejun Heo * No need for _acquire here. @p is accessed only after a successful 2676bba2c361STejun Heo * try_cmpxchg to DISPATCHING. 2677bba2c361STejun Heo */ 2678bba2c361STejun Heo opss = atomic_long_read(&p->scx.ops_state); 2679bba2c361STejun Heo 2680bba2c361STejun Heo switch (opss & SCX_OPSS_STATE_MASK) { 2681bba2c361STejun Heo case SCX_OPSS_DISPATCHING: 2682bba2c361STejun Heo case SCX_OPSS_NONE: 2683bba2c361STejun Heo /* someone else already got to it */ 2684bba2c361STejun Heo return; 2685bba2c361STejun Heo case SCX_OPSS_QUEUED: 2686bba2c361STejun Heo /* 2687bba2c361STejun Heo * If qseq doesn't match, @p has gone through at least one 2688bba2c361STejun Heo * dispatch/dequeue and re-enqueue cycle between 2689bba2c361STejun Heo * scx_bpf_dsq_insert() and here and we have no claim on it. 2690bba2c361STejun Heo */ 2691bba2c361STejun Heo if ((opss & SCX_OPSS_QSEQ_MASK) != qseq_at_dispatch) 2692bba2c361STejun Heo return; 2693bba2c361STejun Heo 2694bba2c361STejun Heo /* see SCX_EV_INSERT_NOT_OWNED definition */ 2695bba2c361STejun Heo if (unlikely(!scx_task_on_sched(sch, p))) { 2696bba2c361STejun Heo __scx_add_event(sch, SCX_EV_INSERT_NOT_OWNED, 1); 2697bba2c361STejun Heo return; 2698bba2c361STejun Heo } 2699bba2c361STejun Heo 2700bba2c361STejun Heo /* 2701bba2c361STejun Heo * While we know @p is accessible, we don't yet have a claim on 2702bba2c361STejun Heo * it - the BPF scheduler is allowed to dispatch tasks 2703bba2c361STejun Heo * spuriously and there can be a racing dequeue attempt. Let's 2704bba2c361STejun Heo * claim @p by atomically transitioning it from QUEUED to 2705bba2c361STejun Heo * DISPATCHING. 2706bba2c361STejun Heo */ 2707bba2c361STejun Heo if (likely(atomic_long_try_cmpxchg(&p->scx.ops_state, &opss, 2708bba2c361STejun Heo SCX_OPSS_DISPATCHING))) 2709bba2c361STejun Heo break; 2710bba2c361STejun Heo goto retry; 2711bba2c361STejun Heo case SCX_OPSS_QUEUEING: 2712bba2c361STejun Heo /* 2713bba2c361STejun Heo * do_enqueue_task() is in the process of transferring the task 2714bba2c361STejun Heo * to the BPF scheduler while holding @p's rq lock. As we aren't 2715bba2c361STejun Heo * holding any kernel or BPF resource that the enqueue path may 2716bba2c361STejun Heo * depend upon, it's safe to wait. 2717bba2c361STejun Heo */ 2718bba2c361STejun Heo wait_ops_state(p, opss); 2719bba2c361STejun Heo goto retry; 2720bba2c361STejun Heo } 2721bba2c361STejun Heo 2722bba2c361STejun Heo BUG_ON(!(p->scx.flags & SCX_TASK_QUEUED)); 2723bba2c361STejun Heo 2724bba2c361STejun Heo dsq = find_dsq_for_dispatch(sch, this_rq(), dsq_id, task_cpu(p)); 2725bba2c361STejun Heo 2726bba2c361STejun Heo if (dsq->id == SCX_DSQ_LOCAL) 2727bba2c361STejun Heo dispatch_to_local_dsq(sch, rq, dsq, p, enq_flags); 2728bba2c361STejun Heo else 2729bba2c361STejun Heo dispatch_enqueue(sch, rq, dsq, p, enq_flags | SCX_ENQ_CLEAR_OPSS); 2730bba2c361STejun Heo } 2731bba2c361STejun Heo 2732bba2c361STejun Heo static void flush_dispatch_buf(struct scx_sched *sch, struct rq *rq) 2733bba2c361STejun Heo { 2734bba2c361STejun Heo struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 2735bba2c361STejun Heo u32 u; 2736bba2c361STejun Heo 2737bba2c361STejun Heo for (u = 0; u < dspc->cursor; u++) { 2738bba2c361STejun Heo struct scx_dsp_buf_ent *ent = &dspc->buf[u]; 2739bba2c361STejun Heo 2740bba2c361STejun Heo finish_dispatch(sch, rq, ent->task, ent->qseq, ent->dsq_id, 2741bba2c361STejun Heo ent->enq_flags); 2742bba2c361STejun Heo } 2743bba2c361STejun Heo 2744bba2c361STejun Heo dspc->nr_tasks += dspc->cursor; 2745bba2c361STejun Heo dspc->cursor = 0; 2746bba2c361STejun Heo } 2747bba2c361STejun Heo 2748bba2c361STejun Heo static inline void maybe_queue_balance_callback(struct rq *rq) 2749bba2c361STejun Heo { 2750bba2c361STejun Heo lockdep_assert_rq_held(rq); 2751bba2c361STejun Heo 2752bba2c361STejun Heo if (!(rq->scx.flags & SCX_RQ_BAL_CB_PENDING)) 2753bba2c361STejun Heo return; 2754bba2c361STejun Heo 2755bba2c361STejun Heo queue_balance_callback(rq, &rq->scx.deferred_bal_cb, 2756bba2c361STejun Heo deferred_bal_cb_workfn); 2757bba2c361STejun Heo 2758bba2c361STejun Heo rq->scx.flags &= ~SCX_RQ_BAL_CB_PENDING; 2759bba2c361STejun Heo } 2760bba2c361STejun Heo 2761bba2c361STejun Heo /* 2762bba2c361STejun Heo * One user of this function is scx_bpf_dispatch() which can be called 2763bba2c361STejun Heo * recursively as sub-sched dispatches nest. Always inline to reduce stack usage 2764bba2c361STejun Heo * from the call frame. 2765bba2c361STejun Heo */ 2766bba2c361STejun Heo static __always_inline bool 2767bba2c361STejun Heo scx_dispatch_sched(struct scx_sched *sch, struct rq *rq, 2768bba2c361STejun Heo struct task_struct *prev, bool nested) 2769bba2c361STejun Heo { 2770bba2c361STejun Heo struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 2771bba2c361STejun Heo int nr_loops = SCX_DSP_MAX_LOOPS; 2772bba2c361STejun Heo s32 cpu = cpu_of(rq); 2773bba2c361STejun Heo bool prev_on_sch = (prev->sched_class == &ext_sched_class) && 2774bba2c361STejun Heo scx_task_on_sched(sch, prev); 2775bba2c361STejun Heo 2776bba2c361STejun Heo if (consume_global_dsq(sch, rq)) 2777bba2c361STejun Heo return true; 2778bba2c361STejun Heo 2779bba2c361STejun Heo if (bypass_dsp_enabled(sch)) { 2780bba2c361STejun Heo /* if @sch is bypassing, only the bypass DSQs are active */ 2781bba2c361STejun Heo if (scx_bypassing(sch, cpu)) 2782bba2c361STejun Heo return consume_dispatch_q(sch, rq, bypass_dsq(sch, cpu), 0); 2783bba2c361STejun Heo 2784bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 2785bba2c361STejun Heo /* 2786bba2c361STejun Heo * If @sch isn't bypassing but its children are, @sch is 2787bba2c361STejun Heo * responsible for making forward progress for both its own 2788bba2c361STejun Heo * tasks that aren't bypassing and the bypassing descendants' 2789bba2c361STejun Heo * tasks. The following implements a simple built-in behavior - 2790bba2c361STejun Heo * let each CPU try to run the bypass DSQ every Nth time. 2791bba2c361STejun Heo * 2792bba2c361STejun Heo * Later, if necessary, we can add an ops flag to suppress the 2793bba2c361STejun Heo * auto-consumption and a kfunc to consume the bypass DSQ and, 2794bba2c361STejun Heo * so that the BPF scheduler can fully control scheduling of 2795bba2c361STejun Heo * bypassed tasks. 2796bba2c361STejun Heo */ 2797bba2c361STejun Heo struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 2798bba2c361STejun Heo 2799bba2c361STejun Heo if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) && 2800bba2c361STejun Heo consume_dispatch_q(sch, rq, bypass_dsq(sch, cpu), 0)) { 2801bba2c361STejun Heo __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1); 2802bba2c361STejun Heo return true; 2803bba2c361STejun Heo } 2804bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 2805bba2c361STejun Heo } 2806bba2c361STejun Heo 2807bba2c361STejun Heo if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !scx_rq_online(rq)) 2808bba2c361STejun Heo return false; 2809bba2c361STejun Heo 2810bba2c361STejun Heo dspc->rq = rq; 2811bba2c361STejun Heo 2812bba2c361STejun Heo /* 2813bba2c361STejun Heo * The dispatch loop. Because flush_dispatch_buf() may drop the rq lock, 2814bba2c361STejun Heo * the local DSQ might still end up empty after a successful 2815bba2c361STejun Heo * ops.dispatch(). If the local DSQ is empty even after ops.dispatch() 2816bba2c361STejun Heo * produced some tasks, retry. The BPF scheduler may depend on this 2817bba2c361STejun Heo * looping behavior to simplify its implementation. 2818bba2c361STejun Heo */ 2819bba2c361STejun Heo do { 2820bba2c361STejun Heo dspc->nr_tasks = 0; 2821bba2c361STejun Heo 2822bba2c361STejun Heo if (nested) { 2823bba2c361STejun Heo SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 2824bba2c361STejun Heo prev_on_sch ? prev : NULL); 2825bba2c361STejun Heo } else { 2826bba2c361STejun Heo /* stash @prev so that nested invocations can access it */ 2827bba2c361STejun Heo rq->scx.sub_dispatch_prev = prev; 2828bba2c361STejun Heo SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 2829bba2c361STejun Heo prev_on_sch ? prev : NULL); 2830bba2c361STejun Heo rq->scx.sub_dispatch_prev = NULL; 2831bba2c361STejun Heo } 2832bba2c361STejun Heo 2833bba2c361STejun Heo flush_dispatch_buf(sch, rq); 2834bba2c361STejun Heo 2835bba2c361STejun Heo if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice) { 2836bba2c361STejun Heo rq->scx.flags |= SCX_RQ_BAL_KEEP; 2837bba2c361STejun Heo return true; 2838bba2c361STejun Heo } 2839bba2c361STejun Heo if (rq->scx.local_dsq.nr) 2840bba2c361STejun Heo return true; 2841bba2c361STejun Heo if (consume_global_dsq(sch, rq)) 2842bba2c361STejun Heo return true; 2843bba2c361STejun Heo 2844bba2c361STejun Heo /* 2845bba2c361STejun Heo * ops.dispatch() can trap us in this loop by repeatedly 2846bba2c361STejun Heo * dispatching ineligible tasks. Break out once in a while to 2847bba2c361STejun Heo * allow the watchdog to run. As IRQ can't be enabled in 2848bba2c361STejun Heo * balance(), we want to complete this scheduling cycle and then 2849bba2c361STejun Heo * start a new one. IOW, we want to call resched_curr() on the 2850bba2c361STejun Heo * next, most likely idle, task, not the current one. Use 2851bba2c361STejun Heo * __scx_bpf_kick_cpu() for deferred kicking. 2852bba2c361STejun Heo */ 2853bba2c361STejun Heo if (unlikely(!--nr_loops)) { 2854bba2c361STejun Heo scx_kick_cpu(sch, cpu, 0); 2855bba2c361STejun Heo break; 2856bba2c361STejun Heo } 2857bba2c361STejun Heo } while (dspc->nr_tasks); 2858bba2c361STejun Heo 2859bba2c361STejun Heo /* 2860bba2c361STejun Heo * Prevent the CPU from going idle while bypassed descendants have tasks 2861bba2c361STejun Heo * queued. Without this fallback, bypassed tasks could stall if the host 2862bba2c361STejun Heo * scheduler's ops.dispatch() doesn't yield any tasks. 2863bba2c361STejun Heo */ 2864bba2c361STejun Heo if (bypass_dsp_enabled(sch)) 2865bba2c361STejun Heo return consume_dispatch_q(sch, rq, bypass_dsq(sch, cpu), 0); 2866bba2c361STejun Heo 2867bba2c361STejun Heo return false; 2868bba2c361STejun Heo } 2869bba2c361STejun Heo 2870bba2c361STejun Heo static int balance_one(struct rq *rq, struct task_struct *prev) 2871bba2c361STejun Heo { 2872bba2c361STejun Heo struct scx_sched *sch = scx_root; 2873bba2c361STejun Heo s32 cpu = cpu_of(rq); 2874bba2c361STejun Heo 2875bba2c361STejun Heo lockdep_assert_rq_held(rq); 2876bba2c361STejun Heo rq->scx.flags |= SCX_RQ_IN_BALANCE; 2877bba2c361STejun Heo rq->scx.flags &= ~SCX_RQ_BAL_KEEP; 2878bba2c361STejun Heo 2879bba2c361STejun Heo if ((sch->ops.flags & SCX_OPS_HAS_CPU_PREEMPT) && 2880bba2c361STejun Heo unlikely(rq->scx.cpu_released)) { 2881bba2c361STejun Heo /* 2882bba2c361STejun Heo * If the previous sched_class for the current CPU was not SCX, 2883bba2c361STejun Heo * notify the BPF scheduler that it again has control of the 2884bba2c361STejun Heo * core. This callback complements ->cpu_release(), which is 2885bba2c361STejun Heo * emitted in switch_class(). 2886bba2c361STejun Heo */ 2887bba2c361STejun Heo if (sch->ops.cpu_acquire) 2888bba2c361STejun Heo SCX_CALL_OP(sch, cpu_acquire, rq, cpu, NULL); 2889bba2c361STejun Heo rq->scx.cpu_released = false; 2890bba2c361STejun Heo } 2891bba2c361STejun Heo 2892bba2c361STejun Heo if (prev->sched_class == &ext_sched_class) { 2893bba2c361STejun Heo update_curr_scx(rq); 2894bba2c361STejun Heo 2895bba2c361STejun Heo /* 2896bba2c361STejun Heo * If @prev is runnable & has slice left, it has priority and 2897bba2c361STejun Heo * fetching more just increases latency for the fetched tasks. 2898bba2c361STejun Heo * Tell pick_task_scx() to keep running @prev. If the BPF 2899bba2c361STejun Heo * scheduler wants to handle this explicitly, it should 2900bba2c361STejun Heo * implement ->cpu_release(). 2901bba2c361STejun Heo * 2902bba2c361STejun Heo * See scx_disable_workfn() for the explanation on the bypassing 2903bba2c361STejun Heo * test. 2904bba2c361STejun Heo */ 2905bba2c361STejun Heo if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice && 2906bba2c361STejun Heo !scx_bypassing(sch, cpu)) { 2907bba2c361STejun Heo rq->scx.flags |= SCX_RQ_BAL_KEEP; 2908bba2c361STejun Heo goto has_tasks; 2909bba2c361STejun Heo } 2910bba2c361STejun Heo } 2911bba2c361STejun Heo 2912bba2c361STejun Heo /* if there already are tasks to run, nothing to do */ 2913bba2c361STejun Heo if (rq->scx.local_dsq.nr) 2914bba2c361STejun Heo goto has_tasks; 2915bba2c361STejun Heo 2916bba2c361STejun Heo if (scx_dispatch_sched(sch, rq, prev, false)) 2917bba2c361STejun Heo goto has_tasks; 2918bba2c361STejun Heo 2919bba2c361STejun Heo /* 2920bba2c361STejun Heo * Didn't find another task to run. Keep running @prev unless 2921bba2c361STejun Heo * %SCX_OPS_ENQ_LAST is in effect. 2922bba2c361STejun Heo */ 2923bba2c361STejun Heo if ((prev->scx.flags & SCX_TASK_QUEUED) && 2924bba2c361STejun Heo (!(sch->ops.flags & SCX_OPS_ENQ_LAST) || scx_bypassing(sch, cpu))) { 2925bba2c361STejun Heo rq->scx.flags |= SCX_RQ_BAL_KEEP; 2926bba2c361STejun Heo __scx_add_event(sch, SCX_EV_DISPATCH_KEEP_LAST, 1); 2927bba2c361STejun Heo goto has_tasks; 2928bba2c361STejun Heo } 2929bba2c361STejun Heo rq->scx.flags &= ~SCX_RQ_IN_BALANCE; 2930bba2c361STejun Heo return false; 2931bba2c361STejun Heo 2932bba2c361STejun Heo has_tasks: 2933bba2c361STejun Heo /* 2934bba2c361STejun Heo * @rq may have extra IMMED tasks without reenq scheduled: 2935bba2c361STejun Heo * 2936bba2c361STejun Heo * - rq_is_open() can't reliably tell when and how slice is going to be 2937bba2c361STejun Heo * modified for $curr and allows IMMED tasks to be queued while 2938bba2c361STejun Heo * dispatch is in progress. 2939bba2c361STejun Heo * 2940bba2c361STejun Heo * - A non-IMMED HEAD task can get queued in front of an IMMED task 2941bba2c361STejun Heo * between the IMMED queueing and the subsequent scheduling event. 2942bba2c361STejun Heo */ 2943bba2c361STejun Heo if (unlikely(rq->scx.local_dsq.nr > 1 && rq->scx.nr_immed)) 2944bba2c361STejun Heo schedule_reenq_local(rq, 0); 2945bba2c361STejun Heo 2946bba2c361STejun Heo rq->scx.flags &= ~SCX_RQ_IN_BALANCE; 2947bba2c361STejun Heo return true; 2948bba2c361STejun Heo } 2949bba2c361STejun Heo 2950bba2c361STejun Heo static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) 2951bba2c361STejun Heo { 2952bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 2953bba2c361STejun Heo 2954bba2c361STejun Heo if (p->scx.flags & SCX_TASK_QUEUED) { 2955bba2c361STejun Heo /* 2956bba2c361STejun Heo * Core-sched might decide to execute @p before it is 2957bba2c361STejun Heo * dispatched. Call ops_dequeue() to notify the BPF scheduler. 2958bba2c361STejun Heo */ 2959bba2c361STejun Heo ops_dequeue(rq, p, SCX_DEQ_CORE_SCHED_EXEC); 2960bba2c361STejun Heo dispatch_dequeue(rq, p); 2961bba2c361STejun Heo } 2962bba2c361STejun Heo 2963bba2c361STejun Heo p->se.exec_start = rq_clock_task(rq); 2964bba2c361STejun Heo 2965bba2c361STejun Heo /* see dequeue_task_scx() on why we skip when !QUEUED */ 2966bba2c361STejun Heo if (SCX_HAS_OP(sch, running) && (p->scx.flags & SCX_TASK_QUEUED)) 2967bba2c361STejun Heo SCX_CALL_OP_TASK(sch, running, rq, p); 2968bba2c361STejun Heo 2969bba2c361STejun Heo clr_task_runnable(p, true); 2970bba2c361STejun Heo 2971bba2c361STejun Heo /* 2972bba2c361STejun Heo * @p is getting newly scheduled or got kicked after someone updated its 2973bba2c361STejun Heo * slice. Refresh whether tick can be stopped. See scx_can_stop_tick(). 2974bba2c361STejun Heo */ 2975bba2c361STejun Heo if ((p->scx.slice == SCX_SLICE_INF) != 2976bba2c361STejun Heo (bool)(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) { 2977bba2c361STejun Heo if (p->scx.slice == SCX_SLICE_INF) 2978bba2c361STejun Heo rq->scx.flags |= SCX_RQ_CAN_STOP_TICK; 2979bba2c361STejun Heo else 2980bba2c361STejun Heo rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK; 2981bba2c361STejun Heo 2982bba2c361STejun Heo sched_update_tick_dependency(rq); 2983bba2c361STejun Heo 2984bba2c361STejun Heo /* 2985bba2c361STejun Heo * For now, let's refresh the load_avgs just when transitioning 2986bba2c361STejun Heo * in and out of nohz. In the future, we might want to add a 2987bba2c361STejun Heo * mechanism which calls the following periodically on 2988bba2c361STejun Heo * tick-stopped CPUs. 2989bba2c361STejun Heo */ 2990bba2c361STejun Heo update_other_load_avgs(rq); 2991bba2c361STejun Heo } 2992bba2c361STejun Heo } 2993bba2c361STejun Heo 2994bba2c361STejun Heo static enum scx_cpu_preempt_reason 2995bba2c361STejun Heo preempt_reason_from_class(const struct sched_class *class) 2996bba2c361STejun Heo { 2997bba2c361STejun Heo if (class == &stop_sched_class) 2998bba2c361STejun Heo return SCX_CPU_PREEMPT_STOP; 2999bba2c361STejun Heo if (class == &dl_sched_class) 3000bba2c361STejun Heo return SCX_CPU_PREEMPT_DL; 3001bba2c361STejun Heo if (class == &rt_sched_class) 3002bba2c361STejun Heo return SCX_CPU_PREEMPT_RT; 3003bba2c361STejun Heo return SCX_CPU_PREEMPT_UNKNOWN; 3004bba2c361STejun Heo } 3005bba2c361STejun Heo 3006bba2c361STejun Heo static void switch_class(struct rq *rq, struct task_struct *next) 3007bba2c361STejun Heo { 3008bba2c361STejun Heo struct scx_sched *sch = scx_root; 3009bba2c361STejun Heo const struct sched_class *next_class = next->sched_class; 3010bba2c361STejun Heo 3011bba2c361STejun Heo if (!(sch->ops.flags & SCX_OPS_HAS_CPU_PREEMPT)) 3012bba2c361STejun Heo return; 3013bba2c361STejun Heo 3014bba2c361STejun Heo /* 3015bba2c361STejun Heo * The callback is conceptually meant to convey that the CPU is no 3016bba2c361STejun Heo * longer under the control of SCX. Therefore, don't invoke the callback 3017bba2c361STejun Heo * if the next class is below SCX (in which case the BPF scheduler has 3018bba2c361STejun Heo * actively decided not to schedule any tasks on the CPU). 3019bba2c361STejun Heo */ 3020bba2c361STejun Heo if (sched_class_above(&ext_sched_class, next_class)) 3021bba2c361STejun Heo return; 3022bba2c361STejun Heo 3023bba2c361STejun Heo /* 3024bba2c361STejun Heo * At this point we know that SCX was preempted by a higher priority 3025bba2c361STejun Heo * sched_class, so invoke the ->cpu_release() callback if we have not 3026bba2c361STejun Heo * done so already. We only send the callback once between SCX being 3027bba2c361STejun Heo * preempted, and it regaining control of the CPU. 3028bba2c361STejun Heo * 3029bba2c361STejun Heo * ->cpu_release() complements ->cpu_acquire(), which is emitted the 3030bba2c361STejun Heo * next time that balance_one() is invoked. 3031bba2c361STejun Heo */ 3032bba2c361STejun Heo if (!rq->scx.cpu_released) { 3033bba2c361STejun Heo if (sch->ops.cpu_release) { 3034bba2c361STejun Heo struct scx_cpu_release_args args = { 3035bba2c361STejun Heo .reason = preempt_reason_from_class(next_class), 3036bba2c361STejun Heo .task = next, 3037bba2c361STejun Heo }; 3038bba2c361STejun Heo 3039bba2c361STejun Heo SCX_CALL_OP(sch, cpu_release, rq, cpu_of(rq), &args); 3040bba2c361STejun Heo } 3041bba2c361STejun Heo rq->scx.cpu_released = true; 3042bba2c361STejun Heo } 3043bba2c361STejun Heo } 3044bba2c361STejun Heo 3045bba2c361STejun Heo static void put_prev_task_scx(struct rq *rq, struct task_struct *p, 3046bba2c361STejun Heo struct task_struct *next) 3047bba2c361STejun Heo { 3048bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 3049bba2c361STejun Heo 3050bba2c361STejun Heo /* see kick_sync_wait_bal_cb() */ 3051bba2c361STejun Heo smp_store_release(&rq->scx.kick_sync, rq->scx.kick_sync + 1); 3052bba2c361STejun Heo 3053bba2c361STejun Heo update_curr_scx(rq); 3054bba2c361STejun Heo 3055bba2c361STejun Heo /* see dequeue_task_scx() on why we skip when !QUEUED */ 3056bba2c361STejun Heo if (SCX_HAS_OP(sch, stopping) && (p->scx.flags & SCX_TASK_QUEUED)) 3057bba2c361STejun Heo SCX_CALL_OP_TASK(sch, stopping, rq, p, true); 3058bba2c361STejun Heo 3059bba2c361STejun Heo if (p->scx.flags & SCX_TASK_QUEUED) { 3060bba2c361STejun Heo set_task_runnable(rq, p); 3061bba2c361STejun Heo 3062bba2c361STejun Heo /* 3063bba2c361STejun Heo * If @p has slice left and is being put, @p is getting 3064bba2c361STejun Heo * preempted by a higher priority scheduler class or core-sched 3065bba2c361STejun Heo * forcing a different task. Leave it at the head of the local 3066bba2c361STejun Heo * DSQ unless it was an IMMED task. IMMED tasks should not 3067bba2c361STejun Heo * linger on a busy CPU, reenqueue them to the BPF scheduler. 3068bba2c361STejun Heo */ 3069bba2c361STejun Heo if (p->scx.slice && !scx_bypassing(sch, cpu_of(rq))) { 3070bba2c361STejun Heo if (p->scx.flags & SCX_TASK_IMMED) { 3071bba2c361STejun Heo p->scx.flags |= SCX_TASK_REENQ_PREEMPTED; 3072bba2c361STejun Heo do_enqueue_task(rq, p, SCX_ENQ_REENQ, -1); 3073bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK; 3074bba2c361STejun Heo } else { 3075bba2c361STejun Heo dispatch_enqueue(sch, rq, &rq->scx.local_dsq, p, SCX_ENQ_HEAD); 3076bba2c361STejun Heo } 3077bba2c361STejun Heo goto switch_class; 3078bba2c361STejun Heo } 3079bba2c361STejun Heo 3080bba2c361STejun Heo /* 3081bba2c361STejun Heo * If @p is runnable but we're about to enter a lower 3082bba2c361STejun Heo * sched_class, %SCX_OPS_ENQ_LAST must be set. Tell 3083bba2c361STejun Heo * ops.enqueue() that @p is the only one available for this cpu, 3084bba2c361STejun Heo * which should trigger an explicit follow-up scheduling event. 3085bba2c361STejun Heo */ 3086bba2c361STejun Heo if (next && sched_class_above(&ext_sched_class, next->sched_class)) { 3087bba2c361STejun Heo WARN_ON_ONCE(!(sch->ops.flags & SCX_OPS_ENQ_LAST)); 3088bba2c361STejun Heo do_enqueue_task(rq, p, SCX_ENQ_LAST, -1); 3089bba2c361STejun Heo } else { 3090bba2c361STejun Heo do_enqueue_task(rq, p, 0, -1); 3091bba2c361STejun Heo } 3092bba2c361STejun Heo } 3093bba2c361STejun Heo 3094bba2c361STejun Heo switch_class: 3095bba2c361STejun Heo if (next && next->sched_class != &ext_sched_class) 3096bba2c361STejun Heo switch_class(rq, next); 3097bba2c361STejun Heo } 3098bba2c361STejun Heo 3099bba2c361STejun Heo static void kick_sync_wait_bal_cb(struct rq *rq) 3100bba2c361STejun Heo { 3101bba2c361STejun Heo struct scx_kick_syncs __rcu *ks = __this_cpu_read(scx_kick_syncs); 3102bba2c361STejun Heo unsigned long *ksyncs = rcu_dereference_sched(ks)->syncs; 3103bba2c361STejun Heo bool waited; 3104bba2c361STejun Heo s32 cpu; 3105bba2c361STejun Heo 3106bba2c361STejun Heo /* 3107bba2c361STejun Heo * Drop rq lock and enable IRQs while waiting. IRQs must be enabled 3108bba2c361STejun Heo * — a target CPU may be waiting for us to process an IPI (e.g. TLB 3109bba2c361STejun Heo * flush) while we wait for its kick_sync to advance. 3110bba2c361STejun Heo * 3111bba2c361STejun Heo * Also, keep advancing our own kick_sync so that new kick_sync waits 3112bba2c361STejun Heo * targeting us, which can start after we drop the lock, cannot form 3113bba2c361STejun Heo * cyclic dependencies. 3114bba2c361STejun Heo */ 3115bba2c361STejun Heo retry: 3116bba2c361STejun Heo waited = false; 3117bba2c361STejun Heo for_each_cpu(cpu, rq->scx.cpus_to_sync) { 3118bba2c361STejun Heo /* 3119bba2c361STejun Heo * smp_load_acquire() pairs with smp_store_release() on 3120bba2c361STejun Heo * kick_sync updates on the target CPUs. 3121bba2c361STejun Heo */ 3122bba2c361STejun Heo if (cpu == cpu_of(rq) || 3123bba2c361STejun Heo smp_load_acquire(&cpu_rq(cpu)->scx.kick_sync) != ksyncs[cpu]) { 3124bba2c361STejun Heo cpumask_clear_cpu(cpu, rq->scx.cpus_to_sync); 3125bba2c361STejun Heo continue; 3126bba2c361STejun Heo } 3127bba2c361STejun Heo 3128bba2c361STejun Heo raw_spin_rq_unlock_irq(rq); 3129bba2c361STejun Heo while (READ_ONCE(cpu_rq(cpu)->scx.kick_sync) == ksyncs[cpu]) { 3130bba2c361STejun Heo smp_store_release(&rq->scx.kick_sync, rq->scx.kick_sync + 1); 3131bba2c361STejun Heo cpu_relax(); 3132bba2c361STejun Heo } 3133bba2c361STejun Heo raw_spin_rq_lock_irq(rq); 3134bba2c361STejun Heo waited = true; 3135bba2c361STejun Heo } 3136bba2c361STejun Heo 3137bba2c361STejun Heo if (waited) 3138bba2c361STejun Heo goto retry; 3139bba2c361STejun Heo } 3140bba2c361STejun Heo 3141bba2c361STejun Heo static struct task_struct *first_local_task(struct rq *rq) 3142bba2c361STejun Heo { 3143bba2c361STejun Heo return list_first_entry_or_null(&rq->scx.local_dsq.list, 3144bba2c361STejun Heo struct task_struct, scx.dsq_list.node); 3145bba2c361STejun Heo } 3146bba2c361STejun Heo 3147bba2c361STejun Heo static struct task_struct * 3148bba2c361STejun Heo do_pick_task_scx(struct rq *rq, struct rq_flags *rf, bool force_scx) 3149bba2c361STejun Heo { 3150bba2c361STejun Heo struct task_struct *prev = rq->curr; 3151bba2c361STejun Heo bool keep_prev; 3152bba2c361STejun Heo struct task_struct *p; 3153bba2c361STejun Heo 3154bba2c361STejun Heo /* see kick_sync_wait_bal_cb() */ 3155bba2c361STejun Heo smp_store_release(&rq->scx.kick_sync, rq->scx.kick_sync + 1); 3156bba2c361STejun Heo 3157bba2c361STejun Heo rq_modified_begin(rq, &ext_sched_class); 3158bba2c361STejun Heo 3159bba2c361STejun Heo rq_unpin_lock(rq, rf); 3160bba2c361STejun Heo balance_one(rq, prev); 3161bba2c361STejun Heo rq_repin_lock(rq, rf); 3162bba2c361STejun Heo maybe_queue_balance_callback(rq); 3163bba2c361STejun Heo 3164bba2c361STejun Heo /* 3165bba2c361STejun Heo * Defer to a balance callback which can drop rq lock and enable 3166bba2c361STejun Heo * IRQs. Waiting directly in the pick path would deadlock against 3167bba2c361STejun Heo * CPUs sending us IPIs (e.g. TLB flushes) while we wait for them. 3168bba2c361STejun Heo */ 3169bba2c361STejun Heo if (unlikely(rq->scx.kick_sync_pending)) { 3170bba2c361STejun Heo rq->scx.kick_sync_pending = false; 3171bba2c361STejun Heo queue_balance_callback(rq, &rq->scx.kick_sync_bal_cb, 3172bba2c361STejun Heo kick_sync_wait_bal_cb); 3173bba2c361STejun Heo } 3174bba2c361STejun Heo 3175bba2c361STejun Heo /* 3176bba2c361STejun Heo * If any higher-priority sched class enqueued a runnable task on 3177bba2c361STejun Heo * this rq during balance_one(), abort and return RETRY_TASK, so 3178bba2c361STejun Heo * that the scheduler loop can restart. 3179bba2c361STejun Heo * 3180bba2c361STejun Heo * If @force_scx is true, always try to pick a SCHED_EXT task, 3181bba2c361STejun Heo * regardless of any higher-priority sched classes activity. 3182bba2c361STejun Heo */ 3183bba2c361STejun Heo if (!force_scx && rq_modified_above(rq, &ext_sched_class)) 3184bba2c361STejun Heo return RETRY_TASK; 3185bba2c361STejun Heo 3186bba2c361STejun Heo keep_prev = rq->scx.flags & SCX_RQ_BAL_KEEP; 3187bba2c361STejun Heo if (unlikely(keep_prev && 3188bba2c361STejun Heo prev->sched_class != &ext_sched_class)) { 3189bba2c361STejun Heo WARN_ON_ONCE(scx_enable_state() == SCX_ENABLED); 3190bba2c361STejun Heo keep_prev = false; 3191bba2c361STejun Heo } 3192bba2c361STejun Heo 3193bba2c361STejun Heo /* 3194bba2c361STejun Heo * If balance_one() is telling us to keep running @prev, replenish slice 3195bba2c361STejun Heo * if necessary and keep running @prev. Otherwise, pop the first one 3196bba2c361STejun Heo * from the local DSQ. 3197bba2c361STejun Heo */ 3198bba2c361STejun Heo if (keep_prev) { 3199bba2c361STejun Heo p = prev; 3200bba2c361STejun Heo if (!p->scx.slice) 3201bba2c361STejun Heo refill_task_slice_dfl(scx_task_sched(p), p); 3202bba2c361STejun Heo } else { 3203bba2c361STejun Heo p = first_local_task(rq); 3204bba2c361STejun Heo if (!p) 3205bba2c361STejun Heo return NULL; 3206bba2c361STejun Heo 3207bba2c361STejun Heo if (unlikely(!p->scx.slice)) { 3208bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 3209bba2c361STejun Heo 3210bba2c361STejun Heo if (!scx_bypassing(sch, cpu_of(rq)) && 3211bba2c361STejun Heo !sch->warned_zero_slice) { 3212bba2c361STejun Heo printk_deferred(KERN_WARNING "sched_ext: %s[%d] has zero slice in %s()\n", 3213bba2c361STejun Heo p->comm, p->pid, __func__); 3214bba2c361STejun Heo sch->warned_zero_slice = true; 3215bba2c361STejun Heo } 3216bba2c361STejun Heo refill_task_slice_dfl(sch, p); 3217bba2c361STejun Heo } 3218bba2c361STejun Heo } 3219bba2c361STejun Heo 3220bba2c361STejun Heo return p; 3221bba2c361STejun Heo } 3222bba2c361STejun Heo 3223bba2c361STejun Heo static struct task_struct *pick_task_scx(struct rq *rq, struct rq_flags *rf) 3224bba2c361STejun Heo { 3225bba2c361STejun Heo return do_pick_task_scx(rq, rf, false); 3226bba2c361STejun Heo } 3227bba2c361STejun Heo 3228bba2c361STejun Heo /* 3229bba2c361STejun Heo * Select the next task to run from the ext scheduling class. 3230bba2c361STejun Heo * 3231bba2c361STejun Heo * Use do_pick_task_scx() directly with @force_scx enabled, since the 3232bba2c361STejun Heo * dl_server must always select a sched_ext task. 3233bba2c361STejun Heo */ 3234bba2c361STejun Heo static struct task_struct * 3235bba2c361STejun Heo ext_server_pick_task(struct sched_dl_entity *dl_se, struct rq_flags *rf) 3236bba2c361STejun Heo { 3237bba2c361STejun Heo if (!scx_enabled()) 3238bba2c361STejun Heo return NULL; 3239bba2c361STejun Heo 3240bba2c361STejun Heo return do_pick_task_scx(dl_se->rq, rf, true); 3241bba2c361STejun Heo } 3242bba2c361STejun Heo 3243bba2c361STejun Heo /* 3244bba2c361STejun Heo * Initialize the ext server deadline entity. 3245bba2c361STejun Heo */ 3246bba2c361STejun Heo void ext_server_init(struct rq *rq) 3247bba2c361STejun Heo { 3248bba2c361STejun Heo struct sched_dl_entity *dl_se = &rq->ext_server; 3249bba2c361STejun Heo 3250bba2c361STejun Heo init_dl_entity(dl_se); 3251bba2c361STejun Heo 3252bba2c361STejun Heo dl_server_init(dl_se, rq, ext_server_pick_task); 3253bba2c361STejun Heo } 3254bba2c361STejun Heo 3255bba2c361STejun Heo #ifdef CONFIG_SCHED_CORE 3256bba2c361STejun Heo /** 3257bba2c361STejun Heo * scx_prio_less - Task ordering for core-sched 3258bba2c361STejun Heo * @a: task A 3259bba2c361STejun Heo * @b: task B 3260bba2c361STejun Heo * @in_fi: in forced idle state 3261bba2c361STejun Heo * 3262bba2c361STejun Heo * Core-sched is implemented as an additional scheduling layer on top of the 3263bba2c361STejun Heo * usual sched_class'es and needs to find out the expected task ordering. For 3264bba2c361STejun Heo * SCX, core-sched calls this function to interrogate the task ordering. 3265bba2c361STejun Heo * 3266bba2c361STejun Heo * Unless overridden by ops.core_sched_before(), @p->scx.core_sched_at is used 3267bba2c361STejun Heo * to implement the default task ordering. The older the timestamp, the higher 3268bba2c361STejun Heo * priority the task - the global FIFO ordering matching the default scheduling 3269bba2c361STejun Heo * behavior. 3270bba2c361STejun Heo * 3271bba2c361STejun Heo * When ops.core_sched_before() is enabled, @p->scx.core_sched_at is used to 3272bba2c361STejun Heo * implement FIFO ordering within each local DSQ. See pick_task_scx(). 3273bba2c361STejun Heo */ 3274bba2c361STejun Heo bool scx_prio_less(const struct task_struct *a, const struct task_struct *b, 3275bba2c361STejun Heo bool in_fi) 3276bba2c361STejun Heo { 3277bba2c361STejun Heo struct scx_sched *sch_a = scx_task_sched(a); 3278bba2c361STejun Heo struct scx_sched *sch_b = scx_task_sched(b); 3279bba2c361STejun Heo 3280bba2c361STejun Heo /* 3281bba2c361STejun Heo * The const qualifiers are dropped from task_struct pointers when 3282bba2c361STejun Heo * calling ops.core_sched_before(). Accesses are controlled by the 3283bba2c361STejun Heo * verifier. 3284bba2c361STejun Heo */ 3285bba2c361STejun Heo if (sch_a == sch_b && SCX_HAS_OP(sch_a, core_sched_before) && 3286bba2c361STejun Heo !scx_bypassing(sch_a, task_cpu(a))) 3287bba2c361STejun Heo return SCX_CALL_OP_2TASKS_RET(sch_a, core_sched_before, 3288bba2c361STejun Heo task_rq(a), 3289bba2c361STejun Heo (struct task_struct *)a, 3290bba2c361STejun Heo (struct task_struct *)b); 3291bba2c361STejun Heo else 3292bba2c361STejun Heo return time_after64(a->scx.core_sched_at, b->scx.core_sched_at); 3293bba2c361STejun Heo } 3294bba2c361STejun Heo #endif /* CONFIG_SCHED_CORE */ 3295bba2c361STejun Heo 3296bba2c361STejun Heo static int select_task_rq_scx(struct task_struct *p, int prev_cpu, int wake_flags) 3297bba2c361STejun Heo { 3298bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 3299bba2c361STejun Heo bool bypassing; 3300bba2c361STejun Heo 3301bba2c361STejun Heo /* 3302bba2c361STejun Heo * sched_exec() calls with %WF_EXEC when @p is about to exec(2) as it 3303bba2c361STejun Heo * can be a good migration opportunity with low cache and memory 3304bba2c361STejun Heo * footprint. Returning a CPU different than @prev_cpu triggers 3305bba2c361STejun Heo * immediate rq migration. However, for SCX, as the current rq 3306bba2c361STejun Heo * association doesn't dictate where the task is going to run, this 3307bba2c361STejun Heo * doesn't fit well. If necessary, we can later add a dedicated method 3308bba2c361STejun Heo * which can decide to preempt self to force it through the regular 3309bba2c361STejun Heo * scheduling path. 3310bba2c361STejun Heo */ 3311bba2c361STejun Heo if (unlikely(wake_flags & WF_EXEC)) 3312bba2c361STejun Heo return prev_cpu; 3313bba2c361STejun Heo 3314bba2c361STejun Heo bypassing = scx_bypassing(sch, task_cpu(p)); 3315bba2c361STejun Heo if (likely(SCX_HAS_OP(sch, select_cpu)) && !bypassing) { 3316bba2c361STejun Heo s32 cpu; 3317bba2c361STejun Heo struct task_struct **ddsp_taskp; 3318bba2c361STejun Heo 3319bba2c361STejun Heo ddsp_taskp = this_cpu_ptr(&direct_dispatch_task); 3320bba2c361STejun Heo WARN_ON_ONCE(*ddsp_taskp); 3321bba2c361STejun Heo *ddsp_taskp = p; 3322bba2c361STejun Heo 3323bba2c361STejun Heo this_rq()->scx.in_select_cpu = true; 3324bba2c361STejun Heo cpu = SCX_CALL_OP_TASK_RET(sch, select_cpu, NULL, p, 3325bba2c361STejun Heo scx_cpu_arg(prev_cpu), wake_flags); 3326bba2c361STejun Heo cpu = scx_cpu_ret(sch, cpu); 3327bba2c361STejun Heo this_rq()->scx.in_select_cpu = false; 3328bba2c361STejun Heo p->scx.selected_cpu = cpu; 3329bba2c361STejun Heo *ddsp_taskp = NULL; 3330bba2c361STejun Heo if (scx_cpu_valid(sch, cpu, "from ops.select_cpu()")) 3331bba2c361STejun Heo return cpu; 3332bba2c361STejun Heo else 3333bba2c361STejun Heo return prev_cpu; 3334bba2c361STejun Heo } else { 3335bba2c361STejun Heo s32 cpu; 3336bba2c361STejun Heo 3337bba2c361STejun Heo cpu = scx_select_cpu_dfl(p, prev_cpu, wake_flags, NULL, 0); 3338bba2c361STejun Heo if (cpu >= 0) { 3339bba2c361STejun Heo refill_task_slice_dfl(sch, p); 3340bba2c361STejun Heo p->scx.ddsp_dsq_id = SCX_DSQ_LOCAL; 3341bba2c361STejun Heo } else { 3342bba2c361STejun Heo cpu = prev_cpu; 3343bba2c361STejun Heo } 3344bba2c361STejun Heo p->scx.selected_cpu = cpu; 3345bba2c361STejun Heo 3346bba2c361STejun Heo if (bypassing) 3347bba2c361STejun Heo __scx_add_event(sch, SCX_EV_BYPASS_DISPATCH, 1); 3348bba2c361STejun Heo return cpu; 3349bba2c361STejun Heo } 3350bba2c361STejun Heo } 3351bba2c361STejun Heo 3352bba2c361STejun Heo static void task_woken_scx(struct rq *rq, struct task_struct *p) 3353bba2c361STejun Heo { 3354bba2c361STejun Heo run_deferred(rq); 3355bba2c361STejun Heo } 3356bba2c361STejun Heo 3357bba2c361STejun Heo static void set_cpus_allowed_scx(struct task_struct *p, 3358bba2c361STejun Heo struct affinity_context *ac) 3359bba2c361STejun Heo { 3360bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 3361bba2c361STejun Heo 3362bba2c361STejun Heo set_cpus_allowed_common(p, ac); 3363bba2c361STejun Heo 3364bba2c361STejun Heo if (task_dead_and_done(p)) 3365bba2c361STejun Heo return; 3366bba2c361STejun Heo 3367bba2c361STejun Heo /* 3368bba2c361STejun Heo * The effective cpumask is stored in @p->cpus_ptr which may temporarily 3369bba2c361STejun Heo * differ from the configured one in @p->cpus_mask. Always tell the bpf 3370bba2c361STejun Heo * scheduler the effective one. 3371bba2c361STejun Heo * 3372bba2c361STejun Heo * Fine-grained memory write control is enforced by BPF making the const 3373bba2c361STejun Heo * designation pointless. Cast it away when calling the operation. 3374bba2c361STejun Heo */ 3375bba2c361STejun Heo if (SCX_HAS_OP(sch, set_cpumask)) 3376bba2c361STejun Heo scx_call_op_set_cpumask(sch, task_rq(p), p, (struct cpumask *)p->cpus_ptr); 3377bba2c361STejun Heo } 3378bba2c361STejun Heo 3379bba2c361STejun Heo static void handle_hotplug(struct rq *rq, bool online) 3380bba2c361STejun Heo { 3381bba2c361STejun Heo struct scx_sched *sch = scx_root; 3382bba2c361STejun Heo s32 cpu = cpu_of(rq); 3383bba2c361STejun Heo 3384bba2c361STejun Heo atomic_long_inc(&scx_hotplug_seq); 3385bba2c361STejun Heo 3386bba2c361STejun Heo /* 3387bba2c361STejun Heo * scx_root updates are protected by cpus_read_lock() and will stay 3388bba2c361STejun Heo * stable here. Note that we can't depend on scx_enabled() test as the 3389bba2c361STejun Heo * hotplug ops need to be enabled before __scx_enabled is set. 3390bba2c361STejun Heo */ 3391bba2c361STejun Heo if (unlikely(!sch)) 3392bba2c361STejun Heo return; 3393bba2c361STejun Heo 3394bba2c361STejun Heo if (scx_enabled()) 3395bba2c361STejun Heo scx_idle_update_selcpu_topology(&sch->ops); 3396bba2c361STejun Heo 3397bba2c361STejun Heo if (online && SCX_HAS_OP(sch, cpu_online)) 3398bba2c361STejun Heo SCX_CALL_OP(sch, cpu_online, NULL, scx_cpu_arg(cpu)); 3399bba2c361STejun Heo else if (!online && SCX_HAS_OP(sch, cpu_offline)) 3400bba2c361STejun Heo SCX_CALL_OP(sch, cpu_offline, NULL, scx_cpu_arg(cpu)); 3401bba2c361STejun Heo else 3402bba2c361STejun Heo scx_exit(sch, SCX_EXIT_UNREG_KERN, 3403bba2c361STejun Heo SCX_ECODE_ACT_RESTART | SCX_ECODE_RSN_HOTPLUG, 3404bba2c361STejun Heo "cpu %d going %s, exiting scheduler", cpu, 3405bba2c361STejun Heo online ? "online" : "offline"); 3406bba2c361STejun Heo } 3407bba2c361STejun Heo 3408bba2c361STejun Heo void scx_rq_activate(struct rq *rq) 3409bba2c361STejun Heo { 3410bba2c361STejun Heo handle_hotplug(rq, true); 3411bba2c361STejun Heo } 3412bba2c361STejun Heo 3413bba2c361STejun Heo void scx_rq_deactivate(struct rq *rq) 3414bba2c361STejun Heo { 3415bba2c361STejun Heo handle_hotplug(rq, false); 3416bba2c361STejun Heo } 3417bba2c361STejun Heo 3418bba2c361STejun Heo static void rq_online_scx(struct rq *rq) 3419bba2c361STejun Heo { 3420bba2c361STejun Heo rq->scx.flags |= SCX_RQ_ONLINE; 3421bba2c361STejun Heo } 3422bba2c361STejun Heo 3423bba2c361STejun Heo static void rq_offline_scx(struct rq *rq) 3424bba2c361STejun Heo { 3425bba2c361STejun Heo rq->scx.flags &= ~SCX_RQ_ONLINE; 3426bba2c361STejun Heo } 3427bba2c361STejun Heo 3428bba2c361STejun Heo static bool check_rq_for_timeouts(struct rq *rq) 3429bba2c361STejun Heo { 3430bba2c361STejun Heo struct scx_sched *sch; 3431bba2c361STejun Heo struct task_struct *p; 3432bba2c361STejun Heo struct rq_flags rf; 3433bba2c361STejun Heo bool timed_out = false; 3434bba2c361STejun Heo 3435bba2c361STejun Heo rq_lock_irqsave(rq, &rf); 3436bba2c361STejun Heo sch = rcu_dereference_bh(scx_root); 3437bba2c361STejun Heo if (unlikely(!sch)) 3438bba2c361STejun Heo goto out_unlock; 3439bba2c361STejun Heo 3440bba2c361STejun Heo list_for_each_entry(p, &rq->scx.runnable_list, scx.runnable_node) { 3441bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 3442bba2c361STejun Heo unsigned long last_runnable = p->scx.runnable_at; 3443bba2c361STejun Heo 3444bba2c361STejun Heo if (unlikely(time_after(jiffies, 3445bba2c361STejun Heo last_runnable + READ_ONCE(sch->watchdog_timeout)))) { 3446bba2c361STejun Heo u32 dur_ms = jiffies_to_msecs(jiffies - last_runnable); 3447bba2c361STejun Heo 3448bba2c361STejun Heo __scx_exit(sch, SCX_EXIT_ERROR_STALL, 0, cpu_of(rq), 3449bba2c361STejun Heo "%s[%d] failed to run for %u.%03us", 3450bba2c361STejun Heo p->comm, p->pid, dur_ms / 1000, 3451bba2c361STejun Heo dur_ms % 1000); 3452bba2c361STejun Heo timed_out = true; 3453bba2c361STejun Heo break; 3454bba2c361STejun Heo } 3455bba2c361STejun Heo } 3456bba2c361STejun Heo out_unlock: 3457bba2c361STejun Heo rq_unlock_irqrestore(rq, &rf); 3458bba2c361STejun Heo return timed_out; 3459bba2c361STejun Heo } 3460bba2c361STejun Heo 3461bba2c361STejun Heo static void scx_watchdog_workfn(struct work_struct *work) 3462bba2c361STejun Heo { 3463bba2c361STejun Heo unsigned long intv; 3464bba2c361STejun Heo int cpu; 3465bba2c361STejun Heo 3466bba2c361STejun Heo WRITE_ONCE(scx_watchdog_timestamp, jiffies); 3467bba2c361STejun Heo 3468bba2c361STejun Heo for_each_online_cpu(cpu) { 3469bba2c361STejun Heo if (unlikely(check_rq_for_timeouts(cpu_rq(cpu)))) 3470bba2c361STejun Heo break; 3471bba2c361STejun Heo 3472bba2c361STejun Heo cond_resched(); 3473bba2c361STejun Heo } 3474bba2c361STejun Heo 3475bba2c361STejun Heo intv = READ_ONCE(scx_watchdog_interval); 3476bba2c361STejun Heo if (intv < ULONG_MAX) 3477bba2c361STejun Heo queue_delayed_work(system_dfl_wq, to_delayed_work(work), intv); 3478bba2c361STejun Heo } 3479bba2c361STejun Heo 3480bba2c361STejun Heo void scx_tick(struct rq *rq) 3481bba2c361STejun Heo { 3482bba2c361STejun Heo struct scx_sched *root; 3483bba2c361STejun Heo unsigned long last_check; 3484bba2c361STejun Heo 3485bba2c361STejun Heo if (!scx_enabled()) 3486bba2c361STejun Heo return; 3487bba2c361STejun Heo 3488bba2c361STejun Heo root = rcu_dereference_bh(scx_root); 3489bba2c361STejun Heo if (unlikely(!root)) 3490bba2c361STejun Heo return; 3491bba2c361STejun Heo 3492bba2c361STejun Heo last_check = READ_ONCE(scx_watchdog_timestamp); 3493bba2c361STejun Heo if (unlikely(time_after(jiffies, 3494bba2c361STejun Heo last_check + READ_ONCE(root->watchdog_timeout)))) { 3495bba2c361STejun Heo u32 dur_ms = jiffies_to_msecs(jiffies - last_check); 3496bba2c361STejun Heo 3497bba2c361STejun Heo scx_exit(root, SCX_EXIT_ERROR_STALL, 0, 3498bba2c361STejun Heo "watchdog failed to check in for %u.%03us", 3499bba2c361STejun Heo dur_ms / 1000, dur_ms % 1000); 3500bba2c361STejun Heo } 3501bba2c361STejun Heo 3502bba2c361STejun Heo update_other_load_avgs(rq); 3503bba2c361STejun Heo } 3504bba2c361STejun Heo 3505bba2c361STejun Heo static void task_tick_scx(struct rq *rq, struct task_struct *curr, int queued) 3506bba2c361STejun Heo { 3507bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(curr); 3508bba2c361STejun Heo 3509bba2c361STejun Heo update_curr_scx(rq); 3510bba2c361STejun Heo 3511bba2c361STejun Heo /* 3512bba2c361STejun Heo * While disabling, always resched and refresh core-sched timestamp as 3513bba2c361STejun Heo * we can't trust the slice management or ops.core_sched_before(). 3514bba2c361STejun Heo */ 3515bba2c361STejun Heo if (scx_bypassing(sch, cpu_of(rq))) { 3516bba2c361STejun Heo curr->scx.slice = 0; 3517bba2c361STejun Heo touch_core_sched(rq, curr); 3518bba2c361STejun Heo } else if (SCX_HAS_OP(sch, tick)) { 3519bba2c361STejun Heo SCX_CALL_OP_TASK(sch, tick, rq, curr); 3520bba2c361STejun Heo } 3521bba2c361STejun Heo 3522bba2c361STejun Heo if (!curr->scx.slice) 3523bba2c361STejun Heo resched_curr(rq); 3524bba2c361STejun Heo } 3525bba2c361STejun Heo 3526bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 3527bba2c361STejun Heo static struct cgroup *tg_cgrp(struct task_group *tg) 3528bba2c361STejun Heo { 3529bba2c361STejun Heo /* 3530bba2c361STejun Heo * If CGROUP_SCHED is disabled, @tg is NULL. If @tg is an autogroup, 3531bba2c361STejun Heo * @tg->css.cgroup is NULL. In both cases, @tg can be treated as the 3532bba2c361STejun Heo * root cgroup. 3533bba2c361STejun Heo */ 3534bba2c361STejun Heo if (tg && tg->css.cgroup) 3535bba2c361STejun Heo return tg->css.cgroup; 3536bba2c361STejun Heo else 3537bba2c361STejun Heo return &cgrp_dfl_root.cgrp; 3538bba2c361STejun Heo } 3539bba2c361STejun Heo 3540bba2c361STejun Heo #define SCX_INIT_TASK_ARGS_CGROUP(tg) .cgroup = tg_cgrp(tg), 3541bba2c361STejun Heo 3542bba2c361STejun Heo #else /* CONFIG_EXT_GROUP_SCHED */ 3543bba2c361STejun Heo 3544bba2c361STejun Heo #define SCX_INIT_TASK_ARGS_CGROUP(tg) 3545bba2c361STejun Heo 3546bba2c361STejun Heo #endif /* CONFIG_EXT_GROUP_SCHED */ 3547bba2c361STejun Heo 3548bba2c361STejun Heo static int __scx_init_task(struct scx_sched *sch, struct task_struct *p, bool fork) 3549bba2c361STejun Heo { 3550bba2c361STejun Heo int ret; 3551bba2c361STejun Heo 3552bba2c361STejun Heo p->scx.disallow = false; 3553bba2c361STejun Heo 3554bba2c361STejun Heo if (SCX_HAS_OP(sch, init_task)) { 3555bba2c361STejun Heo struct scx_init_task_args args = { 3556bba2c361STejun Heo SCX_INIT_TASK_ARGS_CGROUP(task_group(p)) 3557bba2c361STejun Heo .fork = fork, 3558bba2c361STejun Heo }; 3559bba2c361STejun Heo 3560bba2c361STejun Heo ret = SCX_CALL_OP_RET(sch, init_task, NULL, p, &args); 3561bba2c361STejun Heo if (unlikely(ret)) { 3562bba2c361STejun Heo ret = ops_sanitize_err(sch, "init_task", ret); 3563bba2c361STejun Heo return ret; 3564bba2c361STejun Heo } 3565bba2c361STejun Heo } 3566bba2c361STejun Heo 3567bba2c361STejun Heo if (p->scx.disallow) { 3568bba2c361STejun Heo if (unlikely(scx_parent(sch))) { 3569bba2c361STejun Heo scx_error(sch, "non-root ops.init_task() set task->scx.disallow for %s[%d]", 3570bba2c361STejun Heo p->comm, p->pid); 3571bba2c361STejun Heo } else if (unlikely(fork)) { 3572bba2c361STejun Heo scx_error(sch, "ops.init_task() set task->scx.disallow for %s[%d] during fork", 3573bba2c361STejun Heo p->comm, p->pid); 3574bba2c361STejun Heo } else { 3575bba2c361STejun Heo struct rq *rq; 3576bba2c361STejun Heo struct rq_flags rf; 3577bba2c361STejun Heo 3578bba2c361STejun Heo rq = task_rq_lock(p, &rf); 3579bba2c361STejun Heo 3580bba2c361STejun Heo /* 3581bba2c361STejun Heo * We're in the load path and @p->policy will be applied 3582bba2c361STejun Heo * right after. Reverting @p->policy here and rejecting 3583bba2c361STejun Heo * %SCHED_EXT transitions from scx_check_setscheduler() 3584bba2c361STejun Heo * guarantees that if ops.init_task() sets @p->disallow, 3585bba2c361STejun Heo * @p can never be in SCX. 3586bba2c361STejun Heo */ 3587bba2c361STejun Heo if (p->policy == SCHED_EXT) { 3588bba2c361STejun Heo p->policy = SCHED_NORMAL; 3589bba2c361STejun Heo atomic_long_inc(&scx_nr_rejected); 3590bba2c361STejun Heo } 3591bba2c361STejun Heo 3592bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 3593bba2c361STejun Heo } 3594bba2c361STejun Heo } 3595bba2c361STejun Heo 3596bba2c361STejun Heo return 0; 3597bba2c361STejun Heo } 3598bba2c361STejun Heo 3599bba2c361STejun Heo static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p) 3600bba2c361STejun Heo { 3601bba2c361STejun Heo struct rq *rq = task_rq(p); 3602bba2c361STejun Heo u32 weight; 3603bba2c361STejun Heo 3604bba2c361STejun Heo lockdep_assert_rq_held(rq); 3605bba2c361STejun Heo 3606bba2c361STejun Heo /* 3607bba2c361STejun Heo * Verify the task is not in BPF scheduler's custody. If flag 3608bba2c361STejun Heo * transitions are consistent, the flag should always be clear 3609bba2c361STejun Heo * here. 3610bba2c361STejun Heo */ 3611bba2c361STejun Heo WARN_ON_ONCE(p->scx.flags & SCX_TASK_IN_CUSTODY); 3612bba2c361STejun Heo 3613bba2c361STejun Heo /* 3614bba2c361STejun Heo * Set the weight before calling ops.enable() so that the scheduler 3615bba2c361STejun Heo * doesn't see a stale value if they inspect the task struct. 3616bba2c361STejun Heo */ 3617bba2c361STejun Heo if (task_has_idle_policy(p)) 3618bba2c361STejun Heo weight = WEIGHT_IDLEPRIO; 3619bba2c361STejun Heo else 3620bba2c361STejun Heo weight = sched_prio_to_weight[p->static_prio - MAX_RT_PRIO]; 3621bba2c361STejun Heo 3622bba2c361STejun Heo p->scx.weight = sched_weight_to_cgroup(weight); 3623bba2c361STejun Heo 3624bba2c361STejun Heo if (SCX_HAS_OP(sch, enable)) 3625bba2c361STejun Heo SCX_CALL_OP_TASK(sch, enable, rq, p); 3626bba2c361STejun Heo 3627bba2c361STejun Heo if (SCX_HAS_OP(sch, set_weight)) 3628bba2c361STejun Heo SCX_CALL_OP_TASK(sch, set_weight, rq, p, p->scx.weight); 3629bba2c361STejun Heo } 3630bba2c361STejun Heo 3631bba2c361STejun Heo static void scx_enable_task(struct scx_sched *sch, struct task_struct *p) 3632bba2c361STejun Heo { 3633bba2c361STejun Heo __scx_enable_task(sch, p); 3634bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_ENABLED); 3635bba2c361STejun Heo } 3636bba2c361STejun Heo 3637bba2c361STejun Heo static void scx_disable_task(struct scx_sched *sch, struct task_struct *p) 3638bba2c361STejun Heo { 3639bba2c361STejun Heo struct rq *rq = task_rq(p); 3640bba2c361STejun Heo 3641bba2c361STejun Heo lockdep_assert_rq_held(rq); 3642bba2c361STejun Heo WARN_ON_ONCE(scx_get_task_state(p) != SCX_TASK_ENABLED); 3643bba2c361STejun Heo 3644bba2c361STejun Heo clear_direct_dispatch(p); 3645bba2c361STejun Heo 3646bba2c361STejun Heo if (SCX_HAS_OP(sch, disable)) 3647bba2c361STejun Heo SCX_CALL_OP_TASK(sch, disable, rq, p); 3648bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_READY); 3649bba2c361STejun Heo 3650bba2c361STejun Heo /* 3651bba2c361STejun Heo * Verify the task is not in BPF scheduler's custody. If flag 3652bba2c361STejun Heo * transitions are consistent, the flag should always be clear 3653bba2c361STejun Heo * here. 3654bba2c361STejun Heo */ 3655bba2c361STejun Heo WARN_ON_ONCE(p->scx.flags & SCX_TASK_IN_CUSTODY); 3656bba2c361STejun Heo } 3657bba2c361STejun Heo 3658bba2c361STejun Heo static void __scx_disable_and_exit_task(struct scx_sched *sch, 3659bba2c361STejun Heo struct task_struct *p) 3660bba2c361STejun Heo { 3661bba2c361STejun Heo struct scx_exit_task_args args = { 3662bba2c361STejun Heo .cancelled = false, 3663bba2c361STejun Heo }; 3664bba2c361STejun Heo 3665bba2c361STejun Heo lockdep_assert_held(&p->pi_lock); 3666bba2c361STejun Heo lockdep_assert_rq_held(task_rq(p)); 3667bba2c361STejun Heo 3668bba2c361STejun Heo switch (scx_get_task_state(p)) { 3669bba2c361STejun Heo case SCX_TASK_NONE: 3670bba2c361STejun Heo return; 3671bba2c361STejun Heo case SCX_TASK_INIT: 3672bba2c361STejun Heo args.cancelled = true; 3673bba2c361STejun Heo break; 3674bba2c361STejun Heo case SCX_TASK_READY: 3675bba2c361STejun Heo break; 3676bba2c361STejun Heo case SCX_TASK_ENABLED: 3677bba2c361STejun Heo scx_disable_task(sch, p); 3678bba2c361STejun Heo break; 3679bba2c361STejun Heo default: 3680bba2c361STejun Heo WARN_ON_ONCE(true); 3681bba2c361STejun Heo return; 3682bba2c361STejun Heo } 3683bba2c361STejun Heo 3684bba2c361STejun Heo if (SCX_HAS_OP(sch, exit_task)) 3685bba2c361STejun Heo SCX_CALL_OP_TASK(sch, exit_task, task_rq(p), p, &args); 3686bba2c361STejun Heo } 3687bba2c361STejun Heo 3688bba2c361STejun Heo /* 3689bba2c361STejun Heo * Undo a completed __scx_init_task(sch, p, false) when scx_enable_task() never 3690bba2c361STejun Heo * ran. The task state has not been transitioned, so this mirrors the 3691bba2c361STejun Heo * SCX_TASK_INIT branch in __scx_disable_and_exit_task(). 3692bba2c361STejun Heo */ 3693bba2c361STejun Heo static void scx_sub_init_cancel_task(struct scx_sched *sch, struct task_struct *p) 3694bba2c361STejun Heo { 3695bba2c361STejun Heo struct scx_exit_task_args args = { .cancelled = true }; 3696bba2c361STejun Heo 3697bba2c361STejun Heo lockdep_assert_held(&p->pi_lock); 3698bba2c361STejun Heo lockdep_assert_rq_held(task_rq(p)); 3699bba2c361STejun Heo 3700bba2c361STejun Heo if (SCX_HAS_OP(sch, exit_task)) 3701bba2c361STejun Heo SCX_CALL_OP_TASK(sch, exit_task, task_rq(p), p, &args); 3702bba2c361STejun Heo } 3703bba2c361STejun Heo 3704bba2c361STejun Heo static void scx_disable_and_exit_task(struct scx_sched *sch, 3705bba2c361STejun Heo struct task_struct *p) 3706bba2c361STejun Heo { 3707bba2c361STejun Heo __scx_disable_and_exit_task(sch, p); 3708bba2c361STejun Heo 3709bba2c361STejun Heo /* 3710bba2c361STejun Heo * If set, @p exited between __scx_init_task() and scx_enable_task() in 3711bba2c361STejun Heo * scx_sub_enable() and is initialized for both the associated sched and 3712bba2c361STejun Heo * its parent. Exit for the child too - scx_enable_task() never ran for 3713bba2c361STejun Heo * it, so undo only init_task. The flag is only set on the sub-enable 3714bba2c361STejun Heo * path, so it's always clear when @p arrives here in %SCX_TASK_NONE. 3715bba2c361STejun Heo */ 3716bba2c361STejun Heo if (p->scx.flags & SCX_TASK_SUB_INIT) { 3717bba2c361STejun Heo if (!WARN_ON_ONCE(!scx_enabling_sub_sched)) 3718bba2c361STejun Heo scx_sub_init_cancel_task(scx_enabling_sub_sched, p); 3719bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_SUB_INIT; 3720bba2c361STejun Heo } 3721bba2c361STejun Heo 3722bba2c361STejun Heo scx_set_task_sched(p, NULL); 3723bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_NONE); 3724bba2c361STejun Heo } 3725bba2c361STejun Heo 3726bba2c361STejun Heo void init_scx_entity(struct sched_ext_entity *scx) 3727bba2c361STejun Heo { 3728bba2c361STejun Heo memset(scx, 0, sizeof(*scx)); 3729bba2c361STejun Heo INIT_LIST_HEAD(&scx->dsq_list.node); 3730bba2c361STejun Heo RB_CLEAR_NODE(&scx->dsq_priq); 3731bba2c361STejun Heo scx->sticky_cpu = -1; 3732bba2c361STejun Heo scx->holding_cpu = -1; 3733bba2c361STejun Heo INIT_LIST_HEAD(&scx->runnable_node); 3734bba2c361STejun Heo scx->runnable_at = jiffies; 3735bba2c361STejun Heo scx->ddsp_dsq_id = SCX_DSQ_INVALID; 3736bba2c361STejun Heo scx->slice = SCX_SLICE_DFL; 3737bba2c361STejun Heo } 3738bba2c361STejun Heo 3739bba2c361STejun Heo /* See scx_tid_alloc / scx_tid_cursor. */ 3740bba2c361STejun Heo static u64 scx_alloc_tid(void) 3741bba2c361STejun Heo { 3742bba2c361STejun Heo struct scx_tid_alloc *ta; 3743bba2c361STejun Heo 3744bba2c361STejun Heo guard(preempt)(); 3745bba2c361STejun Heo ta = this_cpu_ptr(&scx_tid_alloc); 3746bba2c361STejun Heo 3747bba2c361STejun Heo if (unlikely(ta->next >= ta->end)) { 3748bba2c361STejun Heo ta->next = atomic64_fetch_add(SCX_TID_CHUNK, &scx_tid_cursor); 3749bba2c361STejun Heo ta->end = ta->next + SCX_TID_CHUNK; 3750bba2c361STejun Heo } 3751bba2c361STejun Heo return ta->next++; 3752bba2c361STejun Heo } 3753bba2c361STejun Heo 3754bba2c361STejun Heo static void scx_tid_hash_insert(struct task_struct *p) 3755bba2c361STejun Heo { 3756bba2c361STejun Heo int ret; 3757bba2c361STejun Heo 3758bba2c361STejun Heo lockdep_assert_held(&scx_tasks_lock); 3759bba2c361STejun Heo 3760bba2c361STejun Heo ret = rhashtable_lookup_insert_fast(&scx_tid_hash, 3761bba2c361STejun Heo &p->scx.tid_hash_node, 3762bba2c361STejun Heo scx_tid_hash_params); 3763bba2c361STejun Heo WARN_ON_ONCE(ret); 3764bba2c361STejun Heo } 3765bba2c361STejun Heo 3766bba2c361STejun Heo void scx_pre_fork(struct task_struct *p) 3767bba2c361STejun Heo { 3768bba2c361STejun Heo /* 3769bba2c361STejun Heo * BPF scheduler enable/disable paths want to be able to iterate and 3770bba2c361STejun Heo * update all tasks which can become complex when racing forks. As 3771bba2c361STejun Heo * enable/disable are very cold paths, let's use a percpu_rwsem to 3772bba2c361STejun Heo * exclude forks. 3773bba2c361STejun Heo */ 3774bba2c361STejun Heo percpu_down_read(&scx_fork_rwsem); 3775bba2c361STejun Heo } 3776bba2c361STejun Heo 3777bba2c361STejun Heo int scx_fork(struct task_struct *p, struct kernel_clone_args *kargs) 3778bba2c361STejun Heo { 3779bba2c361STejun Heo s32 ret; 3780bba2c361STejun Heo 3781bba2c361STejun Heo percpu_rwsem_assert_held(&scx_fork_rwsem); 3782bba2c361STejun Heo 3783bba2c361STejun Heo p->scx.tid = scx_alloc_tid(); 3784bba2c361STejun Heo 3785bba2c361STejun Heo if (scx_init_task_enabled) { 3786bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 3787bba2c361STejun Heo struct scx_sched *sch = kargs->cset->dfl_cgrp->scx_sched; 3788bba2c361STejun Heo #else 3789bba2c361STejun Heo struct scx_sched *sch = scx_root; 3790bba2c361STejun Heo #endif 3791bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_INIT_BEGIN); 3792bba2c361STejun Heo ret = __scx_init_task(sch, p, true); 3793bba2c361STejun Heo if (unlikely(ret)) { 3794bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_NONE); 3795bba2c361STejun Heo return ret; 3796bba2c361STejun Heo } 3797bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_INIT); 3798bba2c361STejun Heo scx_set_task_sched(p, sch); 3799bba2c361STejun Heo } 3800bba2c361STejun Heo 3801bba2c361STejun Heo return 0; 3802bba2c361STejun Heo } 3803bba2c361STejun Heo 3804bba2c361STejun Heo void scx_post_fork(struct task_struct *p) 3805bba2c361STejun Heo { 3806bba2c361STejun Heo if (scx_init_task_enabled) { 3807bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_READY); 3808bba2c361STejun Heo 3809bba2c361STejun Heo /* 3810bba2c361STejun Heo * Enable the task immediately if it's running on sched_ext. 3811bba2c361STejun Heo * Otherwise, it'll be enabled in switching_to_scx() if and 3812bba2c361STejun Heo * when it's ever configured to run with a SCHED_EXT policy. 3813bba2c361STejun Heo */ 3814bba2c361STejun Heo if (p->sched_class == &ext_sched_class) { 3815bba2c361STejun Heo struct rq_flags rf; 3816bba2c361STejun Heo struct rq *rq; 3817bba2c361STejun Heo 3818bba2c361STejun Heo rq = task_rq_lock(p, &rf); 3819bba2c361STejun Heo scx_enable_task(scx_task_sched(p), p); 3820bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 3821bba2c361STejun Heo } 3822bba2c361STejun Heo } 3823bba2c361STejun Heo 3824bba2c361STejun Heo scoped_guard(raw_spinlock_irq, &scx_tasks_lock) { 3825bba2c361STejun Heo list_add_tail(&p->scx.tasks_node, &scx_tasks); 3826bba2c361STejun Heo if (scx_tid_to_task_enabled()) 3827bba2c361STejun Heo scx_tid_hash_insert(p); 3828bba2c361STejun Heo } 3829bba2c361STejun Heo 3830bba2c361STejun Heo percpu_up_read(&scx_fork_rwsem); 3831bba2c361STejun Heo } 3832bba2c361STejun Heo 3833bba2c361STejun Heo void scx_cancel_fork(struct task_struct *p) 3834bba2c361STejun Heo { 3835bba2c361STejun Heo if (scx_enabled()) { 3836bba2c361STejun Heo struct rq *rq; 3837bba2c361STejun Heo struct rq_flags rf; 3838bba2c361STejun Heo 3839bba2c361STejun Heo rq = task_rq_lock(p, &rf); 3840bba2c361STejun Heo WARN_ON_ONCE(scx_get_task_state(p) >= SCX_TASK_READY); 3841bba2c361STejun Heo scx_disable_and_exit_task(scx_task_sched(p), p); 3842bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 3843bba2c361STejun Heo } 3844bba2c361STejun Heo 3845bba2c361STejun Heo percpu_up_read(&scx_fork_rwsem); 3846bba2c361STejun Heo } 3847bba2c361STejun Heo 3848bba2c361STejun Heo /** 3849bba2c361STejun Heo * task_dead_and_done - Is a task dead and done running? 3850bba2c361STejun Heo * @p: target task 3851bba2c361STejun Heo * 3852bba2c361STejun Heo * Once sched_ext_dead() removes the dead task from scx_tasks and exits it, the 3853bba2c361STejun Heo * task no longer exists from SCX's POV. However, certain sched_class ops may be 3854bba2c361STejun Heo * invoked on these dead tasks leading to failures - e.g. sched_setscheduler() 3855bba2c361STejun Heo * may try to switch a task which finished sched_ext_dead() back into SCX 3856bba2c361STejun Heo * triggering invalid SCX task state transitions and worse. 3857bba2c361STejun Heo * 3858bba2c361STejun Heo * Once a task has finished the final switch, sched_ext_dead() is the only thing 3859bba2c361STejun Heo * that needs to happen on the task. Use this test to short-circuit sched_class 3860bba2c361STejun Heo * operations which may be called on dead tasks. 3861bba2c361STejun Heo */ 3862bba2c361STejun Heo static bool task_dead_and_done(struct task_struct *p) 3863bba2c361STejun Heo { 3864bba2c361STejun Heo struct rq *rq = task_rq(p); 3865bba2c361STejun Heo 3866bba2c361STejun Heo lockdep_assert_rq_held(rq); 3867bba2c361STejun Heo 3868bba2c361STejun Heo /* 3869bba2c361STejun Heo * In do_task_dead(), a dying task sets %TASK_DEAD with preemption 3870bba2c361STejun Heo * disabled and __schedule(). If @p has %TASK_DEAD set and off CPU, @p 3871bba2c361STejun Heo * won't ever run again. 3872bba2c361STejun Heo */ 3873bba2c361STejun Heo return unlikely(READ_ONCE(p->__state) == TASK_DEAD) && 3874bba2c361STejun Heo !task_on_cpu(rq, p); 3875bba2c361STejun Heo } 3876bba2c361STejun Heo 3877bba2c361STejun Heo void sched_ext_dead(struct task_struct *p) 3878bba2c361STejun Heo { 3879bba2c361STejun Heo /* 3880bba2c361STejun Heo * By the time control reaches here, @p has %TASK_DEAD set, switched out 3881bba2c361STejun Heo * for the last time and then dropped the rq lock - task_dead_and_done() 3882bba2c361STejun Heo * should be returning %true nullifying the straggling sched_class ops. 3883bba2c361STejun Heo * Remove from scx_tasks and exit @p. 3884bba2c361STejun Heo */ 3885bba2c361STejun Heo scoped_guard(raw_spinlock_irqsave, &scx_tasks_lock) { 3886bba2c361STejun Heo list_del_init(&p->scx.tasks_node); 3887bba2c361STejun Heo if (scx_tid_to_task_enabled()) 3888bba2c361STejun Heo rhashtable_remove_fast(&scx_tid_hash, 3889bba2c361STejun Heo &p->scx.tid_hash_node, 3890bba2c361STejun Heo scx_tid_hash_params); 3891bba2c361STejun Heo } 3892bba2c361STejun Heo 3893bba2c361STejun Heo /* 3894bba2c361STejun Heo * @p is off scx_tasks and wholly ours. scx_root_enable()'s READY -> 3895bba2c361STejun Heo * ENABLED transitions can't race us. Disable ops for @p. 3896bba2c361STejun Heo * 3897bba2c361STejun Heo * %SCX_TASK_DEAD synchronizes against cgroup task iteration - see 3898bba2c361STejun Heo * scx_task_iter_next_locked(). NONE tasks need no marking: cgroup 3899bba2c361STejun Heo * iteration is only used from sub-sched paths, which require root 3900bba2c361STejun Heo * enabled. Root enable transitions every live task to at least READY. 3901bba2c361STejun Heo * 3902bba2c361STejun Heo * %INIT_BEGIN means ops.init_task() is running for @p. Don't call 3903bba2c361STejun Heo * into ops; transition to %DEAD so the post-init recheck unwinds 3904bba2c361STejun Heo * via scx_sub_init_cancel_task(). 3905bba2c361STejun Heo */ 3906bba2c361STejun Heo if (scx_get_task_state(p) != SCX_TASK_NONE) { 3907bba2c361STejun Heo struct rq_flags rf; 3908bba2c361STejun Heo struct rq *rq; 3909bba2c361STejun Heo 3910bba2c361STejun Heo rq = task_rq_lock(p, &rf); 3911bba2c361STejun Heo if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN) 3912bba2c361STejun Heo scx_disable_and_exit_task(scx_task_sched(p), p); 3913bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_DEAD); 3914bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 3915bba2c361STejun Heo } 3916bba2c361STejun Heo } 3917bba2c361STejun Heo 3918bba2c361STejun Heo static void reweight_task_scx(struct rq *rq, struct task_struct *p, 3919bba2c361STejun Heo const struct load_weight *lw) 3920bba2c361STejun Heo { 3921bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 3922bba2c361STejun Heo 3923bba2c361STejun Heo lockdep_assert_rq_held(task_rq(p)); 3924bba2c361STejun Heo 3925bba2c361STejun Heo if (task_dead_and_done(p)) 3926bba2c361STejun Heo return; 3927bba2c361STejun Heo 3928bba2c361STejun Heo p->scx.weight = sched_weight_to_cgroup(scale_load_down(lw->weight)); 3929bba2c361STejun Heo if (SCX_HAS_OP(sch, set_weight)) 3930bba2c361STejun Heo SCX_CALL_OP_TASK(sch, set_weight, rq, p, p->scx.weight); 3931bba2c361STejun Heo } 3932bba2c361STejun Heo 3933bba2c361STejun Heo static void prio_changed_scx(struct rq *rq, struct task_struct *p, u64 oldprio) 3934bba2c361STejun Heo { 3935bba2c361STejun Heo } 3936bba2c361STejun Heo 3937bba2c361STejun Heo static void switching_to_scx(struct rq *rq, struct task_struct *p) 3938bba2c361STejun Heo { 3939bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 3940bba2c361STejun Heo 3941bba2c361STejun Heo if (task_dead_and_done(p)) 3942bba2c361STejun Heo return; 3943bba2c361STejun Heo 3944bba2c361STejun Heo scx_enable_task(sch, p); 3945bba2c361STejun Heo 3946bba2c361STejun Heo /* 3947bba2c361STejun Heo * set_cpus_allowed_scx() is not called while @p is associated with a 3948bba2c361STejun Heo * different scheduler class. Keep the BPF scheduler up-to-date. 3949bba2c361STejun Heo */ 3950bba2c361STejun Heo if (SCX_HAS_OP(sch, set_cpumask)) 3951bba2c361STejun Heo scx_call_op_set_cpumask(sch, rq, p, (struct cpumask *)p->cpus_ptr); 3952bba2c361STejun Heo } 3953bba2c361STejun Heo 3954bba2c361STejun Heo static void switched_from_scx(struct rq *rq, struct task_struct *p) 3955bba2c361STejun Heo { 3956bba2c361STejun Heo if (task_dead_and_done(p)) 3957bba2c361STejun Heo return; 3958bba2c361STejun Heo 3959bba2c361STejun Heo /* 3960bba2c361STejun Heo * %NONE means SCX is no longer tracking @p at the task level (e.g. 3961bba2c361STejun Heo * scx_fail_parent() handed @p back to the parent at NONE pending the 3962bba2c361STejun Heo * parent's own teardown). There is nothing to disable; calling 3963bba2c361STejun Heo * scx_disable_task() would WARN on the non-%ENABLED state and trigger a 3964bba2c361STejun Heo * NONE -> READY validation failure. 3965bba2c361STejun Heo */ 3966bba2c361STejun Heo if (scx_get_task_state(p) == SCX_TASK_NONE) 3967bba2c361STejun Heo return; 3968bba2c361STejun Heo 3969bba2c361STejun Heo scx_disable_task(scx_task_sched(p), p); 3970bba2c361STejun Heo } 3971bba2c361STejun Heo 3972bba2c361STejun Heo static void switched_to_scx(struct rq *rq, struct task_struct *p) {} 3973bba2c361STejun Heo 3974bba2c361STejun Heo int scx_check_setscheduler(struct task_struct *p, int policy) 3975bba2c361STejun Heo { 3976bba2c361STejun Heo lockdep_assert_rq_held(task_rq(p)); 3977bba2c361STejun Heo 3978bba2c361STejun Heo /* if disallow, reject transitioning into SCX */ 3979bba2c361STejun Heo if (scx_enabled() && READ_ONCE(p->scx.disallow) && 3980bba2c361STejun Heo p->policy != policy && policy == SCHED_EXT) 3981bba2c361STejun Heo return -EACCES; 3982bba2c361STejun Heo 3983bba2c361STejun Heo return 0; 3984bba2c361STejun Heo } 3985bba2c361STejun Heo 3986bba2c361STejun Heo static void process_ddsp_deferred_locals(struct rq *rq) 3987bba2c361STejun Heo { 3988bba2c361STejun Heo struct task_struct *p; 3989bba2c361STejun Heo 3990bba2c361STejun Heo lockdep_assert_rq_held(rq); 3991bba2c361STejun Heo 3992bba2c361STejun Heo /* 3993bba2c361STejun Heo * Now that @rq can be unlocked, execute the deferred enqueueing of 3994bba2c361STejun Heo * tasks directly dispatched to the local DSQs of other CPUs. See 3995bba2c361STejun Heo * direct_dispatch(). Keep popping from the head instead of using 3996bba2c361STejun Heo * list_for_each_entry_safe() as dispatch_local_dsq() may unlock @rq 3997bba2c361STejun Heo * temporarily. 3998bba2c361STejun Heo */ 3999bba2c361STejun Heo while ((p = list_first_entry_or_null(&rq->scx.ddsp_deferred_locals, 4000bba2c361STejun Heo struct task_struct, scx.dsq_list.node))) { 4001bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 4002bba2c361STejun Heo struct scx_dispatch_q *dsq; 4003bba2c361STejun Heo u64 dsq_id = p->scx.ddsp_dsq_id; 4004bba2c361STejun Heo u64 enq_flags = p->scx.ddsp_enq_flags; 4005bba2c361STejun Heo 4006bba2c361STejun Heo list_del_init(&p->scx.dsq_list.node); 4007bba2c361STejun Heo clear_direct_dispatch(p); 4008bba2c361STejun Heo 4009bba2c361STejun Heo dsq = find_dsq_for_dispatch(sch, rq, dsq_id, task_cpu(p)); 4010bba2c361STejun Heo if (!WARN_ON_ONCE(dsq->id != SCX_DSQ_LOCAL)) 4011bba2c361STejun Heo dispatch_to_local_dsq(sch, rq, dsq, p, enq_flags); 4012bba2c361STejun Heo } 4013bba2c361STejun Heo } 4014bba2c361STejun Heo 4015bba2c361STejun Heo /* 4016bba2c361STejun Heo * Determine whether @p should be reenqueued from a local DSQ. 4017bba2c361STejun Heo * 4018bba2c361STejun Heo * @reenq_flags is mutable and accumulates state across the DSQ walk: 4019bba2c361STejun Heo * 4020bba2c361STejun Heo * - %SCX_REENQ_TSR_NOT_FIRST: Set after the first task is visited. "First" 4021bba2c361STejun Heo * tracks position in the DSQ list, not among IMMED tasks. A non-IMMED task at 4022bba2c361STejun Heo * the head consumes the first slot. 4023bba2c361STejun Heo * 4024bba2c361STejun Heo * - %SCX_REENQ_TSR_RQ_OPEN: Set by reenq_local() before the walk if 4025bba2c361STejun Heo * rq_is_open() is true. 4026bba2c361STejun Heo * 4027bba2c361STejun Heo * An IMMED task is kept (returns %false) only if it's the first task in the DSQ 4028bba2c361STejun Heo * AND the current task is done — i.e. it will execute immediately. All other 4029bba2c361STejun Heo * IMMED tasks are reenqueued. This means if a non-IMMED task sits at the head, 4030bba2c361STejun Heo * every IMMED task behind it gets reenqueued. 4031bba2c361STejun Heo * 4032bba2c361STejun Heo * Reenqueued tasks go through ops.enqueue() with %SCX_ENQ_REENQ | 4033bba2c361STejun Heo * %SCX_TASK_REENQ_IMMED. If the BPF scheduler dispatches back to the same local 4034bba2c361STejun Heo * DSQ with %SCX_ENQ_IMMED while the CPU is still unavailable, this triggers 4035bba2c361STejun Heo * another reenq cycle. Repetitions are bounded by %SCX_REENQ_LOCAL_MAX_REPEAT 4036bba2c361STejun Heo * in process_deferred_reenq_locals(). 4037bba2c361STejun Heo */ 4038bba2c361STejun Heo static bool local_task_should_reenq(struct task_struct *p, u64 *reenq_flags, u32 *reason) 4039bba2c361STejun Heo { 4040bba2c361STejun Heo bool first; 4041bba2c361STejun Heo 4042bba2c361STejun Heo first = !(*reenq_flags & SCX_REENQ_TSR_NOT_FIRST); 4043bba2c361STejun Heo *reenq_flags |= SCX_REENQ_TSR_NOT_FIRST; 4044bba2c361STejun Heo 4045bba2c361STejun Heo *reason = SCX_TASK_REENQ_KFUNC; 4046bba2c361STejun Heo 4047bba2c361STejun Heo if ((p->scx.flags & SCX_TASK_IMMED) && 4048bba2c361STejun Heo (!first || !(*reenq_flags & SCX_REENQ_TSR_RQ_OPEN))) { 4049bba2c361STejun Heo __scx_add_event(scx_task_sched(p), SCX_EV_REENQ_IMMED, 1); 4050bba2c361STejun Heo *reason = SCX_TASK_REENQ_IMMED; 4051bba2c361STejun Heo return true; 4052bba2c361STejun Heo } 4053bba2c361STejun Heo 4054bba2c361STejun Heo return *reenq_flags & SCX_REENQ_ANY; 4055bba2c361STejun Heo } 4056bba2c361STejun Heo 4057bba2c361STejun Heo static u32 reenq_local(struct scx_sched *sch, struct rq *rq, u64 reenq_flags) 4058bba2c361STejun Heo { 4059bba2c361STejun Heo LIST_HEAD(tasks); 4060bba2c361STejun Heo u32 nr_enqueued = 0; 4061bba2c361STejun Heo struct task_struct *p, *n; 4062bba2c361STejun Heo 4063bba2c361STejun Heo lockdep_assert_rq_held(rq); 4064bba2c361STejun Heo 4065bba2c361STejun Heo if (WARN_ON_ONCE(reenq_flags & __SCX_REENQ_TSR_MASK)) 4066bba2c361STejun Heo reenq_flags &= ~__SCX_REENQ_TSR_MASK; 4067bba2c361STejun Heo if (rq_is_open(rq, 0)) 4068bba2c361STejun Heo reenq_flags |= SCX_REENQ_TSR_RQ_OPEN; 4069bba2c361STejun Heo 4070bba2c361STejun Heo /* 4071bba2c361STejun Heo * The BPF scheduler may choose to dispatch tasks back to 4072bba2c361STejun Heo * @rq->scx.local_dsq. Move all candidate tasks off to a private list 4073bba2c361STejun Heo * first to avoid processing the same tasks repeatedly. 4074bba2c361STejun Heo */ 4075bba2c361STejun Heo list_for_each_entry_safe(p, n, &rq->scx.local_dsq.list, 4076bba2c361STejun Heo scx.dsq_list.node) { 4077bba2c361STejun Heo struct scx_sched *task_sch = scx_task_sched(p); 4078bba2c361STejun Heo u32 reason; 4079bba2c361STejun Heo 4080bba2c361STejun Heo /* 4081bba2c361STejun Heo * If @p is being migrated, @p's current CPU may not agree with 4082bba2c361STejun Heo * its allowed CPUs and the migration_cpu_stop is about to 4083bba2c361STejun Heo * deactivate and re-activate @p anyway. Skip re-enqueueing. 4084bba2c361STejun Heo * 4085bba2c361STejun Heo * While racing sched property changes may also dequeue and 4086bba2c361STejun Heo * re-enqueue a migrating task while its current CPU and allowed 4087bba2c361STejun Heo * CPUs disagree, they use %ENQUEUE_RESTORE which is bypassed to 4088bba2c361STejun Heo * the current local DSQ for running tasks and thus are not 4089bba2c361STejun Heo * visible to the BPF scheduler. 4090bba2c361STejun Heo */ 4091bba2c361STejun Heo if (p->migration_pending) 4092bba2c361STejun Heo continue; 4093bba2c361STejun Heo 4094bba2c361STejun Heo if (!scx_is_descendant(task_sch, sch)) 4095bba2c361STejun Heo continue; 4096bba2c361STejun Heo 4097bba2c361STejun Heo if (!local_task_should_reenq(p, &reenq_flags, &reason)) 4098bba2c361STejun Heo continue; 4099bba2c361STejun Heo 4100bba2c361STejun Heo dispatch_dequeue(rq, p); 4101bba2c361STejun Heo 4102bba2c361STejun Heo if (WARN_ON_ONCE(p->scx.flags & SCX_TASK_REENQ_REASON_MASK)) 4103bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK; 4104bba2c361STejun Heo p->scx.flags |= reason; 4105bba2c361STejun Heo 4106bba2c361STejun Heo list_add_tail(&p->scx.dsq_list.node, &tasks); 4107bba2c361STejun Heo } 4108bba2c361STejun Heo 4109bba2c361STejun Heo list_for_each_entry_safe(p, n, &tasks, scx.dsq_list.node) { 4110bba2c361STejun Heo list_del_init(&p->scx.dsq_list.node); 4111bba2c361STejun Heo 4112bba2c361STejun Heo do_enqueue_task(rq, p, SCX_ENQ_REENQ, -1); 4113bba2c361STejun Heo 4114bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK; 4115bba2c361STejun Heo nr_enqueued++; 4116bba2c361STejun Heo } 4117bba2c361STejun Heo 4118bba2c361STejun Heo return nr_enqueued; 4119bba2c361STejun Heo } 4120bba2c361STejun Heo 4121bba2c361STejun Heo static void process_deferred_reenq_locals(struct rq *rq) 4122bba2c361STejun Heo { 4123bba2c361STejun Heo u64 seq = ++rq->scx.deferred_reenq_locals_seq; 4124bba2c361STejun Heo 4125bba2c361STejun Heo lockdep_assert_rq_held(rq); 4126bba2c361STejun Heo 4127bba2c361STejun Heo while (true) { 4128bba2c361STejun Heo struct scx_sched *sch; 4129bba2c361STejun Heo u64 reenq_flags; 4130bba2c361STejun Heo bool skip = false; 4131bba2c361STejun Heo 4132bba2c361STejun Heo scoped_guard (raw_spinlock, &rq->scx.deferred_reenq_lock) { 4133bba2c361STejun Heo struct scx_deferred_reenq_local *drl = 4134bba2c361STejun Heo list_first_entry_or_null(&rq->scx.deferred_reenq_locals, 4135bba2c361STejun Heo struct scx_deferred_reenq_local, 4136bba2c361STejun Heo node); 4137bba2c361STejun Heo struct scx_sched_pcpu *sch_pcpu; 4138bba2c361STejun Heo 4139bba2c361STejun Heo if (!drl) 4140bba2c361STejun Heo return; 4141bba2c361STejun Heo 4142bba2c361STejun Heo sch_pcpu = container_of(drl, struct scx_sched_pcpu, 4143bba2c361STejun Heo deferred_reenq_local); 4144bba2c361STejun Heo sch = sch_pcpu->sch; 4145bba2c361STejun Heo 4146bba2c361STejun Heo reenq_flags = drl->flags; 4147bba2c361STejun Heo WRITE_ONCE(drl->flags, 0); 4148bba2c361STejun Heo list_del_init(&drl->node); 4149bba2c361STejun Heo 4150bba2c361STejun Heo if (likely(drl->seq != seq)) { 4151bba2c361STejun Heo drl->seq = seq; 4152bba2c361STejun Heo drl->cnt = 0; 4153bba2c361STejun Heo } else { 4154bba2c361STejun Heo if (unlikely(++drl->cnt > SCX_REENQ_LOCAL_MAX_REPEAT)) { 4155bba2c361STejun Heo scx_error(sch, "SCX_ENQ_REENQ on SCX_DSQ_LOCAL repeated %u times", 4156bba2c361STejun Heo drl->cnt); 4157bba2c361STejun Heo skip = true; 4158bba2c361STejun Heo } 4159bba2c361STejun Heo 4160bba2c361STejun Heo __scx_add_event(sch, SCX_EV_REENQ_LOCAL_REPEAT, 1); 4161bba2c361STejun Heo } 4162bba2c361STejun Heo } 4163bba2c361STejun Heo 4164bba2c361STejun Heo if (!skip) { 4165bba2c361STejun Heo /* see schedule_dsq_reenq() */ 4166bba2c361STejun Heo smp_mb(); 4167bba2c361STejun Heo 4168bba2c361STejun Heo reenq_local(sch, rq, reenq_flags); 4169bba2c361STejun Heo } 4170bba2c361STejun Heo } 4171bba2c361STejun Heo } 4172bba2c361STejun Heo 4173bba2c361STejun Heo static bool user_task_should_reenq(struct task_struct *p, u64 reenq_flags, u32 *reason) 4174bba2c361STejun Heo { 4175bba2c361STejun Heo *reason = SCX_TASK_REENQ_KFUNC; 4176bba2c361STejun Heo return reenq_flags & SCX_REENQ_ANY; 4177bba2c361STejun Heo } 4178bba2c361STejun Heo 4179bba2c361STejun Heo static void reenq_user(struct rq *rq, struct scx_dispatch_q *dsq, u64 reenq_flags) 4180bba2c361STejun Heo { 4181bba2c361STejun Heo struct rq *locked_rq = rq; 4182bba2c361STejun Heo struct scx_sched *sch = dsq->sched; 4183bba2c361STejun Heo struct scx_dsq_list_node cursor = INIT_DSQ_LIST_CURSOR(cursor, dsq, 0); 4184bba2c361STejun Heo struct task_struct *p; 4185bba2c361STejun Heo s32 nr_enqueued = 0; 4186bba2c361STejun Heo 4187bba2c361STejun Heo lockdep_assert_rq_held(rq); 4188bba2c361STejun Heo 4189bba2c361STejun Heo raw_spin_lock(&dsq->lock); 4190bba2c361STejun Heo 4191bba2c361STejun Heo while (likely(!READ_ONCE(sch->bypass_depth))) { 4192bba2c361STejun Heo struct rq *task_rq; 4193bba2c361STejun Heo u32 reason; 4194bba2c361STejun Heo 4195bba2c361STejun Heo p = nldsq_cursor_next_task(&cursor, dsq); 4196bba2c361STejun Heo if (!p) 4197bba2c361STejun Heo break; 4198bba2c361STejun Heo 4199bba2c361STejun Heo if (!user_task_should_reenq(p, reenq_flags, &reason)) 4200bba2c361STejun Heo continue; 4201bba2c361STejun Heo 4202bba2c361STejun Heo task_rq = task_rq(p); 4203bba2c361STejun Heo 4204bba2c361STejun Heo if (locked_rq != task_rq) { 4205bba2c361STejun Heo if (locked_rq) 4206bba2c361STejun Heo raw_spin_rq_unlock(locked_rq); 4207bba2c361STejun Heo if (unlikely(!raw_spin_rq_trylock(task_rq))) { 4208bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 4209bba2c361STejun Heo raw_spin_rq_lock(task_rq); 4210bba2c361STejun Heo raw_spin_lock(&dsq->lock); 4211bba2c361STejun Heo } 4212bba2c361STejun Heo locked_rq = task_rq; 4213bba2c361STejun Heo 4214bba2c361STejun Heo /* did we lose @p while switching locks? */ 4215bba2c361STejun Heo if (nldsq_cursor_lost_task(&cursor, task_rq, dsq, p)) 4216bba2c361STejun Heo continue; 4217bba2c361STejun Heo } 4218bba2c361STejun Heo 4219bba2c361STejun Heo /* @p is on @dsq, its rq and @dsq are locked */ 4220bba2c361STejun Heo dispatch_dequeue_locked(p, dsq); 4221bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 4222bba2c361STejun Heo 4223bba2c361STejun Heo if (WARN_ON_ONCE(p->scx.flags & SCX_TASK_REENQ_REASON_MASK)) 4224bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK; 4225bba2c361STejun Heo p->scx.flags |= reason; 4226bba2c361STejun Heo 4227bba2c361STejun Heo do_enqueue_task(task_rq, p, SCX_ENQ_REENQ, -1); 4228bba2c361STejun Heo 4229bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK; 4230bba2c361STejun Heo 4231bba2c361STejun Heo if (!(++nr_enqueued % SCX_TASK_ITER_BATCH)) { 4232bba2c361STejun Heo raw_spin_rq_unlock(locked_rq); 4233bba2c361STejun Heo locked_rq = NULL; 4234bba2c361STejun Heo cpu_relax(); 4235bba2c361STejun Heo } 4236bba2c361STejun Heo 4237bba2c361STejun Heo raw_spin_lock(&dsq->lock); 4238bba2c361STejun Heo } 4239bba2c361STejun Heo 4240bba2c361STejun Heo list_del_init(&cursor.node); 4241bba2c361STejun Heo raw_spin_unlock(&dsq->lock); 4242bba2c361STejun Heo 4243bba2c361STejun Heo if (locked_rq != rq) { 4244bba2c361STejun Heo if (locked_rq) 4245bba2c361STejun Heo raw_spin_rq_unlock(locked_rq); 4246bba2c361STejun Heo raw_spin_rq_lock(rq); 4247bba2c361STejun Heo } 4248bba2c361STejun Heo } 4249bba2c361STejun Heo 4250bba2c361STejun Heo static void process_deferred_reenq_users(struct rq *rq) 4251bba2c361STejun Heo { 4252bba2c361STejun Heo lockdep_assert_rq_held(rq); 4253bba2c361STejun Heo 4254bba2c361STejun Heo while (true) { 4255bba2c361STejun Heo struct scx_dispatch_q *dsq; 4256bba2c361STejun Heo u64 reenq_flags; 4257bba2c361STejun Heo 4258bba2c361STejun Heo scoped_guard (raw_spinlock, &rq->scx.deferred_reenq_lock) { 4259bba2c361STejun Heo struct scx_deferred_reenq_user *dru = 4260bba2c361STejun Heo list_first_entry_or_null(&rq->scx.deferred_reenq_users, 4261bba2c361STejun Heo struct scx_deferred_reenq_user, 4262bba2c361STejun Heo node); 4263bba2c361STejun Heo struct scx_dsq_pcpu *dsq_pcpu; 4264bba2c361STejun Heo 4265bba2c361STejun Heo if (!dru) 4266bba2c361STejun Heo return; 4267bba2c361STejun Heo 4268bba2c361STejun Heo dsq_pcpu = container_of(dru, struct scx_dsq_pcpu, 4269bba2c361STejun Heo deferred_reenq_user); 4270bba2c361STejun Heo dsq = dsq_pcpu->dsq; 4271bba2c361STejun Heo reenq_flags = dru->flags; 4272bba2c361STejun Heo WRITE_ONCE(dru->flags, 0); 4273bba2c361STejun Heo list_del_init(&dru->node); 4274bba2c361STejun Heo } 4275bba2c361STejun Heo 4276bba2c361STejun Heo /* see schedule_dsq_reenq() */ 4277bba2c361STejun Heo smp_mb(); 4278bba2c361STejun Heo 4279bba2c361STejun Heo BUG_ON(dsq->id & SCX_DSQ_FLAG_BUILTIN); 4280bba2c361STejun Heo reenq_user(rq, dsq, reenq_flags); 4281bba2c361STejun Heo } 4282bba2c361STejun Heo } 4283bba2c361STejun Heo 4284bba2c361STejun Heo static void run_deferred(struct rq *rq) 4285bba2c361STejun Heo { 4286bba2c361STejun Heo process_ddsp_deferred_locals(rq); 4287bba2c361STejun Heo 4288bba2c361STejun Heo if (!list_empty(&rq->scx.deferred_reenq_locals)) 4289bba2c361STejun Heo process_deferred_reenq_locals(rq); 4290bba2c361STejun Heo 4291bba2c361STejun Heo if (!list_empty(&rq->scx.deferred_reenq_users)) 4292bba2c361STejun Heo process_deferred_reenq_users(rq); 4293bba2c361STejun Heo } 4294bba2c361STejun Heo 4295bba2c361STejun Heo #ifdef CONFIG_NO_HZ_FULL 4296bba2c361STejun Heo bool scx_can_stop_tick(struct rq *rq) 4297bba2c361STejun Heo { 4298bba2c361STejun Heo struct task_struct *p = rq->curr; 4299bba2c361STejun Heo struct scx_sched *sch = scx_task_sched(p); 4300bba2c361STejun Heo 4301bba2c361STejun Heo if (p->sched_class != &ext_sched_class) 4302bba2c361STejun Heo return true; 4303bba2c361STejun Heo 4304bba2c361STejun Heo if (scx_bypassing(sch, cpu_of(rq))) 4305bba2c361STejun Heo return false; 4306bba2c361STejun Heo 4307bba2c361STejun Heo /* 4308bba2c361STejun Heo * @rq can dispatch from different DSQs, so we can't tell whether it 4309bba2c361STejun Heo * needs the tick or not by looking at nr_running. Allow stopping ticks 4310bba2c361STejun Heo * iff the BPF scheduler indicated so. See set_next_task_scx(). 4311bba2c361STejun Heo */ 4312bba2c361STejun Heo return rq->scx.flags & SCX_RQ_CAN_STOP_TICK; 4313bba2c361STejun Heo } 4314bba2c361STejun Heo #endif 4315bba2c361STejun Heo 4316bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 4317bba2c361STejun Heo 4318bba2c361STejun Heo DEFINE_STATIC_PERCPU_RWSEM(scx_cgroup_ops_rwsem); 4319bba2c361STejun Heo static bool scx_cgroup_enabled; 4320bba2c361STejun Heo 4321bba2c361STejun Heo void scx_tg_init(struct task_group *tg) 4322bba2c361STejun Heo { 4323bba2c361STejun Heo tg->scx.weight = CGROUP_WEIGHT_DFL; 4324bba2c361STejun Heo tg->scx.bw_period_us = default_bw_period_us(); 4325bba2c361STejun Heo tg->scx.bw_quota_us = RUNTIME_INF; 4326bba2c361STejun Heo tg->scx.idle = false; 4327bba2c361STejun Heo } 4328bba2c361STejun Heo 4329bba2c361STejun Heo int scx_tg_online(struct task_group *tg) 4330bba2c361STejun Heo { 4331bba2c361STejun Heo struct scx_sched *sch = scx_root; 4332bba2c361STejun Heo int ret = 0; 4333bba2c361STejun Heo 4334bba2c361STejun Heo WARN_ON_ONCE(tg->scx.flags & (SCX_TG_ONLINE | SCX_TG_INITED)); 4335bba2c361STejun Heo 4336bba2c361STejun Heo if (scx_cgroup_enabled) { 4337bba2c361STejun Heo if (SCX_HAS_OP(sch, cgroup_init)) { 4338bba2c361STejun Heo struct scx_cgroup_init_args args = 4339bba2c361STejun Heo { .weight = tg->scx.weight, 4340bba2c361STejun Heo .bw_period_us = tg->scx.bw_period_us, 4341bba2c361STejun Heo .bw_quota_us = tg->scx.bw_quota_us, 4342bba2c361STejun Heo .bw_burst_us = tg->scx.bw_burst_us }; 4343bba2c361STejun Heo 4344bba2c361STejun Heo ret = SCX_CALL_OP_RET(sch, cgroup_init, 4345bba2c361STejun Heo NULL, tg->css.cgroup, &args); 4346bba2c361STejun Heo if (ret) 4347bba2c361STejun Heo ret = ops_sanitize_err(sch, "cgroup_init", ret); 4348bba2c361STejun Heo } 4349bba2c361STejun Heo if (ret == 0) 4350bba2c361STejun Heo tg->scx.flags |= SCX_TG_ONLINE | SCX_TG_INITED; 4351bba2c361STejun Heo } else { 4352bba2c361STejun Heo tg->scx.flags |= SCX_TG_ONLINE; 4353bba2c361STejun Heo } 4354bba2c361STejun Heo 4355bba2c361STejun Heo return ret; 4356bba2c361STejun Heo } 4357bba2c361STejun Heo 4358bba2c361STejun Heo void scx_tg_offline(struct task_group *tg) 4359bba2c361STejun Heo { 4360bba2c361STejun Heo struct scx_sched *sch = scx_root; 4361bba2c361STejun Heo 4362bba2c361STejun Heo WARN_ON_ONCE(!(tg->scx.flags & SCX_TG_ONLINE)); 4363bba2c361STejun Heo 4364bba2c361STejun Heo if (scx_cgroup_enabled && SCX_HAS_OP(sch, cgroup_exit) && 4365bba2c361STejun Heo (tg->scx.flags & SCX_TG_INITED)) 4366bba2c361STejun Heo SCX_CALL_OP(sch, cgroup_exit, NULL, tg->css.cgroup); 4367bba2c361STejun Heo tg->scx.flags &= ~(SCX_TG_ONLINE | SCX_TG_INITED); 4368bba2c361STejun Heo } 4369bba2c361STejun Heo 4370bba2c361STejun Heo int scx_cgroup_can_attach(struct cgroup_taskset *tset) 4371bba2c361STejun Heo { 4372bba2c361STejun Heo struct scx_sched *sch = scx_root; 4373bba2c361STejun Heo struct cgroup_subsys_state *css; 4374bba2c361STejun Heo struct task_struct *p; 4375bba2c361STejun Heo int ret; 4376bba2c361STejun Heo 4377bba2c361STejun Heo if (!scx_cgroup_enabled) 4378bba2c361STejun Heo return 0; 4379bba2c361STejun Heo 4380bba2c361STejun Heo cgroup_taskset_for_each(p, css, tset) { 4381bba2c361STejun Heo struct cgroup *from = tg_cgrp(task_group(p)); 4382bba2c361STejun Heo struct cgroup *to = tg_cgrp(css_tg(css)); 4383bba2c361STejun Heo 4384bba2c361STejun Heo WARN_ON_ONCE(p->scx.cgrp_moving_from); 4385bba2c361STejun Heo 4386bba2c361STejun Heo /* 4387bba2c361STejun Heo * sched_move_task() omits identity migrations. Let's match the 4388bba2c361STejun Heo * behavior so that ops.cgroup_prep_move() and ops.cgroup_move() 4389bba2c361STejun Heo * always match one-to-one. 4390bba2c361STejun Heo */ 4391bba2c361STejun Heo if (from == to) 4392bba2c361STejun Heo continue; 4393bba2c361STejun Heo 4394bba2c361STejun Heo if (SCX_HAS_OP(sch, cgroup_prep_move)) { 4395bba2c361STejun Heo ret = SCX_CALL_OP_RET(sch, cgroup_prep_move, NULL, 4396bba2c361STejun Heo p, from, css->cgroup); 4397bba2c361STejun Heo if (ret) 4398bba2c361STejun Heo goto err; 4399bba2c361STejun Heo } 4400bba2c361STejun Heo 4401bba2c361STejun Heo p->scx.cgrp_moving_from = from; 4402bba2c361STejun Heo } 4403bba2c361STejun Heo 4404bba2c361STejun Heo return 0; 4405bba2c361STejun Heo 4406bba2c361STejun Heo err: 4407bba2c361STejun Heo cgroup_taskset_for_each(p, css, tset) { 4408bba2c361STejun Heo if (SCX_HAS_OP(sch, cgroup_cancel_move) && 4409bba2c361STejun Heo p->scx.cgrp_moving_from) 4410bba2c361STejun Heo SCX_CALL_OP(sch, cgroup_cancel_move, NULL, 4411bba2c361STejun Heo p, p->scx.cgrp_moving_from, css->cgroup); 4412bba2c361STejun Heo p->scx.cgrp_moving_from = NULL; 4413bba2c361STejun Heo } 4414bba2c361STejun Heo 4415bba2c361STejun Heo return ops_sanitize_err(sch, "cgroup_prep_move", ret); 4416bba2c361STejun Heo } 4417bba2c361STejun Heo 4418bba2c361STejun Heo void scx_cgroup_move_task(struct task_struct *p) 4419bba2c361STejun Heo { 4420bba2c361STejun Heo struct scx_sched *sch = scx_root; 4421bba2c361STejun Heo 4422bba2c361STejun Heo if (!scx_cgroup_enabled) 4423bba2c361STejun Heo return; 4424bba2c361STejun Heo 4425bba2c361STejun Heo /* 4426bba2c361STejun Heo * scx_cgroup_can_attach() sets cgrp_moving_from only when the task's 4427bba2c361STejun Heo * cgroup changes. Migration keys off css rather than cgroup identity, 4428bba2c361STejun Heo * so it can hand an unchanged-cgroup task here with cgrp_moving_from 4429bba2c361STejun Heo * NULL. Nothing to report to the BPF scheduler then, so skip it and 4430bba2c361STejun Heo * keep prep_move and move paired. 4431bba2c361STejun Heo */ 4432bba2c361STejun Heo if (SCX_HAS_OP(sch, cgroup_move) && p->scx.cgrp_moving_from) 4433bba2c361STejun Heo SCX_CALL_OP_TASK(sch, cgroup_move, task_rq(p), 4434bba2c361STejun Heo p, p->scx.cgrp_moving_from, 4435bba2c361STejun Heo tg_cgrp(task_group(p))); 4436bba2c361STejun Heo p->scx.cgrp_moving_from = NULL; 4437bba2c361STejun Heo } 4438bba2c361STejun Heo 4439bba2c361STejun Heo void scx_cgroup_cancel_attach(struct cgroup_taskset *tset) 4440bba2c361STejun Heo { 4441bba2c361STejun Heo struct scx_sched *sch = scx_root; 4442bba2c361STejun Heo struct cgroup_subsys_state *css; 4443bba2c361STejun Heo struct task_struct *p; 4444bba2c361STejun Heo 4445bba2c361STejun Heo if (!scx_cgroup_enabled) 4446bba2c361STejun Heo return; 4447bba2c361STejun Heo 4448bba2c361STejun Heo cgroup_taskset_for_each(p, css, tset) { 4449bba2c361STejun Heo if (SCX_HAS_OP(sch, cgroup_cancel_move) && 4450bba2c361STejun Heo p->scx.cgrp_moving_from) 4451bba2c361STejun Heo SCX_CALL_OP(sch, cgroup_cancel_move, NULL, 4452bba2c361STejun Heo p, p->scx.cgrp_moving_from, css->cgroup); 4453bba2c361STejun Heo p->scx.cgrp_moving_from = NULL; 4454bba2c361STejun Heo } 4455bba2c361STejun Heo } 4456bba2c361STejun Heo 4457bba2c361STejun Heo void scx_group_set_weight(struct task_group *tg, unsigned long weight) 4458bba2c361STejun Heo { 4459bba2c361STejun Heo struct scx_sched *sch; 4460bba2c361STejun Heo 4461bba2c361STejun Heo percpu_down_read(&scx_cgroup_ops_rwsem); 4462bba2c361STejun Heo sch = scx_root; 4463bba2c361STejun Heo 4464bba2c361STejun Heo if (scx_cgroup_enabled && SCX_HAS_OP(sch, cgroup_set_weight) && 4465bba2c361STejun Heo tg->scx.weight != weight) 4466bba2c361STejun Heo SCX_CALL_OP(sch, cgroup_set_weight, NULL, tg_cgrp(tg), weight); 4467bba2c361STejun Heo 4468bba2c361STejun Heo tg->scx.weight = weight; 4469bba2c361STejun Heo 4470bba2c361STejun Heo percpu_up_read(&scx_cgroup_ops_rwsem); 4471bba2c361STejun Heo } 4472bba2c361STejun Heo 4473bba2c361STejun Heo void scx_group_set_idle(struct task_group *tg, bool idle) 4474bba2c361STejun Heo { 4475bba2c361STejun Heo struct scx_sched *sch; 4476bba2c361STejun Heo 4477bba2c361STejun Heo percpu_down_read(&scx_cgroup_ops_rwsem); 4478bba2c361STejun Heo sch = scx_root; 4479bba2c361STejun Heo 4480bba2c361STejun Heo if (scx_cgroup_enabled && SCX_HAS_OP(sch, cgroup_set_idle)) 4481bba2c361STejun Heo SCX_CALL_OP(sch, cgroup_set_idle, NULL, tg_cgrp(tg), idle); 4482bba2c361STejun Heo 4483bba2c361STejun Heo /* Update the task group's idle state */ 4484bba2c361STejun Heo tg->scx.idle = idle; 4485bba2c361STejun Heo 4486bba2c361STejun Heo percpu_up_read(&scx_cgroup_ops_rwsem); 4487bba2c361STejun Heo } 4488bba2c361STejun Heo 4489bba2c361STejun Heo void scx_group_set_bandwidth(struct task_group *tg, 4490bba2c361STejun Heo u64 period_us, u64 quota_us, u64 burst_us) 4491bba2c361STejun Heo { 4492bba2c361STejun Heo struct scx_sched *sch; 4493bba2c361STejun Heo 4494bba2c361STejun Heo percpu_down_read(&scx_cgroup_ops_rwsem); 4495bba2c361STejun Heo sch = scx_root; 4496bba2c361STejun Heo 4497bba2c361STejun Heo if (scx_cgroup_enabled && SCX_HAS_OP(sch, cgroup_set_bandwidth) && 4498bba2c361STejun Heo (tg->scx.bw_period_us != period_us || 4499bba2c361STejun Heo tg->scx.bw_quota_us != quota_us || 4500bba2c361STejun Heo tg->scx.bw_burst_us != burst_us)) 4501bba2c361STejun Heo SCX_CALL_OP(sch, cgroup_set_bandwidth, NULL, 4502bba2c361STejun Heo tg_cgrp(tg), period_us, quota_us, burst_us); 4503bba2c361STejun Heo 4504bba2c361STejun Heo tg->scx.bw_period_us = period_us; 4505bba2c361STejun Heo tg->scx.bw_quota_us = quota_us; 4506bba2c361STejun Heo tg->scx.bw_burst_us = burst_us; 4507bba2c361STejun Heo 4508bba2c361STejun Heo percpu_up_read(&scx_cgroup_ops_rwsem); 4509bba2c361STejun Heo } 4510bba2c361STejun Heo #endif /* CONFIG_EXT_GROUP_SCHED */ 4511bba2c361STejun Heo 4512bba2c361STejun Heo #if defined(CONFIG_EXT_GROUP_SCHED) || defined(CONFIG_EXT_SUB_SCHED) 4513bba2c361STejun Heo static struct cgroup *root_cgroup(void) 4514bba2c361STejun Heo { 4515bba2c361STejun Heo return &cgrp_dfl_root.cgrp; 4516bba2c361STejun Heo } 4517bba2c361STejun Heo 4518bba2c361STejun Heo static void scx_cgroup_lock(void) 4519bba2c361STejun Heo { 4520bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 4521bba2c361STejun Heo percpu_down_write(&scx_cgroup_ops_rwsem); 4522bba2c361STejun Heo #endif 4523bba2c361STejun Heo cgroup_lock(); 4524bba2c361STejun Heo } 4525bba2c361STejun Heo 4526bba2c361STejun Heo static void scx_cgroup_unlock(void) 4527bba2c361STejun Heo { 4528bba2c361STejun Heo cgroup_unlock(); 4529bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 4530bba2c361STejun Heo percpu_up_write(&scx_cgroup_ops_rwsem); 4531bba2c361STejun Heo #endif 4532bba2c361STejun Heo } 4533bba2c361STejun Heo #else /* CONFIG_EXT_GROUP_SCHED || CONFIG_EXT_SUB_SCHED */ 4534bba2c361STejun Heo static inline struct cgroup *root_cgroup(void) { return NULL; } 4535bba2c361STejun Heo static inline void scx_cgroup_lock(void) {} 4536bba2c361STejun Heo static inline void scx_cgroup_unlock(void) {} 4537bba2c361STejun Heo #endif /* CONFIG_EXT_GROUP_SCHED || CONFIG_EXT_SUB_SCHED */ 4538bba2c361STejun Heo 4539bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 4540bba2c361STejun Heo static struct cgroup *sch_cgroup(struct scx_sched *sch) 4541bba2c361STejun Heo { 4542bba2c361STejun Heo return sch->cgrp; 4543bba2c361STejun Heo } 4544bba2c361STejun Heo 4545bba2c361STejun Heo /* for each descendant of @cgrp including self, set ->scx_sched to @sch */ 4546bba2c361STejun Heo static void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) 4547bba2c361STejun Heo { 4548bba2c361STejun Heo struct cgroup *pos; 4549bba2c361STejun Heo struct cgroup_subsys_state *css; 4550bba2c361STejun Heo 4551bba2c361STejun Heo cgroup_for_each_live_descendant_pre(pos, css, cgrp) 4552bba2c361STejun Heo rcu_assign_pointer(pos->scx_sched, sch); 4553bba2c361STejun Heo } 4554bba2c361STejun Heo #else /* CONFIG_EXT_SUB_SCHED */ 4555bba2c361STejun Heo static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; } 4556bba2c361STejun Heo static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {} 4557bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 4558bba2c361STejun Heo 4559bba2c361STejun Heo /* 4560bba2c361STejun Heo * Omitted operations: 4561bba2c361STejun Heo * 4562bba2c361STejun Heo * - migrate_task_rq: Unnecessary as task to cpu mapping is transient. 4563bba2c361STejun Heo * 4564bba2c361STejun Heo * - task_fork/dead: We need fork/dead notifications for all tasks regardless of 4565bba2c361STejun Heo * their current sched_class. Call them directly from sched core instead. 4566bba2c361STejun Heo */ 4567bba2c361STejun Heo DEFINE_SCHED_CLASS(ext) = { 4568bba2c361STejun Heo .enqueue_task = enqueue_task_scx, 4569bba2c361STejun Heo .dequeue_task = dequeue_task_scx, 4570bba2c361STejun Heo .yield_task = yield_task_scx, 4571bba2c361STejun Heo .yield_to_task = yield_to_task_scx, 4572bba2c361STejun Heo 4573bba2c361STejun Heo .wakeup_preempt = wakeup_preempt_scx, 4574bba2c361STejun Heo 4575bba2c361STejun Heo .pick_task = pick_task_scx, 4576bba2c361STejun Heo 4577bba2c361STejun Heo .put_prev_task = put_prev_task_scx, 4578bba2c361STejun Heo .set_next_task = set_next_task_scx, 4579bba2c361STejun Heo 4580bba2c361STejun Heo .select_task_rq = select_task_rq_scx, 4581bba2c361STejun Heo .task_woken = task_woken_scx, 4582bba2c361STejun Heo .set_cpus_allowed = set_cpus_allowed_scx, 4583bba2c361STejun Heo 4584bba2c361STejun Heo .rq_online = rq_online_scx, 4585bba2c361STejun Heo .rq_offline = rq_offline_scx, 4586bba2c361STejun Heo 4587bba2c361STejun Heo .task_tick = task_tick_scx, 4588bba2c361STejun Heo 4589bba2c361STejun Heo .switching_to = switching_to_scx, 4590bba2c361STejun Heo .switched_from = switched_from_scx, 4591bba2c361STejun Heo .switched_to = switched_to_scx, 4592bba2c361STejun Heo .reweight_task = reweight_task_scx, 4593bba2c361STejun Heo .prio_changed = prio_changed_scx, 4594bba2c361STejun Heo 4595bba2c361STejun Heo .update_curr = update_curr_scx, 4596bba2c361STejun Heo 4597bba2c361STejun Heo #ifdef CONFIG_UCLAMP_TASK 4598bba2c361STejun Heo .uclamp_enabled = 1, 4599bba2c361STejun Heo #endif 4600bba2c361STejun Heo }; 4601bba2c361STejun Heo 4602bba2c361STejun Heo static s32 init_dsq(struct scx_dispatch_q *dsq, u64 dsq_id, 4603bba2c361STejun Heo struct scx_sched *sch) 4604bba2c361STejun Heo { 4605bba2c361STejun Heo s32 cpu; 4606bba2c361STejun Heo 4607bba2c361STejun Heo memset(dsq, 0, sizeof(*dsq)); 4608bba2c361STejun Heo 4609bba2c361STejun Heo raw_spin_lock_init(&dsq->lock); 4610bba2c361STejun Heo INIT_LIST_HEAD(&dsq->list); 4611bba2c361STejun Heo dsq->id = dsq_id; 4612bba2c361STejun Heo dsq->sched = sch; 4613bba2c361STejun Heo 4614bba2c361STejun Heo dsq->pcpu = alloc_percpu(struct scx_dsq_pcpu); 4615bba2c361STejun Heo if (!dsq->pcpu) 4616bba2c361STejun Heo return -ENOMEM; 4617bba2c361STejun Heo 4618bba2c361STejun Heo for_each_possible_cpu(cpu) { 4619bba2c361STejun Heo struct scx_dsq_pcpu *pcpu = per_cpu_ptr(dsq->pcpu, cpu); 4620bba2c361STejun Heo 4621bba2c361STejun Heo pcpu->dsq = dsq; 4622bba2c361STejun Heo INIT_LIST_HEAD(&pcpu->deferred_reenq_user.node); 4623bba2c361STejun Heo } 4624bba2c361STejun Heo 4625bba2c361STejun Heo return 0; 4626bba2c361STejun Heo } 4627bba2c361STejun Heo 4628bba2c361STejun Heo static void exit_dsq(struct scx_dispatch_q *dsq) 4629bba2c361STejun Heo { 4630bba2c361STejun Heo s32 cpu; 4631bba2c361STejun Heo 4632bba2c361STejun Heo for_each_possible_cpu(cpu) { 4633bba2c361STejun Heo struct scx_dsq_pcpu *pcpu = per_cpu_ptr(dsq->pcpu, cpu); 4634bba2c361STejun Heo struct scx_deferred_reenq_user *dru = &pcpu->deferred_reenq_user; 4635bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 4636bba2c361STejun Heo 4637bba2c361STejun Heo /* 4638bba2c361STejun Heo * There must have been a RCU grace period since the last 4639bba2c361STejun Heo * insertion and @dsq should be off the deferred list by now. 4640bba2c361STejun Heo */ 4641bba2c361STejun Heo if (WARN_ON_ONCE(!list_empty(&dru->node))) { 4642bba2c361STejun Heo guard(raw_spinlock_irqsave)(&rq->scx.deferred_reenq_lock); 4643bba2c361STejun Heo list_del_init(&dru->node); 4644bba2c361STejun Heo } 4645bba2c361STejun Heo } 4646bba2c361STejun Heo 4647bba2c361STejun Heo free_percpu(dsq->pcpu); 4648bba2c361STejun Heo } 4649bba2c361STejun Heo 4650bba2c361STejun Heo static void free_dsq_rcufn(struct rcu_head *rcu) 4651bba2c361STejun Heo { 4652bba2c361STejun Heo struct scx_dispatch_q *dsq = container_of(rcu, struct scx_dispatch_q, rcu); 4653bba2c361STejun Heo 4654bba2c361STejun Heo exit_dsq(dsq); 4655bba2c361STejun Heo kfree(dsq); 4656bba2c361STejun Heo } 4657bba2c361STejun Heo 4658bba2c361STejun Heo static void free_dsq_irq_workfn(struct irq_work *irq_work) 4659bba2c361STejun Heo { 4660bba2c361STejun Heo struct llist_node *to_free = llist_del_all(&dsqs_to_free); 4661bba2c361STejun Heo struct scx_dispatch_q *dsq, *tmp_dsq; 4662bba2c361STejun Heo 4663bba2c361STejun Heo llist_for_each_entry_safe(dsq, tmp_dsq, to_free, free_node) 4664bba2c361STejun Heo call_rcu(&dsq->rcu, free_dsq_rcufn); 4665bba2c361STejun Heo } 4666bba2c361STejun Heo 4667bba2c361STejun Heo static DEFINE_IRQ_WORK(free_dsq_irq_work, free_dsq_irq_workfn); 4668bba2c361STejun Heo 4669bba2c361STejun Heo static void destroy_dsq(struct scx_sched *sch, u64 dsq_id) 4670bba2c361STejun Heo { 4671bba2c361STejun Heo struct scx_dispatch_q *dsq; 4672bba2c361STejun Heo unsigned long flags; 4673bba2c361STejun Heo 4674bba2c361STejun Heo rcu_read_lock(); 4675bba2c361STejun Heo 4676bba2c361STejun Heo dsq = find_user_dsq(sch, dsq_id); 4677bba2c361STejun Heo if (!dsq) 4678bba2c361STejun Heo goto out_unlock_rcu; 4679bba2c361STejun Heo 4680bba2c361STejun Heo raw_spin_lock_irqsave(&dsq->lock, flags); 4681bba2c361STejun Heo 4682bba2c361STejun Heo if (dsq->nr) { 4683bba2c361STejun Heo scx_error(sch, "attempting to destroy in-use dsq 0x%016llx (nr=%u)", 4684bba2c361STejun Heo dsq->id, dsq->nr); 4685bba2c361STejun Heo goto out_unlock_dsq; 4686bba2c361STejun Heo } 4687bba2c361STejun Heo 4688bba2c361STejun Heo if (rhashtable_remove_fast(&sch->dsq_hash, &dsq->hash_node, 4689bba2c361STejun Heo dsq_hash_params)) 4690bba2c361STejun Heo goto out_unlock_dsq; 4691bba2c361STejun Heo 4692bba2c361STejun Heo /* 4693bba2c361STejun Heo * Mark dead by invalidating ->id to prevent dispatch_enqueue() from 4694bba2c361STejun Heo * queueing more tasks. As this function can be called from anywhere, 4695bba2c361STejun Heo * freeing is bounced through an irq work to avoid nesting RCU 4696bba2c361STejun Heo * operations inside scheduler locks. 4697bba2c361STejun Heo */ 4698bba2c361STejun Heo dsq->id = SCX_DSQ_INVALID; 4699bba2c361STejun Heo if (llist_add(&dsq->free_node, &dsqs_to_free)) 4700bba2c361STejun Heo irq_work_queue(&free_dsq_irq_work); 4701bba2c361STejun Heo 4702bba2c361STejun Heo out_unlock_dsq: 4703bba2c361STejun Heo raw_spin_unlock_irqrestore(&dsq->lock, flags); 4704bba2c361STejun Heo out_unlock_rcu: 4705bba2c361STejun Heo rcu_read_unlock(); 4706bba2c361STejun Heo } 4707bba2c361STejun Heo 4708bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 4709bba2c361STejun Heo static void scx_cgroup_exit(struct scx_sched *sch) 4710bba2c361STejun Heo { 4711bba2c361STejun Heo struct cgroup_subsys_state *css; 4712bba2c361STejun Heo 4713bba2c361STejun Heo scx_cgroup_enabled = false; 4714bba2c361STejun Heo 4715bba2c361STejun Heo /* 4716bba2c361STejun Heo * scx_tg_on/offline() are excluded through cgroup_lock(). If we walk 4717bba2c361STejun Heo * cgroups and exit all the inited ones, all online cgroups are exited. 4718bba2c361STejun Heo */ 4719bba2c361STejun Heo css_for_each_descendant_post(css, &root_task_group.css) { 4720bba2c361STejun Heo struct task_group *tg = css_tg(css); 4721bba2c361STejun Heo 4722bba2c361STejun Heo if (!(tg->scx.flags & SCX_TG_INITED)) 4723bba2c361STejun Heo continue; 4724bba2c361STejun Heo tg->scx.flags &= ~SCX_TG_INITED; 4725bba2c361STejun Heo 4726bba2c361STejun Heo if (!sch->ops.cgroup_exit) 4727bba2c361STejun Heo continue; 4728bba2c361STejun Heo 4729bba2c361STejun Heo SCX_CALL_OP(sch, cgroup_exit, NULL, css->cgroup); 4730bba2c361STejun Heo } 4731bba2c361STejun Heo } 4732bba2c361STejun Heo 4733bba2c361STejun Heo static int scx_cgroup_init(struct scx_sched *sch) 4734bba2c361STejun Heo { 4735bba2c361STejun Heo struct cgroup_subsys_state *css; 4736bba2c361STejun Heo int ret; 4737bba2c361STejun Heo 4738bba2c361STejun Heo /* 4739bba2c361STejun Heo * scx_tg_on/offline() are excluded through cgroup_lock(). If we walk 4740bba2c361STejun Heo * cgroups and init, all online cgroups are initialized. 4741bba2c361STejun Heo */ 4742bba2c361STejun Heo css_for_each_descendant_pre(css, &root_task_group.css) { 4743bba2c361STejun Heo struct task_group *tg = css_tg(css); 4744bba2c361STejun Heo struct scx_cgroup_init_args args = { 4745bba2c361STejun Heo .weight = tg->scx.weight, 4746bba2c361STejun Heo .bw_period_us = tg->scx.bw_period_us, 4747bba2c361STejun Heo .bw_quota_us = tg->scx.bw_quota_us, 4748bba2c361STejun Heo .bw_burst_us = tg->scx.bw_burst_us, 4749bba2c361STejun Heo }; 4750bba2c361STejun Heo 4751bba2c361STejun Heo if ((tg->scx.flags & 4752bba2c361STejun Heo (SCX_TG_ONLINE | SCX_TG_INITED)) != SCX_TG_ONLINE) 4753bba2c361STejun Heo continue; 4754bba2c361STejun Heo 4755bba2c361STejun Heo if (!sch->ops.cgroup_init) { 4756bba2c361STejun Heo tg->scx.flags |= SCX_TG_INITED; 4757bba2c361STejun Heo continue; 4758bba2c361STejun Heo } 4759bba2c361STejun Heo 4760bba2c361STejun Heo ret = SCX_CALL_OP_RET(sch, cgroup_init, NULL, 4761bba2c361STejun Heo css->cgroup, &args); 4762bba2c361STejun Heo if (ret) { 4763bba2c361STejun Heo scx_error(sch, "ops.cgroup_init() failed (%d)", ret); 4764bba2c361STejun Heo return ret; 4765bba2c361STejun Heo } 4766bba2c361STejun Heo tg->scx.flags |= SCX_TG_INITED; 4767bba2c361STejun Heo } 4768bba2c361STejun Heo 4769bba2c361STejun Heo WARN_ON_ONCE(scx_cgroup_enabled); 4770bba2c361STejun Heo scx_cgroup_enabled = true; 4771bba2c361STejun Heo 4772bba2c361STejun Heo return 0; 4773bba2c361STejun Heo } 4774bba2c361STejun Heo 4775bba2c361STejun Heo #else 4776bba2c361STejun Heo static void scx_cgroup_exit(struct scx_sched *sch) {} 4777bba2c361STejun Heo static int scx_cgroup_init(struct scx_sched *sch) { return 0; } 4778bba2c361STejun Heo #endif 4779bba2c361STejun Heo 4780bba2c361STejun Heo 4781bba2c361STejun Heo /******************************************************************************** 4782bba2c361STejun Heo * Sysfs interface and ops enable/disable. 4783bba2c361STejun Heo */ 4784bba2c361STejun Heo 4785bba2c361STejun Heo #define SCX_ATTR(_name) \ 4786bba2c361STejun Heo static struct kobj_attribute scx_attr_##_name = { \ 4787bba2c361STejun Heo .attr = { .name = __stringify(_name), .mode = 0444 }, \ 4788bba2c361STejun Heo .show = scx_attr_##_name##_show, \ 4789bba2c361STejun Heo } 4790bba2c361STejun Heo 4791bba2c361STejun Heo static ssize_t scx_attr_state_show(struct kobject *kobj, 4792bba2c361STejun Heo struct kobj_attribute *ka, char *buf) 4793bba2c361STejun Heo { 4794bba2c361STejun Heo return sysfs_emit(buf, "%s\n", scx_enable_state_str[scx_enable_state()]); 4795bba2c361STejun Heo } 4796bba2c361STejun Heo SCX_ATTR(state); 4797bba2c361STejun Heo 4798bba2c361STejun Heo static ssize_t scx_attr_switch_all_show(struct kobject *kobj, 4799bba2c361STejun Heo struct kobj_attribute *ka, char *buf) 4800bba2c361STejun Heo { 4801bba2c361STejun Heo return sysfs_emit(buf, "%d\n", READ_ONCE(scx_switching_all)); 4802bba2c361STejun Heo } 4803bba2c361STejun Heo SCX_ATTR(switch_all); 4804bba2c361STejun Heo 4805bba2c361STejun Heo static ssize_t scx_attr_nr_rejected_show(struct kobject *kobj, 4806bba2c361STejun Heo struct kobj_attribute *ka, char *buf) 4807bba2c361STejun Heo { 4808bba2c361STejun Heo return sysfs_emit(buf, "%ld\n", atomic_long_read(&scx_nr_rejected)); 4809bba2c361STejun Heo } 4810bba2c361STejun Heo SCX_ATTR(nr_rejected); 4811bba2c361STejun Heo 4812bba2c361STejun Heo static ssize_t scx_attr_hotplug_seq_show(struct kobject *kobj, 4813bba2c361STejun Heo struct kobj_attribute *ka, char *buf) 4814bba2c361STejun Heo { 4815bba2c361STejun Heo return sysfs_emit(buf, "%ld\n", atomic_long_read(&scx_hotplug_seq)); 4816bba2c361STejun Heo } 4817bba2c361STejun Heo SCX_ATTR(hotplug_seq); 4818bba2c361STejun Heo 4819bba2c361STejun Heo static ssize_t scx_attr_enable_seq_show(struct kobject *kobj, 4820bba2c361STejun Heo struct kobj_attribute *ka, char *buf) 4821bba2c361STejun Heo { 4822bba2c361STejun Heo return sysfs_emit(buf, "%ld\n", atomic_long_read(&scx_enable_seq)); 4823bba2c361STejun Heo } 4824bba2c361STejun Heo SCX_ATTR(enable_seq); 4825bba2c361STejun Heo 4826bba2c361STejun Heo static struct attribute *scx_global_attrs[] = { 4827bba2c361STejun Heo &scx_attr_state.attr, 4828bba2c361STejun Heo &scx_attr_switch_all.attr, 4829bba2c361STejun Heo &scx_attr_nr_rejected.attr, 4830bba2c361STejun Heo &scx_attr_hotplug_seq.attr, 4831bba2c361STejun Heo &scx_attr_enable_seq.attr, 4832bba2c361STejun Heo NULL, 4833bba2c361STejun Heo }; 4834bba2c361STejun Heo 4835bba2c361STejun Heo static const struct attribute_group scx_global_attr_group = { 4836bba2c361STejun Heo .attrs = scx_global_attrs, 4837bba2c361STejun Heo }; 4838bba2c361STejun Heo 4839bba2c361STejun Heo static void free_pnode(struct scx_sched_pnode *pnode); 4840bba2c361STejun Heo static void free_exit_info(struct scx_exit_info *ei); 4841bba2c361STejun Heo 4842bba2c361STejun Heo static s32 scx_set_cmask_scratch_alloc(struct scx_sched *sch) 4843bba2c361STejun Heo { 4844bba2c361STejun Heo size_t size = struct_size_t(struct scx_cmask, bits, 4845bba2c361STejun Heo SCX_CMASK_NR_WORDS(num_possible_cpus())); 4846bba2c361STejun Heo int cpu; 4847bba2c361STejun Heo 4848bba2c361STejun Heo if (!sch->is_cid_type || !sch->arena_pool) 4849bba2c361STejun Heo return 0; 4850bba2c361STejun Heo 4851bba2c361STejun Heo sch->set_cmask_scratch = alloc_percpu(struct scx_cmask *); 4852bba2c361STejun Heo if (!sch->set_cmask_scratch) 4853bba2c361STejun Heo return -ENOMEM; 4854bba2c361STejun Heo 4855bba2c361STejun Heo for_each_possible_cpu(cpu) { 4856bba2c361STejun Heo struct scx_cmask **slot = per_cpu_ptr(sch->set_cmask_scratch, cpu); 4857bba2c361STejun Heo 4858bba2c361STejun Heo *slot = scx_arena_alloc(sch, size); 4859bba2c361STejun Heo if (!*slot) 4860bba2c361STejun Heo return -ENOMEM; 4861bba2c361STejun Heo scx_cmask_init(*slot, 0, num_possible_cpus()); 4862bba2c361STejun Heo } 4863bba2c361STejun Heo return 0; 4864bba2c361STejun Heo } 4865bba2c361STejun Heo 4866bba2c361STejun Heo static void scx_set_cmask_scratch_free(struct scx_sched *sch) 4867bba2c361STejun Heo { 4868bba2c361STejun Heo size_t size = struct_size_t(struct scx_cmask, bits, 4869bba2c361STejun Heo SCX_CMASK_NR_WORDS(num_possible_cpus())); 4870bba2c361STejun Heo int cpu; 4871bba2c361STejun Heo 4872bba2c361STejun Heo if (!sch->set_cmask_scratch) 4873bba2c361STejun Heo return; 4874bba2c361STejun Heo 4875bba2c361STejun Heo for_each_possible_cpu(cpu) { 4876bba2c361STejun Heo struct scx_cmask **slot = per_cpu_ptr(sch->set_cmask_scratch, cpu); 4877bba2c361STejun Heo 4878bba2c361STejun Heo scx_arena_free(sch, *slot, size); 4879bba2c361STejun Heo } 4880bba2c361STejun Heo free_percpu(sch->set_cmask_scratch); 4881bba2c361STejun Heo sch->set_cmask_scratch = NULL; 4882bba2c361STejun Heo } 4883bba2c361STejun Heo 4884bba2c361STejun Heo static void scx_sched_free_rcu_work(struct work_struct *work) 4885bba2c361STejun Heo { 4886bba2c361STejun Heo struct rcu_work *rcu_work = to_rcu_work(work); 4887bba2c361STejun Heo struct scx_sched *sch = container_of(rcu_work, struct scx_sched, rcu_work); 4888bba2c361STejun Heo struct rhashtable_iter rht_iter; 4889bba2c361STejun Heo struct scx_dispatch_q *dsq; 4890bba2c361STejun Heo int cpu, node; 4891bba2c361STejun Heo 4892bba2c361STejun Heo irq_work_sync(&sch->disable_irq_work); 4893bba2c361STejun Heo kthread_destroy_worker(sch->helper); 4894bba2c361STejun Heo timer_shutdown_sync(&sch->bypass_lb_timer); 4895bba2c361STejun Heo free_cpumask_var(sch->bypass_lb_donee_cpumask); 4896bba2c361STejun Heo free_cpumask_var(sch->bypass_lb_resched_cpumask); 4897bba2c361STejun Heo 4898bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 4899bba2c361STejun Heo kfree(sch->cgrp_path); 4900bba2c361STejun Heo if (sch_cgroup(sch)) 4901bba2c361STejun Heo cgroup_put(sch_cgroup(sch)); 4902bba2c361STejun Heo if (sch->sub_kset) 4903bba2c361STejun Heo kobject_put(&sch->sub_kset->kobj); 4904bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 4905bba2c361STejun Heo 4906bba2c361STejun Heo for_each_possible_cpu(cpu) { 4907bba2c361STejun Heo struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 4908bba2c361STejun Heo 4909bba2c361STejun Heo /* 4910bba2c361STejun Heo * $sch would have entered bypass mode before the RCU grace 4911bba2c361STejun Heo * period. As that blocks new deferrals, all 4912bba2c361STejun Heo * deferred_reenq_local_node's must be off-list by now. 4913bba2c361STejun Heo */ 4914bba2c361STejun Heo WARN_ON_ONCE(!list_empty(&pcpu->deferred_reenq_local.node)); 4915bba2c361STejun Heo 4916bba2c361STejun Heo exit_dsq(bypass_dsq(sch, cpu)); 4917bba2c361STejun Heo } 4918bba2c361STejun Heo 4919bba2c361STejun Heo free_percpu(sch->pcpu); 4920bba2c361STejun Heo 4921bba2c361STejun Heo for_each_node_state(node, N_POSSIBLE) 4922bba2c361STejun Heo free_pnode(sch->pnode[node]); 4923bba2c361STejun Heo kfree(sch->pnode); 4924bba2c361STejun Heo 4925bba2c361STejun Heo rhashtable_walk_enter(&sch->dsq_hash, &rht_iter); 4926bba2c361STejun Heo do { 4927bba2c361STejun Heo rhashtable_walk_start(&rht_iter); 4928bba2c361STejun Heo 4929bba2c361STejun Heo while (!IS_ERR_OR_NULL((dsq = rhashtable_walk_next(&rht_iter)))) 4930bba2c361STejun Heo destroy_dsq(sch, dsq->id); 4931bba2c361STejun Heo 4932bba2c361STejun Heo rhashtable_walk_stop(&rht_iter); 4933bba2c361STejun Heo } while (dsq == ERR_PTR(-EAGAIN)); 4934bba2c361STejun Heo rhashtable_walk_exit(&rht_iter); 4935bba2c361STejun Heo 4936bba2c361STejun Heo rhashtable_free_and_destroy(&sch->dsq_hash, NULL, NULL); 4937bba2c361STejun Heo free_exit_info(sch->exit_info); 4938bba2c361STejun Heo scx_set_cmask_scratch_free(sch); 4939bba2c361STejun Heo scx_arena_pool_destroy(sch); 4940bba2c361STejun Heo if (sch->arena_map) 4941bba2c361STejun Heo bpf_map_put(sch->arena_map); 4942bba2c361STejun Heo kfree(sch); 4943bba2c361STejun Heo } 4944bba2c361STejun Heo 4945bba2c361STejun Heo static void scx_kobj_release(struct kobject *kobj) 4946bba2c361STejun Heo { 4947bba2c361STejun Heo struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj); 4948bba2c361STejun Heo 4949bba2c361STejun Heo INIT_RCU_WORK(&sch->rcu_work, scx_sched_free_rcu_work); 4950bba2c361STejun Heo queue_rcu_work(system_dfl_wq, &sch->rcu_work); 4951bba2c361STejun Heo } 4952bba2c361STejun Heo 4953bba2c361STejun Heo static ssize_t scx_attr_ops_show(struct kobject *kobj, 4954bba2c361STejun Heo struct kobj_attribute *ka, char *buf) 4955bba2c361STejun Heo { 4956bba2c361STejun Heo struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj); 4957bba2c361STejun Heo 4958bba2c361STejun Heo return sysfs_emit(buf, "%s\n", sch->ops.name); 4959bba2c361STejun Heo } 4960bba2c361STejun Heo SCX_ATTR(ops); 4961bba2c361STejun Heo 4962bba2c361STejun Heo #define scx_attr_event_show(buf, at, events, kind) ({ \ 4963bba2c361STejun Heo sysfs_emit_at(buf, at, "%s %llu\n", #kind, (events)->kind); \ 4964bba2c361STejun Heo }) 4965bba2c361STejun Heo 4966bba2c361STejun Heo static ssize_t scx_attr_events_show(struct kobject *kobj, 4967bba2c361STejun Heo struct kobj_attribute *ka, char *buf) 4968bba2c361STejun Heo { 4969bba2c361STejun Heo struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj); 4970bba2c361STejun Heo struct scx_event_stats events; 4971bba2c361STejun Heo int at = 0; 4972bba2c361STejun Heo 4973bba2c361STejun Heo scx_read_events(sch, &events); 4974bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_SELECT_CPU_FALLBACK); 4975bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE); 4976bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_DISPATCH_KEEP_LAST); 4977bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_ENQ_SKIP_EXITING); 4978bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED); 4979bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_REENQ_IMMED); 4980bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_REENQ_LOCAL_REPEAT); 4981bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_REFILL_SLICE_DFL); 4982bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_BYPASS_DURATION); 4983bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_BYPASS_DISPATCH); 4984bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_BYPASS_ACTIVATE); 4985bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_INSERT_NOT_OWNED); 4986bba2c361STejun Heo at += scx_attr_event_show(buf, at, &events, SCX_EV_SUB_BYPASS_DISPATCH); 4987bba2c361STejun Heo return at; 4988bba2c361STejun Heo } 4989bba2c361STejun Heo SCX_ATTR(events); 4990bba2c361STejun Heo 4991bba2c361STejun Heo static struct attribute *scx_sched_attrs[] = { 4992bba2c361STejun Heo &scx_attr_ops.attr, 4993bba2c361STejun Heo &scx_attr_events.attr, 4994bba2c361STejun Heo NULL, 4995bba2c361STejun Heo }; 4996bba2c361STejun Heo ATTRIBUTE_GROUPS(scx_sched); 4997bba2c361STejun Heo 4998bba2c361STejun Heo static const struct kobj_type scx_ktype = { 4999bba2c361STejun Heo .release = scx_kobj_release, 5000bba2c361STejun Heo .sysfs_ops = &kobj_sysfs_ops, 5001bba2c361STejun Heo .default_groups = scx_sched_groups, 5002bba2c361STejun Heo }; 5003bba2c361STejun Heo 5004bba2c361STejun Heo static int scx_uevent(const struct kobject *kobj, struct kobj_uevent_env *env) 5005bba2c361STejun Heo { 5006bba2c361STejun Heo const struct scx_sched *sch; 5007bba2c361STejun Heo 5008bba2c361STejun Heo /* 5009bba2c361STejun Heo * scx_uevent() can be reached by both scx_sched kobjects (scx_ktype) 5010bba2c361STejun Heo * and sub-scheduler kset kobjects (kset_ktype) through the parent 5011bba2c361STejun Heo * chain walk. Filter out the latter to avoid invalid casts. 5012bba2c361STejun Heo */ 5013bba2c361STejun Heo if (kobj->ktype != &scx_ktype) 5014bba2c361STejun Heo return 0; 5015bba2c361STejun Heo 5016bba2c361STejun Heo sch = container_of(kobj, struct scx_sched, kobj); 5017bba2c361STejun Heo 5018bba2c361STejun Heo return add_uevent_var(env, "SCXOPS=%s", sch->ops.name); 5019bba2c361STejun Heo } 5020bba2c361STejun Heo 5021bba2c361STejun Heo static const struct kset_uevent_ops scx_uevent_ops = { 5022bba2c361STejun Heo .uevent = scx_uevent, 5023bba2c361STejun Heo }; 5024bba2c361STejun Heo 5025bba2c361STejun Heo /* 5026bba2c361STejun Heo * Used by sched_fork() and __setscheduler_prio() to pick the matching 5027bba2c361STejun Heo * sched_class. dl/rt are already handled. 5028bba2c361STejun Heo */ 5029bba2c361STejun Heo bool task_should_scx(int policy) 5030bba2c361STejun Heo { 5031bba2c361STejun Heo /* if disabled, nothing should be on it */ 5032bba2c361STejun Heo if (!scx_enabled()) 5033bba2c361STejun Heo return false; 5034bba2c361STejun Heo 5035bba2c361STejun Heo /* scx is taking over all SCHED_OTHER and SCHED_EXT tasks */ 5036bba2c361STejun Heo if (READ_ONCE(scx_switching_all)) 5037bba2c361STejun Heo return true; 5038bba2c361STejun Heo 5039bba2c361STejun Heo /* 5040bba2c361STejun Heo * scx is tearing down - keep new SCHED_EXT tasks out. 5041bba2c361STejun Heo * 5042bba2c361STejun Heo * Must come after scx_switching_all test, which serves as a proxy 5043bba2c361STejun Heo * for __scx_switched_all. While __scx_switched_all is set, we must 5044bba2c361STejun Heo * return true via the branch above: a fork routed to fair would 5045bba2c361STejun Heo * stall because next_active_class() skips fair. 5046bba2c361STejun Heo * 5047bba2c361STejun Heo * This can develop into a deadlock - scx holds scx_enable_mutex across 5048bba2c361STejun Heo * kthread_create() in scx_alloc_and_add_sched(); if the new kthread is 5049bba2c361STejun Heo * the stalled task, the disable path can never grab the mutex to clear 5050bba2c361STejun Heo * scx_switching_all. 5051bba2c361STejun Heo */ 5052bba2c361STejun Heo if (unlikely(scx_enable_state() == SCX_DISABLING)) 5053bba2c361STejun Heo return false; 5054bba2c361STejun Heo 5055bba2c361STejun Heo return policy == SCHED_EXT; 5056bba2c361STejun Heo } 5057bba2c361STejun Heo 5058bba2c361STejun Heo bool scx_allow_ttwu_queue(const struct task_struct *p) 5059bba2c361STejun Heo { 5060bba2c361STejun Heo struct scx_sched *sch; 5061bba2c361STejun Heo 5062bba2c361STejun Heo if (!scx_enabled()) 5063bba2c361STejun Heo return true; 5064bba2c361STejun Heo 5065bba2c361STejun Heo sch = scx_task_sched(p); 5066bba2c361STejun Heo if (unlikely(!sch)) 5067bba2c361STejun Heo return true; 5068bba2c361STejun Heo 5069bba2c361STejun Heo if (sch->ops.flags & SCX_OPS_ALLOW_QUEUED_WAKEUP) 5070bba2c361STejun Heo return true; 5071bba2c361STejun Heo 5072bba2c361STejun Heo if (unlikely(p->sched_class != &ext_sched_class)) 5073bba2c361STejun Heo return true; 5074bba2c361STejun Heo 5075bba2c361STejun Heo return false; 5076bba2c361STejun Heo } 5077bba2c361STejun Heo 5078bba2c361STejun Heo /** 5079bba2c361STejun Heo * handle_lockup - sched_ext common lockup handler 5080bba2c361STejun Heo * @fmt: format string 5081bba2c361STejun Heo * 5082bba2c361STejun Heo * Called on system stall or lockup condition and initiates abort of sched_ext 5083bba2c361STejun Heo * if enabled, which may resolve the reported lockup. 5084bba2c361STejun Heo * 5085bba2c361STejun Heo * Returns %true if sched_ext is enabled and abort was initiated, which may 5086bba2c361STejun Heo * resolve the lockup. %false if sched_ext is not enabled or abort was already 5087bba2c361STejun Heo * initiated by someone else. 5088bba2c361STejun Heo */ 5089bba2c361STejun Heo static __printf(1, 2) bool handle_lockup(const char *fmt, ...) 5090bba2c361STejun Heo { 5091bba2c361STejun Heo struct scx_sched *sch; 5092bba2c361STejun Heo va_list args; 5093bba2c361STejun Heo bool ret; 5094bba2c361STejun Heo 5095bba2c361STejun Heo guard(rcu)(); 5096bba2c361STejun Heo 5097bba2c361STejun Heo sch = rcu_dereference(scx_root); 5098bba2c361STejun Heo if (unlikely(!sch)) 5099bba2c361STejun Heo return false; 5100bba2c361STejun Heo 5101bba2c361STejun Heo switch (scx_enable_state()) { 5102bba2c361STejun Heo case SCX_ENABLING: 5103bba2c361STejun Heo case SCX_ENABLED: 5104bba2c361STejun Heo va_start(args, fmt); 5105bba2c361STejun Heo ret = scx_verror(sch, fmt, args); 5106bba2c361STejun Heo va_end(args); 5107bba2c361STejun Heo return ret; 5108bba2c361STejun Heo default: 5109bba2c361STejun Heo return false; 5110bba2c361STejun Heo } 5111bba2c361STejun Heo } 5112bba2c361STejun Heo 5113bba2c361STejun Heo /** 5114bba2c361STejun Heo * scx_rcu_cpu_stall - sched_ext RCU CPU stall handler 5115bba2c361STejun Heo * 5116bba2c361STejun Heo * While there are various reasons why RCU CPU stalls can occur on a system 5117bba2c361STejun Heo * that may not be caused by the current BPF scheduler, try kicking out the 5118bba2c361STejun Heo * current scheduler in an attempt to recover the system to a good state before 5119bba2c361STejun Heo * issuing panics. 5120bba2c361STejun Heo * 5121bba2c361STejun Heo * Returns %true if sched_ext is enabled and abort was initiated, which may 5122bba2c361STejun Heo * resolve the reported RCU stall. %false if sched_ext is not enabled or someone 5123bba2c361STejun Heo * else already initiated abort. 5124bba2c361STejun Heo */ 5125bba2c361STejun Heo bool scx_rcu_cpu_stall(void) 5126bba2c361STejun Heo { 5127bba2c361STejun Heo return handle_lockup("RCU CPU stall detected!"); 5128bba2c361STejun Heo } 5129bba2c361STejun Heo 5130bba2c361STejun Heo /** 5131bba2c361STejun Heo * scx_softlockup - sched_ext softlockup handler 5132bba2c361STejun Heo * @dur_s: number of seconds of CPU stuck due to soft lockup 5133bba2c361STejun Heo * 5134bba2c361STejun Heo * On some multi-socket setups (e.g. 2x Intel 8480c), the BPF scheduler can 5135bba2c361STejun Heo * live-lock the system by making many CPUs target the same DSQ to the point 5136bba2c361STejun Heo * where soft-lockup detection triggers. This function is called from 5137bba2c361STejun Heo * soft-lockup watchdog when the triggering point is close and tries to unjam 5138bba2c361STejun Heo * the system and aborting the BPF scheduler. 5139bba2c361STejun Heo */ 5140bba2c361STejun Heo void scx_softlockup(u32 dur_s) 5141bba2c361STejun Heo { 5142bba2c361STejun Heo if (!handle_lockup("soft lockup - CPU %d stuck for %us", smp_processor_id(), dur_s)) 5143bba2c361STejun Heo return; 5144bba2c361STejun Heo 5145bba2c361STejun Heo printk_deferred(KERN_ERR "sched_ext: Soft lockup - CPU %d stuck for %us, disabling BPF scheduler\n", 5146bba2c361STejun Heo smp_processor_id(), dur_s); 5147bba2c361STejun Heo } 5148bba2c361STejun Heo 5149bba2c361STejun Heo /* 5150bba2c361STejun Heo * scx_hardlockup() runs from NMI and eventually calls scx_claim_exit(), 5151bba2c361STejun Heo * which takes scx_sched_lock. scx_sched_lock isn't NMI-safe and grabbing 5152bba2c361STejun Heo * it from NMI context can lead to deadlocks. Defer via irq_work; the 5153bba2c361STejun Heo * disable path runs off irq_work anyway. 5154bba2c361STejun Heo */ 5155bba2c361STejun Heo static atomic_t scx_hardlockup_cpu = ATOMIC_INIT(-1); 5156bba2c361STejun Heo 5157bba2c361STejun Heo static void scx_hardlockup_irq_workfn(struct irq_work *work) 5158bba2c361STejun Heo { 5159bba2c361STejun Heo int cpu = atomic_xchg(&scx_hardlockup_cpu, -1); 5160bba2c361STejun Heo 5161bba2c361STejun Heo if (cpu >= 0 && handle_lockup("hard lockup - CPU %d", cpu)) 5162bba2c361STejun Heo printk_deferred(KERN_ERR "sched_ext: Hard lockup - CPU %d, disabling BPF scheduler\n", 5163bba2c361STejun Heo cpu); 5164bba2c361STejun Heo } 5165bba2c361STejun Heo 5166bba2c361STejun Heo static DEFINE_IRQ_WORK(scx_hardlockup_irq_work, scx_hardlockup_irq_workfn); 5167bba2c361STejun Heo 5168bba2c361STejun Heo /** 5169bba2c361STejun Heo * scx_hardlockup - sched_ext hardlockup handler 5170bba2c361STejun Heo * 5171bba2c361STejun Heo * A poorly behaving BPF scheduler can trigger hard lockup by e.g. putting 5172bba2c361STejun Heo * numerous affinitized tasks in a single queue and directing all CPUs at it. 5173bba2c361STejun Heo * Try kicking out the current scheduler in an attempt to recover the system to 5174bba2c361STejun Heo * a good state before taking more drastic actions. 5175bba2c361STejun Heo * 5176bba2c361STejun Heo * Queues an irq_work; the handle_lockup() call happens in IRQ context (see 5177bba2c361STejun Heo * scx_hardlockup_irq_workfn). 5178bba2c361STejun Heo * 5179bba2c361STejun Heo * Returns %true if sched_ext is enabled and the work was queued, %false 5180bba2c361STejun Heo * otherwise. 5181bba2c361STejun Heo */ 5182bba2c361STejun Heo bool scx_hardlockup(int cpu) 5183bba2c361STejun Heo { 5184bba2c361STejun Heo if (!rcu_access_pointer(scx_root)) 5185bba2c361STejun Heo return false; 5186bba2c361STejun Heo 5187bba2c361STejun Heo atomic_cmpxchg(&scx_hardlockup_cpu, -1, cpu); 5188bba2c361STejun Heo irq_work_queue(&scx_hardlockup_irq_work); 5189bba2c361STejun Heo return true; 5190bba2c361STejun Heo } 5191bba2c361STejun Heo 5192bba2c361STejun Heo static u32 bypass_lb_cpu(struct scx_sched *sch, s32 donor, 5193bba2c361STejun Heo struct cpumask *donee_mask, struct cpumask *resched_mask, 5194bba2c361STejun Heo u32 nr_donor_target, u32 nr_donee_target) 5195bba2c361STejun Heo { 5196bba2c361STejun Heo struct rq *donor_rq = cpu_rq(donor); 5197bba2c361STejun Heo struct scx_dispatch_q *donor_dsq = bypass_dsq(sch, donor); 5198bba2c361STejun Heo struct task_struct *p, *n; 5199bba2c361STejun Heo struct scx_dsq_list_node cursor = INIT_DSQ_LIST_CURSOR(cursor, donor_dsq, 0); 5200bba2c361STejun Heo s32 delta = READ_ONCE(donor_dsq->nr) - nr_donor_target; 5201bba2c361STejun Heo u32 nr_balanced = 0, min_delta_us; 5202bba2c361STejun Heo 5203bba2c361STejun Heo /* 5204bba2c361STejun Heo * All we want to guarantee is reasonable forward progress. No reason to 5205bba2c361STejun Heo * fine tune. Assuming every task on @donor_dsq runs their full slice, 5206bba2c361STejun Heo * consider offloading iff the total queued duration is over the 5207bba2c361STejun Heo * threshold. 5208bba2c361STejun Heo */ 5209bba2c361STejun Heo min_delta_us = READ_ONCE(scx_bypass_lb_intv_us) / SCX_BYPASS_LB_MIN_DELTA_DIV; 5210bba2c361STejun Heo if (delta < DIV_ROUND_UP(min_delta_us, READ_ONCE(scx_slice_bypass_us))) 5211bba2c361STejun Heo return 0; 5212bba2c361STejun Heo 5213bba2c361STejun Heo raw_spin_rq_lock_irq(donor_rq); 5214bba2c361STejun Heo raw_spin_lock(&donor_dsq->lock); 5215bba2c361STejun Heo list_add(&cursor.node, &donor_dsq->list); 5216bba2c361STejun Heo resume: 5217bba2c361STejun Heo n = container_of(&cursor, struct task_struct, scx.dsq_list); 5218bba2c361STejun Heo n = nldsq_next_task(donor_dsq, n, false); 5219bba2c361STejun Heo 5220bba2c361STejun Heo while ((p = n)) { 5221bba2c361STejun Heo struct scx_dispatch_q *donee_dsq; 5222bba2c361STejun Heo int donee; 5223bba2c361STejun Heo 5224bba2c361STejun Heo n = nldsq_next_task(donor_dsq, n, false); 5225bba2c361STejun Heo 5226bba2c361STejun Heo if (donor_dsq->nr <= nr_donor_target) 5227bba2c361STejun Heo break; 5228bba2c361STejun Heo 5229bba2c361STejun Heo if (cpumask_empty(donee_mask)) 5230bba2c361STejun Heo break; 5231bba2c361STejun Heo 5232bba2c361STejun Heo /* 5233bba2c361STejun Heo * If an earlier pass placed @p on @donor_dsq from a different 5234bba2c361STejun Heo * CPU and the donee hasn't consumed it yet, @p is still on the 5235bba2c361STejun Heo * previous CPU and task_rq(@p) != @donor_rq. @p can't be moved 5236bba2c361STejun Heo * without its rq locked. Skip. 5237bba2c361STejun Heo */ 5238bba2c361STejun Heo if (task_rq(p) != donor_rq) 5239bba2c361STejun Heo continue; 5240bba2c361STejun Heo 5241bba2c361STejun Heo donee = cpumask_any_and_distribute(donee_mask, p->cpus_ptr); 5242bba2c361STejun Heo if (donee >= nr_cpu_ids) 5243bba2c361STejun Heo continue; 5244bba2c361STejun Heo 5245bba2c361STejun Heo donee_dsq = bypass_dsq(sch, donee); 5246bba2c361STejun Heo 5247bba2c361STejun Heo /* 5248bba2c361STejun Heo * $p's rq is not locked but $p's DSQ lock protects its 5249bba2c361STejun Heo * scheduling properties making this test safe. 5250bba2c361STejun Heo */ 5251bba2c361STejun Heo if (!task_can_run_on_remote_rq(sch, p, cpu_rq(donee), false)) 5252bba2c361STejun Heo continue; 5253bba2c361STejun Heo 5254bba2c361STejun Heo /* 5255bba2c361STejun Heo * Moving $p from one non-local DSQ to another. The source rq 5256bba2c361STejun Heo * and DSQ are already locked. Do an abbreviated dequeue and 5257bba2c361STejun Heo * then perform enqueue without unlocking $donor_dsq. 5258bba2c361STejun Heo * 5259bba2c361STejun Heo * We don't want to drop and reacquire the lock on each 5260bba2c361STejun Heo * iteration as @donor_dsq can be very long and potentially 5261bba2c361STejun Heo * highly contended. Donee DSQs are less likely to be contended. 5262bba2c361STejun Heo * The nested locking is safe as only this LB moves tasks 5263bba2c361STejun Heo * between bypass DSQs. 5264bba2c361STejun Heo */ 5265bba2c361STejun Heo dispatch_dequeue_locked(p, donor_dsq); 5266bba2c361STejun Heo dispatch_enqueue(sch, cpu_rq(donee), donee_dsq, p, SCX_ENQ_NESTED); 5267bba2c361STejun Heo 5268bba2c361STejun Heo /* 5269bba2c361STejun Heo * $donee might have been idle and need to be woken up. No need 5270bba2c361STejun Heo * to be clever. Kick every CPU that receives tasks. 5271bba2c361STejun Heo */ 5272bba2c361STejun Heo cpumask_set_cpu(donee, resched_mask); 5273bba2c361STejun Heo 5274bba2c361STejun Heo if (READ_ONCE(donee_dsq->nr) >= nr_donee_target) 5275bba2c361STejun Heo cpumask_clear_cpu(donee, donee_mask); 5276bba2c361STejun Heo 5277bba2c361STejun Heo nr_balanced++; 5278bba2c361STejun Heo if (!(nr_balanced % SCX_BYPASS_LB_BATCH) && n) { 5279bba2c361STejun Heo list_move_tail(&cursor.node, &n->scx.dsq_list.node); 5280bba2c361STejun Heo raw_spin_unlock(&donor_dsq->lock); 5281bba2c361STejun Heo raw_spin_rq_unlock_irq(donor_rq); 5282bba2c361STejun Heo cpu_relax(); 5283bba2c361STejun Heo raw_spin_rq_lock_irq(donor_rq); 5284bba2c361STejun Heo raw_spin_lock(&donor_dsq->lock); 5285bba2c361STejun Heo goto resume; 5286bba2c361STejun Heo } 5287bba2c361STejun Heo } 5288bba2c361STejun Heo 5289bba2c361STejun Heo list_del_init(&cursor.node); 5290bba2c361STejun Heo raw_spin_unlock(&donor_dsq->lock); 5291bba2c361STejun Heo raw_spin_rq_unlock_irq(donor_rq); 5292bba2c361STejun Heo 5293bba2c361STejun Heo return nr_balanced; 5294bba2c361STejun Heo } 5295bba2c361STejun Heo 5296bba2c361STejun Heo static void bypass_lb_node(struct scx_sched *sch, int node) 5297bba2c361STejun Heo { 5298bba2c361STejun Heo const struct cpumask *node_mask = cpumask_of_node(node); 5299bba2c361STejun Heo struct cpumask *donee_mask = sch->bypass_lb_donee_cpumask; 5300bba2c361STejun Heo struct cpumask *resched_mask = sch->bypass_lb_resched_cpumask; 5301bba2c361STejun Heo u32 nr_tasks = 0, nr_cpus = 0, nr_balanced = 0; 5302bba2c361STejun Heo u32 nr_target, nr_donor_target; 5303bba2c361STejun Heo u32 before_min = U32_MAX, before_max = 0; 5304bba2c361STejun Heo u32 after_min = U32_MAX, after_max = 0; 5305bba2c361STejun Heo int cpu; 5306bba2c361STejun Heo 5307bba2c361STejun Heo /* count the target tasks and CPUs */ 5308bba2c361STejun Heo for_each_cpu_and(cpu, cpu_online_mask, node_mask) { 5309bba2c361STejun Heo u32 nr = READ_ONCE(bypass_dsq(sch, cpu)->nr); 5310bba2c361STejun Heo 5311bba2c361STejun Heo nr_tasks += nr; 5312bba2c361STejun Heo nr_cpus++; 5313bba2c361STejun Heo 5314bba2c361STejun Heo before_min = min(nr, before_min); 5315bba2c361STejun Heo before_max = max(nr, before_max); 5316bba2c361STejun Heo } 5317bba2c361STejun Heo 5318bba2c361STejun Heo if (!nr_cpus) 5319bba2c361STejun Heo return; 5320bba2c361STejun Heo 5321bba2c361STejun Heo /* 5322bba2c361STejun Heo * We don't want CPUs to have more than $nr_donor_target tasks and 5323bba2c361STejun Heo * balancing to fill donee CPUs upto $nr_target. Once targets are 5324bba2c361STejun Heo * calculated, find the donee CPUs. 5325bba2c361STejun Heo */ 5326bba2c361STejun Heo nr_target = DIV_ROUND_UP(nr_tasks, nr_cpus); 5327bba2c361STejun Heo nr_donor_target = DIV_ROUND_UP(nr_target * SCX_BYPASS_LB_DONOR_PCT, 100); 5328bba2c361STejun Heo 5329bba2c361STejun Heo cpumask_clear(donee_mask); 5330bba2c361STejun Heo for_each_cpu_and(cpu, cpu_online_mask, node_mask) { 5331bba2c361STejun Heo if (READ_ONCE(bypass_dsq(sch, cpu)->nr) < nr_target) 5332bba2c361STejun Heo cpumask_set_cpu(cpu, donee_mask); 5333bba2c361STejun Heo } 5334bba2c361STejun Heo 5335bba2c361STejun Heo /* iterate !donee CPUs and see if they should be offloaded */ 5336bba2c361STejun Heo cpumask_clear(resched_mask); 5337bba2c361STejun Heo for_each_cpu_and(cpu, cpu_online_mask, node_mask) { 5338bba2c361STejun Heo if (cpumask_empty(donee_mask)) 5339bba2c361STejun Heo break; 5340bba2c361STejun Heo if (cpumask_test_cpu(cpu, donee_mask)) 5341bba2c361STejun Heo continue; 5342bba2c361STejun Heo if (READ_ONCE(bypass_dsq(sch, cpu)->nr) <= nr_donor_target) 5343bba2c361STejun Heo continue; 5344bba2c361STejun Heo 5345bba2c361STejun Heo nr_balanced += bypass_lb_cpu(sch, cpu, donee_mask, resched_mask, 5346bba2c361STejun Heo nr_donor_target, nr_target); 5347bba2c361STejun Heo } 5348bba2c361STejun Heo 5349bba2c361STejun Heo for_each_cpu(cpu, resched_mask) 5350bba2c361STejun Heo resched_cpu(cpu); 5351bba2c361STejun Heo 5352bba2c361STejun Heo for_each_cpu_and(cpu, cpu_online_mask, node_mask) { 5353bba2c361STejun Heo u32 nr = READ_ONCE(bypass_dsq(sch, cpu)->nr); 5354bba2c361STejun Heo 5355bba2c361STejun Heo after_min = min(nr, after_min); 5356bba2c361STejun Heo after_max = max(nr, after_max); 5357bba2c361STejun Heo 5358bba2c361STejun Heo } 5359bba2c361STejun Heo 5360bba2c361STejun Heo trace_sched_ext_bypass_lb(node, nr_cpus, nr_tasks, nr_balanced, 5361bba2c361STejun Heo before_min, before_max, after_min, after_max); 5362bba2c361STejun Heo } 5363bba2c361STejun Heo 5364bba2c361STejun Heo /* 5365bba2c361STejun Heo * In bypass mode, all tasks are put on the per-CPU bypass DSQs. If the machine 5366bba2c361STejun Heo * is over-saturated and the BPF scheduler skewed tasks into few CPUs, some 5367bba2c361STejun Heo * bypass DSQs can be overloaded. If there are enough tasks to saturate other 5368bba2c361STejun Heo * lightly loaded CPUs, such imbalance can lead to very high execution latency 5369bba2c361STejun Heo * on the overloaded CPUs and thus to hung tasks and RCU stalls. To avoid such 5370bba2c361STejun Heo * outcomes, a simple load balancing mechanism is implemented by the following 5371bba2c361STejun Heo * timer which runs periodically while bypass mode is in effect. 5372bba2c361STejun Heo */ 5373bba2c361STejun Heo static void scx_bypass_lb_timerfn(struct timer_list *timer) 5374bba2c361STejun Heo { 5375bba2c361STejun Heo struct scx_sched *sch = container_of(timer, struct scx_sched, bypass_lb_timer); 5376bba2c361STejun Heo int node; 5377bba2c361STejun Heo u32 intv_us; 5378bba2c361STejun Heo 5379bba2c361STejun Heo if (!bypass_dsp_enabled(sch)) 5380bba2c361STejun Heo return; 5381bba2c361STejun Heo 5382bba2c361STejun Heo for_each_node_with_cpus(node) 5383bba2c361STejun Heo bypass_lb_node(sch, node); 5384bba2c361STejun Heo 5385bba2c361STejun Heo intv_us = READ_ONCE(scx_bypass_lb_intv_us); 5386bba2c361STejun Heo if (intv_us) 5387bba2c361STejun Heo mod_timer(timer, jiffies + usecs_to_jiffies(intv_us)); 5388bba2c361STejun Heo } 5389bba2c361STejun Heo 5390bba2c361STejun Heo static bool inc_bypass_depth(struct scx_sched *sch) 5391bba2c361STejun Heo { 5392bba2c361STejun Heo lockdep_assert_held(&scx_bypass_lock); 5393bba2c361STejun Heo 5394bba2c361STejun Heo WARN_ON_ONCE(sch->bypass_depth < 0); 5395bba2c361STejun Heo WRITE_ONCE(sch->bypass_depth, sch->bypass_depth + 1); 5396bba2c361STejun Heo if (sch->bypass_depth != 1) 5397bba2c361STejun Heo return false; 5398bba2c361STejun Heo 5399bba2c361STejun Heo WRITE_ONCE(sch->slice_dfl, READ_ONCE(scx_slice_bypass_us) * NSEC_PER_USEC); 5400bba2c361STejun Heo sch->bypass_timestamp = ktime_get_ns(); 5401bba2c361STejun Heo scx_add_event(sch, SCX_EV_BYPASS_ACTIVATE, 1); 5402bba2c361STejun Heo return true; 5403bba2c361STejun Heo } 5404bba2c361STejun Heo 5405bba2c361STejun Heo static bool dec_bypass_depth(struct scx_sched *sch) 5406bba2c361STejun Heo { 5407bba2c361STejun Heo lockdep_assert_held(&scx_bypass_lock); 5408bba2c361STejun Heo 5409bba2c361STejun Heo WARN_ON_ONCE(sch->bypass_depth < 1); 5410bba2c361STejun Heo WRITE_ONCE(sch->bypass_depth, sch->bypass_depth - 1); 5411bba2c361STejun Heo if (sch->bypass_depth != 0) 5412bba2c361STejun Heo return false; 5413bba2c361STejun Heo 5414bba2c361STejun Heo WRITE_ONCE(sch->slice_dfl, SCX_SLICE_DFL); 5415bba2c361STejun Heo scx_add_event(sch, SCX_EV_BYPASS_DURATION, 5416bba2c361STejun Heo ktime_get_ns() - sch->bypass_timestamp); 5417bba2c361STejun Heo return true; 5418bba2c361STejun Heo } 5419bba2c361STejun Heo 5420bba2c361STejun Heo static void enable_bypass_dsp(struct scx_sched *sch) 5421bba2c361STejun Heo { 5422bba2c361STejun Heo struct scx_sched *host = scx_parent(sch) ?: sch; 5423bba2c361STejun Heo u32 intv_us = READ_ONCE(scx_bypass_lb_intv_us); 5424bba2c361STejun Heo s32 ret; 5425bba2c361STejun Heo 5426bba2c361STejun Heo /* 5427bba2c361STejun Heo * @sch->bypass_depth transitioning from 0 to 1 triggers enabling. 5428bba2c361STejun Heo * Shouldn't stagger. 5429bba2c361STejun Heo */ 5430bba2c361STejun Heo if (WARN_ON_ONCE(test_and_set_bit(0, &sch->bypass_dsp_claim))) 5431bba2c361STejun Heo return; 5432bba2c361STejun Heo 5433bba2c361STejun Heo /* 5434bba2c361STejun Heo * When a sub-sched bypasses, its tasks are queued on the bypass DSQs of 5435bba2c361STejun Heo * the nearest non-bypassing ancestor or root. As enable_bypass_dsp() is 5436bba2c361STejun Heo * called iff @sch is not already bypassed due to an ancestor bypassing, 5437bba2c361STejun Heo * we can assume that the parent is not bypassing and thus will be the 5438bba2c361STejun Heo * host of the bypass DSQs. 5439bba2c361STejun Heo * 5440bba2c361STejun Heo * While the situation may change in the future, the following 5441bba2c361STejun Heo * guarantees that the nearest non-bypassing ancestor or root has bypass 5442bba2c361STejun Heo * dispatch enabled while a descendant is bypassing, which is all that's 5443bba2c361STejun Heo * required. 5444bba2c361STejun Heo * 5445bba2c361STejun Heo * bypass_dsp_enabled() test is used to determine whether to enter the 5446bba2c361STejun Heo * bypass dispatch handling path from both bypassing and hosting scheds. 5447bba2c361STejun Heo * Bump enable depth on both @sch and bypass dispatch host. 5448bba2c361STejun Heo */ 5449bba2c361STejun Heo ret = atomic_inc_return(&sch->bypass_dsp_enable_depth); 5450bba2c361STejun Heo WARN_ON_ONCE(ret <= 0); 5451bba2c361STejun Heo 5452bba2c361STejun Heo if (host != sch) { 5453bba2c361STejun Heo ret = atomic_inc_return(&host->bypass_dsp_enable_depth); 5454bba2c361STejun Heo WARN_ON_ONCE(ret <= 0); 5455bba2c361STejun Heo } 5456bba2c361STejun Heo 5457bba2c361STejun Heo /* 5458bba2c361STejun Heo * The LB timer will stop running if bypass dispatch is disabled. Start 5459bba2c361STejun Heo * after enabling bypass dispatch. 5460bba2c361STejun Heo */ 5461bba2c361STejun Heo if (intv_us && !timer_pending(&host->bypass_lb_timer)) 5462bba2c361STejun Heo mod_timer(&host->bypass_lb_timer, 5463bba2c361STejun Heo jiffies + usecs_to_jiffies(intv_us)); 5464bba2c361STejun Heo } 5465bba2c361STejun Heo 5466bba2c361STejun Heo /* may be called without holding scx_bypass_lock */ 5467bba2c361STejun Heo static void disable_bypass_dsp(struct scx_sched *sch) 5468bba2c361STejun Heo { 5469bba2c361STejun Heo s32 ret; 5470bba2c361STejun Heo 5471bba2c361STejun Heo if (!test_and_clear_bit(0, &sch->bypass_dsp_claim)) 5472bba2c361STejun Heo return; 5473bba2c361STejun Heo 5474bba2c361STejun Heo ret = atomic_dec_return(&sch->bypass_dsp_enable_depth); 5475bba2c361STejun Heo WARN_ON_ONCE(ret < 0); 5476bba2c361STejun Heo 5477bba2c361STejun Heo if (scx_parent(sch)) { 5478bba2c361STejun Heo ret = atomic_dec_return(&scx_parent(sch)->bypass_dsp_enable_depth); 5479bba2c361STejun Heo WARN_ON_ONCE(ret < 0); 5480bba2c361STejun Heo } 5481bba2c361STejun Heo } 5482bba2c361STejun Heo 5483bba2c361STejun Heo /** 5484bba2c361STejun Heo * scx_bypass - [Un]bypass scx_ops and guarantee forward progress 5485bba2c361STejun Heo * @sch: sched to bypass 5486bba2c361STejun Heo * @bypass: true for bypass, false for unbypass 5487bba2c361STejun Heo * 5488bba2c361STejun Heo * Bypassing guarantees that all runnable tasks make forward progress without 5489bba2c361STejun Heo * trusting the BPF scheduler. We can't grab any mutexes or rwsems as they might 5490bba2c361STejun Heo * be held by tasks that the BPF scheduler is forgetting to run, which 5491bba2c361STejun Heo * unfortunately also excludes toggling the static branches. 5492bba2c361STejun Heo * 5493bba2c361STejun Heo * Let's work around by overriding a couple ops and modifying behaviors based on 5494bba2c361STejun Heo * the DISABLING state and then cycling the queued tasks through dequeue/enqueue 5495bba2c361STejun Heo * to force global FIFO scheduling. 5496bba2c361STejun Heo * 5497bba2c361STejun Heo * - ops.select_cpu() is ignored and the default select_cpu() is used. 5498bba2c361STejun Heo * 5499bba2c361STejun Heo * - ops.enqueue() is ignored and tasks are queued in simple global FIFO order. 5500bba2c361STejun Heo * %SCX_OPS_ENQ_LAST is also ignored. 5501bba2c361STejun Heo * 5502bba2c361STejun Heo * - ops.dispatch() is ignored. 5503bba2c361STejun Heo * 5504bba2c361STejun Heo * - balance_one() does not set %SCX_RQ_BAL_KEEP on non-zero slice as slice 5505bba2c361STejun Heo * can't be trusted. Whenever a tick triggers, the running task is rotated to 5506bba2c361STejun Heo * the tail of the queue with core_sched_at touched. 5507bba2c361STejun Heo * 5508bba2c361STejun Heo * - pick_next_task() suppresses zero slice warning. 5509bba2c361STejun Heo * 5510bba2c361STejun Heo * - scx_kick_cpu() is disabled to avoid irq_work malfunction during PM 5511bba2c361STejun Heo * operations. 5512bba2c361STejun Heo * 5513bba2c361STejun Heo * - scx_prio_less() reverts to the default core_sched_at order. 5514bba2c361STejun Heo */ 5515bba2c361STejun Heo static void scx_bypass(struct scx_sched *sch, bool bypass) 5516bba2c361STejun Heo { 5517bba2c361STejun Heo struct scx_sched *pos; 5518bba2c361STejun Heo unsigned long flags; 5519bba2c361STejun Heo int cpu; 5520bba2c361STejun Heo 5521bba2c361STejun Heo raw_spin_lock_irqsave(&scx_bypass_lock, flags); 5522bba2c361STejun Heo 5523bba2c361STejun Heo if (bypass) { 5524bba2c361STejun Heo if (!inc_bypass_depth(sch)) 5525bba2c361STejun Heo goto unlock; 5526bba2c361STejun Heo 5527bba2c361STejun Heo enable_bypass_dsp(sch); 5528bba2c361STejun Heo } else { 5529bba2c361STejun Heo if (!dec_bypass_depth(sch)) 5530bba2c361STejun Heo goto unlock; 5531bba2c361STejun Heo } 5532bba2c361STejun Heo 5533bba2c361STejun Heo /* 5534bba2c361STejun Heo * Bypass state is propagated to all descendants - an scx_sched bypasses 5535bba2c361STejun Heo * if itself or any of its ancestors are in bypass mode. 5536bba2c361STejun Heo */ 5537bba2c361STejun Heo raw_spin_lock(&scx_sched_lock); 5538bba2c361STejun Heo scx_for_each_descendant_pre(pos, sch) { 5539bba2c361STejun Heo if (pos == sch) 5540bba2c361STejun Heo continue; 5541bba2c361STejun Heo if (bypass) 5542bba2c361STejun Heo inc_bypass_depth(pos); 5543bba2c361STejun Heo else 5544bba2c361STejun Heo dec_bypass_depth(pos); 5545bba2c361STejun Heo } 5546bba2c361STejun Heo raw_spin_unlock(&scx_sched_lock); 5547bba2c361STejun Heo 5548bba2c361STejun Heo /* 5549bba2c361STejun Heo * No task property is changing. We just need to make sure all currently 5550bba2c361STejun Heo * queued tasks are re-queued according to the new scx_bypassing() 5551bba2c361STejun Heo * state. As an optimization, walk each rq's runnable_list instead of 5552bba2c361STejun Heo * the scx_tasks list. 5553bba2c361STejun Heo * 5554bba2c361STejun Heo * This function can't trust the scheduler and thus can't use 5555bba2c361STejun Heo * cpus_read_lock(). Walk all possible CPUs instead of online. 5556bba2c361STejun Heo */ 5557bba2c361STejun Heo for_each_possible_cpu(cpu) { 5558bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 5559bba2c361STejun Heo struct task_struct *p, *n; 5560bba2c361STejun Heo 5561bba2c361STejun Heo raw_spin_rq_lock(rq); 5562bba2c361STejun Heo raw_spin_lock(&scx_sched_lock); 5563bba2c361STejun Heo 5564bba2c361STejun Heo scx_for_each_descendant_pre(pos, sch) { 5565bba2c361STejun Heo struct scx_sched_pcpu *pcpu = per_cpu_ptr(pos->pcpu, cpu); 5566bba2c361STejun Heo 5567bba2c361STejun Heo if (pos->bypass_depth) 5568bba2c361STejun Heo pcpu->flags |= SCX_SCHED_PCPU_BYPASSING; 5569bba2c361STejun Heo else 5570bba2c361STejun Heo pcpu->flags &= ~SCX_SCHED_PCPU_BYPASSING; 5571bba2c361STejun Heo } 5572bba2c361STejun Heo 5573bba2c361STejun Heo raw_spin_unlock(&scx_sched_lock); 5574bba2c361STejun Heo 5575bba2c361STejun Heo /* 5576bba2c361STejun Heo * We need to guarantee that no tasks are on the BPF scheduler 5577bba2c361STejun Heo * while bypassing. Either we see enabled or the enable path 5578bba2c361STejun Heo * sees scx_bypassing() before moving tasks to SCX. 5579bba2c361STejun Heo */ 5580bba2c361STejun Heo if (!scx_enabled()) { 5581bba2c361STejun Heo raw_spin_rq_unlock(rq); 5582bba2c361STejun Heo continue; 5583bba2c361STejun Heo } 5584bba2c361STejun Heo 5585bba2c361STejun Heo /* 5586bba2c361STejun Heo * The use of list_for_each_entry_safe_reverse() is required 5587bba2c361STejun Heo * because each task is going to be removed from and added back 5588bba2c361STejun Heo * to the runnable_list during iteration. Because they're added 5589bba2c361STejun Heo * to the tail of the list, safe reverse iteration can still 5590bba2c361STejun Heo * visit all nodes. 5591bba2c361STejun Heo */ 5592bba2c361STejun Heo list_for_each_entry_safe_reverse(p, n, &rq->scx.runnable_list, 5593bba2c361STejun Heo scx.runnable_node) { 5594bba2c361STejun Heo if (!scx_is_descendant(scx_task_sched(p), sch)) 5595bba2c361STejun Heo continue; 5596bba2c361STejun Heo 5597bba2c361STejun Heo /* cycling deq/enq is enough, see the function comment */ 5598bba2c361STejun Heo scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { 5599bba2c361STejun Heo /* nothing */ ; 5600bba2c361STejun Heo } 5601bba2c361STejun Heo } 5602bba2c361STejun Heo 5603bba2c361STejun Heo /* resched to restore ticks and idle state */ 5604bba2c361STejun Heo if (cpu_online(cpu) || cpu == smp_processor_id()) 5605bba2c361STejun Heo resched_curr(rq); 5606bba2c361STejun Heo 5607bba2c361STejun Heo raw_spin_rq_unlock(rq); 5608bba2c361STejun Heo } 5609bba2c361STejun Heo 5610bba2c361STejun Heo /* disarming must come after moving all tasks out of the bypass DSQs */ 5611bba2c361STejun Heo if (!bypass) 5612bba2c361STejun Heo disable_bypass_dsp(sch); 5613bba2c361STejun Heo unlock: 5614bba2c361STejun Heo raw_spin_unlock_irqrestore(&scx_bypass_lock, flags); 5615bba2c361STejun Heo } 5616bba2c361STejun Heo 5617bba2c361STejun Heo static void free_exit_info(struct scx_exit_info *ei) 5618bba2c361STejun Heo { 5619bba2c361STejun Heo kvfree(ei->dump); 5620bba2c361STejun Heo kfree(ei->msg); 5621bba2c361STejun Heo kfree(ei->bt); 5622bba2c361STejun Heo kfree(ei); 5623bba2c361STejun Heo } 5624bba2c361STejun Heo 5625bba2c361STejun Heo static struct scx_exit_info *alloc_exit_info(size_t exit_dump_len) 5626bba2c361STejun Heo { 5627bba2c361STejun Heo struct scx_exit_info *ei; 5628bba2c361STejun Heo 5629bba2c361STejun Heo ei = kzalloc_obj(*ei); 5630bba2c361STejun Heo if (!ei) 5631bba2c361STejun Heo return NULL; 5632bba2c361STejun Heo 5633bba2c361STejun Heo ei->exit_cpu = -1; 5634bba2c361STejun Heo ei->bt = kzalloc_objs(ei->bt[0], SCX_EXIT_BT_LEN); 5635bba2c361STejun Heo ei->msg = kzalloc(SCX_EXIT_MSG_LEN, GFP_KERNEL); 5636bba2c361STejun Heo ei->dump = kvzalloc(exit_dump_len, GFP_KERNEL); 5637bba2c361STejun Heo 5638bba2c361STejun Heo if (!ei->bt || !ei->msg || !ei->dump) { 5639bba2c361STejun Heo free_exit_info(ei); 5640bba2c361STejun Heo return NULL; 5641bba2c361STejun Heo } 5642bba2c361STejun Heo 5643bba2c361STejun Heo return ei; 5644bba2c361STejun Heo } 5645bba2c361STejun Heo 5646bba2c361STejun Heo static const char *scx_exit_reason(enum scx_exit_kind kind) 5647bba2c361STejun Heo { 5648bba2c361STejun Heo switch (kind) { 5649bba2c361STejun Heo case SCX_EXIT_UNREG: 5650bba2c361STejun Heo return "unregistered from user space"; 5651bba2c361STejun Heo case SCX_EXIT_UNREG_BPF: 5652bba2c361STejun Heo return "unregistered from BPF"; 5653bba2c361STejun Heo case SCX_EXIT_UNREG_KERN: 5654bba2c361STejun Heo return "unregistered from the main kernel"; 5655bba2c361STejun Heo case SCX_EXIT_SYSRQ: 5656bba2c361STejun Heo return "disabled by sysrq-S"; 5657bba2c361STejun Heo case SCX_EXIT_PARENT: 5658bba2c361STejun Heo return "parent exiting"; 5659bba2c361STejun Heo case SCX_EXIT_ERROR: 5660bba2c361STejun Heo return "runtime error"; 5661bba2c361STejun Heo case SCX_EXIT_ERROR_BPF: 5662bba2c361STejun Heo return "scx_bpf_error"; 5663bba2c361STejun Heo case SCX_EXIT_ERROR_STALL: 5664bba2c361STejun Heo return "runnable task stall"; 5665bba2c361STejun Heo default: 5666bba2c361STejun Heo return "<UNKNOWN>"; 5667bba2c361STejun Heo } 5668bba2c361STejun Heo } 5669bba2c361STejun Heo 5670bba2c361STejun Heo static void free_kick_syncs(void) 5671bba2c361STejun Heo { 5672bba2c361STejun Heo int cpu; 5673bba2c361STejun Heo 5674bba2c361STejun Heo for_each_possible_cpu(cpu) { 5675bba2c361STejun Heo struct scx_kick_syncs **ksyncs = per_cpu_ptr(&scx_kick_syncs, cpu); 5676bba2c361STejun Heo struct scx_kick_syncs *to_free; 5677bba2c361STejun Heo 5678bba2c361STejun Heo to_free = rcu_replace_pointer(*ksyncs, NULL, true); 5679bba2c361STejun Heo if (to_free) 5680bba2c361STejun Heo kvfree_rcu(to_free, rcu); 5681bba2c361STejun Heo } 5682bba2c361STejun Heo } 5683bba2c361STejun Heo 5684bba2c361STejun Heo static void refresh_watchdog(void) 5685bba2c361STejun Heo { 5686bba2c361STejun Heo struct scx_sched *sch; 5687bba2c361STejun Heo unsigned long intv = ULONG_MAX; 5688bba2c361STejun Heo 5689bba2c361STejun Heo /* take the shortest timeout and use its half for watchdog interval */ 5690bba2c361STejun Heo rcu_read_lock(); 5691bba2c361STejun Heo list_for_each_entry_rcu(sch, &scx_sched_all, all) 5692bba2c361STejun Heo intv = max(min(intv, sch->watchdog_timeout / 2), 1); 5693bba2c361STejun Heo rcu_read_unlock(); 5694bba2c361STejun Heo 5695bba2c361STejun Heo WRITE_ONCE(scx_watchdog_timestamp, jiffies); 5696bba2c361STejun Heo WRITE_ONCE(scx_watchdog_interval, intv); 5697bba2c361STejun Heo 5698bba2c361STejun Heo if (intv < ULONG_MAX) 5699bba2c361STejun Heo mod_delayed_work(system_dfl_wq, &scx_watchdog_work, intv); 5700bba2c361STejun Heo else 5701bba2c361STejun Heo cancel_delayed_work_sync(&scx_watchdog_work); 5702bba2c361STejun Heo } 5703bba2c361STejun Heo 5704bba2c361STejun Heo static s32 scx_link_sched(struct scx_sched *sch) 5705bba2c361STejun Heo { 5706bba2c361STejun Heo const char *err_msg = ""; 5707bba2c361STejun Heo s32 ret = 0; 5708bba2c361STejun Heo 5709bba2c361STejun Heo scoped_guard(raw_spinlock_irq, &scx_sched_lock) { 5710bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 5711bba2c361STejun Heo struct scx_sched *parent = scx_parent(sch); 5712bba2c361STejun Heo 5713bba2c361STejun Heo if (parent) { 5714bba2c361STejun Heo /* 5715bba2c361STejun Heo * scx_claim_exit() propagates exit_kind transition to 5716bba2c361STejun Heo * its sub-scheds while holding scx_sched_lock - either 5717bba2c361STejun Heo * we can see the parent's non-NONE exit_kind or the 5718bba2c361STejun Heo * parent can shoot us down. 5719bba2c361STejun Heo */ 5720bba2c361STejun Heo if (atomic_read(&parent->exit_kind) != SCX_EXIT_NONE) { 5721bba2c361STejun Heo err_msg = "parent disabled"; 5722bba2c361STejun Heo ret = -ENOENT; 5723bba2c361STejun Heo break; 5724bba2c361STejun Heo } 5725bba2c361STejun Heo 5726bba2c361STejun Heo ret = rhashtable_lookup_insert_fast(&scx_sched_hash, 5727bba2c361STejun Heo &sch->hash_node, scx_sched_hash_params); 5728bba2c361STejun Heo if (ret) { 5729bba2c361STejun Heo err_msg = "failed to insert into scx_sched_hash"; 5730bba2c361STejun Heo break; 5731bba2c361STejun Heo } 5732bba2c361STejun Heo 5733bba2c361STejun Heo list_add_tail(&sch->sibling, &parent->children); 5734bba2c361STejun Heo } 5735bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 5736bba2c361STejun Heo 5737bba2c361STejun Heo list_add_tail_rcu(&sch->all, &scx_sched_all); 5738bba2c361STejun Heo } 5739bba2c361STejun Heo 5740bba2c361STejun Heo /* 5741bba2c361STejun Heo * scx_error() takes scx_sched_lock via scx_claim_exit(), so it must run after 5742bba2c361STejun Heo * the guard above is released. 5743bba2c361STejun Heo */ 5744bba2c361STejun Heo if (ret) { 5745bba2c361STejun Heo scx_error(sch, "%s (%d)", err_msg, ret); 5746bba2c361STejun Heo return ret; 5747bba2c361STejun Heo } 5748bba2c361STejun Heo 5749bba2c361STejun Heo refresh_watchdog(); 5750bba2c361STejun Heo return 0; 5751bba2c361STejun Heo } 5752bba2c361STejun Heo 5753bba2c361STejun Heo static void scx_unlink_sched(struct scx_sched *sch) 5754bba2c361STejun Heo { 5755bba2c361STejun Heo scoped_guard(raw_spinlock_irq, &scx_sched_lock) { 5756bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 5757bba2c361STejun Heo if (scx_parent(sch)) { 5758bba2c361STejun Heo rhashtable_remove_fast(&scx_sched_hash, &sch->hash_node, 5759bba2c361STejun Heo scx_sched_hash_params); 5760bba2c361STejun Heo list_del_init(&sch->sibling); 5761bba2c361STejun Heo } 5762bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 5763bba2c361STejun Heo list_del_rcu(&sch->all); 5764bba2c361STejun Heo } 5765bba2c361STejun Heo 5766bba2c361STejun Heo refresh_watchdog(); 5767bba2c361STejun Heo } 5768bba2c361STejun Heo 5769bba2c361STejun Heo /* 5770bba2c361STejun Heo * Called to disable future dumps and wait for in-progress one while disabling 5771bba2c361STejun Heo * @sch. Once @sch becomes empty during disable, there's no point in dumping it. 5772bba2c361STejun Heo * This prevents calling dump ops on a dead sch. 5773bba2c361STejun Heo */ 5774bba2c361STejun Heo static void scx_disable_dump(struct scx_sched *sch) 5775bba2c361STejun Heo { 5776bba2c361STejun Heo guard(raw_spinlock_irqsave)(&scx_dump_lock); 5777bba2c361STejun Heo sch->dump_disabled = true; 5778bba2c361STejun Heo } 5779bba2c361STejun Heo 5780bba2c361STejun Heo static void scx_log_sched_disable(struct scx_sched *sch) 5781bba2c361STejun Heo { 5782bba2c361STejun Heo struct scx_exit_info *ei = sch->exit_info; 5783bba2c361STejun Heo const char *type = scx_parent(sch) ? "sub-scheduler" : "scheduler"; 5784bba2c361STejun Heo 5785bba2c361STejun Heo if (ei->kind >= SCX_EXIT_ERROR) { 5786bba2c361STejun Heo pr_err("sched_ext: BPF %s \"%s\" disabled (%s)\n", type, 5787bba2c361STejun Heo sch->ops.name, ei->reason); 5788bba2c361STejun Heo 5789bba2c361STejun Heo if (ei->msg[0] != '\0') 5790bba2c361STejun Heo pr_err("sched_ext: %s: %s\n", sch->ops.name, ei->msg); 5791bba2c361STejun Heo #ifdef CONFIG_STACKTRACE 5792bba2c361STejun Heo stack_trace_print(ei->bt, ei->bt_len, 2); 5793bba2c361STejun Heo #endif 5794bba2c361STejun Heo } else { 5795bba2c361STejun Heo pr_info("sched_ext: BPF %s \"%s\" disabled (%s)\n", type, 5796bba2c361STejun Heo sch->ops.name, ei->reason); 5797bba2c361STejun Heo } 5798bba2c361STejun Heo } 5799bba2c361STejun Heo 5800bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 5801bba2c361STejun Heo static DECLARE_WAIT_QUEUE_HEAD(scx_unlink_waitq); 5802bba2c361STejun Heo 5803bba2c361STejun Heo static void drain_descendants(struct scx_sched *sch) 5804bba2c361STejun Heo { 5805bba2c361STejun Heo /* 5806bba2c361STejun Heo * Child scheds that finished the critical part of disabling will take 5807bba2c361STejun Heo * themselves off @sch->children. Wait for it to drain. As propagation 5808bba2c361STejun Heo * is recursive, empty @sch->children means that all proper descendant 5809bba2c361STejun Heo * scheds reached unlinking stage. 5810bba2c361STejun Heo */ 5811bba2c361STejun Heo wait_event(scx_unlink_waitq, list_empty(&sch->children)); 5812bba2c361STejun Heo } 5813bba2c361STejun Heo 5814bba2c361STejun Heo static void scx_fail_parent(struct scx_sched *sch, 5815bba2c361STejun Heo struct task_struct *failed, s32 fail_code) 5816bba2c361STejun Heo { 5817bba2c361STejun Heo struct scx_sched *parent = scx_parent(sch); 5818bba2c361STejun Heo struct scx_task_iter sti; 5819bba2c361STejun Heo struct task_struct *p; 5820bba2c361STejun Heo 5821bba2c361STejun Heo scx_error(parent, "ops.init_task() failed (%d) for %s[%d] while disabling a sub-scheduler", 5822bba2c361STejun Heo fail_code, failed->comm, failed->pid); 5823bba2c361STejun Heo 5824bba2c361STejun Heo /* 5825bba2c361STejun Heo * Once $parent is bypassed, it's safe to put SCX_TASK_NONE tasks into 5826bba2c361STejun Heo * it. This may cause downstream failures on the BPF side but $parent is 5827bba2c361STejun Heo * dying anyway. 5828bba2c361STejun Heo */ 5829bba2c361STejun Heo scx_bypass(parent, true); 5830bba2c361STejun Heo 5831bba2c361STejun Heo scx_task_iter_start(&sti, sch->cgrp); 5832bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 5833bba2c361STejun Heo if (scx_task_on_sched(parent, p)) 5834bba2c361STejun Heo continue; 5835bba2c361STejun Heo 5836bba2c361STejun Heo scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { 5837bba2c361STejun Heo scx_disable_and_exit_task(sch, p); 5838bba2c361STejun Heo scx_set_task_sched(p, parent); 5839bba2c361STejun Heo } 5840bba2c361STejun Heo } 5841bba2c361STejun Heo scx_task_iter_stop(&sti); 5842bba2c361STejun Heo } 5843bba2c361STejun Heo 5844bba2c361STejun Heo static void scx_sub_disable(struct scx_sched *sch) 5845bba2c361STejun Heo { 5846bba2c361STejun Heo struct scx_sched *parent = scx_parent(sch); 5847bba2c361STejun Heo struct scx_task_iter sti; 5848bba2c361STejun Heo struct task_struct *p; 5849bba2c361STejun Heo int ret; 5850bba2c361STejun Heo 5851bba2c361STejun Heo /* 5852bba2c361STejun Heo * Guarantee forward progress and wait for descendants to be disabled. 5853bba2c361STejun Heo * To limit disruptions, $parent is not bypassed. Tasks are fully 5854bba2c361STejun Heo * prepped and then inserted back into $parent. 5855bba2c361STejun Heo */ 5856bba2c361STejun Heo scx_bypass(sch, true); 5857bba2c361STejun Heo drain_descendants(sch); 5858bba2c361STejun Heo 5859bba2c361STejun Heo /* 5860bba2c361STejun Heo * Here, every runnable task is guaranteed to make forward progress and 5861bba2c361STejun Heo * we can safely use blocking synchronization constructs. Actually 5862bba2c361STejun Heo * disable ops. 5863bba2c361STejun Heo */ 5864bba2c361STejun Heo mutex_lock(&scx_enable_mutex); 5865bba2c361STejun Heo percpu_down_write(&scx_fork_rwsem); 5866bba2c361STejun Heo scx_cgroup_lock(); 5867bba2c361STejun Heo 5868bba2c361STejun Heo set_cgroup_sched(sch_cgroup(sch), parent); 5869bba2c361STejun Heo 5870bba2c361STejun Heo scx_task_iter_start(&sti, sch->cgrp); 5871bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 5872bba2c361STejun Heo struct rq *rq; 5873bba2c361STejun Heo struct rq_flags rf; 5874bba2c361STejun Heo 5875bba2c361STejun Heo /* filter out duplicate visits */ 5876bba2c361STejun Heo if (scx_task_on_sched(parent, p)) 5877bba2c361STejun Heo continue; 5878bba2c361STejun Heo 5879bba2c361STejun Heo /* 5880bba2c361STejun Heo * By the time control reaches here, all descendant schedulers 5881bba2c361STejun Heo * should already have been disabled. 5882bba2c361STejun Heo */ 5883bba2c361STejun Heo WARN_ON_ONCE(!scx_task_on_sched(sch, p)); 5884bba2c361STejun Heo 5885bba2c361STejun Heo /* 5886bba2c361STejun Heo * @p is pinned by the iter: css_task_iter_next() takes a 5887bba2c361STejun Heo * reference and holds it until the next iter_next() call, so 5888bba2c361STejun Heo * @p->usage is guaranteed > 0. 5889bba2c361STejun Heo */ 5890bba2c361STejun Heo get_task_struct(p); 5891bba2c361STejun Heo 5892bba2c361STejun Heo scx_task_iter_unlock(&sti); 5893bba2c361STejun Heo 5894bba2c361STejun Heo /* 5895bba2c361STejun Heo * $p is READY or ENABLED on @sch. Initialize for $parent, 5896bba2c361STejun Heo * disable and exit from @sch, and then switch over to $parent. 5897bba2c361STejun Heo * 5898bba2c361STejun Heo * If a task fails to initialize for $parent, the only available 5899bba2c361STejun Heo * action is disabling $parent too. While this allows disabling 5900bba2c361STejun Heo * of a child sched to cause the parent scheduler to fail, the 5901bba2c361STejun Heo * failure can only originate from ops.init_task() of the 5902bba2c361STejun Heo * parent. A child can't directly affect the parent through its 5903bba2c361STejun Heo * own failures. 5904bba2c361STejun Heo */ 5905bba2c361STejun Heo ret = __scx_init_task(parent, p, false); 5906bba2c361STejun Heo if (ret) { 5907bba2c361STejun Heo scx_fail_parent(sch, p, ret); 5908bba2c361STejun Heo put_task_struct(p); 5909bba2c361STejun Heo break; 5910bba2c361STejun Heo } 5911bba2c361STejun Heo 5912bba2c361STejun Heo rq = task_rq_lock(p, &rf); 5913bba2c361STejun Heo 5914bba2c361STejun Heo if (scx_get_task_state(p) == SCX_TASK_DEAD) { 5915bba2c361STejun Heo /* 5916bba2c361STejun Heo * sched_ext_dead() raced us between __scx_init_task() 5917bba2c361STejun Heo * and this rq lock and ran exit_task() on @sch (the 5918bba2c361STejun Heo * sched @p was on at that point), not on $parent. 5919bba2c361STejun Heo * $parent's just-completed init is owed an exit_task() 5920bba2c361STejun Heo * and we issue it here. 5921bba2c361STejun Heo */ 5922bba2c361STejun Heo scx_sub_init_cancel_task(parent, p); 5923bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 5924bba2c361STejun Heo put_task_struct(p); 5925bba2c361STejun Heo continue; 5926bba2c361STejun Heo } 5927bba2c361STejun Heo 5928bba2c361STejun Heo scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { 5929bba2c361STejun Heo /* 5930bba2c361STejun Heo * $p is initialized for $parent and still attached to 5931bba2c361STejun Heo * @sch. Disable and exit for @sch, switch over to 5932bba2c361STejun Heo * $parent, override the state to READY to account for 5933bba2c361STejun Heo * $p having already been initialized, and then enable. 5934bba2c361STejun Heo */ 5935bba2c361STejun Heo scx_disable_and_exit_task(sch, p); 5936bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_INIT_BEGIN); 5937bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_INIT); 5938bba2c361STejun Heo scx_set_task_sched(p, parent); 5939bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_READY); 5940bba2c361STejun Heo scx_enable_task(parent, p); 5941bba2c361STejun Heo } 5942bba2c361STejun Heo 5943bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 5944bba2c361STejun Heo put_task_struct(p); 5945bba2c361STejun Heo } 5946bba2c361STejun Heo scx_task_iter_stop(&sti); 5947bba2c361STejun Heo 5948bba2c361STejun Heo scx_disable_dump(sch); 5949bba2c361STejun Heo 5950bba2c361STejun Heo scx_cgroup_unlock(); 5951bba2c361STejun Heo percpu_up_write(&scx_fork_rwsem); 5952bba2c361STejun Heo 5953bba2c361STejun Heo /* 5954bba2c361STejun Heo * All tasks are moved off of @sch but there may still be on-going 5955bba2c361STejun Heo * operations (e.g. ops.select_cpu()). Drain them by flushing RCU. Use 5956bba2c361STejun Heo * the expedited version as ancestors may be waiting in bypass mode. 5957bba2c361STejun Heo * Also, tell the parent that there is no need to keep running bypass 5958bba2c361STejun Heo * DSQs for us. 5959bba2c361STejun Heo */ 5960bba2c361STejun Heo synchronize_rcu_expedited(); 5961bba2c361STejun Heo disable_bypass_dsp(sch); 5962bba2c361STejun Heo 5963bba2c361STejun Heo scx_unlink_sched(sch); 5964bba2c361STejun Heo 5965bba2c361STejun Heo mutex_unlock(&scx_enable_mutex); 5966bba2c361STejun Heo 5967bba2c361STejun Heo /* 5968bba2c361STejun Heo * @sch is now unlinked from the parent's children list. Notify and call 5969bba2c361STejun Heo * ops.sub_detach/exit(). Note that ops.sub_detach/exit() must be called 5970bba2c361STejun Heo * after unlinking and releasing all locks. See scx_claim_exit(). 5971bba2c361STejun Heo */ 5972bba2c361STejun Heo wake_up_all(&scx_unlink_waitq); 5973bba2c361STejun Heo 5974bba2c361STejun Heo if (parent->ops.sub_detach && sch->sub_attached) { 5975bba2c361STejun Heo struct scx_sub_detach_args sub_detach_args = { 5976bba2c361STejun Heo .ops = &sch->ops, 5977bba2c361STejun Heo .cgroup_path = sch->cgrp_path, 5978bba2c361STejun Heo }; 5979bba2c361STejun Heo SCX_CALL_OP(parent, sub_detach, NULL, 5980bba2c361STejun Heo &sub_detach_args); 5981bba2c361STejun Heo } 5982bba2c361STejun Heo 5983bba2c361STejun Heo scx_log_sched_disable(sch); 5984bba2c361STejun Heo 5985bba2c361STejun Heo if (sch->ops.exit) 5986bba2c361STejun Heo SCX_CALL_OP(sch, exit, NULL, sch->exit_info); 5987bba2c361STejun Heo if (sch->sub_kset) 5988bba2c361STejun Heo kobject_del(&sch->sub_kset->kobj); 5989bba2c361STejun Heo kobject_del(&sch->kobj); 5990bba2c361STejun Heo } 5991bba2c361STejun Heo #else /* CONFIG_EXT_SUB_SCHED */ 5992bba2c361STejun Heo static inline void drain_descendants(struct scx_sched *sch) { } 5993bba2c361STejun Heo static inline void scx_sub_disable(struct scx_sched *sch) { } 5994bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 5995bba2c361STejun Heo 5996bba2c361STejun Heo static void scx_root_disable(struct scx_sched *sch) 5997bba2c361STejun Heo { 5998bba2c361STejun Heo struct scx_task_iter sti; 5999bba2c361STejun Heo struct task_struct *p; 6000bba2c361STejun Heo bool was_switched_all; 6001bba2c361STejun Heo int cpu; 6002bba2c361STejun Heo 6003bba2c361STejun Heo /* guarantee forward progress and wait for descendants to be disabled */ 6004bba2c361STejun Heo scx_bypass(sch, true); 6005bba2c361STejun Heo drain_descendants(sch); 6006bba2c361STejun Heo 6007bba2c361STejun Heo switch (scx_set_enable_state(SCX_DISABLING)) { 6008bba2c361STejun Heo case SCX_DISABLING: 6009bba2c361STejun Heo WARN_ONCE(true, "sched_ext: duplicate disabling instance?"); 6010bba2c361STejun Heo break; 6011bba2c361STejun Heo case SCX_DISABLED: 6012bba2c361STejun Heo pr_warn("sched_ext: ops error detected without ops (%s)\n", 6013bba2c361STejun Heo sch->exit_info->msg); 6014bba2c361STejun Heo WARN_ON_ONCE(scx_set_enable_state(SCX_DISABLED) != SCX_DISABLING); 6015bba2c361STejun Heo goto done; 6016bba2c361STejun Heo default: 6017bba2c361STejun Heo break; 6018bba2c361STejun Heo } 6019bba2c361STejun Heo 6020bba2c361STejun Heo /* 6021bba2c361STejun Heo * Here, every runnable task is guaranteed to make forward progress and 6022bba2c361STejun Heo * we can safely use blocking synchronization constructs. Actually 6023bba2c361STejun Heo * disable ops. 6024bba2c361STejun Heo */ 6025bba2c361STejun Heo mutex_lock(&scx_enable_mutex); 6026bba2c361STejun Heo 6027bba2c361STejun Heo was_switched_all = scx_switched_all(); 6028bba2c361STejun Heo 6029bba2c361STejun Heo static_branch_disable(&__scx_switched_all); 6030bba2c361STejun Heo WRITE_ONCE(scx_switching_all, false); 6031bba2c361STejun Heo 6032bba2c361STejun Heo /* 6033bba2c361STejun Heo * Shut down cgroup support before tasks so that the cgroup attach path 6034bba2c361STejun Heo * doesn't race against scx_disable_and_exit_task(). 6035bba2c361STejun Heo */ 6036bba2c361STejun Heo scx_cgroup_lock(); 6037bba2c361STejun Heo scx_cgroup_exit(sch); 6038bba2c361STejun Heo scx_cgroup_unlock(); 6039bba2c361STejun Heo 6040bba2c361STejun Heo /* 6041bba2c361STejun Heo * The BPF scheduler is going away. All tasks including %TASK_DEAD ones 6042bba2c361STejun Heo * must be switched out and exited synchronously. 6043bba2c361STejun Heo */ 6044bba2c361STejun Heo percpu_down_write(&scx_fork_rwsem); 6045bba2c361STejun Heo 6046bba2c361STejun Heo scx_init_task_enabled = false; 6047bba2c361STejun Heo 6048bba2c361STejun Heo scx_task_iter_start(&sti, NULL); 6049bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 6050bba2c361STejun Heo unsigned int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; 6051bba2c361STejun Heo const struct sched_class *old_class = p->sched_class; 6052bba2c361STejun Heo const struct sched_class *new_class = scx_setscheduler_class(p); 6053bba2c361STejun Heo 6054bba2c361STejun Heo update_rq_clock(task_rq(p)); 6055bba2c361STejun Heo 6056bba2c361STejun Heo if (old_class != new_class) 6057bba2c361STejun Heo queue_flags |= DEQUEUE_CLASS; 6058bba2c361STejun Heo 6059bba2c361STejun Heo scoped_guard (sched_change, p, queue_flags) { 6060bba2c361STejun Heo p->sched_class = new_class; 6061bba2c361STejun Heo } 6062bba2c361STejun Heo 6063bba2c361STejun Heo scx_disable_and_exit_task(scx_task_sched(p), p); 6064bba2c361STejun Heo } 6065bba2c361STejun Heo scx_task_iter_stop(&sti); 6066bba2c361STejun Heo 6067bba2c361STejun Heo scx_disable_dump(sch); 6068bba2c361STejun Heo 6069bba2c361STejun Heo scx_cgroup_lock(); 6070bba2c361STejun Heo set_cgroup_sched(sch_cgroup(sch), NULL); 6071bba2c361STejun Heo scx_cgroup_unlock(); 6072bba2c361STejun Heo 6073bba2c361STejun Heo percpu_up_write(&scx_fork_rwsem); 6074bba2c361STejun Heo 6075bba2c361STejun Heo /* 6076bba2c361STejun Heo * Invalidate all the rq clocks to prevent getting outdated 6077bba2c361STejun Heo * rq clocks from a previous scx scheduler. 6078bba2c361STejun Heo * 6079bba2c361STejun Heo * Also re-balance the dl_server bandwidth reservations: detach 6080bba2c361STejun Heo * ext_server (no more sched_ext tasks) and reinstate fair_server if it 6081bba2c361STejun Heo * was previously detached because we were running in full mode. 6082bba2c361STejun Heo * 6083bba2c361STejun Heo * Unlike the enable path, this runs on a recovery path that cannot 6084bba2c361STejun Heo * fail, so we use dl_server_swap_bw() to atomically free ext_server's 6085bba2c361STejun Heo * bandwidth and reclaim it for fair_server under the same dl_b lock. 6086bba2c361STejun Heo * 6087bba2c361STejun Heo * The swap can still fail with -EBUSY if someone bumped ext_server's 6088bba2c361STejun Heo * runtime via debugfs between enable and disable; in that narrow case 6089bba2c361STejun Heo * both servers end up detached and we just WARN. 6090bba2c361STejun Heo */ 6091bba2c361STejun Heo for_each_possible_cpu(cpu) { 6092bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 6093bba2c361STejun Heo 6094bba2c361STejun Heo scx_rq_clock_invalidate(rq); 6095bba2c361STejun Heo 6096bba2c361STejun Heo scoped_guard(rq_lock_irqsave, rq) { 6097bba2c361STejun Heo update_rq_clock(rq); 6098bba2c361STejun Heo if (was_switched_all) { 6099bba2c361STejun Heo if (WARN_ON_ONCE(dl_server_swap_bw(&rq->ext_server, 6100bba2c361STejun Heo &rq->fair_server))) 6101bba2c361STejun Heo pr_warn("failed to re-attach fair_server on CPU %d\n", cpu); 6102bba2c361STejun Heo } else { 6103bba2c361STejun Heo dl_server_detach_bw(&rq->ext_server); 6104bba2c361STejun Heo } 6105bba2c361STejun Heo } 6106bba2c361STejun Heo } 6107bba2c361STejun Heo 6108bba2c361STejun Heo /* no task is on scx, turn off all the switches and flush in-progress calls */ 6109bba2c361STejun Heo static_branch_disable(&__scx_enabled); 6110bba2c361STejun Heo static_branch_disable(&__scx_is_cid_type); 6111bba2c361STejun Heo if (sch->ops.flags & SCX_OPS_TID_TO_TASK) 6112bba2c361STejun Heo static_branch_disable(&__scx_tid_to_task_enabled); 6113bba2c361STejun Heo bitmap_zero(sch->has_op, SCX_OPI_END); 6114bba2c361STejun Heo scx_idle_disable(); 6115bba2c361STejun Heo synchronize_rcu(); 6116bba2c361STejun Heo if (sch->ops.flags & SCX_OPS_TID_TO_TASK) 6117bba2c361STejun Heo rhashtable_free_and_destroy(&scx_tid_hash, NULL, NULL); 6118bba2c361STejun Heo 6119bba2c361STejun Heo scx_log_sched_disable(sch); 6120bba2c361STejun Heo 6121bba2c361STejun Heo if (sch->ops.exit) 6122bba2c361STejun Heo SCX_CALL_OP(sch, exit, NULL, sch->exit_info); 6123bba2c361STejun Heo 6124bba2c361STejun Heo scx_unlink_sched(sch); 6125bba2c361STejun Heo 6126bba2c361STejun Heo /* 6127bba2c361STejun Heo * scx_root clearing must be inside cpus_read_lock(). See 6128bba2c361STejun Heo * handle_hotplug(). 6129bba2c361STejun Heo */ 6130bba2c361STejun Heo cpus_read_lock(); 6131bba2c361STejun Heo RCU_INIT_POINTER(scx_root, NULL); 6132bba2c361STejun Heo cpus_read_unlock(); 6133bba2c361STejun Heo 6134bba2c361STejun Heo /* 6135bba2c361STejun Heo * Delete the kobject from the hierarchy synchronously. Otherwise, sysfs 6136bba2c361STejun Heo * could observe an object of the same name still in the hierarchy when 6137bba2c361STejun Heo * the next scheduler is loaded. 6138bba2c361STejun Heo */ 6139bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 6140bba2c361STejun Heo if (sch->sub_kset) 6141bba2c361STejun Heo kobject_del(&sch->sub_kset->kobj); 6142bba2c361STejun Heo #endif 6143bba2c361STejun Heo kobject_del(&sch->kobj); 6144bba2c361STejun Heo 6145bba2c361STejun Heo free_kick_syncs(); 6146bba2c361STejun Heo 6147bba2c361STejun Heo mutex_unlock(&scx_enable_mutex); 6148bba2c361STejun Heo 6149bba2c361STejun Heo WARN_ON_ONCE(scx_set_enable_state(SCX_DISABLED) != SCX_DISABLING); 6150bba2c361STejun Heo done: 6151bba2c361STejun Heo scx_bypass(sch, false); 6152bba2c361STejun Heo } 6153bba2c361STejun Heo 6154bba2c361STejun Heo /* 6155bba2c361STejun Heo * Claim the exit on @sch. The caller must ensure that the helper kthread work 6156bba2c361STejun Heo * is kicked before the current task can be preempted. Once exit_kind is 6157bba2c361STejun Heo * claimed, scx_error() can no longer trigger, so if the current task gets 6158bba2c361STejun Heo * preempted and the BPF scheduler fails to schedule it back, the helper work 6159bba2c361STejun Heo * will never be kicked and the whole system can wedge. 6160bba2c361STejun Heo */ 6161bba2c361STejun Heo static bool scx_claim_exit(struct scx_sched *sch, enum scx_exit_kind kind) 6162bba2c361STejun Heo { 6163bba2c361STejun Heo int none = SCX_EXIT_NONE; 6164bba2c361STejun Heo 6165bba2c361STejun Heo lockdep_assert_preemption_disabled(); 6166bba2c361STejun Heo 6167bba2c361STejun Heo if (WARN_ON_ONCE(kind == SCX_EXIT_NONE || kind == SCX_EXIT_DONE)) 6168bba2c361STejun Heo kind = SCX_EXIT_ERROR; 6169bba2c361STejun Heo 6170bba2c361STejun Heo if (!atomic_try_cmpxchg(&sch->exit_kind, &none, kind)) 6171bba2c361STejun Heo return false; 6172bba2c361STejun Heo 6173bba2c361STejun Heo /* 6174bba2c361STejun Heo * Some CPUs may be trapped in the dispatch paths. Set the aborting 6175bba2c361STejun Heo * flag to break potential live-lock scenarios, ensuring we can 6176bba2c361STejun Heo * successfully reach scx_bypass(). 6177bba2c361STejun Heo */ 6178bba2c361STejun Heo WRITE_ONCE(sch->aborting, true); 6179bba2c361STejun Heo 6180bba2c361STejun Heo /* 6181bba2c361STejun Heo * Propagate exits to descendants immediately. Each has a dedicated 6182bba2c361STejun Heo * helper kthread and can run in parallel. While most of disabling is 6183bba2c361STejun Heo * serialized, running them in separate threads allows parallelizing 6184bba2c361STejun Heo * ops.exit(), which can take arbitrarily long prolonging bypass mode. 6185bba2c361STejun Heo * 6186bba2c361STejun Heo * To guarantee forward progress, this propagation must be in-line so 6187bba2c361STejun Heo * that ->aborting is synchronously asserted for all sub-scheds. The 6188bba2c361STejun Heo * propagation is also the interlocking point against sub-sched 6189bba2c361STejun Heo * attachment. See scx_link_sched(). 6190bba2c361STejun Heo * 6191bba2c361STejun Heo * This doesn't cause recursions as propagation only takes place for 6192bba2c361STejun Heo * non-propagation exits. 6193bba2c361STejun Heo */ 6194bba2c361STejun Heo if (kind != SCX_EXIT_PARENT) { 6195bba2c361STejun Heo scoped_guard (raw_spinlock_irqsave, &scx_sched_lock) { 6196bba2c361STejun Heo struct scx_sched *pos; 6197bba2c361STejun Heo scx_for_each_descendant_pre(pos, sch) 6198bba2c361STejun Heo scx_disable(pos, SCX_EXIT_PARENT); 6199bba2c361STejun Heo } 6200bba2c361STejun Heo } 6201bba2c361STejun Heo 6202bba2c361STejun Heo return true; 6203bba2c361STejun Heo } 6204bba2c361STejun Heo 6205bba2c361STejun Heo static void scx_disable_workfn(struct kthread_work *work) 6206bba2c361STejun Heo { 6207bba2c361STejun Heo struct scx_sched *sch = container_of(work, struct scx_sched, disable_work); 6208bba2c361STejun Heo struct scx_exit_info *ei = sch->exit_info; 6209bba2c361STejun Heo int kind; 6210bba2c361STejun Heo 6211bba2c361STejun Heo kind = atomic_read(&sch->exit_kind); 6212bba2c361STejun Heo while (true) { 6213bba2c361STejun Heo if (kind == SCX_EXIT_DONE) /* already disabled? */ 6214bba2c361STejun Heo return; 6215bba2c361STejun Heo WARN_ON_ONCE(kind == SCX_EXIT_NONE); 6216bba2c361STejun Heo if (atomic_try_cmpxchg(&sch->exit_kind, &kind, SCX_EXIT_DONE)) 6217bba2c361STejun Heo break; 6218bba2c361STejun Heo } 6219bba2c361STejun Heo ei->kind = kind; 6220bba2c361STejun Heo ei->reason = scx_exit_reason(ei->kind); 6221bba2c361STejun Heo 6222bba2c361STejun Heo if (scx_parent(sch)) 6223bba2c361STejun Heo scx_sub_disable(sch); 6224bba2c361STejun Heo else 6225bba2c361STejun Heo scx_root_disable(sch); 6226bba2c361STejun Heo } 6227bba2c361STejun Heo 6228bba2c361STejun Heo static void scx_disable(struct scx_sched *sch, enum scx_exit_kind kind) 6229bba2c361STejun Heo { 6230bba2c361STejun Heo guard(preempt)(); 6231bba2c361STejun Heo if (scx_claim_exit(sch, kind)) 6232bba2c361STejun Heo irq_work_queue(&sch->disable_irq_work); 6233bba2c361STejun Heo } 6234bba2c361STejun Heo 6235bba2c361STejun Heo /** 6236bba2c361STejun Heo * scx_flush_disable_work - flush the disable work and wait for it to finish 6237bba2c361STejun Heo * @sch: the scheduler 6238bba2c361STejun Heo * 6239bba2c361STejun Heo * sch->disable_work might still not queued, causing kthread_flush_work() 6240bba2c361STejun Heo * as a noop. Syncing the irq_work first is required to guarantee the 6241bba2c361STejun Heo * kthread work has been queued before waiting for it. 6242bba2c361STejun Heo */ 6243bba2c361STejun Heo static void scx_flush_disable_work(struct scx_sched *sch) 6244bba2c361STejun Heo { 6245bba2c361STejun Heo int kind; 6246bba2c361STejun Heo 6247bba2c361STejun Heo do { 6248bba2c361STejun Heo irq_work_sync(&sch->disable_irq_work); 6249bba2c361STejun Heo kthread_flush_work(&sch->disable_work); 6250bba2c361STejun Heo kind = atomic_read(&sch->exit_kind); 6251bba2c361STejun Heo } while (kind != SCX_EXIT_NONE && kind != SCX_EXIT_DONE); 6252bba2c361STejun Heo } 6253bba2c361STejun Heo 6254bba2c361STejun Heo static void dump_newline(struct seq_buf *s) 6255bba2c361STejun Heo { 6256bba2c361STejun Heo trace_sched_ext_dump(""); 6257bba2c361STejun Heo 6258bba2c361STejun Heo /* @s may be zero sized and seq_buf triggers WARN if so */ 6259bba2c361STejun Heo if (s->size) 6260bba2c361STejun Heo seq_buf_putc(s, '\n'); 6261bba2c361STejun Heo } 6262bba2c361STejun Heo 6263bba2c361STejun Heo static __printf(2, 3) void dump_line(struct seq_buf *s, const char *fmt, ...) 6264bba2c361STejun Heo { 6265bba2c361STejun Heo va_list args; 6266bba2c361STejun Heo 6267bba2c361STejun Heo #ifdef CONFIG_TRACEPOINTS 6268bba2c361STejun Heo if (trace_sched_ext_dump_enabled()) { 6269bba2c361STejun Heo /* protected by scx_dump_lock */ 6270bba2c361STejun Heo static char line_buf[SCX_EXIT_MSG_LEN]; 6271bba2c361STejun Heo 6272bba2c361STejun Heo va_start(args, fmt); 6273bba2c361STejun Heo vscnprintf(line_buf, sizeof(line_buf), fmt, args); 6274bba2c361STejun Heo va_end(args); 6275bba2c361STejun Heo 6276bba2c361STejun Heo trace_call__sched_ext_dump(line_buf); 6277bba2c361STejun Heo } 6278bba2c361STejun Heo #endif 6279bba2c361STejun Heo /* @s may be zero sized and seq_buf triggers WARN if so */ 6280bba2c361STejun Heo if (s->size) { 6281bba2c361STejun Heo va_start(args, fmt); 6282bba2c361STejun Heo seq_buf_vprintf(s, fmt, args); 6283bba2c361STejun Heo va_end(args); 6284bba2c361STejun Heo 6285bba2c361STejun Heo seq_buf_putc(s, '\n'); 6286bba2c361STejun Heo } 6287bba2c361STejun Heo } 6288bba2c361STejun Heo 6289bba2c361STejun Heo static void dump_stack_trace(struct seq_buf *s, const char *prefix, 6290bba2c361STejun Heo const unsigned long *bt, unsigned int len) 6291bba2c361STejun Heo { 6292bba2c361STejun Heo unsigned int i; 6293bba2c361STejun Heo 6294bba2c361STejun Heo for (i = 0; i < len; i++) 6295bba2c361STejun Heo dump_line(s, "%s%pS", prefix, (void *)bt[i]); 6296bba2c361STejun Heo } 6297bba2c361STejun Heo 6298bba2c361STejun Heo static void ops_dump_init(struct seq_buf *s, const char *prefix) 6299bba2c361STejun Heo { 6300bba2c361STejun Heo struct scx_dump_data *dd = &scx_dump_data; 6301bba2c361STejun Heo 6302bba2c361STejun Heo lockdep_assert_irqs_disabled(); 6303bba2c361STejun Heo 6304bba2c361STejun Heo dd->cpu = smp_processor_id(); /* allow scx_bpf_dump() */ 6305bba2c361STejun Heo dd->first = true; 6306bba2c361STejun Heo dd->cursor = 0; 6307bba2c361STejun Heo dd->s = s; 6308bba2c361STejun Heo dd->prefix = prefix; 6309bba2c361STejun Heo } 6310bba2c361STejun Heo 6311bba2c361STejun Heo static void ops_dump_flush(void) 6312bba2c361STejun Heo { 6313bba2c361STejun Heo struct scx_dump_data *dd = &scx_dump_data; 6314bba2c361STejun Heo char *line = dd->buf.line; 6315bba2c361STejun Heo 6316bba2c361STejun Heo if (!dd->cursor) 6317bba2c361STejun Heo return; 6318bba2c361STejun Heo 6319bba2c361STejun Heo /* 6320bba2c361STejun Heo * There's something to flush and this is the first line. Insert a blank 6321bba2c361STejun Heo * line to distinguish ops dump. 6322bba2c361STejun Heo */ 6323bba2c361STejun Heo if (dd->first) { 6324bba2c361STejun Heo dump_newline(dd->s); 6325bba2c361STejun Heo dd->first = false; 6326bba2c361STejun Heo } 6327bba2c361STejun Heo 6328bba2c361STejun Heo /* 6329bba2c361STejun Heo * There may be multiple lines in $line. Scan and emit each line 6330bba2c361STejun Heo * separately. 6331bba2c361STejun Heo */ 6332bba2c361STejun Heo while (true) { 6333bba2c361STejun Heo char *end = line; 6334bba2c361STejun Heo char c; 6335bba2c361STejun Heo 6336bba2c361STejun Heo while (*end != '\n' && *end != '\0') 6337bba2c361STejun Heo end++; 6338bba2c361STejun Heo 6339bba2c361STejun Heo /* 6340bba2c361STejun Heo * If $line overflowed, it may not have newline at the end. 6341bba2c361STejun Heo * Always emit with a newline. 6342bba2c361STejun Heo */ 6343bba2c361STejun Heo c = *end; 6344bba2c361STejun Heo *end = '\0'; 6345bba2c361STejun Heo dump_line(dd->s, "%s%s", dd->prefix, line); 6346bba2c361STejun Heo if (c == '\0') 6347bba2c361STejun Heo break; 6348bba2c361STejun Heo 6349bba2c361STejun Heo /* move to the next line */ 6350bba2c361STejun Heo end++; 6351bba2c361STejun Heo if (*end == '\0') 6352bba2c361STejun Heo break; 6353bba2c361STejun Heo line = end; 6354bba2c361STejun Heo } 6355bba2c361STejun Heo 6356bba2c361STejun Heo dd->cursor = 0; 6357bba2c361STejun Heo } 6358bba2c361STejun Heo 6359bba2c361STejun Heo static void ops_dump_exit(void) 6360bba2c361STejun Heo { 6361bba2c361STejun Heo ops_dump_flush(); 6362bba2c361STejun Heo scx_dump_data.cpu = -1; 6363bba2c361STejun Heo } 6364bba2c361STejun Heo 6365bba2c361STejun Heo static void scx_dump_task(struct scx_sched *sch, struct seq_buf *s, struct scx_dump_ctx *dctx, 6366bba2c361STejun Heo struct rq *rq, struct task_struct *p, char marker) 6367bba2c361STejun Heo { 6368bba2c361STejun Heo static unsigned long bt[SCX_EXIT_BT_LEN]; 6369bba2c361STejun Heo struct scx_sched *task_sch = scx_task_sched(p); 6370bba2c361STejun Heo const char *own_marker; 6371bba2c361STejun Heo char sch_id_buf[32]; 6372bba2c361STejun Heo char dsq_id_buf[19] = "(n/a)"; 6373bba2c361STejun Heo unsigned long ops_state = atomic_long_read(&p->scx.ops_state); 6374bba2c361STejun Heo unsigned int bt_len = 0; 6375bba2c361STejun Heo 6376bba2c361STejun Heo own_marker = task_sch == sch ? "*" : ""; 6377bba2c361STejun Heo 6378bba2c361STejun Heo if (task_sch->level == 0) 6379bba2c361STejun Heo scnprintf(sch_id_buf, sizeof(sch_id_buf), "root"); 6380bba2c361STejun Heo else 6381bba2c361STejun Heo scnprintf(sch_id_buf, sizeof(sch_id_buf), "sub%d-%llu", 6382bba2c361STejun Heo task_sch->level, task_sch->ops.sub_cgroup_id); 6383bba2c361STejun Heo 6384bba2c361STejun Heo if (p->scx.dsq) 6385bba2c361STejun Heo scnprintf(dsq_id_buf, sizeof(dsq_id_buf), "0x%llx", 6386bba2c361STejun Heo (unsigned long long)p->scx.dsq->id); 6387bba2c361STejun Heo 6388bba2c361STejun Heo dump_newline(s); 6389bba2c361STejun Heo dump_line(s, " %c%c %s[%d] %s%s %+ldms", 6390bba2c361STejun Heo marker, task_state_to_char(p), p->comm, p->pid, 6391bba2c361STejun Heo own_marker, sch_id_buf, 6392bba2c361STejun Heo jiffies_delta_msecs(p->scx.runnable_at, dctx->at_jiffies)); 6393bba2c361STejun Heo dump_line(s, " scx_state/flags=%u/0x%x dsq_flags=0x%x ops_state/qseq=%lu/%lu", 6394bba2c361STejun Heo scx_get_task_state(p) >> SCX_TASK_STATE_SHIFT, 6395bba2c361STejun Heo p->scx.flags & ~SCX_TASK_STATE_MASK, 6396bba2c361STejun Heo p->scx.dsq_flags, ops_state & SCX_OPSS_STATE_MASK, 6397bba2c361STejun Heo ops_state >> SCX_OPSS_QSEQ_SHIFT); 6398bba2c361STejun Heo dump_line(s, " sticky/holding_cpu=%d/%d dsq_id=%s", 6399bba2c361STejun Heo p->scx.sticky_cpu, p->scx.holding_cpu, dsq_id_buf); 6400bba2c361STejun Heo dump_line(s, " dsq_vtime=%llu slice=%llu weight=%u", 6401bba2c361STejun Heo p->scx.dsq_vtime, p->scx.slice, p->scx.weight); 6402bba2c361STejun Heo dump_line(s, " cpus=%*pb no_mig=%u", cpumask_pr_args(p->cpus_ptr), 6403bba2c361STejun Heo p->migration_disabled); 6404bba2c361STejun Heo 6405bba2c361STejun Heo if (SCX_HAS_OP(sch, dump_task)) { 6406bba2c361STejun Heo ops_dump_init(s, " "); 6407bba2c361STejun Heo SCX_CALL_OP(sch, dump_task, rq, dctx, p); 6408bba2c361STejun Heo ops_dump_exit(); 6409bba2c361STejun Heo } 6410bba2c361STejun Heo 6411bba2c361STejun Heo #ifdef CONFIG_STACKTRACE 6412bba2c361STejun Heo bt_len = stack_trace_save_tsk(p, bt, SCX_EXIT_BT_LEN, 1); 6413bba2c361STejun Heo #endif 6414bba2c361STejun Heo if (bt_len) { 6415bba2c361STejun Heo dump_newline(s); 6416bba2c361STejun Heo dump_stack_trace(s, " ", bt, bt_len); 6417bba2c361STejun Heo } 6418bba2c361STejun Heo } 6419bba2c361STejun Heo 6420bba2c361STejun Heo static void scx_dump_cpu(struct scx_sched *sch, struct seq_buf *s, 6421bba2c361STejun Heo struct scx_dump_ctx *dctx, int cpu, 6422bba2c361STejun Heo bool dump_all_tasks) 6423bba2c361STejun Heo { 6424bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 6425bba2c361STejun Heo struct rq_flags rf; 6426bba2c361STejun Heo struct task_struct *p; 6427bba2c361STejun Heo struct seq_buf ns; 6428bba2c361STejun Heo size_t avail, used; 6429bba2c361STejun Heo char *buf; 6430bba2c361STejun Heo bool idle; 6431bba2c361STejun Heo 6432bba2c361STejun Heo rq_lock_irqsave(rq, &rf); 6433bba2c361STejun Heo 6434bba2c361STejun Heo idle = list_empty(&rq->scx.runnable_list) && 6435bba2c361STejun Heo rq->curr->sched_class == &idle_sched_class; 6436bba2c361STejun Heo 6437bba2c361STejun Heo if (idle && !SCX_HAS_OP(sch, dump_cpu)) 6438bba2c361STejun Heo goto next; 6439bba2c361STejun Heo 6440bba2c361STejun Heo /* 6441bba2c361STejun Heo * We don't yet know whether ops.dump_cpu() will produce output 6442bba2c361STejun Heo * and we may want to skip the default CPU dump if it doesn't. 6443bba2c361STejun Heo * Use a nested seq_buf to generate the standard dump so that we 6444bba2c361STejun Heo * can decide whether to commit later. 6445bba2c361STejun Heo */ 6446bba2c361STejun Heo avail = seq_buf_get_buf(s, &buf); 6447bba2c361STejun Heo seq_buf_init(&ns, buf, avail); 6448bba2c361STejun Heo 6449bba2c361STejun Heo dump_newline(&ns); 6450bba2c361STejun Heo dump_line(&ns, "CPU %-4d: nr_run=%u flags=0x%x cpu_rel=%d ops_qseq=%lu ksync=%lu", 6451bba2c361STejun Heo cpu, rq->scx.nr_running, rq->scx.flags, 6452bba2c361STejun Heo rq->scx.cpu_released, rq->scx.ops_qseq, 6453bba2c361STejun Heo rq->scx.kick_sync); 6454bba2c361STejun Heo dump_line(&ns, " curr=%s[%d] class=%ps", 6455bba2c361STejun Heo rq->curr->comm, rq->curr->pid, 6456bba2c361STejun Heo rq->curr->sched_class); 6457bba2c361STejun Heo if (!cpumask_empty(rq->scx.cpus_to_kick)) 6458bba2c361STejun Heo dump_line(&ns, " cpus_to_kick : %*pb", 6459bba2c361STejun Heo cpumask_pr_args(rq->scx.cpus_to_kick)); 6460bba2c361STejun Heo if (!cpumask_empty(rq->scx.cpus_to_kick_if_idle)) 6461bba2c361STejun Heo dump_line(&ns, " idle_to_kick : %*pb", 6462bba2c361STejun Heo cpumask_pr_args(rq->scx.cpus_to_kick_if_idle)); 6463bba2c361STejun Heo if (!cpumask_empty(rq->scx.cpus_to_preempt)) 6464bba2c361STejun Heo dump_line(&ns, " cpus_to_preempt: %*pb", 6465bba2c361STejun Heo cpumask_pr_args(rq->scx.cpus_to_preempt)); 6466bba2c361STejun Heo if (!cpumask_empty(rq->scx.cpus_to_wait)) 6467bba2c361STejun Heo dump_line(&ns, " cpus_to_wait : %*pb", 6468bba2c361STejun Heo cpumask_pr_args(rq->scx.cpus_to_wait)); 6469bba2c361STejun Heo if (!cpumask_empty(rq->scx.cpus_to_sync)) 6470bba2c361STejun Heo dump_line(&ns, " cpus_to_sync : %*pb", 6471bba2c361STejun Heo cpumask_pr_args(rq->scx.cpus_to_sync)); 6472bba2c361STejun Heo 6473bba2c361STejun Heo used = seq_buf_used(&ns); 6474bba2c361STejun Heo if (SCX_HAS_OP(sch, dump_cpu)) { 6475bba2c361STejun Heo ops_dump_init(&ns, " "); 6476bba2c361STejun Heo SCX_CALL_OP(sch, dump_cpu, rq, dctx, scx_cpu_arg(cpu), idle); 6477bba2c361STejun Heo ops_dump_exit(); 6478bba2c361STejun Heo } 6479bba2c361STejun Heo 6480bba2c361STejun Heo /* 6481bba2c361STejun Heo * If idle && nothing generated by ops.dump_cpu(), there's 6482bba2c361STejun Heo * nothing interesting. Skip. 6483bba2c361STejun Heo */ 6484bba2c361STejun Heo if (idle && used == seq_buf_used(&ns)) 6485bba2c361STejun Heo goto next; 6486bba2c361STejun Heo 6487bba2c361STejun Heo /* 6488bba2c361STejun Heo * $s may already have overflowed when $ns was created. If so, 6489bba2c361STejun Heo * calling commit on it will trigger BUG. 6490bba2c361STejun Heo */ 6491bba2c361STejun Heo if (avail) { 6492bba2c361STejun Heo seq_buf_commit(s, seq_buf_used(&ns)); 6493bba2c361STejun Heo if (seq_buf_has_overflowed(&ns)) 6494bba2c361STejun Heo seq_buf_set_overflow(s); 6495bba2c361STejun Heo } 6496bba2c361STejun Heo 6497bba2c361STejun Heo if (rq->curr->sched_class == &ext_sched_class && 6498bba2c361STejun Heo (dump_all_tasks || scx_task_on_sched(sch, rq->curr))) 6499bba2c361STejun Heo scx_dump_task(sch, s, dctx, rq, rq->curr, '*'); 6500bba2c361STejun Heo 6501bba2c361STejun Heo list_for_each_entry(p, &rq->scx.runnable_list, scx.runnable_node) 6502bba2c361STejun Heo if (dump_all_tasks || scx_task_on_sched(sch, p)) 6503bba2c361STejun Heo scx_dump_task(sch, s, dctx, rq, p, ' '); 6504bba2c361STejun Heo next: 6505bba2c361STejun Heo rq_unlock_irqrestore(rq, &rf); 6506bba2c361STejun Heo } 6507bba2c361STejun Heo 6508bba2c361STejun Heo /* 6509bba2c361STejun Heo * Dump scheduler state. If @dump_all_tasks is true, dump all tasks regardless 6510bba2c361STejun Heo * of which scheduler they belong to. If false, only dump tasks owned by @sch. 6511bba2c361STejun Heo * For SysRq-D dumps, @dump_all_tasks=false since all schedulers are dumped 6512bba2c361STejun Heo * separately. For error dumps, @dump_all_tasks=true since only the failing 6513bba2c361STejun Heo * scheduler is dumped. 6514bba2c361STejun Heo */ 6515bba2c361STejun Heo static void scx_dump_state(struct scx_sched *sch, struct scx_exit_info *ei, 6516bba2c361STejun Heo size_t dump_len, bool dump_all_tasks) 6517bba2c361STejun Heo { 6518bba2c361STejun Heo static const char trunc_marker[] = "\n\n~~~~ TRUNCATED ~~~~\n"; 6519bba2c361STejun Heo struct scx_dump_ctx dctx = { 6520bba2c361STejun Heo .kind = ei->kind, 6521bba2c361STejun Heo .exit_code = ei->exit_code, 6522bba2c361STejun Heo .reason = ei->reason, 6523bba2c361STejun Heo .at_ns = ktime_get_ns(), 6524bba2c361STejun Heo .at_jiffies = jiffies, 6525bba2c361STejun Heo }; 6526bba2c361STejun Heo struct seq_buf s; 6527bba2c361STejun Heo struct scx_event_stats events; 6528bba2c361STejun Heo int cpu; 6529bba2c361STejun Heo 6530bba2c361STejun Heo guard(raw_spinlock_irqsave)(&scx_dump_lock); 6531bba2c361STejun Heo 6532bba2c361STejun Heo if (sch->dump_disabled) 6533bba2c361STejun Heo return; 6534bba2c361STejun Heo 6535bba2c361STejun Heo seq_buf_init(&s, ei->dump, dump_len); 6536bba2c361STejun Heo 6537bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 6538bba2c361STejun Heo if (sch->level == 0) 6539bba2c361STejun Heo dump_line(&s, "%s: root", sch->ops.name); 6540bba2c361STejun Heo else 6541bba2c361STejun Heo dump_line(&s, "%s: sub%d-%llu %s", 6542bba2c361STejun Heo sch->ops.name, sch->level, sch->ops.sub_cgroup_id, 6543bba2c361STejun Heo sch->cgrp_path); 6544bba2c361STejun Heo #endif 6545bba2c361STejun Heo if (ei->kind == SCX_EXIT_NONE) { 6546bba2c361STejun Heo dump_line(&s, "Debug dump triggered by %s", ei->reason); 6547bba2c361STejun Heo } else { 6548bba2c361STejun Heo if (ei->exit_cpu >= 0) 6549bba2c361STejun Heo dump_line(&s, "%s[%d] triggered exit kind %d on CPU %d:", 6550bba2c361STejun Heo current->comm, current->pid, ei->kind, 6551bba2c361STejun Heo ei->exit_cpu); 6552bba2c361STejun Heo else 6553bba2c361STejun Heo dump_line(&s, "%s[%d] triggered exit kind %d:", 6554bba2c361STejun Heo current->comm, current->pid, ei->kind); 6555bba2c361STejun Heo dump_line(&s, " %s (%s)", ei->reason, ei->msg); 6556bba2c361STejun Heo dump_newline(&s); 6557bba2c361STejun Heo dump_line(&s, "Backtrace:"); 6558bba2c361STejun Heo dump_stack_trace(&s, " ", ei->bt, ei->bt_len); 6559bba2c361STejun Heo } 6560bba2c361STejun Heo 6561bba2c361STejun Heo if (SCX_HAS_OP(sch, dump)) { 6562bba2c361STejun Heo ops_dump_init(&s, ""); 6563bba2c361STejun Heo SCX_CALL_OP(sch, dump, NULL, &dctx); 6564bba2c361STejun Heo ops_dump_exit(); 6565bba2c361STejun Heo } 6566bba2c361STejun Heo 6567bba2c361STejun Heo dump_newline(&s); 6568bba2c361STejun Heo dump_line(&s, "CPU states"); 6569bba2c361STejun Heo dump_line(&s, "----------"); 6570bba2c361STejun Heo 6571bba2c361STejun Heo /* 6572bba2c361STejun Heo * Dump the exit CPU first so it isn't lost to dump truncation, then 6573bba2c361STejun Heo * walk the rest in order, skipping the one already dumped. 6574bba2c361STejun Heo */ 6575bba2c361STejun Heo if (ei->exit_cpu >= 0) 6576bba2c361STejun Heo scx_dump_cpu(sch, &s, &dctx, ei->exit_cpu, dump_all_tasks); 6577bba2c361STejun Heo for_each_possible_cpu(cpu) { 6578bba2c361STejun Heo if (cpu != ei->exit_cpu) 6579bba2c361STejun Heo scx_dump_cpu(sch, &s, &dctx, cpu, dump_all_tasks); 6580bba2c361STejun Heo } 6581bba2c361STejun Heo 6582bba2c361STejun Heo dump_newline(&s); 6583bba2c361STejun Heo dump_line(&s, "Event counters"); 6584bba2c361STejun Heo dump_line(&s, "--------------"); 6585bba2c361STejun Heo 6586bba2c361STejun Heo scx_read_events(sch, &events); 6587bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_SELECT_CPU_FALLBACK); 6588bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE); 6589bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_DISPATCH_KEEP_LAST); 6590bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_ENQ_SKIP_EXITING); 6591bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED); 6592bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_REENQ_IMMED); 6593bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_REENQ_LOCAL_REPEAT); 6594bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_REFILL_SLICE_DFL); 6595bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_BYPASS_DURATION); 6596bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_BYPASS_DISPATCH); 6597bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_BYPASS_ACTIVATE); 6598bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_INSERT_NOT_OWNED); 6599bba2c361STejun Heo scx_dump_event(s, &events, SCX_EV_SUB_BYPASS_DISPATCH); 6600bba2c361STejun Heo 6601bba2c361STejun Heo if (seq_buf_has_overflowed(&s) && dump_len >= sizeof(trunc_marker)) 6602bba2c361STejun Heo memcpy(ei->dump + dump_len - sizeof(trunc_marker), 6603bba2c361STejun Heo trunc_marker, sizeof(trunc_marker)); 6604bba2c361STejun Heo } 6605bba2c361STejun Heo 6606bba2c361STejun Heo static void scx_disable_irq_workfn(struct irq_work *irq_work) 6607bba2c361STejun Heo { 6608bba2c361STejun Heo struct scx_sched *sch = container_of(irq_work, struct scx_sched, disable_irq_work); 6609bba2c361STejun Heo struct scx_exit_info *ei = sch->exit_info; 6610bba2c361STejun Heo 6611bba2c361STejun Heo if (ei->kind >= SCX_EXIT_ERROR) 6612bba2c361STejun Heo scx_dump_state(sch, ei, sch->ops.exit_dump_len, true); 6613bba2c361STejun Heo 6614bba2c361STejun Heo kthread_queue_work(sch->helper, &sch->disable_work); 6615bba2c361STejun Heo } 6616bba2c361STejun Heo 6617bba2c361STejun Heo bool scx_vexit(struct scx_sched *sch, 6618bba2c361STejun Heo enum scx_exit_kind kind, s64 exit_code, s32 exit_cpu, 6619bba2c361STejun Heo const char *fmt, va_list args) 6620bba2c361STejun Heo { 6621bba2c361STejun Heo struct scx_exit_info *ei = sch->exit_info; 6622bba2c361STejun Heo 6623bba2c361STejun Heo guard(preempt)(); 6624bba2c361STejun Heo 6625bba2c361STejun Heo if (!scx_claim_exit(sch, kind)) 6626bba2c361STejun Heo return false; 6627bba2c361STejun Heo 6628bba2c361STejun Heo ei->exit_code = exit_code; 6629bba2c361STejun Heo #ifdef CONFIG_STACKTRACE 6630bba2c361STejun Heo if (kind >= SCX_EXIT_ERROR) 6631bba2c361STejun Heo ei->bt_len = stack_trace_save(ei->bt, SCX_EXIT_BT_LEN, 1); 6632bba2c361STejun Heo #endif 6633bba2c361STejun Heo vscnprintf(ei->msg, SCX_EXIT_MSG_LEN, fmt, args); 6634bba2c361STejun Heo 6635bba2c361STejun Heo /* 6636bba2c361STejun Heo * Set ei->kind and ->reason for scx_dump_state(). They'll be set again 6637bba2c361STejun Heo * in scx_disable_workfn(). 6638bba2c361STejun Heo */ 6639bba2c361STejun Heo ei->kind = kind; 6640bba2c361STejun Heo ei->reason = scx_exit_reason(ei->kind); 6641bba2c361STejun Heo ei->exit_cpu = exit_cpu; 6642bba2c361STejun Heo 6643bba2c361STejun Heo irq_work_queue(&sch->disable_irq_work); 6644bba2c361STejun Heo return true; 6645bba2c361STejun Heo } 6646bba2c361STejun Heo 6647bba2c361STejun Heo static int alloc_kick_syncs(void) 6648bba2c361STejun Heo { 6649bba2c361STejun Heo int cpu; 6650bba2c361STejun Heo 6651bba2c361STejun Heo /* 6652bba2c361STejun Heo * Allocate per-CPU arrays sized by nr_cpu_ids. Use kvzalloc as size 6653bba2c361STejun Heo * can exceed percpu allocator limits on large machines. 6654bba2c361STejun Heo */ 6655bba2c361STejun Heo for_each_possible_cpu(cpu) { 6656bba2c361STejun Heo struct scx_kick_syncs **ksyncs = per_cpu_ptr(&scx_kick_syncs, cpu); 6657bba2c361STejun Heo struct scx_kick_syncs *new_ksyncs; 6658bba2c361STejun Heo 6659bba2c361STejun Heo WARN_ON_ONCE(rcu_access_pointer(*ksyncs)); 6660bba2c361STejun Heo 6661bba2c361STejun Heo new_ksyncs = kvzalloc_node(struct_size(new_ksyncs, syncs, nr_cpu_ids), 6662bba2c361STejun Heo GFP_KERNEL, cpu_to_node(cpu)); 6663bba2c361STejun Heo if (!new_ksyncs) { 6664bba2c361STejun Heo free_kick_syncs(); 6665bba2c361STejun Heo return -ENOMEM; 6666bba2c361STejun Heo } 6667bba2c361STejun Heo 6668bba2c361STejun Heo rcu_assign_pointer(*ksyncs, new_ksyncs); 6669bba2c361STejun Heo } 6670bba2c361STejun Heo 6671bba2c361STejun Heo return 0; 6672bba2c361STejun Heo } 6673bba2c361STejun Heo 6674bba2c361STejun Heo static void free_pnode(struct scx_sched_pnode *pnode) 6675bba2c361STejun Heo { 6676bba2c361STejun Heo if (!pnode) 6677bba2c361STejun Heo return; 6678bba2c361STejun Heo exit_dsq(&pnode->global_dsq); 6679bba2c361STejun Heo kfree(pnode); 6680bba2c361STejun Heo } 6681bba2c361STejun Heo 6682bba2c361STejun Heo static struct scx_sched_pnode *alloc_pnode(struct scx_sched *sch, int node) 6683bba2c361STejun Heo { 6684bba2c361STejun Heo struct scx_sched_pnode *pnode; 6685bba2c361STejun Heo 6686bba2c361STejun Heo pnode = kzalloc_node(sizeof(*pnode), GFP_KERNEL, node); 6687bba2c361STejun Heo if (!pnode) 6688bba2c361STejun Heo return NULL; 6689bba2c361STejun Heo 6690bba2c361STejun Heo if (init_dsq(&pnode->global_dsq, SCX_DSQ_GLOBAL, sch)) { 6691bba2c361STejun Heo kfree(pnode); 6692bba2c361STejun Heo return NULL; 6693bba2c361STejun Heo } 6694bba2c361STejun Heo 6695bba2c361STejun Heo return pnode; 6696bba2c361STejun Heo } 6697bba2c361STejun Heo 6698bba2c361STejun Heo /* 6699bba2c361STejun Heo * scx_enable() is offloaded to a dedicated system-wide RT kthread to avoid 6700bba2c361STejun Heo * starvation. During the READY -> ENABLED task switching loop, the calling 6701bba2c361STejun Heo * thread's sched_class gets switched from fair to ext. As fair has higher 6702bba2c361STejun Heo * priority than ext, the calling thread can be indefinitely starved under 6703bba2c361STejun Heo * fair-class saturation, leading to a system hang. 6704bba2c361STejun Heo */ 6705bba2c361STejun Heo struct scx_enable_cmd { 6706bba2c361STejun Heo struct kthread_work work; 6707bba2c361STejun Heo union { 6708bba2c361STejun Heo struct sched_ext_ops *ops; 6709bba2c361STejun Heo struct sched_ext_ops_cid *ops_cid; 6710bba2c361STejun Heo }; 6711bba2c361STejun Heo bool is_cid_type; 6712bba2c361STejun Heo struct bpf_map *arena_map; /* arena ref to transfer to sch */ 6713bba2c361STejun Heo int ret; 6714bba2c361STejun Heo }; 6715bba2c361STejun Heo 6716bba2c361STejun Heo /* 6717bba2c361STejun Heo * Allocate and initialize a new scx_sched. @cgrp's reference is always 6718bba2c361STejun Heo * consumed whether the function succeeds or fails. 6719bba2c361STejun Heo */ 6720bba2c361STejun Heo static struct scx_sched *scx_alloc_and_add_sched(struct scx_enable_cmd *cmd, 6721bba2c361STejun Heo struct cgroup *cgrp, 6722bba2c361STejun Heo struct scx_sched *parent) 6723bba2c361STejun Heo { 6724bba2c361STejun Heo struct sched_ext_ops *ops = cmd->ops; 6725bba2c361STejun Heo struct scx_sched *sch; 6726bba2c361STejun Heo s32 level = parent ? parent->level + 1 : 0; 6727bba2c361STejun Heo s32 node, cpu, ret, bypass_fail_cpu = nr_cpu_ids; 6728bba2c361STejun Heo 6729bba2c361STejun Heo sch = kzalloc_flex(*sch, ancestors, level + 1); 6730bba2c361STejun Heo if (!sch) { 6731bba2c361STejun Heo ret = -ENOMEM; 6732bba2c361STejun Heo goto err_put_cgrp; 6733bba2c361STejun Heo } 6734bba2c361STejun Heo 6735bba2c361STejun Heo sch->exit_info = alloc_exit_info(ops->exit_dump_len); 6736bba2c361STejun Heo if (!sch->exit_info) { 6737bba2c361STejun Heo ret = -ENOMEM; 6738bba2c361STejun Heo goto err_free_sch; 6739bba2c361STejun Heo } 6740bba2c361STejun Heo 6741bba2c361STejun Heo ret = rhashtable_init(&sch->dsq_hash, &dsq_hash_params); 6742bba2c361STejun Heo if (ret < 0) 6743bba2c361STejun Heo goto err_free_ei; 6744bba2c361STejun Heo 6745bba2c361STejun Heo sch->pnode = kzalloc_objs(sch->pnode[0], nr_node_ids); 6746bba2c361STejun Heo if (!sch->pnode) { 6747bba2c361STejun Heo ret = -ENOMEM; 6748bba2c361STejun Heo goto err_free_hash; 6749bba2c361STejun Heo } 6750bba2c361STejun Heo 6751bba2c361STejun Heo for_each_node_state(node, N_POSSIBLE) { 6752bba2c361STejun Heo sch->pnode[node] = alloc_pnode(sch, node); 6753bba2c361STejun Heo if (!sch->pnode[node]) { 6754bba2c361STejun Heo ret = -ENOMEM; 6755bba2c361STejun Heo goto err_free_pnode; 6756bba2c361STejun Heo } 6757bba2c361STejun Heo } 6758bba2c361STejun Heo 6759bba2c361STejun Heo sch->dsp_max_batch = ops->dispatch_max_batch ?: SCX_DSP_DFL_MAX_BATCH; 6760bba2c361STejun Heo sch->pcpu = __alloc_percpu(struct_size_t(struct scx_sched_pcpu, 6761bba2c361STejun Heo dsp_ctx.buf, sch->dsp_max_batch), 6762bba2c361STejun Heo __alignof__(struct scx_sched_pcpu)); 6763bba2c361STejun Heo if (!sch->pcpu) { 6764bba2c361STejun Heo ret = -ENOMEM; 6765bba2c361STejun Heo goto err_free_pnode; 6766bba2c361STejun Heo } 6767bba2c361STejun Heo 6768bba2c361STejun Heo for_each_possible_cpu(cpu) { 6769bba2c361STejun Heo ret = init_dsq(bypass_dsq(sch, cpu), SCX_DSQ_BYPASS, sch); 6770bba2c361STejun Heo if (ret) { 6771bba2c361STejun Heo bypass_fail_cpu = cpu; 6772bba2c361STejun Heo goto err_free_pcpu; 6773bba2c361STejun Heo } 6774bba2c361STejun Heo } 6775bba2c361STejun Heo 6776bba2c361STejun Heo for_each_possible_cpu(cpu) { 6777bba2c361STejun Heo struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 6778bba2c361STejun Heo 6779bba2c361STejun Heo pcpu->sch = sch; 6780bba2c361STejun Heo INIT_LIST_HEAD(&pcpu->deferred_reenq_local.node); 6781bba2c361STejun Heo } 6782bba2c361STejun Heo 6783bba2c361STejun Heo sch->helper = kthread_run_worker(0, "sched_ext_helper"); 6784bba2c361STejun Heo if (IS_ERR(sch->helper)) { 6785bba2c361STejun Heo ret = PTR_ERR(sch->helper); 6786bba2c361STejun Heo goto err_free_pcpu; 6787bba2c361STejun Heo } 6788bba2c361STejun Heo 6789bba2c361STejun Heo sched_set_fifo(sch->helper->task); 6790bba2c361STejun Heo 6791bba2c361STejun Heo if (parent) 6792bba2c361STejun Heo memcpy(sch->ancestors, parent->ancestors, 6793bba2c361STejun Heo level * sizeof(parent->ancestors[0])); 6794bba2c361STejun Heo sch->ancestors[level] = sch; 6795bba2c361STejun Heo sch->level = level; 6796bba2c361STejun Heo 6797bba2c361STejun Heo if (ops->timeout_ms) 6798bba2c361STejun Heo sch->watchdog_timeout = msecs_to_jiffies(ops->timeout_ms); 6799bba2c361STejun Heo else 6800bba2c361STejun Heo sch->watchdog_timeout = SCX_WATCHDOG_MAX_TIMEOUT; 6801bba2c361STejun Heo 6802bba2c361STejun Heo sch->slice_dfl = SCX_SLICE_DFL; 6803bba2c361STejun Heo atomic_set(&sch->exit_kind, SCX_EXIT_NONE); 6804bba2c361STejun Heo sch->disable_irq_work = IRQ_WORK_INIT_HARD(scx_disable_irq_workfn); 6805bba2c361STejun Heo kthread_init_work(&sch->disable_work, scx_disable_workfn); 6806bba2c361STejun Heo timer_setup(&sch->bypass_lb_timer, scx_bypass_lb_timerfn, 0); 6807bba2c361STejun Heo 6808bba2c361STejun Heo if (!alloc_cpumask_var(&sch->bypass_lb_donee_cpumask, GFP_KERNEL)) { 6809bba2c361STejun Heo ret = -ENOMEM; 6810bba2c361STejun Heo goto err_stop_helper; 6811bba2c361STejun Heo } 6812bba2c361STejun Heo if (!alloc_cpumask_var(&sch->bypass_lb_resched_cpumask, GFP_KERNEL)) { 6813bba2c361STejun Heo ret = -ENOMEM; 6814bba2c361STejun Heo goto err_free_lb_cpumask; 6815bba2c361STejun Heo } 6816bba2c361STejun Heo /* 6817bba2c361STejun Heo * Copy ops through the right union view. For cid-form the source is 6818bba2c361STejun Heo * struct sched_ext_ops_cid which lacks the trailing cpu_acquire/ 6819bba2c361STejun Heo * cpu_release; those stay zero from kzalloc. 6820bba2c361STejun Heo */ 6821bba2c361STejun Heo if (cmd->is_cid_type) { 6822bba2c361STejun Heo sch->ops_cid = *cmd->ops_cid; 6823bba2c361STejun Heo sch->is_cid_type = true; 6824bba2c361STejun Heo } else { 6825bba2c361STejun Heo sch->ops = *cmd->ops; 6826bba2c361STejun Heo } 6827bba2c361STejun Heo 6828bba2c361STejun Heo rcu_assign_pointer(ops->priv, sch); 6829bba2c361STejun Heo 6830bba2c361STejun Heo sch->kobj.kset = scx_kset; 6831bba2c361STejun Heo INIT_LIST_HEAD(&sch->all); 6832bba2c361STejun Heo 6833bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 6834bba2c361STejun Heo char *buf = kzalloc(PATH_MAX, GFP_KERNEL); 6835bba2c361STejun Heo if (!buf) { 6836bba2c361STejun Heo ret = -ENOMEM; 6837bba2c361STejun Heo goto err_free_lb_resched; 6838bba2c361STejun Heo } 6839bba2c361STejun Heo cgroup_path(cgrp, buf, PATH_MAX); 6840bba2c361STejun Heo sch->cgrp_path = kstrdup(buf, GFP_KERNEL); 6841bba2c361STejun Heo kfree(buf); 6842bba2c361STejun Heo if (!sch->cgrp_path) { 6843bba2c361STejun Heo ret = -ENOMEM; 6844bba2c361STejun Heo goto err_free_lb_resched; 6845bba2c361STejun Heo } 6846bba2c361STejun Heo 6847bba2c361STejun Heo sch->cgrp = cgrp; 6848bba2c361STejun Heo INIT_LIST_HEAD(&sch->children); 6849bba2c361STejun Heo INIT_LIST_HEAD(&sch->sibling); 6850bba2c361STejun Heo 6851bba2c361STejun Heo if (parent) 6852bba2c361STejun Heo ret = kobject_init_and_add(&sch->kobj, &scx_ktype, 6853bba2c361STejun Heo &parent->sub_kset->kobj, 6854bba2c361STejun Heo "sub-%llu", cgroup_id(cgrp)); 6855bba2c361STejun Heo else 6856bba2c361STejun Heo ret = kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root"); 6857bba2c361STejun Heo 6858bba2c361STejun Heo if (ret < 0) { 6859bba2c361STejun Heo RCU_INIT_POINTER(ops->priv, NULL); 6860bba2c361STejun Heo kobject_put(&sch->kobj); 6861bba2c361STejun Heo return ERR_PTR(ret); 6862bba2c361STejun Heo } 6863bba2c361STejun Heo 6864bba2c361STejun Heo if (ops->sub_attach) { 6865bba2c361STejun Heo sch->sub_kset = kset_create_and_add("sub", NULL, &sch->kobj); 6866bba2c361STejun Heo if (!sch->sub_kset) { 6867bba2c361STejun Heo RCU_INIT_POINTER(ops->priv, NULL); 6868bba2c361STejun Heo kobject_put(&sch->kobj); 6869bba2c361STejun Heo return ERR_PTR(-ENOMEM); 6870bba2c361STejun Heo } 6871bba2c361STejun Heo } 6872bba2c361STejun Heo #else /* CONFIG_EXT_SUB_SCHED */ 6873bba2c361STejun Heo ret = kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root"); 6874bba2c361STejun Heo if (ret < 0) { 6875bba2c361STejun Heo RCU_INIT_POINTER(ops->priv, NULL); 6876bba2c361STejun Heo kobject_put(&sch->kobj); 6877bba2c361STejun Heo return ERR_PTR(ret); 6878bba2c361STejun Heo } 6879bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 6880bba2c361STejun Heo 6881bba2c361STejun Heo /* 6882bba2c361STejun Heo * Consume the arena_map ref bpf_scx_reg_cid() took. Defer to here so 6883bba2c361STejun Heo * earlier failure paths leave cmd->arena_map set and bpf_scx_reg_cid 6884bba2c361STejun Heo * drops the ref. After this point, sch owns the ref and any cleanup 6885bba2c361STejun Heo * runs through scx_sched_free_rcu_work() which puts it. 6886bba2c361STejun Heo */ 6887bba2c361STejun Heo sch->arena_map = cmd->arena_map; 6888bba2c361STejun Heo /* BPF arena is only available on MMU && 64BIT */ 6889bba2c361STejun Heo #if defined(CONFIG_MMU) && defined(CONFIG_64BIT) 6890bba2c361STejun Heo if (sch->arena_map) 6891bba2c361STejun Heo sch->arena_kern_base = bpf_arena_map_kern_vm_start(sch->arena_map); 6892bba2c361STejun Heo #endif 6893bba2c361STejun Heo cmd->arena_map = NULL; 6894bba2c361STejun Heo return sch; 6895bba2c361STejun Heo 6896bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 6897bba2c361STejun Heo err_free_lb_resched: 6898bba2c361STejun Heo RCU_INIT_POINTER(ops->priv, NULL); 6899bba2c361STejun Heo free_cpumask_var(sch->bypass_lb_resched_cpumask); 6900bba2c361STejun Heo #endif 6901bba2c361STejun Heo err_free_lb_cpumask: 6902bba2c361STejun Heo free_cpumask_var(sch->bypass_lb_donee_cpumask); 6903bba2c361STejun Heo err_stop_helper: 6904bba2c361STejun Heo kthread_destroy_worker(sch->helper); 6905bba2c361STejun Heo err_free_pcpu: 6906bba2c361STejun Heo for_each_possible_cpu(cpu) { 6907bba2c361STejun Heo if (cpu == bypass_fail_cpu) 6908bba2c361STejun Heo break; 6909bba2c361STejun Heo exit_dsq(bypass_dsq(sch, cpu)); 6910bba2c361STejun Heo } 6911bba2c361STejun Heo free_percpu(sch->pcpu); 6912bba2c361STejun Heo err_free_pnode: 6913bba2c361STejun Heo for_each_node_state(node, N_POSSIBLE) 6914bba2c361STejun Heo free_pnode(sch->pnode[node]); 6915bba2c361STejun Heo kfree(sch->pnode); 6916bba2c361STejun Heo err_free_hash: 6917bba2c361STejun Heo rhashtable_free_and_destroy(&sch->dsq_hash, NULL, NULL); 6918bba2c361STejun Heo err_free_ei: 6919bba2c361STejun Heo free_exit_info(sch->exit_info); 6920bba2c361STejun Heo err_free_sch: 6921bba2c361STejun Heo kfree(sch); 6922bba2c361STejun Heo err_put_cgrp: 6923bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 6924bba2c361STejun Heo cgroup_put(cgrp); 6925bba2c361STejun Heo #endif 6926bba2c361STejun Heo return ERR_PTR(ret); 6927bba2c361STejun Heo } 6928bba2c361STejun Heo 6929bba2c361STejun Heo static int check_hotplug_seq(struct scx_sched *sch, 6930bba2c361STejun Heo const struct sched_ext_ops *ops) 6931bba2c361STejun Heo { 6932bba2c361STejun Heo unsigned long long global_hotplug_seq; 6933bba2c361STejun Heo 6934bba2c361STejun Heo /* 6935bba2c361STejun Heo * If a hotplug event has occurred between when a scheduler was 6936bba2c361STejun Heo * initialized, and when we were able to attach, exit and notify user 6937bba2c361STejun Heo * space about it. 6938bba2c361STejun Heo */ 6939bba2c361STejun Heo if (ops->hotplug_seq) { 6940bba2c361STejun Heo global_hotplug_seq = atomic_long_read(&scx_hotplug_seq); 6941bba2c361STejun Heo if (ops->hotplug_seq != global_hotplug_seq) { 6942bba2c361STejun Heo scx_exit(sch, SCX_EXIT_UNREG_KERN, 6943bba2c361STejun Heo SCX_ECODE_ACT_RESTART | SCX_ECODE_RSN_HOTPLUG, 6944bba2c361STejun Heo "expected hotplug seq %llu did not match actual %llu", 6945bba2c361STejun Heo ops->hotplug_seq, global_hotplug_seq); 6946bba2c361STejun Heo return -EBUSY; 6947bba2c361STejun Heo } 6948bba2c361STejun Heo } 6949bba2c361STejun Heo 6950bba2c361STejun Heo return 0; 6951bba2c361STejun Heo } 6952bba2c361STejun Heo 6953bba2c361STejun Heo static int validate_ops(struct scx_sched *sch, const struct sched_ext_ops *ops) 6954bba2c361STejun Heo { 6955bba2c361STejun Heo /* 6956bba2c361STejun Heo * It doesn't make sense to specify the SCX_OPS_ENQ_LAST flag if the 6957bba2c361STejun Heo * ops.enqueue() callback isn't implemented. 6958bba2c361STejun Heo */ 6959bba2c361STejun Heo if ((ops->flags & SCX_OPS_ENQ_LAST) && !ops->enqueue) { 6960bba2c361STejun Heo scx_error(sch, "SCX_OPS_ENQ_LAST requires ops.enqueue() to be implemented"); 6961bba2c361STejun Heo return -EINVAL; 6962bba2c361STejun Heo } 6963bba2c361STejun Heo 6964bba2c361STejun Heo /* 6965bba2c361STejun Heo * SCX_OPS_TID_TO_TASK is enabled by the root scheduler. A sub-sched 6966bba2c361STejun Heo * may set it to declare a dependency; reject if the root hasn't 6967bba2c361STejun Heo * enabled it. 6968bba2c361STejun Heo */ 6969bba2c361STejun Heo if ((ops->flags & SCX_OPS_TID_TO_TASK) && scx_parent(sch) && 6970bba2c361STejun Heo !(scx_root->ops.flags & SCX_OPS_TID_TO_TASK)) { 6971bba2c361STejun Heo scx_error(sch, "SCX_OPS_TID_TO_TASK requires root scheduler to enable it"); 6972bba2c361STejun Heo return -EINVAL; 6973bba2c361STejun Heo } 6974bba2c361STejun Heo 6975bba2c361STejun Heo /* 6976bba2c361STejun Heo * SCX_OPS_BUILTIN_IDLE_PER_NODE requires built-in CPU idle 6977bba2c361STejun Heo * selection policy to be enabled. 6978bba2c361STejun Heo */ 6979bba2c361STejun Heo if ((ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE) && 6980bba2c361STejun Heo (ops->update_idle && !(ops->flags & SCX_OPS_KEEP_BUILTIN_IDLE))) { 6981bba2c361STejun Heo scx_error(sch, "SCX_OPS_BUILTIN_IDLE_PER_NODE requires CPU idle selection enabled"); 6982bba2c361STejun Heo return -EINVAL; 6983bba2c361STejun Heo } 6984bba2c361STejun Heo 6985bba2c361STejun Heo /* 6986bba2c361STejun Heo * cid-form's struct is shorter and doesn't include the cpu_acquire / 6987bba2c361STejun Heo * cpu_release tail; reading those fields off a cid-form @ops would 6988bba2c361STejun Heo * run past the BPF allocation. Skip for cid-form. 6989bba2c361STejun Heo */ 6990bba2c361STejun Heo if (!sch->is_cid_type && (ops->cpu_acquire || ops->cpu_release)) 6991bba2c361STejun Heo pr_warn("ops->cpu_acquire/release() are deprecated, use sched_switch TP instead\n"); 6992bba2c361STejun Heo 6993bba2c361STejun Heo /* 6994bba2c361STejun Heo * Sub-scheduler support is tied to the cid-form struct_ops. A sub-sched 6995bba2c361STejun Heo * attaches through a cid-form-only interface (sub_attach/sub_detach), 6996bba2c361STejun Heo * and a root that accepts sub-scheds must expose cid-form state to 6997bba2c361STejun Heo * them. Reject cpu-form schedulers on either side. 6998bba2c361STejun Heo */ 6999bba2c361STejun Heo if (!sch->is_cid_type) { 7000bba2c361STejun Heo if (scx_parent(sch)) { 7001bba2c361STejun Heo scx_error(sch, "sub-sched requires cid-form struct_ops"); 7002bba2c361STejun Heo return -EINVAL; 7003bba2c361STejun Heo } 7004bba2c361STejun Heo if (ops->sub_attach || ops->sub_detach) { 7005bba2c361STejun Heo scx_error(sch, "sub_attach/sub_detach requires cid-form struct_ops"); 7006bba2c361STejun Heo return -EINVAL; 7007bba2c361STejun Heo } 7008bba2c361STejun Heo } 7009bba2c361STejun Heo 7010bba2c361STejun Heo return 0; 7011bba2c361STejun Heo } 7012bba2c361STejun Heo 7013bba2c361STejun Heo static void scx_root_enable_workfn(struct kthread_work *work) 7014bba2c361STejun Heo { 7015bba2c361STejun Heo struct scx_enable_cmd *cmd = container_of(work, struct scx_enable_cmd, work); 7016bba2c361STejun Heo struct sched_ext_ops *ops = cmd->ops; 7017bba2c361STejun Heo struct cgroup *cgrp = root_cgroup(); 7018bba2c361STejun Heo struct scx_sched *sch; 7019bba2c361STejun Heo struct scx_task_iter sti; 7020bba2c361STejun Heo struct task_struct *p; 7021bba2c361STejun Heo int i, cpu, ret; 7022bba2c361STejun Heo 7023bba2c361STejun Heo mutex_lock(&scx_enable_mutex); 7024bba2c361STejun Heo 7025bba2c361STejun Heo if (scx_enable_state() != SCX_DISABLED) { 7026bba2c361STejun Heo ret = -EBUSY; 7027bba2c361STejun Heo goto err_unlock; 7028bba2c361STejun Heo } 7029bba2c361STejun Heo 7030bba2c361STejun Heo /* 7031bba2c361STejun Heo * @ops->priv binds @ops to its scx_sched instance. It is set here by 7032bba2c361STejun Heo * scx_alloc_and_add_sched() and cleared at the tail of bpf_scx_unreg(), 7033bba2c361STejun Heo * which runs after scx_root_disable() has dropped scx_enable_mutex. If 7034bba2c361STejun Heo * it's still non-NULL here, a previous attachment on @ops has not 7035bba2c361STejun Heo * finished tearing down; proceeding would let the in-flight unreg's 7036bba2c361STejun Heo * RCU_INIT_POINTER(NULL) clobber the @ops->priv we are about to assign. 7037bba2c361STejun Heo */ 7038bba2c361STejun Heo if (rcu_access_pointer(ops->priv)) { 7039bba2c361STejun Heo ret = -EBUSY; 7040bba2c361STejun Heo goto err_unlock; 7041bba2c361STejun Heo } 7042bba2c361STejun Heo 7043bba2c361STejun Heo ret = alloc_kick_syncs(); 7044bba2c361STejun Heo if (ret) 7045bba2c361STejun Heo goto err_unlock; 7046bba2c361STejun Heo 7047bba2c361STejun Heo if (ops->flags & SCX_OPS_TID_TO_TASK) { 7048bba2c361STejun Heo ret = rhashtable_init(&scx_tid_hash, &scx_tid_hash_params); 7049bba2c361STejun Heo if (ret) 7050bba2c361STejun Heo goto err_free_ksyncs; 7051bba2c361STejun Heo } 7052bba2c361STejun Heo 7053bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 7054bba2c361STejun Heo cgroup_get(cgrp); 7055bba2c361STejun Heo #endif 7056bba2c361STejun Heo sch = scx_alloc_and_add_sched(cmd, cgrp, NULL); 7057bba2c361STejun Heo if (IS_ERR(sch)) { 7058bba2c361STejun Heo ret = PTR_ERR(sch); 7059bba2c361STejun Heo goto err_free_tid_hash; 7060bba2c361STejun Heo } 7061bba2c361STejun Heo 7062bba2c361STejun Heo if (sch->is_cid_type) 7063bba2c361STejun Heo static_branch_enable(&__scx_is_cid_type); 7064bba2c361STejun Heo 7065bba2c361STejun Heo /* 7066bba2c361STejun Heo * Transition to ENABLING and clear exit info to arm the disable path. 7067bba2c361STejun Heo * Failure triggers full disabling from here on. 7068bba2c361STejun Heo */ 7069bba2c361STejun Heo WARN_ON_ONCE(scx_set_enable_state(SCX_ENABLING) != SCX_DISABLED); 7070bba2c361STejun Heo WARN_ON_ONCE(scx_root); 7071bba2c361STejun Heo 7072bba2c361STejun Heo atomic_long_set(&scx_nr_rejected, 0); 7073bba2c361STejun Heo 7074bba2c361STejun Heo for_each_possible_cpu(cpu) { 7075bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 7076bba2c361STejun Heo 7077bba2c361STejun Heo rq->scx.local_dsq.sched = sch; 7078bba2c361STejun Heo rq->scx.cpuperf_target = SCX_CPUPERF_ONE; 7079bba2c361STejun Heo } 7080bba2c361STejun Heo 7081bba2c361STejun Heo /* 7082bba2c361STejun Heo * Keep CPUs stable during enable so that the BPF scheduler can track 7083bba2c361STejun Heo * online CPUs by watching ->on/offline_cpu() after ->init(). 7084bba2c361STejun Heo */ 7085bba2c361STejun Heo cpus_read_lock(); 7086bba2c361STejun Heo 7087bba2c361STejun Heo /* 7088bba2c361STejun Heo * Build the cid mapping before publishing scx_root. The cid kfuncs 7089bba2c361STejun Heo * dereference the cid arrays unconditionally once scx_prog_sched() 7090bba2c361STejun Heo * returns non-NULL; the rcu_assign_pointer() below pairs with their 7091bba2c361STejun Heo * rcu_dereference() to make the populated arrays visible. 7092bba2c361STejun Heo */ 7093bba2c361STejun Heo ret = scx_cid_init(sch); 7094bba2c361STejun Heo if (ret) { 7095bba2c361STejun Heo cpus_read_unlock(); 7096bba2c361STejun Heo goto err_disable; 7097bba2c361STejun Heo } 7098bba2c361STejun Heo 7099bba2c361STejun Heo /* 7100bba2c361STejun Heo * Make the scheduler instance visible. Must be inside cpus_read_lock(). 7101bba2c361STejun Heo * See handle_hotplug(). 7102bba2c361STejun Heo */ 7103bba2c361STejun Heo rcu_assign_pointer(scx_root, sch); 7104bba2c361STejun Heo 7105bba2c361STejun Heo ret = scx_link_sched(sch); 7106bba2c361STejun Heo if (ret) { 7107bba2c361STejun Heo cpus_read_unlock(); 7108bba2c361STejun Heo goto err_disable; 7109bba2c361STejun Heo } 7110bba2c361STejun Heo 7111bba2c361STejun Heo scx_idle_enable(ops); 7112bba2c361STejun Heo 7113bba2c361STejun Heo if (sch->ops.init) { 7114bba2c361STejun Heo ret = SCX_CALL_OP_RET(sch, init, NULL); 7115bba2c361STejun Heo if (ret) { 7116bba2c361STejun Heo ret = ops_sanitize_err(sch, "init", ret); 7117bba2c361STejun Heo cpus_read_unlock(); 7118bba2c361STejun Heo scx_error(sch, "ops.init() failed (%d)", ret); 7119bba2c361STejun Heo goto err_disable; 7120bba2c361STejun Heo } 7121bba2c361STejun Heo sch->exit_info->flags |= SCX_EFLAG_INITIALIZED; 7122bba2c361STejun Heo } 7123bba2c361STejun Heo 7124bba2c361STejun Heo ret = scx_arena_pool_init(sch); 7125bba2c361STejun Heo if (ret) { 7126bba2c361STejun Heo cpus_read_unlock(); 7127bba2c361STejun Heo goto err_disable; 7128bba2c361STejun Heo } 7129bba2c361STejun Heo 7130bba2c361STejun Heo ret = scx_set_cmask_scratch_alloc(sch); 7131bba2c361STejun Heo if (ret) { 7132bba2c361STejun Heo cpus_read_unlock(); 7133bba2c361STejun Heo goto err_disable; 7134bba2c361STejun Heo } 7135bba2c361STejun Heo 7136bba2c361STejun Heo for (i = SCX_OPI_CPU_HOTPLUG_BEGIN; i < SCX_OPI_CPU_HOTPLUG_END; i++) 7137bba2c361STejun Heo if (((void (**)(void))ops)[i]) 7138bba2c361STejun Heo set_bit(i, sch->has_op); 7139bba2c361STejun Heo 7140bba2c361STejun Heo ret = check_hotplug_seq(sch, ops); 7141bba2c361STejun Heo if (ret) { 7142bba2c361STejun Heo cpus_read_unlock(); 7143bba2c361STejun Heo goto err_disable; 7144bba2c361STejun Heo } 7145bba2c361STejun Heo scx_idle_update_selcpu_topology(ops); 7146bba2c361STejun Heo 7147bba2c361STejun Heo cpus_read_unlock(); 7148bba2c361STejun Heo 7149bba2c361STejun Heo ret = validate_ops(sch, ops); 7150bba2c361STejun Heo if (ret) 7151bba2c361STejun Heo goto err_disable; 7152bba2c361STejun Heo 7153bba2c361STejun Heo /* 7154bba2c361STejun Heo * Attach the ext_server bandwidth reservation before anything is 7155bba2c361STejun Heo * committed so that we can fail the enable if the root domain cannot 7156bba2c361STejun Heo * accommodate it. The matching fair_server detach is deferred to the 7157bba2c361STejun Heo * tail of this function, after the switch is fully committed and can no 7158bba2c361STejun Heo * longer fail. 7159bba2c361STejun Heo * 7160bba2c361STejun Heo * On failure, err_disable funnels into scx_root_disable() which 7161bba2c361STejun Heo * detaches ext_server, so partially-attached state is cleaned up 7162bba2c361STejun Heo * automatically. 7163bba2c361STejun Heo */ 7164bba2c361STejun Heo for_each_possible_cpu(cpu) { 7165bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 7166bba2c361STejun Heo 7167bba2c361STejun Heo scoped_guard(rq_lock_irqsave, rq) { 7168bba2c361STejun Heo update_rq_clock(rq); 7169bba2c361STejun Heo ret = dl_server_attach_bw(&rq->ext_server); 7170bba2c361STejun Heo } 7171bba2c361STejun Heo if (ret) { 7172bba2c361STejun Heo pr_warn("sched_ext: failed to attach ext_server on CPU %d (%d)\n", 7173bba2c361STejun Heo cpu, ret); 7174bba2c361STejun Heo goto err_disable; 7175bba2c361STejun Heo } 7176bba2c361STejun Heo } 7177bba2c361STejun Heo 7178bba2c361STejun Heo /* 7179bba2c361STejun Heo * Once __scx_enabled is set, %current can be switched to SCX anytime. 7180bba2c361STejun Heo * This can lead to stalls as some BPF schedulers (e.g. userspace 7181bba2c361STejun Heo * scheduling) may not function correctly before all tasks are switched. 7182bba2c361STejun Heo * Init in bypass mode to guarantee forward progress. 7183bba2c361STejun Heo */ 7184bba2c361STejun Heo scx_bypass(sch, true); 7185bba2c361STejun Heo 7186bba2c361STejun Heo for (i = SCX_OPI_NORMAL_BEGIN; i < SCX_OPI_NORMAL_END; i++) 7187bba2c361STejun Heo if (((void (**)(void))ops)[i]) 7188bba2c361STejun Heo set_bit(i, sch->has_op); 7189bba2c361STejun Heo 7190bba2c361STejun Heo if (sch->ops.cpu_acquire || sch->ops.cpu_release) 7191bba2c361STejun Heo sch->ops.flags |= SCX_OPS_HAS_CPU_PREEMPT; 7192bba2c361STejun Heo 7193bba2c361STejun Heo /* 7194bba2c361STejun Heo * Lock out forks, cgroup on/offlining and moves before opening the 7195bba2c361STejun Heo * floodgate so that they don't wander into the operations prematurely. 7196bba2c361STejun Heo */ 7197bba2c361STejun Heo percpu_down_write(&scx_fork_rwsem); 7198bba2c361STejun Heo 7199bba2c361STejun Heo WARN_ON_ONCE(scx_init_task_enabled); 7200bba2c361STejun Heo scx_init_task_enabled = true; 7201bba2c361STejun Heo 7202bba2c361STejun Heo /* flip under fork_rwsem; the iter below covers existing tasks */ 7203bba2c361STejun Heo if (ops->flags & SCX_OPS_TID_TO_TASK) 7204bba2c361STejun Heo static_branch_enable(&__scx_tid_to_task_enabled); 7205bba2c361STejun Heo 7206bba2c361STejun Heo /* 7207bba2c361STejun Heo * Enable ops for every task. Fork is excluded by scx_fork_rwsem 7208bba2c361STejun Heo * preventing new tasks from being added. No need to exclude tasks 7209bba2c361STejun Heo * leaving as sched_ext_free() can handle both prepped and enabled 7210bba2c361STejun Heo * tasks. Prep all tasks first and then enable them with preemption 7211bba2c361STejun Heo * disabled. 7212bba2c361STejun Heo * 7213bba2c361STejun Heo * All cgroups should be initialized before scx_init_task() so that the 7214bba2c361STejun Heo * BPF scheduler can reliably track each task's cgroup membership from 7215bba2c361STejun Heo * scx_init_task(). Lock out cgroup on/offlining and task migrations 7216bba2c361STejun Heo * while tasks are being initialized so that scx_cgroup_can_attach() 7217bba2c361STejun Heo * never sees uninitialized tasks. 7218bba2c361STejun Heo */ 7219bba2c361STejun Heo scx_cgroup_lock(); 7220bba2c361STejun Heo set_cgroup_sched(sch_cgroup(sch), sch); 7221bba2c361STejun Heo ret = scx_cgroup_init(sch); 7222bba2c361STejun Heo if (ret) 7223bba2c361STejun Heo goto err_disable_unlock_all; 7224bba2c361STejun Heo 7225bba2c361STejun Heo scx_task_iter_start(&sti, NULL); 7226bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 7227bba2c361STejun Heo /* 7228bba2c361STejun Heo * @p is in scx_tasks under scx_tasks_lock, and SCX_TASK_DEAD 7229bba2c361STejun Heo * tasks are filtered by scx_task_iter_next_locked(). 7230bba2c361STejun Heo * sched_ext_dead() removes @p from scx_tasks under the same 7231bba2c361STejun Heo * lock before put_task_struct_rcu_user() runs, so @p->usage 7232bba2c361STejun Heo * is guaranteed > 0 here. 7233bba2c361STejun Heo */ 7234bba2c361STejun Heo get_task_struct(p); 7235bba2c361STejun Heo 7236bba2c361STejun Heo /* 7237bba2c361STejun Heo * Set %INIT_BEGIN under the iter's rq lock so that a concurrent 7238bba2c361STejun Heo * sched_ext_dead() does not call ops.exit_task() on @p while 7239bba2c361STejun Heo * ops.init_task() is running. If sched_ext_dead() runs before 7240bba2c361STejun Heo * this store, it has already removed @p from scx_tasks and the 7241bba2c361STejun Heo * iter won't visit @p; if it runs after, it observes 7242bba2c361STejun Heo * %INIT_BEGIN and transitions to %DEAD without calling ops, 7243bba2c361STejun Heo * leaving the post-init recheck below to unwind. 7244bba2c361STejun Heo */ 7245bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_INIT_BEGIN); 7246bba2c361STejun Heo scx_task_iter_unlock(&sti); 7247bba2c361STejun Heo 7248bba2c361STejun Heo ret = __scx_init_task(sch, p, false); 7249bba2c361STejun Heo 7250bba2c361STejun Heo scx_task_iter_relock(&sti, p); 7251bba2c361STejun Heo 7252bba2c361STejun Heo if (unlikely(ret)) { 7253bba2c361STejun Heo if (scx_get_task_state(p) != SCX_TASK_DEAD) 7254bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_NONE); 7255bba2c361STejun Heo scx_task_iter_stop(&sti); 7256bba2c361STejun Heo scx_error(sch, "ops.init_task() failed (%d) for %s[%d]", 7257bba2c361STejun Heo ret, p->comm, p->pid); 7258bba2c361STejun Heo put_task_struct(p); 7259bba2c361STejun Heo goto err_disable_unlock_all; 7260bba2c361STejun Heo } 7261bba2c361STejun Heo 7262bba2c361STejun Heo if (scx_get_task_state(p) == SCX_TASK_DEAD) { 7263bba2c361STejun Heo /* 7264bba2c361STejun Heo * sched_ext_dead() observed %INIT_BEGIN and set %DEAD. 7265bba2c361STejun Heo * ops.exit_task() is owed to the sched __scx_init_task() 7266bba2c361STejun Heo * ran against; call it now. 7267bba2c361STejun Heo */ 7268bba2c361STejun Heo scx_sub_init_cancel_task(sch, p); 7269bba2c361STejun Heo } else { 7270bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_INIT); 7271bba2c361STejun Heo scx_set_task_sched(p, sch); 7272bba2c361STejun Heo scx_set_task_state(p, SCX_TASK_READY); 7273bba2c361STejun Heo } 7274bba2c361STejun Heo 7275bba2c361STejun Heo /* 7276bba2c361STejun Heo * Insert into the tid hash. scx_tasks_lock is held by the iter; 7277bba2c361STejun Heo * list_empty() guards against sched_ext_dead() having taken @p 7278bba2c361STejun Heo * off the list while init ran unlocked. 7279bba2c361STejun Heo */ 7280bba2c361STejun Heo if (scx_tid_to_task_enabled() && !list_empty(&p->scx.tasks_node)) 7281bba2c361STejun Heo scx_tid_hash_insert(p); 7282bba2c361STejun Heo 7283bba2c361STejun Heo put_task_struct(p); 7284bba2c361STejun Heo } 7285bba2c361STejun Heo scx_task_iter_stop(&sti); 7286bba2c361STejun Heo scx_cgroup_unlock(); 7287bba2c361STejun Heo percpu_up_write(&scx_fork_rwsem); 7288bba2c361STejun Heo 7289bba2c361STejun Heo /* 7290bba2c361STejun Heo * All tasks are READY. It's safe to turn on scx_enabled() and switch 7291bba2c361STejun Heo * all eligible tasks. 7292bba2c361STejun Heo */ 7293bba2c361STejun Heo WRITE_ONCE(scx_switching_all, !(ops->flags & SCX_OPS_SWITCH_PARTIAL)); 7294bba2c361STejun Heo static_branch_enable(&__scx_enabled); 7295bba2c361STejun Heo 7296bba2c361STejun Heo /* 7297bba2c361STejun Heo * We're fully committed and can't fail. The task READY -> ENABLED 7298bba2c361STejun Heo * transitions here are synchronized against sched_ext_free() through 7299bba2c361STejun Heo * scx_tasks_lock. 7300bba2c361STejun Heo */ 7301bba2c361STejun Heo percpu_down_write(&scx_fork_rwsem); 7302bba2c361STejun Heo scx_task_iter_start(&sti, NULL); 7303bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 7304bba2c361STejun Heo unsigned int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE; 7305bba2c361STejun Heo const struct sched_class *old_class = p->sched_class; 7306bba2c361STejun Heo const struct sched_class *new_class = scx_setscheduler_class(p); 7307bba2c361STejun Heo 7308bba2c361STejun Heo if (scx_get_task_state(p) != SCX_TASK_READY) 7309bba2c361STejun Heo continue; 7310bba2c361STejun Heo 7311bba2c361STejun Heo if (old_class != new_class) 7312bba2c361STejun Heo queue_flags |= DEQUEUE_CLASS; 7313bba2c361STejun Heo 7314bba2c361STejun Heo scoped_guard (sched_change, p, queue_flags) { 7315bba2c361STejun Heo p->scx.slice = READ_ONCE(sch->slice_dfl); 7316bba2c361STejun Heo p->sched_class = new_class; 7317bba2c361STejun Heo } 7318bba2c361STejun Heo } 7319bba2c361STejun Heo scx_task_iter_stop(&sti); 7320bba2c361STejun Heo percpu_up_write(&scx_fork_rwsem); 7321bba2c361STejun Heo 7322bba2c361STejun Heo scx_bypass(sch, false); 7323bba2c361STejun Heo 7324bba2c361STejun Heo if (!scx_tryset_enable_state(SCX_ENABLED, SCX_ENABLING)) { 7325bba2c361STejun Heo WARN_ON_ONCE(atomic_read(&sch->exit_kind) == SCX_EXIT_NONE); 7326bba2c361STejun Heo goto err_disable; 7327bba2c361STejun Heo } 7328bba2c361STejun Heo 7329bba2c361STejun Heo if (!(ops->flags & SCX_OPS_SWITCH_PARTIAL)) 7330bba2c361STejun Heo static_branch_enable(&__scx_switched_all); 7331bba2c361STejun Heo 7332bba2c361STejun Heo /* 7333bba2c361STejun Heo * Detach the fair_server bandwidth reservation now that the switch 7334bba2c361STejun Heo * is fully committed. In full mode (!SCX_OPS_SWITCH_PARTIAL) no 7335bba2c361STejun Heo * task will ever run in the fair class, so give that bandwidth 7336bba2c361STejun Heo * back to the RT class. The matching ext_server attach already 7337bba2c361STejun Heo * happened earlier; this only releases bandwidth and cannot fail. 7338bba2c361STejun Heo * 7339bba2c361STejun Heo * In partial mode keep fair_server attached. 7340bba2c361STejun Heo */ 7341bba2c361STejun Heo if (scx_switched_all()) { 7342bba2c361STejun Heo for_each_possible_cpu(cpu) { 7343bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 7344bba2c361STejun Heo 7345bba2c361STejun Heo guard(rq_lock_irqsave)(rq); 7346bba2c361STejun Heo update_rq_clock(rq); 7347bba2c361STejun Heo dl_server_detach_bw(&rq->fair_server); 7348bba2c361STejun Heo } 7349bba2c361STejun Heo } 7350bba2c361STejun Heo 7351bba2c361STejun Heo pr_info("sched_ext: BPF scheduler \"%s\" enabled%s\n", 7352bba2c361STejun Heo sch->ops.name, scx_switched_all() ? "" : " (partial)"); 7353bba2c361STejun Heo kobject_uevent(&sch->kobj, KOBJ_ADD); 7354bba2c361STejun Heo mutex_unlock(&scx_enable_mutex); 7355bba2c361STejun Heo 7356bba2c361STejun Heo atomic_long_inc(&scx_enable_seq); 7357bba2c361STejun Heo 7358bba2c361STejun Heo cmd->ret = 0; 7359bba2c361STejun Heo return; 7360bba2c361STejun Heo 7361bba2c361STejun Heo err_free_tid_hash: 7362bba2c361STejun Heo if (ops->flags & SCX_OPS_TID_TO_TASK) 7363bba2c361STejun Heo rhashtable_free_and_destroy(&scx_tid_hash, NULL, NULL); 7364bba2c361STejun Heo err_free_ksyncs: 7365bba2c361STejun Heo free_kick_syncs(); 7366bba2c361STejun Heo err_unlock: 7367bba2c361STejun Heo mutex_unlock(&scx_enable_mutex); 7368bba2c361STejun Heo cmd->ret = ret; 7369bba2c361STejun Heo return; 7370bba2c361STejun Heo 7371bba2c361STejun Heo err_disable_unlock_all: 7372bba2c361STejun Heo scx_cgroup_unlock(); 7373bba2c361STejun Heo percpu_up_write(&scx_fork_rwsem); 7374bba2c361STejun Heo /* we'll soon enter disable path, keep bypass on */ 7375bba2c361STejun Heo err_disable: 7376bba2c361STejun Heo mutex_unlock(&scx_enable_mutex); 7377bba2c361STejun Heo /* 7378bba2c361STejun Heo * Returning an error code here would not pass all the error information 7379bba2c361STejun Heo * to userspace. Record errno using scx_error() for cases scx_error() 7380bba2c361STejun Heo * wasn't already invoked and exit indicating success so that the error 7381bba2c361STejun Heo * is notified through ops.exit() with all the details. 7382bba2c361STejun Heo * 7383bba2c361STejun Heo * Flush scx_disable_work to ensure that error is reported before init 7384bba2c361STejun Heo * completion. sch's base reference will be put by bpf_scx_unreg(). 7385bba2c361STejun Heo */ 7386bba2c361STejun Heo scx_error(sch, "scx_root_enable() failed (%d)", ret); 7387bba2c361STejun Heo scx_flush_disable_work(sch); 7388bba2c361STejun Heo cmd->ret = 0; 7389bba2c361STejun Heo } 7390bba2c361STejun Heo 7391bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 7392bba2c361STejun Heo /* verify that a scheduler can be attached to @cgrp and return the parent */ 7393bba2c361STejun Heo static struct scx_sched *find_parent_sched(struct cgroup *cgrp) 7394bba2c361STejun Heo { 7395bba2c361STejun Heo struct scx_sched *parent = cgrp->scx_sched; 7396bba2c361STejun Heo struct scx_sched *pos; 7397bba2c361STejun Heo 7398bba2c361STejun Heo lockdep_assert_held(&scx_sched_lock); 7399bba2c361STejun Heo 7400bba2c361STejun Heo /* can't attach twice to the same cgroup */ 7401bba2c361STejun Heo if (parent->cgrp == cgrp) 7402bba2c361STejun Heo return ERR_PTR(-EBUSY); 7403bba2c361STejun Heo 7404bba2c361STejun Heo /* does $parent allow sub-scheds? */ 7405bba2c361STejun Heo if (!parent->ops.sub_attach) 7406bba2c361STejun Heo return ERR_PTR(-EOPNOTSUPP); 7407bba2c361STejun Heo 7408bba2c361STejun Heo /* can't insert between $parent and its exiting children */ 7409bba2c361STejun Heo list_for_each_entry(pos, &parent->children, sibling) 7410bba2c361STejun Heo if (cgroup_is_descendant(pos->cgrp, cgrp)) 7411bba2c361STejun Heo return ERR_PTR(-EBUSY); 7412bba2c361STejun Heo 7413bba2c361STejun Heo return parent; 7414bba2c361STejun Heo } 7415bba2c361STejun Heo 7416bba2c361STejun Heo static bool assert_task_ready_or_enabled(struct task_struct *p) 7417bba2c361STejun Heo { 7418bba2c361STejun Heo u32 state = scx_get_task_state(p); 7419bba2c361STejun Heo 7420bba2c361STejun Heo switch (state) { 7421bba2c361STejun Heo case SCX_TASK_READY: 7422bba2c361STejun Heo case SCX_TASK_ENABLED: 7423bba2c361STejun Heo return true; 7424bba2c361STejun Heo default: 7425bba2c361STejun Heo WARN_ONCE(true, "sched_ext: Invalid task state %d for %s[%d] during enabling sub sched", 7426bba2c361STejun Heo state, p->comm, p->pid); 7427bba2c361STejun Heo return false; 7428bba2c361STejun Heo } 7429bba2c361STejun Heo } 7430bba2c361STejun Heo 7431bba2c361STejun Heo static void scx_sub_enable_workfn(struct kthread_work *work) 7432bba2c361STejun Heo { 7433bba2c361STejun Heo struct scx_enable_cmd *cmd = container_of(work, struct scx_enable_cmd, work); 7434bba2c361STejun Heo struct sched_ext_ops *ops = cmd->ops; 7435bba2c361STejun Heo struct cgroup *cgrp; 7436bba2c361STejun Heo struct scx_sched *parent, *sch; 7437bba2c361STejun Heo struct scx_task_iter sti; 7438bba2c361STejun Heo struct task_struct *p; 7439bba2c361STejun Heo s32 i, ret; 7440bba2c361STejun Heo 7441bba2c361STejun Heo mutex_lock(&scx_enable_mutex); 7442bba2c361STejun Heo 7443bba2c361STejun Heo if (!scx_enabled()) { 7444bba2c361STejun Heo ret = -ENODEV; 7445bba2c361STejun Heo goto out_unlock; 7446bba2c361STejun Heo } 7447bba2c361STejun Heo 7448bba2c361STejun Heo /* See scx_root_enable_workfn() for the @ops->priv check. */ 7449bba2c361STejun Heo if (rcu_access_pointer(ops->priv)) { 7450bba2c361STejun Heo ret = -EBUSY; 7451bba2c361STejun Heo goto out_unlock; 7452bba2c361STejun Heo } 7453bba2c361STejun Heo 7454bba2c361STejun Heo cgrp = cgroup_get_from_id(ops->sub_cgroup_id); 7455bba2c361STejun Heo if (IS_ERR(cgrp)) { 7456bba2c361STejun Heo ret = PTR_ERR(cgrp); 7457bba2c361STejun Heo goto out_unlock; 7458bba2c361STejun Heo } 7459bba2c361STejun Heo 7460bba2c361STejun Heo raw_spin_lock_irq(&scx_sched_lock); 7461bba2c361STejun Heo parent = find_parent_sched(cgrp); 7462bba2c361STejun Heo if (IS_ERR(parent)) { 7463bba2c361STejun Heo raw_spin_unlock_irq(&scx_sched_lock); 7464bba2c361STejun Heo ret = PTR_ERR(parent); 7465bba2c361STejun Heo goto out_put_cgrp; 7466bba2c361STejun Heo } 7467bba2c361STejun Heo kobject_get(&parent->kobj); 7468bba2c361STejun Heo raw_spin_unlock_irq(&scx_sched_lock); 7469bba2c361STejun Heo 7470bba2c361STejun Heo /* scx_alloc_and_add_sched() consumes @cgrp whether it succeeds or not */ 7471bba2c361STejun Heo sch = scx_alloc_and_add_sched(cmd, cgrp, parent); 7472bba2c361STejun Heo kobject_put(&parent->kobj); 7473bba2c361STejun Heo if (IS_ERR(sch)) { 7474bba2c361STejun Heo ret = PTR_ERR(sch); 7475bba2c361STejun Heo goto out_unlock; 7476bba2c361STejun Heo } 7477bba2c361STejun Heo 7478bba2c361STejun Heo ret = scx_link_sched(sch); 7479bba2c361STejun Heo if (ret) 7480bba2c361STejun Heo goto err_disable; 7481bba2c361STejun Heo 7482bba2c361STejun Heo if (sch->level >= SCX_SUB_MAX_DEPTH) { 7483bba2c361STejun Heo scx_error(sch, "max nesting depth %d violated", 7484bba2c361STejun Heo SCX_SUB_MAX_DEPTH); 7485bba2c361STejun Heo goto err_disable; 7486bba2c361STejun Heo } 7487bba2c361STejun Heo 7488bba2c361STejun Heo if (sch->ops.init) { 7489bba2c361STejun Heo ret = SCX_CALL_OP_RET(sch, init, NULL); 7490bba2c361STejun Heo if (ret) { 7491bba2c361STejun Heo ret = ops_sanitize_err(sch, "init", ret); 7492bba2c361STejun Heo scx_error(sch, "ops.init() failed (%d)", ret); 7493bba2c361STejun Heo goto err_disable; 7494bba2c361STejun Heo } 7495bba2c361STejun Heo sch->exit_info->flags |= SCX_EFLAG_INITIALIZED; 7496bba2c361STejun Heo } 7497bba2c361STejun Heo 7498bba2c361STejun Heo ret = scx_arena_pool_init(sch); 7499bba2c361STejun Heo if (ret) 7500bba2c361STejun Heo goto err_disable; 7501bba2c361STejun Heo 7502bba2c361STejun Heo ret = scx_set_cmask_scratch_alloc(sch); 7503bba2c361STejun Heo if (ret) 7504bba2c361STejun Heo goto err_disable; 7505bba2c361STejun Heo 7506bba2c361STejun Heo if (validate_ops(sch, ops)) 7507bba2c361STejun Heo goto err_disable; 7508bba2c361STejun Heo 7509bba2c361STejun Heo struct scx_sub_attach_args sub_attach_args = { 7510bba2c361STejun Heo .ops = &sch->ops, 7511bba2c361STejun Heo .cgroup_path = sch->cgrp_path, 7512bba2c361STejun Heo }; 7513bba2c361STejun Heo 7514bba2c361STejun Heo ret = SCX_CALL_OP_RET(parent, sub_attach, NULL, 7515bba2c361STejun Heo &sub_attach_args); 7516bba2c361STejun Heo if (ret) { 7517bba2c361STejun Heo ret = ops_sanitize_err(sch, "sub_attach", ret); 7518bba2c361STejun Heo scx_error(sch, "parent rejected (%d)", ret); 7519bba2c361STejun Heo goto err_disable; 7520bba2c361STejun Heo } 7521bba2c361STejun Heo sch->sub_attached = true; 7522bba2c361STejun Heo 7523bba2c361STejun Heo scx_bypass(sch, true); 7524bba2c361STejun Heo 7525bba2c361STejun Heo for (i = SCX_OPI_BEGIN; i < SCX_OPI_END; i++) 7526bba2c361STejun Heo if (((void (**)(void))ops)[i]) 7527bba2c361STejun Heo set_bit(i, sch->has_op); 7528bba2c361STejun Heo 7529bba2c361STejun Heo percpu_down_write(&scx_fork_rwsem); 7530bba2c361STejun Heo scx_cgroup_lock(); 7531bba2c361STejun Heo 7532bba2c361STejun Heo /* 7533bba2c361STejun Heo * Set cgroup->scx_sched's and check CSS_ONLINE. Either we see 7534bba2c361STejun Heo * !CSS_ONLINE or scx_cgroup_lifetime_notify() sees and shoots us down. 7535bba2c361STejun Heo */ 7536bba2c361STejun Heo set_cgroup_sched(sch_cgroup(sch), sch); 7537bba2c361STejun Heo if (!(cgrp->self.flags & CSS_ONLINE)) { 7538bba2c361STejun Heo scx_error(sch, "cgroup is not online"); 7539bba2c361STejun Heo goto err_unlock_and_disable; 7540bba2c361STejun Heo } 7541bba2c361STejun Heo 7542bba2c361STejun Heo /* 7543bba2c361STejun Heo * Initialize tasks for the new child $sch without exiting them for 7544bba2c361STejun Heo * $parent so that the tasks can always be reverted back to $parent 7545bba2c361STejun Heo * sched on child init failure. 7546bba2c361STejun Heo */ 7547bba2c361STejun Heo WARN_ON_ONCE(scx_enabling_sub_sched); 7548bba2c361STejun Heo scx_enabling_sub_sched = sch; 7549bba2c361STejun Heo 7550bba2c361STejun Heo scx_task_iter_start(&sti, sch->cgrp); 7551bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 7552bba2c361STejun Heo struct rq *rq; 7553bba2c361STejun Heo struct rq_flags rf; 7554bba2c361STejun Heo 7555bba2c361STejun Heo /* 7556bba2c361STejun Heo * Task iteration may visit the same task twice when racing 7557bba2c361STejun Heo * against exiting. Use %SCX_TASK_SUB_INIT to mark tasks which 7558bba2c361STejun Heo * finished __scx_init_task() and skip if set. 7559bba2c361STejun Heo * 7560bba2c361STejun Heo * A task may exit and get freed between __scx_init_task() 7561bba2c361STejun Heo * completion and scx_enable_task(). In such cases, 7562bba2c361STejun Heo * scx_disable_and_exit_task() must exit the task for both the 7563bba2c361STejun Heo * parent and child scheds. 7564bba2c361STejun Heo */ 7565bba2c361STejun Heo if (p->scx.flags & SCX_TASK_SUB_INIT) 7566bba2c361STejun Heo continue; 7567bba2c361STejun Heo 7568bba2c361STejun Heo /* @p is pinned by the iter; see scx_sub_disable() */ 7569bba2c361STejun Heo get_task_struct(p); 7570bba2c361STejun Heo 7571bba2c361STejun Heo if (!assert_task_ready_or_enabled(p)) { 7572bba2c361STejun Heo ret = -EINVAL; 7573bba2c361STejun Heo goto abort; 7574bba2c361STejun Heo } 7575bba2c361STejun Heo 7576bba2c361STejun Heo scx_task_iter_unlock(&sti); 7577bba2c361STejun Heo 7578bba2c361STejun Heo /* 7579bba2c361STejun Heo * As $p is still on $parent, it can't be transitioned to INIT. 7580bba2c361STejun Heo * Let's worry about task state later. Use __scx_init_task(). 7581bba2c361STejun Heo */ 7582bba2c361STejun Heo ret = __scx_init_task(sch, p, false); 7583bba2c361STejun Heo if (ret) 7584bba2c361STejun Heo goto abort; 7585bba2c361STejun Heo 7586bba2c361STejun Heo rq = task_rq_lock(p, &rf); 7587bba2c361STejun Heo 7588bba2c361STejun Heo if (scx_get_task_state(p) == SCX_TASK_DEAD) { 7589bba2c361STejun Heo /* 7590bba2c361STejun Heo * sched_ext_dead() raced us between __scx_init_task() 7591bba2c361STejun Heo * and this rq lock and ran exit_task() on $parent (the 7592bba2c361STejun Heo * sched @p was on at that point), not on @sch. @sch's 7593bba2c361STejun Heo * just-completed init is owed an exit_task() and we 7594bba2c361STejun Heo * issue it here. 7595bba2c361STejun Heo */ 7596bba2c361STejun Heo scx_sub_init_cancel_task(sch, p); 7597bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 7598bba2c361STejun Heo put_task_struct(p); 7599bba2c361STejun Heo continue; 7600bba2c361STejun Heo } 7601bba2c361STejun Heo 7602bba2c361STejun Heo p->scx.flags |= SCX_TASK_SUB_INIT; 7603bba2c361STejun Heo task_rq_unlock(rq, p, &rf); 7604bba2c361STejun Heo 7605bba2c361STejun Heo put_task_struct(p); 7606bba2c361STejun Heo } 7607bba2c361STejun Heo scx_task_iter_stop(&sti); 7608bba2c361STejun Heo 7609bba2c361STejun Heo /* 7610bba2c361STejun Heo * All tasks are prepped. Disable/exit tasks for $parent and enable for 7611bba2c361STejun Heo * the new @sch. 7612bba2c361STejun Heo */ 7613bba2c361STejun Heo scx_task_iter_start(&sti, sch->cgrp); 7614bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 7615bba2c361STejun Heo /* 7616bba2c361STejun Heo * Use clearing of %SCX_TASK_SUB_INIT to detect and skip 7617bba2c361STejun Heo * duplicate iterations. 7618bba2c361STejun Heo */ 7619bba2c361STejun Heo if (!(p->scx.flags & SCX_TASK_SUB_INIT)) 7620bba2c361STejun Heo continue; 7621bba2c361STejun Heo 7622bba2c361STejun Heo scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { 7623bba2c361STejun Heo /* 7624bba2c361STejun Heo * $p must be either READY or ENABLED. If ENABLED, 7625bba2c361STejun Heo * __scx_disabled_and_exit_task() first disables and 7626bba2c361STejun Heo * makes it READY. However, after exiting $p, it will 7627bba2c361STejun Heo * leave $p as READY. 7628bba2c361STejun Heo */ 7629bba2c361STejun Heo assert_task_ready_or_enabled(p); 7630bba2c361STejun Heo __scx_disable_and_exit_task(parent, p); 7631bba2c361STejun Heo 7632bba2c361STejun Heo /* 7633bba2c361STejun Heo * $p is now only initialized for @sch and READY, which 7634bba2c361STejun Heo * is what we want. Assign it to @sch and enable. 7635bba2c361STejun Heo */ 7636bba2c361STejun Heo scx_set_task_sched(p, sch); 7637bba2c361STejun Heo scx_enable_task(sch, p); 7638bba2c361STejun Heo 7639bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_SUB_INIT; 7640bba2c361STejun Heo } 7641bba2c361STejun Heo } 7642bba2c361STejun Heo scx_task_iter_stop(&sti); 7643bba2c361STejun Heo 7644bba2c361STejun Heo scx_enabling_sub_sched = NULL; 7645bba2c361STejun Heo 7646bba2c361STejun Heo scx_cgroup_unlock(); 7647bba2c361STejun Heo percpu_up_write(&scx_fork_rwsem); 7648bba2c361STejun Heo 7649bba2c361STejun Heo scx_bypass(sch, false); 7650bba2c361STejun Heo 7651bba2c361STejun Heo pr_info("sched_ext: BPF sub-scheduler \"%s\" enabled\n", sch->ops.name); 7652bba2c361STejun Heo kobject_uevent(&sch->kobj, KOBJ_ADD); 7653bba2c361STejun Heo ret = 0; 7654bba2c361STejun Heo goto out_unlock; 7655bba2c361STejun Heo 7656bba2c361STejun Heo out_put_cgrp: 7657bba2c361STejun Heo cgroup_put(cgrp); 7658bba2c361STejun Heo out_unlock: 7659bba2c361STejun Heo mutex_unlock(&scx_enable_mutex); 7660bba2c361STejun Heo cmd->ret = ret; 7661bba2c361STejun Heo return; 7662bba2c361STejun Heo 7663bba2c361STejun Heo abort: 7664bba2c361STejun Heo put_task_struct(p); 7665bba2c361STejun Heo scx_task_iter_stop(&sti); 7666bba2c361STejun Heo 7667bba2c361STejun Heo /* 7668bba2c361STejun Heo * Undo __scx_init_task() for tasks we marked. scx_enable_task() never 7669bba2c361STejun Heo * ran for @sch on them, so calling scx_disable_task() here would invoke 7670bba2c361STejun Heo * ops.disable() without a matching ops.enable(). scx_enabling_sub_sched 7671bba2c361STejun Heo * must stay set until SUB_INIT is cleared from every marked task - 7672bba2c361STejun Heo * scx_disable_and_exit_task() reads it when a task exits concurrently. 7673bba2c361STejun Heo */ 7674bba2c361STejun Heo scx_task_iter_start(&sti, sch->cgrp); 7675bba2c361STejun Heo while ((p = scx_task_iter_next_locked(&sti))) { 7676bba2c361STejun Heo if (p->scx.flags & SCX_TASK_SUB_INIT) { 7677bba2c361STejun Heo scx_sub_init_cancel_task(sch, p); 7678bba2c361STejun Heo p->scx.flags &= ~SCX_TASK_SUB_INIT; 7679bba2c361STejun Heo } 7680bba2c361STejun Heo } 7681bba2c361STejun Heo scx_task_iter_stop(&sti); 7682bba2c361STejun Heo scx_enabling_sub_sched = NULL; 7683bba2c361STejun Heo err_unlock_and_disable: 7684bba2c361STejun Heo /* we'll soon enter disable path, keep bypass on */ 7685bba2c361STejun Heo scx_cgroup_unlock(); 7686bba2c361STejun Heo percpu_up_write(&scx_fork_rwsem); 7687bba2c361STejun Heo err_disable: 7688bba2c361STejun Heo mutex_unlock(&scx_enable_mutex); 7689bba2c361STejun Heo scx_flush_disable_work(sch); 7690bba2c361STejun Heo cmd->ret = 0; 7691bba2c361STejun Heo } 7692bba2c361STejun Heo 7693bba2c361STejun Heo static s32 scx_cgroup_lifetime_notify(struct notifier_block *nb, 7694bba2c361STejun Heo unsigned long action, void *data) 7695bba2c361STejun Heo { 7696bba2c361STejun Heo struct cgroup *cgrp = data; 7697bba2c361STejun Heo struct cgroup *parent = cgroup_parent(cgrp); 7698bba2c361STejun Heo 7699bba2c361STejun Heo if (!cgroup_on_dfl(cgrp)) 7700bba2c361STejun Heo return NOTIFY_OK; 7701bba2c361STejun Heo 7702bba2c361STejun Heo switch (action) { 7703bba2c361STejun Heo case CGROUP_LIFETIME_ONLINE: 7704bba2c361STejun Heo /* inherit ->scx_sched from $parent */ 7705bba2c361STejun Heo if (parent) 7706bba2c361STejun Heo rcu_assign_pointer(cgrp->scx_sched, parent->scx_sched); 7707bba2c361STejun Heo break; 7708bba2c361STejun Heo case CGROUP_LIFETIME_OFFLINE: 7709bba2c361STejun Heo /* if there is a sched attached, shoot it down */ 7710bba2c361STejun Heo if (cgrp->scx_sched && cgrp->scx_sched->cgrp == cgrp) 7711bba2c361STejun Heo scx_exit(cgrp->scx_sched, SCX_EXIT_UNREG_KERN, 7712bba2c361STejun Heo SCX_ECODE_RSN_CGROUP_OFFLINE, 7713bba2c361STejun Heo "cgroup %llu going offline", cgroup_id(cgrp)); 7714bba2c361STejun Heo break; 7715bba2c361STejun Heo } 7716bba2c361STejun Heo 7717bba2c361STejun Heo return NOTIFY_OK; 7718bba2c361STejun Heo } 7719bba2c361STejun Heo 7720bba2c361STejun Heo static struct notifier_block scx_cgroup_lifetime_nb = { 7721bba2c361STejun Heo .notifier_call = scx_cgroup_lifetime_notify, 7722bba2c361STejun Heo }; 7723bba2c361STejun Heo 7724bba2c361STejun Heo static s32 __init scx_cgroup_lifetime_notifier_init(void) 7725bba2c361STejun Heo { 7726bba2c361STejun Heo return blocking_notifier_chain_register(&cgroup_lifetime_notifier, 7727bba2c361STejun Heo &scx_cgroup_lifetime_nb); 7728bba2c361STejun Heo } 7729bba2c361STejun Heo core_initcall(scx_cgroup_lifetime_notifier_init); 7730bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 7731bba2c361STejun Heo 7732bba2c361STejun Heo static s32 scx_enable(struct scx_enable_cmd *cmd, struct bpf_link *link) 7733bba2c361STejun Heo { 7734bba2c361STejun Heo static struct kthread_worker *helper; 7735bba2c361STejun Heo static DEFINE_MUTEX(helper_mutex); 7736bba2c361STejun Heo 7737bba2c361STejun Heo if (housekeeping_enabled(HK_TYPE_DOMAIN_BOOT)) { 7738bba2c361STejun Heo pr_err("sched_ext: Not compatible with \"isolcpus=\" domain isolation\n"); 7739bba2c361STejun Heo return -EINVAL; 7740bba2c361STejun Heo } 7741bba2c361STejun Heo 7742bba2c361STejun Heo if (!READ_ONCE(helper)) { 7743bba2c361STejun Heo mutex_lock(&helper_mutex); 7744bba2c361STejun Heo if (!helper) { 7745bba2c361STejun Heo struct kthread_worker *w = 7746bba2c361STejun Heo kthread_run_worker(0, "scx_enable_helper"); 7747bba2c361STejun Heo if (IS_ERR_OR_NULL(w)) { 7748bba2c361STejun Heo mutex_unlock(&helper_mutex); 7749bba2c361STejun Heo return -ENOMEM; 7750bba2c361STejun Heo } 7751bba2c361STejun Heo sched_set_fifo(w->task); 7752bba2c361STejun Heo WRITE_ONCE(helper, w); 7753bba2c361STejun Heo } 7754bba2c361STejun Heo mutex_unlock(&helper_mutex); 7755bba2c361STejun Heo } 7756bba2c361STejun Heo 7757bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 7758bba2c361STejun Heo if (cmd->ops->sub_cgroup_id > 1) 7759bba2c361STejun Heo kthread_init_work(&cmd->work, scx_sub_enable_workfn); 7760bba2c361STejun Heo else 7761bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 7762bba2c361STejun Heo kthread_init_work(&cmd->work, scx_root_enable_workfn); 7763bba2c361STejun Heo 7764bba2c361STejun Heo kthread_queue_work(READ_ONCE(helper), &cmd->work); 7765bba2c361STejun Heo kthread_flush_work(&cmd->work); 7766bba2c361STejun Heo return cmd->ret; 7767bba2c361STejun Heo } 7768bba2c361STejun Heo 7769bba2c361STejun Heo 7770bba2c361STejun Heo /******************************************************************************** 7771bba2c361STejun Heo * bpf_struct_ops plumbing. 7772bba2c361STejun Heo */ 7773bba2c361STejun Heo #include <linux/bpf_verifier.h> 7774bba2c361STejun Heo #include <linux/bpf.h> 7775bba2c361STejun Heo #include <linux/btf.h> 7776bba2c361STejun Heo 7777bba2c361STejun Heo static const struct btf_type *task_struct_type; 7778bba2c361STejun Heo 7779bba2c361STejun Heo static bool bpf_scx_is_valid_access(int off, int size, 7780bba2c361STejun Heo enum bpf_access_type type, 7781bba2c361STejun Heo const struct bpf_prog *prog, 7782bba2c361STejun Heo struct bpf_insn_access_aux *info) 7783bba2c361STejun Heo { 7784bba2c361STejun Heo if (type != BPF_READ) 7785bba2c361STejun Heo return false; 7786bba2c361STejun Heo if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) 7787bba2c361STejun Heo return false; 7788bba2c361STejun Heo if (off % size != 0) 7789bba2c361STejun Heo return false; 7790bba2c361STejun Heo 7791bba2c361STejun Heo return btf_ctx_access(off, size, type, prog, info); 7792bba2c361STejun Heo } 7793bba2c361STejun Heo 7794bba2c361STejun Heo static int bpf_scx_btf_struct_access(struct bpf_verifier_log *log, 7795bba2c361STejun Heo const struct bpf_reg_state *reg, int off, 7796bba2c361STejun Heo int size) 7797bba2c361STejun Heo { 7798bba2c361STejun Heo const struct btf_type *t; 7799bba2c361STejun Heo 7800bba2c361STejun Heo t = btf_type_by_id(reg->btf, reg->btf_id); 7801bba2c361STejun Heo if (t == task_struct_type) { 7802bba2c361STejun Heo /* 7803bba2c361STejun Heo * COMPAT: Will be removed in v6.23. 7804bba2c361STejun Heo */ 7805bba2c361STejun Heo if ((off >= offsetof(struct task_struct, scx.slice) && 7806bba2c361STejun Heo off + size <= offsetofend(struct task_struct, scx.slice)) || 7807bba2c361STejun Heo (off >= offsetof(struct task_struct, scx.dsq_vtime) && 7808bba2c361STejun Heo off + size <= offsetofend(struct task_struct, scx.dsq_vtime))) { 7809bba2c361STejun Heo pr_warn("sched_ext: Writing directly to p->scx.slice/dsq_vtime is deprecated, use scx_bpf_task_set_slice/dsq_vtime()"); 7810bba2c361STejun Heo return SCALAR_VALUE; 7811bba2c361STejun Heo } 7812bba2c361STejun Heo 7813bba2c361STejun Heo if (off >= offsetof(struct task_struct, scx.disallow) && 7814bba2c361STejun Heo off + size <= offsetofend(struct task_struct, scx.disallow)) 7815bba2c361STejun Heo return SCALAR_VALUE; 7816bba2c361STejun Heo } 7817bba2c361STejun Heo 7818bba2c361STejun Heo return -EACCES; 7819bba2c361STejun Heo } 7820bba2c361STejun Heo 7821bba2c361STejun Heo static const struct bpf_verifier_ops bpf_scx_verifier_ops = { 7822bba2c361STejun Heo .get_func_proto = bpf_base_func_proto, 7823bba2c361STejun Heo .is_valid_access = bpf_scx_is_valid_access, 7824bba2c361STejun Heo .btf_struct_access = bpf_scx_btf_struct_access, 7825bba2c361STejun Heo }; 7826bba2c361STejun Heo 7827bba2c361STejun Heo static int bpf_scx_init_member(const struct btf_type *t, 7828bba2c361STejun Heo const struct btf_member *member, 7829bba2c361STejun Heo void *kdata, const void *udata) 7830bba2c361STejun Heo { 7831bba2c361STejun Heo const struct sched_ext_ops *uops = udata; 7832bba2c361STejun Heo struct sched_ext_ops *ops = kdata; 7833bba2c361STejun Heo u32 moff = __btf_member_bit_offset(t, member) / 8; 7834bba2c361STejun Heo int ret; 7835bba2c361STejun Heo 7836bba2c361STejun Heo switch (moff) { 7837bba2c361STejun Heo case offsetof(struct sched_ext_ops, dispatch_max_batch): 7838bba2c361STejun Heo if (*(u32 *)(udata + moff) > INT_MAX) 7839bba2c361STejun Heo return -E2BIG; 7840bba2c361STejun Heo ops->dispatch_max_batch = *(u32 *)(udata + moff); 7841bba2c361STejun Heo return 1; 7842bba2c361STejun Heo case offsetof(struct sched_ext_ops, flags): 7843bba2c361STejun Heo if (*(u64 *)(udata + moff) & ~SCX_OPS_ALL_FLAGS) 7844bba2c361STejun Heo return -EINVAL; 7845bba2c361STejun Heo ops->flags = *(u64 *)(udata + moff); 7846bba2c361STejun Heo return 1; 7847bba2c361STejun Heo case offsetof(struct sched_ext_ops, name): 7848bba2c361STejun Heo ret = bpf_obj_name_cpy(ops->name, uops->name, 7849bba2c361STejun Heo sizeof(ops->name)); 7850bba2c361STejun Heo if (ret < 0) 7851bba2c361STejun Heo return ret; 7852bba2c361STejun Heo if (ret == 0) 7853bba2c361STejun Heo return -EINVAL; 7854bba2c361STejun Heo return 1; 7855bba2c361STejun Heo case offsetof(struct sched_ext_ops, timeout_ms): 7856bba2c361STejun Heo if (msecs_to_jiffies(*(u32 *)(udata + moff)) > 7857bba2c361STejun Heo SCX_WATCHDOG_MAX_TIMEOUT) 7858bba2c361STejun Heo return -E2BIG; 7859bba2c361STejun Heo ops->timeout_ms = *(u32 *)(udata + moff); 7860bba2c361STejun Heo return 1; 7861bba2c361STejun Heo case offsetof(struct sched_ext_ops, exit_dump_len): 7862bba2c361STejun Heo ops->exit_dump_len = 7863bba2c361STejun Heo *(u32 *)(udata + moff) ?: SCX_EXIT_DUMP_DFL_LEN; 7864bba2c361STejun Heo return 1; 7865bba2c361STejun Heo case offsetof(struct sched_ext_ops, hotplug_seq): 7866bba2c361STejun Heo ops->hotplug_seq = *(u64 *)(udata + moff); 7867bba2c361STejun Heo return 1; 7868bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 7869bba2c361STejun Heo case offsetof(struct sched_ext_ops, sub_cgroup_id): 7870bba2c361STejun Heo ops->sub_cgroup_id = *(u64 *)(udata + moff); 7871bba2c361STejun Heo return 1; 7872bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 7873bba2c361STejun Heo } 7874bba2c361STejun Heo 7875bba2c361STejun Heo return 0; 7876bba2c361STejun Heo } 7877bba2c361STejun Heo 7878bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 7879bba2c361STejun Heo static void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog) 7880bba2c361STejun Heo { 7881bba2c361STejun Heo struct scx_sched *sch; 7882bba2c361STejun Heo 7883bba2c361STejun Heo guard(rcu)(); 7884bba2c361STejun Heo sch = scx_prog_sched(prog->aux); 7885bba2c361STejun Heo if (unlikely(!sch)) 7886bba2c361STejun Heo return; 7887bba2c361STejun Heo 7888bba2c361STejun Heo scx_error(sch, "dispatch recursion detected"); 7889bba2c361STejun Heo } 7890bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 7891bba2c361STejun Heo 7892bba2c361STejun Heo static int bpf_scx_check_member(const struct btf_type *t, 7893bba2c361STejun Heo const struct btf_member *member, 7894bba2c361STejun Heo const struct bpf_prog *prog) 7895bba2c361STejun Heo { 7896bba2c361STejun Heo u32 moff = __btf_member_bit_offset(t, member) / 8; 7897bba2c361STejun Heo 7898bba2c361STejun Heo switch (moff) { 7899bba2c361STejun Heo case offsetof(struct sched_ext_ops, init_task): 7900bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 7901bba2c361STejun Heo case offsetof(struct sched_ext_ops, cgroup_init): 7902bba2c361STejun Heo case offsetof(struct sched_ext_ops, cgroup_exit): 7903bba2c361STejun Heo case offsetof(struct sched_ext_ops, cgroup_prep_move): 7904bba2c361STejun Heo #endif 7905bba2c361STejun Heo case offsetof(struct sched_ext_ops, cpu_online): 7906bba2c361STejun Heo case offsetof(struct sched_ext_ops, cpu_offline): 7907bba2c361STejun Heo case offsetof(struct sched_ext_ops, init): 7908bba2c361STejun Heo case offsetof(struct sched_ext_ops, exit): 7909bba2c361STejun Heo case offsetof(struct sched_ext_ops, sub_attach): 7910bba2c361STejun Heo case offsetof(struct sched_ext_ops, sub_detach): 7911bba2c361STejun Heo break; 7912bba2c361STejun Heo default: 7913bba2c361STejun Heo if (prog->sleepable) 7914bba2c361STejun Heo return -EINVAL; 7915bba2c361STejun Heo } 7916bba2c361STejun Heo 7917bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 7918bba2c361STejun Heo /* 7919bba2c361STejun Heo * Enable private stack for operations that can nest along the 7920bba2c361STejun Heo * hierarchy. 7921bba2c361STejun Heo * 7922bba2c361STejun Heo * XXX - Ideally, we should only do this for scheds that allow 7923bba2c361STejun Heo * sub-scheds and sub-scheds themselves but I don't know how to access 7924bba2c361STejun Heo * struct_ops from here. 7925bba2c361STejun Heo */ 7926bba2c361STejun Heo switch (moff) { 7927bba2c361STejun Heo case offsetof(struct sched_ext_ops, dispatch): 7928bba2c361STejun Heo prog->aux->priv_stack_requested = true; 7929bba2c361STejun Heo prog->aux->recursion_detected = scx_pstack_recursion_on_dispatch; 7930bba2c361STejun Heo } 7931bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 7932bba2c361STejun Heo 7933bba2c361STejun Heo return 0; 7934bba2c361STejun Heo } 7935bba2c361STejun Heo 7936bba2c361STejun Heo static int bpf_scx_reg(void *kdata, struct bpf_link *link) 7937bba2c361STejun Heo { 7938bba2c361STejun Heo struct scx_enable_cmd cmd = { .ops = kdata }; 7939bba2c361STejun Heo 7940bba2c361STejun Heo return scx_enable(&cmd, link); 7941bba2c361STejun Heo } 7942bba2c361STejun Heo 7943bba2c361STejun Heo struct scx_arena_scan { 7944bba2c361STejun Heo struct bpf_map *arena; 7945bba2c361STejun Heo int err; 7946bba2c361STejun Heo }; 7947bba2c361STejun Heo 7948bba2c361STejun Heo /* 7949bba2c361STejun Heo * The verifier enforces one arena per BPF program, so each struct_ops 7950bba2c361STejun Heo * member prog contributes at most one arena via bpf_prog_arena(). 7951bba2c361STejun Heo * Require all non-NULL contributions to match. 7952bba2c361STejun Heo */ 7953bba2c361STejun Heo static int scx_arena_scan_prog(struct bpf_prog *prog, void *data) 7954bba2c361STejun Heo { 7955bba2c361STejun Heo struct scx_arena_scan *s = data; 7956bba2c361STejun Heo struct bpf_map *arena = NULL; 7957bba2c361STejun Heo 7958bba2c361STejun Heo /* arena.o, which defines these, is built only on MMU && 64BIT */ 7959bba2c361STejun Heo #if defined(CONFIG_MMU) && defined(CONFIG_64BIT) 7960bba2c361STejun Heo arena = bpf_prog_arena(prog); 7961bba2c361STejun Heo #endif 7962bba2c361STejun Heo if (!arena) 7963bba2c361STejun Heo return 0; 7964bba2c361STejun Heo if (s->arena && s->arena != arena) { 7965bba2c361STejun Heo s->err = -EINVAL; 7966bba2c361STejun Heo return 1; 7967bba2c361STejun Heo } 7968bba2c361STejun Heo s->arena = arena; 7969bba2c361STejun Heo return 0; 7970bba2c361STejun Heo } 7971bba2c361STejun Heo 7972bba2c361STejun Heo static int bpf_scx_reg_cid(void *kdata, struct bpf_link *link) 7973bba2c361STejun Heo { 7974bba2c361STejun Heo struct scx_enable_cmd cmd = { .ops_cid = kdata, .is_cid_type = true }; 7975bba2c361STejun Heo struct scx_arena_scan scan = {}; 7976bba2c361STejun Heo int ret; 7977bba2c361STejun Heo 7978bba2c361STejun Heo bpf_struct_ops_for_each_prog(kdata, scx_arena_scan_prog, &scan); 7979bba2c361STejun Heo if (scan.err) { 7980bba2c361STejun Heo pr_err("sched_ext: cid-form scheduler uses multiple arena maps\n"); 7981bba2c361STejun Heo return scan.err; 7982bba2c361STejun Heo } 7983bba2c361STejun Heo if (!scan.arena) { 7984bba2c361STejun Heo pr_err("sched_ext: cid-form scheduler must use a BPF arena map\n"); 7985bba2c361STejun Heo return -EINVAL; 7986bba2c361STejun Heo } 7987bba2c361STejun Heo 7988bba2c361STejun Heo bpf_map_inc(scan.arena); 7989bba2c361STejun Heo cmd.arena_map = scan.arena; 7990bba2c361STejun Heo ret = scx_enable(&cmd, link); 7991bba2c361STejun Heo if (cmd.arena_map) /* not consumed by scx_alloc_and_add_sched() */ 7992bba2c361STejun Heo bpf_map_put(cmd.arena_map); 7993bba2c361STejun Heo return ret; 7994bba2c361STejun Heo } 7995bba2c361STejun Heo 7996bba2c361STejun Heo static void bpf_scx_unreg(void *kdata, struct bpf_link *link) 7997bba2c361STejun Heo { 7998bba2c361STejun Heo struct sched_ext_ops *ops = kdata; 7999bba2c361STejun Heo struct scx_sched *sch = rcu_dereference_protected(ops->priv, true); 8000bba2c361STejun Heo 8001bba2c361STejun Heo scx_disable(sch, SCX_EXIT_UNREG); 8002bba2c361STejun Heo scx_flush_disable_work(sch); 8003bba2c361STejun Heo RCU_INIT_POINTER(ops->priv, NULL); 8004bba2c361STejun Heo kobject_put(&sch->kobj); 8005bba2c361STejun Heo } 8006bba2c361STejun Heo 8007bba2c361STejun Heo static int bpf_scx_init(struct btf *btf) 8008bba2c361STejun Heo { 8009bba2c361STejun Heo task_struct_type = btf_type_by_id(btf, btf_tracing_ids[BTF_TRACING_TYPE_TASK]); 8010bba2c361STejun Heo 8011bba2c361STejun Heo return 0; 8012bba2c361STejun Heo } 8013bba2c361STejun Heo 8014bba2c361STejun Heo static int bpf_scx_update(void *kdata, void *old_kdata, struct bpf_link *link) 8015bba2c361STejun Heo { 8016bba2c361STejun Heo /* 8017bba2c361STejun Heo * sched_ext does not support updating the actively-loaded BPF 8018bba2c361STejun Heo * scheduler, as registering a BPF scheduler can always fail if the 8019bba2c361STejun Heo * scheduler returns an error code for e.g. ops.init(), ops.init_task(), 8020bba2c361STejun Heo * etc. Similarly, we can always race with unregistration happening 8021bba2c361STejun Heo * elsewhere, such as with sysrq. 8022bba2c361STejun Heo */ 8023bba2c361STejun Heo return -EOPNOTSUPP; 8024bba2c361STejun Heo } 8025bba2c361STejun Heo 8026bba2c361STejun Heo static int bpf_scx_validate(void *kdata) 8027bba2c361STejun Heo { 8028bba2c361STejun Heo return 0; 8029bba2c361STejun Heo } 8030bba2c361STejun Heo 8031bba2c361STejun Heo static s32 sched_ext_ops__select_cpu(struct task_struct *p, s32 prev_cpu, u64 wake_flags) { return -EINVAL; } 8032bba2c361STejun Heo static void sched_ext_ops__enqueue(struct task_struct *p, u64 enq_flags) {} 8033bba2c361STejun Heo static void sched_ext_ops__dequeue(struct task_struct *p, u64 enq_flags) {} 8034bba2c361STejun Heo static void sched_ext_ops__dispatch(s32 prev_cpu, struct task_struct *prev__nullable) {} 8035bba2c361STejun Heo static void sched_ext_ops__tick(struct task_struct *p) {} 8036bba2c361STejun Heo static void sched_ext_ops__runnable(struct task_struct *p, u64 enq_flags) {} 8037bba2c361STejun Heo static void sched_ext_ops__running(struct task_struct *p) {} 8038bba2c361STejun Heo static void sched_ext_ops__stopping(struct task_struct *p, bool runnable) {} 8039bba2c361STejun Heo static void sched_ext_ops__quiescent(struct task_struct *p, u64 deq_flags) {} 8040bba2c361STejun Heo static bool sched_ext_ops__yield(struct task_struct *from, struct task_struct *to__nullable) { return false; } 8041bba2c361STejun Heo static bool sched_ext_ops__core_sched_before(struct task_struct *a, struct task_struct *b) { return false; } 8042bba2c361STejun Heo static void sched_ext_ops__set_weight(struct task_struct *p, u32 weight) {} 8043bba2c361STejun Heo static void sched_ext_ops__set_cpumask(struct task_struct *p, const struct cpumask *mask) {} 8044bba2c361STejun Heo static void sched_ext_ops__update_idle(s32 cpu, bool idle) {} 8045bba2c361STejun Heo static void sched_ext_ops__cpu_acquire(s32 cpu, struct scx_cpu_acquire_args *args) {} 8046bba2c361STejun Heo static void sched_ext_ops__cpu_release(s32 cpu, struct scx_cpu_release_args *args) {} 8047bba2c361STejun Heo static s32 sched_ext_ops__init_task(struct task_struct *p, struct scx_init_task_args *args) { return -EINVAL; } 8048bba2c361STejun Heo static void sched_ext_ops__exit_task(struct task_struct *p, struct scx_exit_task_args *args) {} 8049bba2c361STejun Heo static void sched_ext_ops__enable(struct task_struct *p) {} 8050bba2c361STejun Heo static void sched_ext_ops__disable(struct task_struct *p) {} 8051bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 8052bba2c361STejun Heo static s32 sched_ext_ops__cgroup_init(struct cgroup *cgrp, struct scx_cgroup_init_args *args) { return -EINVAL; } 8053bba2c361STejun Heo static void sched_ext_ops__cgroup_exit(struct cgroup *cgrp) {} 8054bba2c361STejun Heo static s32 sched_ext_ops__cgroup_prep_move(struct task_struct *p, struct cgroup *from, struct cgroup *to) { return -EINVAL; } 8055bba2c361STejun Heo static void sched_ext_ops__cgroup_move(struct task_struct *p, struct cgroup *from, struct cgroup *to) {} 8056bba2c361STejun Heo static void sched_ext_ops__cgroup_cancel_move(struct task_struct *p, struct cgroup *from, struct cgroup *to) {} 8057bba2c361STejun Heo static void sched_ext_ops__cgroup_set_weight(struct cgroup *cgrp, u32 weight) {} 8058bba2c361STejun Heo static void sched_ext_ops__cgroup_set_bandwidth(struct cgroup *cgrp, u64 period_us, u64 quota_us, u64 burst_us) {} 8059bba2c361STejun Heo static void sched_ext_ops__cgroup_set_idle(struct cgroup *cgrp, bool idle) {} 8060bba2c361STejun Heo #endif /* CONFIG_EXT_GROUP_SCHED */ 8061bba2c361STejun Heo static s32 sched_ext_ops__sub_attach(struct scx_sub_attach_args *args) { return -EINVAL; } 8062bba2c361STejun Heo static void sched_ext_ops__sub_detach(struct scx_sub_detach_args *args) {} 8063bba2c361STejun Heo static void sched_ext_ops__cpu_online(s32 cpu) {} 8064bba2c361STejun Heo static void sched_ext_ops__cpu_offline(s32 cpu) {} 8065bba2c361STejun Heo static s32 sched_ext_ops__init(void) { return -EINVAL; } 8066bba2c361STejun Heo static void sched_ext_ops__exit(struct scx_exit_info *info) {} 8067bba2c361STejun Heo static void sched_ext_ops__dump(struct scx_dump_ctx *ctx) {} 8068bba2c361STejun Heo static void sched_ext_ops__dump_cpu(struct scx_dump_ctx *ctx, s32 cpu, bool idle) {} 8069bba2c361STejun Heo static void sched_ext_ops__dump_task(struct scx_dump_ctx *ctx, struct task_struct *p) {} 8070bba2c361STejun Heo 8071bba2c361STejun Heo static struct sched_ext_ops __bpf_ops_sched_ext_ops = { 8072bba2c361STejun Heo .select_cpu = sched_ext_ops__select_cpu, 8073bba2c361STejun Heo .enqueue = sched_ext_ops__enqueue, 8074bba2c361STejun Heo .dequeue = sched_ext_ops__dequeue, 8075bba2c361STejun Heo .dispatch = sched_ext_ops__dispatch, 8076bba2c361STejun Heo .tick = sched_ext_ops__tick, 8077bba2c361STejun Heo .runnable = sched_ext_ops__runnable, 8078bba2c361STejun Heo .running = sched_ext_ops__running, 8079bba2c361STejun Heo .stopping = sched_ext_ops__stopping, 8080bba2c361STejun Heo .quiescent = sched_ext_ops__quiescent, 8081bba2c361STejun Heo .yield = sched_ext_ops__yield, 8082bba2c361STejun Heo .core_sched_before = sched_ext_ops__core_sched_before, 8083bba2c361STejun Heo .set_weight = sched_ext_ops__set_weight, 8084bba2c361STejun Heo .set_cpumask = sched_ext_ops__set_cpumask, 8085bba2c361STejun Heo .update_idle = sched_ext_ops__update_idle, 8086bba2c361STejun Heo .cpu_acquire = sched_ext_ops__cpu_acquire, 8087bba2c361STejun Heo .cpu_release = sched_ext_ops__cpu_release, 8088bba2c361STejun Heo .init_task = sched_ext_ops__init_task, 8089bba2c361STejun Heo .exit_task = sched_ext_ops__exit_task, 8090bba2c361STejun Heo .enable = sched_ext_ops__enable, 8091bba2c361STejun Heo .disable = sched_ext_ops__disable, 8092bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 8093bba2c361STejun Heo .cgroup_init = sched_ext_ops__cgroup_init, 8094bba2c361STejun Heo .cgroup_exit = sched_ext_ops__cgroup_exit, 8095bba2c361STejun Heo .cgroup_prep_move = sched_ext_ops__cgroup_prep_move, 8096bba2c361STejun Heo .cgroup_move = sched_ext_ops__cgroup_move, 8097bba2c361STejun Heo .cgroup_cancel_move = sched_ext_ops__cgroup_cancel_move, 8098bba2c361STejun Heo .cgroup_set_weight = sched_ext_ops__cgroup_set_weight, 8099bba2c361STejun Heo .cgroup_set_bandwidth = sched_ext_ops__cgroup_set_bandwidth, 8100bba2c361STejun Heo .cgroup_set_idle = sched_ext_ops__cgroup_set_idle, 8101bba2c361STejun Heo #endif 8102bba2c361STejun Heo .sub_attach = sched_ext_ops__sub_attach, 8103bba2c361STejun Heo .sub_detach = sched_ext_ops__sub_detach, 8104bba2c361STejun Heo .cpu_online = sched_ext_ops__cpu_online, 8105bba2c361STejun Heo .cpu_offline = sched_ext_ops__cpu_offline, 8106bba2c361STejun Heo .init = sched_ext_ops__init, 8107bba2c361STejun Heo .exit = sched_ext_ops__exit, 8108bba2c361STejun Heo .dump = sched_ext_ops__dump, 8109bba2c361STejun Heo .dump_cpu = sched_ext_ops__dump_cpu, 8110bba2c361STejun Heo .dump_task = sched_ext_ops__dump_task, 8111bba2c361STejun Heo }; 8112bba2c361STejun Heo 8113bba2c361STejun Heo static struct bpf_struct_ops bpf_sched_ext_ops = { 8114bba2c361STejun Heo .verifier_ops = &bpf_scx_verifier_ops, 8115bba2c361STejun Heo .reg = bpf_scx_reg, 8116bba2c361STejun Heo .unreg = bpf_scx_unreg, 8117bba2c361STejun Heo .check_member = bpf_scx_check_member, 8118bba2c361STejun Heo .init_member = bpf_scx_init_member, 8119bba2c361STejun Heo .init = bpf_scx_init, 8120bba2c361STejun Heo .update = bpf_scx_update, 8121bba2c361STejun Heo .validate = bpf_scx_validate, 8122bba2c361STejun Heo .name = "sched_ext_ops", 8123bba2c361STejun Heo .owner = THIS_MODULE, 8124bba2c361STejun Heo .cfi_stubs = &__bpf_ops_sched_ext_ops 8125bba2c361STejun Heo }; 8126bba2c361STejun Heo 8127bba2c361STejun Heo /* 8128bba2c361STejun Heo * cid-form cfi stubs. Stubs whose signatures match the cpu-form (param types 8129bba2c361STejun Heo * identical, only param names differ across structs) are reused; only 8130bba2c361STejun Heo * set_cmask needs a fresh stub since the second argument type differs. 8131bba2c361STejun Heo */ 8132bba2c361STejun Heo static void sched_ext_ops_cid__set_cmask(struct task_struct *p, 8133bba2c361STejun Heo const struct scx_cmask *cmask) {} 8134bba2c361STejun Heo 8135bba2c361STejun Heo static struct sched_ext_ops_cid __bpf_ops_sched_ext_ops_cid = { 8136bba2c361STejun Heo .select_cid = sched_ext_ops__select_cpu, 8137bba2c361STejun Heo .enqueue = sched_ext_ops__enqueue, 8138bba2c361STejun Heo .dequeue = sched_ext_ops__dequeue, 8139bba2c361STejun Heo .dispatch = sched_ext_ops__dispatch, 8140bba2c361STejun Heo .tick = sched_ext_ops__tick, 8141bba2c361STejun Heo .runnable = sched_ext_ops__runnable, 8142bba2c361STejun Heo .running = sched_ext_ops__running, 8143bba2c361STejun Heo .stopping = sched_ext_ops__stopping, 8144bba2c361STejun Heo .quiescent = sched_ext_ops__quiescent, 8145bba2c361STejun Heo .yield = sched_ext_ops__yield, 8146bba2c361STejun Heo .core_sched_before = sched_ext_ops__core_sched_before, 8147bba2c361STejun Heo .set_weight = sched_ext_ops__set_weight, 8148bba2c361STejun Heo .set_cmask = sched_ext_ops_cid__set_cmask, 8149bba2c361STejun Heo .update_idle = sched_ext_ops__update_idle, 8150bba2c361STejun Heo .init_task = sched_ext_ops__init_task, 8151bba2c361STejun Heo .exit_task = sched_ext_ops__exit_task, 8152bba2c361STejun Heo .enable = sched_ext_ops__enable, 8153bba2c361STejun Heo .disable = sched_ext_ops__disable, 8154bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 8155bba2c361STejun Heo .cgroup_init = sched_ext_ops__cgroup_init, 8156bba2c361STejun Heo .cgroup_exit = sched_ext_ops__cgroup_exit, 8157bba2c361STejun Heo .cgroup_prep_move = sched_ext_ops__cgroup_prep_move, 8158bba2c361STejun Heo .cgroup_move = sched_ext_ops__cgroup_move, 8159bba2c361STejun Heo .cgroup_cancel_move = sched_ext_ops__cgroup_cancel_move, 8160bba2c361STejun Heo .cgroup_set_weight = sched_ext_ops__cgroup_set_weight, 8161bba2c361STejun Heo .cgroup_set_bandwidth = sched_ext_ops__cgroup_set_bandwidth, 8162bba2c361STejun Heo .cgroup_set_idle = sched_ext_ops__cgroup_set_idle, 8163bba2c361STejun Heo #endif 8164bba2c361STejun Heo .sub_attach = sched_ext_ops__sub_attach, 8165bba2c361STejun Heo .sub_detach = sched_ext_ops__sub_detach, 8166bba2c361STejun Heo .cid_online = sched_ext_ops__cpu_online, 8167bba2c361STejun Heo .cid_offline = sched_ext_ops__cpu_offline, 8168bba2c361STejun Heo .init = sched_ext_ops__init, 8169bba2c361STejun Heo .exit = sched_ext_ops__exit, 8170bba2c361STejun Heo .dump = sched_ext_ops__dump, 8171bba2c361STejun Heo .dump_cid = sched_ext_ops__dump_cpu, 8172bba2c361STejun Heo .dump_task = sched_ext_ops__dump_task, 8173bba2c361STejun Heo }; 8174bba2c361STejun Heo 8175bba2c361STejun Heo /* 8176bba2c361STejun Heo * The cid-form struct_ops shares all bpf_struct_ops hooks with the cpu form. 8177bba2c361STejun Heo * init_member, check_member, reg, unreg, etc. process kdata as the byte block 8178bba2c361STejun Heo * verified to match by the BUILD_BUG_ON checks in scx_init(). 8179bba2c361STejun Heo */ 8180bba2c361STejun Heo static struct bpf_struct_ops bpf_sched_ext_ops_cid = { 8181bba2c361STejun Heo .verifier_ops = &bpf_scx_verifier_ops, 8182bba2c361STejun Heo .reg = bpf_scx_reg_cid, 8183bba2c361STejun Heo .unreg = bpf_scx_unreg, 8184bba2c361STejun Heo .check_member = bpf_scx_check_member, 8185bba2c361STejun Heo .init_member = bpf_scx_init_member, 8186bba2c361STejun Heo .init = bpf_scx_init, 8187bba2c361STejun Heo .update = bpf_scx_update, 8188bba2c361STejun Heo .validate = bpf_scx_validate, 8189bba2c361STejun Heo .name = "sched_ext_ops_cid", 8190bba2c361STejun Heo .owner = THIS_MODULE, 8191bba2c361STejun Heo .cfi_stubs = &__bpf_ops_sched_ext_ops_cid 8192bba2c361STejun Heo }; 8193bba2c361STejun Heo 8194bba2c361STejun Heo 8195bba2c361STejun Heo /******************************************************************************** 8196bba2c361STejun Heo * System integration and init. 8197bba2c361STejun Heo */ 8198bba2c361STejun Heo 8199bba2c361STejun Heo static void sysrq_handle_sched_ext_reset(u8 key) 8200bba2c361STejun Heo { 8201bba2c361STejun Heo struct scx_sched *sch; 8202bba2c361STejun Heo 8203bba2c361STejun Heo sch = rcu_dereference(scx_root); 8204bba2c361STejun Heo if (likely(sch)) 8205bba2c361STejun Heo scx_disable(sch, SCX_EXIT_SYSRQ); 8206bba2c361STejun Heo else 8207bba2c361STejun Heo pr_info("sched_ext: BPF schedulers not loaded\n"); 8208bba2c361STejun Heo } 8209bba2c361STejun Heo 8210bba2c361STejun Heo static const struct sysrq_key_op sysrq_sched_ext_reset_op = { 8211bba2c361STejun Heo .handler = sysrq_handle_sched_ext_reset, 8212bba2c361STejun Heo .help_msg = "reset-sched-ext(S)", 8213bba2c361STejun Heo .action_msg = "Disable sched_ext and revert all tasks to CFS", 8214bba2c361STejun Heo .enable_mask = SYSRQ_ENABLE_RTNICE, 8215bba2c361STejun Heo }; 8216bba2c361STejun Heo 8217bba2c361STejun Heo static void sysrq_handle_sched_ext_dump(u8 key) 8218bba2c361STejun Heo { 8219bba2c361STejun Heo struct scx_exit_info ei = { 8220bba2c361STejun Heo .kind = SCX_EXIT_NONE, 8221bba2c361STejun Heo .exit_cpu = -1, 8222bba2c361STejun Heo .reason = "SysRq-D", 8223bba2c361STejun Heo }; 8224bba2c361STejun Heo struct scx_sched *sch; 8225bba2c361STejun Heo 8226bba2c361STejun Heo list_for_each_entry_rcu(sch, &scx_sched_all, all) 8227bba2c361STejun Heo scx_dump_state(sch, &ei, 0, false); 8228bba2c361STejun Heo } 8229bba2c361STejun Heo 8230bba2c361STejun Heo static const struct sysrq_key_op sysrq_sched_ext_dump_op = { 8231bba2c361STejun Heo .handler = sysrq_handle_sched_ext_dump, 8232bba2c361STejun Heo .help_msg = "dump-sched-ext(D)", 8233bba2c361STejun Heo .action_msg = "Trigger sched_ext debug dump", 8234bba2c361STejun Heo .enable_mask = SYSRQ_ENABLE_RTNICE, 8235bba2c361STejun Heo }; 8236bba2c361STejun Heo 8237bba2c361STejun Heo static bool can_skip_idle_kick(struct rq *rq) 8238bba2c361STejun Heo { 8239bba2c361STejun Heo lockdep_assert_rq_held(rq); 8240bba2c361STejun Heo 8241bba2c361STejun Heo /* 8242bba2c361STejun Heo * We can skip idle kicking if @rq is going to go through at least one 8243bba2c361STejun Heo * full SCX scheduling cycle before going idle. Just checking whether 8244bba2c361STejun Heo * curr is not idle is insufficient because we could be racing 8245bba2c361STejun Heo * balance_one() trying to pull the next task from a remote rq, which 8246bba2c361STejun Heo * may fail, and @rq may become idle afterwards. 8247bba2c361STejun Heo * 8248bba2c361STejun Heo * The race window is small and we don't and can't guarantee that @rq is 8249bba2c361STejun Heo * only kicked while idle anyway. Skip only when sure. 8250bba2c361STejun Heo */ 8251bba2c361STejun Heo return !is_idle_task(rq->curr) && !(rq->scx.flags & SCX_RQ_IN_BALANCE); 8252bba2c361STejun Heo } 8253bba2c361STejun Heo 8254bba2c361STejun Heo static bool kick_one_cpu(s32 cpu, struct rq *this_rq, unsigned long *ksyncs) 8255bba2c361STejun Heo { 8256bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 8257bba2c361STejun Heo struct scx_rq *this_scx = &this_rq->scx; 8258bba2c361STejun Heo const struct sched_class *cur_class; 8259bba2c361STejun Heo bool should_wait = false; 8260bba2c361STejun Heo unsigned long flags; 8261bba2c361STejun Heo 8262bba2c361STejun Heo raw_spin_rq_lock_irqsave(rq, flags); 8263bba2c361STejun Heo cur_class = rq->curr->sched_class; 8264bba2c361STejun Heo 8265bba2c361STejun Heo /* 8266bba2c361STejun Heo * During CPU hotplug, a CPU may depend on kicking itself to make 8267bba2c361STejun Heo * forward progress. Allow kicking self regardless of online state. If 8268bba2c361STejun Heo * @cpu is running a higher class task, we have no control over @cpu. 8269bba2c361STejun Heo * Skip kicking. 8270bba2c361STejun Heo */ 8271bba2c361STejun Heo if ((cpu_online(cpu) || cpu == cpu_of(this_rq)) && 8272bba2c361STejun Heo !sched_class_above(cur_class, &ext_sched_class)) { 8273bba2c361STejun Heo if (cpumask_test_cpu(cpu, this_scx->cpus_to_preempt)) { 8274bba2c361STejun Heo if (cur_class == &ext_sched_class) 8275bba2c361STejun Heo rq->curr->scx.slice = 0; 8276bba2c361STejun Heo cpumask_clear_cpu(cpu, this_scx->cpus_to_preempt); 8277bba2c361STejun Heo } 8278bba2c361STejun Heo 8279bba2c361STejun Heo if (cpumask_test_cpu(cpu, this_scx->cpus_to_wait)) { 8280bba2c361STejun Heo if (cur_class == &ext_sched_class) { 8281bba2c361STejun Heo cpumask_set_cpu(cpu, this_scx->cpus_to_sync); 8282bba2c361STejun Heo ksyncs[cpu] = rq->scx.kick_sync; 8283bba2c361STejun Heo should_wait = true; 8284bba2c361STejun Heo } 8285bba2c361STejun Heo cpumask_clear_cpu(cpu, this_scx->cpus_to_wait); 8286bba2c361STejun Heo } 8287bba2c361STejun Heo 8288bba2c361STejun Heo resched_curr(rq); 8289bba2c361STejun Heo } else { 8290bba2c361STejun Heo cpumask_clear_cpu(cpu, this_scx->cpus_to_preempt); 8291bba2c361STejun Heo cpumask_clear_cpu(cpu, this_scx->cpus_to_wait); 8292bba2c361STejun Heo } 8293bba2c361STejun Heo 8294bba2c361STejun Heo raw_spin_rq_unlock_irqrestore(rq, flags); 8295bba2c361STejun Heo 8296bba2c361STejun Heo return should_wait; 8297bba2c361STejun Heo } 8298bba2c361STejun Heo 8299bba2c361STejun Heo static void kick_one_cpu_if_idle(s32 cpu, struct rq *this_rq) 8300bba2c361STejun Heo { 8301bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 8302bba2c361STejun Heo unsigned long flags; 8303bba2c361STejun Heo 8304bba2c361STejun Heo raw_spin_rq_lock_irqsave(rq, flags); 8305bba2c361STejun Heo 8306bba2c361STejun Heo if (!can_skip_idle_kick(rq) && 8307bba2c361STejun Heo (cpu_online(cpu) || cpu == cpu_of(this_rq))) 8308bba2c361STejun Heo resched_curr(rq); 8309bba2c361STejun Heo 8310bba2c361STejun Heo raw_spin_rq_unlock_irqrestore(rq, flags); 8311bba2c361STejun Heo } 8312bba2c361STejun Heo 8313bba2c361STejun Heo static void kick_cpus_irq_workfn(struct irq_work *irq_work) 8314bba2c361STejun Heo { 8315bba2c361STejun Heo struct rq *this_rq = this_rq(); 8316bba2c361STejun Heo struct scx_rq *this_scx = &this_rq->scx; 8317bba2c361STejun Heo struct scx_kick_syncs __rcu *ksyncs_pcpu = __this_cpu_read(scx_kick_syncs); 8318bba2c361STejun Heo bool should_wait = false; 8319bba2c361STejun Heo unsigned long *ksyncs; 8320bba2c361STejun Heo s32 cpu; 8321bba2c361STejun Heo 8322bba2c361STejun Heo /* can race with free_kick_syncs() during scheduler disable */ 8323bba2c361STejun Heo if (unlikely(!ksyncs_pcpu)) 8324bba2c361STejun Heo return; 8325bba2c361STejun Heo 8326bba2c361STejun Heo ksyncs = rcu_dereference_bh(ksyncs_pcpu)->syncs; 8327bba2c361STejun Heo 8328bba2c361STejun Heo for_each_cpu(cpu, this_scx->cpus_to_kick) { 8329bba2c361STejun Heo should_wait |= kick_one_cpu(cpu, this_rq, ksyncs); 8330bba2c361STejun Heo cpumask_clear_cpu(cpu, this_scx->cpus_to_kick); 8331bba2c361STejun Heo cpumask_clear_cpu(cpu, this_scx->cpus_to_kick_if_idle); 8332bba2c361STejun Heo } 8333bba2c361STejun Heo 8334bba2c361STejun Heo for_each_cpu(cpu, this_scx->cpus_to_kick_if_idle) { 8335bba2c361STejun Heo kick_one_cpu_if_idle(cpu, this_rq); 8336bba2c361STejun Heo cpumask_clear_cpu(cpu, this_scx->cpus_to_kick_if_idle); 8337bba2c361STejun Heo } 8338bba2c361STejun Heo 8339bba2c361STejun Heo /* 8340bba2c361STejun Heo * Can't wait in hardirq — kick_sync can't advance, deadlocking if 8341bba2c361STejun Heo * CPUs wait for each other. Defer to kick_sync_wait_bal_cb(). 8342bba2c361STejun Heo */ 8343bba2c361STejun Heo if (should_wait) { 8344bba2c361STejun Heo raw_spin_rq_lock(this_rq); 8345bba2c361STejun Heo this_scx->kick_sync_pending = true; 8346bba2c361STejun Heo resched_curr(this_rq); 8347bba2c361STejun Heo raw_spin_rq_unlock(this_rq); 8348bba2c361STejun Heo } 8349bba2c361STejun Heo } 8350bba2c361STejun Heo 8351bba2c361STejun Heo /** 8352bba2c361STejun Heo * print_scx_info - print out sched_ext scheduler state 8353bba2c361STejun Heo * @log_lvl: the log level to use when printing 8354bba2c361STejun Heo * @p: target task 8355bba2c361STejun Heo * 8356bba2c361STejun Heo * If a sched_ext scheduler is enabled, print the name and state of the 8357bba2c361STejun Heo * scheduler. If @p is on sched_ext, print further information about the task. 8358bba2c361STejun Heo * 8359bba2c361STejun Heo * This function can be safely called on any task as long as the task_struct 8360bba2c361STejun Heo * itself is accessible. While safe, this function isn't synchronized and may 8361bba2c361STejun Heo * print out mixups or garbages of limited length. 8362bba2c361STejun Heo */ 8363bba2c361STejun Heo void print_scx_info(const char *log_lvl, struct task_struct *p) 8364bba2c361STejun Heo { 8365bba2c361STejun Heo struct scx_sched *sch; 8366bba2c361STejun Heo enum scx_enable_state state = scx_enable_state(); 8367bba2c361STejun Heo const char *all = READ_ONCE(scx_switching_all) ? "+all" : ""; 8368bba2c361STejun Heo char runnable_at_buf[22] = "?"; 8369bba2c361STejun Heo struct sched_class *class; 8370bba2c361STejun Heo unsigned long runnable_at; 8371bba2c361STejun Heo 8372bba2c361STejun Heo guard(rcu)(); 8373bba2c361STejun Heo 8374bba2c361STejun Heo sch = scx_task_sched_rcu(p); 8375bba2c361STejun Heo 8376bba2c361STejun Heo if (!sch) 8377bba2c361STejun Heo return; 8378bba2c361STejun Heo 8379bba2c361STejun Heo /* 8380bba2c361STejun Heo * Carefully check if the task was running on sched_ext, and then 8381bba2c361STejun Heo * carefully copy the time it's been runnable, and its state. 8382bba2c361STejun Heo */ 8383bba2c361STejun Heo if (copy_from_kernel_nofault(&class, &p->sched_class, sizeof(class)) || 8384bba2c361STejun Heo class != &ext_sched_class) { 8385bba2c361STejun Heo printk("%sSched_ext: %s (%s%s)", log_lvl, sch->ops.name, 8386bba2c361STejun Heo scx_enable_state_str[state], all); 8387bba2c361STejun Heo return; 8388bba2c361STejun Heo } 8389bba2c361STejun Heo 8390bba2c361STejun Heo if (!copy_from_kernel_nofault(&runnable_at, &p->scx.runnable_at, 8391bba2c361STejun Heo sizeof(runnable_at))) 8392bba2c361STejun Heo scnprintf(runnable_at_buf, sizeof(runnable_at_buf), "%+ldms", 8393bba2c361STejun Heo jiffies_delta_msecs(runnable_at, jiffies)); 8394bba2c361STejun Heo 8395bba2c361STejun Heo /* print everything onto one line to conserve console space */ 8396bba2c361STejun Heo printk("%sSched_ext: %s (%s%s), task: runnable_at=%s", 8397bba2c361STejun Heo log_lvl, sch->ops.name, scx_enable_state_str[state], all, 8398bba2c361STejun Heo runnable_at_buf); 8399bba2c361STejun Heo } 8400bba2c361STejun Heo 8401bba2c361STejun Heo static int scx_pm_handler(struct notifier_block *nb, unsigned long event, void *ptr) 8402bba2c361STejun Heo { 8403bba2c361STejun Heo struct scx_sched *sch; 8404bba2c361STejun Heo 8405bba2c361STejun Heo guard(rcu)(); 8406bba2c361STejun Heo 8407bba2c361STejun Heo sch = rcu_dereference(scx_root); 8408bba2c361STejun Heo if (!sch) 8409bba2c361STejun Heo return NOTIFY_OK; 8410bba2c361STejun Heo 8411bba2c361STejun Heo /* 8412bba2c361STejun Heo * SCX schedulers often have userspace components which are sometimes 8413bba2c361STejun Heo * involved in critial scheduling paths. PM operations involve freezing 8414bba2c361STejun Heo * userspace which can lead to scheduling misbehaviors including stalls. 8415bba2c361STejun Heo * Let's bypass while PM operations are in progress. 8416bba2c361STejun Heo */ 8417bba2c361STejun Heo switch (event) { 8418bba2c361STejun Heo case PM_HIBERNATION_PREPARE: 8419bba2c361STejun Heo case PM_SUSPEND_PREPARE: 8420bba2c361STejun Heo case PM_RESTORE_PREPARE: 8421bba2c361STejun Heo scx_bypass(sch, true); 8422bba2c361STejun Heo break; 8423bba2c361STejun Heo case PM_POST_HIBERNATION: 8424bba2c361STejun Heo case PM_POST_SUSPEND: 8425bba2c361STejun Heo case PM_POST_RESTORE: 8426bba2c361STejun Heo scx_bypass(sch, false); 8427bba2c361STejun Heo break; 8428bba2c361STejun Heo } 8429bba2c361STejun Heo 8430bba2c361STejun Heo return NOTIFY_OK; 8431bba2c361STejun Heo } 8432bba2c361STejun Heo 8433bba2c361STejun Heo static struct notifier_block scx_pm_notifier = { 8434bba2c361STejun Heo .notifier_call = scx_pm_handler, 8435bba2c361STejun Heo }; 8436bba2c361STejun Heo 8437bba2c361STejun Heo void __init init_sched_ext_class(void) 8438bba2c361STejun Heo { 8439bba2c361STejun Heo s32 cpu, v; 8440bba2c361STejun Heo 8441bba2c361STejun Heo /* 8442bba2c361STejun Heo * The following is to prevent the compiler from optimizing out the enum 8443bba2c361STejun Heo * definitions so that BPF scheduler implementations can use them 8444bba2c361STejun Heo * through the generated vmlinux.h. 8445bba2c361STejun Heo */ 8446bba2c361STejun Heo WRITE_ONCE(v, SCX_ENQ_WAKEUP | SCX_DEQ_SLEEP | SCX_KICK_PREEMPT | 8447bba2c361STejun Heo SCX_TG_ONLINE); 8448bba2c361STejun Heo 8449bba2c361STejun Heo scx_idle_init_masks(); 8450bba2c361STejun Heo 8451bba2c361STejun Heo for_each_possible_cpu(cpu) { 8452bba2c361STejun Heo struct rq *rq = cpu_rq(cpu); 8453bba2c361STejun Heo int n = cpu_to_node(cpu); 8454bba2c361STejun Heo 8455bba2c361STejun Heo /* local_dsq's sch will be set during scx_root_enable() */ 8456bba2c361STejun Heo BUG_ON(init_dsq(&rq->scx.local_dsq, SCX_DSQ_LOCAL, NULL)); 8457bba2c361STejun Heo 8458bba2c361STejun Heo INIT_LIST_HEAD(&rq->scx.runnable_list); 8459bba2c361STejun Heo INIT_LIST_HEAD(&rq->scx.ddsp_deferred_locals); 8460bba2c361STejun Heo 8461bba2c361STejun Heo BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_kick, GFP_KERNEL, n)); 8462bba2c361STejun Heo BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_kick_if_idle, GFP_KERNEL, n)); 8463bba2c361STejun Heo BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_preempt, GFP_KERNEL, n)); 8464bba2c361STejun Heo BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_wait, GFP_KERNEL, n)); 8465bba2c361STejun Heo BUG_ON(!zalloc_cpumask_var_node(&rq->scx.cpus_to_sync, GFP_KERNEL, n)); 8466bba2c361STejun Heo raw_spin_lock_init(&rq->scx.deferred_reenq_lock); 8467bba2c361STejun Heo INIT_LIST_HEAD(&rq->scx.deferred_reenq_locals); 8468bba2c361STejun Heo INIT_LIST_HEAD(&rq->scx.deferred_reenq_users); 8469bba2c361STejun Heo rq->scx.deferred_irq_work = IRQ_WORK_INIT_HARD(deferred_irq_workfn); 8470bba2c361STejun Heo rq->scx.kick_cpus_irq_work = IRQ_WORK_INIT_HARD(kick_cpus_irq_workfn); 8471bba2c361STejun Heo 8472bba2c361STejun Heo if (cpu_online(cpu)) 8473bba2c361STejun Heo cpu_rq(cpu)->scx.flags |= SCX_RQ_ONLINE; 8474bba2c361STejun Heo } 8475bba2c361STejun Heo 8476bba2c361STejun Heo register_sysrq_key('S', &sysrq_sched_ext_reset_op); 8477bba2c361STejun Heo register_sysrq_key('D', &sysrq_sched_ext_dump_op); 8478bba2c361STejun Heo INIT_DELAYED_WORK(&scx_watchdog_work, scx_watchdog_workfn); 8479bba2c361STejun Heo 8480bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 8481bba2c361STejun Heo BUG_ON(rhashtable_init(&scx_sched_hash, &scx_sched_hash_params)); 8482bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 8483bba2c361STejun Heo } 8484bba2c361STejun Heo 8485bba2c361STejun Heo 8486bba2c361STejun Heo /******************************************************************************** 8487bba2c361STejun Heo * Helpers that can be called from the BPF scheduler. 8488bba2c361STejun Heo */ 8489bba2c361STejun Heo static bool scx_vet_enq_flags(struct scx_sched *sch, u64 dsq_id, u64 *enq_flags) 8490bba2c361STejun Heo { 8491bba2c361STejun Heo bool is_local = dsq_id == SCX_DSQ_LOCAL || 8492bba2c361STejun Heo (dsq_id & SCX_DSQ_LOCAL_ON) == SCX_DSQ_LOCAL_ON; 8493bba2c361STejun Heo 8494bba2c361STejun Heo if (*enq_flags & SCX_ENQ_IMMED) { 8495bba2c361STejun Heo if (unlikely(!is_local)) { 8496bba2c361STejun Heo scx_error(sch, "SCX_ENQ_IMMED on a non-local DSQ 0x%llx", dsq_id); 8497bba2c361STejun Heo return false; 8498bba2c361STejun Heo } 8499bba2c361STejun Heo } else if ((sch->ops.flags & SCX_OPS_ALWAYS_ENQ_IMMED) && is_local) { 8500bba2c361STejun Heo *enq_flags |= SCX_ENQ_IMMED; 8501bba2c361STejun Heo } 8502bba2c361STejun Heo 8503bba2c361STejun Heo return true; 8504bba2c361STejun Heo } 8505bba2c361STejun Heo 8506bba2c361STejun Heo static bool scx_dsq_insert_preamble(struct scx_sched *sch, struct task_struct *p, 8507bba2c361STejun Heo u64 dsq_id, u64 *enq_flags) 8508bba2c361STejun Heo { 8509bba2c361STejun Heo lockdep_assert_irqs_disabled(); 8510bba2c361STejun Heo 8511bba2c361STejun Heo if (unlikely(!p)) { 8512bba2c361STejun Heo scx_error(sch, "called with NULL task"); 8513bba2c361STejun Heo return false; 8514bba2c361STejun Heo } 8515bba2c361STejun Heo 8516bba2c361STejun Heo if (unlikely(*enq_flags & __SCX_ENQ_INTERNAL_MASK)) { 8517bba2c361STejun Heo scx_error(sch, "invalid enq_flags 0x%llx", *enq_flags); 8518bba2c361STejun Heo return false; 8519bba2c361STejun Heo } 8520bba2c361STejun Heo 8521bba2c361STejun Heo /* see SCX_EV_INSERT_NOT_OWNED definition */ 8522bba2c361STejun Heo if (unlikely(!scx_task_on_sched(sch, p))) { 8523bba2c361STejun Heo __scx_add_event(sch, SCX_EV_INSERT_NOT_OWNED, 1); 8524bba2c361STejun Heo return false; 8525bba2c361STejun Heo } 8526bba2c361STejun Heo 8527bba2c361STejun Heo if (!scx_vet_enq_flags(sch, dsq_id, enq_flags)) 8528bba2c361STejun Heo return false; 8529bba2c361STejun Heo 8530bba2c361STejun Heo return true; 8531bba2c361STejun Heo } 8532bba2c361STejun Heo 8533bba2c361STejun Heo static void scx_dsq_insert_commit(struct scx_sched *sch, struct task_struct *p, 8534bba2c361STejun Heo u64 dsq_id, u64 enq_flags) 8535bba2c361STejun Heo { 8536bba2c361STejun Heo struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 8537bba2c361STejun Heo struct task_struct *ddsp_task; 8538bba2c361STejun Heo 8539bba2c361STejun Heo ddsp_task = __this_cpu_read(direct_dispatch_task); 8540bba2c361STejun Heo if (ddsp_task) { 8541bba2c361STejun Heo mark_direct_dispatch(sch, ddsp_task, p, dsq_id, enq_flags); 8542bba2c361STejun Heo return; 8543bba2c361STejun Heo } 8544bba2c361STejun Heo 8545bba2c361STejun Heo if (unlikely(dspc->cursor >= sch->dsp_max_batch)) { 8546bba2c361STejun Heo scx_error(sch, "dispatch buffer overflow"); 8547bba2c361STejun Heo return; 8548bba2c361STejun Heo } 8549bba2c361STejun Heo 8550bba2c361STejun Heo dspc->buf[dspc->cursor++] = (struct scx_dsp_buf_ent){ 8551bba2c361STejun Heo .task = p, 8552bba2c361STejun Heo .qseq = atomic_long_read(&p->scx.ops_state) & SCX_OPSS_QSEQ_MASK, 8553bba2c361STejun Heo .dsq_id = dsq_id, 8554bba2c361STejun Heo .enq_flags = enq_flags, 8555bba2c361STejun Heo }; 8556bba2c361STejun Heo } 8557bba2c361STejun Heo 8558bba2c361STejun Heo __bpf_kfunc_start_defs(); 8559bba2c361STejun Heo 8560bba2c361STejun Heo /** 8561bba2c361STejun Heo * scx_bpf_dsq_insert - Insert a task into the FIFO queue of a DSQ 8562bba2c361STejun Heo * @p: task_struct to insert 8563bba2c361STejun Heo * @dsq_id: DSQ to insert into 8564bba2c361STejun Heo * @slice: duration @p can run for in nsecs, 0 to keep the current value 8565bba2c361STejun Heo * @enq_flags: SCX_ENQ_* 8566bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 8567bba2c361STejun Heo * 8568bba2c361STejun Heo * Insert @p into the FIFO queue of the DSQ identified by @dsq_id. It is safe to 8569bba2c361STejun Heo * call this function spuriously. Can be called from ops.enqueue(), 8570bba2c361STejun Heo * ops.select_cpu(), and ops.dispatch(). 8571bba2c361STejun Heo * 8572bba2c361STejun Heo * When called from ops.select_cpu() or ops.enqueue(), it's for direct dispatch 8573bba2c361STejun Heo * and @p must match the task being enqueued. 8574bba2c361STejun Heo * 8575bba2c361STejun Heo * When called from ops.select_cpu(), @enq_flags and @dsp_id are stored, and @p 8576bba2c361STejun Heo * will be directly inserted into the corresponding dispatch queue after 8577bba2c361STejun Heo * ops.select_cpu() returns. If @p is inserted into SCX_DSQ_LOCAL, it will be 8578bba2c361STejun Heo * inserted into the local DSQ of the CPU returned by ops.select_cpu(). 8579bba2c361STejun Heo * @enq_flags are OR'd with the enqueue flags on the enqueue path before the 8580bba2c361STejun Heo * task is inserted. 8581bba2c361STejun Heo * 8582bba2c361STejun Heo * When called from ops.dispatch(), there are no restrictions on @p or @dsq_id 8583bba2c361STejun Heo * and this function can be called upto ops.dispatch_max_batch times to insert 8584bba2c361STejun Heo * multiple tasks. scx_bpf_dispatch_nr_slots() returns the number of the 8585bba2c361STejun Heo * remaining slots. scx_bpf_dsq_move_to_local() flushes the batch and resets the 8586bba2c361STejun Heo * counter. 8587bba2c361STejun Heo * 8588bba2c361STejun Heo * This function doesn't have any locking restrictions and may be called under 8589bba2c361STejun Heo * BPF locks (in the future when BPF introduces more flexible locking). 8590bba2c361STejun Heo * 8591bba2c361STejun Heo * @p is allowed to run for @slice. The scheduling path is triggered on slice 8592bba2c361STejun Heo * exhaustion. If zero, the current residual slice is maintained. If 8593bba2c361STejun Heo * %SCX_SLICE_INF, @p never expires and the BPF scheduler must kick the CPU with 8594bba2c361STejun Heo * scx_bpf_kick_cpu() to trigger scheduling. 8595bba2c361STejun Heo * 8596bba2c361STejun Heo * Returns %true on successful insertion, %false on failure. On the root 8597bba2c361STejun Heo * scheduler, %false return triggers scheduler abort and the caller doesn't need 8598bba2c361STejun Heo * to check the return value. 8599bba2c361STejun Heo */ 8600bba2c361STejun Heo __bpf_kfunc bool scx_bpf_dsq_insert___v2(struct task_struct *p, u64 dsq_id, 8601bba2c361STejun Heo u64 slice, u64 enq_flags, 8602bba2c361STejun Heo const struct bpf_prog_aux *aux) 8603bba2c361STejun Heo { 8604bba2c361STejun Heo struct scx_sched *sch; 8605bba2c361STejun Heo 8606bba2c361STejun Heo guard(rcu)(); 8607bba2c361STejun Heo sch = scx_prog_sched(aux); 8608bba2c361STejun Heo if (unlikely(!sch)) 8609bba2c361STejun Heo return false; 8610bba2c361STejun Heo 8611bba2c361STejun Heo if (!scx_dsq_insert_preamble(sch, p, dsq_id, &enq_flags)) 8612bba2c361STejun Heo return false; 8613bba2c361STejun Heo 8614bba2c361STejun Heo if (slice) 8615bba2c361STejun Heo p->scx.slice = slice; 8616bba2c361STejun Heo else 8617bba2c361STejun Heo p->scx.slice = p->scx.slice ?: 1; 8618bba2c361STejun Heo 8619bba2c361STejun Heo scx_dsq_insert_commit(sch, p, dsq_id, enq_flags); 8620bba2c361STejun Heo 8621bba2c361STejun Heo return true; 8622bba2c361STejun Heo } 8623bba2c361STejun Heo 8624bba2c361STejun Heo /* 8625bba2c361STejun Heo * COMPAT: Will be removed in v6.23 along with the ___v2 suffix. 8626bba2c361STejun Heo */ 8627bba2c361STejun Heo __bpf_kfunc void scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, 8628bba2c361STejun Heo u64 slice, u64 enq_flags, 8629bba2c361STejun Heo const struct bpf_prog_aux *aux) 8630bba2c361STejun Heo { 8631bba2c361STejun Heo scx_bpf_dsq_insert___v2(p, dsq_id, slice, enq_flags, aux); 8632bba2c361STejun Heo } 8633bba2c361STejun Heo 8634bba2c361STejun Heo static bool scx_dsq_insert_vtime(struct scx_sched *sch, struct task_struct *p, 8635bba2c361STejun Heo u64 dsq_id, u64 slice, u64 vtime, u64 enq_flags) 8636bba2c361STejun Heo { 8637bba2c361STejun Heo if (!scx_dsq_insert_preamble(sch, p, dsq_id, &enq_flags)) 8638bba2c361STejun Heo return false; 8639bba2c361STejun Heo 8640bba2c361STejun Heo if (slice) 8641bba2c361STejun Heo p->scx.slice = slice; 8642bba2c361STejun Heo else 8643bba2c361STejun Heo p->scx.slice = p->scx.slice ?: 1; 8644bba2c361STejun Heo 8645bba2c361STejun Heo p->scx.dsq_vtime = vtime; 8646bba2c361STejun Heo 8647bba2c361STejun Heo scx_dsq_insert_commit(sch, p, dsq_id, enq_flags | SCX_ENQ_DSQ_PRIQ); 8648bba2c361STejun Heo 8649bba2c361STejun Heo return true; 8650bba2c361STejun Heo } 8651bba2c361STejun Heo 8652bba2c361STejun Heo struct scx_bpf_dsq_insert_vtime_args { 8653bba2c361STejun Heo /* @p can't be packed together as KF_RCU is not transitive */ 8654bba2c361STejun Heo u64 dsq_id; 8655bba2c361STejun Heo u64 slice; 8656bba2c361STejun Heo u64 vtime; 8657bba2c361STejun Heo u64 enq_flags; 8658bba2c361STejun Heo }; 8659bba2c361STejun Heo 8660bba2c361STejun Heo /** 8661bba2c361STejun Heo * __scx_bpf_dsq_insert_vtime - Arg-wrapped vtime DSQ insertion 8662bba2c361STejun Heo * @p: task_struct to insert 8663bba2c361STejun Heo * @args: struct containing the rest of the arguments 8664bba2c361STejun Heo * @args->dsq_id: DSQ to insert into 8665bba2c361STejun Heo * @args->slice: duration @p can run for in nsecs, 0 to keep the current value 8666bba2c361STejun Heo * @args->vtime: @p's ordering inside the vtime-sorted queue of the target DSQ 8667bba2c361STejun Heo * @args->enq_flags: SCX_ENQ_* 8668bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 8669bba2c361STejun Heo * 8670bba2c361STejun Heo * Wrapper kfunc that takes arguments via struct to work around BPF's 5 argument 8671bba2c361STejun Heo * limit. BPF programs should use scx_bpf_dsq_insert_vtime() which is provided 8672bba2c361STejun Heo * as an inline wrapper in common.bpf.h. 8673bba2c361STejun Heo * 8674bba2c361STejun Heo * Insert @p into the vtime priority queue of the DSQ identified by 8675bba2c361STejun Heo * @args->dsq_id. Tasks queued into the priority queue are ordered by 8676bba2c361STejun Heo * @args->vtime. All other aspects are identical to scx_bpf_dsq_insert(). 8677bba2c361STejun Heo * 8678bba2c361STejun Heo * @args->vtime ordering is according to time_before64() which considers 8679bba2c361STejun Heo * wrapping. A numerically larger vtime may indicate an earlier position in the 8680bba2c361STejun Heo * ordering and vice-versa. 8681bba2c361STejun Heo * 8682bba2c361STejun Heo * A DSQ can only be used as a FIFO or priority queue at any given time and this 8683bba2c361STejun Heo * function must not be called on a DSQ which already has one or more FIFO tasks 8684bba2c361STejun Heo * queued and vice-versa. Also, the built-in DSQs (SCX_DSQ_LOCAL and 8685bba2c361STejun Heo * SCX_DSQ_GLOBAL) cannot be used as priority queues. 8686bba2c361STejun Heo * 8687bba2c361STejun Heo * Returns %true on successful insertion, %false on failure. On the root 8688bba2c361STejun Heo * scheduler, %false return triggers scheduler abort and the caller doesn't need 8689bba2c361STejun Heo * to check the return value. 8690bba2c361STejun Heo */ 8691bba2c361STejun Heo __bpf_kfunc bool 8692bba2c361STejun Heo __scx_bpf_dsq_insert_vtime(struct task_struct *p, 8693bba2c361STejun Heo struct scx_bpf_dsq_insert_vtime_args *args, 8694bba2c361STejun Heo const struct bpf_prog_aux *aux) 8695bba2c361STejun Heo { 8696bba2c361STejun Heo struct scx_sched *sch; 8697bba2c361STejun Heo 8698bba2c361STejun Heo guard(rcu)(); 8699bba2c361STejun Heo 8700bba2c361STejun Heo sch = scx_prog_sched(aux); 8701bba2c361STejun Heo if (unlikely(!sch)) 8702bba2c361STejun Heo return false; 8703bba2c361STejun Heo 8704bba2c361STejun Heo return scx_dsq_insert_vtime(sch, p, args->dsq_id, args->slice, 8705bba2c361STejun Heo args->vtime, args->enq_flags); 8706bba2c361STejun Heo } 8707bba2c361STejun Heo 8708bba2c361STejun Heo /* 8709bba2c361STejun Heo * COMPAT: Will be removed in v6.23. 8710bba2c361STejun Heo */ 8711bba2c361STejun Heo __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, 8712bba2c361STejun Heo u64 slice, u64 vtime, u64 enq_flags) 8713bba2c361STejun Heo { 8714bba2c361STejun Heo struct scx_sched *sch; 8715bba2c361STejun Heo 8716bba2c361STejun Heo guard(rcu)(); 8717bba2c361STejun Heo 8718bba2c361STejun Heo sch = rcu_dereference(scx_root); 8719bba2c361STejun Heo if (unlikely(!sch)) 8720bba2c361STejun Heo return; 8721bba2c361STejun Heo 8722bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 8723bba2c361STejun Heo /* 8724bba2c361STejun Heo * Disallow if any sub-scheds are attached. There is no way to tell 8725bba2c361STejun Heo * which scheduler called us, just error out @p's scheduler. 8726bba2c361STejun Heo */ 8727bba2c361STejun Heo if (unlikely(!list_empty(&sch->children))) { 8728bba2c361STejun Heo scx_error(scx_task_sched(p), "__scx_bpf_dsq_insert_vtime() must be used"); 8729bba2c361STejun Heo return; 8730bba2c361STejun Heo } 8731bba2c361STejun Heo #endif 8732bba2c361STejun Heo 8733bba2c361STejun Heo scx_dsq_insert_vtime(sch, p, dsq_id, slice, vtime, enq_flags); 8734bba2c361STejun Heo } 8735bba2c361STejun Heo 8736bba2c361STejun Heo __bpf_kfunc_end_defs(); 8737bba2c361STejun Heo 8738bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch) 8739bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_insert, KF_IMPLICIT_ARGS | KF_RCU) 8740bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_insert___v2, KF_IMPLICIT_ARGS | KF_RCU) 8741bba2c361STejun Heo BTF_ID_FLAGS(func, __scx_bpf_dsq_insert_vtime, KF_IMPLICIT_ARGS | KF_RCU) 8742bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_insert_vtime, KF_RCU) 8743bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_enqueue_dispatch) 8744bba2c361STejun Heo 8745bba2c361STejun Heo static const struct btf_kfunc_id_set scx_kfunc_set_enqueue_dispatch = { 8746bba2c361STejun Heo .owner = THIS_MODULE, 8747bba2c361STejun Heo .set = &scx_kfunc_ids_enqueue_dispatch, 8748bba2c361STejun Heo .filter = scx_kfunc_context_filter, 8749bba2c361STejun Heo }; 8750bba2c361STejun Heo 8751bba2c361STejun Heo static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, 8752bba2c361STejun Heo struct task_struct *p, u64 dsq_id, u64 enq_flags) 8753bba2c361STejun Heo { 8754bba2c361STejun Heo struct scx_dispatch_q *src_dsq = kit->dsq, *dst_dsq; 8755bba2c361STejun Heo struct scx_sched *sch; 8756bba2c361STejun Heo struct rq *this_rq, *src_rq, *locked_rq; 8757bba2c361STejun Heo bool dispatched = false; 8758bba2c361STejun Heo bool in_balance; 8759bba2c361STejun Heo unsigned long flags; 8760bba2c361STejun Heo 8761bba2c361STejun Heo /* 8762bba2c361STejun Heo * The verifier considers an iterator slot initialized on any 8763bba2c361STejun Heo * KF_ITER_NEW return, so a BPF program may legally reach here after 8764bba2c361STejun Heo * bpf_iter_scx_dsq_new() failed and left @kit->dsq NULL. 8765bba2c361STejun Heo */ 8766bba2c361STejun Heo if (unlikely(!src_dsq)) 8767bba2c361STejun Heo return false; 8768bba2c361STejun Heo 8769bba2c361STejun Heo sch = src_dsq->sched; 8770bba2c361STejun Heo 8771bba2c361STejun Heo if (!scx_vet_enq_flags(sch, dsq_id, &enq_flags)) 8772bba2c361STejun Heo return false; 8773bba2c361STejun Heo 8774bba2c361STejun Heo /* 8775bba2c361STejun Heo * If the BPF scheduler keeps calling this function repeatedly, it can 8776bba2c361STejun Heo * cause similar live-lock conditions as consume_dispatch_q(). 8777bba2c361STejun Heo */ 8778bba2c361STejun Heo if (unlikely(READ_ONCE(sch->aborting))) 8779bba2c361STejun Heo return false; 8780bba2c361STejun Heo 8781bba2c361STejun Heo if (unlikely(!scx_task_on_sched(sch, p))) { 8782bba2c361STejun Heo scx_error(sch, "scx_bpf_dsq_move[_vtime]() on %s[%d] but the task belongs to a different scheduler", 8783bba2c361STejun Heo p->comm, p->pid); 8784bba2c361STejun Heo return false; 8785bba2c361STejun Heo } 8786bba2c361STejun Heo 8787bba2c361STejun Heo /* 8788bba2c361STejun Heo * Can be called from either ops.dispatch() locking this_rq() or any 8789bba2c361STejun Heo * context where no rq lock is held. If latter, lock @p's task_rq which 8790bba2c361STejun Heo * we'll likely need anyway. 8791bba2c361STejun Heo */ 8792bba2c361STejun Heo src_rq = task_rq(p); 8793bba2c361STejun Heo 8794bba2c361STejun Heo local_irq_save(flags); 8795bba2c361STejun Heo this_rq = this_rq(); 8796bba2c361STejun Heo in_balance = this_rq->scx.flags & SCX_RQ_IN_BALANCE; 8797bba2c361STejun Heo 8798bba2c361STejun Heo if (in_balance) { 8799bba2c361STejun Heo if (this_rq != src_rq) { 8800bba2c361STejun Heo raw_spin_rq_unlock(this_rq); 8801bba2c361STejun Heo raw_spin_rq_lock(src_rq); 8802bba2c361STejun Heo } 8803bba2c361STejun Heo } else { 8804bba2c361STejun Heo raw_spin_rq_lock(src_rq); 8805bba2c361STejun Heo } 8806bba2c361STejun Heo 8807bba2c361STejun Heo locked_rq = src_rq; 8808bba2c361STejun Heo raw_spin_lock(&src_dsq->lock); 8809bba2c361STejun Heo 8810bba2c361STejun Heo /* did someone else get to it while we dropped the locks? */ 8811bba2c361STejun Heo if (nldsq_cursor_lost_task(&kit->cursor, src_rq, src_dsq, p)) { 8812bba2c361STejun Heo raw_spin_unlock(&src_dsq->lock); 8813bba2c361STejun Heo goto out; 8814bba2c361STejun Heo } 8815bba2c361STejun Heo 8816bba2c361STejun Heo /* @p is still on $src_dsq and stable, determine the destination */ 8817bba2c361STejun Heo dst_dsq = find_dsq_for_dispatch(sch, this_rq, dsq_id, task_cpu(p)); 8818bba2c361STejun Heo 8819bba2c361STejun Heo /* 8820bba2c361STejun Heo * Apply vtime and slice updates before moving so that the new time is 8821bba2c361STejun Heo * visible before inserting into $dst_dsq. @p is still on $src_dsq but 8822bba2c361STejun Heo * this is safe as we're locking it. 8823bba2c361STejun Heo */ 8824bba2c361STejun Heo if (kit->cursor.flags & __SCX_DSQ_ITER_HAS_VTIME) 8825bba2c361STejun Heo p->scx.dsq_vtime = kit->vtime; 8826bba2c361STejun Heo if (kit->cursor.flags & __SCX_DSQ_ITER_HAS_SLICE) 8827bba2c361STejun Heo p->scx.slice = kit->slice; 8828bba2c361STejun Heo 8829bba2c361STejun Heo /* execute move */ 8830bba2c361STejun Heo locked_rq = move_task_between_dsqs(sch, p, enq_flags, src_dsq, dst_dsq); 8831bba2c361STejun Heo dispatched = true; 8832bba2c361STejun Heo out: 8833bba2c361STejun Heo if (in_balance) { 8834bba2c361STejun Heo if (this_rq != locked_rq) { 8835bba2c361STejun Heo raw_spin_rq_unlock(locked_rq); 8836bba2c361STejun Heo raw_spin_rq_lock(this_rq); 8837bba2c361STejun Heo } 8838bba2c361STejun Heo } else { 8839bba2c361STejun Heo raw_spin_rq_unlock_irqrestore(locked_rq, flags); 8840bba2c361STejun Heo } 8841bba2c361STejun Heo 8842bba2c361STejun Heo kit->cursor.flags &= ~(__SCX_DSQ_ITER_HAS_SLICE | 8843bba2c361STejun Heo __SCX_DSQ_ITER_HAS_VTIME); 8844bba2c361STejun Heo return dispatched; 8845bba2c361STejun Heo } 8846bba2c361STejun Heo 8847bba2c361STejun Heo __bpf_kfunc_start_defs(); 8848bba2c361STejun Heo 8849bba2c361STejun Heo /** 8850bba2c361STejun Heo * scx_bpf_dispatch_nr_slots - Return the number of remaining dispatch slots 8851bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 8852bba2c361STejun Heo * 8853bba2c361STejun Heo * Can only be called from ops.dispatch(). 8854bba2c361STejun Heo */ 8855bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_dispatch_nr_slots(const struct bpf_prog_aux *aux) 8856bba2c361STejun Heo { 8857bba2c361STejun Heo struct scx_sched *sch; 8858bba2c361STejun Heo 8859bba2c361STejun Heo guard(rcu)(); 8860bba2c361STejun Heo 8861bba2c361STejun Heo sch = scx_prog_sched(aux); 8862bba2c361STejun Heo if (unlikely(!sch)) 8863bba2c361STejun Heo return 0; 8864bba2c361STejun Heo 8865bba2c361STejun Heo return sch->dsp_max_batch - __this_cpu_read(sch->pcpu->dsp_ctx.cursor); 8866bba2c361STejun Heo } 8867bba2c361STejun Heo 8868bba2c361STejun Heo /** 8869bba2c361STejun Heo * scx_bpf_dispatch_cancel - Cancel the latest dispatch 8870bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 8871bba2c361STejun Heo * 8872bba2c361STejun Heo * Cancel the latest dispatch. Can be called multiple times to cancel further 8873bba2c361STejun Heo * dispatches. Can only be called from ops.dispatch(). 8874bba2c361STejun Heo */ 8875bba2c361STejun Heo __bpf_kfunc void scx_bpf_dispatch_cancel(const struct bpf_prog_aux *aux) 8876bba2c361STejun Heo { 8877bba2c361STejun Heo struct scx_sched *sch; 8878bba2c361STejun Heo struct scx_dsp_ctx *dspc; 8879bba2c361STejun Heo 8880bba2c361STejun Heo guard(rcu)(); 8881bba2c361STejun Heo 8882bba2c361STejun Heo sch = scx_prog_sched(aux); 8883bba2c361STejun Heo if (unlikely(!sch)) 8884bba2c361STejun Heo return; 8885bba2c361STejun Heo 8886bba2c361STejun Heo dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 8887bba2c361STejun Heo 8888bba2c361STejun Heo if (dspc->cursor > 0) 8889bba2c361STejun Heo dspc->cursor--; 8890bba2c361STejun Heo else 8891bba2c361STejun Heo scx_error(sch, "dispatch buffer underflow"); 8892bba2c361STejun Heo } 8893bba2c361STejun Heo 8894bba2c361STejun Heo /** 8895bba2c361STejun Heo * scx_bpf_dsq_move_to_local - move a task from a DSQ to the current CPU's local DSQ 8896bba2c361STejun Heo * @dsq_id: DSQ to move task from. Must be a user-created DSQ 8897bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 8898bba2c361STejun Heo * @enq_flags: %SCX_ENQ_* 8899bba2c361STejun Heo * 8900bba2c361STejun Heo * Move a task from the non-local DSQ identified by @dsq_id to the current CPU's 8901bba2c361STejun Heo * local DSQ for execution with @enq_flags applied. Can only be called from 8902bba2c361STejun Heo * ops.dispatch(). 8903bba2c361STejun Heo * 8904bba2c361STejun Heo * Built-in DSQs (%SCX_DSQ_GLOBAL and %SCX_DSQ_LOCAL*) are not supported as 8905bba2c361STejun Heo * sources. Local DSQs support reenqueueing (a task can be picked up for 8906bba2c361STejun Heo * execution, dequeued for property changes, or reenqueued), but the BPF 8907bba2c361STejun Heo * scheduler cannot directly iterate or move tasks from them. %SCX_DSQ_GLOBAL 8908bba2c361STejun Heo * is similar but also doesn't support reenqueueing, as it maps to multiple 8909bba2c361STejun Heo * per-node DSQs making the scope difficult to define; this may change in the 8910bba2c361STejun Heo * future. 8911bba2c361STejun Heo * 8912bba2c361STejun Heo * This function flushes the in-flight dispatches from scx_bpf_dsq_insert() 8913bba2c361STejun Heo * before trying to move from the specified DSQ. It may also grab rq locks and 8914bba2c361STejun Heo * thus can't be called under any BPF locks. 8915bba2c361STejun Heo * 8916bba2c361STejun Heo * Returns %true if a task has been moved, %false if there isn't any task to 8917bba2c361STejun Heo * move. 8918bba2c361STejun Heo */ 8919bba2c361STejun Heo __bpf_kfunc bool scx_bpf_dsq_move_to_local___v2(u64 dsq_id, u64 enq_flags, 8920bba2c361STejun Heo const struct bpf_prog_aux *aux) 8921bba2c361STejun Heo { 8922bba2c361STejun Heo struct scx_dispatch_q *dsq; 8923bba2c361STejun Heo struct scx_sched *sch; 8924bba2c361STejun Heo struct scx_dsp_ctx *dspc; 8925bba2c361STejun Heo 8926bba2c361STejun Heo guard(rcu)(); 8927bba2c361STejun Heo 8928bba2c361STejun Heo sch = scx_prog_sched(aux); 8929bba2c361STejun Heo if (unlikely(!sch)) 8930bba2c361STejun Heo return false; 8931bba2c361STejun Heo 8932bba2c361STejun Heo if (!scx_vet_enq_flags(sch, SCX_DSQ_LOCAL, &enq_flags)) 8933bba2c361STejun Heo return false; 8934bba2c361STejun Heo 8935bba2c361STejun Heo dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 8936bba2c361STejun Heo 8937bba2c361STejun Heo flush_dispatch_buf(sch, dspc->rq); 8938bba2c361STejun Heo 8939bba2c361STejun Heo dsq = find_user_dsq(sch, dsq_id); 8940bba2c361STejun Heo if (unlikely(!dsq)) { 8941bba2c361STejun Heo scx_error(sch, "invalid DSQ ID 0x%016llx", dsq_id); 8942bba2c361STejun Heo return false; 8943bba2c361STejun Heo } 8944bba2c361STejun Heo 8945bba2c361STejun Heo if (consume_dispatch_q(sch, dspc->rq, dsq, enq_flags)) { 8946bba2c361STejun Heo /* 8947bba2c361STejun Heo * A successfully consumed task can be dequeued before it starts 8948bba2c361STejun Heo * running while the CPU is trying to migrate other dispatched 8949bba2c361STejun Heo * tasks. Bump nr_tasks to tell balance_one() to retry on empty 8950bba2c361STejun Heo * local DSQ. 8951bba2c361STejun Heo */ 8952bba2c361STejun Heo dspc->nr_tasks++; 8953bba2c361STejun Heo return true; 8954bba2c361STejun Heo } else { 8955bba2c361STejun Heo return false; 8956bba2c361STejun Heo } 8957bba2c361STejun Heo } 8958bba2c361STejun Heo 8959bba2c361STejun Heo /* 8960bba2c361STejun Heo * COMPAT: ___v2 was introduced in v7.1. Remove this and ___v2 tag in the future. 8961bba2c361STejun Heo */ 8962bba2c361STejun Heo __bpf_kfunc bool scx_bpf_dsq_move_to_local(u64 dsq_id, const struct bpf_prog_aux *aux) 8963bba2c361STejun Heo { 8964bba2c361STejun Heo return scx_bpf_dsq_move_to_local___v2(dsq_id, 0, aux); 8965bba2c361STejun Heo } 8966bba2c361STejun Heo 8967bba2c361STejun Heo /** 8968bba2c361STejun Heo * scx_bpf_dsq_move_set_slice - Override slice when moving between DSQs 8969bba2c361STejun Heo * @it__iter: DSQ iterator in progress 8970bba2c361STejun Heo * @slice: duration the moved task can run for in nsecs 8971bba2c361STejun Heo * 8972bba2c361STejun Heo * Override the slice of the next task that will be moved from @it__iter using 8973bba2c361STejun Heo * scx_bpf_dsq_move[_vtime](). If this function is not called, the previous 8974bba2c361STejun Heo * slice duration is kept. 8975bba2c361STejun Heo */ 8976bba2c361STejun Heo __bpf_kfunc void scx_bpf_dsq_move_set_slice(struct bpf_iter_scx_dsq *it__iter, 8977bba2c361STejun Heo u64 slice) 8978bba2c361STejun Heo { 8979bba2c361STejun Heo struct bpf_iter_scx_dsq_kern *kit = (void *)it__iter; 8980bba2c361STejun Heo 8981bba2c361STejun Heo kit->slice = slice; 8982bba2c361STejun Heo kit->cursor.flags |= __SCX_DSQ_ITER_HAS_SLICE; 8983bba2c361STejun Heo } 8984bba2c361STejun Heo 8985bba2c361STejun Heo /** 8986bba2c361STejun Heo * scx_bpf_dsq_move_set_vtime - Override vtime when moving between DSQs 8987bba2c361STejun Heo * @it__iter: DSQ iterator in progress 8988bba2c361STejun Heo * @vtime: task's ordering inside the vtime-sorted queue of the target DSQ 8989bba2c361STejun Heo * 8990bba2c361STejun Heo * Override the vtime of the next task that will be moved from @it__iter using 8991bba2c361STejun Heo * scx_bpf_dsq_move_vtime(). If this function is not called, the previous slice 8992bba2c361STejun Heo * vtime is kept. If scx_bpf_dsq_move() is used to dispatch the next task, the 8993bba2c361STejun Heo * override is ignored and cleared. 8994bba2c361STejun Heo */ 8995bba2c361STejun Heo __bpf_kfunc void scx_bpf_dsq_move_set_vtime(struct bpf_iter_scx_dsq *it__iter, 8996bba2c361STejun Heo u64 vtime) 8997bba2c361STejun Heo { 8998bba2c361STejun Heo struct bpf_iter_scx_dsq_kern *kit = (void *)it__iter; 8999bba2c361STejun Heo 9000bba2c361STejun Heo kit->vtime = vtime; 9001bba2c361STejun Heo kit->cursor.flags |= __SCX_DSQ_ITER_HAS_VTIME; 9002bba2c361STejun Heo } 9003bba2c361STejun Heo 9004bba2c361STejun Heo /** 9005bba2c361STejun Heo * scx_bpf_dsq_move - Move a task from DSQ iteration to a DSQ 9006bba2c361STejun Heo * @it__iter: DSQ iterator in progress 9007bba2c361STejun Heo * @p: task to transfer 9008bba2c361STejun Heo * @dsq_id: DSQ to move @p to 9009bba2c361STejun Heo * @enq_flags: SCX_ENQ_* 9010bba2c361STejun Heo * 9011bba2c361STejun Heo * Transfer @p which is on the DSQ currently iterated by @it__iter to the DSQ 9012bba2c361STejun Heo * specified by @dsq_id. All DSQs - local DSQs, global DSQ and user DSQs - can 9013bba2c361STejun Heo * be the destination. 9014bba2c361STejun Heo * 9015bba2c361STejun Heo * For the transfer to be successful, @p must still be on the DSQ and have been 9016bba2c361STejun Heo * queued before the DSQ iteration started. This function doesn't care whether 9017bba2c361STejun Heo * @p was obtained from the DSQ iteration. @p just has to be on the DSQ and have 9018bba2c361STejun Heo * been queued before the iteration started. 9019bba2c361STejun Heo * 9020bba2c361STejun Heo * @p's slice is kept by default. Use scx_bpf_dsq_move_set_slice() to update. 9021bba2c361STejun Heo * 9022bba2c361STejun Heo * Can be called from ops.dispatch() or any BPF context which doesn't hold a rq 9023bba2c361STejun Heo * lock (e.g. BPF timers or SYSCALL programs). 9024bba2c361STejun Heo * 9025bba2c361STejun Heo * Returns %true if @p has been consumed, %false if @p had already been 9026bba2c361STejun Heo * consumed, dequeued, or, for sub-scheds, @dsq_id points to a disallowed local 9027bba2c361STejun Heo * DSQ. 9028bba2c361STejun Heo */ 9029bba2c361STejun Heo __bpf_kfunc bool scx_bpf_dsq_move(struct bpf_iter_scx_dsq *it__iter, 9030bba2c361STejun Heo struct task_struct *p, u64 dsq_id, 9031bba2c361STejun Heo u64 enq_flags) 9032bba2c361STejun Heo { 9033bba2c361STejun Heo return scx_dsq_move((struct bpf_iter_scx_dsq_kern *)it__iter, 9034bba2c361STejun Heo p, dsq_id, enq_flags); 9035bba2c361STejun Heo } 9036bba2c361STejun Heo 9037bba2c361STejun Heo /** 9038bba2c361STejun Heo * scx_bpf_dsq_move_vtime - Move a task from DSQ iteration to a PRIQ DSQ 9039bba2c361STejun Heo * @it__iter: DSQ iterator in progress 9040bba2c361STejun Heo * @p: task to transfer 9041bba2c361STejun Heo * @dsq_id: DSQ to move @p to 9042bba2c361STejun Heo * @enq_flags: SCX_ENQ_* 9043bba2c361STejun Heo * 9044bba2c361STejun Heo * Transfer @p which is on the DSQ currently iterated by @it__iter to the 9045bba2c361STejun Heo * priority queue of the DSQ specified by @dsq_id. The destination must be a 9046bba2c361STejun Heo * user DSQ as only user DSQs support priority queue. 9047bba2c361STejun Heo * 9048bba2c361STejun Heo * @p's slice and vtime are kept by default. Use scx_bpf_dsq_move_set_slice() 9049bba2c361STejun Heo * and scx_bpf_dsq_move_set_vtime() to update. 9050bba2c361STejun Heo * 9051bba2c361STejun Heo * All other aspects are identical to scx_bpf_dsq_move(). See 9052bba2c361STejun Heo * scx_bpf_dsq_insert_vtime() for more information on @vtime. 9053bba2c361STejun Heo */ 9054bba2c361STejun Heo __bpf_kfunc bool scx_bpf_dsq_move_vtime(struct bpf_iter_scx_dsq *it__iter, 9055bba2c361STejun Heo struct task_struct *p, u64 dsq_id, 9056bba2c361STejun Heo u64 enq_flags) 9057bba2c361STejun Heo { 9058bba2c361STejun Heo return scx_dsq_move((struct bpf_iter_scx_dsq_kern *)it__iter, 9059bba2c361STejun Heo p, dsq_id, enq_flags | SCX_ENQ_DSQ_PRIQ); 9060bba2c361STejun Heo } 9061bba2c361STejun Heo 9062bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 9063bba2c361STejun Heo /** 9064bba2c361STejun Heo * scx_bpf_sub_dispatch - Trigger dispatching on a child scheduler 9065bba2c361STejun Heo * @cgroup_id: cgroup ID of the child scheduler to dispatch 9066bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9067bba2c361STejun Heo * 9068bba2c361STejun Heo * Allows a parent scheduler to trigger dispatching on one of its direct 9069bba2c361STejun Heo * child schedulers. The child scheduler runs its dispatch operation to 9070bba2c361STejun Heo * move tasks from dispatch queues to the local runqueue. 9071bba2c361STejun Heo * 9072bba2c361STejun Heo * Returns: true on success, false if cgroup_id is invalid, not a direct 9073bba2c361STejun Heo * child, or caller lacks dispatch permission. 9074bba2c361STejun Heo */ 9075bba2c361STejun Heo __bpf_kfunc bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux) 9076bba2c361STejun Heo { 9077bba2c361STejun Heo struct rq *this_rq = this_rq(); 9078bba2c361STejun Heo struct scx_sched *parent, *child; 9079bba2c361STejun Heo 9080bba2c361STejun Heo guard(rcu)(); 9081bba2c361STejun Heo parent = scx_prog_sched(aux); 9082bba2c361STejun Heo if (unlikely(!parent)) 9083bba2c361STejun Heo return false; 9084bba2c361STejun Heo 9085bba2c361STejun Heo child = scx_find_sub_sched(cgroup_id); 9086bba2c361STejun Heo 9087bba2c361STejun Heo if (unlikely(!child)) 9088bba2c361STejun Heo return false; 9089bba2c361STejun Heo 9090bba2c361STejun Heo if (unlikely(scx_parent(child) != parent)) { 9091bba2c361STejun Heo scx_error(parent, "trying to dispatch a distant sub-sched on cgroup %llu", 9092bba2c361STejun Heo cgroup_id); 9093bba2c361STejun Heo return false; 9094bba2c361STejun Heo } 9095bba2c361STejun Heo 9096bba2c361STejun Heo return scx_dispatch_sched(child, this_rq, this_rq->scx.sub_dispatch_prev, 9097bba2c361STejun Heo true); 9098bba2c361STejun Heo } 9099bba2c361STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 9100bba2c361STejun Heo 9101bba2c361STejun Heo __bpf_kfunc_end_defs(); 9102bba2c361STejun Heo 9103bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_dispatch) 9104bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dispatch_nr_slots, KF_IMPLICIT_ARGS) 9105bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dispatch_cancel, KF_IMPLICIT_ARGS) 9106bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_to_local, KF_IMPLICIT_ARGS) 9107bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_to_local___v2, KF_IMPLICIT_ARGS) 9108bba2c361STejun Heo /* scx_bpf_dsq_move*() also in scx_kfunc_ids_unlocked: callable from unlocked contexts */ 9109bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_slice, KF_RCU) 9110bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_vtime, KF_RCU) 9111bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move, KF_RCU) 9112bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_vtime, KF_RCU) 9113bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 9114bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_sub_dispatch, KF_IMPLICIT_ARGS) 9115bba2c361STejun Heo #endif 9116bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_dispatch) 9117bba2c361STejun Heo 9118bba2c361STejun Heo static const struct btf_kfunc_id_set scx_kfunc_set_dispatch = { 9119bba2c361STejun Heo .owner = THIS_MODULE, 9120bba2c361STejun Heo .set = &scx_kfunc_ids_dispatch, 9121bba2c361STejun Heo .filter = scx_kfunc_context_filter, 9122bba2c361STejun Heo }; 9123bba2c361STejun Heo 9124bba2c361STejun Heo __bpf_kfunc_start_defs(); 9125bba2c361STejun Heo 9126bba2c361STejun Heo /** 9127bba2c361STejun Heo * scx_bpf_reenqueue_local - Re-enqueue tasks on a local DSQ 9128bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9129bba2c361STejun Heo * 9130bba2c361STejun Heo * Iterate over all of the tasks currently enqueued on the local DSQ of the 9131bba2c361STejun Heo * caller's CPU, and re-enqueue them in the BPF scheduler. Returns the number of 9132bba2c361STejun Heo * processed tasks. Can only be called from ops.cpu_release(). 9133bba2c361STejun Heo */ 9134bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_reenqueue_local(const struct bpf_prog_aux *aux) 9135bba2c361STejun Heo { 9136bba2c361STejun Heo struct scx_sched *sch; 9137bba2c361STejun Heo struct rq *rq; 9138bba2c361STejun Heo 9139bba2c361STejun Heo guard(rcu)(); 9140bba2c361STejun Heo sch = scx_prog_sched(aux); 9141bba2c361STejun Heo if (unlikely(!sch)) 9142bba2c361STejun Heo return 0; 9143bba2c361STejun Heo 9144bba2c361STejun Heo rq = cpu_rq(smp_processor_id()); 9145bba2c361STejun Heo lockdep_assert_rq_held(rq); 9146bba2c361STejun Heo 9147bba2c361STejun Heo return reenq_local(sch, rq, SCX_REENQ_ANY); 9148bba2c361STejun Heo } 9149bba2c361STejun Heo 9150bba2c361STejun Heo __bpf_kfunc_end_defs(); 9151bba2c361STejun Heo 9152bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_cpu_release) 9153bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_reenqueue_local, KF_IMPLICIT_ARGS) 9154bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_cpu_release) 9155bba2c361STejun Heo 9156bba2c361STejun Heo static const struct btf_kfunc_id_set scx_kfunc_set_cpu_release = { 9157bba2c361STejun Heo .owner = THIS_MODULE, 9158bba2c361STejun Heo .set = &scx_kfunc_ids_cpu_release, 9159bba2c361STejun Heo .filter = scx_kfunc_context_filter, 9160bba2c361STejun Heo }; 9161bba2c361STejun Heo 9162bba2c361STejun Heo __bpf_kfunc_start_defs(); 9163bba2c361STejun Heo 9164bba2c361STejun Heo /** 9165bba2c361STejun Heo * scx_bpf_create_dsq - Create a custom DSQ 9166bba2c361STejun Heo * @dsq_id: DSQ to create 9167bba2c361STejun Heo * @node: NUMA node to allocate from 9168bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9169bba2c361STejun Heo * 9170bba2c361STejun Heo * Create a custom DSQ identified by @dsq_id. Can be called from any sleepable 9171bba2c361STejun Heo * scx callback, and any BPF_PROG_TYPE_SYSCALL prog. 9172bba2c361STejun Heo */ 9173bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_create_dsq(u64 dsq_id, s32 node, const struct bpf_prog_aux *aux) 9174bba2c361STejun Heo { 9175bba2c361STejun Heo struct scx_dispatch_q *dsq; 9176bba2c361STejun Heo struct scx_sched *sch; 9177bba2c361STejun Heo s32 ret; 9178bba2c361STejun Heo 9179bba2c361STejun Heo if (unlikely(node >= (int)nr_node_ids || 9180bba2c361STejun Heo (node < 0 && node != NUMA_NO_NODE))) 9181bba2c361STejun Heo return -EINVAL; 9182bba2c361STejun Heo 9183bba2c361STejun Heo if (unlikely(dsq_id & SCX_DSQ_FLAG_BUILTIN)) 9184bba2c361STejun Heo return -EINVAL; 9185bba2c361STejun Heo 9186bba2c361STejun Heo dsq = kmalloc_node(sizeof(*dsq), GFP_KERNEL, node); 9187bba2c361STejun Heo if (!dsq) 9188bba2c361STejun Heo return -ENOMEM; 9189bba2c361STejun Heo 9190bba2c361STejun Heo /* 9191bba2c361STejun Heo * init_dsq() must be called in GFP_KERNEL context. Init it with NULL 9192bba2c361STejun Heo * @sch and update afterwards. 9193bba2c361STejun Heo */ 9194bba2c361STejun Heo ret = init_dsq(dsq, dsq_id, NULL); 9195bba2c361STejun Heo if (ret) { 9196bba2c361STejun Heo kfree(dsq); 9197bba2c361STejun Heo return ret; 9198bba2c361STejun Heo } 9199bba2c361STejun Heo 9200bba2c361STejun Heo rcu_read_lock(); 9201bba2c361STejun Heo 9202bba2c361STejun Heo sch = scx_prog_sched(aux); 9203bba2c361STejun Heo if (sch) { 9204bba2c361STejun Heo dsq->sched = sch; 9205bba2c361STejun Heo ret = rhashtable_lookup_insert_fast(&sch->dsq_hash, &dsq->hash_node, 9206bba2c361STejun Heo dsq_hash_params); 9207bba2c361STejun Heo } else { 9208bba2c361STejun Heo ret = -ENODEV; 9209bba2c361STejun Heo } 9210bba2c361STejun Heo 9211bba2c361STejun Heo rcu_read_unlock(); 9212bba2c361STejun Heo if (ret) { 9213bba2c361STejun Heo exit_dsq(dsq); 9214bba2c361STejun Heo kfree(dsq); 9215bba2c361STejun Heo } 9216bba2c361STejun Heo return ret; 9217bba2c361STejun Heo } 9218bba2c361STejun Heo 9219bba2c361STejun Heo __bpf_kfunc_end_defs(); 9220bba2c361STejun Heo 9221bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_unlocked) 9222bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_create_dsq, KF_IMPLICIT_ARGS | KF_SLEEPABLE) 9223bba2c361STejun Heo /* also in scx_kfunc_ids_dispatch: also callable from ops.dispatch() */ 9224bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_slice, KF_RCU) 9225bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_set_vtime, KF_RCU) 9226bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move, KF_RCU) 9227bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_move_vtime, KF_RCU) 9228bba2c361STejun Heo /* also in scx_kfunc_ids_select_cpu: also callable from ops.select_cpu()/ops.enqueue() */ 9229bba2c361STejun Heo BTF_ID_FLAGS(func, __scx_bpf_select_cpu_and, KF_IMPLICIT_ARGS | KF_RCU) 9230bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_select_cpu_and, KF_RCU) 9231bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_select_cpu_dfl, KF_IMPLICIT_ARGS | KF_RCU) 9232bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_unlocked) 9233bba2c361STejun Heo 9234bba2c361STejun Heo static const struct btf_kfunc_id_set scx_kfunc_set_unlocked = { 9235bba2c361STejun Heo .owner = THIS_MODULE, 9236bba2c361STejun Heo .set = &scx_kfunc_ids_unlocked, 9237bba2c361STejun Heo .filter = scx_kfunc_context_filter, 9238bba2c361STejun Heo }; 9239bba2c361STejun Heo 9240bba2c361STejun Heo __bpf_kfunc_start_defs(); 9241bba2c361STejun Heo 9242bba2c361STejun Heo /** 9243bba2c361STejun Heo * scx_bpf_task_set_slice - Set task's time slice 9244bba2c361STejun Heo * @p: task of interest 9245bba2c361STejun Heo * @slice: time slice to set in nsecs 9246bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9247bba2c361STejun Heo * 9248bba2c361STejun Heo * Set @p's time slice to @slice. Returns %true on success, %false if the 9249bba2c361STejun Heo * calling scheduler doesn't have authority over @p. 9250bba2c361STejun Heo */ 9251bba2c361STejun Heo __bpf_kfunc bool scx_bpf_task_set_slice(struct task_struct *p, u64 slice, 9252bba2c361STejun Heo const struct bpf_prog_aux *aux) 9253bba2c361STejun Heo { 9254bba2c361STejun Heo struct scx_sched *sch; 9255bba2c361STejun Heo 9256bba2c361STejun Heo guard(rcu)(); 9257bba2c361STejun Heo sch = scx_prog_sched(aux); 9258bba2c361STejun Heo if (unlikely(!sch || !scx_task_on_sched(sch, p))) 9259bba2c361STejun Heo return false; 9260bba2c361STejun Heo 9261bba2c361STejun Heo p->scx.slice = slice; 9262bba2c361STejun Heo return true; 9263bba2c361STejun Heo } 9264bba2c361STejun Heo 9265bba2c361STejun Heo /** 9266bba2c361STejun Heo * scx_bpf_task_set_dsq_vtime - Set task's virtual time for DSQ ordering 9267bba2c361STejun Heo * @p: task of interest 9268bba2c361STejun Heo * @vtime: virtual time to set 9269bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9270bba2c361STejun Heo * 9271bba2c361STejun Heo * Set @p's virtual time to @vtime. Returns %true on success, %false if the 9272bba2c361STejun Heo * calling scheduler doesn't have authority over @p. 9273bba2c361STejun Heo */ 9274bba2c361STejun Heo __bpf_kfunc bool scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime, 9275bba2c361STejun Heo const struct bpf_prog_aux *aux) 9276bba2c361STejun Heo { 9277bba2c361STejun Heo struct scx_sched *sch; 9278bba2c361STejun Heo 9279bba2c361STejun Heo guard(rcu)(); 9280bba2c361STejun Heo sch = scx_prog_sched(aux); 9281bba2c361STejun Heo if (unlikely(!sch || !scx_task_on_sched(sch, p))) 9282bba2c361STejun Heo return false; 9283bba2c361STejun Heo 9284bba2c361STejun Heo p->scx.dsq_vtime = vtime; 9285bba2c361STejun Heo return true; 9286bba2c361STejun Heo } 9287bba2c361STejun Heo 9288bba2c361STejun Heo static void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) 9289bba2c361STejun Heo { 9290bba2c361STejun Heo struct rq *this_rq; 9291bba2c361STejun Heo unsigned long irq_flags; 9292bba2c361STejun Heo 9293bba2c361STejun Heo local_irq_save(irq_flags); 9294bba2c361STejun Heo 9295bba2c361STejun Heo this_rq = this_rq(); 9296bba2c361STejun Heo 9297bba2c361STejun Heo /* 9298bba2c361STejun Heo * While bypassing for PM ops, IRQ handling may not be online which can 9299bba2c361STejun Heo * lead to irq_work_queue() malfunction such as infinite busy wait for 9300bba2c361STejun Heo * IRQ status update. Suppress kicking. 9301bba2c361STejun Heo */ 9302bba2c361STejun Heo if (scx_bypassing(sch, cpu_of(this_rq))) 9303bba2c361STejun Heo goto out; 9304bba2c361STejun Heo 9305bba2c361STejun Heo /* 9306bba2c361STejun Heo * Actual kicking is bounced to kick_cpus_irq_workfn() to avoid nesting 9307bba2c361STejun Heo * rq locks. We can probably be smarter and avoid bouncing if called 9308bba2c361STejun Heo * from ops which don't hold a rq lock. 9309bba2c361STejun Heo */ 9310bba2c361STejun Heo if (flags & SCX_KICK_IDLE) { 9311bba2c361STejun Heo struct rq *target_rq = cpu_rq(cpu); 9312bba2c361STejun Heo 9313bba2c361STejun Heo if (unlikely(flags & (SCX_KICK_PREEMPT | SCX_KICK_WAIT))) 9314bba2c361STejun Heo scx_error(sch, "PREEMPT/WAIT cannot be used with SCX_KICK_IDLE"); 9315bba2c361STejun Heo 9316bba2c361STejun Heo if (raw_spin_rq_trylock(target_rq)) { 9317bba2c361STejun Heo if (can_skip_idle_kick(target_rq)) { 9318bba2c361STejun Heo raw_spin_rq_unlock(target_rq); 9319bba2c361STejun Heo goto out; 9320bba2c361STejun Heo } 9321bba2c361STejun Heo raw_spin_rq_unlock(target_rq); 9322bba2c361STejun Heo } 9323bba2c361STejun Heo cpumask_set_cpu(cpu, this_rq->scx.cpus_to_kick_if_idle); 9324bba2c361STejun Heo } else { 9325bba2c361STejun Heo cpumask_set_cpu(cpu, this_rq->scx.cpus_to_kick); 9326bba2c361STejun Heo 9327bba2c361STejun Heo if (flags & SCX_KICK_PREEMPT) 9328bba2c361STejun Heo cpumask_set_cpu(cpu, this_rq->scx.cpus_to_preempt); 9329bba2c361STejun Heo if (flags & SCX_KICK_WAIT) 9330bba2c361STejun Heo cpumask_set_cpu(cpu, this_rq->scx.cpus_to_wait); 9331bba2c361STejun Heo } 9332bba2c361STejun Heo 9333bba2c361STejun Heo irq_work_queue(&this_rq->scx.kick_cpus_irq_work); 9334bba2c361STejun Heo out: 9335bba2c361STejun Heo local_irq_restore(irq_flags); 9336bba2c361STejun Heo } 9337bba2c361STejun Heo 9338bba2c361STejun Heo /** 9339bba2c361STejun Heo * scx_bpf_kick_cpu - Trigger reschedule on a CPU 9340bba2c361STejun Heo * @cpu: cpu to kick 9341bba2c361STejun Heo * @flags: %SCX_KICK_* flags 9342bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9343bba2c361STejun Heo * 9344bba2c361STejun Heo * Kick @cpu into rescheduling. This can be used to wake up an idle CPU or 9345bba2c361STejun Heo * trigger rescheduling on a busy CPU. This can be called from any online 9346bba2c361STejun Heo * scx_ops operation and the actual kicking is performed asynchronously through 9347bba2c361STejun Heo * an irq work. 9348bba2c361STejun Heo */ 9349bba2c361STejun Heo __bpf_kfunc void scx_bpf_kick_cpu(s32 cpu, u64 flags, const struct bpf_prog_aux *aux) 9350bba2c361STejun Heo { 9351bba2c361STejun Heo struct scx_sched *sch; 9352bba2c361STejun Heo 9353bba2c361STejun Heo guard(rcu)(); 9354bba2c361STejun Heo sch = scx_prog_sched(aux); 9355bba2c361STejun Heo if (likely(sch) && scx_cpu_valid(sch, cpu, NULL)) 9356bba2c361STejun Heo scx_kick_cpu(sch, cpu, flags); 9357bba2c361STejun Heo } 9358bba2c361STejun Heo 9359bba2c361STejun Heo /** 9360bba2c361STejun Heo * scx_bpf_kick_cid - Trigger reschedule on the CPU mapped to @cid 9361bba2c361STejun Heo * @cid: cid to kick 9362bba2c361STejun Heo * @flags: %SCX_KICK_* flags 9363bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9364bba2c361STejun Heo * 9365bba2c361STejun Heo * cid-addressed equivalent of scx_bpf_kick_cpu(). Return 0 on success, 9366bba2c361STejun Heo * -errno otherwise. 9367bba2c361STejun Heo */ 9368bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux) 9369bba2c361STejun Heo { 9370bba2c361STejun Heo struct scx_sched *sch; 9371bba2c361STejun Heo s32 cpu; 9372bba2c361STejun Heo 9373bba2c361STejun Heo guard(rcu)(); 9374bba2c361STejun Heo sch = scx_prog_sched(aux); 9375bba2c361STejun Heo if (unlikely(!sch)) 9376bba2c361STejun Heo return -ENODEV; 9377bba2c361STejun Heo cpu = scx_cid_to_cpu(sch, cid); 9378bba2c361STejun Heo if (cpu < 0) 9379bba2c361STejun Heo return cpu; 9380bba2c361STejun Heo scx_kick_cpu(sch, cpu, flags); 9381bba2c361STejun Heo return 0; 9382bba2c361STejun Heo } 9383bba2c361STejun Heo 9384bba2c361STejun Heo /** 9385bba2c361STejun Heo * scx_bpf_dsq_nr_queued - Return the number of queued tasks 9386bba2c361STejun Heo * @dsq_id: id of the DSQ 9387bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9388bba2c361STejun Heo * 9389bba2c361STejun Heo * Return the number of tasks in the DSQ matching @dsq_id. If not found, 9390bba2c361STejun Heo * -%ENOENT is returned. 9391bba2c361STejun Heo */ 9392bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_dsq_nr_queued(u64 dsq_id, const struct bpf_prog_aux *aux) 9393bba2c361STejun Heo { 9394bba2c361STejun Heo struct scx_sched *sch; 9395bba2c361STejun Heo struct scx_dispatch_q *dsq; 9396bba2c361STejun Heo s32 ret; 9397bba2c361STejun Heo 9398bba2c361STejun Heo preempt_disable(); 9399bba2c361STejun Heo 9400bba2c361STejun Heo sch = scx_prog_sched(aux); 9401bba2c361STejun Heo if (unlikely(!sch)) { 9402bba2c361STejun Heo ret = -ENODEV; 9403bba2c361STejun Heo goto out; 9404bba2c361STejun Heo } 9405bba2c361STejun Heo 9406bba2c361STejun Heo if (dsq_id == SCX_DSQ_LOCAL) { 9407bba2c361STejun Heo ret = READ_ONCE(this_rq()->scx.local_dsq.nr); 9408bba2c361STejun Heo goto out; 9409bba2c361STejun Heo } else if ((dsq_id & SCX_DSQ_LOCAL_ON) == SCX_DSQ_LOCAL_ON) { 9410bba2c361STejun Heo s32 cpu = scx_cpu_ret(sch, dsq_id & SCX_DSQ_LOCAL_CPU_MASK); 9411bba2c361STejun Heo 9412bba2c361STejun Heo if (scx_cpu_valid(sch, cpu, NULL)) { 9413bba2c361STejun Heo ret = READ_ONCE(cpu_rq(cpu)->scx.local_dsq.nr); 9414bba2c361STejun Heo goto out; 9415bba2c361STejun Heo } 9416bba2c361STejun Heo } else { 9417bba2c361STejun Heo dsq = find_user_dsq(sch, dsq_id); 9418bba2c361STejun Heo if (dsq) { 9419bba2c361STejun Heo ret = READ_ONCE(dsq->nr); 9420bba2c361STejun Heo goto out; 9421bba2c361STejun Heo } 9422bba2c361STejun Heo } 9423bba2c361STejun Heo ret = -ENOENT; 9424bba2c361STejun Heo out: 9425bba2c361STejun Heo preempt_enable(); 9426bba2c361STejun Heo return ret; 9427bba2c361STejun Heo } 9428bba2c361STejun Heo 9429bba2c361STejun Heo /** 9430bba2c361STejun Heo * scx_bpf_destroy_dsq - Destroy a custom DSQ 9431bba2c361STejun Heo * @dsq_id: DSQ to destroy 9432bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9433bba2c361STejun Heo * 9434bba2c361STejun Heo * Destroy the custom DSQ identified by @dsq_id. Only DSQs created with 9435bba2c361STejun Heo * scx_bpf_create_dsq() can be destroyed. The caller must ensure that the DSQ is 9436bba2c361STejun Heo * empty and no further tasks are dispatched to it. Ignored if called on a DSQ 9437bba2c361STejun Heo * which doesn't exist. Can be called from any online scx_ops operations. 9438bba2c361STejun Heo */ 9439bba2c361STejun Heo __bpf_kfunc void scx_bpf_destroy_dsq(u64 dsq_id, const struct bpf_prog_aux *aux) 9440bba2c361STejun Heo { 9441bba2c361STejun Heo struct scx_sched *sch; 9442bba2c361STejun Heo 9443bba2c361STejun Heo guard(rcu)(); 9444bba2c361STejun Heo sch = scx_prog_sched(aux); 9445bba2c361STejun Heo if (sch) 9446bba2c361STejun Heo destroy_dsq(sch, dsq_id); 9447bba2c361STejun Heo } 9448bba2c361STejun Heo 9449bba2c361STejun Heo /** 9450bba2c361STejun Heo * bpf_iter_scx_dsq_new - Create a DSQ iterator 9451bba2c361STejun Heo * @it: iterator to initialize 9452bba2c361STejun Heo * @dsq_id: DSQ to iterate 9453bba2c361STejun Heo * @flags: %SCX_DSQ_ITER_* 9454bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9455bba2c361STejun Heo * 9456bba2c361STejun Heo * Initialize BPF iterator @it which can be used with bpf_for_each() to walk 9457bba2c361STejun Heo * tasks in the DSQ specified by @dsq_id. Iteration using @it only includes 9458bba2c361STejun Heo * tasks which are already queued when this function is invoked. 9459bba2c361STejun Heo */ 9460bba2c361STejun Heo __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, 9461bba2c361STejun Heo u64 flags, const struct bpf_prog_aux *aux) 9462bba2c361STejun Heo { 9463bba2c361STejun Heo struct bpf_iter_scx_dsq_kern *kit = (void *)it; 9464bba2c361STejun Heo struct scx_sched *sch; 9465bba2c361STejun Heo 9466bba2c361STejun Heo BUILD_BUG_ON(sizeof(struct bpf_iter_scx_dsq_kern) > 9467bba2c361STejun Heo sizeof(struct bpf_iter_scx_dsq)); 9468bba2c361STejun Heo BUILD_BUG_ON(__alignof__(struct bpf_iter_scx_dsq_kern) != 9469bba2c361STejun Heo __alignof__(struct bpf_iter_scx_dsq)); 9470bba2c361STejun Heo BUILD_BUG_ON(__SCX_DSQ_ITER_ALL_FLAGS & 9471bba2c361STejun Heo ((1U << __SCX_DSQ_LNODE_PRIV_SHIFT) - 1)); 9472bba2c361STejun Heo 9473bba2c361STejun Heo /* 9474bba2c361STejun Heo * next() and destroy() will be called regardless of the return value. 9475bba2c361STejun Heo * Always clear $kit->dsq. 9476bba2c361STejun Heo */ 9477bba2c361STejun Heo kit->dsq = NULL; 9478bba2c361STejun Heo 9479bba2c361STejun Heo sch = scx_prog_sched(aux); 9480bba2c361STejun Heo if (unlikely(!sch)) 9481bba2c361STejun Heo return -ENODEV; 9482bba2c361STejun Heo 9483bba2c361STejun Heo if (flags & ~__SCX_DSQ_ITER_USER_FLAGS) 9484bba2c361STejun Heo return -EINVAL; 9485bba2c361STejun Heo 9486bba2c361STejun Heo kit->dsq = find_user_dsq(sch, dsq_id); 9487bba2c361STejun Heo if (!kit->dsq) 9488bba2c361STejun Heo return -ENOENT; 9489bba2c361STejun Heo 9490bba2c361STejun Heo kit->cursor = INIT_DSQ_LIST_CURSOR(kit->cursor, kit->dsq, flags); 9491bba2c361STejun Heo 9492bba2c361STejun Heo return 0; 9493bba2c361STejun Heo } 9494bba2c361STejun Heo 9495bba2c361STejun Heo /** 9496bba2c361STejun Heo * bpf_iter_scx_dsq_next - Progress a DSQ iterator 9497bba2c361STejun Heo * @it: iterator to progress 9498bba2c361STejun Heo * 9499bba2c361STejun Heo * Return the next task. See bpf_iter_scx_dsq_new(). 9500bba2c361STejun Heo */ 9501bba2c361STejun Heo __bpf_kfunc struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) 9502bba2c361STejun Heo { 9503bba2c361STejun Heo struct bpf_iter_scx_dsq_kern *kit = (void *)it; 9504bba2c361STejun Heo 9505bba2c361STejun Heo if (!kit->dsq) 9506bba2c361STejun Heo return NULL; 9507bba2c361STejun Heo 9508bba2c361STejun Heo guard(raw_spinlock_irqsave)(&kit->dsq->lock); 9509bba2c361STejun Heo 9510bba2c361STejun Heo return nldsq_cursor_next_task(&kit->cursor, kit->dsq); 9511bba2c361STejun Heo } 9512bba2c361STejun Heo 9513bba2c361STejun Heo /** 9514bba2c361STejun Heo * bpf_iter_scx_dsq_destroy - Destroy a DSQ iterator 9515bba2c361STejun Heo * @it: iterator to destroy 9516bba2c361STejun Heo * 9517bba2c361STejun Heo * Undo scx_iter_scx_dsq_new(). 9518bba2c361STejun Heo */ 9519bba2c361STejun Heo __bpf_kfunc void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) 9520bba2c361STejun Heo { 9521bba2c361STejun Heo struct bpf_iter_scx_dsq_kern *kit = (void *)it; 9522bba2c361STejun Heo 9523bba2c361STejun Heo if (!kit->dsq) 9524bba2c361STejun Heo return; 9525bba2c361STejun Heo 9526bba2c361STejun Heo if (!list_empty(&kit->cursor.node)) { 9527bba2c361STejun Heo unsigned long flags; 9528bba2c361STejun Heo 9529bba2c361STejun Heo raw_spin_lock_irqsave(&kit->dsq->lock, flags); 9530bba2c361STejun Heo list_del_init(&kit->cursor.node); 9531bba2c361STejun Heo raw_spin_unlock_irqrestore(&kit->dsq->lock, flags); 9532bba2c361STejun Heo } 9533bba2c361STejun Heo kit->dsq = NULL; 9534bba2c361STejun Heo } 9535bba2c361STejun Heo 9536bba2c361STejun Heo /** 9537bba2c361STejun Heo * scx_bpf_dsq_peek - Lockless peek at the first element. 9538bba2c361STejun Heo * @dsq_id: DSQ to examine. 9539bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9540bba2c361STejun Heo * 9541bba2c361STejun Heo * Read the first element in the DSQ. This is semantically equivalent to using 9542bba2c361STejun Heo * the DSQ iterator, but is lockfree. Of course, like any lockless operation, 9543bba2c361STejun Heo * this provides only a point-in-time snapshot, and the contents may change 9544bba2c361STejun Heo * by the time any subsequent locking operation reads the queue. 9545bba2c361STejun Heo * 9546bba2c361STejun Heo * Returns the pointer, or NULL indicates an empty queue OR internal error. 9547bba2c361STejun Heo */ 9548bba2c361STejun Heo __bpf_kfunc struct task_struct *scx_bpf_dsq_peek(u64 dsq_id, 9549bba2c361STejun Heo const struct bpf_prog_aux *aux) 9550bba2c361STejun Heo { 9551bba2c361STejun Heo struct scx_sched *sch; 9552bba2c361STejun Heo struct scx_dispatch_q *dsq; 9553bba2c361STejun Heo 9554bba2c361STejun Heo sch = scx_prog_sched(aux); 9555bba2c361STejun Heo if (unlikely(!sch)) 9556bba2c361STejun Heo return NULL; 9557bba2c361STejun Heo 9558bba2c361STejun Heo if (unlikely(dsq_id & SCX_DSQ_FLAG_BUILTIN)) { 9559bba2c361STejun Heo scx_error(sch, "peek disallowed on builtin DSQ 0x%llx", dsq_id); 9560bba2c361STejun Heo return NULL; 9561bba2c361STejun Heo } 9562bba2c361STejun Heo 9563bba2c361STejun Heo dsq = find_user_dsq(sch, dsq_id); 9564bba2c361STejun Heo if (unlikely(!dsq)) { 9565bba2c361STejun Heo scx_error(sch, "peek on non-existent DSQ 0x%llx", dsq_id); 9566bba2c361STejun Heo return NULL; 9567bba2c361STejun Heo } 9568bba2c361STejun Heo 9569bba2c361STejun Heo return rcu_dereference(dsq->first_task); 9570bba2c361STejun Heo } 9571bba2c361STejun Heo 9572bba2c361STejun Heo /** 9573bba2c361STejun Heo * scx_bpf_dsq_reenq - Re-enqueue tasks on a DSQ 9574bba2c361STejun Heo * @dsq_id: DSQ to re-enqueue 9575bba2c361STejun Heo * @reenq_flags: %SCX_RENQ_* 9576bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9577bba2c361STejun Heo * 9578bba2c361STejun Heo * Iterate over all of the tasks currently enqueued on the DSQ identified by 9579bba2c361STejun Heo * @dsq_id, and re-enqueue them in the BPF scheduler. The following DSQs are 9580bba2c361STejun Heo * supported: 9581bba2c361STejun Heo * 9582bba2c361STejun Heo * - Local DSQs (%SCX_DSQ_LOCAL or %SCX_DSQ_LOCAL_ON | $cpu) 9583bba2c361STejun Heo * - User DSQs 9584bba2c361STejun Heo * 9585bba2c361STejun Heo * Re-enqueues are performed asynchronously. Can be called from anywhere. 9586bba2c361STejun Heo */ 9587bba2c361STejun Heo __bpf_kfunc void scx_bpf_dsq_reenq(u64 dsq_id, u64 reenq_flags, 9588bba2c361STejun Heo const struct bpf_prog_aux *aux) 9589bba2c361STejun Heo { 9590bba2c361STejun Heo struct scx_sched *sch; 9591bba2c361STejun Heo struct scx_dispatch_q *dsq; 9592bba2c361STejun Heo 9593bba2c361STejun Heo guard(preempt)(); 9594bba2c361STejun Heo 9595bba2c361STejun Heo sch = scx_prog_sched(aux); 9596bba2c361STejun Heo if (unlikely(!sch)) 9597bba2c361STejun Heo return; 9598bba2c361STejun Heo 9599bba2c361STejun Heo if (unlikely(reenq_flags & ~__SCX_REENQ_USER_MASK)) { 9600bba2c361STejun Heo scx_error(sch, "invalid SCX_REENQ flags 0x%llx", reenq_flags); 9601bba2c361STejun Heo return; 9602bba2c361STejun Heo } 9603bba2c361STejun Heo 9604bba2c361STejun Heo /* not specifying any filter bits is the same as %SCX_REENQ_ANY */ 9605bba2c361STejun Heo if (!(reenq_flags & __SCX_REENQ_FILTER_MASK)) 9606bba2c361STejun Heo reenq_flags |= SCX_REENQ_ANY; 9607bba2c361STejun Heo 9608bba2c361STejun Heo dsq = find_dsq_for_dispatch(sch, this_rq(), dsq_id, smp_processor_id()); 9609bba2c361STejun Heo schedule_dsq_reenq(sch, dsq, reenq_flags, scx_locked_rq()); 9610bba2c361STejun Heo } 9611bba2c361STejun Heo 9612bba2c361STejun Heo /** 9613bba2c361STejun Heo * scx_bpf_reenqueue_local - Re-enqueue tasks on a local DSQ 9614bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9615bba2c361STejun Heo * 9616bba2c361STejun Heo * Iterate over all of the tasks currently enqueued on the local DSQ of the 9617bba2c361STejun Heo * caller's CPU, and re-enqueue them in the BPF scheduler. Can be called from 9618bba2c361STejun Heo * anywhere. 9619bba2c361STejun Heo * 9620bba2c361STejun Heo * This is now a special case of scx_bpf_dsq_reenq() and may be removed in the 9621bba2c361STejun Heo * future. 9622bba2c361STejun Heo */ 9623bba2c361STejun Heo __bpf_kfunc void scx_bpf_reenqueue_local___v2(const struct bpf_prog_aux *aux) 9624bba2c361STejun Heo { 9625bba2c361STejun Heo scx_bpf_dsq_reenq(SCX_DSQ_LOCAL, 0, aux); 9626bba2c361STejun Heo } 9627bba2c361STejun Heo 9628bba2c361STejun Heo __bpf_kfunc_end_defs(); 9629bba2c361STejun Heo 9630bba2c361STejun Heo __printf(5, 0) 9631bba2c361STejun Heo static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf, 9632bba2c361STejun Heo size_t line_size, char *fmt, unsigned long long *data, 9633bba2c361STejun Heo u32 data__sz) 9634bba2c361STejun Heo { 9635bba2c361STejun Heo struct bpf_bprintf_data bprintf_data = { .get_bin_args = true }; 9636bba2c361STejun Heo s32 ret; 9637bba2c361STejun Heo 9638bba2c361STejun Heo if (data__sz % 8 || data__sz > MAX_BPRINTF_VARARGS * 8 || 9639bba2c361STejun Heo (data__sz && !data)) { 9640bba2c361STejun Heo scx_error(sch, "invalid data=%p and data__sz=%u", (void *)data, data__sz); 9641bba2c361STejun Heo return -EINVAL; 9642bba2c361STejun Heo } 9643bba2c361STejun Heo 9644bba2c361STejun Heo ret = copy_from_kernel_nofault(data_buf, data, data__sz); 9645bba2c361STejun Heo if (ret < 0) { 9646bba2c361STejun Heo scx_error(sch, "failed to read data fields (%d)", ret); 9647bba2c361STejun Heo return ret; 9648bba2c361STejun Heo } 9649bba2c361STejun Heo 9650bba2c361STejun Heo ret = bpf_bprintf_prepare(fmt, UINT_MAX, data_buf, data__sz / 8, 9651bba2c361STejun Heo &bprintf_data); 9652bba2c361STejun Heo if (ret < 0) { 9653bba2c361STejun Heo scx_error(sch, "format preparation failed (%d)", ret); 9654bba2c361STejun Heo return ret; 9655bba2c361STejun Heo } 9656bba2c361STejun Heo 9657bba2c361STejun Heo ret = bstr_printf(line_buf, line_size, fmt, 9658bba2c361STejun Heo bprintf_data.bin_args); 9659bba2c361STejun Heo bpf_bprintf_cleanup(&bprintf_data); 9660bba2c361STejun Heo if (ret < 0) { 9661bba2c361STejun Heo scx_error(sch, "(\"%s\", %p, %u) failed to format", fmt, data, data__sz); 9662bba2c361STejun Heo return ret; 9663bba2c361STejun Heo } 9664bba2c361STejun Heo 9665bba2c361STejun Heo return ret; 9666bba2c361STejun Heo } 9667bba2c361STejun Heo 9668bba2c361STejun Heo __printf(3, 0) 9669bba2c361STejun Heo static s32 bstr_format(struct scx_sched *sch, struct scx_bstr_buf *buf, 9670bba2c361STejun Heo char *fmt, unsigned long long *data, u32 data__sz) 9671bba2c361STejun Heo { 9672bba2c361STejun Heo return __bstr_format(sch, buf->data, buf->line, sizeof(buf->line), 9673bba2c361STejun Heo fmt, data, data__sz); 9674bba2c361STejun Heo } 9675bba2c361STejun Heo 9676bba2c361STejun Heo __bpf_kfunc_start_defs(); 9677bba2c361STejun Heo 9678bba2c361STejun Heo /** 9679bba2c361STejun Heo * scx_bpf_exit_bstr - Gracefully exit the BPF scheduler. 9680bba2c361STejun Heo * @exit_code: Exit value to pass to user space via struct scx_exit_info. 9681bba2c361STejun Heo * @fmt: error message format string 9682bba2c361STejun Heo * @data: format string parameters packaged using ___bpf_fill() macro 9683bba2c361STejun Heo * @data__sz: @data len, must end in '__sz' for the verifier 9684bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9685bba2c361STejun Heo * 9686bba2c361STejun Heo * Indicate that the BPF scheduler wants to exit gracefully, and initiate ops 9687bba2c361STejun Heo * disabling. 9688bba2c361STejun Heo */ 9689bba2c361STejun Heo __printf(2, 0) 9690bba2c361STejun Heo __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt, 9691bba2c361STejun Heo unsigned long long *data, u32 data__sz, 9692bba2c361STejun Heo const struct bpf_prog_aux *aux) 9693bba2c361STejun Heo { 9694bba2c361STejun Heo struct scx_sched *sch; 9695bba2c361STejun Heo unsigned long flags; 9696bba2c361STejun Heo 9697bba2c361STejun Heo raw_spin_lock_irqsave(&scx_exit_bstr_buf_lock, flags); 9698bba2c361STejun Heo sch = scx_prog_sched(aux); 9699bba2c361STejun Heo if (likely(sch) && 9700bba2c361STejun Heo bstr_format(sch, &scx_exit_bstr_buf, fmt, data, data__sz) >= 0) 9701bba2c361STejun Heo scx_exit(sch, SCX_EXIT_UNREG_BPF, exit_code, "%s", scx_exit_bstr_buf.line); 9702bba2c361STejun Heo raw_spin_unlock_irqrestore(&scx_exit_bstr_buf_lock, flags); 9703bba2c361STejun Heo } 9704bba2c361STejun Heo 9705bba2c361STejun Heo /** 9706bba2c361STejun Heo * scx_bpf_error_bstr - Indicate fatal error 9707bba2c361STejun Heo * @fmt: error message format string 9708bba2c361STejun Heo * @data: format string parameters packaged using ___bpf_fill() macro 9709bba2c361STejun Heo * @data__sz: @data len, must end in '__sz' for the verifier 9710bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9711bba2c361STejun Heo * 9712bba2c361STejun Heo * Indicate that the BPF scheduler encountered a fatal error and initiate ops 9713bba2c361STejun Heo * disabling. 9714bba2c361STejun Heo */ 9715bba2c361STejun Heo __printf(1, 0) 9716bba2c361STejun Heo __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data, 9717bba2c361STejun Heo u32 data__sz, const struct bpf_prog_aux *aux) 9718bba2c361STejun Heo { 9719bba2c361STejun Heo struct scx_sched *sch; 9720bba2c361STejun Heo unsigned long flags; 9721bba2c361STejun Heo 9722bba2c361STejun Heo raw_spin_lock_irqsave(&scx_exit_bstr_buf_lock, flags); 9723bba2c361STejun Heo sch = scx_prog_sched(aux); 9724bba2c361STejun Heo if (likely(sch) && 9725bba2c361STejun Heo bstr_format(sch, &scx_exit_bstr_buf, fmt, data, data__sz) >= 0) 9726bba2c361STejun Heo scx_exit(sch, SCX_EXIT_ERROR_BPF, 0, "%s", scx_exit_bstr_buf.line); 9727bba2c361STejun Heo raw_spin_unlock_irqrestore(&scx_exit_bstr_buf_lock, flags); 9728bba2c361STejun Heo } 9729bba2c361STejun Heo 9730bba2c361STejun Heo /** 9731bba2c361STejun Heo * scx_bpf_dump_bstr - Generate extra debug dump specific to the BPF scheduler 9732bba2c361STejun Heo * @fmt: format string 9733bba2c361STejun Heo * @data: format string parameters packaged using ___bpf_fill() macro 9734bba2c361STejun Heo * @data__sz: @data len, must end in '__sz' for the verifier 9735bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9736bba2c361STejun Heo * 9737bba2c361STejun Heo * To be called through scx_bpf_dump() helper from ops.dump(), dump_cpu() and 9738bba2c361STejun Heo * dump_task() to generate extra debug dump specific to the BPF scheduler. 9739bba2c361STejun Heo * 9740bba2c361STejun Heo * The extra dump may be multiple lines. A single line may be split over 9741bba2c361STejun Heo * multiple calls. The last line is automatically terminated. 9742bba2c361STejun Heo */ 9743bba2c361STejun Heo __printf(1, 0) 9744bba2c361STejun Heo __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, 9745bba2c361STejun Heo u32 data__sz, const struct bpf_prog_aux *aux) 9746bba2c361STejun Heo { 9747bba2c361STejun Heo struct scx_sched *sch; 9748bba2c361STejun Heo struct scx_dump_data *dd = &scx_dump_data; 9749bba2c361STejun Heo struct scx_bstr_buf *buf = &dd->buf; 9750bba2c361STejun Heo s32 ret; 9751bba2c361STejun Heo 9752bba2c361STejun Heo guard(rcu)(); 9753bba2c361STejun Heo 9754bba2c361STejun Heo sch = scx_prog_sched(aux); 9755bba2c361STejun Heo if (unlikely(!sch)) 9756bba2c361STejun Heo return; 9757bba2c361STejun Heo 9758bba2c361STejun Heo if (raw_smp_processor_id() != dd->cpu) { 9759bba2c361STejun Heo scx_error(sch, "scx_bpf_dump() must only be called from ops.dump() and friends"); 9760bba2c361STejun Heo return; 9761bba2c361STejun Heo } 9762bba2c361STejun Heo 9763bba2c361STejun Heo /* append the formatted string to the line buf */ 9764bba2c361STejun Heo ret = __bstr_format(sch, buf->data, buf->line + dd->cursor, 9765bba2c361STejun Heo sizeof(buf->line) - dd->cursor, fmt, data, data__sz); 9766bba2c361STejun Heo if (ret < 0) { 9767bba2c361STejun Heo dump_line(dd->s, "%s[!] (\"%s\", %p, %u) failed to format (%d)", 9768bba2c361STejun Heo dd->prefix, fmt, data, data__sz, ret); 9769bba2c361STejun Heo return; 9770bba2c361STejun Heo } 9771bba2c361STejun Heo 9772bba2c361STejun Heo dd->cursor += ret; 9773bba2c361STejun Heo dd->cursor = min_t(s32, dd->cursor, sizeof(buf->line)); 9774bba2c361STejun Heo 9775bba2c361STejun Heo if (!dd->cursor) 9776bba2c361STejun Heo return; 9777bba2c361STejun Heo 9778bba2c361STejun Heo /* 9779bba2c361STejun Heo * If the line buf overflowed or ends in a newline, flush it into the 9780bba2c361STejun Heo * dump. This is to allow the caller to generate a single line over 9781bba2c361STejun Heo * multiple calls. As ops_dump_flush() can also handle multiple lines in 9782bba2c361STejun Heo * the line buf, the only case which can lead to an unexpected 9783bba2c361STejun Heo * truncation is when the caller keeps generating newlines in the middle 9784bba2c361STejun Heo * instead of the end consecutively. Don't do that. 9785bba2c361STejun Heo */ 9786bba2c361STejun Heo if (dd->cursor >= sizeof(buf->line) || buf->line[dd->cursor - 1] == '\n') 9787bba2c361STejun Heo ops_dump_flush(); 9788bba2c361STejun Heo } 9789bba2c361STejun Heo 9790bba2c361STejun Heo /** 9791bba2c361STejun Heo * scx_bpf_cpuperf_cap - Query the maximum relative capacity of a CPU 9792bba2c361STejun Heo * @cpu: CPU of interest 9793bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9794bba2c361STejun Heo * 9795bba2c361STejun Heo * Return the maximum relative capacity of @cpu in relation to the most 9796bba2c361STejun Heo * performant CPU in the system. The return value is in the range [1, 9797bba2c361STejun Heo * %SCX_CPUPERF_ONE]. See scx_bpf_cpuperf_cur(). 9798bba2c361STejun Heo */ 9799bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_cpuperf_cap(s32 cpu, const struct bpf_prog_aux *aux) 9800bba2c361STejun Heo { 9801bba2c361STejun Heo struct scx_sched *sch; 9802bba2c361STejun Heo 9803bba2c361STejun Heo guard(rcu)(); 9804bba2c361STejun Heo 9805bba2c361STejun Heo sch = scx_prog_sched(aux); 9806bba2c361STejun Heo if (likely(sch) && scx_cpu_valid(sch, cpu, NULL)) 9807bba2c361STejun Heo return arch_scale_cpu_capacity(cpu); 9808bba2c361STejun Heo else 9809bba2c361STejun Heo return SCX_CPUPERF_ONE; 9810bba2c361STejun Heo } 9811bba2c361STejun Heo 9812bba2c361STejun Heo /** 9813bba2c361STejun Heo * scx_bpf_cidperf_cap - Query the maximum relative capacity of the CPU at @cid 9814bba2c361STejun Heo * @cid: cid of the CPU to query 9815bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9816bba2c361STejun Heo * 9817bba2c361STejun Heo * cid-addressed equivalent of scx_bpf_cpuperf_cap(). 9818bba2c361STejun Heo */ 9819bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_cidperf_cap(s32 cid, const struct bpf_prog_aux *aux) 9820bba2c361STejun Heo { 9821bba2c361STejun Heo struct scx_sched *sch; 9822bba2c361STejun Heo s32 cpu; 9823bba2c361STejun Heo 9824bba2c361STejun Heo guard(rcu)(); 9825bba2c361STejun Heo 9826bba2c361STejun Heo sch = scx_prog_sched(aux); 9827bba2c361STejun Heo if (unlikely(!sch)) 9828bba2c361STejun Heo return SCX_CPUPERF_ONE; 9829bba2c361STejun Heo cpu = scx_cid_to_cpu(sch, cid); 9830bba2c361STejun Heo if (cpu < 0) 9831bba2c361STejun Heo return SCX_CPUPERF_ONE; 9832bba2c361STejun Heo return arch_scale_cpu_capacity(cpu); 9833bba2c361STejun Heo } 9834bba2c361STejun Heo 9835bba2c361STejun Heo /** 9836bba2c361STejun Heo * scx_bpf_cpuperf_cur - Query the current relative performance of a CPU 9837bba2c361STejun Heo * @cpu: CPU of interest 9838bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9839bba2c361STejun Heo * 9840bba2c361STejun Heo * Return the current relative performance of @cpu in relation to its maximum. 9841bba2c361STejun Heo * The return value is in the range [1, %SCX_CPUPERF_ONE]. 9842bba2c361STejun Heo * 9843bba2c361STejun Heo * The current performance level of a CPU in relation to the maximum performance 9844bba2c361STejun Heo * available in the system can be calculated as follows: 9845bba2c361STejun Heo * 9846bba2c361STejun Heo * scx_bpf_cpuperf_cap() * scx_bpf_cpuperf_cur() / %SCX_CPUPERF_ONE 9847bba2c361STejun Heo * 9848bba2c361STejun Heo * The result is in the range [1, %SCX_CPUPERF_ONE]. 9849bba2c361STejun Heo */ 9850bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_cpuperf_cur(s32 cpu, const struct bpf_prog_aux *aux) 9851bba2c361STejun Heo { 9852bba2c361STejun Heo struct scx_sched *sch; 9853bba2c361STejun Heo 9854bba2c361STejun Heo guard(rcu)(); 9855bba2c361STejun Heo 9856bba2c361STejun Heo sch = scx_prog_sched(aux); 9857bba2c361STejun Heo if (likely(sch) && scx_cpu_valid(sch, cpu, NULL)) 9858bba2c361STejun Heo return arch_scale_freq_capacity(cpu); 9859bba2c361STejun Heo else 9860bba2c361STejun Heo return SCX_CPUPERF_ONE; 9861bba2c361STejun Heo } 9862bba2c361STejun Heo 9863bba2c361STejun Heo /** 9864bba2c361STejun Heo * scx_bpf_cidperf_cur - Query the current performance of the CPU at @cid 9865bba2c361STejun Heo * @cid: cid of the CPU to query 9866bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9867bba2c361STejun Heo * 9868bba2c361STejun Heo * cid-addressed equivalent of scx_bpf_cpuperf_cur(). 9869bba2c361STejun Heo */ 9870bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_cidperf_cur(s32 cid, const struct bpf_prog_aux *aux) 9871bba2c361STejun Heo { 9872bba2c361STejun Heo struct scx_sched *sch; 9873bba2c361STejun Heo s32 cpu; 9874bba2c361STejun Heo 9875bba2c361STejun Heo guard(rcu)(); 9876bba2c361STejun Heo 9877bba2c361STejun Heo sch = scx_prog_sched(aux); 9878bba2c361STejun Heo if (unlikely(!sch)) 9879bba2c361STejun Heo return SCX_CPUPERF_ONE; 9880bba2c361STejun Heo cpu = scx_cid_to_cpu(sch, cid); 9881bba2c361STejun Heo if (cpu < 0) 9882bba2c361STejun Heo return SCX_CPUPERF_ONE; 9883bba2c361STejun Heo return arch_scale_freq_capacity(cpu); 9884bba2c361STejun Heo } 9885bba2c361STejun Heo 9886bba2c361STejun Heo /** 9887bba2c361STejun Heo * scx_bpf_cpuperf_set - Set the relative performance target of a CPU 9888bba2c361STejun Heo * @cpu: CPU of interest 9889bba2c361STejun Heo * @perf: target performance level [0, %SCX_CPUPERF_ONE] 9890bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9891bba2c361STejun Heo * 9892bba2c361STejun Heo * Set the target performance level of @cpu to @perf. @perf is in linear 9893bba2c361STejun Heo * relative scale between 0 and %SCX_CPUPERF_ONE. This determines how the 9894bba2c361STejun Heo * schedutil cpufreq governor chooses the target frequency. 9895bba2c361STejun Heo * 9896bba2c361STejun Heo * The actual performance level chosen, CPU grouping, and the overhead and 9897bba2c361STejun Heo * latency of the operations are dependent on the hardware and cpufreq driver in 9898bba2c361STejun Heo * use. Consult hardware and cpufreq documentation for more information. The 9899bba2c361STejun Heo * current performance level can be monitored using scx_bpf_cpuperf_cur(). 9900bba2c361STejun Heo */ 9901bba2c361STejun Heo __bpf_kfunc void scx_bpf_cpuperf_set(s32 cpu, u32 perf, const struct bpf_prog_aux *aux) 9902bba2c361STejun Heo { 9903bba2c361STejun Heo struct scx_sched *sch; 9904bba2c361STejun Heo 9905bba2c361STejun Heo guard(rcu)(); 9906bba2c361STejun Heo 9907bba2c361STejun Heo sch = scx_prog_sched(aux); 9908bba2c361STejun Heo if (unlikely(!sch)) 9909bba2c361STejun Heo return; 9910bba2c361STejun Heo 9911bba2c361STejun Heo if (unlikely(perf > SCX_CPUPERF_ONE)) { 9912bba2c361STejun Heo scx_error(sch, "Invalid cpuperf target %u for CPU %d", perf, cpu); 9913bba2c361STejun Heo return; 9914bba2c361STejun Heo } 9915bba2c361STejun Heo 9916bba2c361STejun Heo if (scx_cpu_valid(sch, cpu, NULL)) { 9917bba2c361STejun Heo struct rq *rq = cpu_rq(cpu), *locked_rq = scx_locked_rq(); 9918bba2c361STejun Heo struct rq_flags rf; 9919bba2c361STejun Heo 9920bba2c361STejun Heo /* 9921bba2c361STejun Heo * When called with an rq lock held, restrict the operation 9922bba2c361STejun Heo * to the corresponding CPU to prevent ABBA deadlocks. 9923bba2c361STejun Heo */ 9924bba2c361STejun Heo if (locked_rq && rq != locked_rq) { 9925bba2c361STejun Heo scx_error(sch, "Invalid target CPU %d", cpu); 9926bba2c361STejun Heo return; 9927bba2c361STejun Heo } 9928bba2c361STejun Heo 9929bba2c361STejun Heo /* 9930bba2c361STejun Heo * If no rq lock is held, allow to operate on any CPU by 9931bba2c361STejun Heo * acquiring the corresponding rq lock. 9932bba2c361STejun Heo */ 9933bba2c361STejun Heo if (!locked_rq) { 9934bba2c361STejun Heo rq_lock_irqsave(rq, &rf); 9935bba2c361STejun Heo update_rq_clock(rq); 9936bba2c361STejun Heo } 9937bba2c361STejun Heo 9938bba2c361STejun Heo rq->scx.cpuperf_target = perf; 9939bba2c361STejun Heo cpufreq_update_util(rq, 0); 9940bba2c361STejun Heo 9941bba2c361STejun Heo if (!locked_rq) 9942bba2c361STejun Heo rq_unlock_irqrestore(rq, &rf); 9943bba2c361STejun Heo } 9944bba2c361STejun Heo } 9945bba2c361STejun Heo 9946bba2c361STejun Heo /** 9947bba2c361STejun Heo * scx_bpf_cidperf_set - Set the performance target of the CPU at @cid 9948bba2c361STejun Heo * @cid: cid of the CPU to target 9949bba2c361STejun Heo * @perf: target performance level [0, %SCX_CPUPERF_ONE] 9950bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 9951bba2c361STejun Heo * 9952bba2c361STejun Heo * cid-addressed equivalent of scx_bpf_cpuperf_set(). 9953bba2c361STejun Heo */ 9954bba2c361STejun Heo __bpf_kfunc void scx_bpf_cidperf_set(s32 cid, u32 perf, 9955bba2c361STejun Heo const struct bpf_prog_aux *aux) 9956bba2c361STejun Heo { 9957bba2c361STejun Heo struct scx_sched *sch; 9958bba2c361STejun Heo s32 cpu; 9959bba2c361STejun Heo 9960bba2c361STejun Heo guard(rcu)(); 9961bba2c361STejun Heo 9962bba2c361STejun Heo sch = scx_prog_sched(aux); 9963bba2c361STejun Heo if (unlikely(!sch)) 9964bba2c361STejun Heo return; 9965bba2c361STejun Heo cpu = scx_cid_to_cpu(sch, cid); 9966bba2c361STejun Heo if (cpu < 0) 9967bba2c361STejun Heo return; 9968bba2c361STejun Heo scx_bpf_cpuperf_set(cpu, perf, aux); 9969bba2c361STejun Heo } 9970bba2c361STejun Heo 9971bba2c361STejun Heo /** 9972bba2c361STejun Heo * scx_bpf_nr_node_ids - Return the number of possible node IDs 9973bba2c361STejun Heo * 9974bba2c361STejun Heo * All valid node IDs in the system are smaller than the returned value. 9975bba2c361STejun Heo */ 9976bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_nr_node_ids(void) 9977bba2c361STejun Heo { 9978bba2c361STejun Heo return nr_node_ids; 9979bba2c361STejun Heo } 9980bba2c361STejun Heo 9981bba2c361STejun Heo /** 9982bba2c361STejun Heo * scx_bpf_nr_cpu_ids - Return the number of possible CPU IDs 9983bba2c361STejun Heo * 9984bba2c361STejun Heo * All valid CPU IDs in the system are smaller than the returned value. 9985bba2c361STejun Heo */ 9986bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_nr_cpu_ids(void) 9987bba2c361STejun Heo { 9988bba2c361STejun Heo return nr_cpu_ids; 9989bba2c361STejun Heo } 9990bba2c361STejun Heo 9991bba2c361STejun Heo /** 9992bba2c361STejun Heo * scx_bpf_nr_cids - Return the size of the cid space 9993bba2c361STejun Heo * 9994bba2c361STejun Heo * Equals num_possible_cpus(). All valid cids are in [0, return value). 9995bba2c361STejun Heo */ 9996bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_nr_cids(void) 9997bba2c361STejun Heo { 9998bba2c361STejun Heo return num_possible_cpus(); 9999bba2c361STejun Heo } 10000bba2c361STejun Heo 10001bba2c361STejun Heo /** 10002bba2c361STejun Heo * scx_bpf_nr_online_cids - Return current count of online CPUs in cid space 10003bba2c361STejun Heo * 10004bba2c361STejun Heo * Return num_online_cpus(). The standard model restarts the scheduler on 10005bba2c361STejun Heo * hotplug, which lets schedulers treat [0, nr_online_cids) as the online 10006bba2c361STejun Heo * range. Schedulers that prefer to handle hotplug without a restart should 10007bba2c361STejun Heo * install a custom mapping via scx_bpf_cid_override() and track onlining 10008bba2c361STejun Heo * through the ops.cid_online / ops.cid_offline callbacks. 10009bba2c361STejun Heo */ 10010bba2c361STejun Heo __bpf_kfunc u32 scx_bpf_nr_online_cids(void) 10011bba2c361STejun Heo { 10012bba2c361STejun Heo return num_online_cpus(); 10013bba2c361STejun Heo } 10014bba2c361STejun Heo 10015bba2c361STejun Heo /** 10016bba2c361STejun Heo * scx_bpf_this_cid - Return the cid of the CPU this program is running on 10017bba2c361STejun Heo * 10018bba2c361STejun Heo * cid-addressed equivalent of bpf_get_smp_processor_id() for scx programs. 10019bba2c361STejun Heo * The current cpu is trivially valid, so this is just a table lookup. Return 10020bba2c361STejun Heo * -EINVAL if called from a non-SCX program before any scheduler has ever 10021bba2c361STejun Heo * been enabled (the cid table is still unallocated at that point). 10022bba2c361STejun Heo */ 10023bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_this_cid(void) 10024bba2c361STejun Heo { 10025bba2c361STejun Heo s16 *tbl = READ_ONCE(scx_cpu_to_cid_tbl); 10026bba2c361STejun Heo 10027bba2c361STejun Heo if (!tbl) 10028bba2c361STejun Heo return -EINVAL; 10029bba2c361STejun Heo return tbl[raw_smp_processor_id()]; 10030bba2c361STejun Heo } 10031bba2c361STejun Heo 10032bba2c361STejun Heo /** 10033bba2c361STejun Heo * scx_bpf_get_possible_cpumask - Get a referenced kptr to cpu_possible_mask 10034bba2c361STejun Heo */ 10035bba2c361STejun Heo __bpf_kfunc const struct cpumask *scx_bpf_get_possible_cpumask(void) 10036bba2c361STejun Heo { 10037bba2c361STejun Heo return cpu_possible_mask; 10038bba2c361STejun Heo } 10039bba2c361STejun Heo 10040bba2c361STejun Heo /** 10041bba2c361STejun Heo * scx_bpf_get_online_cpumask - Get a referenced kptr to cpu_online_mask 10042bba2c361STejun Heo */ 10043bba2c361STejun Heo __bpf_kfunc const struct cpumask *scx_bpf_get_online_cpumask(void) 10044bba2c361STejun Heo { 10045bba2c361STejun Heo return cpu_online_mask; 10046bba2c361STejun Heo } 10047bba2c361STejun Heo 10048bba2c361STejun Heo /** 10049bba2c361STejun Heo * scx_bpf_put_cpumask - Release a possible/online cpumask 10050bba2c361STejun Heo * @cpumask: cpumask to release 10051bba2c361STejun Heo */ 10052bba2c361STejun Heo __bpf_kfunc void scx_bpf_put_cpumask(const struct cpumask *cpumask) 10053bba2c361STejun Heo { 10054bba2c361STejun Heo /* 10055bba2c361STejun Heo * Empty function body because we aren't actually acquiring or releasing 10056bba2c361STejun Heo * a reference to a global cpumask, which is read-only in the caller and 10057bba2c361STejun Heo * is never released. The acquire / release semantics here are just used 10058bba2c361STejun Heo * to make the cpumask is a trusted pointer in the caller. 10059bba2c361STejun Heo */ 10060bba2c361STejun Heo } 10061bba2c361STejun Heo 10062bba2c361STejun Heo /** 10063bba2c361STejun Heo * scx_bpf_task_running - Is task currently running? 10064bba2c361STejun Heo * @p: task of interest 10065bba2c361STejun Heo */ 10066bba2c361STejun Heo __bpf_kfunc bool scx_bpf_task_running(const struct task_struct *p) 10067bba2c361STejun Heo { 10068bba2c361STejun Heo return task_rq(p)->curr == p; 10069bba2c361STejun Heo } 10070bba2c361STejun Heo 10071bba2c361STejun Heo /** 10072bba2c361STejun Heo * scx_bpf_task_cpu - CPU a task is currently associated with 10073bba2c361STejun Heo * @p: task of interest 10074bba2c361STejun Heo */ 10075bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p) 10076bba2c361STejun Heo { 10077bba2c361STejun Heo return task_cpu(p); 10078bba2c361STejun Heo } 10079bba2c361STejun Heo 10080bba2c361STejun Heo /** 10081bba2c361STejun Heo * scx_bpf_task_cid - cid a task is currently associated with 10082bba2c361STejun Heo * @p: task of interest 10083bba2c361STejun Heo * 10084bba2c361STejun Heo * cid-addressed equivalent of scx_bpf_task_cpu(). task_cpu(p) is always a 10085bba2c361STejun Heo * valid cpu, so this is just a table lookup. Return -EINVAL if called from 10086bba2c361STejun Heo * a non-SCX program before any scheduler has ever been enabled. 10087bba2c361STejun Heo */ 10088bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_task_cid(const struct task_struct *p) 10089bba2c361STejun Heo { 10090bba2c361STejun Heo s16 *tbl = READ_ONCE(scx_cpu_to_cid_tbl); 10091bba2c361STejun Heo 10092bba2c361STejun Heo if (!tbl) 10093bba2c361STejun Heo return -EINVAL; 10094bba2c361STejun Heo return tbl[task_cpu(p)]; 10095bba2c361STejun Heo } 10096bba2c361STejun Heo 10097bba2c361STejun Heo /** 10098bba2c361STejun Heo * scx_bpf_cpu_rq - Fetch the rq of a CPU 10099bba2c361STejun Heo * @cpu: CPU of the rq 10100bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 10101bba2c361STejun Heo */ 10102bba2c361STejun Heo __bpf_kfunc struct rq *scx_bpf_cpu_rq(s32 cpu, const struct bpf_prog_aux *aux) 10103bba2c361STejun Heo { 10104bba2c361STejun Heo struct scx_sched *sch; 10105bba2c361STejun Heo 10106bba2c361STejun Heo guard(rcu)(); 10107bba2c361STejun Heo 10108bba2c361STejun Heo sch = scx_prog_sched(aux); 10109bba2c361STejun Heo if (unlikely(!sch)) 10110bba2c361STejun Heo return NULL; 10111bba2c361STejun Heo 10112bba2c361STejun Heo if (!scx_cpu_valid(sch, cpu, NULL)) 10113bba2c361STejun Heo return NULL; 10114bba2c361STejun Heo 10115bba2c361STejun Heo if (!sch->warned_deprecated_rq) { 10116bba2c361STejun Heo printk_deferred(KERN_WARNING "sched_ext: %s() is deprecated; " 10117bba2c361STejun Heo "use scx_bpf_locked_rq() when holding rq lock " 10118bba2c361STejun Heo "or scx_bpf_cpu_curr() to read remote curr safely.\n", __func__); 10119bba2c361STejun Heo sch->warned_deprecated_rq = true; 10120bba2c361STejun Heo } 10121bba2c361STejun Heo 10122bba2c361STejun Heo return cpu_rq(cpu); 10123bba2c361STejun Heo } 10124bba2c361STejun Heo 10125bba2c361STejun Heo /** 10126bba2c361STejun Heo * scx_bpf_locked_rq - Return the rq currently locked by SCX 10127bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 10128bba2c361STejun Heo * 10129bba2c361STejun Heo * Returns the rq if a rq lock is currently held by SCX. 10130bba2c361STejun Heo * Otherwise emits an error and returns NULL. 10131bba2c361STejun Heo */ 10132bba2c361STejun Heo __bpf_kfunc struct rq *scx_bpf_locked_rq(const struct bpf_prog_aux *aux) 10133bba2c361STejun Heo { 10134bba2c361STejun Heo struct scx_sched *sch; 10135bba2c361STejun Heo struct rq *rq; 10136bba2c361STejun Heo 10137bba2c361STejun Heo guard(preempt)(); 10138bba2c361STejun Heo 10139bba2c361STejun Heo sch = scx_prog_sched(aux); 10140bba2c361STejun Heo if (unlikely(!sch)) 10141bba2c361STejun Heo return NULL; 10142bba2c361STejun Heo 10143bba2c361STejun Heo rq = scx_locked_rq(); 10144bba2c361STejun Heo if (!rq) { 10145bba2c361STejun Heo scx_error(sch, "accessing rq without holding rq lock"); 10146bba2c361STejun Heo return NULL; 10147bba2c361STejun Heo } 10148bba2c361STejun Heo 10149bba2c361STejun Heo return rq; 10150bba2c361STejun Heo } 10151bba2c361STejun Heo 10152bba2c361STejun Heo /** 10153bba2c361STejun Heo * scx_bpf_cpu_curr - Return remote CPU's curr task 10154bba2c361STejun Heo * @cpu: CPU of interest 10155bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 10156bba2c361STejun Heo * 10157bba2c361STejun Heo * Callers must hold RCU read lock (KF_RCU). 10158bba2c361STejun Heo */ 10159bba2c361STejun Heo __bpf_kfunc struct task_struct *scx_bpf_cpu_curr(s32 cpu, const struct bpf_prog_aux *aux) 10160bba2c361STejun Heo { 10161bba2c361STejun Heo struct scx_sched *sch; 10162bba2c361STejun Heo 10163bba2c361STejun Heo guard(rcu)(); 10164bba2c361STejun Heo 10165bba2c361STejun Heo sch = scx_prog_sched(aux); 10166bba2c361STejun Heo if (unlikely(!sch)) 10167bba2c361STejun Heo return NULL; 10168bba2c361STejun Heo 10169bba2c361STejun Heo if (!scx_cpu_valid(sch, cpu, NULL)) 10170bba2c361STejun Heo return NULL; 10171bba2c361STejun Heo 10172bba2c361STejun Heo return rcu_dereference(cpu_rq(cpu)->curr); 10173bba2c361STejun Heo } 10174bba2c361STejun Heo 10175bba2c361STejun Heo /** 10176bba2c361STejun Heo * scx_bpf_cid_curr - Return the curr task on the CPU at @cid 10177bba2c361STejun Heo * @cid: cid of interest 10178bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 10179bba2c361STejun Heo * 10180bba2c361STejun Heo * cid-addressed equivalent of scx_bpf_cpu_curr(). Callers must hold RCU 10181bba2c361STejun Heo * read lock (KF_RCU). 10182bba2c361STejun Heo */ 10183bba2c361STejun Heo __bpf_kfunc struct task_struct *scx_bpf_cid_curr(s32 cid, const struct bpf_prog_aux *aux) 10184bba2c361STejun Heo { 10185bba2c361STejun Heo struct scx_sched *sch; 10186bba2c361STejun Heo s32 cpu; 10187bba2c361STejun Heo 10188bba2c361STejun Heo guard(rcu)(); 10189bba2c361STejun Heo 10190bba2c361STejun Heo sch = scx_prog_sched(aux); 10191bba2c361STejun Heo if (unlikely(!sch)) 10192bba2c361STejun Heo return NULL; 10193bba2c361STejun Heo cpu = scx_cid_to_cpu(sch, cid); 10194bba2c361STejun Heo if (cpu < 0) 10195bba2c361STejun Heo return NULL; 10196bba2c361STejun Heo return rcu_dereference(cpu_rq(cpu)->curr); 10197bba2c361STejun Heo } 10198bba2c361STejun Heo 10199bba2c361STejun Heo /** 10200bba2c361STejun Heo * scx_bpf_tid_to_task - Look up a task by its scx tid 10201bba2c361STejun Heo * @tid: task ID previously read from p->scx.tid 10202bba2c361STejun Heo * 10203bba2c361STejun Heo * Returns the task with the given tid, or NULL if no such task exists. The 10204bba2c361STejun Heo * returned pointer is valid until the end of the current RCU read section 10205bba2c361STejun Heo * (KF_RCU_PROTECTED). Requires SCX_OPS_TID_TO_TASK to be set on the root 10206bba2c361STejun Heo * scheduler; otherwise an error is raised and NULL returned. 10207bba2c361STejun Heo */ 10208bba2c361STejun Heo __bpf_kfunc struct task_struct *scx_bpf_tid_to_task(u64 tid) 10209bba2c361STejun Heo { 10210bba2c361STejun Heo struct sched_ext_entity *scx; 10211bba2c361STejun Heo 10212bba2c361STejun Heo if (!scx_tid_to_task_enabled()) { 10213bba2c361STejun Heo struct scx_sched *sch = rcu_dereference(scx_root); 10214bba2c361STejun Heo 10215bba2c361STejun Heo if (sch) 10216bba2c361STejun Heo scx_error(sch, "scx_bpf_tid_to_task() called without SCX_OPS_TID_TO_TASK"); 10217bba2c361STejun Heo return NULL; 10218bba2c361STejun Heo } 10219bba2c361STejun Heo 10220bba2c361STejun Heo scx = rhashtable_lookup(&scx_tid_hash, &tid, scx_tid_hash_params); 10221bba2c361STejun Heo if (!scx) 10222bba2c361STejun Heo return NULL; 10223bba2c361STejun Heo 10224bba2c361STejun Heo return container_of(scx, struct task_struct, scx); 10225bba2c361STejun Heo } 10226bba2c361STejun Heo 10227bba2c361STejun Heo /** 10228bba2c361STejun Heo * scx_bpf_now - Returns a high-performance monotonically non-decreasing 10229bba2c361STejun Heo * clock for the current CPU. The clock returned is in nanoseconds. 10230bba2c361STejun Heo * 10231bba2c361STejun Heo * It provides the following properties: 10232bba2c361STejun Heo * 10233bba2c361STejun Heo * 1) High performance: Many BPF schedulers call bpf_ktime_get_ns() frequently 10234bba2c361STejun Heo * to account for execution time and track tasks' runtime properties. 10235bba2c361STejun Heo * Unfortunately, in some hardware platforms, bpf_ktime_get_ns() -- which 10236bba2c361STejun Heo * eventually reads a hardware timestamp counter -- is neither performant nor 10237bba2c361STejun Heo * scalable. scx_bpf_now() aims to provide a high-performance clock by 10238bba2c361STejun Heo * using the rq clock in the scheduler core whenever possible. 10239bba2c361STejun Heo * 10240bba2c361STejun Heo * 2) High enough resolution for the BPF scheduler use cases: In most BPF 10241bba2c361STejun Heo * scheduler use cases, the required clock resolution is lower than the most 10242bba2c361STejun Heo * accurate hardware clock (e.g., rdtsc in x86). scx_bpf_now() basically 10243bba2c361STejun Heo * uses the rq clock in the scheduler core whenever it is valid. It considers 10244bba2c361STejun Heo * that the rq clock is valid from the time the rq clock is updated 10245bba2c361STejun Heo * (update_rq_clock) until the rq is unlocked (rq_unpin_lock). 10246bba2c361STejun Heo * 10247bba2c361STejun Heo * 3) Monotonically non-decreasing clock for the same CPU: scx_bpf_now() 10248bba2c361STejun Heo * guarantees the clock never goes backward when comparing them in the same 10249bba2c361STejun Heo * CPU. On the other hand, when comparing clocks in different CPUs, there 10250bba2c361STejun Heo * is no such guarantee -- the clock can go backward. It provides a 10251bba2c361STejun Heo * monotonically *non-decreasing* clock so that it would provide the same 10252bba2c361STejun Heo * clock values in two different scx_bpf_now() calls in the same CPU 10253bba2c361STejun Heo * during the same period of when the rq clock is valid. 10254bba2c361STejun Heo */ 10255bba2c361STejun Heo __bpf_kfunc u64 scx_bpf_now(void) 10256bba2c361STejun Heo { 10257bba2c361STejun Heo struct rq *rq; 10258bba2c361STejun Heo u64 clock; 10259bba2c361STejun Heo 10260bba2c361STejun Heo preempt_disable(); 10261bba2c361STejun Heo 10262bba2c361STejun Heo rq = this_rq(); 10263bba2c361STejun Heo if (smp_load_acquire(&rq->scx.flags) & SCX_RQ_CLK_VALID) { 10264bba2c361STejun Heo /* 10265bba2c361STejun Heo * If the rq clock is valid, use the cached rq clock. 10266bba2c361STejun Heo * 10267bba2c361STejun Heo * Note that scx_bpf_now() is re-entrant between a process 10268bba2c361STejun Heo * context and an interrupt context (e.g., timer interrupt). 10269bba2c361STejun Heo * However, we don't need to consider the race between them 10270bba2c361STejun Heo * because such race is not observable from a caller. 10271bba2c361STejun Heo */ 10272bba2c361STejun Heo clock = READ_ONCE(rq->scx.clock); 10273bba2c361STejun Heo } else { 10274bba2c361STejun Heo /* 10275bba2c361STejun Heo * Otherwise, return a fresh rq clock. 10276bba2c361STejun Heo * 10277bba2c361STejun Heo * The rq clock is updated outside of the rq lock. 10278bba2c361STejun Heo * In this case, keep the updated rq clock invalid so the next 10279bba2c361STejun Heo * kfunc call outside the rq lock gets a fresh rq clock. 10280bba2c361STejun Heo */ 10281bba2c361STejun Heo clock = sched_clock_cpu(cpu_of(rq)); 10282bba2c361STejun Heo } 10283bba2c361STejun Heo 10284bba2c361STejun Heo preempt_enable(); 10285bba2c361STejun Heo 10286bba2c361STejun Heo return clock; 10287bba2c361STejun Heo } 10288bba2c361STejun Heo 10289bba2c361STejun Heo static void scx_read_events(struct scx_sched *sch, struct scx_event_stats *events) 10290bba2c361STejun Heo { 10291bba2c361STejun Heo struct scx_event_stats *e_cpu; 10292bba2c361STejun Heo int cpu; 10293bba2c361STejun Heo 10294bba2c361STejun Heo /* Aggregate per-CPU event counters into @events. */ 10295bba2c361STejun Heo memset(events, 0, sizeof(*events)); 10296bba2c361STejun Heo for_each_possible_cpu(cpu) { 10297bba2c361STejun Heo e_cpu = &per_cpu_ptr(sch->pcpu, cpu)->event_stats; 10298bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_SELECT_CPU_FALLBACK); 10299bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE); 10300bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_DISPATCH_KEEP_LAST); 10301bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_ENQ_SKIP_EXITING); 10302bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_ENQ_SKIP_MIGRATION_DISABLED); 10303bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_REENQ_IMMED); 10304bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_REENQ_LOCAL_REPEAT); 10305bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_REFILL_SLICE_DFL); 10306bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_BYPASS_DURATION); 10307bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_BYPASS_DISPATCH); 10308bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_BYPASS_ACTIVATE); 10309bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_INSERT_NOT_OWNED); 10310bba2c361STejun Heo scx_agg_event(events, e_cpu, SCX_EV_SUB_BYPASS_DISPATCH); 10311bba2c361STejun Heo } 10312bba2c361STejun Heo } 10313bba2c361STejun Heo 10314bba2c361STejun Heo /* 10315bba2c361STejun Heo * scx_bpf_events - Get a system-wide event counter to 10316bba2c361STejun Heo * @events: output buffer from a BPF program 10317bba2c361STejun Heo * @events__sz: @events len, must end in '__sz'' for the verifier 10318bba2c361STejun Heo */ 10319bba2c361STejun Heo __bpf_kfunc void scx_bpf_events(struct scx_event_stats *events, 10320bba2c361STejun Heo size_t events__sz) 10321bba2c361STejun Heo { 10322bba2c361STejun Heo struct scx_sched *sch; 10323bba2c361STejun Heo struct scx_event_stats e_sys; 10324bba2c361STejun Heo 10325bba2c361STejun Heo rcu_read_lock(); 10326bba2c361STejun Heo sch = rcu_dereference(scx_root); 10327bba2c361STejun Heo if (sch) 10328bba2c361STejun Heo scx_read_events(sch, &e_sys); 10329bba2c361STejun Heo else 10330bba2c361STejun Heo memset(&e_sys, 0, sizeof(e_sys)); 10331bba2c361STejun Heo rcu_read_unlock(); 10332bba2c361STejun Heo 10333bba2c361STejun Heo /* 10334bba2c361STejun Heo * We cannot entirely trust a BPF-provided size since a BPF program 10335bba2c361STejun Heo * might be compiled against a different vmlinux.h, of which 10336bba2c361STejun Heo * scx_event_stats would be larger (a newer vmlinux.h) or smaller 10337bba2c361STejun Heo * (an older vmlinux.h). Hence, we use the smaller size to avoid 10338bba2c361STejun Heo * memory corruption. 10339bba2c361STejun Heo */ 10340bba2c361STejun Heo events__sz = min(events__sz, sizeof(*events)); 10341bba2c361STejun Heo memcpy(events, &e_sys, events__sz); 10342bba2c361STejun Heo } 10343bba2c361STejun Heo 10344bba2c361STejun Heo #ifdef CONFIG_CGROUP_SCHED 10345bba2c361STejun Heo /** 10346bba2c361STejun Heo * scx_bpf_task_cgroup - Return the sched cgroup of a task 10347bba2c361STejun Heo * @p: task of interest 10348bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 10349bba2c361STejun Heo * 10350bba2c361STejun Heo * @p->sched_task_group->css.cgroup represents the cgroup @p is associated with 10351bba2c361STejun Heo * from the scheduler's POV. SCX operations should use this function to 10352bba2c361STejun Heo * determine @p's current cgroup as, unlike following @p->cgroups, 10353bba2c361STejun Heo * @p->sched_task_group is stable for the duration of the SCX op. See 10354bba2c361STejun Heo * SCX_CALL_OP_TASK() for details. 10355bba2c361STejun Heo */ 10356bba2c361STejun Heo __bpf_kfunc struct cgroup *scx_bpf_task_cgroup(struct task_struct *p, 10357bba2c361STejun Heo const struct bpf_prog_aux *aux) 10358bba2c361STejun Heo { 10359bba2c361STejun Heo struct task_group *tg = p->sched_task_group; 10360bba2c361STejun Heo struct cgroup *cgrp = &cgrp_dfl_root.cgrp; 10361bba2c361STejun Heo struct scx_sched *sch; 10362bba2c361STejun Heo 10363bba2c361STejun Heo guard(rcu)(); 10364bba2c361STejun Heo 10365bba2c361STejun Heo sch = scx_prog_sched(aux); 10366bba2c361STejun Heo if (unlikely(!sch)) 10367bba2c361STejun Heo goto out; 10368bba2c361STejun Heo 10369bba2c361STejun Heo if (!scx_kf_arg_task_ok(sch, p)) 10370bba2c361STejun Heo goto out; 10371bba2c361STejun Heo 10372bba2c361STejun Heo cgrp = tg_cgrp(tg); 10373bba2c361STejun Heo 10374bba2c361STejun Heo out: 10375bba2c361STejun Heo cgroup_get(cgrp); 10376bba2c361STejun Heo return cgrp; 10377bba2c361STejun Heo } 10378bba2c361STejun Heo #endif /* CONFIG_CGROUP_SCHED */ 10379bba2c361STejun Heo 10380bba2c361STejun Heo __bpf_kfunc_end_defs(); 10381bba2c361STejun Heo 10382bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_any) 10383bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_task_set_slice, KF_IMPLICIT_ARGS | KF_RCU); 10384bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_task_set_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU); 10385bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS) 10386bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_kick_cid, KF_IMPLICIT_ARGS) 10387bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued, KF_IMPLICIT_ARGS) 10388bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_destroy_dsq, KF_IMPLICIT_ARGS) 10389bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_peek, KF_IMPLICIT_ARGS | KF_RCU_PROTECTED | KF_RET_NULL) 10390bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dsq_reenq, KF_IMPLICIT_ARGS) 10391bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_reenqueue_local___v2, KF_IMPLICIT_ARGS) 10392bba2c361STejun Heo BTF_ID_FLAGS(func, bpf_iter_scx_dsq_new, KF_IMPLICIT_ARGS | KF_ITER_NEW | KF_RCU_PROTECTED) 10393bba2c361STejun Heo BTF_ID_FLAGS(func, bpf_iter_scx_dsq_next, KF_ITER_NEXT | KF_RET_NULL) 10394bba2c361STejun Heo BTF_ID_FLAGS(func, bpf_iter_scx_dsq_destroy, KF_ITER_DESTROY) 10395bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_exit_bstr, KF_IMPLICIT_ARGS) 10396bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_error_bstr, KF_IMPLICIT_ARGS) 10397bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_dump_bstr, KF_IMPLICIT_ARGS) 10398bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpuperf_cap, KF_IMPLICIT_ARGS) 10399bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpuperf_cur, KF_IMPLICIT_ARGS) 10400bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpuperf_set, KF_IMPLICIT_ARGS) 10401bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cidperf_cap, KF_IMPLICIT_ARGS) 10402bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cidperf_cur, KF_IMPLICIT_ARGS) 10403bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cidperf_set, KF_IMPLICIT_ARGS) 10404bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_nr_node_ids) 10405bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_nr_cpu_ids) 10406bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_nr_cids) 10407bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_nr_online_cids) 10408bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_this_cid) 10409bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_possible_cpumask, KF_ACQUIRE) 10410bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE) 10411bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) 10412bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU) 10413bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU) 10414bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU) 10415bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpu_rq, KF_IMPLICIT_ARGS) 10416bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL) 10417bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_IMPLICIT_ARGS | KF_RET_NULL | KF_RCU_PROTECTED) 10418bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cid_curr, KF_IMPLICIT_ARGS | KF_RET_NULL | KF_RCU_PROTECTED) 10419bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_tid_to_task, KF_RET_NULL | KF_RCU_PROTECTED) 10420bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_now) 10421bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_events) 10422bba2c361STejun Heo #ifdef CONFIG_CGROUP_SCHED 10423bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_task_cgroup, KF_IMPLICIT_ARGS | KF_RCU | KF_ACQUIRE) 10424bba2c361STejun Heo #endif 10425bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_any) 10426bba2c361STejun Heo 10427bba2c361STejun Heo static const struct btf_kfunc_id_set scx_kfunc_set_any = { 10428bba2c361STejun Heo .owner = THIS_MODULE, 10429bba2c361STejun Heo .set = &scx_kfunc_ids_any, 10430bba2c361STejun Heo .filter = scx_kfunc_context_filter, 10431bba2c361STejun Heo }; 10432bba2c361STejun Heo 10433bba2c361STejun Heo /* 10434bba2c361STejun Heo * cpu-form kfuncs that are forbidden from cid-form schedulers 10435bba2c361STejun Heo * (bpf_sched_ext_ops_cid). Programs targeting the cid struct_ops type must 10436bba2c361STejun Heo * use the cid-form alternative (cid/cmask kfuncs). 10437bba2c361STejun Heo * 10438bba2c361STejun Heo * Membership overlaps with scx_kfunc_ids_{any,idle,select_cpu}; the filter 10439bba2c361STejun Heo * tests this set independently and rejects matches before the per-op 10440bba2c361STejun Heo * allow-list check runs. 10441bba2c361STejun Heo * 10442bba2c361STejun Heo * pahole/resolve_btfids scans every BTF_ID_FLAGS() at build time and 10443bba2c361STejun Heo * intersects flags across duplicate entries, so each entry must carry the 10444bba2c361STejun Heo * same flags as the kfunc's primary declaration; otherwise the flags get 10445bba2c361STejun Heo * dropped globally. 10446bba2c361STejun Heo */ 10447bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_cpu_only) 10448bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS) 10449bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU) 10450bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpu_rq, KF_IMPLICIT_ARGS) 10451bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_IMPLICIT_ARGS | KF_RET_NULL | KF_RCU_PROTECTED) 10452bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpu_node, KF_IMPLICIT_ARGS) 10453bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpuperf_cap, KF_IMPLICIT_ARGS) 10454bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpuperf_cur, KF_IMPLICIT_ARGS) 10455bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpuperf_set, KF_IMPLICIT_ARGS) 10456bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_possible_cpumask, KF_ACQUIRE) 10457bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE) 10458bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) 10459bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_select_cpu_dfl, KF_IMPLICIT_ARGS | KF_RCU) 10460bba2c361STejun Heo BTF_ID_FLAGS(func, __scx_bpf_select_cpu_and, KF_IMPLICIT_ARGS | KF_RCU) 10461bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_select_cpu_and, KF_RCU) 10462bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask, KF_IMPLICIT_ARGS | KF_ACQUIRE) 10463bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask_node, KF_IMPLICIT_ARGS | KF_ACQUIRE) 10464bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask, KF_IMPLICIT_ARGS | KF_ACQUIRE) 10465bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask_node, KF_IMPLICIT_ARGS | KF_ACQUIRE) 10466bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_put_idle_cpumask, KF_RELEASE) 10467bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_test_and_clear_cpu_idle, KF_IMPLICIT_ARGS) 10468bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu, KF_IMPLICIT_ARGS | KF_RCU) 10469bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu_node, KF_IMPLICIT_ARGS | KF_RCU) 10470bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu, KF_IMPLICIT_ARGS | KF_RCU) 10471bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu_node, KF_IMPLICIT_ARGS | KF_RCU) 10472bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_cpu_only) 10473bba2c361STejun Heo 10474bba2c361STejun Heo /* 10475bba2c361STejun Heo * Per-op kfunc allow flags. Each bit corresponds to a context-sensitive kfunc 10476bba2c361STejun Heo * group; an op may permit zero or more groups, with the union expressed in 10477bba2c361STejun Heo * scx_kf_allow_flags[]. The verifier-time filter (scx_kfunc_context_filter()) 10478bba2c361STejun Heo * consults this table to decide whether a context-sensitive kfunc is callable 10479bba2c361STejun Heo * from a given SCX op. 10480bba2c361STejun Heo */ 10481bba2c361STejun Heo enum scx_kf_allow_flags { 10482bba2c361STejun Heo SCX_KF_ALLOW_UNLOCKED = 1 << 0, 10483bba2c361STejun Heo SCX_KF_ALLOW_INIT = 1 << 1, 10484bba2c361STejun Heo SCX_KF_ALLOW_CPU_RELEASE = 1 << 2, 10485bba2c361STejun Heo SCX_KF_ALLOW_DISPATCH = 1 << 3, 10486bba2c361STejun Heo SCX_KF_ALLOW_ENQUEUE = 1 << 4, 10487bba2c361STejun Heo SCX_KF_ALLOW_SELECT_CPU = 1 << 5, 10488bba2c361STejun Heo }; 10489bba2c361STejun Heo 10490bba2c361STejun Heo /* 10491bba2c361STejun Heo * Map each SCX op to the union of kfunc groups it permits, indexed by 10492bba2c361STejun Heo * SCX_OP_IDX(op). Ops not listed only permit kfuncs that are not 10493bba2c361STejun Heo * context-sensitive. 10494bba2c361STejun Heo */ 10495bba2c361STejun Heo static const u32 scx_kf_allow_flags[] = { 10496bba2c361STejun Heo [SCX_OP_IDX(select_cpu)] = SCX_KF_ALLOW_SELECT_CPU | SCX_KF_ALLOW_ENQUEUE, 10497bba2c361STejun Heo [SCX_OP_IDX(enqueue)] = SCX_KF_ALLOW_SELECT_CPU | SCX_KF_ALLOW_ENQUEUE, 10498bba2c361STejun Heo [SCX_OP_IDX(dispatch)] = SCX_KF_ALLOW_ENQUEUE | SCX_KF_ALLOW_DISPATCH, 10499bba2c361STejun Heo [SCX_OP_IDX(cpu_release)] = SCX_KF_ALLOW_CPU_RELEASE, 10500bba2c361STejun Heo [SCX_OP_IDX(init_task)] = SCX_KF_ALLOW_UNLOCKED, 10501bba2c361STejun Heo [SCX_OP_IDX(dump)] = SCX_KF_ALLOW_UNLOCKED, 10502bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 10503bba2c361STejun Heo [SCX_OP_IDX(cgroup_init)] = SCX_KF_ALLOW_UNLOCKED, 10504bba2c361STejun Heo [SCX_OP_IDX(cgroup_exit)] = SCX_KF_ALLOW_UNLOCKED, 10505bba2c361STejun Heo [SCX_OP_IDX(cgroup_prep_move)] = SCX_KF_ALLOW_UNLOCKED, 10506bba2c361STejun Heo [SCX_OP_IDX(cgroup_cancel_move)] = SCX_KF_ALLOW_UNLOCKED, 10507bba2c361STejun Heo [SCX_OP_IDX(cgroup_set_weight)] = SCX_KF_ALLOW_UNLOCKED, 10508bba2c361STejun Heo [SCX_OP_IDX(cgroup_set_bandwidth)] = SCX_KF_ALLOW_UNLOCKED, 10509bba2c361STejun Heo [SCX_OP_IDX(cgroup_set_idle)] = SCX_KF_ALLOW_UNLOCKED, 10510bba2c361STejun Heo #endif /* CONFIG_EXT_GROUP_SCHED */ 10511bba2c361STejun Heo [SCX_OP_IDX(sub_attach)] = SCX_KF_ALLOW_UNLOCKED, 10512bba2c361STejun Heo [SCX_OP_IDX(sub_detach)] = SCX_KF_ALLOW_UNLOCKED, 10513bba2c361STejun Heo [SCX_OP_IDX(cpu_online)] = SCX_KF_ALLOW_UNLOCKED, 10514bba2c361STejun Heo [SCX_OP_IDX(cpu_offline)] = SCX_KF_ALLOW_UNLOCKED, 10515bba2c361STejun Heo [SCX_OP_IDX(init)] = SCX_KF_ALLOW_UNLOCKED | SCX_KF_ALLOW_INIT, 10516bba2c361STejun Heo [SCX_OP_IDX(exit)] = SCX_KF_ALLOW_UNLOCKED, 10517bba2c361STejun Heo }; 10518bba2c361STejun Heo 10519bba2c361STejun Heo /* 10520bba2c361STejun Heo * Verifier-time filter for SCX kfuncs. Registered via the .filter field on 10521bba2c361STejun Heo * each per-group btf_kfunc_id_set. The BPF core invokes this for every kfunc 10522bba2c361STejun Heo * call in the registered hook (BPF_PROG_TYPE_STRUCT_OPS or 10523bba2c361STejun Heo * BPF_PROG_TYPE_SYSCALL), regardless of which set originally introduced the 10524bba2c361STejun Heo * kfunc - so the filter must short-circuit on kfuncs it doesn't govern by 10525bba2c361STejun Heo * falling through to "allow" when none of the SCX sets contain the kfunc. 10526bba2c361STejun Heo */ 10527bba2c361STejun Heo int scx_kfunc_context_filter(const struct bpf_prog *prog, u32 kfunc_id) 10528bba2c361STejun Heo { 10529bba2c361STejun Heo bool in_unlocked = btf_id_set8_contains(&scx_kfunc_ids_unlocked, kfunc_id); 10530bba2c361STejun Heo bool in_init = btf_id_set8_contains(&scx_kfunc_ids_init, kfunc_id); 10531bba2c361STejun Heo bool in_select_cpu = btf_id_set8_contains(&scx_kfunc_ids_select_cpu, kfunc_id); 10532bba2c361STejun Heo bool in_enqueue = btf_id_set8_contains(&scx_kfunc_ids_enqueue_dispatch, kfunc_id); 10533bba2c361STejun Heo bool in_dispatch = btf_id_set8_contains(&scx_kfunc_ids_dispatch, kfunc_id); 10534bba2c361STejun Heo bool in_cpu_release = btf_id_set8_contains(&scx_kfunc_ids_cpu_release, kfunc_id); 10535bba2c361STejun Heo bool in_idle = btf_id_set8_contains(&scx_kfunc_ids_idle, kfunc_id); 10536bba2c361STejun Heo bool in_any = btf_id_set8_contains(&scx_kfunc_ids_any, kfunc_id); 10537bba2c361STejun Heo bool in_cpu_only = btf_id_set8_contains(&scx_kfunc_ids_cpu_only, kfunc_id); 10538bba2c361STejun Heo u32 moff, flags; 10539bba2c361STejun Heo 10540bba2c361STejun Heo /* Not an SCX kfunc - allow. */ 10541bba2c361STejun Heo if (!(in_unlocked || in_init || in_select_cpu || in_enqueue || in_dispatch || 10542bba2c361STejun Heo in_cpu_release || in_idle || in_any)) 10543bba2c361STejun Heo return 0; 10544bba2c361STejun Heo 10545bba2c361STejun Heo /* SYSCALL progs (e.g. BPF test_run()) may call unlocked and select_cpu kfuncs. */ 10546bba2c361STejun Heo if (prog->type == BPF_PROG_TYPE_SYSCALL) 10547bba2c361STejun Heo return (in_unlocked || in_select_cpu || in_idle || in_any) ? 0 : -EACCES; 10548bba2c361STejun Heo 10549bba2c361STejun Heo if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) 10550bba2c361STejun Heo return (in_any || in_idle) ? 0 : -EACCES; 10551bba2c361STejun Heo 10552bba2c361STejun Heo /* 10553bba2c361STejun Heo * add_subprog_and_kfunc() collects all kfunc calls, including dead code 10554bba2c361STejun Heo * guarded by bpf_ksym_exists(), before check_attach_btf_id() sets 10555bba2c361STejun Heo * prog->aux->st_ops. Allow all kfuncs when st_ops is not yet set; 10556bba2c361STejun Heo * do_check_main() re-runs the filter with st_ops set and enforces the 10557bba2c361STejun Heo * actual restrictions. 10558bba2c361STejun Heo */ 10559bba2c361STejun Heo if (!prog->aux->st_ops) 10560bba2c361STejun Heo return 0; 10561bba2c361STejun Heo 10562bba2c361STejun Heo /* 10563bba2c361STejun Heo * Non-SCX struct_ops: SCX kfuncs are not permitted. 10564bba2c361STejun Heo * 10565bba2c361STejun Heo * Both bpf_sched_ext_ops (cpu-form) and bpf_sched_ext_ops_cid 10566bba2c361STejun Heo * (cid-form) are valid SCX struct_ops. Member offsets match between 10567bba2c361STejun Heo * the two (verified by BUILD_BUG_ON in scx_init()), so the shared 10568bba2c361STejun Heo * scx_kf_allow_flags[] table indexed by SCX_MOFF_IDX(moff) applies to 10569bba2c361STejun Heo * both. 10570bba2c361STejun Heo */ 10571bba2c361STejun Heo if (prog->aux->st_ops != &bpf_sched_ext_ops && 10572bba2c361STejun Heo prog->aux->st_ops != &bpf_sched_ext_ops_cid) 10573bba2c361STejun Heo return -EACCES; 10574bba2c361STejun Heo 10575bba2c361STejun Heo /* 10576bba2c361STejun Heo * cid-form schedulers must use cid/cmask kfuncs. cid and cpu are both 10577bba2c361STejun Heo * small s32s and trivially confused, so cpu-only kfuncs are rejected at 10578bba2c361STejun Heo * load time. The reverse (cpu-form calling cid-form kfuncs) is 10579bba2c361STejun Heo * intentionally permissive to ease gradual cpumask -> cid migration. 10580bba2c361STejun Heo */ 10581bba2c361STejun Heo if (prog->aux->st_ops == &bpf_sched_ext_ops_cid && in_cpu_only) 10582bba2c361STejun Heo return -EACCES; 10583bba2c361STejun Heo 10584bba2c361STejun Heo /* SCX struct_ops: check the per-op allow list. */ 10585bba2c361STejun Heo if (in_any || in_idle) 10586bba2c361STejun Heo return 0; 10587bba2c361STejun Heo 10588bba2c361STejun Heo moff = prog->aux->attach_st_ops_member_off; 10589bba2c361STejun Heo flags = scx_kf_allow_flags[SCX_MOFF_IDX(moff)]; 10590bba2c361STejun Heo 10591bba2c361STejun Heo if ((flags & SCX_KF_ALLOW_UNLOCKED) && in_unlocked) 10592bba2c361STejun Heo return 0; 10593bba2c361STejun Heo if ((flags & SCX_KF_ALLOW_INIT) && in_init) 10594bba2c361STejun Heo return 0; 10595bba2c361STejun Heo if ((flags & SCX_KF_ALLOW_CPU_RELEASE) && in_cpu_release) 10596bba2c361STejun Heo return 0; 10597bba2c361STejun Heo if ((flags & SCX_KF_ALLOW_DISPATCH) && in_dispatch) 10598bba2c361STejun Heo return 0; 10599bba2c361STejun Heo if ((flags & SCX_KF_ALLOW_ENQUEUE) && in_enqueue) 10600bba2c361STejun Heo return 0; 10601bba2c361STejun Heo if ((flags & SCX_KF_ALLOW_SELECT_CPU) && in_select_cpu) 10602bba2c361STejun Heo return 0; 10603bba2c361STejun Heo 10604bba2c361STejun Heo return -EACCES; 10605bba2c361STejun Heo } 10606bba2c361STejun Heo 10607bba2c361STejun Heo static int __init scx_init(void) 10608bba2c361STejun Heo { 10609bba2c361STejun Heo int ret; 10610bba2c361STejun Heo 10611bba2c361STejun Heo /* 10612bba2c361STejun Heo * sched_ext_ops_cid mirrors sched_ext_ops up to and including @priv. 10613bba2c361STejun Heo * Both bpf_scx_init_member() and bpf_scx_check_member() use offsets 10614bba2c361STejun Heo * from struct sched_ext_ops; sched_ext_ops_cid relies on those offsets 10615bba2c361STejun Heo * matching for the shared fields. Catch any drift at boot. 10616bba2c361STejun Heo */ 10617bba2c361STejun Heo #define CID_OFFSET_MATCH(cpu_field, cid_field) \ 10618bba2c361STejun Heo BUILD_BUG_ON(offsetof(struct sched_ext_ops, cpu_field) != \ 10619bba2c361STejun Heo offsetof(struct sched_ext_ops_cid, cid_field)) 10620bba2c361STejun Heo /* data fields used by bpf_scx_init_member() */ 10621bba2c361STejun Heo CID_OFFSET_MATCH(dispatch_max_batch, dispatch_max_batch); 10622bba2c361STejun Heo CID_OFFSET_MATCH(flags, flags); 10623bba2c361STejun Heo CID_OFFSET_MATCH(name, name); 10624bba2c361STejun Heo CID_OFFSET_MATCH(timeout_ms, timeout_ms); 10625bba2c361STejun Heo CID_OFFSET_MATCH(exit_dump_len, exit_dump_len); 10626bba2c361STejun Heo CID_OFFSET_MATCH(hotplug_seq, hotplug_seq); 10627bba2c361STejun Heo CID_OFFSET_MATCH(sub_cgroup_id, sub_cgroup_id); 10628bba2c361STejun Heo /* shared callbacks: the union view requires byte-for-byte offset match */ 10629bba2c361STejun Heo CID_OFFSET_MATCH(enqueue, enqueue); 10630bba2c361STejun Heo CID_OFFSET_MATCH(dequeue, dequeue); 10631bba2c361STejun Heo CID_OFFSET_MATCH(dispatch, dispatch); 10632bba2c361STejun Heo CID_OFFSET_MATCH(tick, tick); 10633bba2c361STejun Heo CID_OFFSET_MATCH(runnable, runnable); 10634bba2c361STejun Heo CID_OFFSET_MATCH(running, running); 10635bba2c361STejun Heo CID_OFFSET_MATCH(stopping, stopping); 10636bba2c361STejun Heo CID_OFFSET_MATCH(quiescent, quiescent); 10637bba2c361STejun Heo CID_OFFSET_MATCH(yield, yield); 10638bba2c361STejun Heo CID_OFFSET_MATCH(core_sched_before, core_sched_before); 10639bba2c361STejun Heo CID_OFFSET_MATCH(set_weight, set_weight); 10640bba2c361STejun Heo CID_OFFSET_MATCH(update_idle, update_idle); 10641bba2c361STejun Heo CID_OFFSET_MATCH(init_task, init_task); 10642bba2c361STejun Heo CID_OFFSET_MATCH(exit_task, exit_task); 10643bba2c361STejun Heo CID_OFFSET_MATCH(enable, enable); 10644bba2c361STejun Heo CID_OFFSET_MATCH(disable, disable); 10645bba2c361STejun Heo CID_OFFSET_MATCH(dump, dump); 10646bba2c361STejun Heo CID_OFFSET_MATCH(dump_task, dump_task); 10647bba2c361STejun Heo CID_OFFSET_MATCH(sub_attach, sub_attach); 10648bba2c361STejun Heo CID_OFFSET_MATCH(sub_detach, sub_detach); 10649bba2c361STejun Heo CID_OFFSET_MATCH(init, init); 10650bba2c361STejun Heo CID_OFFSET_MATCH(exit, exit); 10651bba2c361STejun Heo #ifdef CONFIG_EXT_GROUP_SCHED 10652bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_init, cgroup_init); 10653bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_exit, cgroup_exit); 10654bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_prep_move, cgroup_prep_move); 10655bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_move, cgroup_move); 10656bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_cancel_move, cgroup_cancel_move); 10657bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_set_weight, cgroup_set_weight); 10658bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_set_bandwidth, cgroup_set_bandwidth); 10659bba2c361STejun Heo CID_OFFSET_MATCH(cgroup_set_idle, cgroup_set_idle); 10660bba2c361STejun Heo #endif 10661bba2c361STejun Heo /* renamed callbacks must occupy the same slot as their cpu-form sibling */ 10662bba2c361STejun Heo CID_OFFSET_MATCH(select_cpu, select_cid); 10663bba2c361STejun Heo CID_OFFSET_MATCH(set_cpumask, set_cmask); 10664bba2c361STejun Heo CID_OFFSET_MATCH(cpu_online, cid_online); 10665bba2c361STejun Heo CID_OFFSET_MATCH(cpu_offline, cid_offline); 10666bba2c361STejun Heo CID_OFFSET_MATCH(dump_cpu, dump_cid); 10667bba2c361STejun Heo /* @priv tail must align since both share the same data block */ 10668bba2c361STejun Heo CID_OFFSET_MATCH(priv, priv); 10669bba2c361STejun Heo /* 10670bba2c361STejun Heo * cid-form must end exactly at @priv - validate_ops() skips 10671bba2c361STejun Heo * cpu_acquire/cpu_release for cid-form because reading those fields 10672bba2c361STejun Heo * past the BPF allocation would be UB. 10673bba2c361STejun Heo */ 10674bba2c361STejun Heo BUILD_BUG_ON(offsetof(struct sched_ext_ops_cid, __end) != 10675bba2c361STejun Heo offsetofend(struct sched_ext_ops, priv)); 10676bba2c361STejun Heo #undef CID_OFFSET_MATCH 10677bba2c361STejun Heo 10678bba2c361STejun Heo /* 10679bba2c361STejun Heo * kfunc registration can't be done from init_sched_ext_class() as 10680bba2c361STejun Heo * register_btf_kfunc_id_set() needs most of the system to be up. 10681bba2c361STejun Heo * 10682bba2c361STejun Heo * Some kfuncs are context-sensitive and can only be called from 10683bba2c361STejun Heo * specific SCX ops. They are grouped into per-context BTF sets, each 10684bba2c361STejun Heo * registered with scx_kfunc_context_filter as its .filter callback. The 10685bba2c361STejun Heo * BPF core dedups identical filter pointers per hook 10686bba2c361STejun Heo * (btf_populate_kfunc_set()), so the filter is invoked exactly once per 10687bba2c361STejun Heo * kfunc lookup; it consults scx_kf_allow_flags[] to enforce per-op 10688bba2c361STejun Heo * restrictions at verify time. 10689bba2c361STejun Heo */ 10690bba2c361STejun Heo if ((ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, 10691bba2c361STejun Heo &scx_kfunc_set_enqueue_dispatch)) || 10692bba2c361STejun Heo (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, 10693bba2c361STejun Heo &scx_kfunc_set_dispatch)) || 10694bba2c361STejun Heo (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, 10695bba2c361STejun Heo &scx_kfunc_set_cpu_release)) || 10696bba2c361STejun Heo (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, 10697bba2c361STejun Heo &scx_kfunc_set_unlocked)) || 10698bba2c361STejun Heo (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, 10699bba2c361STejun Heo &scx_kfunc_set_unlocked)) || 10700bba2c361STejun Heo (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, 10701bba2c361STejun Heo &scx_kfunc_set_any)) || 10702bba2c361STejun Heo (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, 10703bba2c361STejun Heo &scx_kfunc_set_any)) || 10704bba2c361STejun Heo (ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, 10705bba2c361STejun Heo &scx_kfunc_set_any))) { 10706bba2c361STejun Heo pr_err("sched_ext: Failed to register kfunc sets (%d)\n", ret); 10707bba2c361STejun Heo return ret; 10708bba2c361STejun Heo } 10709bba2c361STejun Heo 10710bba2c361STejun Heo ret = scx_idle_init(); 10711bba2c361STejun Heo if (ret) { 10712bba2c361STejun Heo pr_err("sched_ext: Failed to initialize idle tracking (%d)\n", ret); 10713bba2c361STejun Heo return ret; 10714bba2c361STejun Heo } 10715bba2c361STejun Heo 10716bba2c361STejun Heo ret = scx_cid_kfunc_init(); 10717bba2c361STejun Heo if (ret) { 10718bba2c361STejun Heo pr_err("sched_ext: Failed to register cid kfuncs (%d)\n", ret); 10719bba2c361STejun Heo return ret; 10720bba2c361STejun Heo } 10721bba2c361STejun Heo 10722bba2c361STejun Heo ret = register_bpf_struct_ops(&bpf_sched_ext_ops, sched_ext_ops); 10723bba2c361STejun Heo if (ret) { 10724bba2c361STejun Heo pr_err("sched_ext: Failed to register struct_ops (%d)\n", ret); 10725bba2c361STejun Heo return ret; 10726bba2c361STejun Heo } 10727bba2c361STejun Heo 10728bba2c361STejun Heo ret = register_bpf_struct_ops(&bpf_sched_ext_ops_cid, sched_ext_ops_cid); 10729bba2c361STejun Heo if (ret) { 10730bba2c361STejun Heo pr_err("sched_ext: Failed to register cid struct_ops (%d)\n", ret); 10731bba2c361STejun Heo return ret; 10732bba2c361STejun Heo } 10733bba2c361STejun Heo 10734bba2c361STejun Heo ret = register_pm_notifier(&scx_pm_notifier); 10735bba2c361STejun Heo if (ret) { 10736bba2c361STejun Heo pr_err("sched_ext: Failed to register PM notifier (%d)\n", ret); 10737bba2c361STejun Heo return ret; 10738bba2c361STejun Heo } 10739bba2c361STejun Heo 10740bba2c361STejun Heo scx_kset = kset_create_and_add("sched_ext", &scx_uevent_ops, kernel_kobj); 10741bba2c361STejun Heo if (!scx_kset) { 10742bba2c361STejun Heo pr_err("sched_ext: Failed to create /sys/kernel/sched_ext\n"); 10743bba2c361STejun Heo return -ENOMEM; 10744bba2c361STejun Heo } 10745bba2c361STejun Heo 10746bba2c361STejun Heo ret = sysfs_create_group(&scx_kset->kobj, &scx_global_attr_group); 10747bba2c361STejun Heo if (ret < 0) { 10748bba2c361STejun Heo pr_err("sched_ext: Failed to add global attributes\n"); 10749bba2c361STejun Heo return ret; 10750bba2c361STejun Heo } 10751bba2c361STejun Heo 10752bba2c361STejun Heo return 0; 10753bba2c361STejun Heo } 10754bba2c361STejun Heo __initcall(scx_init); 10755