xref: /linux/kernel/sched/ext/sub.h (revision 11260c335ec6071af5543aef73000b28f041c124)
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 
15daf8e166STejun Heo #ifdef CONFIG_EXT_SUB_SCHED
16daf8e166STejun Heo 
17bbda59d8STejun Heo struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root);
18daf8e166STejun Heo struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root);
19daf8e166STejun Heo void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch);
20daf8e166STejun Heo struct cgroup *sch_cgroup(struct scx_sched *sch);
21daf8e166STejun Heo void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch);
22daf8e166STejun Heo void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog);
235f2a9a4cSTejun Heo void scx_pstack_recursion_on_caps_updated(struct bpf_prog *prog);
24daf8e166STejun Heo void drain_descendants(struct scx_sched *sch);
25daf8e166STejun Heo void scx_sub_disable(struct scx_sched *sch);
26daf8e166STejun Heo void scx_sub_enable_workfn(struct kthread_work *work);
27daf8e166STejun Heo bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux);
288dba3bbdSTejun Heo void scx_free_pshards(struct scx_sched *sch);
298dba3bbdSTejun Heo s32 scx_alloc_pshards(struct scx_sched *sch);
3086094b95STejun Heo void scx_init_root_caps(struct scx_sched *sch);
31b81a6c01STejun Heo void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev);
3275c268edSTejun Heo void scx_unbypass_replay_ecaps(struct rq *rq, struct scx_sched *sch);
33b81a6c01STejun Heo void scx_online_ecaps(struct rq *rq);
34b81a6c01STejun Heo void scx_offline_ecaps(struct rq *rq);
3556fdc35bSTejun Heo void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu);
3656fdc35bSTejun Heo void scx_discard_stale_ecaps_syncs(void);
378b3b8522STejun Heo struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *rq,
3875a8c820STejun Heo 					     struct task_struct *p, u64 *enq_flags);
3975a8c820STejun Heo bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p);
4075a8c820STejun Heo void scx_reenq_reject(struct rq *rq);
41*5fd50174STejun Heo void scx_rescue_charge(struct rq *rq, s64 delta_exec);
42*5fd50174STejun Heo void scx_rescue_end(struct rq *rq);
43*5fd50174STejun Heo bool scx_rescue_keep(struct rq *rq, struct task_struct *p);
44*5fd50174STejun Heo void scx_rescue_flush(struct rq *rq);
45*5fd50174STejun Heo void scx_rescue_dump(struct seq_buf *s, struct rq *rq);
46*5fd50174STejun Heo void scx_rescue_set_knobs(struct scx_sched *sch);
47*5fd50174STejun Heo void scx_rescue_init(struct rq *rq);
48daf8e166STejun Heo 
4979474420STejun Heo /*
5079474420STejun Heo  * cgrp->scx_sched is written by root/sub enable/disable under all of
5179474420STejun Heo  * scx_enable_mutex, scx_fork_rwsem and cgroup_mutex. A new cgroup inherits the
5279474420STejun Heo  * parent's sched under just cgroup_mutex but is not yet reachable by the other
5379474420STejun Heo  * two lock holders. Any one of the three locks stabilizes the association.
5479474420STejun Heo  */
scx_cgroup_sched(struct cgroup * cgrp)5579474420STejun Heo static inline struct scx_sched *scx_cgroup_sched(struct cgroup *cgrp)
5679474420STejun Heo {
5779474420STejun Heo 	return rcu_dereference_check(cgrp->scx_sched,
5879474420STejun Heo 				     lockdep_is_held(&cgroup_mutex) ||
5979474420STejun Heo 				     percpu_rwsem_is_held(&scx_fork_rwsem) ||
6079474420STejun Heo 				     lockdep_is_held(&scx_enable_mutex));
6179474420STejun Heo }
6279474420STejun Heo 
sch_cgrp_path(struct scx_sched * sch)63cbcda14bSPat Somaru static inline const char *sch_cgrp_path(struct scx_sched *sch)
64cbcda14bSPat Somaru {
65cbcda14bSPat Somaru 	return sch->cgrp_path;
66cbcda14bSPat Somaru }
67cbcda14bSPat Somaru 
688946dbd3STejun Heo /* a dying sub's hot-path influence ends in scx_sched_free_rcu_work() */
scx_dec_has_subs(struct scx_sched * sch)698946dbd3STejun Heo static inline void scx_dec_has_subs(struct scx_sched *sch)
708946dbd3STejun Heo {
718946dbd3STejun Heo 	if (sch->level)
728946dbd3STejun Heo 		static_branch_dec(&__scx_has_subs);
738946dbd3STejun Heo }
748946dbd3STejun Heo 
75daf8e166STejun Heo #else	/* CONFIG_EXT_SUB_SCHED */
76daf8e166STejun Heo 
scx_next_descendant_pre(struct scx_sched * pos,struct scx_sched * root)77daf8e166STejun Heo static inline struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root) { return pos ? NULL : root; }
scx_skip_subtree_pre(struct scx_sched * pos,struct scx_sched * root)78bbda59d8STejun Heo static inline struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root) { return NULL; }
scx_set_task_sched(struct task_struct * p,struct scx_sched * sch)79daf8e166STejun Heo static inline void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch) {}
sch_cgroup(struct scx_sched * sch)80daf8e166STejun Heo static inline struct cgroup *sch_cgroup(struct scx_sched *sch) { return NULL; }
sch_cgrp_path(struct scx_sched * sch)81cbcda14bSPat Somaru static inline const char *sch_cgrp_path(struct scx_sched *sch) { return "/"; }
set_cgroup_sched(struct cgroup * cgrp,struct scx_sched * sch)82daf8e166STejun Heo static inline void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch) {}
drain_descendants(struct scx_sched * sch)83daf8e166STejun Heo static inline void drain_descendants(struct scx_sched *sch) { }
scx_sub_disable(struct scx_sched * sch)84daf8e166STejun Heo static inline void scx_sub_disable(struct scx_sched *sch) { }
scx_free_pshards(struct scx_sched * sch)858dba3bbdSTejun Heo static inline void scx_free_pshards(struct scx_sched *sch) {}
scx_alloc_pshards(struct scx_sched * sch)868dba3bbdSTejun Heo static inline s32 scx_alloc_pshards(struct scx_sched *sch) { return 0; }
scx_init_root_caps(struct scx_sched * sch)8786094b95STejun Heo static inline void scx_init_root_caps(struct scx_sched *sch) {}
scx_process_sync_ecaps(struct rq * rq,struct task_struct * prev)88b81a6c01STejun Heo static inline void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev) {}
scx_unbypass_replay_ecaps(struct rq * rq,struct scx_sched * sch)8975c268edSTejun Heo static inline void scx_unbypass_replay_ecaps(struct rq *rq, struct scx_sched *sch) {}
scx_online_ecaps(struct rq * rq)90b81a6c01STejun Heo static inline void scx_online_ecaps(struct rq *rq) {}
scx_offline_ecaps(struct rq * rq)91b81a6c01STejun Heo static inline void scx_offline_ecaps(struct rq *rq) {}
scx_discard_ecaps_to_sync(s32 cpu,struct scx_sched_pcpu * pcpu)9256fdc35bSTejun Heo static inline void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu) {}
scx_discard_stale_ecaps_syncs(void)9356fdc35bSTejun Heo static inline void scx_discard_stale_ecaps_syncs(void) {}
scx_resolve_local_dsq(struct scx_sched * sch,struct rq * rq,struct task_struct * p,u64 * enq_flags)948b3b8522STejun Heo static inline struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *rq, struct task_struct *p, u64 *enq_flags) { return &rq->scx.local_dsq; }
scx_task_reenq_on_cap_revoke(struct rq * rq,struct task_struct * p)9575a8c820STejun Heo static inline bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p) { return false; }
scx_reenq_reject(struct rq * rq)9675a8c820STejun Heo static inline void scx_reenq_reject(struct rq *rq) {}
scx_rescue_charge(struct rq * rq,s64 delta_exec)97*5fd50174STejun Heo static inline void scx_rescue_charge(struct rq *rq, s64 delta_exec) {}
scx_rescue_end(struct rq * rq)98*5fd50174STejun Heo static inline void scx_rescue_end(struct rq *rq) {}
scx_rescue_keep(struct rq * rq,struct task_struct * p)99*5fd50174STejun Heo static inline bool scx_rescue_keep(struct rq *rq, struct task_struct *p) { return false; }
scx_rescue_flush(struct rq * rq)100*5fd50174STejun Heo static inline void scx_rescue_flush(struct rq *rq) {}
scx_rescue_dump(struct seq_buf * s,struct rq * rq)101*5fd50174STejun Heo static inline void scx_rescue_dump(struct seq_buf *s, struct rq *rq) {}
scx_rescue_set_knobs(struct scx_sched * sch)102*5fd50174STejun Heo static inline void scx_rescue_set_knobs(struct scx_sched *sch) {}
scx_rescue_init(struct rq * rq)103*5fd50174STejun Heo static inline void scx_rescue_init(struct rq *rq) {}
scx_dec_has_subs(struct scx_sched * sch)1048946dbd3STejun Heo static inline void scx_dec_has_subs(struct scx_sched *sch) {}
105daf8e166STejun Heo 
106daf8e166STejun Heo #endif	/* CONFIG_EXT_SUB_SCHED */
107daf8e166STejun Heo 
108daf8e166STejun Heo /**
109daf8e166STejun Heo  * scx_for_each_descendant_pre - pre-order walk of a sched's descendants
110daf8e166STejun Heo  * @pos: iteration cursor
111daf8e166STejun Heo  * @root: sched to walk the descendants of
112daf8e166STejun Heo  *
113daf8e166STejun Heo  * Walk @root's descendants. @root is included in the iteration and the first
11470f8b178STejun Heo  * node to be visited. Must be called with scx_enable_mutex, scx_sched_lock, or
11570f8b178STejun Heo  * RCU read lock.
116daf8e166STejun Heo  */
117daf8e166STejun Heo #define scx_for_each_descendant_pre(pos, root)					\
118daf8e166STejun Heo 	for ((pos) = scx_next_descendant_pre(NULL, (root)); (pos);		\
119daf8e166STejun Heo 	     (pos) = scx_next_descendant_pre((pos), (root)))
120daf8e166STejun Heo 
12156fdc35bSTejun Heo #ifdef CONFIG_EXT_SUB_SCHED
12256fdc35bSTejun Heo 
12375a8c820STejun Heo /**
12475a8c820STejun Heo  * scx_missing_caps - The caps in @needed that @sch lacks on @cpu
12575a8c820STejun Heo  * @sch: sched to test
12675a8c820STejun Heo  * @cpu: cpu to test on
12775a8c820STejun Heo  * @needed: bitmask of SCX_CAP_* values
12875a8c820STejun Heo  *
12975a8c820STejun Heo  * Return the caps in @needed that @sch lacks for @cpu, 0 if it holds them all.
13075a8c820STejun Heo  */
scx_missing_caps(struct scx_sched * sch,s32 cpu,u64 needed)13175a8c820STejun Heo static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed)
13275a8c820STejun Heo {
13375a8c820STejun Heo 	u64 ecaps;
13475a8c820STejun Heo 
1358946dbd3STejun Heo 	/* no sub-scheds, no missing caps */
1368946dbd3STejun Heo 	if (!scx_has_subs())
1378946dbd3STejun Heo 		return 0;
1388946dbd3STejun Heo 
13975a8c820STejun Heo 	/* root holds every cap on every cpu */
14075a8c820STejun Heo 	if (!sch->level)
14175a8c820STejun Heo 		return 0;
14275a8c820STejun Heo 
14375a8c820STejun Heo 	ecaps = READ_ONCE(per_cpu_ptr(sch->pcpu, cpu)->ecaps);
14475a8c820STejun Heo 
14575a8c820STejun Heo 	return needed & ~ecaps;
14675a8c820STejun Heo }
14775a8c820STejun Heo 
14875a8c820STejun Heo /*
14975a8c820STejun Heo  * Cap semantics: which caps an action requires, and which caps a cap implies.
15075a8c820STejun Heo  * Keep all such mappings collected here.
15175a8c820STejun Heo  */
15275a8c820STejun Heo 
15375a8c820STejun Heo /* map @enq_flags to the SCX_CAP_* bit required for the local-DSQ insert */
scx_caps_for_enq(u64 enq_flags)15475a8c820STejun Heo static inline u64 scx_caps_for_enq(u64 enq_flags)
15575a8c820STejun Heo {
1568b175234STejun Heo 	/* a restored task must be put into the local DSQ regardless of caps */
15778f8d726STejun Heo 	if (unlikely(enq_flags & SCX_ENQ_IGNORE_CAPS))
1588b175234STejun Heo 		return 0;
159701b7bcaSTejun Heo 	if (enq_flags & SCX_ENQ_IMMED)
160147d1885STejun Heo 		return SCX_CAP_ENQ_IMMED;
161701b7bcaSTejun Heo 	return SCX_CAP_ENQ;
16275a8c820STejun Heo }
16375a8c820STejun Heo 
16475a8c820STejun Heo /* map queued @p to the SCX_CAP_* bit required to stay on its local DSQ */
scx_caps_for_task(struct task_struct * p)16575a8c820STejun Heo static inline u64 scx_caps_for_task(struct task_struct *p)
16675a8c820STejun Heo {
167701b7bcaSTejun Heo 	if (p->scx.flags & SCX_TASK_IMMED)
168147d1885STejun Heo 		return SCX_CAP_ENQ_IMMED;
169701b7bcaSTejun Heo 	return SCX_CAP_ENQ;
17075a8c820STejun Heo }
17175a8c820STejun Heo 
1726ea3be36STejun Heo /* the cap @sch needs to preempt @rq's current task, 0 if none */
scx_caps_for_preempt(struct scx_sched * sch,struct rq * rq,u64 enq_flags)17378f8d726STejun Heo static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq, u64 enq_flags)
1746ea3be36STejun Heo {
1756ea3be36STejun Heo 	struct task_struct *curr = rq->curr;
1766ea3be36STejun Heo 
17778f8d726STejun Heo 	/* a kernel-forced placement preempts regardless of caps */
17878f8d726STejun Heo 	if (unlikely(enq_flags & SCX_ENQ_IGNORE_CAPS))
17978f8d726STejun Heo 		return 0;
1806ea3be36STejun Heo 	/* a non-ext task can't be preempted by ext, own-subtree needs no cap */
1816ea3be36STejun Heo 	if (curr->sched_class != &ext_sched_class ||
1826ea3be36STejun Heo 	    scx_is_descendant(scx_task_sched(curr), sch))
1836ea3be36STejun Heo 		return 0;
1846ea3be36STejun Heo 	return SCX_CAP_PREEMPT;
1856ea3be36STejun Heo }
1866ea3be36STejun Heo 
18756fdc35bSTejun Heo /* caps implied by holding @cap */
scx_caps_implied(u64 cap)18856fdc35bSTejun Heo static inline u64 scx_caps_implied(u64 cap)
18956fdc35bSTejun Heo {
190701b7bcaSTejun Heo 	switch (cap) {
1916ea3be36STejun Heo 	case SCX_CAP_PREEMPT:
1926ea3be36STejun Heo 		return SCX_CAP_ENQ | SCX_CAP_ENQ_IMMED;
193701b7bcaSTejun Heo 	case SCX_CAP_ENQ:
194701b7bcaSTejun Heo 		return SCX_CAP_ENQ_IMMED;
195701b7bcaSTejun Heo 	}
19656fdc35bSTejun Heo 	return 0;
19756fdc35bSTejun Heo }
19856fdc35bSTejun Heo 
19946a85ae6STejun Heo /* may @p keep running on @rq's cpu? requires baseline cpu access */
scx_task_can_stay_on_cpu(struct rq * rq,struct task_struct * p)20046a85ae6STejun Heo static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p)
20146a85ae6STejun Heo {
2028946dbd3STejun Heo 	if (!scx_has_subs())
2038946dbd3STejun Heo 		return true;
2048946dbd3STejun Heo 
20546a85ae6STejun Heo 	/* a migration-disabled task is let in without caps, keep it likewise */
20646a85ae6STejun Heo 	if (unlikely(is_migration_disabled(p)))
20746a85ae6STejun Heo 		return true;
20846a85ae6STejun Heo 
20946a85ae6STejun Heo 	return likely(!scx_missing_caps(scx_task_sched(p), cpu_of(rq), SCX_CAP_BASE));
21046a85ae6STejun Heo }
21146a85ae6STejun Heo 
212*5fd50174STejun Heo /* the task admitted for rescue on @rq, NULL if none */
scx_rescuee(struct rq * rq)213*5fd50174STejun Heo static inline struct task_struct *scx_rescuee(struct rq *rq)
214*5fd50174STejun Heo {
215*5fd50174STejun Heo 	lockdep_assert_rq_held(rq);
216*5fd50174STejun Heo 
217*5fd50174STejun Heo 	if (!scx_has_subs())
218*5fd50174STejun Heo 		return NULL;
219*5fd50174STejun Heo 
220*5fd50174STejun Heo 	return rq->scx.rescue.curr;
221*5fd50174STejun Heo }
222*5fd50174STejun Heo 
22375a8c820STejun Heo #else	/* CONFIG_EXT_SUB_SCHED */
22475a8c820STejun Heo 
scx_missing_caps(struct scx_sched * sch,s32 cpu,u64 needed)22575a8c820STejun Heo static inline u64 scx_missing_caps(struct scx_sched *sch, s32 cpu, u64 needed) { return 0; }
scx_caps_for_preempt(struct scx_sched * sch,struct rq * rq,u64 enq_flags)22678f8d726STejun Heo static inline u64 scx_caps_for_preempt(struct scx_sched *sch, struct rq *rq, u64 enq_flags) { return 0; }
scx_task_can_stay_on_cpu(struct rq * rq,struct task_struct * p)22746a85ae6STejun Heo static inline bool scx_task_can_stay_on_cpu(struct rq *rq, struct task_struct *p) { return true; }
scx_rescuee(struct rq * rq)228*5fd50174STejun Heo static inline struct task_struct *scx_rescuee(struct rq *rq) { return NULL; }
22975a8c820STejun Heo 
23056fdc35bSTejun Heo #endif	/* CONFIG_EXT_SUB_SCHED */
23156fdc35bSTejun Heo 
232daf8e166STejun Heo #endif /* _KERNEL_SCHED_EXT_SUB_H */
233