1daf8e166STejun Heo /* SPDX-License-Identifier: GPL-2.0 */ 2daf8e166STejun Heo /* 3daf8e166STejun Heo * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst 4daf8e166STejun Heo * 5daf8e166STejun Heo * Sub-scheduler hierarchy support. 6daf8e166STejun Heo * 7daf8e166STejun Heo * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. 8daf8e166STejun Heo * Copyright (c) 2026 Tejun Heo <tj@kernel.org> 9daf8e166STejun Heo */ 10daf8e166STejun Heo #ifndef _KERNEL_SCHED_EXT_SUB_H 11daf8e166STejun Heo #define _KERNEL_SCHED_EXT_SUB_H 12daf8e166STejun Heo 13daf8e166STejun Heo #include "internal.h" 14daf8e166STejun Heo #include "cid.h" 15daf8e166STejun Heo 16daf8e166STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 17daf8e166STejun Heo 18*bbda59d8STejun Heo struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root); 19daf8e166STejun Heo struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root); 20daf8e166STejun Heo void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch); 21daf8e166STejun Heo struct cgroup *sch_cgroup(struct scx_sched *sch); 22daf8e166STejun Heo void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch); 23daf8e166STejun Heo void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog); 24daf8e166STejun Heo void drain_descendants(struct scx_sched *sch); 25daf8e166STejun Heo void scx_sub_disable(struct scx_sched *sch); 26daf8e166STejun Heo void scx_sub_enable_workfn(struct kthread_work *work); 27daf8e166STejun Heo bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux); 288dba3bbdSTejun Heo void scx_free_pshards(struct scx_sched *sch); 298dba3bbdSTejun Heo s32 scx_alloc_pshards(struct scx_sched *sch); 30daf8e166STejun Heo 31cbcda14bSPat Somaru static inline const char *sch_cgrp_path(struct scx_sched *sch) 32cbcda14bSPat Somaru { 33cbcda14bSPat Somaru return sch->cgrp_path; 34cbcda14bSPat Somaru } 35cbcda14bSPat Somaru 36daf8e166STejun Heo #else /* CONFIG_EXT_SUB_SCHED */ 37daf8e166STejun Heo 38daf8e166STejun Heo static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; } 39*bbda59d8STejun Heo static inline struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root) { return NULL; } 40daf8e166STejun Heo static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {} 41daf8e166STejun Heo static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; } 42cbcda14bSPat Somaru static inline const char *sch_cgrp_path(struct scx_sched *sch) { return "/"; } 43daf8e166STejun Heo static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {} 44daf8e166STejun Heo static inline void drain_descendants(struct scx_sched *sch) { } 45daf8e166STejun Heo static inline void scx_sub_disable(struct scx_sched *sch) { } 468dba3bbdSTejun Heo static inline void scx_free_pshards(struct scx_sched *sch) {} 478dba3bbdSTejun Heo static inline s32 scx_alloc_pshards(struct scx_sched *sch) { return 0; } 48daf8e166STejun Heo 49daf8e166STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 50daf8e166STejun Heo 51daf8e166STejun Heo /** 52daf8e166STejun Heo * scx_for_each_descendant_pre - pre-order walk of a sched's descendants 53daf8e166STejun Heo * @pos: iteration cursor 54daf8e166STejun Heo * @root: sched to walk the descendants of 55daf8e166STejun Heo * 56daf8e166STejun Heo * Walk @root's descendants. @root is included in the iteration and the first 5770f8b178STejun Heo * node to be visited. Must be called with scx_enable_mutex, scx_sched_lock, or 5870f8b178STejun Heo * RCU read lock. 59daf8e166STejun Heo */ 60daf8e166STejun Heo #define scx_for_each_descendant_pre(pos, root) \ 61daf8e166STejun Heo for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos); \ 62daf8e166STejun Heo (pos) = scx_next_descendant_pre((pos), (root))) 63daf8e166STejun Heo 64daf8e166STejun Heo /* 65daf8e166STejun Heo * One user of this function is scx_bpf_dispatch() which can be called 66daf8e166STejun Heo * recursively as sub-sched dispatches nest. Always inline to reduce stack usage 67daf8e166STejun Heo * from the call frame. 68daf8e166STejun Heo */ 69daf8e166STejun Heo static __always_inline bool 70daf8e166STejun Heo scx_dispatch_sched(struct scx_sched *sch, struct rq *rq, 71daf8e166STejun Heo struct task_struct *prev, bool nested) 72daf8e166STejun Heo { 73daf8e166STejun Heo struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx; 74daf8e166STejun Heo int nr_loops = SCX_DSP_MAX_LOOPS; 75daf8e166STejun Heo s32 cpu = cpu_of(rq); 76daf8e166STejun Heo bool prev_on_sch = (prev->sched_class == &ext_sched_class) && 77daf8e166STejun Heo scx_task_on_sched(sch, prev); 78daf8e166STejun Heo 79daf8e166STejun Heo if (scx_consume_global_dsq(sch, rq)) 80daf8e166STejun Heo return true; 81daf8e166STejun Heo 82daf8e166STejun Heo if (scx_bypass_dsp_enabled(sch)) { 83daf8e166STejun Heo /* if @sch is bypassing, only the bypass DSQs are active */ 84daf8e166STejun Heo if (scx_bypassing(sch, cpu)) 85daf8e166STejun Heo return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 86daf8e166STejun Heo 87daf8e166STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 88daf8e166STejun Heo /* 89daf8e166STejun Heo * If @sch isn't bypassing but its children are, @sch is 90daf8e166STejun Heo * responsible for making forward progress for both its own 91daf8e166STejun Heo * tasks that aren't bypassing and the bypassing descendants' 92daf8e166STejun Heo * tasks. The following implements a simple built-in behavior - 93daf8e166STejun Heo * let each CPU try to run the bypass DSQ every Nth time. 94daf8e166STejun Heo * 95daf8e166STejun Heo * Later, if necessary, we can add an ops flag to suppress the 96daf8e166STejun Heo * auto-consumption and a kfunc to consume the bypass DSQ and, 97daf8e166STejun Heo * so that the BPF scheduler can fully control scheduling of 98daf8e166STejun Heo * bypassed tasks. 99daf8e166STejun Heo */ 100daf8e166STejun Heo struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu); 101daf8e166STejun Heo 102daf8e166STejun Heo if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) && 103daf8e166STejun Heo scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0)) { 104daf8e166STejun Heo __scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1); 105daf8e166STejun Heo return true; 106daf8e166STejun Heo } 107daf8e166STejun Heo #endif /* CONFIG_EXT_SUB_SCHED */ 108daf8e166STejun Heo } 109daf8e166STejun Heo 110daf8e166STejun Heo if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !scx_rq_online(rq)) 111daf8e166STejun Heo return false; 112daf8e166STejun Heo 113daf8e166STejun Heo dspc->rq = rq; 114daf8e166STejun Heo 115daf8e166STejun Heo /* 116daf8e166STejun Heo * The dispatch loop. Because scx_flush_dispatch_buf() may drop the rq 117daf8e166STejun Heo * lock, the local DSQ might still end up empty after a successful 118daf8e166STejun Heo * ops.dispatch(). If the local DSQ is empty even after ops.dispatch() 119daf8e166STejun Heo * produced some tasks, retry. The BPF scheduler may depend on this 120daf8e166STejun Heo * looping behavior to simplify its implementation. 121daf8e166STejun Heo */ 122daf8e166STejun Heo do { 123daf8e166STejun Heo dspc->nr_tasks = 0; 124daf8e166STejun Heo 125daf8e166STejun Heo if (nested) { 126daf8e166STejun Heo SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 127daf8e166STejun Heo prev_on_sch ? prev : NULL); 128daf8e166STejun Heo } else { 129daf8e166STejun Heo /* stash @prev so that nested invocations can access it */ 130daf8e166STejun Heo rq->scx.sub_dispatch_prev = prev; 131daf8e166STejun Heo SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu), 132daf8e166STejun Heo prev_on_sch ? prev : NULL); 133daf8e166STejun Heo rq->scx.sub_dispatch_prev = NULL; 134daf8e166STejun Heo } 135daf8e166STejun Heo 136daf8e166STejun Heo scx_flush_dispatch_buf(sch, rq); 137daf8e166STejun Heo 138daf8e166STejun Heo if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice) { 139daf8e166STejun Heo rq->scx.flags |= SCX_RQ_BAL_KEEP; 140daf8e166STejun Heo return true; 141daf8e166STejun Heo } 142daf8e166STejun Heo if (rq->scx.local_dsq.nr) 143daf8e166STejun Heo return true; 144daf8e166STejun Heo if (scx_consume_global_dsq(sch, rq)) 145daf8e166STejun Heo return true; 146daf8e166STejun Heo 147daf8e166STejun Heo /* 148daf8e166STejun Heo * ops.dispatch() can trap us in this loop by repeatedly 149daf8e166STejun Heo * dispatching ineligible tasks. Break out once in a while to 150daf8e166STejun Heo * allow the watchdog to run. As IRQ can't be enabled in 151daf8e166STejun Heo * balance(), we want to complete this scheduling cycle and then 152daf8e166STejun Heo * start a new one. IOW, we want to call resched_curr() on the 153daf8e166STejun Heo * next, most likely idle, task, not the current one. Use 154daf8e166STejun Heo * __scx_bpf_kick_cpu() for deferred kicking. 155daf8e166STejun Heo */ 156daf8e166STejun Heo if (unlikely(!--nr_loops)) { 157daf8e166STejun Heo scx_kick_cpu(sch, cpu, 0); 158daf8e166STejun Heo break; 159daf8e166STejun Heo } 160daf8e166STejun Heo } while (dspc->nr_tasks); 161daf8e166STejun Heo 162daf8e166STejun Heo /* 163daf8e166STejun Heo * Prevent the CPU from going idle while bypassed descendants have tasks 164daf8e166STejun Heo * queued. Without this fallback, bypassed tasks could stall if the host 165daf8e166STejun Heo * scheduler's ops.dispatch() doesn't yield any tasks. 166daf8e166STejun Heo */ 167daf8e166STejun Heo if (scx_bypass_dsp_enabled(sch)) 168daf8e166STejun Heo return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0); 169daf8e166STejun Heo 170daf8e166STejun Heo return false; 171daf8e166STejun Heo } 172daf8e166STejun Heo 173daf8e166STejun Heo #endif /* _KERNEL_SCHED_EXT_SUB_H */ 174