xref: /linux/kernel/sched/ext/sub.c (revision 0dc90ce1be18ea6b18b545f4752de64cfc140ccd)
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  * A sub-scheduler is an scx_sched attached to a cgroup subtree under another
8daf8e166STejun Heo  * scx_sched. This file holds the sub-scheduler implementation: the scheduler
9daf8e166STejun Heo  * tree walk, capability delegation, per-shard cap state and its sync, and the
10daf8e166STejun Heo  * sub-scheduler enable/disable paths. The core dispatch/enqueue machinery it
11daf8e166STejun Heo  * builds on lives in ext.c.
12daf8e166STejun Heo  *
13daf8e166STejun Heo  * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
14daf8e166STejun Heo  * Copyright (c) 2026 Tejun Heo <tj@kernel.org>
15daf8e166STejun Heo  */
16daf8e166STejun Heo #include <linux/rhashtable.h>
17daf8e166STejun Heo #include "internal.h"
18daf8e166STejun Heo #include "cid.h"
19daf8e166STejun Heo #include "arena.h"
20daf8e166STejun Heo #include "sub.h"
217f480f34STejun Heo #include "inlines.h"
22daf8e166STejun Heo 
23daf8e166STejun Heo #ifdef CONFIG_EXT_SUB_SCHED
24daf8e166STejun Heo 
258946dbd3STejun Heo /*
268946dbd3STejun Heo  * On while any sub-scheduler exists so that a root-only system doesn't pay for
278946dbd3STejun Heo  * the sub-sched portions of hot paths. See scx_has_subs().
288946dbd3STejun Heo  */
298946dbd3STejun Heo DEFINE_STATIC_KEY_FALSE(__scx_has_subs);
308946dbd3STejun Heo 
31daf8e166STejun Heo /**
32bbda59d8STejun Heo  * scx_skip_subtree_pre - Skip @pos's subtree in a pre-order walk
33bbda59d8STejun Heo  * @pos: current position
34bbda59d8STejun Heo  * @root: walk root
35bbda59d8STejun Heo  *
36bbda59d8STejun Heo  * In a walk started by scx_next_descendant_pre(), continue past @pos's subtree:
37bbda59d8STejun Heo  * return @pos's next sibling, or the closest ancestor's next sibling, or NULL
38bbda59d8STejun Heo  * if @pos's subtree is the last under @root. Same locking rules.
39bbda59d8STejun Heo  */
40bbda59d8STejun Heo struct scx_sched *scx_skip_subtree_pre(struct scx_sched *pos, struct scx_sched *root)
41bbda59d8STejun Heo {
42bbda59d8STejun Heo 	struct scx_sched *next;
43bbda59d8STejun Heo 
44bbda59d8STejun Heo 	lockdep_assert(lockdep_is_held(&scx_enable_mutex) ||
45bbda59d8STejun Heo 		       lockdep_is_held(&scx_sched_lock) ||
46bbda59d8STejun Heo 		       rcu_read_lock_any_held());
47bbda59d8STejun Heo 
48bbda59d8STejun Heo 	while (pos != root) {
49bbda59d8STejun Heo 		next = list_next_or_null_rcu(&scx_parent(pos)->children, &pos->sibling,
50bbda59d8STejun Heo 					     struct scx_sched, sibling);
51bbda59d8STejun Heo 		if (next)
52bbda59d8STejun Heo 			return next;
53bbda59d8STejun Heo 		pos = scx_parent(pos);
54bbda59d8STejun Heo 	}
55bbda59d8STejun Heo 	return NULL;
56bbda59d8STejun Heo }
57bbda59d8STejun Heo 
58bbda59d8STejun Heo /**
59daf8e166STejun Heo  * scx_next_descendant_pre - find the next descendant for pre-order walk
60daf8e166STejun Heo  * @pos: the current position (%NULL to initiate traversal)
61daf8e166STejun Heo  * @root: sched whose descendants to walk
62daf8e166STejun Heo  *
63daf8e166STejun Heo  * To be used by scx_for_each_descendant_pre(). Find the next descendant to
64daf8e166STejun Heo  * visit for pre-order traversal of @root's descendants. @root is included in
65daf8e166STejun Heo  * the iteration and the first node to be visited.
66daf8e166STejun Heo  */
67daf8e166STejun Heo struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root)
68daf8e166STejun Heo {
69daf8e166STejun Heo 	struct scx_sched *next;
70daf8e166STejun Heo 
71daf8e166STejun Heo 	lockdep_assert(lockdep_is_held(&scx_enable_mutex) ||
7270f8b178STejun Heo 		       lockdep_is_held(&scx_sched_lock) ||
7370f8b178STejun Heo 		       rcu_read_lock_any_held());
74daf8e166STejun Heo 
75daf8e166STejun Heo 	/* if first iteration, visit @root */
76daf8e166STejun Heo 	if (!pos)
77daf8e166STejun Heo 		return root;
78daf8e166STejun Heo 
79daf8e166STejun Heo 	/* visit the first child if exists */
8070f8b178STejun Heo 	next = list_first_or_null_rcu(&pos->children, struct scx_sched, sibling);
81daf8e166STejun Heo 	if (next)
82daf8e166STejun Heo 		return next;
83daf8e166STejun Heo 
84daf8e166STejun Heo 	/* no child, visit my or the closest ancestor's next sibling */
85bbda59d8STejun Heo 	return scx_skip_subtree_pre(pos, root);
86daf8e166STejun Heo }
87daf8e166STejun Heo 
88daf8e166STejun Heo static struct scx_sched *scx_find_sub_sched(u64 cgroup_id)
89daf8e166STejun Heo {
90daf8e166STejun Heo 	return rhashtable_lookup(&scx_sched_hash, &cgroup_id,
91daf8e166STejun Heo 				 scx_sched_hash_params);
92daf8e166STejun Heo }
93daf8e166STejun Heo 
94daf8e166STejun Heo void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch)
95daf8e166STejun Heo {
96daf8e166STejun Heo 	rcu_assign_pointer(p->scx.sched, sch);
97daf8e166STejun Heo }
98daf8e166STejun Heo 
99daf8e166STejun Heo struct cgroup *sch_cgroup(struct scx_sched *sch)
100daf8e166STejun Heo {
101daf8e166STejun Heo 	return sch->cgrp;
102daf8e166STejun Heo }
103daf8e166STejun Heo 
104daf8e166STejun Heo /* for each descendant of @cgrp including self, set ->scx_sched to @sch */
105daf8e166STejun Heo void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch)
106daf8e166STejun Heo {
107daf8e166STejun Heo 	struct cgroup *pos;
108daf8e166STejun Heo 	struct cgroup_subsys_state *css;
109daf8e166STejun Heo 
110daf8e166STejun Heo 	cgroup_for_each_live_descendant_pre(pos, css, cgrp)
111daf8e166STejun Heo 		rcu_assign_pointer(pos->scx_sched, sch);
112daf8e166STejun Heo }
113daf8e166STejun Heo 
1148dba3bbdSTejun Heo static void free_pshard(struct scx_pshard *pshard)
1158dba3bbdSTejun Heo {
1165f2a9a4cSTejun Heo 	struct scx_caps_updated *cu;
1175f2a9a4cSTejun Heo 
1185f2a9a4cSTejun Heo 	if (!pshard)
1195f2a9a4cSTejun Heo 		return;
1205f2a9a4cSTejun Heo 	cu = &pshard->caps_updated;
1215f2a9a4cSTejun Heo 	if (cu->cmask_arena_out)
1225f2a9a4cSTejun Heo 		scx_arena_free(pshard->sch, cu->cmask_arena_out,
1235f2a9a4cSTejun Heo 			       struct_size_t(struct scx_cmask, bits,
1245f2a9a4cSTejun Heo 					     SCX_CMASK_NR_WORDS(pshard->nr_cids)));
1258dba3bbdSTejun Heo 	kfree(pshard);
1268dba3bbdSTejun Heo }
1278dba3bbdSTejun Heo 
1288dba3bbdSTejun Heo void scx_free_pshards(struct scx_sched *sch)
1298dba3bbdSTejun Heo {
1308dba3bbdSTejun Heo 	s32 si;
1318dba3bbdSTejun Heo 
1328dba3bbdSTejun Heo 	if (!sch->pshard)
1338dba3bbdSTejun Heo 		return;
1348dba3bbdSTejun Heo 	for (si = 0; si < sch->nr_pshards; si++)
1358dba3bbdSTejun Heo 		free_pshard(sch->pshard[si]);
1368dba3bbdSTejun Heo 	kfree(sch->pshard);
1378dba3bbdSTejun Heo }
1388dba3bbdSTejun Heo 
1398dba3bbdSTejun Heo static struct scx_pshard *alloc_pshard(struct scx_sched *sch, s32 shard_idx, s32 node)
1408dba3bbdSTejun Heo {
14186094b95STejun Heo 	const struct scx_cid_shard *shard = &scx_cid_shard_ranges[shard_idx];
1425f2a9a4cSTejun Heo 	size_t cmask_size = struct_size_t(struct scx_cmask, bits,
1435f2a9a4cSTejun Heo 					  SCX_CMASK_NR_WORDS(shard->nr_cids));
14486094b95STejun Heo 	struct scx_pshard *pshard;
1455f2a9a4cSTejun Heo 	struct scx_caps_updated *cu;
14686094b95STejun Heo 	s32 i;
14786094b95STejun Heo 
14886094b95STejun Heo 	pshard = kzalloc_node(sizeof(*pshard), GFP_KERNEL, node);
14986094b95STejun Heo 	if (!pshard)
15086094b95STejun Heo 		return NULL;
15186094b95STejun Heo 
15286094b95STejun Heo 	raw_spin_lock_init(&pshard->lock);
15386094b95STejun Heo 	pshard->sch = sch;
1545f2a9a4cSTejun Heo 	pshard->base = shard->base_cid;
1555f2a9a4cSTejun Heo 	pshard->nr_cids = shard->nr_cids;
15686094b95STejun Heo 
15786094b95STejun Heo 	for (i = 0; i < __SCX_NR_CAPS; i++)
15886094b95STejun Heo 		scx_cmask_init(&pshard->caps[i].cmask, shard->base_cid, shard->nr_cids);
15986094b95STejun Heo 
1605f2a9a4cSTejun Heo 	cu = &pshard->caps_updated;
1615f2a9a4cSTejun Heo 	raw_spin_lock_init(&cu->lock);
1625f2a9a4cSTejun Heo 	INIT_LIST_HEAD(&cu->node_in_flight);
1635f2a9a4cSTejun Heo 	__scx_cmask_init(&cu->cmask, shard->base_cid, shard->nr_cids, SCX_CID_SHARD_MAX_CPUS);
1645f2a9a4cSTejun Heo 
1655f2a9a4cSTejun Heo 	cu->cmask_arena_out = scx_arena_alloc(sch, cmask_size);
1665f2a9a4cSTejun Heo 	if (!cu->cmask_arena_out) {
1675f2a9a4cSTejun Heo 		free_pshard(pshard);
1685f2a9a4cSTejun Heo 		return NULL;
1695f2a9a4cSTejun Heo 	}
1705f2a9a4cSTejun Heo 
1715f2a9a4cSTejun Heo 	scx_cmask_init(cu->cmask_arena_out, shard->base_cid, shard->nr_cids);
1725f2a9a4cSTejun Heo 
17386094b95STejun Heo 	return pshard;
1748dba3bbdSTejun Heo }
1758dba3bbdSTejun Heo 
1768dba3bbdSTejun Heo s32 scx_alloc_pshards(struct scx_sched *sch)
1778dba3bbdSTejun Heo {
1788dba3bbdSTejun Heo 	struct scx_pshard **pshard;
1798dba3bbdSTejun Heo 	s32 si;
1808dba3bbdSTejun Heo 
1818dba3bbdSTejun Heo 	if (!sch->is_cid_type || !sch->arena_pool)
1828dba3bbdSTejun Heo 		return 0;
1838dba3bbdSTejun Heo 
1848dba3bbdSTejun Heo 	pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards, GFP_KERNEL);
1858dba3bbdSTejun Heo 	if (!pshard)
1868dba3bbdSTejun Heo 		return -ENOMEM;
1878dba3bbdSTejun Heo 
1888dba3bbdSTejun Heo 	for (si = 0; si < scx_nr_cid_shards; si++) {
1898dba3bbdSTejun Heo 		pshard[si] = alloc_pshard(sch, si, scx_shard_node[si]);
1908dba3bbdSTejun Heo 		if (!pshard[si]) {
1918dba3bbdSTejun Heo 			while (--si >= 0)
1928dba3bbdSTejun Heo 				free_pshard(pshard[si]);
1938dba3bbdSTejun Heo 			kfree(pshard);
1948dba3bbdSTejun Heo 			return -ENOMEM;
1958dba3bbdSTejun Heo 		}
1968dba3bbdSTejun Heo 	}
1978dba3bbdSTejun Heo 
1988dba3bbdSTejun Heo 	sch->nr_pshards = scx_nr_cid_shards;
1998dba3bbdSTejun Heo 	/*
2008dba3bbdSTejun Heo 	 * Publish only after every entry is built so a reader observing
2018dba3bbdSTejun Heo 	 * @sch->pshard never sees a partially-filled array. Pair the store
2028dba3bbdSTejun Heo 	 * with a barrier and READ_ONCE() on the read side.
2038dba3bbdSTejun Heo 	 */
2048dba3bbdSTejun Heo 	smp_wmb();
2058dba3bbdSTejun Heo 	WRITE_ONCE(sch->pshard, pshard);
2068dba3bbdSTejun Heo 	return 0;
2078dba3bbdSTejun Heo }
2088dba3bbdSTejun Heo 
20986094b95STejun Heo /*
21086094b95STejun Heo  * Seed the root's caps fully. Root owns all cids on all caps at enable time.
21186094b95STejun Heo  * Children acquire caps via scx_bpf_sub_grant().
21286094b95STejun Heo  */
21386094b95STejun Heo void scx_init_root_caps(struct scx_sched *sch)
21486094b95STejun Heo {
21586094b95STejun Heo 	s32 si, i;
21686094b95STejun Heo 
21786094b95STejun Heo 	for (si = 0; si < sch->nr_pshards; si++) {
21886094b95STejun Heo 		struct scx_pshard *ps = sch->pshard[si];
21986094b95STejun Heo 
22086094b95STejun Heo 		for (i = 0; i < __SCX_NR_CAPS; i++)
22186094b95STejun Heo 			scx_cmask_fill(&ps->caps[i].cmask);
22286094b95STejun Heo 	}
22386094b95STejun Heo }
22486094b95STejun Heo 
22575a8c820STejun Heo /**
22675a8c820STejun Heo  * scx_local_or_reject_dsq - Pick the local or reject DSQ for an insert
22775a8c820STejun Heo  * @sch: enqueuing sub-sched
22875a8c820STejun Heo  * @rq: rq whose local DSQ @p targets
22975a8c820STejun Heo  * @p: task being inserted
2306ea3be36STejun Heo  * @enq_flags: in/out, unhonored flags are cleared
23175a8c820STejun Heo  *
23275a8c820STejun Heo  * Return @rq's local DSQ if @sch holds the required caps on @rq's cid,
23375a8c820STejun Heo  * otherwise @rq's reject DSQ after recording the reenq reason on @p.
23475a8c820STejun Heo  *
2356ea3be36STejun Heo  * %SCX_ENQ_IMMED and %SCX_ENQ_PREEMPT are cleared when diverting to reject.
2366ea3be36STejun Heo  * %SCX_ENQ_PREEMPT is also cleared on a fallback migration-disabled admission.
2376ea3be36STejun Heo  *
23875a8c820STejun Heo  * Bypass doesn't need special-casing as a bypassing sched's tasks are enqueued
23975a8c820STejun Heo  * to and run by its nearest non-bypassing ancestor. If root is bypassing, it
24075a8c820STejun Heo  * always holds all caps.
24175a8c820STejun Heo  */
24275a8c820STejun Heo struct scx_dispatch_q *scx_local_or_reject_dsq(struct scx_sched *sch, struct rq *rq,
24375a8c820STejun Heo 					       struct task_struct *p, u64 *enq_flags)
24475a8c820STejun Heo {
2458946dbd3STejun Heo 	if (!scx_has_subs())
2468946dbd3STejun Heo 		return &rq->scx.local_dsq;
2478946dbd3STejun Heo 
24875a8c820STejun Heo 	s32 cid = __scx_cpu_to_cid(cpu_of(rq));
249f2c9f515STejun Heo 	struct scx_sched *asch = rq->scx.remote_activate_sch ?: sch;
2506ea3be36STejun Heo 	u64 needed = scx_caps_for_enq(*enq_flags);
2516ea3be36STejun Heo 	u64 missing;
2526ea3be36STejun Heo 
253f2c9f515STejun Heo 	/*
254f2c9f515STejun Heo 	 * On a remote activation the scheduling sched (@asch) differs from
255f2c9f515STejun Heo 	 * @p's owner (@sch). Check caps against the scheduling sched.
256f2c9f515STejun Heo 	 */
2576ea3be36STejun Heo 	if (*enq_flags & SCX_ENQ_PREEMPT)
258f2c9f515STejun Heo 		needed |= scx_caps_for_preempt(asch, rq);
259f2c9f515STejun Heo 	missing = scx_missing_caps(asch, cpu_of(rq), needed);
26075a8c820STejun Heo 
26175a8c820STejun Heo 	/* requirements met */
26275a8c820STejun Heo 	if (likely(!missing))
26375a8c820STejun Heo 		return &rq->scx.local_dsq;
26475a8c820STejun Heo 
26575a8c820STejun Heo 	/*
26675a8c820STejun Heo 	 * The task must run on this CPU regardless of caps: the rq is draining
26775a8c820STejun Heo 	 * offline (BPF scheduler bypassed), the task is migration-disabled, or a
26875a8c820STejun Heo 	 * migration is pending. Admit despite the missing caps and count it.
2696ea3be36STejun Heo 	 * Refuse preemptions.
27075a8c820STejun Heo 	 */
27175a8c820STejun Heo 	if (unlikely(!scx_rq_online(rq) || is_migration_disabled(p) ||
27275a8c820STejun Heo 		     p->migration_pending)) {
27375a8c820STejun Heo 		__scx_add_event(sch, SCX_EV_SUB_FORCED_ADMIT, 1);
2746ea3be36STejun Heo 		*enq_flags &= ~SCX_ENQ_PREEMPT;
27575a8c820STejun Heo 		return &rq->scx.local_dsq;
27675a8c820STejun Heo 	}
27775a8c820STejun Heo 
27875a8c820STejun Heo 	p->scx.reenq_reason_caps = missing;
27975a8c820STejun Heo 	p->scx.reenq_reason_cid = cid;
28075a8c820STejun Heo 
28175a8c820STejun Heo 	/*
28275a8c820STejun Heo 	 * Only local DSQ can honor IMMED and dsq_inc_nr() WARNs on IMMED into
28375a8c820STejun Heo 	 * others. Strip both the enq flag and the sticky task flag - the
2846ea3be36STejun Heo 	 * latter can carry in from an earlier admitted IMMED insert. Strip
2856ea3be36STejun Heo 	 * PREEMPT too.
28675a8c820STejun Heo 	 */
2876ea3be36STejun Heo 	*enq_flags &= ~(SCX_ENQ_IMMED | SCX_ENQ_PREEMPT);
28875a8c820STejun Heo 	p->scx.flags &= ~SCX_TASK_IMMED;
28975a8c820STejun Heo 
29075a8c820STejun Heo 	return &rq->scx.reject_dsq;
29175a8c820STejun Heo }
29275a8c820STejun Heo 
29375a8c820STejun Heo /* @p lost the caps needed to stay on @rq's local DSQ? Record reason if so. */
29475a8c820STejun Heo bool scx_task_reenq_on_cap_revoke(struct rq *rq, struct task_struct *p)
29575a8c820STejun Heo {
29675a8c820STejun Heo 	u64 missing;
29775a8c820STejun Heo 
29875a8c820STejun Heo 	/* migration-disabled tasks are admitted regardless of caps */
29975a8c820STejun Heo 	if (is_migration_disabled(p))
30075a8c820STejun Heo 		return false;
30175a8c820STejun Heo 
30275a8c820STejun Heo 	missing = scx_missing_caps(scx_task_sched(p), cpu_of(rq), scx_caps_for_task(p));
30375a8c820STejun Heo 	if (likely(!missing))
30475a8c820STejun Heo 		return false;
30575a8c820STejun Heo 
30675a8c820STejun Heo 	p->scx.reenq_reason_caps = missing;
30775a8c820STejun Heo 	p->scx.reenq_reason_cid = __scx_cpu_to_cid(cpu_of(rq));
30875a8c820STejun Heo 	return true;
30975a8c820STejun Heo }
31075a8c820STejun Heo 
31175a8c820STejun Heo /*
31275a8c820STejun Heo  * Drain @rq->scx.reject_dsq, reenqueueing each task so the BPF re-decides
31375a8c820STejun Heo  * from p->scx.reenq_reason_*.
31475a8c820STejun Heo  *
31575a8c820STejun Heo  * A task can be re-rejected repeatedly, and there's no repeat limit here.
31675a8c820STejun Heo  * Rejection can't happen for root, and sub-scheds can be safely ejected after
31775a8c820STejun Heo  * triggering the stall watchdog.
31875a8c820STejun Heo  */
31975a8c820STejun Heo void scx_reenq_reject(struct rq *rq)
32075a8c820STejun Heo {
32175a8c820STejun Heo 	LIST_HEAD(tasks);
32275a8c820STejun Heo 	struct task_struct *p, *n;
32375a8c820STejun Heo 
32475a8c820STejun Heo 	lockdep_assert_rq_held(rq);
32575a8c820STejun Heo 
3268946dbd3STejun Heo 	if (!scx_has_subs() || list_empty(&rq->scx.reject_dsq.list))
32775a8c820STejun Heo 		return;
32875a8c820STejun Heo 
32975a8c820STejun Heo 	/*
33075a8c820STejun Heo 	 * Move to a private list so a task re-rejected by the
33175a8c820STejun Heo 	 * scx_do_enqueue_task() below isn't revisited this round.
33275a8c820STejun Heo 	 */
33375a8c820STejun Heo 	list_for_each_entry_safe(p, n, &rq->scx.reject_dsq.list, scx.dsq_list.node) {
33475a8c820STejun Heo 		/* migration_pending tasks should have bypassed to local DSQ */
33575a8c820STejun Heo 		if (WARN_ON_ONCE(p->migration_pending))
33675a8c820STejun Heo 			continue;
33775a8c820STejun Heo 
33875a8c820STejun Heo 		scx_dispatch_dequeue(rq, p);
33975a8c820STejun Heo 
34075a8c820STejun Heo 		if (WARN_ON_ONCE(p->scx.flags & SCX_TASK_REENQ_REASON_MASK))
34175a8c820STejun Heo 			p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK;
34275a8c820STejun Heo 		p->scx.flags |= SCX_TASK_REENQ_CAP;
34375a8c820STejun Heo 
34475a8c820STejun Heo 		list_add_tail(&p->scx.dsq_list.node, &tasks);
34575a8c820STejun Heo 	}
34675a8c820STejun Heo 
34775a8c820STejun Heo 	list_for_each_entry_safe(p, n, &tasks, scx.dsq_list.node) {
34875a8c820STejun Heo 		list_del_init(&p->scx.dsq_list.node);
34975a8c820STejun Heo 
35075a8c820STejun Heo 		scx_do_enqueue_task(rq, p, SCX_ENQ_REENQ, -1);
35175a8c820STejun Heo 
35275a8c820STejun Heo 		p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK;
35375a8c820STejun Heo 	}
35475a8c820STejun Heo }
35575a8c820STejun Heo 
3565f2a9a4cSTejun Heo /* record a caps change, see struct scx_caps_updated */
3575f2a9a4cSTejun Heo static void caps_updated_record(struct scx_pshard *ps, const struct scx_cmask *cids, u64 caps,
3585f2a9a4cSTejun Heo 				struct list_head *to_deliver)
3595f2a9a4cSTejun Heo {
3605f2a9a4cSTejun Heo 	struct scx_caps_updated *cu = &ps->caps_updated;
3615f2a9a4cSTejun Heo 
3625f2a9a4cSTejun Heo 	guard(raw_spinlock)(&cu->lock);
3635f2a9a4cSTejun Heo 	scx_cmask_or(&cu->cmask, cids);
3645f2a9a4cSTejun Heo 	cu->caps |= caps;
3655f2a9a4cSTejun Heo 	if (list_empty(&cu->node_in_flight))
3665f2a9a4cSTejun Heo 		list_add_tail(&cu->node_in_flight, to_deliver);
3675f2a9a4cSTejun Heo }
3685f2a9a4cSTejun Heo 
3695f2a9a4cSTejun Heo /* deliver queued caps_updated callbacks, see struct scx_caps_updated */
3705f2a9a4cSTejun Heo static void caps_updated_deliver(struct list_head *to_deliver)
3715f2a9a4cSTejun Heo {
3725f2a9a4cSTejun Heo 	struct scx_caps_updated *cu, *tmp;
3735f2a9a4cSTejun Heo 
3745f2a9a4cSTejun Heo 	list_for_each_entry_safe(cu, tmp, to_deliver, node_in_flight) {
3755f2a9a4cSTejun Heo 		struct scx_pshard *ps = container_of(cu, struct scx_pshard, caps_updated);
3765f2a9a4cSTejun Heo 		struct scx_sched *sch = ps->sch;
3775f2a9a4cSTejun Heo 
3785f2a9a4cSTejun Heo 		while (true) {
3795f2a9a4cSTejun Heo 			u64 caps = 0;
3805f2a9a4cSTejun Heo 
3815f2a9a4cSTejun Heo 			/*
3825f2a9a4cSTejun Heo 			 * During enable, has_op is set after ops.sub_attach(),
3835f2a9a4cSTejun Heo 			 * so !has_op means the op is absent or the sched isn't
3845f2a9a4cSTejun Heo 			 * live yet - e.g. caps grant from ops.sub_attach().
3855f2a9a4cSTejun Heo 			 * Either way don't consume - leave for
3865f2a9a4cSTejun Heo 			 * scx_sub_seed_caps() to deliver once live.
3875f2a9a4cSTejun Heo 			 */
3885f2a9a4cSTejun Heo 			scoped_guard (raw_spinlock, &cu->lock) {
3895f2a9a4cSTejun Heo 				if (cu->caps && SCX_HAS_OP(sch, sub_caps_updated) &&
3905f2a9a4cSTejun Heo 				    likely(!READ_ONCE(sch->aborting))) {
3915f2a9a4cSTejun Heo 					struct scx_cmask_ref ref;
3925f2a9a4cSTejun Heo 
3935f2a9a4cSTejun Heo 					caps = cu->caps;
3945f2a9a4cSTejun Heo 					scx_cmask_ref_init_kern(sch, cu->cmask_arena_out,
3955f2a9a4cSTejun Heo 								ps->base, ps->nr_cids, &ref);
3965f2a9a4cSTejun Heo 					scx_cmask_ref_copy(&ref, &cu->cmask);
3975f2a9a4cSTejun Heo 					scx_cmask_clear(&cu->cmask);
3985f2a9a4cSTejun Heo 					cu->caps = 0;
3995f2a9a4cSTejun Heo 				} else {
4005f2a9a4cSTejun Heo 					list_del_init(&cu->node_in_flight);
4015f2a9a4cSTejun Heo 				}
4025f2a9a4cSTejun Heo 			}
4035f2a9a4cSTejun Heo 			if (!caps)
4045f2a9a4cSTejun Heo 				break;
4055f2a9a4cSTejun Heo 
4065f2a9a4cSTejun Heo 			/* caps != 0 only when deliverable (has_op, above) */
4075f2a9a4cSTejun Heo 			SCX_CALL_OP(sch, sub_caps_updated, NULL,
4085f2a9a4cSTejun Heo 				    scx_kaddr_to_arena(sch, cu->cmask_arena_out),
4095f2a9a4cSTejun Heo 				    caps);
4105f2a9a4cSTejun Heo 		}
4115f2a9a4cSTejun Heo 	}
4125f2a9a4cSTejun Heo }
4135f2a9a4cSTejun Heo 
4145f2a9a4cSTejun Heo /*
4155f2a9a4cSTejun Heo  * Deliver caps owed to @sch that couldn't be delivered earlier (e.g. a grant
4165f2a9a4cSTejun Heo  * taken during its sub_attach(), before has_op was set). Called once @sch is
4175f2a9a4cSTejun Heo  * enabled.
4185f2a9a4cSTejun Heo  */
4195f2a9a4cSTejun Heo static void scx_sub_seed_caps(struct scx_sched *sch)
4205f2a9a4cSTejun Heo {
4215f2a9a4cSTejun Heo 	LIST_HEAD(to_deliver);
4225f2a9a4cSTejun Heo 	s32 si;
4235f2a9a4cSTejun Heo 
4245f2a9a4cSTejun Heo 	guard(irqsave)();
4255f2a9a4cSTejun Heo 
4265f2a9a4cSTejun Heo 	for (si = 0; si < sch->nr_pshards; si++) {
4275f2a9a4cSTejun Heo 		struct scx_pshard *ps = sch->pshard[si];
4285f2a9a4cSTejun Heo 		struct scx_caps_updated *cu = &ps->caps_updated;
4295f2a9a4cSTejun Heo 
4305f2a9a4cSTejun Heo 		scoped_guard (raw_spinlock, &cu->lock) {
4315f2a9a4cSTejun Heo 			if (cu->caps && list_empty(&cu->node_in_flight))
4325f2a9a4cSTejun Heo 				list_add_tail(&cu->node_in_flight, &to_deliver);
4335f2a9a4cSTejun Heo 		}
4345f2a9a4cSTejun Heo 	}
4355f2a9a4cSTejun Heo 	caps_updated_deliver(&to_deliver);
4365f2a9a4cSTejun Heo }
4375f2a9a4cSTejun Heo 
43856fdc35bSTejun Heo static u64 calc_effective_caps(struct scx_pshard *ps, s32 cid)
43956fdc35bSTejun Heo {
44056fdc35bSTejun Heo 	u64 ecaps = 0;
44156fdc35bSTejun Heo 	u32 cap_bit;
44256fdc35bSTejun Heo 
44356fdc35bSTejun Heo 	for (cap_bit = 0; cap_bit < __SCX_NR_CAPS; cap_bit++)
44456fdc35bSTejun Heo 		if (scx_cmask_test(cid, &ps->caps[cap_bit].cmask))
44556fdc35bSTejun Heo 			ecaps |= BIT_U64(cap_bit) | scx_caps_implied(BIT_U64(cap_bit));
44656fdc35bSTejun Heo 	return ecaps;
44756fdc35bSTejun Heo }
44856fdc35bSTejun Heo 
44956fdc35bSTejun Heo /**
45056fdc35bSTejun Heo  * queue_sync_ecaps - Queue ecaps update for a (sch, cid) pair
45156fdc35bSTejun Heo  * @sch: sched to update
45256fdc35bSTejun Heo  * @cid: cid to update
45356fdc35bSTejun Heo  *
45456fdc35bSTejun Heo  * Queue an ecaps update for @sch's @cid and kick the cpu so that it syncs in
45556fdc35bSTejun Heo  * balance_one().
45656fdc35bSTejun Heo  */
45756fdc35bSTejun Heo static void queue_sync_ecaps(struct scx_sched *sch, s32 cid)
45856fdc35bSTejun Heo {
45956fdc35bSTejun Heo 	s32 cpu = __scx_cid_to_cpu(cid);
46056fdc35bSTejun Heo 	struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu);
46156fdc35bSTejun Heo 
46256fdc35bSTejun Heo 	/*
46356fdc35bSTejun Heo 	 * Pairs with smp_mb() in scx_process_sync_ecaps(). Either the check
46456fdc35bSTejun Heo 	 * below sees the node off the list and queues it, or the in-flight sync
46556fdc35bSTejun Heo 	 * sees the caps[] update made before this call.
46656fdc35bSTejun Heo 	 */
46756fdc35bSTejun Heo 	smp_mb();
46856fdc35bSTejun Heo 
46956fdc35bSTejun Heo 	/* @cid's pshard->lock excludes concurrent queueing attempts */
47056fdc35bSTejun Heo 	if (llist_on_list(&pcpu->ecaps_to_sync_node))
47156fdc35bSTejun Heo 		return;
47256fdc35bSTejun Heo 	if (llist_add(&pcpu->ecaps_to_sync_node, &cpu_rq(cpu)->scx.ecaps_to_sync))
47356fdc35bSTejun Heo 		scx_kick_cpu(scx_root, cpu, 0);
47456fdc35bSTejun Heo }
47556fdc35bSTejun Heo 
47656fdc35bSTejun Heo /* discard @rq's queued ecaps syncs */
47756fdc35bSTejun Heo static void discard_queued_syncs(struct rq *rq)
47856fdc35bSTejun Heo {
47956fdc35bSTejun Heo 	struct llist_node *pos, *tmp;
48056fdc35bSTejun Heo 
48156fdc35bSTejun Heo 	lockdep_assert_rq_held(rq);
48256fdc35bSTejun Heo 
48356fdc35bSTejun Heo 	llist_for_each_safe(pos, tmp, llist_del_all(&rq->scx.ecaps_to_sync))
48456fdc35bSTejun Heo 		init_llist_node(pos);
48556fdc35bSTejun Heo }
48656fdc35bSTejun Heo 
48756fdc35bSTejun Heo /**
48856fdc35bSTejun Heo  * scx_process_sync_ecaps - Sync this cpu's ecaps to pshard->caps[]
48956fdc35bSTejun Heo  * @rq: the cid's cpu rq
490b81a6c01STejun Heo  * @prev: @rq's previous task from the in-progress balance
49156fdc35bSTejun Heo  *
49256fdc35bSTejun Heo  * pshard->caps[] is the target configuration. pcpu->ecaps is the effective
49356fdc35bSTejun Heo  * transposed copy owned by the cid's cpu and written only here under @rq's
49456fdc35bSTejun Heo  * lock.
495ca3aec45STejun Heo  *
496ca3aec45STejun Heo  * A sched that newly gains baseline access here is owed an update_idle() so it
497ca3aec45STejun Heo  * learns the cid's idle state. Such a gain arms the per-rq
498ca3aec45STejun Heo  * %SCX_RQ_SUB_IDLE_RENOTIFY gate so the next idle pick delivers it.
49956fdc35bSTejun Heo  */
500b81a6c01STejun Heo void scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev)
50156fdc35bSTejun Heo {
502b81a6c01STejun Heo 	s32 cpu = cpu_of(rq);
503b81a6c01STejun Heo 	s32 cid, shard;
50456fdc35bSTejun Heo 	struct llist_node *batch, *pos, *tmp;
50575a8c820STejun Heo 	u64 lost_all = 0;
50656fdc35bSTejun Heo 
50756fdc35bSTejun Heo 	lockdep_assert_rq_held(rq);
50856fdc35bSTejun Heo 
5098946dbd3STejun Heo 	if (!scx_has_subs() || likely(llist_empty(&rq->scx.ecaps_to_sync)))
51056fdc35bSTejun Heo 		return;
51156fdc35bSTejun Heo 
512b81a6c01STejun Heo 	/*
513b81a6c01STejun Heo 	 * ecaps are zeroed while the cpu is inactive and must stay zero.
514b81a6c01STejun Heo 	 * Discard queued syncs instead of processing them - the
515b81a6c01STejun Heo 	 * scx_online_ecaps() reseed re-syncs every sched on activation.
516b81a6c01STejun Heo 	 * cpu_active() clears before the offline zeroing and sets before the
517b81a6c01STejun Heo 	 * reseed is queued, so this test can neither miss a racing sync nor
518b81a6c01STejun Heo 	 * eat the reseed.
519b81a6c01STejun Heo 	 */
520b81a6c01STejun Heo 	if (unlikely(!cpu_active(cpu))) {
521b81a6c01STejun Heo 		discard_queued_syncs(rq);
522b81a6c01STejun Heo 		return;
523b81a6c01STejun Heo 	}
524b81a6c01STejun Heo 
525b81a6c01STejun Heo 	/* @cid is valid here: the cpu is active with queued syncs */
526b81a6c01STejun Heo 	cid = __scx_cpu_to_cid(cpu);
527b81a6c01STejun Heo 	shard = scx_cid_to_shard[cid];
528b81a6c01STejun Heo 
52956fdc35bSTejun Heo 	batch = llist_del_all(&rq->scx.ecaps_to_sync);
53056fdc35bSTejun Heo 	llist_for_each_safe(pos, tmp, batch) {
53156fdc35bSTejun Heo 		struct scx_sched_pcpu *pcpu =
53256fdc35bSTejun Heo 			container_of(pos, struct scx_sched_pcpu, ecaps_to_sync_node);
53356fdc35bSTejun Heo 		struct scx_pshard *ps = pcpu->sch->pshard[shard];
534ca3aec45STejun Heo 		u64 old, ecaps, lost, gained;
53556fdc35bSTejun Heo 
53656fdc35bSTejun Heo 		init_llist_node(pos);
53756fdc35bSTejun Heo 
53856fdc35bSTejun Heo 		/* pairs with smp_mb() in queue_sync_ecaps(), see there */
53956fdc35bSTejun Heo 		smp_mb();
54056fdc35bSTejun Heo 
54175a8c820STejun Heo 		old = READ_ONCE(pcpu->ecaps);
542b81a6c01STejun Heo 		ecaps = calc_effective_caps(ps, cid);
543b81a6c01STejun Heo 		WRITE_ONCE(pcpu->ecaps, ecaps);
544b81a6c01STejun Heo 
54575a8c820STejun Heo 		lost = old & ~ecaps;
546ca3aec45STejun Heo 		gained = ecaps & ~old;
54775a8c820STejun Heo 		lost_all |= lost;
54875a8c820STejun Heo 
54934e0fbfeSTejun Heo 		/*
55034e0fbfeSTejun Heo 		 * Tell the sched its effective caps on this cid changed. The
55134e0fbfeSTejun Heo 		 * invocation is equivalent to the dispatch path and may drop
55234e0fbfeSTejun Heo 		 * and re-acquire the rq lock temporarily while the rest of
55334e0fbfeSTejun Heo 		 * @batch is held privately, see scx_discard_ecaps_to_sync().
55434e0fbfeSTejun Heo 		 */
555b81a6c01STejun Heo 		if (ecaps != pcpu->reported_ecaps &&
556b81a6c01STejun Heo 		    SCX_HAS_OP(pcpu->sch, sub_ecaps_updated) &&
557b81a6c01STejun Heo 		    !scx_bypassing(pcpu->sch, cpu)) {
558b81a6c01STejun Heo 			struct scx_dsp_ctx *dspc = &pcpu->dsp_ctx;
559b81a6c01STejun Heo 
560b81a6c01STejun Heo 			dspc->rq = rq;
561b81a6c01STejun Heo 			/* stash @prev so nested dispatches can access it */
562b81a6c01STejun Heo 			rq->scx.sub_dispatch_prev = prev;
563b81a6c01STejun Heo 			SCX_CALL_OP(pcpu->sch, sub_ecaps_updated, rq, scx_cpu_arg(cpu),
564b81a6c01STejun Heo 				    pcpu->reported_ecaps, ecaps);
565b81a6c01STejun Heo 			rq->scx.sub_dispatch_prev = NULL;
566b81a6c01STejun Heo 			scx_flush_dispatch_buf(pcpu->sch, rq);
567b81a6c01STejun Heo 			pcpu->reported_ecaps = ecaps;
568b81a6c01STejun Heo 		}
569ca3aec45STejun Heo 
570ca3aec45STejun Heo 		/*
571ca3aec45STejun Heo 		 * Gaining baseline access owes an update_idle() so the sched
572ca3aec45STejun Heo 		 * learns the cpu's idle state. Arm the per-rq gate so the next
573ca3aec45STejun Heo 		 * idle pick flushes it. Losing access drops any pending notify.
574ca3aec45STejun Heo 		 */
575ca3aec45STejun Heo 		if (gained & SCX_CAP_BASE) {
576ca3aec45STejun Heo 			pcpu->idle_renotify = true;
577ca3aec45STejun Heo 			rq->scx.flags |= SCX_RQ_SUB_IDLE_RENOTIFY;
578ca3aec45STejun Heo 		} else if (lost & SCX_CAP_BASE) {
579ca3aec45STejun Heo 			pcpu->idle_renotify = false;
580ca3aec45STejun Heo 		}
581b81a6c01STejun Heo 	}
58275a8c820STejun Heo 
58375a8c820STejun Heo 	/*
58475a8c820STejun Heo 	 * Losing a cap can strand already-queued tasks. Schedule a reenq scan
58575a8c820STejun Heo 	 * to move the now-capless ones off the local DSQ. The scan tests
58675a8c820STejun Heo 	 * against the effective caps and thus must come after the ecaps sync.
58775a8c820STejun Heo 	 */
58875a8c820STejun Heo 	if (lost_all & SCX_CAPS_REENQ_ON_LOSS)
58975a8c820STejun Heo 		scx_schedule_reenq_local(rq, SCX_REENQ_CAP_REVOKE);
590b81a6c01STejun Heo }
591b81a6c01STejun Heo 
59275c268edSTejun Heo /**
59375c268edSTejun Heo  * scx_unbypass_replay_ecaps - Replay a bypass-suppressed ecaps notification
59475c268edSTejun Heo  * @rq: rq of the cpu leaving bypass
59575c268edSTejun Heo  * @sch: scheduler that just left bypass on @rq's cpu
59675c268edSTejun Heo  *
59775c268edSTejun Heo  * scx_process_sync_ecaps() consumes syncs while bypassing without delivering
59875c268edSTejun Heo  * ops.sub_ecaps_updated(), leaving reported_ecaps stale. Nothing re-queues a
59975c268edSTejun Heo  * sync when bypass lifts, so without a replay a cid that never changes again
60075c268edSTejun Heo  * would never be notified. The attach-time initial grants are the acute case
60175c268edSTejun Heo  * as they are consumed during the enable bypass window. Re-queue a sync for
60275c268edSTejun Heo  * any undelivered delta so the next balance delivers it.
60375c268edSTejun Heo  */
60475c268edSTejun Heo void scx_unbypass_replay_ecaps(struct rq *rq, struct scx_sched *sch)
60575c268edSTejun Heo {
60675c268edSTejun Heo 	s32 cpu = cpu_of(rq);
60775c268edSTejun Heo 	struct scx_sched_pcpu *pcpu = per_cpu_ptr(sch->pcpu, cpu);
60875c268edSTejun Heo 	struct scx_pshard *ps;
60975c268edSTejun Heo 	s32 cid;
61075c268edSTejun Heo 
61175c268edSTejun Heo 	lockdep_assert_rq_held(rq);
61275c268edSTejun Heo 
61375c268edSTejun Heo 	/* root holds every cap and never uses ecaps */
61475c268edSTejun Heo 	if (!sch->level)
61575c268edSTejun Heo 		return;
61675c268edSTejun Heo 
61775c268edSTejun Heo 	if (READ_ONCE(pcpu->ecaps) == pcpu->reported_ecaps)
61875c268edSTejun Heo 		return;
61975c268edSTejun Heo 
62075c268edSTejun Heo 	cid = __scx_cpu_to_cid(cpu);
62175c268edSTejun Heo 	ps = sch->pshard[scx_cid_to_shard[cid]];
62275c268edSTejun Heo 
62375c268edSTejun Heo 	guard(raw_spinlock)(&ps->lock);
62475c268edSTejun Heo 	queue_sync_ecaps(sch, cid);
62575c268edSTejun Heo }
62675c268edSTejun Heo 
627b81a6c01STejun Heo /*
628b81a6c01STejun Heo  * A cpu came back. Re-seed each sub-sched's ecaps on the cpu's cid. The sync
629b81a6c01STejun Heo  * recomputes effective caps from the pshard and fires ops.sub_ecaps_updated()
630b81a6c01STejun Heo  * only on a real change since offline.
631b81a6c01STejun Heo  */
632b81a6c01STejun Heo void scx_online_ecaps(struct rq *rq)
633b81a6c01STejun Heo {
634b81a6c01STejun Heo 	s32 cid = __scx_cpu_to_cid(cpu_of(rq));
635b81a6c01STejun Heo 	s32 shard = scx_cid_to_shard[cid];
636b81a6c01STejun Heo 	struct scx_sched *pos;
637b81a6c01STejun Heo 
638b81a6c01STejun Heo 	guard(rq_lock_irqsave)(rq);
639b81a6c01STejun Heo 
640b81a6c01STejun Heo 	scx_for_each_descendant_pre(pos, scx_root) {
641b81a6c01STejun Heo 		struct scx_pshard *ps;
642b81a6c01STejun Heo 
643b81a6c01STejun Heo 		/* root holds every cap and never uses ecaps */
644b81a6c01STejun Heo 		if (pos == scx_root)
645b81a6c01STejun Heo 			continue;
646b81a6c01STejun Heo 
647b81a6c01STejun Heo 		ps = pos->pshard[shard];
648b81a6c01STejun Heo 		guard(raw_spinlock)(&ps->lock);
649b81a6c01STejun Heo 		queue_sync_ecaps(pos, cid);
650b81a6c01STejun Heo 	}
651b81a6c01STejun Heo }
652b81a6c01STejun Heo 
653b81a6c01STejun Heo /*
654b81a6c01STejun Heo  * A cpu is going down. Zero each sub-sched's in-effect ecaps so cap checks
655b81a6c01STejun Heo  * treat the cpu as capless while offline. Pending and late-queued syncs are
656b81a6c01STejun Heo  * discarded at consumption by scx_process_sync_ecaps() while the cpu is
657b81a6c01STejun Heo  * inactive. Leave reported_ecaps. Ownership is unchanged, so the
658b81a6c01STejun Heo  * scx_online_ecaps() reseed reports only a genuine delta. No callback fires
659b81a6c01STejun Heo  * here.
660b81a6c01STejun Heo  */
661b81a6c01STejun Heo void scx_offline_ecaps(struct rq *rq)
662b81a6c01STejun Heo {
663b81a6c01STejun Heo 	s32 cpu = cpu_of(rq);
664b81a6c01STejun Heo 	struct scx_sched *pos;
665b81a6c01STejun Heo 
666b81a6c01STejun Heo 	guard(rq_lock_irqsave)(rq);
667b81a6c01STejun Heo 
668b81a6c01STejun Heo 	scx_for_each_descendant_pre(pos, scx_root) {
669b81a6c01STejun Heo 		/* root holds every cap and never uses ecaps */
670b81a6c01STejun Heo 		if (pos == scx_root)
671b81a6c01STejun Heo 			continue;
672b81a6c01STejun Heo 
673b81a6c01STejun Heo 		WRITE_ONCE(per_cpu_ptr(pos->pcpu, cpu)->ecaps, 0);
67456fdc35bSTejun Heo 	}
67556fdc35bSTejun Heo }
67656fdc35bSTejun Heo 
67756fdc35bSTejun Heo /*
67834e0fbfeSTejun Heo  * @pcpu's sched was unhashed before the grace period, so nothing re-queues its
67934e0fbfeSTejun Heo  * sync node. Remove the node from @rq's pending list so the pcpu can be freed.
68056fdc35bSTejun Heo  */
68156fdc35bSTejun Heo void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpu)
68256fdc35bSTejun Heo {
683b81a6c01STejun Heo 	struct rq *rq = cpu_rq(cpu);
68434e0fbfeSTejun Heo 	struct llist_node *head = NULL, *tail = NULL;
68534e0fbfeSTejun Heo 	struct llist_node *pos, *tmp;
68656fdc35bSTejun Heo 
68734e0fbfeSTejun Heo 	/*
68834e0fbfeSTejun Heo 	 * llist can't unlink a single node. Take all queued nodes, drop @pcpu's
68934e0fbfeSTejun Heo 	 * and resplice the rest. Nodes in the taken batch read as on-list
69034e0fbfeSTejun Heo 	 * throughout, so queue_sync_ecaps() stays correct.
69134e0fbfeSTejun Heo 	 */
69234e0fbfeSTejun Heo 	if (llist_on_list(&pcpu->ecaps_to_sync_node)) {
69334e0fbfeSTejun Heo 		scoped_guard (rq_lock_irqsave, rq) {
69434e0fbfeSTejun Heo 			llist_for_each_safe(pos, tmp, llist_del_all(&rq->scx.ecaps_to_sync)) {
69534e0fbfeSTejun Heo 				if (pos == &pcpu->ecaps_to_sync_node) {
69634e0fbfeSTejun Heo 					init_llist_node(pos);
69734e0fbfeSTejun Heo 				} else {
69834e0fbfeSTejun Heo 					pos->next = head;
69934e0fbfeSTejun Heo 					head = pos;
70034e0fbfeSTejun Heo 					if (!tail)
70134e0fbfeSTejun Heo 						tail = pos;
70234e0fbfeSTejun Heo 				}
70334e0fbfeSTejun Heo 			}
70434e0fbfeSTejun Heo 			if (head)
70534e0fbfeSTejun Heo 				llist_add_batch(head, tail, &rq->scx.ecaps_to_sync);
70634e0fbfeSTejun Heo 		}
70734e0fbfeSTejun Heo 	}
70834e0fbfeSTejun Heo 
70934e0fbfeSTejun Heo 	/*
71034e0fbfeSTejun Heo 	 * An in-flight scx_process_sync_ecaps() batch may still hold the node
71134e0fbfeSTejun Heo 	 * privately across dispatch-induced rq unlocks, reading as on-list.
71234e0fbfeSTejun Heo 	 *
71334e0fbfeSTejun Heo 	 * Because a bypassing sched gets no op call, init_llist_node() and all
71434e0fbfeSTejun Heo 	 * @pcpu accesses share one contiguous lock hold, off-list under the rq
71534e0fbfeSTejun Heo 	 * lock means @pcpu won't be accessed again.
71634e0fbfeSTejun Heo 	 */
717b81a6c01STejun Heo 	while (true) {
718b81a6c01STejun Heo 		scoped_guard (rq_lock_irqsave, rq) {
719b81a6c01STejun Heo 			if (!llist_on_list(&pcpu->ecaps_to_sync_node))
720b81a6c01STejun Heo 				return;
721b81a6c01STejun Heo 		}
72234e0fbfeSTejun Heo 		cpu_relax();
723b81a6c01STejun Heo 	}
72456fdc35bSTejun Heo }
72556fdc35bSTejun Heo 
72656fdc35bSTejun Heo /**
72756fdc35bSTejun Heo  * scx_discard_stale_ecaps_syncs - Discard ecaps syncs from earlier schedulers
72856fdc35bSTejun Heo  *
72956fdc35bSTejun Heo  * To be called during root enable before the scheduler goes live. An earlier
73056fdc35bSTejun Heo  * root's sub-sched may not have gone through its RCU free path yet (e.g. a
73156fdc35bSTejun Heo  * still-open link fd defers it) and can leave queued ecaps syncs behind.
73256fdc35bSTejun Heo  * Processing them would decode the dead sched's pshards with the current cid
73356fdc35bSTejun Heo  * layout. Discard them instead. The backing scx_sched_pcpu's are still
73434e0fbfeSTejun Heo  * allocated as the free path removes ecaps_to_sync_node before freeing.
73556fdc35bSTejun Heo  */
73656fdc35bSTejun Heo void scx_discard_stale_ecaps_syncs(void)
73756fdc35bSTejun Heo {
73856fdc35bSTejun Heo 	s32 cpu;
73956fdc35bSTejun Heo 
74056fdc35bSTejun Heo 	for_each_possible_cpu(cpu) {
74156fdc35bSTejun Heo 		struct rq *rq = cpu_rq(cpu);
74256fdc35bSTejun Heo 
74356fdc35bSTejun Heo 		guard(rq_lock_irqsave)(rq);
74456fdc35bSTejun Heo 		discard_queued_syncs(rq);
74556fdc35bSTejun Heo 	}
74656fdc35bSTejun Heo }
74756fdc35bSTejun Heo 
748daf8e166STejun Heo static DECLARE_WAIT_QUEUE_HEAD(scx_unlink_waitq);
749daf8e166STejun Heo 
750daf8e166STejun Heo void drain_descendants(struct scx_sched *sch)
751daf8e166STejun Heo {
752daf8e166STejun Heo 	/*
753daf8e166STejun Heo 	 * Child scheds that finished the critical part of disabling will take
754daf8e166STejun Heo 	 * themselves off @sch->children. Wait for it to drain. As propagation
755daf8e166STejun Heo 	 * is recursive, empty @sch->children means that all proper descendant
756daf8e166STejun Heo 	 * scheds reached unlinking stage.
757daf8e166STejun Heo 	 */
758daf8e166STejun Heo 	wait_event(scx_unlink_waitq, list_empty(&sch->children));
759daf8e166STejun Heo }
760daf8e166STejun Heo 
761*0dc90ce1STejun Heo /**
762*0dc90ce1STejun Heo  * scx_rehome_task - Move a task to a sched it has been initialized for
763*0dc90ce1STejun Heo  * @to: sched taking over @p, @p's init on it already complete
764*0dc90ce1STejun Heo  * @p: task to re-home
765*0dc90ce1STejun Heo  *
766*0dc90ce1STejun Heo  * Exit @p from its current sched and switch it over to @to, overriding the
767*0dc90ce1STejun Heo  * state to %SCX_TASK_READY to account for the already completed init. A task
768*0dc90ce1STejun Heo  * on a non-ext class, possible under an %SCX_OPS_SWITCH_PARTIAL root, stays
769*0dc90ce1STejun Heo  * %READY and is enabled by switching_to_scx() if it switches over.
770*0dc90ce1STejun Heo  */
771*0dc90ce1STejun Heo static void scx_rehome_task(struct scx_sched *to, struct task_struct *p)
772*0dc90ce1STejun Heo {
773*0dc90ce1STejun Heo 	lockdep_assert_held(&p->pi_lock);
774*0dc90ce1STejun Heo 	lockdep_assert_rq_held(task_rq(p));
775*0dc90ce1STejun Heo 
776*0dc90ce1STejun Heo 	scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
777*0dc90ce1STejun Heo 		scx_disable_and_exit_task(scx_task_sched(p), p);
778*0dc90ce1STejun Heo 		scx_set_task_state(p, SCX_TASK_INIT_BEGIN);
779*0dc90ce1STejun Heo 		scx_set_task_state(p, SCX_TASK_INIT);
780*0dc90ce1STejun Heo 		scx_set_task_sched(p, to);
781*0dc90ce1STejun Heo 		scx_set_task_state(p, SCX_TASK_READY);
782*0dc90ce1STejun Heo 		if (p->sched_class == &ext_sched_class)
783*0dc90ce1STejun Heo 			scx_enable_task(to, p);
784*0dc90ce1STejun Heo 	}
785*0dc90ce1STejun Heo }
786*0dc90ce1STejun Heo 
787*0dc90ce1STejun Heo /**
788*0dc90ce1STejun Heo  * scx_punt_task - Hand a task to a failed sched without initialization
789*0dc90ce1STejun Heo  * @to: failed and bypassed sched taking custody of @p
790*0dc90ce1STejun Heo  * @p: task to punt
791*0dc90ce1STejun Heo  *
792*0dc90ce1STejun Heo  * Take @p off its current sched and put it on @to at %SCX_TASK_NONE. @to is
793*0dc90ce1STejun Heo  * dying and its teardown will re-home @p properly.
794*0dc90ce1STejun Heo  *
795*0dc90ce1STejun Heo  * Used when @to must take over @p but failed to initialize it. Bypass keeps
796*0dc90ce1STejun Heo  * scheduling decisions away from @to but @p can still trigger its task ops,
797*0dc90ce1STejun Heo  * which may confuse the BPF side. @to is dying anyway. The exit paths skip
798*0dc90ce1STejun Heo  * %NONE tasks (see __scx_disable_and_exit_task() and switched_from_scx()).
799*0dc90ce1STejun Heo  */
800*0dc90ce1STejun Heo static void scx_punt_task(struct scx_sched *to, struct task_struct *p)
801*0dc90ce1STejun Heo {
802*0dc90ce1STejun Heo 	lockdep_assert_held(&p->pi_lock);
803*0dc90ce1STejun Heo 	lockdep_assert_rq_held(task_rq(p));
804*0dc90ce1STejun Heo 	WARN_ON_ONCE(!READ_ONCE(to->bypass_depth));
805*0dc90ce1STejun Heo 
806*0dc90ce1STejun Heo 	scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
807*0dc90ce1STejun Heo 		scx_disable_and_exit_task(scx_task_sched(p), p);
808*0dc90ce1STejun Heo 		scx_set_task_sched(p, to);
809*0dc90ce1STejun Heo 	}
810*0dc90ce1STejun Heo }
811*0dc90ce1STejun Heo 
812daf8e166STejun Heo static void scx_fail_parent(struct scx_sched *sch,
813daf8e166STejun Heo 			    struct task_struct *failed, s32 fail_code)
814daf8e166STejun Heo {
815daf8e166STejun Heo 	struct scx_sched *parent = scx_parent(sch);
816daf8e166STejun Heo 	struct scx_task_iter sti;
817daf8e166STejun Heo 	struct task_struct *p;
818daf8e166STejun Heo 
819daf8e166STejun Heo 	scx_error(parent, "ops.init_task() failed (%d) for %s[%d] while disabling a sub-scheduler",
820daf8e166STejun Heo 		  fail_code, failed->comm, failed->pid);
821daf8e166STejun Heo 
822daf8e166STejun Heo 	/*
823*0dc90ce1STejun Heo 	 * Once $parent is bypassed, tasks can be punted into it. This may
824*0dc90ce1STejun Heo 	 * cause downstream failures on the BPF side but $parent is dying
825*0dc90ce1STejun Heo 	 * anyway.
826daf8e166STejun Heo 	 */
827daf8e166STejun Heo 	scx_bypass(parent, true);
828daf8e166STejun Heo 
829daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
830daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
831daf8e166STejun Heo 		if (scx_task_on_sched(parent, p))
832daf8e166STejun Heo 			continue;
833daf8e166STejun Heo 
834*0dc90ce1STejun Heo 		scx_punt_task(parent, p);
835daf8e166STejun Heo 	}
836daf8e166STejun Heo 	scx_task_iter_stop(&sti);
837daf8e166STejun Heo }
838daf8e166STejun Heo 
839daf8e166STejun Heo void scx_sub_disable(struct scx_sched *sch)
840daf8e166STejun Heo {
841daf8e166STejun Heo 	struct scx_sched *parent = scx_parent(sch);
842daf8e166STejun Heo 	struct scx_task_iter sti;
843daf8e166STejun Heo 	struct task_struct *p;
844daf8e166STejun Heo 	int ret;
845daf8e166STejun Heo 
846daf8e166STejun Heo 	/*
847daf8e166STejun Heo 	 * Guarantee forward progress and wait for descendants to be disabled.
848daf8e166STejun Heo 	 * To limit disruptions, $parent is not bypassed. Tasks are fully
849daf8e166STejun Heo 	 * prepped and then inserted back into $parent.
850daf8e166STejun Heo 	 */
851daf8e166STejun Heo 	scx_bypass(sch, true);
852daf8e166STejun Heo 	drain_descendants(sch);
853daf8e166STejun Heo 
854daf8e166STejun Heo 	/*
855daf8e166STejun Heo 	 * Here, every runnable task is guaranteed to make forward progress and
856daf8e166STejun Heo 	 * we can safely use blocking synchronization constructs. Actually
857daf8e166STejun Heo 	 * disable ops.
858daf8e166STejun Heo 	 */
859daf8e166STejun Heo 	mutex_lock(&scx_enable_mutex);
860daf8e166STejun Heo 	percpu_down_write(&scx_fork_rwsem);
861daf8e166STejun Heo 	scx_cgroup_lock();
862daf8e166STejun Heo 
8637c2cd767STejun Heo 	/*
8647c2cd767STejun Heo 	 * An enable that failed before scx_link_sched() never owned a cgroup or
8657c2cd767STejun Heo 	 * task and won't be waited on by an ancestor's drain_descendants().
8667c2cd767STejun Heo 	 * Nothing to reparent and walking the tasks can misbehave as the task
8677c2cd767STejun Heo 	 * ownership invariant (either owned by self or parent) does not hold.
8687c2cd767STejun Heo 	 */
8697c2cd767STejun Heo 	if (list_empty(&sch->sibling))
8707c2cd767STejun Heo 		goto dump;
8717c2cd767STejun Heo 
872daf8e166STejun Heo 	set_cgroup_sched(sch_cgroup(sch), parent);
873daf8e166STejun Heo 
874daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
875daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
876daf8e166STejun Heo 		struct rq *rq;
877daf8e166STejun Heo 		struct rq_flags rf;
878daf8e166STejun Heo 
879daf8e166STejun Heo 		/* filter out duplicate visits */
880daf8e166STejun Heo 		if (scx_task_on_sched(parent, p))
881daf8e166STejun Heo 			continue;
882daf8e166STejun Heo 
883daf8e166STejun Heo 		/*
8847c2cd767STejun Heo 		 * By the time control reaches here, all linked descendant
8857c2cd767STejun Heo 		 * schedulers should have been disabled.
886daf8e166STejun Heo 		 */
887daf8e166STejun Heo 		WARN_ON_ONCE(!scx_task_on_sched(sch, p));
888daf8e166STejun Heo 
889daf8e166STejun Heo 		/*
890daf8e166STejun Heo 		 * @p is pinned by the iter: css_task_iter_next() takes a
891daf8e166STejun Heo 		 * reference and holds it until the next iter_next() call, so
892daf8e166STejun Heo 		 * @p->usage is guaranteed > 0.
893daf8e166STejun Heo 		 */
894daf8e166STejun Heo 		get_task_struct(p);
895daf8e166STejun Heo 
896daf8e166STejun Heo 		scx_task_iter_unlock(&sti);
897daf8e166STejun Heo 
898daf8e166STejun Heo 		/*
899daf8e166STejun Heo 		 * $p is READY or ENABLED on @sch. Initialize for $parent,
900daf8e166STejun Heo 		 * disable and exit from @sch, and then switch over to $parent.
901daf8e166STejun Heo 		 *
902daf8e166STejun Heo 		 * If a task fails to initialize for $parent, the only available
903daf8e166STejun Heo 		 * action is disabling $parent too. While this allows disabling
904daf8e166STejun Heo 		 * of a child sched to cause the parent scheduler to fail, the
905daf8e166STejun Heo 		 * failure can only originate from ops.init_task() of the
906daf8e166STejun Heo 		 * parent. A child can't directly affect the parent through its
907daf8e166STejun Heo 		 * own failures.
908daf8e166STejun Heo 		 */
909daf8e166STejun Heo 		ret = __scx_init_task(parent, p, false);
910daf8e166STejun Heo 		if (ret) {
911daf8e166STejun Heo 			scx_fail_parent(sch, p, ret);
912daf8e166STejun Heo 			put_task_struct(p);
913daf8e166STejun Heo 			break;
914daf8e166STejun Heo 		}
915daf8e166STejun Heo 
916daf8e166STejun Heo 		rq = task_rq_lock(p, &rf);
917daf8e166STejun Heo 
918daf8e166STejun Heo 		if (scx_get_task_state(p) == SCX_TASK_DEAD) {
919daf8e166STejun Heo 			/*
920daf8e166STejun Heo 			 * sched_ext_dead() raced us between __scx_init_task()
921daf8e166STejun Heo 			 * and this rq lock and ran exit_task() on @sch (the
922daf8e166STejun Heo 			 * sched @p was on at that point), not on $parent.
923daf8e166STejun Heo 			 * $parent's just-completed init is owed an exit_task()
924daf8e166STejun Heo 			 * and we issue it here.
925daf8e166STejun Heo 			 */
926daf8e166STejun Heo 			scx_sub_init_cancel_task(parent, p);
927daf8e166STejun Heo 			task_rq_unlock(rq, p, &rf);
928daf8e166STejun Heo 			put_task_struct(p);
929daf8e166STejun Heo 			continue;
930daf8e166STejun Heo 		}
931daf8e166STejun Heo 
932*0dc90ce1STejun Heo 		scx_rehome_task(parent, p);
933daf8e166STejun Heo 
934daf8e166STejun Heo 		task_rq_unlock(rq, p, &rf);
935daf8e166STejun Heo 		put_task_struct(p);
936daf8e166STejun Heo 	}
937daf8e166STejun Heo 	scx_task_iter_stop(&sti);
938daf8e166STejun Heo 
9397c2cd767STejun Heo dump:
940daf8e166STejun Heo 	scx_disable_dump(sch);
941daf8e166STejun Heo 
942daf8e166STejun Heo 	scx_cgroup_unlock();
943daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
944daf8e166STejun Heo 
945daf8e166STejun Heo 	/*
946daf8e166STejun Heo 	 * All tasks are moved off of @sch but there may still be on-going
947daf8e166STejun Heo 	 * operations (e.g. ops.select_cpu()). Drain them by flushing RCU. Use
948daf8e166STejun Heo 	 * the expedited version as ancestors may be waiting in bypass mode.
949daf8e166STejun Heo 	 * Also, tell the parent that there is no need to keep running bypass
950daf8e166STejun Heo 	 * DSQs for us.
951daf8e166STejun Heo 	 */
952daf8e166STejun Heo 	synchronize_rcu_expedited();
953daf8e166STejun Heo 	scx_disable_bypass_dsp(sch);
954daf8e166STejun Heo 
955daf8e166STejun Heo 	scx_unlink_sched(sch);
956daf8e166STejun Heo 
957daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
958daf8e166STejun Heo 
959daf8e166STejun Heo 	/*
960daf8e166STejun Heo 	 * @sch is now unlinked from the parent's children list. Notify and call
961daf8e166STejun Heo 	 * ops.sub_detach/exit(). Note that ops.sub_detach/exit() must be called
962daf8e166STejun Heo 	 * after unlinking and releasing all locks. See scx_claim_exit().
963daf8e166STejun Heo 	 */
964daf8e166STejun Heo 	wake_up_all(&scx_unlink_waitq);
965daf8e166STejun Heo 
966daf8e166STejun Heo 	if (parent->ops.sub_detach && sch->sub_attached) {
967daf8e166STejun Heo 		struct scx_sub_detach_args sub_detach_args = {
968daf8e166STejun Heo 			.ops = &sch->ops,
969daf8e166STejun Heo 			.cgroup_path = sch->cgrp_path,
970daf8e166STejun Heo 		};
971daf8e166STejun Heo 		SCX_CALL_OP(parent, sub_detach, NULL,
972daf8e166STejun Heo 			    &sub_detach_args);
973daf8e166STejun Heo 	}
974daf8e166STejun Heo 
975daf8e166STejun Heo 	scx_log_sched_disable(sch);
976daf8e166STejun Heo 
977daf8e166STejun Heo 	if (sch->ops.exit)
978daf8e166STejun Heo 		SCX_CALL_OP(sch, exit, NULL, sch->exit_info);
97981507f14STejun Heo 
98081507f14STejun Heo 	/*
98181507f14STejun Heo 	 * @sch's non-ops programs such as timers and tracers can fire after
98281507f14STejun Heo 	 * ops.exit(). Now that exit is complete, stop scx_prog_sched() from
98381507f14STejun Heo 	 * resolving to @sch and drain in-flight resolvers.
98481507f14STejun Heo 	 */
98581507f14STejun Heo 	WRITE_ONCE(sch->dead, true);
98681507f14STejun Heo 	synchronize_rcu();
98781507f14STejun Heo 
988daf8e166STejun Heo 	if (sch->sub_kset)
989daf8e166STejun Heo 		kobject_del(&sch->sub_kset->kobj);
99080e6adaaSTejun Heo 	/* not added if enable failed before scx_sched_sysfs_add() */
99180e6adaaSTejun Heo 	if (sch->kobj.state_in_sysfs)
992daf8e166STejun Heo 		kobject_del(&sch->kobj);
993daf8e166STejun Heo }
994daf8e166STejun Heo 
995daf8e166STejun Heo /* verify that a scheduler can be attached to @cgrp and return the parent */
996daf8e166STejun Heo static struct scx_sched *find_parent_sched(struct cgroup *cgrp)
997daf8e166STejun Heo {
998daf8e166STejun Heo 	struct scx_sched *parent = cgrp->scx_sched;
999daf8e166STejun Heo 	struct scx_sched *pos;
1000daf8e166STejun Heo 
1001daf8e166STejun Heo 	lockdep_assert_held(&scx_sched_lock);
1002daf8e166STejun Heo 
1003daf8e166STejun Heo 	/* can't attach twice to the same cgroup */
1004daf8e166STejun Heo 	if (parent->cgrp == cgrp)
1005daf8e166STejun Heo 		return ERR_PTR(-EBUSY);
1006daf8e166STejun Heo 
1007daf8e166STejun Heo 	/* does $parent allow sub-scheds? */
1008daf8e166STejun Heo 	if (!parent->ops.sub_attach)
1009daf8e166STejun Heo 		return ERR_PTR(-EOPNOTSUPP);
1010daf8e166STejun Heo 
1011daf8e166STejun Heo 	/* can't insert between $parent and its exiting children */
1012daf8e166STejun Heo 	list_for_each_entry(pos, &parent->children, sibling)
1013daf8e166STejun Heo 		if (cgroup_is_descendant(pos->cgrp, cgrp))
1014daf8e166STejun Heo 			return ERR_PTR(-EBUSY);
1015daf8e166STejun Heo 
1016daf8e166STejun Heo 	return parent;
1017daf8e166STejun Heo }
1018daf8e166STejun Heo 
1019daf8e166STejun Heo static bool assert_task_ready_or_enabled(struct task_struct *p)
1020daf8e166STejun Heo {
1021daf8e166STejun Heo 	u32 state = scx_get_task_state(p);
1022daf8e166STejun Heo 
1023daf8e166STejun Heo 	switch (state) {
1024daf8e166STejun Heo 	case SCX_TASK_READY:
1025daf8e166STejun Heo 	case SCX_TASK_ENABLED:
1026daf8e166STejun Heo 		return true;
1027daf8e166STejun Heo 	default:
1028daf8e166STejun Heo 		WARN_ONCE(true, "sched_ext: Invalid task state %d for %s[%d] during enabling sub sched",
1029daf8e166STejun Heo 			  state, p->comm, p->pid);
1030daf8e166STejun Heo 		return false;
1031daf8e166STejun Heo 	}
1032daf8e166STejun Heo }
1033daf8e166STejun Heo 
1034daf8e166STejun Heo void scx_sub_enable_workfn(struct kthread_work *work)
1035daf8e166STejun Heo {
1036daf8e166STejun Heo 	struct scx_enable_cmd *cmd = container_of(work, struct scx_enable_cmd, work);
1037daf8e166STejun Heo 	struct sched_ext_ops *ops = cmd->ops;
1038daf8e166STejun Heo 	struct cgroup *cgrp;
1039daf8e166STejun Heo 	struct scx_sched *parent, *sch;
1040daf8e166STejun Heo 	struct scx_task_iter sti;
1041daf8e166STejun Heo 	struct task_struct *p;
1042daf8e166STejun Heo 	s32 i, ret;
1043daf8e166STejun Heo 
1044daf8e166STejun Heo 	mutex_lock(&scx_enable_mutex);
1045daf8e166STejun Heo 
1046daf8e166STejun Heo 	if (!scx_enabled()) {
1047daf8e166STejun Heo 		ret = -ENODEV;
1048daf8e166STejun Heo 		goto out_unlock;
1049daf8e166STejun Heo 	}
1050daf8e166STejun Heo 
1051daf8e166STejun Heo 	/* See scx_root_enable_workfn() for the @ops->priv check. */
1052daf8e166STejun Heo 	if (rcu_access_pointer(ops->priv)) {
1053daf8e166STejun Heo 		ret = -EBUSY;
1054daf8e166STejun Heo 		goto out_unlock;
1055daf8e166STejun Heo 	}
1056daf8e166STejun Heo 
1057daf8e166STejun Heo 	cgrp = cgroup_get_from_id(ops->sub_cgroup_id);
1058daf8e166STejun Heo 	if (IS_ERR(cgrp)) {
1059daf8e166STejun Heo 		ret = PTR_ERR(cgrp);
1060daf8e166STejun Heo 		goto out_unlock;
1061daf8e166STejun Heo 	}
1062daf8e166STejun Heo 
1063daf8e166STejun Heo 	raw_spin_lock_irq(&scx_sched_lock);
1064daf8e166STejun Heo 	parent = find_parent_sched(cgrp);
1065daf8e166STejun Heo 	if (IS_ERR(parent)) {
1066daf8e166STejun Heo 		raw_spin_unlock_irq(&scx_sched_lock);
1067daf8e166STejun Heo 		ret = PTR_ERR(parent);
1068daf8e166STejun Heo 		goto out_put_cgrp;
1069daf8e166STejun Heo 	}
1070daf8e166STejun Heo 	kobject_get(&parent->kobj);
1071daf8e166STejun Heo 	raw_spin_unlock_irq(&scx_sched_lock);
1072daf8e166STejun Heo 
10738946dbd3STejun Heo 	/*
10748946dbd3STejun Heo 	 * Flip the hot-path gates before ops->priv is published - the sub's
10758946dbd3STejun Heo 	 * programs can e.g. kick cpus from that point on. The matching dec is
10768946dbd3STejun Heo 	 * at the end of scx_sched_free_rcu_work().
10778946dbd3STejun Heo 	 */
10788946dbd3STejun Heo 	static_branch_inc(&__scx_has_subs);
10798946dbd3STejun Heo 
1080daf8e166STejun Heo 	/* scx_alloc_and_add_sched() consumes @cgrp whether it succeeds or not */
1081daf8e166STejun Heo 	sch = scx_alloc_and_add_sched(cmd, cgrp, parent);
1082daf8e166STejun Heo 	kobject_put(&parent->kobj);
1083daf8e166STejun Heo 	if (IS_ERR(sch)) {
10848946dbd3STejun Heo 		static_branch_dec(&__scx_has_subs);
1085daf8e166STejun Heo 		ret = PTR_ERR(sch);
1086daf8e166STejun Heo 		goto out_unlock;
1087daf8e166STejun Heo 	}
1088daf8e166STejun Heo 
108986094b95STejun Heo 	/*
109086094b95STejun Heo 	 * Validate before scx_link_sched() publishes @sch, so an invalid sub
109186094b95STejun Heo 	 * never becomes visible with an unallocated pshard.
109286094b95STejun Heo 	 */
109386094b95STejun Heo 	ret = scx_validate_ops(sch, ops);
109486094b95STejun Heo 	if (ret)
109586094b95STejun Heo 		goto err_disable;
109686094b95STejun Heo 
109786094b95STejun Heo 	/*
109886094b95STejun Heo 	 * Allocate pshard[] before scx_link_sched() publishes @sch into the
109986094b95STejun Heo 	 * parent's RCU children list. A concurrent revoke walking the tree
110086094b95STejun Heo 	 * would otherwise dereference sch->pshard[si] while it's still NULL.
110186094b95STejun Heo 	 * Unlike the root path, the cid shard layout is stable at this point.
110286094b95STejun Heo 	 *
110386094b95STejun Heo 	 * scx_alloc_pshards() skips allocation when @sch's arena pool isn't
110486094b95STejun Heo 	 * initialized, so scx_arena_pool_init() must run first.
110586094b95STejun Heo 	 */
110686094b95STejun Heo 	ret = scx_arena_pool_init(sch);
110786094b95STejun Heo 	if (ret)
110886094b95STejun Heo 		goto err_disable;
110986094b95STejun Heo 
111086094b95STejun Heo 	ret = scx_alloc_pshards(sch);
111186094b95STejun Heo 	if (ret)
111286094b95STejun Heo 		goto err_disable;
111386094b95STejun Heo 
1114daf8e166STejun Heo 	ret = scx_link_sched(sch);
1115daf8e166STejun Heo 	if (ret)
1116daf8e166STejun Heo 		goto err_disable;
1117daf8e166STejun Heo 
111880e6adaaSTejun Heo 	ret = scx_sched_sysfs_add(sch);
111980e6adaaSTejun Heo 	if (ret)
112080e6adaaSTejun Heo 		goto err_disable;
112180e6adaaSTejun Heo 
1122daf8e166STejun Heo 	if (sch->level >= SCX_SUB_MAX_DEPTH) {
1123daf8e166STejun Heo 		scx_error(sch, "max nesting depth %d violated",
1124daf8e166STejun Heo 			  SCX_SUB_MAX_DEPTH);
1125daf8e166STejun Heo 		goto err_disable;
1126daf8e166STejun Heo 	}
1127daf8e166STejun Heo 
1128daf8e166STejun Heo 	if (sch->ops.init) {
1129daf8e166STejun Heo 		ret = SCX_CALL_OP_RET(sch, init, NULL);
1130daf8e166STejun Heo 		if (ret) {
1131daf8e166STejun Heo 			ret = scx_ops_sanitize_err(sch, "init", ret);
1132daf8e166STejun Heo 			scx_error(sch, "ops.init() failed (%d)", ret);
1133daf8e166STejun Heo 			goto err_disable;
1134daf8e166STejun Heo 		}
1135daf8e166STejun Heo 		sch->exit_info->flags |= SCX_EFLAG_INITIALIZED;
1136daf8e166STejun Heo 	}
1137daf8e166STejun Heo 
1138daf8e166STejun Heo 	ret = scx_set_cmask_scratch_alloc(sch);
1139daf8e166STejun Heo 	if (ret)
1140daf8e166STejun Heo 		goto err_disable;
1141daf8e166STejun Heo 
1142daf8e166STejun Heo 	struct scx_sub_attach_args sub_attach_args = {
1143daf8e166STejun Heo 		.ops = &sch->ops,
1144daf8e166STejun Heo 		.cgroup_path = sch->cgrp_path,
1145daf8e166STejun Heo 	};
1146daf8e166STejun Heo 
1147daf8e166STejun Heo 	ret = SCX_CALL_OP_RET(parent, sub_attach, NULL,
1148daf8e166STejun Heo 			      &sub_attach_args);
1149daf8e166STejun Heo 	if (ret) {
1150daf8e166STejun Heo 		ret = scx_ops_sanitize_err(sch, "sub_attach", ret);
1151daf8e166STejun Heo 		scx_error(sch, "parent rejected (%d)", ret);
1152daf8e166STejun Heo 		goto err_disable;
1153daf8e166STejun Heo 	}
1154daf8e166STejun Heo 	sch->sub_attached = true;
1155daf8e166STejun Heo 
1156daf8e166STejun Heo 	scx_bypass(sch, true);
1157daf8e166STejun Heo 
1158daf8e166STejun Heo 	for (i = SCX_OPI_BEGIN; i < SCX_OPI_END; i++)
1159daf8e166STejun Heo 		if (((void (**)(void))ops)[i])
1160daf8e166STejun Heo 			set_bit(i, sch->has_op);
1161daf8e166STejun Heo 
1162daf8e166STejun Heo 	percpu_down_write(&scx_fork_rwsem);
1163daf8e166STejun Heo 	scx_cgroup_lock();
1164daf8e166STejun Heo 
1165daf8e166STejun Heo 	/*
1166daf8e166STejun Heo 	 * Set cgroup->scx_sched's and check CSS_ONLINE. Either we see
1167daf8e166STejun Heo 	 * !CSS_ONLINE or scx_cgroup_lifetime_notify() sees and shoots us down.
1168daf8e166STejun Heo 	 */
1169daf8e166STejun Heo 	set_cgroup_sched(sch_cgroup(sch), sch);
1170daf8e166STejun Heo 	if (!(cgrp->self.flags & CSS_ONLINE)) {
1171daf8e166STejun Heo 		scx_error(sch, "cgroup is not online");
1172daf8e166STejun Heo 		goto err_unlock_and_disable;
1173daf8e166STejun Heo 	}
1174daf8e166STejun Heo 
1175daf8e166STejun Heo 	/*
1176daf8e166STejun Heo 	 * Initialize tasks for the new child $sch without exiting them for
1177daf8e166STejun Heo 	 * $parent so that the tasks can always be reverted back to $parent
1178daf8e166STejun Heo 	 * sched on child init failure.
1179daf8e166STejun Heo 	 */
1180daf8e166STejun Heo 	WARN_ON_ONCE(scx_enabling_sub_sched);
1181daf8e166STejun Heo 	scx_enabling_sub_sched = sch;
1182daf8e166STejun Heo 
1183daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
1184daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
1185daf8e166STejun Heo 		struct rq *rq;
1186daf8e166STejun Heo 		struct rq_flags rf;
1187daf8e166STejun Heo 
1188daf8e166STejun Heo 		/*
1189daf8e166STejun Heo 		 * Task iteration may visit the same task twice when racing
1190daf8e166STejun Heo 		 * against exiting. Use %SCX_TASK_SUB_INIT to mark tasks which
1191daf8e166STejun Heo 		 * finished __scx_init_task() and skip if set.
1192daf8e166STejun Heo 		 *
1193daf8e166STejun Heo 		 * A task may exit and get freed between __scx_init_task()
1194daf8e166STejun Heo 		 * completion and scx_enable_task(). In such cases,
1195daf8e166STejun Heo 		 * scx_disable_and_exit_task() must exit the task for both the
1196daf8e166STejun Heo 		 * parent and child scheds.
1197daf8e166STejun Heo 		 */
1198daf8e166STejun Heo 		if (p->scx.flags & SCX_TASK_SUB_INIT)
1199daf8e166STejun Heo 			continue;
1200daf8e166STejun Heo 
1201daf8e166STejun Heo 		/* @p is pinned by the iter; see scx_sub_disable() */
1202daf8e166STejun Heo 		get_task_struct(p);
1203daf8e166STejun Heo 
1204daf8e166STejun Heo 		if (!assert_task_ready_or_enabled(p)) {
1205daf8e166STejun Heo 			ret = -EINVAL;
1206daf8e166STejun Heo 			goto abort;
1207daf8e166STejun Heo 		}
1208daf8e166STejun Heo 
1209daf8e166STejun Heo 		scx_task_iter_unlock(&sti);
1210daf8e166STejun Heo 
1211daf8e166STejun Heo 		/*
1212daf8e166STejun Heo 		 * As $p is still on $parent, it can't be transitioned to INIT.
1213daf8e166STejun Heo 		 * Let's worry about task state later. Use __scx_init_task().
1214daf8e166STejun Heo 		 */
1215daf8e166STejun Heo 		ret = __scx_init_task(sch, p, false);
1216daf8e166STejun Heo 		if (ret)
1217daf8e166STejun Heo 			goto abort;
1218daf8e166STejun Heo 
1219daf8e166STejun Heo 		rq = task_rq_lock(p, &rf);
1220daf8e166STejun Heo 
1221daf8e166STejun Heo 		if (scx_get_task_state(p) == SCX_TASK_DEAD) {
1222daf8e166STejun Heo 			/*
1223daf8e166STejun Heo 			 * sched_ext_dead() raced us between __scx_init_task()
1224daf8e166STejun Heo 			 * and this rq lock and ran exit_task() on $parent (the
1225daf8e166STejun Heo 			 * sched @p was on at that point), not on @sch. @sch's
1226daf8e166STejun Heo 			 * just-completed init is owed an exit_task() and we
1227daf8e166STejun Heo 			 * issue it here.
1228daf8e166STejun Heo 			 */
1229daf8e166STejun Heo 			scx_sub_init_cancel_task(sch, p);
1230daf8e166STejun Heo 			task_rq_unlock(rq, p, &rf);
1231daf8e166STejun Heo 			put_task_struct(p);
1232daf8e166STejun Heo 			continue;
1233daf8e166STejun Heo 		}
1234daf8e166STejun Heo 
1235daf8e166STejun Heo 		p->scx.flags |= SCX_TASK_SUB_INIT;
1236daf8e166STejun Heo 		task_rq_unlock(rq, p, &rf);
1237daf8e166STejun Heo 
1238daf8e166STejun Heo 		put_task_struct(p);
1239daf8e166STejun Heo 	}
1240daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1241daf8e166STejun Heo 
1242daf8e166STejun Heo 	/*
1243daf8e166STejun Heo 	 * All tasks are prepped. Disable/exit tasks for $parent and enable for
1244daf8e166STejun Heo 	 * the new @sch.
1245daf8e166STejun Heo 	 */
1246daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
1247daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
1248daf8e166STejun Heo 		/*
1249daf8e166STejun Heo 		 * Use clearing of %SCX_TASK_SUB_INIT to detect and skip
1250daf8e166STejun Heo 		 * duplicate iterations.
1251daf8e166STejun Heo 		 */
1252daf8e166STejun Heo 		if (!(p->scx.flags & SCX_TASK_SUB_INIT))
1253daf8e166STejun Heo 			continue;
1254daf8e166STejun Heo 
1255daf8e166STejun Heo 		scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
1256daf8e166STejun Heo 			/*
1257daf8e166STejun Heo 			 * $p must be either READY or ENABLED. If ENABLED,
1258daf8e166STejun Heo 			 * __scx_disabled_and_exit_task() first disables and
1259daf8e166STejun Heo 			 * makes it READY. However, after exiting $p, it will
1260daf8e166STejun Heo 			 * leave $p as READY.
1261daf8e166STejun Heo 			 */
1262daf8e166STejun Heo 			assert_task_ready_or_enabled(p);
1263daf8e166STejun Heo 			__scx_disable_and_exit_task(parent, p);
1264daf8e166STejun Heo 
1265daf8e166STejun Heo 			/*
1266daf8e166STejun Heo 			 * $p is now only initialized for @sch and READY, which
12677c2cd767STejun Heo 			 * is what we want. Assign it to @sch and, if it's on
12687c2cd767STejun Heo 			 * the ext class, enable. A non-ext task, possible under
12697c2cd767STejun Heo 			 * an %SCX_OPS_SWITCH_PARTIAL root, stays READY and is
12707c2cd767STejun Heo 			 * enabled by switching_to_scx() if it switches over.
1271daf8e166STejun Heo 			 */
1272daf8e166STejun Heo 			scx_set_task_sched(p, sch);
12737c2cd767STejun Heo 			if (p->sched_class == &ext_sched_class)
1274daf8e166STejun Heo 				scx_enable_task(sch, p);
1275daf8e166STejun Heo 
1276daf8e166STejun Heo 			p->scx.flags &= ~SCX_TASK_SUB_INIT;
1277daf8e166STejun Heo 		}
1278daf8e166STejun Heo 	}
1279daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1280daf8e166STejun Heo 
1281daf8e166STejun Heo 	scx_enabling_sub_sched = NULL;
1282daf8e166STejun Heo 
1283daf8e166STejun Heo 	scx_cgroup_unlock();
1284daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
1285daf8e166STejun Heo 
1286daf8e166STejun Heo 	scx_bypass(sch, false);
1287daf8e166STejun Heo 
12885f2a9a4cSTejun Heo 	/* @sch is enabled; deliver any caps owed since its sub_attach() */
12895f2a9a4cSTejun Heo 	scx_sub_seed_caps(sch);
12905f2a9a4cSTejun Heo 
1291daf8e166STejun Heo 	pr_info("sched_ext: BPF sub-scheduler \"%s\" enabled\n", sch->ops.name);
1292daf8e166STejun Heo 	kobject_uevent(&sch->kobj, KOBJ_ADD);
1293daf8e166STejun Heo 	ret = 0;
1294daf8e166STejun Heo 	goto out_unlock;
1295daf8e166STejun Heo 
1296daf8e166STejun Heo out_put_cgrp:
1297daf8e166STejun Heo 	cgroup_put(cgrp);
1298daf8e166STejun Heo out_unlock:
1299daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
1300daf8e166STejun Heo 	cmd->ret = ret;
1301daf8e166STejun Heo 	return;
1302daf8e166STejun Heo 
1303daf8e166STejun Heo abort:
1304daf8e166STejun Heo 	put_task_struct(p);
1305daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1306daf8e166STejun Heo 
1307daf8e166STejun Heo 	/*
1308daf8e166STejun Heo 	 * Undo __scx_init_task() for tasks we marked. scx_enable_task() never
1309daf8e166STejun Heo 	 * ran for @sch on them, so calling scx_disable_task() here would invoke
1310daf8e166STejun Heo 	 * ops.disable() without a matching ops.enable(). scx_enabling_sub_sched
1311daf8e166STejun Heo 	 * must stay set until SUB_INIT is cleared from every marked task -
1312daf8e166STejun Heo 	 * scx_disable_and_exit_task() reads it when a task exits concurrently.
1313daf8e166STejun Heo 	 */
1314daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
1315daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
1316daf8e166STejun Heo 		if (p->scx.flags & SCX_TASK_SUB_INIT) {
1317daf8e166STejun Heo 			scx_sub_init_cancel_task(sch, p);
1318daf8e166STejun Heo 			p->scx.flags &= ~SCX_TASK_SUB_INIT;
1319daf8e166STejun Heo 		}
1320daf8e166STejun Heo 	}
1321daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1322daf8e166STejun Heo 	scx_enabling_sub_sched = NULL;
1323daf8e166STejun Heo err_unlock_and_disable:
1324daf8e166STejun Heo 	/* we'll soon enter disable path, keep bypass on */
1325daf8e166STejun Heo 	scx_cgroup_unlock();
1326daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
1327daf8e166STejun Heo err_disable:
1328daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
1329ad45691dSTejun Heo 	/*
1330ad45691dSTejun Heo 	 * Some enable failures only return an errno (e.g. -ENOMEM from an
1331ad45691dSTejun Heo 	 * allocation) without calling scx_error(). Record it so
1332ad45691dSTejun Heo 	 * scx_flush_disable_work() runs the disable and ops.exit() fires.
1333ad45691dSTejun Heo 	 */
1334ad45691dSTejun Heo 	scx_error(sch, "scx_sub_enable() failed (%d)", ret);
1335daf8e166STejun Heo 	scx_flush_disable_work(sch);
1336daf8e166STejun Heo 	cmd->ret = 0;
1337daf8e166STejun Heo }
1338daf8e166STejun Heo 
1339daf8e166STejun Heo static s32 scx_cgroup_lifetime_notify(struct notifier_block *nb,
1340daf8e166STejun Heo 				      unsigned long action, void *data)
1341daf8e166STejun Heo {
1342daf8e166STejun Heo 	struct cgroup *cgrp = data;
1343daf8e166STejun Heo 	struct cgroup *parent = cgroup_parent(cgrp);
1344daf8e166STejun Heo 
1345daf8e166STejun Heo 	if (!cgroup_on_dfl(cgrp))
1346daf8e166STejun Heo 		return NOTIFY_OK;
1347daf8e166STejun Heo 
1348daf8e166STejun Heo 	switch (action) {
1349daf8e166STejun Heo 	case CGROUP_LIFETIME_ONLINE:
1350daf8e166STejun Heo 		/* inherit ->scx_sched from $parent */
1351daf8e166STejun Heo 		if (parent)
1352daf8e166STejun Heo 			rcu_assign_pointer(cgrp->scx_sched, parent->scx_sched);
1353daf8e166STejun Heo 		break;
1354daf8e166STejun Heo 	case CGROUP_LIFETIME_OFFLINE:
1355daf8e166STejun Heo 		/* if there is a sched attached, shoot it down */
1356daf8e166STejun Heo 		if (cgrp->scx_sched && cgrp->scx_sched->cgrp == cgrp)
1357daf8e166STejun Heo 			scx_exit(cgrp->scx_sched, SCX_EXIT_UNREG_KERN,
1358daf8e166STejun Heo 				 SCX_ECODE_RSN_CGROUP_OFFLINE,
1359daf8e166STejun Heo 				 "cgroup %llu going offline", cgroup_id(cgrp));
1360daf8e166STejun Heo 		break;
1361daf8e166STejun Heo 	}
1362daf8e166STejun Heo 
1363daf8e166STejun Heo 	return NOTIFY_OK;
1364daf8e166STejun Heo }
1365daf8e166STejun Heo 
1366daf8e166STejun Heo static struct notifier_block scx_cgroup_lifetime_nb = {
1367daf8e166STejun Heo 	.notifier_call = scx_cgroup_lifetime_notify,
1368daf8e166STejun Heo };
1369daf8e166STejun Heo 
1370daf8e166STejun Heo static s32 __init scx_cgroup_lifetime_notifier_init(void)
1371daf8e166STejun Heo {
1372daf8e166STejun Heo 	return blocking_notifier_chain_register(&cgroup_lifetime_notifier,
1373daf8e166STejun Heo 						&scx_cgroup_lifetime_nb);
1374daf8e166STejun Heo }
1375daf8e166STejun Heo core_initcall(scx_cgroup_lifetime_notifier_init);
1376daf8e166STejun Heo 
13775f2a9a4cSTejun Heo static void scx_pstack_recursion(struct bpf_prog *prog, const char *op)
1378daf8e166STejun Heo {
1379daf8e166STejun Heo 	struct scx_sched *sch;
1380daf8e166STejun Heo 
1381daf8e166STejun Heo 	guard(rcu)();
1382daf8e166STejun Heo 	sch = scx_prog_sched(prog->aux);
1383daf8e166STejun Heo 	if (unlikely(!sch))
1384daf8e166STejun Heo 		return;
1385daf8e166STejun Heo 
13865f2a9a4cSTejun Heo 	scx_error(sch, "%s recursion detected", op);
13875f2a9a4cSTejun Heo }
13885f2a9a4cSTejun Heo 
13895f2a9a4cSTejun Heo void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog)
13905f2a9a4cSTejun Heo {
13915f2a9a4cSTejun Heo 	scx_pstack_recursion(prog, "dispatch");
13925f2a9a4cSTejun Heo }
13935f2a9a4cSTejun Heo 
13945f2a9a4cSTejun Heo void scx_pstack_recursion_on_caps_updated(struct bpf_prog *prog)
13955f2a9a4cSTejun Heo {
13965f2a9a4cSTejun Heo 	scx_pstack_recursion(prog, "sub_caps_updated");
1397daf8e166STejun Heo }
1398daf8e166STejun Heo 
1399daf8e166STejun Heo __bpf_kfunc_start_defs();
1400daf8e166STejun Heo 
1401daf8e166STejun Heo /**
1402daf8e166STejun Heo  * scx_bpf_sub_dispatch - Trigger dispatching on a child scheduler
1403daf8e166STejun Heo  * @cgroup_id: cgroup ID of the child scheduler to dispatch
1404daf8e166STejun Heo  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
1405daf8e166STejun Heo  *
1406daf8e166STejun Heo  * Allows a parent scheduler to trigger dispatching on one of its direct
1407daf8e166STejun Heo  * child schedulers. The child scheduler runs its dispatch operation to
1408daf8e166STejun Heo  * move tasks from dispatch queues to the local runqueue.
1409daf8e166STejun Heo  *
1410daf8e166STejun Heo  * Returns: true on success, false if cgroup_id is invalid, not a direct
1411daf8e166STejun Heo  * child, or caller lacks dispatch permission.
1412daf8e166STejun Heo  */
1413daf8e166STejun Heo __bpf_kfunc bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux)
1414daf8e166STejun Heo {
1415daf8e166STejun Heo 	struct rq *this_rq = this_rq();
1416daf8e166STejun Heo 	struct scx_sched *parent, *child;
1417daf8e166STejun Heo 
1418daf8e166STejun Heo 	guard(rcu)();
1419daf8e166STejun Heo 	parent = scx_prog_sched(aux);
1420daf8e166STejun Heo 	if (unlikely(!parent))
1421daf8e166STejun Heo 		return false;
1422daf8e166STejun Heo 
1423daf8e166STejun Heo 	child = scx_find_sub_sched(cgroup_id);
1424daf8e166STejun Heo 
1425daf8e166STejun Heo 	if (unlikely(!child))
1426daf8e166STejun Heo 		return false;
1427daf8e166STejun Heo 
1428daf8e166STejun Heo 	if (unlikely(scx_parent(child) != parent)) {
1429daf8e166STejun Heo 		scx_error(parent, "trying to dispatch a distant sub-sched on cgroup %llu",
1430daf8e166STejun Heo 			  cgroup_id);
1431daf8e166STejun Heo 		return false;
1432daf8e166STejun Heo 	}
1433daf8e166STejun Heo 
1434147d1885STejun Heo 	/*
1435147d1885STejun Heo 	 * Skip a child that does not effectively hold the base cap on this cpu:
1436147d1885STejun Heo 	 * its inserts would only be rejected. ecaps are synced at the top of
1437147d1885STejun Heo 	 * balance_one() before dispatch, so this reflects the in-effect state.
1438147d1885STejun Heo 	 */
1439147d1885STejun Heo 	if (scx_missing_caps(child, cpu_of(this_rq), SCX_CAP_BASE))
1440147d1885STejun Heo 		return false;
1441147d1885STejun Heo 
1442daf8e166STejun Heo 	return scx_dispatch_sched(child, this_rq, this_rq->scx.sub_dispatch_prev,
1443daf8e166STejun Heo 				  true);
1444daf8e166STejun Heo }
1445daf8e166STejun Heo 
144686094b95STejun Heo /* Validate common inputs. On success, *parent_out and *child_out are set. */
144786094b95STejun Heo static s32 sub_cap_preamble(u64 cgroup_id, u64 caps, const struct bpf_prog_aux *aux,
144886094b95STejun Heo 			    struct scx_sched **parent_out, struct scx_sched **child_out)
144986094b95STejun Heo {
145086094b95STejun Heo 	struct scx_sched *parent, *child;
145186094b95STejun Heo 
145286094b95STejun Heo 	parent = scx_prog_sched(aux);
145386094b95STejun Heo 	if (unlikely(!parent))
145486094b95STejun Heo 		return -ENODEV;
145586094b95STejun Heo 
145686094b95STejun Heo 	if (!scx_is_cid_type()) {
145786094b95STejun Heo 		scx_error(parent, "sub-cap kfuncs require a cid-form scheduler");
145886094b95STejun Heo 		return -EOPNOTSUPP;
145986094b95STejun Heo 	}
146086094b95STejun Heo 
146186094b95STejun Heo 	child = scx_find_sub_sched(cgroup_id);
146286094b95STejun Heo 	if (unlikely(!child))
146386094b95STejun Heo 		return -ENODEV;
146486094b95STejun Heo 
146586094b95STejun Heo 	if (unlikely(scx_parent(child) != parent)) {
146686094b95STejun Heo 		scx_error(parent, "%s: sub-%llu is not a direct child",
146786094b95STejun Heo 			  parent->cgrp_path, cgroup_id);
146886094b95STejun Heo 		return -EINVAL;
146986094b95STejun Heo 	}
147086094b95STejun Heo 
147186094b95STejun Heo 	if (unlikely(caps & ~__SCX_CAP_ALL)) {
147286094b95STejun Heo 		scx_error(parent, "invalid caps 0x%llx", caps);
147386094b95STejun Heo 		return -EINVAL;
147486094b95STejun Heo 	}
147586094b95STejun Heo 
147686094b95STejun Heo 	*parent_out = parent;
147786094b95STejun Heo 	*child_out = child;
147886094b95STejun Heo 	return 0;
147986094b95STejun Heo }
148086094b95STejun Heo 
148186094b95STejun Heo /**
148286094b95STejun Heo  * scx_bpf_sub_grant - Grant @caps on @cmask__ign's cids to a direct child
148386094b95STejun Heo  * @cgroup_id: cgroup id of the direct child sub-sched
148486094b95STejun Heo  * @caps: bitmask of SCX_CAP_* to grant
148586094b95STejun Heo  * @cmask__ign: cid cmask to grant @caps on (arena pointer)
148686094b95STejun Heo  * @denied_out__ign: optional arena cmask accumulating refused cids
148786094b95STejun Heo  * @aux: implicit BPF argument
148886094b95STejun Heo  *
148986094b95STejun Heo  * A cid in @cmask__ign is granted to the child only if the parent holds every
149086094b95STejun Heo  * requested cap on it. Refused cids are OR'd into @denied_out__ign when
149186094b95STejun Heo  * provided. Refusals outside @denied_out__ign's range are not recorded.
149286094b95STejun Heo  *
149386094b95STejun Heo  * All-or-nothing keeps the caller-visible result binary per cid, so
149486094b95STejun Heo  * @denied_out__ign is one mask to interpret rather than a per-cap matrix.
149586094b95STejun Heo  *
149686094b95STejun Heo  * Return 0 on full success, -EPERM if any cid was refused, or a negative
149786094b95STejun Heo  * errno on other failures.
149886094b95STejun Heo  */
149986094b95STejun Heo __bpf_kfunc s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps,
150086094b95STejun Heo 				  const struct scx_cmask *cmask__ign,
150186094b95STejun Heo 				  struct scx_cmask *denied_out__ign,
150286094b95STejun Heo 				  const struct bpf_prog_aux *aux)
150386094b95STejun Heo {
150486094b95STejun Heo 	struct scx_cmask_ref ref, denied_ref;
150586094b95STejun Heo 	struct scx_sched *parent, *child;
150686094b95STejun Heo 	bool any_denied = false;
15075f2a9a4cSTejun Heo 	LIST_HEAD(to_deliver);
150886094b95STejun Heo 	s32 si, ret;
150986094b95STejun Heo 
151086094b95STejun Heo 	guard(irqsave)();
151186094b95STejun Heo 
151286094b95STejun Heo 	ret = sub_cap_preamble(cgroup_id, caps, aux, &parent, &child);
151386094b95STejun Heo 	if (ret)
151486094b95STejun Heo 		return ret;
151586094b95STejun Heo 
151686094b95STejun Heo 	ret = scx_cmask_ref_init(parent, cmask__ign, &ref);
151786094b95STejun Heo 	if (ret) {
151886094b95STejun Heo 		scx_error(parent, "invalid cmask (%d)", ret);
151986094b95STejun Heo 		return ret;
152086094b95STejun Heo 	}
152186094b95STejun Heo 
152286094b95STejun Heo 	if (denied_out__ign) {
152386094b95STejun Heo 		ret = scx_cmask_ref_init(parent, denied_out__ign, &denied_ref);
152486094b95STejun Heo 		if (ret) {
152586094b95STejun Heo 			scx_error(parent, "invalid denied_out (%d)", ret);
152686094b95STejun Heo 			return ret;
152786094b95STejun Heo 		}
152886094b95STejun Heo 	}
152986094b95STejun Heo 
153086094b95STejun Heo 	/* apply the grant one shard at a time */
153186094b95STejun Heo 	for (si = ref.shard_first; si < ref.shard_end; si++) {
153286094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(slice, 0, SCX_CID_SHARD_MAX_CPUS);
153386094b95STejun Heo 		struct scx_pshard *pps = parent->pshard[si];
153486094b95STejun Heo 		struct scx_pshard *cps = child->pshard[si];
15355f2a9a4cSTejun Heo 		u64 granted_caps = 0;
153686094b95STejun Heo 		u32 cap_bit;
153786094b95STejun Heo 
153886094b95STejun Heo 		scx_cmask_ref_shard(&ref, si, slice);
153986094b95STejun Heo 		if (scx_cmask_empty(slice))
154086094b95STejun Heo 			continue;
154186094b95STejun Heo 
154286094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(granted_cids, slice->base, slice->nr_cids);
15435f2a9a4cSTejun Heo 		SCX_CMASK_DEFINE_SHARD(changed_cids, slice->base, slice->nr_cids);
15445f2a9a4cSTejun Heo 		SCX_CMASK_DEFINE_SHARD(delta, slice->base, slice->nr_cids);
15455f2a9a4cSTejun Heo 
154686094b95STejun Heo 		scx_cmask_copy(granted_cids, slice);
154786094b95STejun Heo 
154886094b95STejun Heo 		scoped_guard (raw_spinlock, &pps->lock) {
154986094b95STejun Heo 			guard(raw_spinlock_nested)(&cps->lock);
155086094b95STejun Heo 
155186094b95STejun Heo 			/*
155286094b95STejun Heo 			 * Narrow granted_cids to cids the parent holds every
155386094b95STejun Heo 			 * requested cap on. All-or-nothing per cid.
155486094b95STejun Heo 			 */
155586094b95STejun Heo 			scx_for_each_cap_bit(cap_bit, caps)
155686094b95STejun Heo 				scx_cmask_and(granted_cids, &pps->caps[cap_bit].cmask);
155786094b95STejun Heo 
15585f2a9a4cSTejun Heo 			/*
15595f2a9a4cSTejun Heo 			 * For each requested cap, fold the newly-set cids into
15605f2a9a4cSTejun Heo 			 * the child and accumulate the delta.
15615f2a9a4cSTejun Heo 			 */
15625f2a9a4cSTejun Heo 			scx_for_each_cap_bit(cap_bit, caps) {
15635f2a9a4cSTejun Heo 				struct scx_cmask *ccm = &cps->caps[cap_bit].cmask;
15645f2a9a4cSTejun Heo 
15655f2a9a4cSTejun Heo 				scx_cmask_copy(delta, granted_cids);
15665f2a9a4cSTejun Heo 				scx_cmask_andnot(delta, ccm);
15675f2a9a4cSTejun Heo 				if (scx_cmask_empty(delta))
15685f2a9a4cSTejun Heo 					continue;
15695f2a9a4cSTejun Heo 
15705f2a9a4cSTejun Heo 				scx_cmask_or(ccm, delta);
15715f2a9a4cSTejun Heo 				scx_cmask_or(changed_cids, delta);
15725f2a9a4cSTejun Heo 				granted_caps |= BIT_U64(cap_bit);
15735f2a9a4cSTejun Heo 			}
15745f2a9a4cSTejun Heo 
157556fdc35bSTejun Heo 			if (granted_caps) {
157656fdc35bSTejun Heo 				s32 cid;
157756fdc35bSTejun Heo 
15785f2a9a4cSTejun Heo 				caps_updated_record(cps, changed_cids, granted_caps,
15795f2a9a4cSTejun Heo 						    &to_deliver);
1580ca3aec45STejun Heo 				/*
1581ca3aec45STejun Heo 				 * The sync arms an update_idle() re-notify if
1582ca3aec45STejun Heo 				 * the cid gains baseline access, so the holder
1583ca3aec45STejun Heo 				 * learns of an already-idle cid.
1584ca3aec45STejun Heo 				 */
158556fdc35bSTejun Heo 				scx_cmask_for_each_cid(cid, changed_cids)
158656fdc35bSTejun Heo 					queue_sync_ecaps(child, cid);
158756fdc35bSTejun Heo 			}
158886094b95STejun Heo 		}
158986094b95STejun Heo 
159086094b95STejun Heo 		/* record cids that didn't make it through into @denied_out */
159186094b95STejun Heo 		if (!scx_cmask_subset(slice, granted_cids)) {
159286094b95STejun Heo 			any_denied = true;
159386094b95STejun Heo 			if (denied_out__ign) {
159486094b95STejun Heo 				SCX_CMASK_DEFINE_SHARD(denied, slice->base, slice->nr_cids);
159586094b95STejun Heo 
159686094b95STejun Heo 				scx_cmask_copy(denied, slice);
159786094b95STejun Heo 				scx_cmask_andnot(denied, granted_cids);
159886094b95STejun Heo 				scx_cmask_ref_or(&denied_ref, denied);
159986094b95STejun Heo 			}
160086094b95STejun Heo 		}
160186094b95STejun Heo 	}
16025f2a9a4cSTejun Heo 
16035f2a9a4cSTejun Heo 	caps_updated_deliver(&to_deliver);
16045f2a9a4cSTejun Heo 
160586094b95STejun Heo 	return any_denied ? -EPERM : 0;
160686094b95STejun Heo }
160786094b95STejun Heo 
160886094b95STejun Heo /**
160986094b95STejun Heo  * scx_bpf_sub_revoke - Revoke @caps on @cmask__ign's cids from @child
161086094b95STejun Heo  * @cgroup_id: cgroup id of the direct child sub-sched
161186094b95STejun Heo  * @caps: bitmask of SCX_CAP_* to revoke
161286094b95STejun Heo  * @cmask__ign: cid cmask to revoke @caps on (arena pointer)
161386094b95STejun Heo  * @aux: implicit BPF argument
161486094b95STejun Heo  *
161586094b95STejun Heo  * Clear @caps bits on @cmask__ign from the child named by @cgroup_id and all
161686094b95STejun Heo  * its descendants. The origin parent's pshard lock is held across the subtree
161786094b95STejun Heo  * walk so a concurrent grant from the origin parent observes the revoked
161886094b95STejun Heo  * state.
161986094b95STejun Heo  */
162086094b95STejun Heo __bpf_kfunc void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps,
162186094b95STejun Heo 				    const struct scx_cmask *cmask__ign,
162286094b95STejun Heo 				    const struct bpf_prog_aux *aux)
162386094b95STejun Heo {
162486094b95STejun Heo 	struct scx_cmask_ref ref;
162586094b95STejun Heo 	struct scx_sched *parent, *child, *pos;
16265f2a9a4cSTejun Heo 	LIST_HEAD(to_deliver);
162786094b95STejun Heo 	s32 si, ret;
162886094b95STejun Heo 
162986094b95STejun Heo 	guard(irqsave)();
163086094b95STejun Heo 
163186094b95STejun Heo 	if (sub_cap_preamble(cgroup_id, caps, aux, &parent, &child))
163286094b95STejun Heo 		return;
163386094b95STejun Heo 
163486094b95STejun Heo 	ret = scx_cmask_ref_init(parent, cmask__ign, &ref);
163586094b95STejun Heo 	if (ret) {
163686094b95STejun Heo 		scx_error(parent, "invalid cmask (%d)", ret);
163786094b95STejun Heo 		return;
163886094b95STejun Heo 	}
163986094b95STejun Heo 
164086094b95STejun Heo 	/* per-shard, walk child's subtree and clear @caps */
164186094b95STejun Heo 	for (si = ref.shard_first; si < ref.shard_end; si++) {
164286094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(slice, 0, SCX_CID_SHARD_MAX_CPUS);
164386094b95STejun Heo 
164486094b95STejun Heo 		scx_cmask_ref_shard(&ref, si, slice);
164586094b95STejun Heo 		if (scx_cmask_empty(slice))
164686094b95STejun Heo 			continue;
164786094b95STejun Heo 
164886094b95STejun Heo 		/*
164986094b95STejun Heo 		 * Pre-order with subtree skip: a descendant that cleared
165086094b95STejun Heo 		 * nothing means no descendant of it can hold @caps on these
165186094b95STejun Heo 		 * cids either.
165286094b95STejun Heo 		 */
165386094b95STejun Heo 		guard(raw_spinlock)(&parent->pshard[si]->lock);
165486094b95STejun Heo 		pos = scx_next_descendant_pre(NULL, child);
165586094b95STejun Heo 		while (pos) {
165686094b95STejun Heo 			struct scx_pshard *ps = pos->pshard[si];
16575f2a9a4cSTejun Heo 			SCX_CMASK_DEFINE_SHARD(changed_cids, slice->base, slice->nr_cids);
16585f2a9a4cSTejun Heo 			SCX_CMASK_DEFINE_SHARD(delta, slice->base, slice->nr_cids);
165986094b95STejun Heo 			u64 revoked_caps = 0;
166086094b95STejun Heo 			u32 cap_bit;
166186094b95STejun Heo 
166286094b95STejun Heo 			scoped_guard (raw_spinlock_nested, &ps->lock) {
16635f2a9a4cSTejun Heo 				/*
16645f2a9a4cSTejun Heo 				 * For each cap, clear lost cids and accumulate
16655f2a9a4cSTejun Heo 				 * the per-cap diff for notification.
16665f2a9a4cSTejun Heo 				 */
166786094b95STejun Heo 				scx_for_each_cap_bit(cap_bit, caps) {
166886094b95STejun Heo 					struct scx_cmask *cm = &ps->caps[cap_bit].cmask;
166986094b95STejun Heo 
16705f2a9a4cSTejun Heo 					scx_cmask_copy(delta, cm);
16715f2a9a4cSTejun Heo 					scx_cmask_and(delta, slice);
16725f2a9a4cSTejun Heo 					if (scx_cmask_empty(delta))
167386094b95STejun Heo 						continue;
16745f2a9a4cSTejun Heo 
16755f2a9a4cSTejun Heo 					scx_cmask_andnot(cm, delta);
16765f2a9a4cSTejun Heo 					scx_cmask_or(changed_cids, delta);
167786094b95STejun Heo 					revoked_caps |= BIT_U64(cap_bit);
167886094b95STejun Heo 				}
16795f2a9a4cSTejun Heo 
168056fdc35bSTejun Heo 				if (revoked_caps) {
168156fdc35bSTejun Heo 					s32 cid;
168256fdc35bSTejun Heo 
16835f2a9a4cSTejun Heo 					caps_updated_record(ps, changed_cids, revoked_caps,
16845f2a9a4cSTejun Heo 							    &to_deliver);
168556fdc35bSTejun Heo 					scx_cmask_for_each_cid(cid, changed_cids)
168656fdc35bSTejun Heo 						queue_sync_ecaps(pos, cid);
168756fdc35bSTejun Heo 				}
168886094b95STejun Heo 			}
168986094b95STejun Heo 
169086094b95STejun Heo 			if (revoked_caps)
169186094b95STejun Heo 				pos = scx_next_descendant_pre(pos, child);
169286094b95STejun Heo 			else
169386094b95STejun Heo 				pos = scx_skip_subtree_pre(pos, child);
169486094b95STejun Heo 		}
169586094b95STejun Heo 	}
16965f2a9a4cSTejun Heo 
16975f2a9a4cSTejun Heo 	caps_updated_deliver(&to_deliver);
169886094b95STejun Heo }
169986094b95STejun Heo 
170086094b95STejun Heo /**
170186094b95STejun Heo  * scx_bpf_sub_caps - Read self's or a direct child's cap cmasks
170286094b95STejun Heo  * @cgroup_id: 0 for self, or a direct child's cgroup id
170386094b95STejun Heo  * @caps: one or more SCX_CAP_* bits
170486094b95STejun Heo  * @out__ign: arena cmask to receive the union of @caps within its range
170586094b95STejun Heo  * @aux: implicit BPF argument
170686094b95STejun Heo  *
170786094b95STejun Heo  * Read the cap cmasks granted on each cid for self (@cgroup_id 0) or a direct
170886094b95STejun Heo  * child - the literal granted set. A sched can read only itself or a direct
170986094b95STejun Heo  * child.
171086094b95STejun Heo  *
171186094b95STejun Heo  * Return 0, -ENODEV if @cgroup_id names no direct child, or -EINVAL on bad
171286094b95STejun Heo  * inputs.
171386094b95STejun Heo  */
171486094b95STejun Heo __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out__ign,
171586094b95STejun Heo 				 const struct bpf_prog_aux *aux)
171686094b95STejun Heo {
171786094b95STejun Heo 	struct scx_cmask_ref ref;
171886094b95STejun Heo 	struct scx_sched *sch, *target;
171986094b95STejun Heo 	struct scx_pshard **pshard;
172086094b95STejun Heo 	s32 si, ret;
172186094b95STejun Heo 
172286094b95STejun Heo 	guard(irqsave)();
172386094b95STejun Heo 
172486094b95STejun Heo 	sch = scx_prog_sched(aux);
172586094b95STejun Heo 	if (unlikely(!sch))
172686094b95STejun Heo 		return -ENODEV;
172786094b95STejun Heo 
172886094b95STejun Heo 	if (!scx_is_cid_type()) {
172986094b95STejun Heo 		scx_error(sch, "sub-cap kfuncs require a cid-form scheduler");
173086094b95STejun Heo 		return -EOPNOTSUPP;
173186094b95STejun Heo 	}
173286094b95STejun Heo 
173386094b95STejun Heo 	if (unlikely(caps & ~__SCX_CAP_ALL)) {
173486094b95STejun Heo 		scx_error(sch, "invalid caps 0x%llx", caps);
173586094b95STejun Heo 		return -EINVAL;
173686094b95STejun Heo 	}
173786094b95STejun Heo 
173886094b95STejun Heo 	/* @cgroup_id 0 reads self, otherwise a direct child */
173986094b95STejun Heo 	if (cgroup_id) {
174086094b95STejun Heo 		target = scx_find_sub_sched(cgroup_id);
174186094b95STejun Heo 		if (unlikely(!target))
174286094b95STejun Heo 			return -ENODEV;
174386094b95STejun Heo 		if (unlikely(scx_parent(target) != sch)) {
174486094b95STejun Heo 			scx_error(sch, "%s: sub-%llu is not a direct child",
174586094b95STejun Heo 				  sch->cgrp_path, cgroup_id);
174686094b95STejun Heo 			return -EINVAL;
174786094b95STejun Heo 		}
174886094b95STejun Heo 	} else {
174986094b95STejun Heo 		target = sch;
175086094b95STejun Heo 	}
175186094b95STejun Heo 
175286094b95STejun Heo 	/*
175386094b95STejun Heo 	 * The target's caps storage may not be set up yet (e.g. a self-read
175486094b95STejun Heo 	 * during ops.init_cids()). Pairs with the publish in
175586094b95STejun Heo 	 * scx_alloc_pshards(): a non-NULL pshard has every element set.
175686094b95STejun Heo 	 */
175786094b95STejun Heo 	pshard = READ_ONCE(target->pshard);
175886094b95STejun Heo 	if (unlikely(!pshard)) {
175986094b95STejun Heo 		scx_error(sch, "scx_bpf_sub_caps() called before caps storage is initialized");
176086094b95STejun Heo 		return -ENODEV;
176186094b95STejun Heo 	}
176286094b95STejun Heo 
176386094b95STejun Heo 	ret = scx_cmask_ref_init(sch, out__ign, &ref);
176486094b95STejun Heo 	if (ret) {
176586094b95STejun Heo 		scx_error(sch, "invalid out (%d)", ret);
176686094b95STejun Heo 		return ret;
176786094b95STejun Heo 	}
176886094b95STejun Heo 
176986094b95STejun Heo 	for (si = ref.shard_first; si < ref.shard_end; si++) {
177086094b95STejun Heo 		const struct scx_cid_shard *shard = &scx_cid_shard_ranges[si];
177186094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(local_out, shard->base_cid, shard->nr_cids);
177286094b95STejun Heo 		u32 cap_bit;
177386094b95STejun Heo 
177486094b95STejun Heo 		scx_for_each_cap_bit(cap_bit, caps)
177586094b95STejun Heo 			scx_cmask_or(local_out, &pshard[si]->caps[cap_bit].cmask);
177686094b95STejun Heo 		scx_cmask_ref_copy(&ref, local_out);
177786094b95STejun Heo 	}
177886094b95STejun Heo 	return 0;
177986094b95STejun Heo }
178086094b95STejun Heo 
1781b0a2ca6aSTejun Heo /**
1782b0a2ca6aSTejun Heo  * scx_bpf_sub_kill_bstr - Kill a direct child sub-scheduler
1783b0a2ca6aSTejun Heo  * @cgroup_id: cgroup id of the direct child to kill
1784b0a2ca6aSTejun Heo  * @fmt: reason message format string
1785b0a2ca6aSTejun Heo  * @data: format string parameters packaged using ___bpf_fill() macro
1786b0a2ca6aSTejun Heo  * @data__sz: @data len, must end in '__sz' for the verifier
1787b0a2ca6aSTejun Heo  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
1788b0a2ca6aSTejun Heo  *
1789b0a2ca6aSTejun Heo  * Evict a direct child sub-scheduler, disabling it with the supplied reason.
1790b0a2ca6aSTejun Heo  * The child and its subtree are torn down asynchronously through the usual
1791b0a2ca6aSTejun Heo  * disable path.
1792b0a2ca6aSTejun Heo  *
1793b0a2ca6aSTejun Heo  * Unlike scx_bpf_exit(), no exit code is taken: the child is a separate
1794b0a2ca6aSTejun Heo  * scheduler with its own exit-code semantics, so a code chosen by the parent
1795b0a2ca6aSTejun Heo  * would have no defined meaning. The reason string carries the intent.
1796b0a2ca6aSTejun Heo  *
1797b0a2ca6aSTejun Heo  * Return 0 on success or -ENODEV if @cgroup_id names no sub-scheduler, which
1798b0a2ca6aSTejun Heo  * can race with the child detaching on its own and so is not a scheduler error.
1799b0a2ca6aSTejun Heo  * Naming a sched that exists but is not a direct child aborts the parent.
1800b0a2ca6aSTejun Heo  */
1801b0a2ca6aSTejun Heo __printf(2, 0)
1802b0a2ca6aSTejun Heo __bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
1803b0a2ca6aSTejun Heo 				      unsigned long long *data, u32 data__sz,
1804b0a2ca6aSTejun Heo 				      const struct bpf_prog_aux *aux)
1805b0a2ca6aSTejun Heo {
1806b0a2ca6aSTejun Heo 	struct scx_sched *parent, *child;
1807b0a2ca6aSTejun Heo 	s32 ret;
1808b0a2ca6aSTejun Heo 
1809b0a2ca6aSTejun Heo 	guard(rcu)();
1810b0a2ca6aSTejun Heo 
1811b0a2ca6aSTejun Heo 	parent = scx_prog_sched(aux);
1812b0a2ca6aSTejun Heo 	if (unlikely(!parent))
1813b0a2ca6aSTejun Heo 		return -ENODEV;
1814b0a2ca6aSTejun Heo 
1815b0a2ca6aSTejun Heo 	if (!scx_is_cid_type()) {
1816b0a2ca6aSTejun Heo 		scx_error(parent, "sub-cap kfuncs require a cid-form scheduler");
1817b0a2ca6aSTejun Heo 		return -EOPNOTSUPP;
1818b0a2ca6aSTejun Heo 	}
1819b0a2ca6aSTejun Heo 
1820b0a2ca6aSTejun Heo 	child = scx_find_sub_sched(cgroup_id);
1821b0a2ca6aSTejun Heo 	if (unlikely(!child))
1822b0a2ca6aSTejun Heo 		return -ENODEV;
1823b0a2ca6aSTejun Heo 
1824b0a2ca6aSTejun Heo 	if (unlikely(scx_parent(child) != parent)) {
1825b0a2ca6aSTejun Heo 		scx_error(parent, "%s: sub-%llu is not a direct child",
1826b0a2ca6aSTejun Heo 			  parent->cgrp_path, cgroup_id);
1827b0a2ca6aSTejun Heo 		return -EINVAL;
1828b0a2ca6aSTejun Heo 	}
1829b0a2ca6aSTejun Heo 
1830b0a2ca6aSTejun Heo 	guard(raw_spinlock_irqsave)(&scx_exit_bstr_buf_lock);
1831b0a2ca6aSTejun Heo 	ret = scx_bstr_format(parent, &scx_exit_bstr_buf, fmt, data, data__sz);
1832b0a2ca6aSTejun Heo 	if (ret < 0)
1833b0a2ca6aSTejun Heo 		return ret;
1834b0a2ca6aSTejun Heo 	scx_exit(child, SCX_EXIT_PARENT_KILL, 0, "%s", scx_exit_bstr_buf.line);
1835b0a2ca6aSTejun Heo 	return 0;
1836b0a2ca6aSTejun Heo }
1837b0a2ca6aSTejun Heo 
1838daf8e166STejun Heo __bpf_kfunc_end_defs();
1839daf8e166STejun Heo 
1840daf8e166STejun Heo #endif	/* CONFIG_EXT_SUB_SCHED */
1841