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