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