xref: /linux/kernel/sched/ext/inlines.h (revision 7f480f34b78a0f482fbaa2aadece036aad275a74)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst
4  *
5  * Inline definitions layered on top of internal.h and cid.h.
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_INLINES_H
11 #define _KERNEL_SCHED_EXT_INLINES_H
12 
13 #include "internal.h"
14 #include "cid.h"
15 
16 /*
17  * One user of this function is scx_bpf_dispatch() which can be called
18  * recursively as sub-sched dispatches nest. Always inline to reduce stack usage
19  * from the call frame.
20  */
21 static __always_inline bool
22 scx_dispatch_sched(struct scx_sched *sch, struct rq *rq,
23 		   struct task_struct *prev, bool nested)
24 {
25 	struct scx_dsp_ctx *dspc = &this_cpu_ptr(sch->pcpu)->dsp_ctx;
26 	int nr_loops = SCX_DSP_MAX_LOOPS;
27 	s32 cpu = cpu_of(rq);
28 	bool prev_on_sch = (prev->sched_class == &ext_sched_class) &&
29 		scx_task_on_sched(sch, prev);
30 
31 	if (scx_consume_global_dsq(sch, rq))
32 		return true;
33 
34 	if (scx_bypass_dsp_enabled(sch)) {
35 		/* if @sch is bypassing, only the bypass DSQs are active */
36 		if (scx_bypassing(sch, cpu))
37 			return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0);
38 
39 #ifdef CONFIG_EXT_SUB_SCHED
40 		/*
41 		 * If @sch isn't bypassing but its children are, @sch is
42 		 * responsible for making forward progress for both its own
43 		 * tasks that aren't bypassing and the bypassing descendants'
44 		 * tasks. The following implements a simple built-in behavior -
45 		 * let each CPU try to run the bypass DSQ every Nth time.
46 		 *
47 		 * Later, if necessary, we can add an ops flag to suppress the
48 		 * auto-consumption and a kfunc to consume the bypass DSQ and,
49 		 * so that the BPF scheduler can fully control scheduling of
50 		 * bypassed tasks.
51 		 */
52 		struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu);
53 
54 		if (!(pcpu->bypass_host_seq++ % SCX_BYPASS_HOST_NTH) &&
55 		    scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0)) {
56 			__scx_add_event(sch, SCX_EV_SUB_BYPASS_DISPATCH, 1);
57 			return true;
58 		}
59 #endif	/* CONFIG_EXT_SUB_SCHED */
60 	}
61 
62 	if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !scx_rq_online(rq))
63 		return false;
64 
65 	dspc->rq = rq;
66 
67 	/*
68 	 * The dispatch loop. Because scx_flush_dispatch_buf() may drop the rq
69 	 * lock, the local DSQ might still end up empty after a successful
70 	 * ops.dispatch(). If the local DSQ is empty even after ops.dispatch()
71 	 * produced some tasks, retry. The BPF scheduler may depend on this
72 	 * looping behavior to simplify its implementation.
73 	 */
74 	do {
75 		dspc->nr_tasks = 0;
76 
77 		if (nested) {
78 			SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu),
79 				    prev_on_sch ? prev : NULL);
80 		} else {
81 			/* stash @prev so that nested invocations can access it */
82 			rq->scx.sub_dispatch_prev = prev;
83 			SCX_CALL_OP(sch, dispatch, rq, scx_cpu_arg(cpu),
84 				    prev_on_sch ? prev : NULL);
85 			rq->scx.sub_dispatch_prev = NULL;
86 		}
87 
88 		scx_flush_dispatch_buf(sch, rq);
89 
90 		if ((prev->scx.flags & SCX_TASK_QUEUED) && prev->scx.slice) {
91 			rq->scx.flags |= SCX_RQ_BAL_KEEP;
92 			return true;
93 		}
94 		if (rq->scx.local_dsq.nr)
95 			return true;
96 		if (scx_consume_global_dsq(sch, rq))
97 			return true;
98 
99 		/*
100 		 * ops.dispatch() can trap us in this loop by repeatedly
101 		 * dispatching ineligible tasks. Break out once in a while to
102 		 * allow the watchdog to run. As IRQ can't be enabled in
103 		 * balance(), we want to complete this scheduling cycle and then
104 		 * start a new one. IOW, we want to call resched_curr() on the
105 		 * next, most likely idle, task, not the current one. Use
106 		 * __scx_bpf_kick_cpu() for deferred kicking.
107 		 */
108 		if (unlikely(!--nr_loops)) {
109 			scx_kick_cpu(sch, cpu, 0);
110 			break;
111 		}
112 	} while (dspc->nr_tasks);
113 
114 	/*
115 	 * Prevent the CPU from going idle while bypassed descendants have tasks
116 	 * queued. Without this fallback, bypassed tasks could stall if the host
117 	 * scheduler's ops.dispatch() doesn't yield any tasks.
118 	 */
119 	if (scx_bypass_dsp_enabled(sch))
120 		return scx_consume_dispatch_q(sch, rq, scx_bypass_dsq(sch, cpu), 0);
121 
122 	return false;
123 }
124 
125 #endif /* _KERNEL_SCHED_EXT_INLINES_H */
126