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