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