xref: /linux/kernel/sched/ext/sub.c (revision 8946dbd3aa91acfb75b1633859290ba248e313b2)
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 
25*8946dbd3STejun Heo /*
26*8946dbd3STejun Heo  * On while any sub-scheduler exists so that a root-only system doesn't pay for
27*8946dbd3STejun Heo  * the sub-sched portions of hot paths. See scx_has_subs().
28*8946dbd3STejun Heo  */
29*8946dbd3STejun Heo DEFINE_STATIC_KEY_FALSE(__scx_has_subs);
30*8946dbd3STejun 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 {
245*8946dbd3STejun Heo 	if (!scx_has_subs())
246*8946dbd3STejun Heo 		return &rq->scx.local_dsq;
247*8946dbd3STejun 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 
326*8946dbd3STejun 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 
509*8946dbd3STejun 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 
761daf8e166STejun Heo static void scx_fail_parent(struct scx_sched *sch,
762daf8e166STejun Heo 			    struct task_struct *failed, s32 fail_code)
763daf8e166STejun Heo {
764daf8e166STejun Heo 	struct scx_sched *parent = scx_parent(sch);
765daf8e166STejun Heo 	struct scx_task_iter sti;
766daf8e166STejun Heo 	struct task_struct *p;
767daf8e166STejun Heo 
768daf8e166STejun Heo 	scx_error(parent, "ops.init_task() failed (%d) for %s[%d] while disabling a sub-scheduler",
769daf8e166STejun Heo 		  fail_code, failed->comm, failed->pid);
770daf8e166STejun Heo 
771daf8e166STejun Heo 	/*
772daf8e166STejun Heo 	 * Once $parent is bypassed, it's safe to put SCX_TASK_NONE tasks into
773daf8e166STejun Heo 	 * it. This may cause downstream failures on the BPF side but $parent is
774daf8e166STejun Heo 	 * dying anyway.
775daf8e166STejun Heo 	 */
776daf8e166STejun Heo 	scx_bypass(parent, true);
777daf8e166STejun Heo 
778daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
779daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
780daf8e166STejun Heo 		if (scx_task_on_sched(parent, p))
781daf8e166STejun Heo 			continue;
782daf8e166STejun Heo 
783daf8e166STejun Heo 		scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
784daf8e166STejun Heo 			scx_disable_and_exit_task(sch, p);
785daf8e166STejun Heo 			scx_set_task_sched(p, parent);
786daf8e166STejun Heo 		}
787daf8e166STejun Heo 	}
788daf8e166STejun Heo 	scx_task_iter_stop(&sti);
789daf8e166STejun Heo }
790daf8e166STejun Heo 
791daf8e166STejun Heo void scx_sub_disable(struct scx_sched *sch)
792daf8e166STejun Heo {
793daf8e166STejun Heo 	struct scx_sched *parent = scx_parent(sch);
794daf8e166STejun Heo 	struct scx_task_iter sti;
795daf8e166STejun Heo 	struct task_struct *p;
796daf8e166STejun Heo 	int ret;
797daf8e166STejun Heo 
798daf8e166STejun Heo 	/*
799daf8e166STejun Heo 	 * Guarantee forward progress and wait for descendants to be disabled.
800daf8e166STejun Heo 	 * To limit disruptions, $parent is not bypassed. Tasks are fully
801daf8e166STejun Heo 	 * prepped and then inserted back into $parent.
802daf8e166STejun Heo 	 */
803daf8e166STejun Heo 	scx_bypass(sch, true);
804daf8e166STejun Heo 	drain_descendants(sch);
805daf8e166STejun Heo 
806daf8e166STejun Heo 	/*
807daf8e166STejun Heo 	 * Here, every runnable task is guaranteed to make forward progress and
808daf8e166STejun Heo 	 * we can safely use blocking synchronization constructs. Actually
809daf8e166STejun Heo 	 * disable ops.
810daf8e166STejun Heo 	 */
811daf8e166STejun Heo 	mutex_lock(&scx_enable_mutex);
812daf8e166STejun Heo 	percpu_down_write(&scx_fork_rwsem);
813daf8e166STejun Heo 	scx_cgroup_lock();
814daf8e166STejun Heo 
815daf8e166STejun Heo 	set_cgroup_sched(sch_cgroup(sch), parent);
816daf8e166STejun Heo 
817daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
818daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
819daf8e166STejun Heo 		struct rq *rq;
820daf8e166STejun Heo 		struct rq_flags rf;
821daf8e166STejun Heo 
822daf8e166STejun Heo 		/* filter out duplicate visits */
823daf8e166STejun Heo 		if (scx_task_on_sched(parent, p))
824daf8e166STejun Heo 			continue;
825daf8e166STejun Heo 
826daf8e166STejun Heo 		/*
827daf8e166STejun Heo 		 * By the time control reaches here, all descendant schedulers
828daf8e166STejun Heo 		 * should already have been disabled.
829daf8e166STejun Heo 		 */
830daf8e166STejun Heo 		WARN_ON_ONCE(!scx_task_on_sched(sch, p));
831daf8e166STejun Heo 
832daf8e166STejun Heo 		/*
833daf8e166STejun Heo 		 * @p is pinned by the iter: css_task_iter_next() takes a
834daf8e166STejun Heo 		 * reference and holds it until the next iter_next() call, so
835daf8e166STejun Heo 		 * @p->usage is guaranteed > 0.
836daf8e166STejun Heo 		 */
837daf8e166STejun Heo 		get_task_struct(p);
838daf8e166STejun Heo 
839daf8e166STejun Heo 		scx_task_iter_unlock(&sti);
840daf8e166STejun Heo 
841daf8e166STejun Heo 		/*
842daf8e166STejun Heo 		 * $p is READY or ENABLED on @sch. Initialize for $parent,
843daf8e166STejun Heo 		 * disable and exit from @sch, and then switch over to $parent.
844daf8e166STejun Heo 		 *
845daf8e166STejun Heo 		 * If a task fails to initialize for $parent, the only available
846daf8e166STejun Heo 		 * action is disabling $parent too. While this allows disabling
847daf8e166STejun Heo 		 * of a child sched to cause the parent scheduler to fail, the
848daf8e166STejun Heo 		 * failure can only originate from ops.init_task() of the
849daf8e166STejun Heo 		 * parent. A child can't directly affect the parent through its
850daf8e166STejun Heo 		 * own failures.
851daf8e166STejun Heo 		 */
852daf8e166STejun Heo 		ret = __scx_init_task(parent, p, false);
853daf8e166STejun Heo 		if (ret) {
854daf8e166STejun Heo 			scx_fail_parent(sch, p, ret);
855daf8e166STejun Heo 			put_task_struct(p);
856daf8e166STejun Heo 			break;
857daf8e166STejun Heo 		}
858daf8e166STejun Heo 
859daf8e166STejun Heo 		rq = task_rq_lock(p, &rf);
860daf8e166STejun Heo 
861daf8e166STejun Heo 		if (scx_get_task_state(p) == SCX_TASK_DEAD) {
862daf8e166STejun Heo 			/*
863daf8e166STejun Heo 			 * sched_ext_dead() raced us between __scx_init_task()
864daf8e166STejun Heo 			 * and this rq lock and ran exit_task() on @sch (the
865daf8e166STejun Heo 			 * sched @p was on at that point), not on $parent.
866daf8e166STejun Heo 			 * $parent's just-completed init is owed an exit_task()
867daf8e166STejun Heo 			 * and we issue it here.
868daf8e166STejun Heo 			 */
869daf8e166STejun Heo 			scx_sub_init_cancel_task(parent, p);
870daf8e166STejun Heo 			task_rq_unlock(rq, p, &rf);
871daf8e166STejun Heo 			put_task_struct(p);
872daf8e166STejun Heo 			continue;
873daf8e166STejun Heo 		}
874daf8e166STejun Heo 
875daf8e166STejun Heo 		scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
876daf8e166STejun Heo 			/*
877daf8e166STejun Heo 			 * $p is initialized for $parent and still attached to
878daf8e166STejun Heo 			 * @sch. Disable and exit for @sch, switch over to
879daf8e166STejun Heo 			 * $parent, override the state to READY to account for
880daf8e166STejun Heo 			 * $p having already been initialized, and then enable.
881daf8e166STejun Heo 			 */
882daf8e166STejun Heo 			scx_disable_and_exit_task(sch, p);
883daf8e166STejun Heo 			scx_set_task_state(p, SCX_TASK_INIT_BEGIN);
884daf8e166STejun Heo 			scx_set_task_state(p, SCX_TASK_INIT);
885daf8e166STejun Heo 			scx_set_task_sched(p, parent);
886daf8e166STejun Heo 			scx_set_task_state(p, SCX_TASK_READY);
887daf8e166STejun Heo 			scx_enable_task(parent, p);
888daf8e166STejun Heo 		}
889daf8e166STejun Heo 
890daf8e166STejun Heo 		task_rq_unlock(rq, p, &rf);
891daf8e166STejun Heo 		put_task_struct(p);
892daf8e166STejun Heo 	}
893daf8e166STejun Heo 	scx_task_iter_stop(&sti);
894daf8e166STejun Heo 
895daf8e166STejun Heo 	scx_disable_dump(sch);
896daf8e166STejun Heo 
897daf8e166STejun Heo 	scx_cgroup_unlock();
898daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
899daf8e166STejun Heo 
900daf8e166STejun Heo 	/*
901daf8e166STejun Heo 	 * All tasks are moved off of @sch but there may still be on-going
902daf8e166STejun Heo 	 * operations (e.g. ops.select_cpu()). Drain them by flushing RCU. Use
903daf8e166STejun Heo 	 * the expedited version as ancestors may be waiting in bypass mode.
904daf8e166STejun Heo 	 * Also, tell the parent that there is no need to keep running bypass
905daf8e166STejun Heo 	 * DSQs for us.
906daf8e166STejun Heo 	 */
907daf8e166STejun Heo 	synchronize_rcu_expedited();
908daf8e166STejun Heo 	scx_disable_bypass_dsp(sch);
909daf8e166STejun Heo 
910daf8e166STejun Heo 	scx_unlink_sched(sch);
911daf8e166STejun Heo 
912daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
913daf8e166STejun Heo 
914daf8e166STejun Heo 	/*
915daf8e166STejun Heo 	 * @sch is now unlinked from the parent's children list. Notify and call
916daf8e166STejun Heo 	 * ops.sub_detach/exit(). Note that ops.sub_detach/exit() must be called
917daf8e166STejun Heo 	 * after unlinking and releasing all locks. See scx_claim_exit().
918daf8e166STejun Heo 	 */
919daf8e166STejun Heo 	wake_up_all(&scx_unlink_waitq);
920daf8e166STejun Heo 
921daf8e166STejun Heo 	if (parent->ops.sub_detach && sch->sub_attached) {
922daf8e166STejun Heo 		struct scx_sub_detach_args sub_detach_args = {
923daf8e166STejun Heo 			.ops = &sch->ops,
924daf8e166STejun Heo 			.cgroup_path = sch->cgrp_path,
925daf8e166STejun Heo 		};
926daf8e166STejun Heo 		SCX_CALL_OP(parent, sub_detach, NULL,
927daf8e166STejun Heo 			    &sub_detach_args);
928daf8e166STejun Heo 	}
929daf8e166STejun Heo 
930daf8e166STejun Heo 	scx_log_sched_disable(sch);
931daf8e166STejun Heo 
932daf8e166STejun Heo 	if (sch->ops.exit)
933daf8e166STejun Heo 		SCX_CALL_OP(sch, exit, NULL, sch->exit_info);
93481507f14STejun Heo 
93581507f14STejun Heo 	/*
93681507f14STejun Heo 	 * @sch's non-ops programs such as timers and tracers can fire after
93781507f14STejun Heo 	 * ops.exit(). Now that exit is complete, stop scx_prog_sched() from
93881507f14STejun Heo 	 * resolving to @sch and drain in-flight resolvers.
93981507f14STejun Heo 	 */
94081507f14STejun Heo 	WRITE_ONCE(sch->dead, true);
94181507f14STejun Heo 	synchronize_rcu();
94281507f14STejun Heo 
943daf8e166STejun Heo 	if (sch->sub_kset)
944daf8e166STejun Heo 		kobject_del(&sch->sub_kset->kobj);
94580e6adaaSTejun Heo 	/* not added if enable failed before scx_sched_sysfs_add() */
94680e6adaaSTejun Heo 	if (sch->kobj.state_in_sysfs)
947daf8e166STejun Heo 		kobject_del(&sch->kobj);
948daf8e166STejun Heo }
949daf8e166STejun Heo 
950daf8e166STejun Heo /* verify that a scheduler can be attached to @cgrp and return the parent */
951daf8e166STejun Heo static struct scx_sched *find_parent_sched(struct cgroup *cgrp)
952daf8e166STejun Heo {
953daf8e166STejun Heo 	struct scx_sched *parent = cgrp->scx_sched;
954daf8e166STejun Heo 	struct scx_sched *pos;
955daf8e166STejun Heo 
956daf8e166STejun Heo 	lockdep_assert_held(&scx_sched_lock);
957daf8e166STejun Heo 
958daf8e166STejun Heo 	/* can't attach twice to the same cgroup */
959daf8e166STejun Heo 	if (parent->cgrp == cgrp)
960daf8e166STejun Heo 		return ERR_PTR(-EBUSY);
961daf8e166STejun Heo 
962daf8e166STejun Heo 	/* does $parent allow sub-scheds? */
963daf8e166STejun Heo 	if (!parent->ops.sub_attach)
964daf8e166STejun Heo 		return ERR_PTR(-EOPNOTSUPP);
965daf8e166STejun Heo 
966daf8e166STejun Heo 	/* can't insert between $parent and its exiting children */
967daf8e166STejun Heo 	list_for_each_entry(pos, &parent->children, sibling)
968daf8e166STejun Heo 		if (cgroup_is_descendant(pos->cgrp, cgrp))
969daf8e166STejun Heo 			return ERR_PTR(-EBUSY);
970daf8e166STejun Heo 
971daf8e166STejun Heo 	return parent;
972daf8e166STejun Heo }
973daf8e166STejun Heo 
974daf8e166STejun Heo static bool assert_task_ready_or_enabled(struct task_struct *p)
975daf8e166STejun Heo {
976daf8e166STejun Heo 	u32 state = scx_get_task_state(p);
977daf8e166STejun Heo 
978daf8e166STejun Heo 	switch (state) {
979daf8e166STejun Heo 	case SCX_TASK_READY:
980daf8e166STejun Heo 	case SCX_TASK_ENABLED:
981daf8e166STejun Heo 		return true;
982daf8e166STejun Heo 	default:
983daf8e166STejun Heo 		WARN_ONCE(true, "sched_ext: Invalid task state %d for %s[%d] during enabling sub sched",
984daf8e166STejun Heo 			  state, p->comm, p->pid);
985daf8e166STejun Heo 		return false;
986daf8e166STejun Heo 	}
987daf8e166STejun Heo }
988daf8e166STejun Heo 
989daf8e166STejun Heo void scx_sub_enable_workfn(struct kthread_work *work)
990daf8e166STejun Heo {
991daf8e166STejun Heo 	struct scx_enable_cmd *cmd = container_of(work, struct scx_enable_cmd, work);
992daf8e166STejun Heo 	struct sched_ext_ops *ops = cmd->ops;
993daf8e166STejun Heo 	struct cgroup *cgrp;
994daf8e166STejun Heo 	struct scx_sched *parent, *sch;
995daf8e166STejun Heo 	struct scx_task_iter sti;
996daf8e166STejun Heo 	struct task_struct *p;
997daf8e166STejun Heo 	s32 i, ret;
998daf8e166STejun Heo 
999daf8e166STejun Heo 	mutex_lock(&scx_enable_mutex);
1000daf8e166STejun Heo 
1001daf8e166STejun Heo 	if (!scx_enabled()) {
1002daf8e166STejun Heo 		ret = -ENODEV;
1003daf8e166STejun Heo 		goto out_unlock;
1004daf8e166STejun Heo 	}
1005daf8e166STejun Heo 
1006daf8e166STejun Heo 	/* See scx_root_enable_workfn() for the @ops->priv check. */
1007daf8e166STejun Heo 	if (rcu_access_pointer(ops->priv)) {
1008daf8e166STejun Heo 		ret = -EBUSY;
1009daf8e166STejun Heo 		goto out_unlock;
1010daf8e166STejun Heo 	}
1011daf8e166STejun Heo 
1012daf8e166STejun Heo 	cgrp = cgroup_get_from_id(ops->sub_cgroup_id);
1013daf8e166STejun Heo 	if (IS_ERR(cgrp)) {
1014daf8e166STejun Heo 		ret = PTR_ERR(cgrp);
1015daf8e166STejun Heo 		goto out_unlock;
1016daf8e166STejun Heo 	}
1017daf8e166STejun Heo 
1018daf8e166STejun Heo 	raw_spin_lock_irq(&scx_sched_lock);
1019daf8e166STejun Heo 	parent = find_parent_sched(cgrp);
1020daf8e166STejun Heo 	if (IS_ERR(parent)) {
1021daf8e166STejun Heo 		raw_spin_unlock_irq(&scx_sched_lock);
1022daf8e166STejun Heo 		ret = PTR_ERR(parent);
1023daf8e166STejun Heo 		goto out_put_cgrp;
1024daf8e166STejun Heo 	}
1025daf8e166STejun Heo 	kobject_get(&parent->kobj);
1026daf8e166STejun Heo 	raw_spin_unlock_irq(&scx_sched_lock);
1027daf8e166STejun Heo 
1028*8946dbd3STejun Heo 	/*
1029*8946dbd3STejun Heo 	 * Flip the hot-path gates before ops->priv is published - the sub's
1030*8946dbd3STejun Heo 	 * programs can e.g. kick cpus from that point on. The matching dec is
1031*8946dbd3STejun Heo 	 * at the end of scx_sched_free_rcu_work().
1032*8946dbd3STejun Heo 	 */
1033*8946dbd3STejun Heo 	static_branch_inc(&__scx_has_subs);
1034*8946dbd3STejun Heo 
1035daf8e166STejun Heo 	/* scx_alloc_and_add_sched() consumes @cgrp whether it succeeds or not */
1036daf8e166STejun Heo 	sch = scx_alloc_and_add_sched(cmd, cgrp, parent);
1037daf8e166STejun Heo 	kobject_put(&parent->kobj);
1038daf8e166STejun Heo 	if (IS_ERR(sch)) {
1039*8946dbd3STejun Heo 		static_branch_dec(&__scx_has_subs);
1040daf8e166STejun Heo 		ret = PTR_ERR(sch);
1041daf8e166STejun Heo 		goto out_unlock;
1042daf8e166STejun Heo 	}
1043daf8e166STejun Heo 
104486094b95STejun Heo 	/*
104586094b95STejun Heo 	 * Validate before scx_link_sched() publishes @sch, so an invalid sub
104686094b95STejun Heo 	 * never becomes visible with an unallocated pshard.
104786094b95STejun Heo 	 */
104886094b95STejun Heo 	ret = scx_validate_ops(sch, ops);
104986094b95STejun Heo 	if (ret)
105086094b95STejun Heo 		goto err_disable;
105186094b95STejun Heo 
105286094b95STejun Heo 	/*
105386094b95STejun Heo 	 * Allocate pshard[] before scx_link_sched() publishes @sch into the
105486094b95STejun Heo 	 * parent's RCU children list. A concurrent revoke walking the tree
105586094b95STejun Heo 	 * would otherwise dereference sch->pshard[si] while it's still NULL.
105686094b95STejun Heo 	 * Unlike the root path, the cid shard layout is stable at this point.
105786094b95STejun Heo 	 *
105886094b95STejun Heo 	 * scx_alloc_pshards() skips allocation when @sch's arena pool isn't
105986094b95STejun Heo 	 * initialized, so scx_arena_pool_init() must run first.
106086094b95STejun Heo 	 */
106186094b95STejun Heo 	ret = scx_arena_pool_init(sch);
106286094b95STejun Heo 	if (ret)
106386094b95STejun Heo 		goto err_disable;
106486094b95STejun Heo 
106586094b95STejun Heo 	ret = scx_alloc_pshards(sch);
106686094b95STejun Heo 	if (ret)
106786094b95STejun Heo 		goto err_disable;
106886094b95STejun Heo 
1069daf8e166STejun Heo 	ret = scx_link_sched(sch);
1070daf8e166STejun Heo 	if (ret)
1071daf8e166STejun Heo 		goto err_disable;
1072daf8e166STejun Heo 
107380e6adaaSTejun Heo 	ret = scx_sched_sysfs_add(sch);
107480e6adaaSTejun Heo 	if (ret)
107580e6adaaSTejun Heo 		goto err_disable;
107680e6adaaSTejun Heo 
1077daf8e166STejun Heo 	if (sch->level >= SCX_SUB_MAX_DEPTH) {
1078daf8e166STejun Heo 		scx_error(sch, "max nesting depth %d violated",
1079daf8e166STejun Heo 			  SCX_SUB_MAX_DEPTH);
1080daf8e166STejun Heo 		goto err_disable;
1081daf8e166STejun Heo 	}
1082daf8e166STejun Heo 
1083daf8e166STejun Heo 	if (sch->ops.init) {
1084daf8e166STejun Heo 		ret = SCX_CALL_OP_RET(sch, init, NULL);
1085daf8e166STejun Heo 		if (ret) {
1086daf8e166STejun Heo 			ret = scx_ops_sanitize_err(sch, "init", ret);
1087daf8e166STejun Heo 			scx_error(sch, "ops.init() failed (%d)", ret);
1088daf8e166STejun Heo 			goto err_disable;
1089daf8e166STejun Heo 		}
1090daf8e166STejun Heo 		sch->exit_info->flags |= SCX_EFLAG_INITIALIZED;
1091daf8e166STejun Heo 	}
1092daf8e166STejun Heo 
1093daf8e166STejun Heo 	ret = scx_set_cmask_scratch_alloc(sch);
1094daf8e166STejun Heo 	if (ret)
1095daf8e166STejun Heo 		goto err_disable;
1096daf8e166STejun Heo 
1097daf8e166STejun Heo 	struct scx_sub_attach_args sub_attach_args = {
1098daf8e166STejun Heo 		.ops = &sch->ops,
1099daf8e166STejun Heo 		.cgroup_path = sch->cgrp_path,
1100daf8e166STejun Heo 	};
1101daf8e166STejun Heo 
1102daf8e166STejun Heo 	ret = SCX_CALL_OP_RET(parent, sub_attach, NULL,
1103daf8e166STejun Heo 			      &sub_attach_args);
1104daf8e166STejun Heo 	if (ret) {
1105daf8e166STejun Heo 		ret = scx_ops_sanitize_err(sch, "sub_attach", ret);
1106daf8e166STejun Heo 		scx_error(sch, "parent rejected (%d)", ret);
1107daf8e166STejun Heo 		goto err_disable;
1108daf8e166STejun Heo 	}
1109daf8e166STejun Heo 	sch->sub_attached = true;
1110daf8e166STejun Heo 
1111daf8e166STejun Heo 	scx_bypass(sch, true);
1112daf8e166STejun Heo 
1113daf8e166STejun Heo 	for (i = SCX_OPI_BEGIN; i < SCX_OPI_END; i++)
1114daf8e166STejun Heo 		if (((void (**)(void))ops)[i])
1115daf8e166STejun Heo 			set_bit(i, sch->has_op);
1116daf8e166STejun Heo 
1117daf8e166STejun Heo 	percpu_down_write(&scx_fork_rwsem);
1118daf8e166STejun Heo 	scx_cgroup_lock();
1119daf8e166STejun Heo 
1120daf8e166STejun Heo 	/*
1121daf8e166STejun Heo 	 * Set cgroup->scx_sched's and check CSS_ONLINE. Either we see
1122daf8e166STejun Heo 	 * !CSS_ONLINE or scx_cgroup_lifetime_notify() sees and shoots us down.
1123daf8e166STejun Heo 	 */
1124daf8e166STejun Heo 	set_cgroup_sched(sch_cgroup(sch), sch);
1125daf8e166STejun Heo 	if (!(cgrp->self.flags & CSS_ONLINE)) {
1126daf8e166STejun Heo 		scx_error(sch, "cgroup is not online");
1127daf8e166STejun Heo 		goto err_unlock_and_disable;
1128daf8e166STejun Heo 	}
1129daf8e166STejun Heo 
1130daf8e166STejun Heo 	/*
1131daf8e166STejun Heo 	 * Initialize tasks for the new child $sch without exiting them for
1132daf8e166STejun Heo 	 * $parent so that the tasks can always be reverted back to $parent
1133daf8e166STejun Heo 	 * sched on child init failure.
1134daf8e166STejun Heo 	 */
1135daf8e166STejun Heo 	WARN_ON_ONCE(scx_enabling_sub_sched);
1136daf8e166STejun Heo 	scx_enabling_sub_sched = sch;
1137daf8e166STejun Heo 
1138daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
1139daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
1140daf8e166STejun Heo 		struct rq *rq;
1141daf8e166STejun Heo 		struct rq_flags rf;
1142daf8e166STejun Heo 
1143daf8e166STejun Heo 		/*
1144daf8e166STejun Heo 		 * Task iteration may visit the same task twice when racing
1145daf8e166STejun Heo 		 * against exiting. Use %SCX_TASK_SUB_INIT to mark tasks which
1146daf8e166STejun Heo 		 * finished __scx_init_task() and skip if set.
1147daf8e166STejun Heo 		 *
1148daf8e166STejun Heo 		 * A task may exit and get freed between __scx_init_task()
1149daf8e166STejun Heo 		 * completion and scx_enable_task(). In such cases,
1150daf8e166STejun Heo 		 * scx_disable_and_exit_task() must exit the task for both the
1151daf8e166STejun Heo 		 * parent and child scheds.
1152daf8e166STejun Heo 		 */
1153daf8e166STejun Heo 		if (p->scx.flags & SCX_TASK_SUB_INIT)
1154daf8e166STejun Heo 			continue;
1155daf8e166STejun Heo 
1156daf8e166STejun Heo 		/* @p is pinned by the iter; see scx_sub_disable() */
1157daf8e166STejun Heo 		get_task_struct(p);
1158daf8e166STejun Heo 
1159daf8e166STejun Heo 		if (!assert_task_ready_or_enabled(p)) {
1160daf8e166STejun Heo 			ret = -EINVAL;
1161daf8e166STejun Heo 			goto abort;
1162daf8e166STejun Heo 		}
1163daf8e166STejun Heo 
1164daf8e166STejun Heo 		scx_task_iter_unlock(&sti);
1165daf8e166STejun Heo 
1166daf8e166STejun Heo 		/*
1167daf8e166STejun Heo 		 * As $p is still on $parent, it can't be transitioned to INIT.
1168daf8e166STejun Heo 		 * Let's worry about task state later. Use __scx_init_task().
1169daf8e166STejun Heo 		 */
1170daf8e166STejun Heo 		ret = __scx_init_task(sch, p, false);
1171daf8e166STejun Heo 		if (ret)
1172daf8e166STejun Heo 			goto abort;
1173daf8e166STejun Heo 
1174daf8e166STejun Heo 		rq = task_rq_lock(p, &rf);
1175daf8e166STejun Heo 
1176daf8e166STejun Heo 		if (scx_get_task_state(p) == SCX_TASK_DEAD) {
1177daf8e166STejun Heo 			/*
1178daf8e166STejun Heo 			 * sched_ext_dead() raced us between __scx_init_task()
1179daf8e166STejun Heo 			 * and this rq lock and ran exit_task() on $parent (the
1180daf8e166STejun Heo 			 * sched @p was on at that point), not on @sch. @sch's
1181daf8e166STejun Heo 			 * just-completed init is owed an exit_task() and we
1182daf8e166STejun Heo 			 * issue it here.
1183daf8e166STejun Heo 			 */
1184daf8e166STejun Heo 			scx_sub_init_cancel_task(sch, p);
1185daf8e166STejun Heo 			task_rq_unlock(rq, p, &rf);
1186daf8e166STejun Heo 			put_task_struct(p);
1187daf8e166STejun Heo 			continue;
1188daf8e166STejun Heo 		}
1189daf8e166STejun Heo 
1190daf8e166STejun Heo 		p->scx.flags |= SCX_TASK_SUB_INIT;
1191daf8e166STejun Heo 		task_rq_unlock(rq, p, &rf);
1192daf8e166STejun Heo 
1193daf8e166STejun Heo 		put_task_struct(p);
1194daf8e166STejun Heo 	}
1195daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1196daf8e166STejun Heo 
1197daf8e166STejun Heo 	/*
1198daf8e166STejun Heo 	 * All tasks are prepped. Disable/exit tasks for $parent and enable for
1199daf8e166STejun Heo 	 * the new @sch.
1200daf8e166STejun Heo 	 */
1201daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
1202daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
1203daf8e166STejun Heo 		/*
1204daf8e166STejun Heo 		 * Use clearing of %SCX_TASK_SUB_INIT to detect and skip
1205daf8e166STejun Heo 		 * duplicate iterations.
1206daf8e166STejun Heo 		 */
1207daf8e166STejun Heo 		if (!(p->scx.flags & SCX_TASK_SUB_INIT))
1208daf8e166STejun Heo 			continue;
1209daf8e166STejun Heo 
1210daf8e166STejun Heo 		scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
1211daf8e166STejun Heo 			/*
1212daf8e166STejun Heo 			 * $p must be either READY or ENABLED. If ENABLED,
1213daf8e166STejun Heo 			 * __scx_disabled_and_exit_task() first disables and
1214daf8e166STejun Heo 			 * makes it READY. However, after exiting $p, it will
1215daf8e166STejun Heo 			 * leave $p as READY.
1216daf8e166STejun Heo 			 */
1217daf8e166STejun Heo 			assert_task_ready_or_enabled(p);
1218daf8e166STejun Heo 			__scx_disable_and_exit_task(parent, p);
1219daf8e166STejun Heo 
1220daf8e166STejun Heo 			/*
1221daf8e166STejun Heo 			 * $p is now only initialized for @sch and READY, which
1222daf8e166STejun Heo 			 * is what we want. Assign it to @sch and enable.
1223daf8e166STejun Heo 			 */
1224daf8e166STejun Heo 			scx_set_task_sched(p, sch);
1225daf8e166STejun Heo 			scx_enable_task(sch, p);
1226daf8e166STejun Heo 
1227daf8e166STejun Heo 			p->scx.flags &= ~SCX_TASK_SUB_INIT;
1228daf8e166STejun Heo 		}
1229daf8e166STejun Heo 	}
1230daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1231daf8e166STejun Heo 
1232daf8e166STejun Heo 	scx_enabling_sub_sched = NULL;
1233daf8e166STejun Heo 
1234daf8e166STejun Heo 	scx_cgroup_unlock();
1235daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
1236daf8e166STejun Heo 
1237daf8e166STejun Heo 	scx_bypass(sch, false);
1238daf8e166STejun Heo 
12395f2a9a4cSTejun Heo 	/* @sch is enabled; deliver any caps owed since its sub_attach() */
12405f2a9a4cSTejun Heo 	scx_sub_seed_caps(sch);
12415f2a9a4cSTejun Heo 
1242daf8e166STejun Heo 	pr_info("sched_ext: BPF sub-scheduler \"%s\" enabled\n", sch->ops.name);
1243daf8e166STejun Heo 	kobject_uevent(&sch->kobj, KOBJ_ADD);
1244daf8e166STejun Heo 	ret = 0;
1245daf8e166STejun Heo 	goto out_unlock;
1246daf8e166STejun Heo 
1247daf8e166STejun Heo out_put_cgrp:
1248daf8e166STejun Heo 	cgroup_put(cgrp);
1249daf8e166STejun Heo out_unlock:
1250daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
1251daf8e166STejun Heo 	cmd->ret = ret;
1252daf8e166STejun Heo 	return;
1253daf8e166STejun Heo 
1254daf8e166STejun Heo abort:
1255daf8e166STejun Heo 	put_task_struct(p);
1256daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1257daf8e166STejun Heo 
1258daf8e166STejun Heo 	/*
1259daf8e166STejun Heo 	 * Undo __scx_init_task() for tasks we marked. scx_enable_task() never
1260daf8e166STejun Heo 	 * ran for @sch on them, so calling scx_disable_task() here would invoke
1261daf8e166STejun Heo 	 * ops.disable() without a matching ops.enable(). scx_enabling_sub_sched
1262daf8e166STejun Heo 	 * must stay set until SUB_INIT is cleared from every marked task -
1263daf8e166STejun Heo 	 * scx_disable_and_exit_task() reads it when a task exits concurrently.
1264daf8e166STejun Heo 	 */
1265daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
1266daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
1267daf8e166STejun Heo 		if (p->scx.flags & SCX_TASK_SUB_INIT) {
1268daf8e166STejun Heo 			scx_sub_init_cancel_task(sch, p);
1269daf8e166STejun Heo 			p->scx.flags &= ~SCX_TASK_SUB_INIT;
1270daf8e166STejun Heo 		}
1271daf8e166STejun Heo 	}
1272daf8e166STejun Heo 	scx_task_iter_stop(&sti);
1273daf8e166STejun Heo 	scx_enabling_sub_sched = NULL;
1274daf8e166STejun Heo err_unlock_and_disable:
1275daf8e166STejun Heo 	/* we'll soon enter disable path, keep bypass on */
1276daf8e166STejun Heo 	scx_cgroup_unlock();
1277daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
1278daf8e166STejun Heo err_disable:
1279daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
1280ad45691dSTejun Heo 	/*
1281ad45691dSTejun Heo 	 * Some enable failures only return an errno (e.g. -ENOMEM from an
1282ad45691dSTejun Heo 	 * allocation) without calling scx_error(). Record it so
1283ad45691dSTejun Heo 	 * scx_flush_disable_work() runs the disable and ops.exit() fires.
1284ad45691dSTejun Heo 	 */
1285ad45691dSTejun Heo 	scx_error(sch, "scx_sub_enable() failed (%d)", ret);
1286daf8e166STejun Heo 	scx_flush_disable_work(sch);
1287daf8e166STejun Heo 	cmd->ret = 0;
1288daf8e166STejun Heo }
1289daf8e166STejun Heo 
1290daf8e166STejun Heo static s32 scx_cgroup_lifetime_notify(struct notifier_block *nb,
1291daf8e166STejun Heo 				      unsigned long action, void *data)
1292daf8e166STejun Heo {
1293daf8e166STejun Heo 	struct cgroup *cgrp = data;
1294daf8e166STejun Heo 	struct cgroup *parent = cgroup_parent(cgrp);
1295daf8e166STejun Heo 
1296daf8e166STejun Heo 	if (!cgroup_on_dfl(cgrp))
1297daf8e166STejun Heo 		return NOTIFY_OK;
1298daf8e166STejun Heo 
1299daf8e166STejun Heo 	switch (action) {
1300daf8e166STejun Heo 	case CGROUP_LIFETIME_ONLINE:
1301daf8e166STejun Heo 		/* inherit ->scx_sched from $parent */
1302daf8e166STejun Heo 		if (parent)
1303daf8e166STejun Heo 			rcu_assign_pointer(cgrp->scx_sched, parent->scx_sched);
1304daf8e166STejun Heo 		break;
1305daf8e166STejun Heo 	case CGROUP_LIFETIME_OFFLINE:
1306daf8e166STejun Heo 		/* if there is a sched attached, shoot it down */
1307daf8e166STejun Heo 		if (cgrp->scx_sched && cgrp->scx_sched->cgrp == cgrp)
1308daf8e166STejun Heo 			scx_exit(cgrp->scx_sched, SCX_EXIT_UNREG_KERN,
1309daf8e166STejun Heo 				 SCX_ECODE_RSN_CGROUP_OFFLINE,
1310daf8e166STejun Heo 				 "cgroup %llu going offline", cgroup_id(cgrp));
1311daf8e166STejun Heo 		break;
1312daf8e166STejun Heo 	}
1313daf8e166STejun Heo 
1314daf8e166STejun Heo 	return NOTIFY_OK;
1315daf8e166STejun Heo }
1316daf8e166STejun Heo 
1317daf8e166STejun Heo static struct notifier_block scx_cgroup_lifetime_nb = {
1318daf8e166STejun Heo 	.notifier_call = scx_cgroup_lifetime_notify,
1319daf8e166STejun Heo };
1320daf8e166STejun Heo 
1321daf8e166STejun Heo static s32 __init scx_cgroup_lifetime_notifier_init(void)
1322daf8e166STejun Heo {
1323daf8e166STejun Heo 	return blocking_notifier_chain_register(&cgroup_lifetime_notifier,
1324daf8e166STejun Heo 						&scx_cgroup_lifetime_nb);
1325daf8e166STejun Heo }
1326daf8e166STejun Heo core_initcall(scx_cgroup_lifetime_notifier_init);
1327daf8e166STejun Heo 
13285f2a9a4cSTejun Heo static void scx_pstack_recursion(struct bpf_prog *prog, const char *op)
1329daf8e166STejun Heo {
1330daf8e166STejun Heo 	struct scx_sched *sch;
1331daf8e166STejun Heo 
1332daf8e166STejun Heo 	guard(rcu)();
1333daf8e166STejun Heo 	sch = scx_prog_sched(prog->aux);
1334daf8e166STejun Heo 	if (unlikely(!sch))
1335daf8e166STejun Heo 		return;
1336daf8e166STejun Heo 
13375f2a9a4cSTejun Heo 	scx_error(sch, "%s recursion detected", op);
13385f2a9a4cSTejun Heo }
13395f2a9a4cSTejun Heo 
13405f2a9a4cSTejun Heo void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog)
13415f2a9a4cSTejun Heo {
13425f2a9a4cSTejun Heo 	scx_pstack_recursion(prog, "dispatch");
13435f2a9a4cSTejun Heo }
13445f2a9a4cSTejun Heo 
13455f2a9a4cSTejun Heo void scx_pstack_recursion_on_caps_updated(struct bpf_prog *prog)
13465f2a9a4cSTejun Heo {
13475f2a9a4cSTejun Heo 	scx_pstack_recursion(prog, "sub_caps_updated");
1348daf8e166STejun Heo }
1349daf8e166STejun Heo 
1350daf8e166STejun Heo __bpf_kfunc_start_defs();
1351daf8e166STejun Heo 
1352daf8e166STejun Heo /**
1353daf8e166STejun Heo  * scx_bpf_sub_dispatch - Trigger dispatching on a child scheduler
1354daf8e166STejun Heo  * @cgroup_id: cgroup ID of the child scheduler to dispatch
1355daf8e166STejun Heo  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
1356daf8e166STejun Heo  *
1357daf8e166STejun Heo  * Allows a parent scheduler to trigger dispatching on one of its direct
1358daf8e166STejun Heo  * child schedulers. The child scheduler runs its dispatch operation to
1359daf8e166STejun Heo  * move tasks from dispatch queues to the local runqueue.
1360daf8e166STejun Heo  *
1361daf8e166STejun Heo  * Returns: true on success, false if cgroup_id is invalid, not a direct
1362daf8e166STejun Heo  * child, or caller lacks dispatch permission.
1363daf8e166STejun Heo  */
1364daf8e166STejun Heo __bpf_kfunc bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux)
1365daf8e166STejun Heo {
1366daf8e166STejun Heo 	struct rq *this_rq = this_rq();
1367daf8e166STejun Heo 	struct scx_sched *parent, *child;
1368daf8e166STejun Heo 
1369daf8e166STejun Heo 	guard(rcu)();
1370daf8e166STejun Heo 	parent = scx_prog_sched(aux);
1371daf8e166STejun Heo 	if (unlikely(!parent))
1372daf8e166STejun Heo 		return false;
1373daf8e166STejun Heo 
1374daf8e166STejun Heo 	child = scx_find_sub_sched(cgroup_id);
1375daf8e166STejun Heo 
1376daf8e166STejun Heo 	if (unlikely(!child))
1377daf8e166STejun Heo 		return false;
1378daf8e166STejun Heo 
1379daf8e166STejun Heo 	if (unlikely(scx_parent(child) != parent)) {
1380daf8e166STejun Heo 		scx_error(parent, "trying to dispatch a distant sub-sched on cgroup %llu",
1381daf8e166STejun Heo 			  cgroup_id);
1382daf8e166STejun Heo 		return false;
1383daf8e166STejun Heo 	}
1384daf8e166STejun Heo 
1385147d1885STejun Heo 	/*
1386147d1885STejun Heo 	 * Skip a child that does not effectively hold the base cap on this cpu:
1387147d1885STejun Heo 	 * its inserts would only be rejected. ecaps are synced at the top of
1388147d1885STejun Heo 	 * balance_one() before dispatch, so this reflects the in-effect state.
1389147d1885STejun Heo 	 */
1390147d1885STejun Heo 	if (scx_missing_caps(child, cpu_of(this_rq), SCX_CAP_BASE))
1391147d1885STejun Heo 		return false;
1392147d1885STejun Heo 
1393daf8e166STejun Heo 	return scx_dispatch_sched(child, this_rq, this_rq->scx.sub_dispatch_prev,
1394daf8e166STejun Heo 				  true);
1395daf8e166STejun Heo }
1396daf8e166STejun Heo 
139786094b95STejun Heo /* Validate common inputs. On success, *parent_out and *child_out are set. */
139886094b95STejun Heo static s32 sub_cap_preamble(u64 cgroup_id, u64 caps, const struct bpf_prog_aux *aux,
139986094b95STejun Heo 			    struct scx_sched **parent_out, struct scx_sched **child_out)
140086094b95STejun Heo {
140186094b95STejun Heo 	struct scx_sched *parent, *child;
140286094b95STejun Heo 
140386094b95STejun Heo 	parent = scx_prog_sched(aux);
140486094b95STejun Heo 	if (unlikely(!parent))
140586094b95STejun Heo 		return -ENODEV;
140686094b95STejun Heo 
140786094b95STejun Heo 	if (!scx_is_cid_type()) {
140886094b95STejun Heo 		scx_error(parent, "sub-cap kfuncs require a cid-form scheduler");
140986094b95STejun Heo 		return -EOPNOTSUPP;
141086094b95STejun Heo 	}
141186094b95STejun Heo 
141286094b95STejun Heo 	child = scx_find_sub_sched(cgroup_id);
141386094b95STejun Heo 	if (unlikely(!child))
141486094b95STejun Heo 		return -ENODEV;
141586094b95STejun Heo 
141686094b95STejun Heo 	if (unlikely(scx_parent(child) != parent)) {
141786094b95STejun Heo 		scx_error(parent, "%s: sub-%llu is not a direct child",
141886094b95STejun Heo 			  parent->cgrp_path, cgroup_id);
141986094b95STejun Heo 		return -EINVAL;
142086094b95STejun Heo 	}
142186094b95STejun Heo 
142286094b95STejun Heo 	if (unlikely(caps & ~__SCX_CAP_ALL)) {
142386094b95STejun Heo 		scx_error(parent, "invalid caps 0x%llx", caps);
142486094b95STejun Heo 		return -EINVAL;
142586094b95STejun Heo 	}
142686094b95STejun Heo 
142786094b95STejun Heo 	*parent_out = parent;
142886094b95STejun Heo 	*child_out = child;
142986094b95STejun Heo 	return 0;
143086094b95STejun Heo }
143186094b95STejun Heo 
143286094b95STejun Heo /**
143386094b95STejun Heo  * scx_bpf_sub_grant - Grant @caps on @cmask__ign's cids to a direct child
143486094b95STejun Heo  * @cgroup_id: cgroup id of the direct child sub-sched
143586094b95STejun Heo  * @caps: bitmask of SCX_CAP_* to grant
143686094b95STejun Heo  * @cmask__ign: cid cmask to grant @caps on (arena pointer)
143786094b95STejun Heo  * @denied_out__ign: optional arena cmask accumulating refused cids
143886094b95STejun Heo  * @aux: implicit BPF argument
143986094b95STejun Heo  *
144086094b95STejun Heo  * A cid in @cmask__ign is granted to the child only if the parent holds every
144186094b95STejun Heo  * requested cap on it. Refused cids are OR'd into @denied_out__ign when
144286094b95STejun Heo  * provided. Refusals outside @denied_out__ign's range are not recorded.
144386094b95STejun Heo  *
144486094b95STejun Heo  * All-or-nothing keeps the caller-visible result binary per cid, so
144586094b95STejun Heo  * @denied_out__ign is one mask to interpret rather than a per-cap matrix.
144686094b95STejun Heo  *
144786094b95STejun Heo  * Return 0 on full success, -EPERM if any cid was refused, or a negative
144886094b95STejun Heo  * errno on other failures.
144986094b95STejun Heo  */
145086094b95STejun Heo __bpf_kfunc s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps,
145186094b95STejun Heo 				  const struct scx_cmask *cmask__ign,
145286094b95STejun Heo 				  struct scx_cmask *denied_out__ign,
145386094b95STejun Heo 				  const struct bpf_prog_aux *aux)
145486094b95STejun Heo {
145586094b95STejun Heo 	struct scx_cmask_ref ref, denied_ref;
145686094b95STejun Heo 	struct scx_sched *parent, *child;
145786094b95STejun Heo 	bool any_denied = false;
14585f2a9a4cSTejun Heo 	LIST_HEAD(to_deliver);
145986094b95STejun Heo 	s32 si, ret;
146086094b95STejun Heo 
146186094b95STejun Heo 	guard(irqsave)();
146286094b95STejun Heo 
146386094b95STejun Heo 	ret = sub_cap_preamble(cgroup_id, caps, aux, &parent, &child);
146486094b95STejun Heo 	if (ret)
146586094b95STejun Heo 		return ret;
146686094b95STejun Heo 
146786094b95STejun Heo 	ret = scx_cmask_ref_init(parent, cmask__ign, &ref);
146886094b95STejun Heo 	if (ret) {
146986094b95STejun Heo 		scx_error(parent, "invalid cmask (%d)", ret);
147086094b95STejun Heo 		return ret;
147186094b95STejun Heo 	}
147286094b95STejun Heo 
147386094b95STejun Heo 	if (denied_out__ign) {
147486094b95STejun Heo 		ret = scx_cmask_ref_init(parent, denied_out__ign, &denied_ref);
147586094b95STejun Heo 		if (ret) {
147686094b95STejun Heo 			scx_error(parent, "invalid denied_out (%d)", ret);
147786094b95STejun Heo 			return ret;
147886094b95STejun Heo 		}
147986094b95STejun Heo 	}
148086094b95STejun Heo 
148186094b95STejun Heo 	/* apply the grant one shard at a time */
148286094b95STejun Heo 	for (si = ref.shard_first; si < ref.shard_end; si++) {
148386094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(slice, 0, SCX_CID_SHARD_MAX_CPUS);
148486094b95STejun Heo 		struct scx_pshard *pps = parent->pshard[si];
148586094b95STejun Heo 		struct scx_pshard *cps = child->pshard[si];
14865f2a9a4cSTejun Heo 		u64 granted_caps = 0;
148786094b95STejun Heo 		u32 cap_bit;
148886094b95STejun Heo 
148986094b95STejun Heo 		scx_cmask_ref_shard(&ref, si, slice);
149086094b95STejun Heo 		if (scx_cmask_empty(slice))
149186094b95STejun Heo 			continue;
149286094b95STejun Heo 
149386094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(granted_cids, slice->base, slice->nr_cids);
14945f2a9a4cSTejun Heo 		SCX_CMASK_DEFINE_SHARD(changed_cids, slice->base, slice->nr_cids);
14955f2a9a4cSTejun Heo 		SCX_CMASK_DEFINE_SHARD(delta, slice->base, slice->nr_cids);
14965f2a9a4cSTejun Heo 
149786094b95STejun Heo 		scx_cmask_copy(granted_cids, slice);
149886094b95STejun Heo 
149986094b95STejun Heo 		scoped_guard (raw_spinlock, &pps->lock) {
150086094b95STejun Heo 			guard(raw_spinlock_nested)(&cps->lock);
150186094b95STejun Heo 
150286094b95STejun Heo 			/*
150386094b95STejun Heo 			 * Narrow granted_cids to cids the parent holds every
150486094b95STejun Heo 			 * requested cap on. All-or-nothing per cid.
150586094b95STejun Heo 			 */
150686094b95STejun Heo 			scx_for_each_cap_bit(cap_bit, caps)
150786094b95STejun Heo 				scx_cmask_and(granted_cids, &pps->caps[cap_bit].cmask);
150886094b95STejun Heo 
15095f2a9a4cSTejun Heo 			/*
15105f2a9a4cSTejun Heo 			 * For each requested cap, fold the newly-set cids into
15115f2a9a4cSTejun Heo 			 * the child and accumulate the delta.
15125f2a9a4cSTejun Heo 			 */
15135f2a9a4cSTejun Heo 			scx_for_each_cap_bit(cap_bit, caps) {
15145f2a9a4cSTejun Heo 				struct scx_cmask *ccm = &cps->caps[cap_bit].cmask;
15155f2a9a4cSTejun Heo 
15165f2a9a4cSTejun Heo 				scx_cmask_copy(delta, granted_cids);
15175f2a9a4cSTejun Heo 				scx_cmask_andnot(delta, ccm);
15185f2a9a4cSTejun Heo 				if (scx_cmask_empty(delta))
15195f2a9a4cSTejun Heo 					continue;
15205f2a9a4cSTejun Heo 
15215f2a9a4cSTejun Heo 				scx_cmask_or(ccm, delta);
15225f2a9a4cSTejun Heo 				scx_cmask_or(changed_cids, delta);
15235f2a9a4cSTejun Heo 				granted_caps |= BIT_U64(cap_bit);
15245f2a9a4cSTejun Heo 			}
15255f2a9a4cSTejun Heo 
152656fdc35bSTejun Heo 			if (granted_caps) {
152756fdc35bSTejun Heo 				s32 cid;
152856fdc35bSTejun Heo 
15295f2a9a4cSTejun Heo 				caps_updated_record(cps, changed_cids, granted_caps,
15305f2a9a4cSTejun Heo 						    &to_deliver);
1531ca3aec45STejun Heo 				/*
1532ca3aec45STejun Heo 				 * The sync arms an update_idle() re-notify if
1533ca3aec45STejun Heo 				 * the cid gains baseline access, so the holder
1534ca3aec45STejun Heo 				 * learns of an already-idle cid.
1535ca3aec45STejun Heo 				 */
153656fdc35bSTejun Heo 				scx_cmask_for_each_cid(cid, changed_cids)
153756fdc35bSTejun Heo 					queue_sync_ecaps(child, cid);
153856fdc35bSTejun Heo 			}
153986094b95STejun Heo 		}
154086094b95STejun Heo 
154186094b95STejun Heo 		/* record cids that didn't make it through into @denied_out */
154286094b95STejun Heo 		if (!scx_cmask_subset(slice, granted_cids)) {
154386094b95STejun Heo 			any_denied = true;
154486094b95STejun Heo 			if (denied_out__ign) {
154586094b95STejun Heo 				SCX_CMASK_DEFINE_SHARD(denied, slice->base, slice->nr_cids);
154686094b95STejun Heo 
154786094b95STejun Heo 				scx_cmask_copy(denied, slice);
154886094b95STejun Heo 				scx_cmask_andnot(denied, granted_cids);
154986094b95STejun Heo 				scx_cmask_ref_or(&denied_ref, denied);
155086094b95STejun Heo 			}
155186094b95STejun Heo 		}
155286094b95STejun Heo 	}
15535f2a9a4cSTejun Heo 
15545f2a9a4cSTejun Heo 	caps_updated_deliver(&to_deliver);
15555f2a9a4cSTejun Heo 
155686094b95STejun Heo 	return any_denied ? -EPERM : 0;
155786094b95STejun Heo }
155886094b95STejun Heo 
155986094b95STejun Heo /**
156086094b95STejun Heo  * scx_bpf_sub_revoke - Revoke @caps on @cmask__ign's cids from @child
156186094b95STejun Heo  * @cgroup_id: cgroup id of the direct child sub-sched
156286094b95STejun Heo  * @caps: bitmask of SCX_CAP_* to revoke
156386094b95STejun Heo  * @cmask__ign: cid cmask to revoke @caps on (arena pointer)
156486094b95STejun Heo  * @aux: implicit BPF argument
156586094b95STejun Heo  *
156686094b95STejun Heo  * Clear @caps bits on @cmask__ign from the child named by @cgroup_id and all
156786094b95STejun Heo  * its descendants. The origin parent's pshard lock is held across the subtree
156886094b95STejun Heo  * walk so a concurrent grant from the origin parent observes the revoked
156986094b95STejun Heo  * state.
157086094b95STejun Heo  */
157186094b95STejun Heo __bpf_kfunc void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps,
157286094b95STejun Heo 				    const struct scx_cmask *cmask__ign,
157386094b95STejun Heo 				    const struct bpf_prog_aux *aux)
157486094b95STejun Heo {
157586094b95STejun Heo 	struct scx_cmask_ref ref;
157686094b95STejun Heo 	struct scx_sched *parent, *child, *pos;
15775f2a9a4cSTejun Heo 	LIST_HEAD(to_deliver);
157886094b95STejun Heo 	s32 si, ret;
157986094b95STejun Heo 
158086094b95STejun Heo 	guard(irqsave)();
158186094b95STejun Heo 
158286094b95STejun Heo 	if (sub_cap_preamble(cgroup_id, caps, aux, &parent, &child))
158386094b95STejun Heo 		return;
158486094b95STejun Heo 
158586094b95STejun Heo 	ret = scx_cmask_ref_init(parent, cmask__ign, &ref);
158686094b95STejun Heo 	if (ret) {
158786094b95STejun Heo 		scx_error(parent, "invalid cmask (%d)", ret);
158886094b95STejun Heo 		return;
158986094b95STejun Heo 	}
159086094b95STejun Heo 
159186094b95STejun Heo 	/* per-shard, walk child's subtree and clear @caps */
159286094b95STejun Heo 	for (si = ref.shard_first; si < ref.shard_end; si++) {
159386094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(slice, 0, SCX_CID_SHARD_MAX_CPUS);
159486094b95STejun Heo 
159586094b95STejun Heo 		scx_cmask_ref_shard(&ref, si, slice);
159686094b95STejun Heo 		if (scx_cmask_empty(slice))
159786094b95STejun Heo 			continue;
159886094b95STejun Heo 
159986094b95STejun Heo 		/*
160086094b95STejun Heo 		 * Pre-order with subtree skip: a descendant that cleared
160186094b95STejun Heo 		 * nothing means no descendant of it can hold @caps on these
160286094b95STejun Heo 		 * cids either.
160386094b95STejun Heo 		 */
160486094b95STejun Heo 		guard(raw_spinlock)(&parent->pshard[si]->lock);
160586094b95STejun Heo 		pos = scx_next_descendant_pre(NULL, child);
160686094b95STejun Heo 		while (pos) {
160786094b95STejun Heo 			struct scx_pshard *ps = pos->pshard[si];
16085f2a9a4cSTejun Heo 			SCX_CMASK_DEFINE_SHARD(changed_cids, slice->base, slice->nr_cids);
16095f2a9a4cSTejun Heo 			SCX_CMASK_DEFINE_SHARD(delta, slice->base, slice->nr_cids);
161086094b95STejun Heo 			u64 revoked_caps = 0;
161186094b95STejun Heo 			u32 cap_bit;
161286094b95STejun Heo 
161386094b95STejun Heo 			scoped_guard (raw_spinlock_nested, &ps->lock) {
16145f2a9a4cSTejun Heo 				/*
16155f2a9a4cSTejun Heo 				 * For each cap, clear lost cids and accumulate
16165f2a9a4cSTejun Heo 				 * the per-cap diff for notification.
16175f2a9a4cSTejun Heo 				 */
161886094b95STejun Heo 				scx_for_each_cap_bit(cap_bit, caps) {
161986094b95STejun Heo 					struct scx_cmask *cm = &ps->caps[cap_bit].cmask;
162086094b95STejun Heo 
16215f2a9a4cSTejun Heo 					scx_cmask_copy(delta, cm);
16225f2a9a4cSTejun Heo 					scx_cmask_and(delta, slice);
16235f2a9a4cSTejun Heo 					if (scx_cmask_empty(delta))
162486094b95STejun Heo 						continue;
16255f2a9a4cSTejun Heo 
16265f2a9a4cSTejun Heo 					scx_cmask_andnot(cm, delta);
16275f2a9a4cSTejun Heo 					scx_cmask_or(changed_cids, delta);
162886094b95STejun Heo 					revoked_caps |= BIT_U64(cap_bit);
162986094b95STejun Heo 				}
16305f2a9a4cSTejun Heo 
163156fdc35bSTejun Heo 				if (revoked_caps) {
163256fdc35bSTejun Heo 					s32 cid;
163356fdc35bSTejun Heo 
16345f2a9a4cSTejun Heo 					caps_updated_record(ps, changed_cids, revoked_caps,
16355f2a9a4cSTejun Heo 							    &to_deliver);
163656fdc35bSTejun Heo 					scx_cmask_for_each_cid(cid, changed_cids)
163756fdc35bSTejun Heo 						queue_sync_ecaps(pos, cid);
163856fdc35bSTejun Heo 				}
163986094b95STejun Heo 			}
164086094b95STejun Heo 
164186094b95STejun Heo 			if (revoked_caps)
164286094b95STejun Heo 				pos = scx_next_descendant_pre(pos, child);
164386094b95STejun Heo 			else
164486094b95STejun Heo 				pos = scx_skip_subtree_pre(pos, child);
164586094b95STejun Heo 		}
164686094b95STejun Heo 	}
16475f2a9a4cSTejun Heo 
16485f2a9a4cSTejun Heo 	caps_updated_deliver(&to_deliver);
164986094b95STejun Heo }
165086094b95STejun Heo 
165186094b95STejun Heo /**
165286094b95STejun Heo  * scx_bpf_sub_caps - Read self's or a direct child's cap cmasks
165386094b95STejun Heo  * @cgroup_id: 0 for self, or a direct child's cgroup id
165486094b95STejun Heo  * @caps: one or more SCX_CAP_* bits
165586094b95STejun Heo  * @out__ign: arena cmask to receive the union of @caps within its range
165686094b95STejun Heo  * @aux: implicit BPF argument
165786094b95STejun Heo  *
165886094b95STejun Heo  * Read the cap cmasks granted on each cid for self (@cgroup_id 0) or a direct
165986094b95STejun Heo  * child - the literal granted set. A sched can read only itself or a direct
166086094b95STejun Heo  * child.
166186094b95STejun Heo  *
166286094b95STejun Heo  * Return 0, -ENODEV if @cgroup_id names no direct child, or -EINVAL on bad
166386094b95STejun Heo  * inputs.
166486094b95STejun Heo  */
166586094b95STejun Heo __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out__ign,
166686094b95STejun Heo 				 const struct bpf_prog_aux *aux)
166786094b95STejun Heo {
166886094b95STejun Heo 	struct scx_cmask_ref ref;
166986094b95STejun Heo 	struct scx_sched *sch, *target;
167086094b95STejun Heo 	struct scx_pshard **pshard;
167186094b95STejun Heo 	s32 si, ret;
167286094b95STejun Heo 
167386094b95STejun Heo 	guard(irqsave)();
167486094b95STejun Heo 
167586094b95STejun Heo 	sch = scx_prog_sched(aux);
167686094b95STejun Heo 	if (unlikely(!sch))
167786094b95STejun Heo 		return -ENODEV;
167886094b95STejun Heo 
167986094b95STejun Heo 	if (!scx_is_cid_type()) {
168086094b95STejun Heo 		scx_error(sch, "sub-cap kfuncs require a cid-form scheduler");
168186094b95STejun Heo 		return -EOPNOTSUPP;
168286094b95STejun Heo 	}
168386094b95STejun Heo 
168486094b95STejun Heo 	if (unlikely(caps & ~__SCX_CAP_ALL)) {
168586094b95STejun Heo 		scx_error(sch, "invalid caps 0x%llx", caps);
168686094b95STejun Heo 		return -EINVAL;
168786094b95STejun Heo 	}
168886094b95STejun Heo 
168986094b95STejun Heo 	/* @cgroup_id 0 reads self, otherwise a direct child */
169086094b95STejun Heo 	if (cgroup_id) {
169186094b95STejun Heo 		target = scx_find_sub_sched(cgroup_id);
169286094b95STejun Heo 		if (unlikely(!target))
169386094b95STejun Heo 			return -ENODEV;
169486094b95STejun Heo 		if (unlikely(scx_parent(target) != sch)) {
169586094b95STejun Heo 			scx_error(sch, "%s: sub-%llu is not a direct child",
169686094b95STejun Heo 				  sch->cgrp_path, cgroup_id);
169786094b95STejun Heo 			return -EINVAL;
169886094b95STejun Heo 		}
169986094b95STejun Heo 	} else {
170086094b95STejun Heo 		target = sch;
170186094b95STejun Heo 	}
170286094b95STejun Heo 
170386094b95STejun Heo 	/*
170486094b95STejun Heo 	 * The target's caps storage may not be set up yet (e.g. a self-read
170586094b95STejun Heo 	 * during ops.init_cids()). Pairs with the publish in
170686094b95STejun Heo 	 * scx_alloc_pshards(): a non-NULL pshard has every element set.
170786094b95STejun Heo 	 */
170886094b95STejun Heo 	pshard = READ_ONCE(target->pshard);
170986094b95STejun Heo 	if (unlikely(!pshard)) {
171086094b95STejun Heo 		scx_error(sch, "scx_bpf_sub_caps() called before caps storage is initialized");
171186094b95STejun Heo 		return -ENODEV;
171286094b95STejun Heo 	}
171386094b95STejun Heo 
171486094b95STejun Heo 	ret = scx_cmask_ref_init(sch, out__ign, &ref);
171586094b95STejun Heo 	if (ret) {
171686094b95STejun Heo 		scx_error(sch, "invalid out (%d)", ret);
171786094b95STejun Heo 		return ret;
171886094b95STejun Heo 	}
171986094b95STejun Heo 
172086094b95STejun Heo 	for (si = ref.shard_first; si < ref.shard_end; si++) {
172186094b95STejun Heo 		const struct scx_cid_shard *shard = &scx_cid_shard_ranges[si];
172286094b95STejun Heo 		SCX_CMASK_DEFINE_SHARD(local_out, shard->base_cid, shard->nr_cids);
172386094b95STejun Heo 		u32 cap_bit;
172486094b95STejun Heo 
172586094b95STejun Heo 		scx_for_each_cap_bit(cap_bit, caps)
172686094b95STejun Heo 			scx_cmask_or(local_out, &pshard[si]->caps[cap_bit].cmask);
172786094b95STejun Heo 		scx_cmask_ref_copy(&ref, local_out);
172886094b95STejun Heo 	}
172986094b95STejun Heo 	return 0;
173086094b95STejun Heo }
173186094b95STejun Heo 
1732b0a2ca6aSTejun Heo /**
1733b0a2ca6aSTejun Heo  * scx_bpf_sub_kill_bstr - Kill a direct child sub-scheduler
1734b0a2ca6aSTejun Heo  * @cgroup_id: cgroup id of the direct child to kill
1735b0a2ca6aSTejun Heo  * @fmt: reason message format string
1736b0a2ca6aSTejun Heo  * @data: format string parameters packaged using ___bpf_fill() macro
1737b0a2ca6aSTejun Heo  * @data__sz: @data len, must end in '__sz' for the verifier
1738b0a2ca6aSTejun Heo  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
1739b0a2ca6aSTejun Heo  *
1740b0a2ca6aSTejun Heo  * Evict a direct child sub-scheduler, disabling it with the supplied reason.
1741b0a2ca6aSTejun Heo  * The child and its subtree are torn down asynchronously through the usual
1742b0a2ca6aSTejun Heo  * disable path.
1743b0a2ca6aSTejun Heo  *
1744b0a2ca6aSTejun Heo  * Unlike scx_bpf_exit(), no exit code is taken: the child is a separate
1745b0a2ca6aSTejun Heo  * scheduler with its own exit-code semantics, so a code chosen by the parent
1746b0a2ca6aSTejun Heo  * would have no defined meaning. The reason string carries the intent.
1747b0a2ca6aSTejun Heo  *
1748b0a2ca6aSTejun Heo  * Return 0 on success or -ENODEV if @cgroup_id names no sub-scheduler, which
1749b0a2ca6aSTejun Heo  * can race with the child detaching on its own and so is not a scheduler error.
1750b0a2ca6aSTejun Heo  * Naming a sched that exists but is not a direct child aborts the parent.
1751b0a2ca6aSTejun Heo  */
1752b0a2ca6aSTejun Heo __printf(2, 0)
1753b0a2ca6aSTejun Heo __bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
1754b0a2ca6aSTejun Heo 				      unsigned long long *data, u32 data__sz,
1755b0a2ca6aSTejun Heo 				      const struct bpf_prog_aux *aux)
1756b0a2ca6aSTejun Heo {
1757b0a2ca6aSTejun Heo 	struct scx_sched *parent, *child;
1758b0a2ca6aSTejun Heo 	s32 ret;
1759b0a2ca6aSTejun Heo 
1760b0a2ca6aSTejun Heo 	guard(rcu)();
1761b0a2ca6aSTejun Heo 
1762b0a2ca6aSTejun Heo 	parent = scx_prog_sched(aux);
1763b0a2ca6aSTejun Heo 	if (unlikely(!parent))
1764b0a2ca6aSTejun Heo 		return -ENODEV;
1765b0a2ca6aSTejun Heo 
1766b0a2ca6aSTejun Heo 	if (!scx_is_cid_type()) {
1767b0a2ca6aSTejun Heo 		scx_error(parent, "sub-cap kfuncs require a cid-form scheduler");
1768b0a2ca6aSTejun Heo 		return -EOPNOTSUPP;
1769b0a2ca6aSTejun Heo 	}
1770b0a2ca6aSTejun Heo 
1771b0a2ca6aSTejun Heo 	child = scx_find_sub_sched(cgroup_id);
1772b0a2ca6aSTejun Heo 	if (unlikely(!child))
1773b0a2ca6aSTejun Heo 		return -ENODEV;
1774b0a2ca6aSTejun Heo 
1775b0a2ca6aSTejun Heo 	if (unlikely(scx_parent(child) != parent)) {
1776b0a2ca6aSTejun Heo 		scx_error(parent, "%s: sub-%llu is not a direct child",
1777b0a2ca6aSTejun Heo 			  parent->cgrp_path, cgroup_id);
1778b0a2ca6aSTejun Heo 		return -EINVAL;
1779b0a2ca6aSTejun Heo 	}
1780b0a2ca6aSTejun Heo 
1781b0a2ca6aSTejun Heo 	guard(raw_spinlock_irqsave)(&scx_exit_bstr_buf_lock);
1782b0a2ca6aSTejun Heo 	ret = scx_bstr_format(parent, &scx_exit_bstr_buf, fmt, data, data__sz);
1783b0a2ca6aSTejun Heo 	if (ret < 0)
1784b0a2ca6aSTejun Heo 		return ret;
1785b0a2ca6aSTejun Heo 	scx_exit(child, SCX_EXIT_PARENT_KILL, 0, "%s", scx_exit_bstr_buf.line);
1786b0a2ca6aSTejun Heo 	return 0;
1787b0a2ca6aSTejun Heo }
1788b0a2ca6aSTejun Heo 
1789daf8e166STejun Heo __bpf_kfunc_end_defs();
1790daf8e166STejun Heo 
1791daf8e166STejun Heo #endif	/* CONFIG_EXT_SUB_SCHED */
1792