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