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