xref: /linux/kernel/sched/ext/sub.c (revision 8dba3bbd63e39c97a4d45aea6e95e00b5d14bc8d)
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"
21daf8e166STejun Heo 
22daf8e166STejun Heo #ifdef CONFIG_EXT_SUB_SCHED
23daf8e166STejun Heo 
24daf8e166STejun Heo /**
25daf8e166STejun Heo  * scx_next_descendant_pre - find the next descendant for pre-order walk
26daf8e166STejun Heo  * @pos: the current position (%NULL to initiate traversal)
27daf8e166STejun Heo  * @root: sched whose descendants to walk
28daf8e166STejun Heo  *
29daf8e166STejun Heo  * To be used by scx_for_each_descendant_pre(). Find the next descendant to
30daf8e166STejun Heo  * visit for pre-order traversal of @root's descendants. @root is included in
31daf8e166STejun Heo  * the iteration and the first node to be visited.
32daf8e166STejun Heo  */
33daf8e166STejun Heo struct scx_sched *scx_next_descendant_pre(struct scx_sched *pos, struct scx_sched *root)
34daf8e166STejun Heo {
35daf8e166STejun Heo 	struct scx_sched *next;
36daf8e166STejun Heo 
37daf8e166STejun Heo 	lockdep_assert(lockdep_is_held(&scx_enable_mutex) ||
38daf8e166STejun Heo 		       lockdep_is_held(&scx_sched_lock));
39daf8e166STejun Heo 
40daf8e166STejun Heo 	/* if first iteration, visit @root */
41daf8e166STejun Heo 	if (!pos)
42daf8e166STejun Heo 		return root;
43daf8e166STejun Heo 
44daf8e166STejun Heo 	/* visit the first child if exists */
45daf8e166STejun Heo 	next = list_first_entry_or_null(&pos->children, struct scx_sched, sibling);
46daf8e166STejun Heo 	if (next)
47daf8e166STejun Heo 		return next;
48daf8e166STejun Heo 
49daf8e166STejun Heo 	/* no child, visit my or the closest ancestor's next sibling */
50daf8e166STejun Heo 	while (pos != root) {
51daf8e166STejun Heo 		if (!list_is_last(&pos->sibling, &scx_parent(pos)->children))
52daf8e166STejun Heo 			return list_next_entry(pos, sibling);
53daf8e166STejun Heo 		pos = scx_parent(pos);
54daf8e166STejun Heo 	}
55daf8e166STejun Heo 
56daf8e166STejun Heo 	return NULL;
57daf8e166STejun Heo }
58daf8e166STejun Heo 
59daf8e166STejun Heo static struct scx_sched *scx_find_sub_sched(u64 cgroup_id)
60daf8e166STejun Heo {
61daf8e166STejun Heo 	return rhashtable_lookup(&scx_sched_hash, &cgroup_id,
62daf8e166STejun Heo 				 scx_sched_hash_params);
63daf8e166STejun Heo }
64daf8e166STejun Heo 
65daf8e166STejun Heo void scx_set_task_sched(struct task_struct *p, struct scx_sched *sch)
66daf8e166STejun Heo {
67daf8e166STejun Heo 	rcu_assign_pointer(p->scx.sched, sch);
68daf8e166STejun Heo }
69daf8e166STejun Heo 
70daf8e166STejun Heo struct cgroup *sch_cgroup(struct scx_sched *sch)
71daf8e166STejun Heo {
72daf8e166STejun Heo 	return sch->cgrp;
73daf8e166STejun Heo }
74daf8e166STejun Heo 
75daf8e166STejun Heo /* for each descendant of @cgrp including self, set ->scx_sched to @sch */
76daf8e166STejun Heo void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch)
77daf8e166STejun Heo {
78daf8e166STejun Heo 	struct cgroup *pos;
79daf8e166STejun Heo 	struct cgroup_subsys_state *css;
80daf8e166STejun Heo 
81daf8e166STejun Heo 	cgroup_for_each_live_descendant_pre(pos, css, cgrp)
82daf8e166STejun Heo 		rcu_assign_pointer(pos->scx_sched, sch);
83daf8e166STejun Heo }
84daf8e166STejun Heo 
85*8dba3bbdSTejun Heo static void free_pshard(struct scx_pshard *pshard)
86*8dba3bbdSTejun Heo {
87*8dba3bbdSTejun Heo 	kfree(pshard);
88*8dba3bbdSTejun Heo }
89*8dba3bbdSTejun Heo 
90*8dba3bbdSTejun Heo void scx_free_pshards(struct scx_sched *sch)
91*8dba3bbdSTejun Heo {
92*8dba3bbdSTejun Heo 	s32 si;
93*8dba3bbdSTejun Heo 
94*8dba3bbdSTejun Heo 	if (!sch->pshard)
95*8dba3bbdSTejun Heo 		return;
96*8dba3bbdSTejun Heo 	for (si = 0; si < sch->nr_pshards; si++)
97*8dba3bbdSTejun Heo 		free_pshard(sch->pshard[si]);
98*8dba3bbdSTejun Heo 	kfree(sch->pshard);
99*8dba3bbdSTejun Heo }
100*8dba3bbdSTejun Heo 
101*8dba3bbdSTejun Heo static struct scx_pshard *alloc_pshard(struct scx_sched *sch, s32 shard_idx, s32 node)
102*8dba3bbdSTejun Heo {
103*8dba3bbdSTejun Heo 	return kzalloc_node(sizeof(struct scx_pshard), GFP_KERNEL, node);
104*8dba3bbdSTejun Heo }
105*8dba3bbdSTejun Heo 
106*8dba3bbdSTejun Heo s32 scx_alloc_pshards(struct scx_sched *sch)
107*8dba3bbdSTejun Heo {
108*8dba3bbdSTejun Heo 	struct scx_pshard **pshard;
109*8dba3bbdSTejun Heo 	s32 si;
110*8dba3bbdSTejun Heo 
111*8dba3bbdSTejun Heo 	if (!sch->is_cid_type || !sch->arena_pool)
112*8dba3bbdSTejun Heo 		return 0;
113*8dba3bbdSTejun Heo 
114*8dba3bbdSTejun Heo 	pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards, GFP_KERNEL);
115*8dba3bbdSTejun Heo 	if (!pshard)
116*8dba3bbdSTejun Heo 		return -ENOMEM;
117*8dba3bbdSTejun Heo 
118*8dba3bbdSTejun Heo 	for (si = 0; si < scx_nr_cid_shards; si++) {
119*8dba3bbdSTejun Heo 		pshard[si] = alloc_pshard(sch, si, scx_shard_node[si]);
120*8dba3bbdSTejun Heo 		if (!pshard[si]) {
121*8dba3bbdSTejun Heo 			while (--si >= 0)
122*8dba3bbdSTejun Heo 				free_pshard(pshard[si]);
123*8dba3bbdSTejun Heo 			kfree(pshard);
124*8dba3bbdSTejun Heo 			return -ENOMEM;
125*8dba3bbdSTejun Heo 		}
126*8dba3bbdSTejun Heo 	}
127*8dba3bbdSTejun Heo 
128*8dba3bbdSTejun Heo 	sch->nr_pshards = scx_nr_cid_shards;
129*8dba3bbdSTejun Heo 	/*
130*8dba3bbdSTejun Heo 	 * Publish only after every entry is built so a reader observing
131*8dba3bbdSTejun Heo 	 * @sch->pshard never sees a partially-filled array. Pair the store
132*8dba3bbdSTejun Heo 	 * with a barrier and READ_ONCE() on the read side.
133*8dba3bbdSTejun Heo 	 */
134*8dba3bbdSTejun Heo 	smp_wmb();
135*8dba3bbdSTejun Heo 	WRITE_ONCE(sch->pshard, pshard);
136*8dba3bbdSTejun Heo 	return 0;
137*8dba3bbdSTejun Heo }
138*8dba3bbdSTejun Heo 
139daf8e166STejun Heo static DECLARE_WAIT_QUEUE_HEAD(scx_unlink_waitq);
140daf8e166STejun Heo 
141daf8e166STejun Heo void drain_descendants(struct scx_sched *sch)
142daf8e166STejun Heo {
143daf8e166STejun Heo 	/*
144daf8e166STejun Heo 	 * Child scheds that finished the critical part of disabling will take
145daf8e166STejun Heo 	 * themselves off @sch->children. Wait for it to drain. As propagation
146daf8e166STejun Heo 	 * is recursive, empty @sch->children means that all proper descendant
147daf8e166STejun Heo 	 * scheds reached unlinking stage.
148daf8e166STejun Heo 	 */
149daf8e166STejun Heo 	wait_event(scx_unlink_waitq, list_empty(&sch->children));
150daf8e166STejun Heo }
151daf8e166STejun Heo 
152daf8e166STejun Heo static void scx_fail_parent(struct scx_sched *sch,
153daf8e166STejun Heo 			    struct task_struct *failed, s32 fail_code)
154daf8e166STejun Heo {
155daf8e166STejun Heo 	struct scx_sched *parent = scx_parent(sch);
156daf8e166STejun Heo 	struct scx_task_iter sti;
157daf8e166STejun Heo 	struct task_struct *p;
158daf8e166STejun Heo 
159daf8e166STejun Heo 	scx_error(parent, "ops.init_task() failed (%d) for %s[%d] while disabling a sub-scheduler",
160daf8e166STejun Heo 		  fail_code, failed->comm, failed->pid);
161daf8e166STejun Heo 
162daf8e166STejun Heo 	/*
163daf8e166STejun Heo 	 * Once $parent is bypassed, it's safe to put SCX_TASK_NONE tasks into
164daf8e166STejun Heo 	 * it. This may cause downstream failures on the BPF side but $parent is
165daf8e166STejun Heo 	 * dying anyway.
166daf8e166STejun Heo 	 */
167daf8e166STejun Heo 	scx_bypass(parent, true);
168daf8e166STejun Heo 
169daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
170daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
171daf8e166STejun Heo 		if (scx_task_on_sched(parent, p))
172daf8e166STejun Heo 			continue;
173daf8e166STejun Heo 
174daf8e166STejun Heo 		scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
175daf8e166STejun Heo 			scx_disable_and_exit_task(sch, p);
176daf8e166STejun Heo 			scx_set_task_sched(p, parent);
177daf8e166STejun Heo 		}
178daf8e166STejun Heo 	}
179daf8e166STejun Heo 	scx_task_iter_stop(&sti);
180daf8e166STejun Heo }
181daf8e166STejun Heo 
182daf8e166STejun Heo void scx_sub_disable(struct scx_sched *sch)
183daf8e166STejun Heo {
184daf8e166STejun Heo 	struct scx_sched *parent = scx_parent(sch);
185daf8e166STejun Heo 	struct scx_task_iter sti;
186daf8e166STejun Heo 	struct task_struct *p;
187daf8e166STejun Heo 	int ret;
188daf8e166STejun Heo 
189daf8e166STejun Heo 	/*
190daf8e166STejun Heo 	 * Guarantee forward progress and wait for descendants to be disabled.
191daf8e166STejun Heo 	 * To limit disruptions, $parent is not bypassed. Tasks are fully
192daf8e166STejun Heo 	 * prepped and then inserted back into $parent.
193daf8e166STejun Heo 	 */
194daf8e166STejun Heo 	scx_bypass(sch, true);
195daf8e166STejun Heo 	drain_descendants(sch);
196daf8e166STejun Heo 
197daf8e166STejun Heo 	/*
198daf8e166STejun Heo 	 * Here, every runnable task is guaranteed to make forward progress and
199daf8e166STejun Heo 	 * we can safely use blocking synchronization constructs. Actually
200daf8e166STejun Heo 	 * disable ops.
201daf8e166STejun Heo 	 */
202daf8e166STejun Heo 	mutex_lock(&scx_enable_mutex);
203daf8e166STejun Heo 	percpu_down_write(&scx_fork_rwsem);
204daf8e166STejun Heo 	scx_cgroup_lock();
205daf8e166STejun Heo 
206daf8e166STejun Heo 	set_cgroup_sched(sch_cgroup(sch), parent);
207daf8e166STejun Heo 
208daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
209daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
210daf8e166STejun Heo 		struct rq *rq;
211daf8e166STejun Heo 		struct rq_flags rf;
212daf8e166STejun Heo 
213daf8e166STejun Heo 		/* filter out duplicate visits */
214daf8e166STejun Heo 		if (scx_task_on_sched(parent, p))
215daf8e166STejun Heo 			continue;
216daf8e166STejun Heo 
217daf8e166STejun Heo 		/*
218daf8e166STejun Heo 		 * By the time control reaches here, all descendant schedulers
219daf8e166STejun Heo 		 * should already have been disabled.
220daf8e166STejun Heo 		 */
221daf8e166STejun Heo 		WARN_ON_ONCE(!scx_task_on_sched(sch, p));
222daf8e166STejun Heo 
223daf8e166STejun Heo 		/*
224daf8e166STejun Heo 		 * @p is pinned by the iter: css_task_iter_next() takes a
225daf8e166STejun Heo 		 * reference and holds it until the next iter_next() call, so
226daf8e166STejun Heo 		 * @p->usage is guaranteed > 0.
227daf8e166STejun Heo 		 */
228daf8e166STejun Heo 		get_task_struct(p);
229daf8e166STejun Heo 
230daf8e166STejun Heo 		scx_task_iter_unlock(&sti);
231daf8e166STejun Heo 
232daf8e166STejun Heo 		/*
233daf8e166STejun Heo 		 * $p is READY or ENABLED on @sch. Initialize for $parent,
234daf8e166STejun Heo 		 * disable and exit from @sch, and then switch over to $parent.
235daf8e166STejun Heo 		 *
236daf8e166STejun Heo 		 * If a task fails to initialize for $parent, the only available
237daf8e166STejun Heo 		 * action is disabling $parent too. While this allows disabling
238daf8e166STejun Heo 		 * of a child sched to cause the parent scheduler to fail, the
239daf8e166STejun Heo 		 * failure can only originate from ops.init_task() of the
240daf8e166STejun Heo 		 * parent. A child can't directly affect the parent through its
241daf8e166STejun Heo 		 * own failures.
242daf8e166STejun Heo 		 */
243daf8e166STejun Heo 		ret = __scx_init_task(parent, p, false);
244daf8e166STejun Heo 		if (ret) {
245daf8e166STejun Heo 			scx_fail_parent(sch, p, ret);
246daf8e166STejun Heo 			put_task_struct(p);
247daf8e166STejun Heo 			break;
248daf8e166STejun Heo 		}
249daf8e166STejun Heo 
250daf8e166STejun Heo 		rq = task_rq_lock(p, &rf);
251daf8e166STejun Heo 
252daf8e166STejun Heo 		if (scx_get_task_state(p) == SCX_TASK_DEAD) {
253daf8e166STejun Heo 			/*
254daf8e166STejun Heo 			 * sched_ext_dead() raced us between __scx_init_task()
255daf8e166STejun Heo 			 * and this rq lock and ran exit_task() on @sch (the
256daf8e166STejun Heo 			 * sched @p was on at that point), not on $parent.
257daf8e166STejun Heo 			 * $parent's just-completed init is owed an exit_task()
258daf8e166STejun Heo 			 * and we issue it here.
259daf8e166STejun Heo 			 */
260daf8e166STejun Heo 			scx_sub_init_cancel_task(parent, p);
261daf8e166STejun Heo 			task_rq_unlock(rq, p, &rf);
262daf8e166STejun Heo 			put_task_struct(p);
263daf8e166STejun Heo 			continue;
264daf8e166STejun Heo 		}
265daf8e166STejun Heo 
266daf8e166STejun Heo 		scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
267daf8e166STejun Heo 			/*
268daf8e166STejun Heo 			 * $p is initialized for $parent and still attached to
269daf8e166STejun Heo 			 * @sch. Disable and exit for @sch, switch over to
270daf8e166STejun Heo 			 * $parent, override the state to READY to account for
271daf8e166STejun Heo 			 * $p having already been initialized, and then enable.
272daf8e166STejun Heo 			 */
273daf8e166STejun Heo 			scx_disable_and_exit_task(sch, p);
274daf8e166STejun Heo 			scx_set_task_state(p, SCX_TASK_INIT_BEGIN);
275daf8e166STejun Heo 			scx_set_task_state(p, SCX_TASK_INIT);
276daf8e166STejun Heo 			scx_set_task_sched(p, parent);
277daf8e166STejun Heo 			scx_set_task_state(p, SCX_TASK_READY);
278daf8e166STejun Heo 			scx_enable_task(parent, p);
279daf8e166STejun Heo 		}
280daf8e166STejun Heo 
281daf8e166STejun Heo 		task_rq_unlock(rq, p, &rf);
282daf8e166STejun Heo 		put_task_struct(p);
283daf8e166STejun Heo 	}
284daf8e166STejun Heo 	scx_task_iter_stop(&sti);
285daf8e166STejun Heo 
286daf8e166STejun Heo 	scx_disable_dump(sch);
287daf8e166STejun Heo 
288daf8e166STejun Heo 	scx_cgroup_unlock();
289daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
290daf8e166STejun Heo 
291daf8e166STejun Heo 	/*
292daf8e166STejun Heo 	 * All tasks are moved off of @sch but there may still be on-going
293daf8e166STejun Heo 	 * operations (e.g. ops.select_cpu()). Drain them by flushing RCU. Use
294daf8e166STejun Heo 	 * the expedited version as ancestors may be waiting in bypass mode.
295daf8e166STejun Heo 	 * Also, tell the parent that there is no need to keep running bypass
296daf8e166STejun Heo 	 * DSQs for us.
297daf8e166STejun Heo 	 */
298daf8e166STejun Heo 	synchronize_rcu_expedited();
299daf8e166STejun Heo 	scx_disable_bypass_dsp(sch);
300daf8e166STejun Heo 
301daf8e166STejun Heo 	scx_unlink_sched(sch);
302daf8e166STejun Heo 
303daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
304daf8e166STejun Heo 
305daf8e166STejun Heo 	/*
306daf8e166STejun Heo 	 * @sch is now unlinked from the parent's children list. Notify and call
307daf8e166STejun Heo 	 * ops.sub_detach/exit(). Note that ops.sub_detach/exit() must be called
308daf8e166STejun Heo 	 * after unlinking and releasing all locks. See scx_claim_exit().
309daf8e166STejun Heo 	 */
310daf8e166STejun Heo 	wake_up_all(&scx_unlink_waitq);
311daf8e166STejun Heo 
312daf8e166STejun Heo 	if (parent->ops.sub_detach && sch->sub_attached) {
313daf8e166STejun Heo 		struct scx_sub_detach_args sub_detach_args = {
314daf8e166STejun Heo 			.ops = &sch->ops,
315daf8e166STejun Heo 			.cgroup_path = sch->cgrp_path,
316daf8e166STejun Heo 		};
317daf8e166STejun Heo 		SCX_CALL_OP(parent, sub_detach, NULL,
318daf8e166STejun Heo 			    &sub_detach_args);
319daf8e166STejun Heo 	}
320daf8e166STejun Heo 
321daf8e166STejun Heo 	scx_log_sched_disable(sch);
322daf8e166STejun Heo 
323daf8e166STejun Heo 	if (sch->ops.exit)
324daf8e166STejun Heo 		SCX_CALL_OP(sch, exit, NULL, sch->exit_info);
325daf8e166STejun Heo 	if (sch->sub_kset)
326daf8e166STejun Heo 		kobject_del(&sch->sub_kset->kobj);
32780e6adaaSTejun Heo 	/* not added if enable failed before scx_sched_sysfs_add() */
32880e6adaaSTejun Heo 	if (sch->kobj.state_in_sysfs)
329daf8e166STejun Heo 		kobject_del(&sch->kobj);
330daf8e166STejun Heo }
331daf8e166STejun Heo 
332daf8e166STejun Heo /* verify that a scheduler can be attached to @cgrp and return the parent */
333daf8e166STejun Heo static struct scx_sched *find_parent_sched(struct cgroup *cgrp)
334daf8e166STejun Heo {
335daf8e166STejun Heo 	struct scx_sched *parent = cgrp->scx_sched;
336daf8e166STejun Heo 	struct scx_sched *pos;
337daf8e166STejun Heo 
338daf8e166STejun Heo 	lockdep_assert_held(&scx_sched_lock);
339daf8e166STejun Heo 
340daf8e166STejun Heo 	/* can't attach twice to the same cgroup */
341daf8e166STejun Heo 	if (parent->cgrp == cgrp)
342daf8e166STejun Heo 		return ERR_PTR(-EBUSY);
343daf8e166STejun Heo 
344daf8e166STejun Heo 	/* does $parent allow sub-scheds? */
345daf8e166STejun Heo 	if (!parent->ops.sub_attach)
346daf8e166STejun Heo 		return ERR_PTR(-EOPNOTSUPP);
347daf8e166STejun Heo 
348daf8e166STejun Heo 	/* can't insert between $parent and its exiting children */
349daf8e166STejun Heo 	list_for_each_entry(pos, &parent->children, sibling)
350daf8e166STejun Heo 		if (cgroup_is_descendant(pos->cgrp, cgrp))
351daf8e166STejun Heo 			return ERR_PTR(-EBUSY);
352daf8e166STejun Heo 
353daf8e166STejun Heo 	return parent;
354daf8e166STejun Heo }
355daf8e166STejun Heo 
356daf8e166STejun Heo static bool assert_task_ready_or_enabled(struct task_struct *p)
357daf8e166STejun Heo {
358daf8e166STejun Heo 	u32 state = scx_get_task_state(p);
359daf8e166STejun Heo 
360daf8e166STejun Heo 	switch (state) {
361daf8e166STejun Heo 	case SCX_TASK_READY:
362daf8e166STejun Heo 	case SCX_TASK_ENABLED:
363daf8e166STejun Heo 		return true;
364daf8e166STejun Heo 	default:
365daf8e166STejun Heo 		WARN_ONCE(true, "sched_ext: Invalid task state %d for %s[%d] during enabling sub sched",
366daf8e166STejun Heo 			  state, p->comm, p->pid);
367daf8e166STejun Heo 		return false;
368daf8e166STejun Heo 	}
369daf8e166STejun Heo }
370daf8e166STejun Heo 
371daf8e166STejun Heo void scx_sub_enable_workfn(struct kthread_work *work)
372daf8e166STejun Heo {
373daf8e166STejun Heo 	struct scx_enable_cmd *cmd = container_of(work, struct scx_enable_cmd, work);
374daf8e166STejun Heo 	struct sched_ext_ops *ops = cmd->ops;
375daf8e166STejun Heo 	struct cgroup *cgrp;
376daf8e166STejun Heo 	struct scx_sched *parent, *sch;
377daf8e166STejun Heo 	struct scx_task_iter sti;
378daf8e166STejun Heo 	struct task_struct *p;
379daf8e166STejun Heo 	s32 i, ret;
380daf8e166STejun Heo 
381daf8e166STejun Heo 	mutex_lock(&scx_enable_mutex);
382daf8e166STejun Heo 
383daf8e166STejun Heo 	if (!scx_enabled()) {
384daf8e166STejun Heo 		ret = -ENODEV;
385daf8e166STejun Heo 		goto out_unlock;
386daf8e166STejun Heo 	}
387daf8e166STejun Heo 
388daf8e166STejun Heo 	/* See scx_root_enable_workfn() for the @ops->priv check. */
389daf8e166STejun Heo 	if (rcu_access_pointer(ops->priv)) {
390daf8e166STejun Heo 		ret = -EBUSY;
391daf8e166STejun Heo 		goto out_unlock;
392daf8e166STejun Heo 	}
393daf8e166STejun Heo 
394daf8e166STejun Heo 	cgrp = cgroup_get_from_id(ops->sub_cgroup_id);
395daf8e166STejun Heo 	if (IS_ERR(cgrp)) {
396daf8e166STejun Heo 		ret = PTR_ERR(cgrp);
397daf8e166STejun Heo 		goto out_unlock;
398daf8e166STejun Heo 	}
399daf8e166STejun Heo 
400daf8e166STejun Heo 	raw_spin_lock_irq(&scx_sched_lock);
401daf8e166STejun Heo 	parent = find_parent_sched(cgrp);
402daf8e166STejun Heo 	if (IS_ERR(parent)) {
403daf8e166STejun Heo 		raw_spin_unlock_irq(&scx_sched_lock);
404daf8e166STejun Heo 		ret = PTR_ERR(parent);
405daf8e166STejun Heo 		goto out_put_cgrp;
406daf8e166STejun Heo 	}
407daf8e166STejun Heo 	kobject_get(&parent->kobj);
408daf8e166STejun Heo 	raw_spin_unlock_irq(&scx_sched_lock);
409daf8e166STejun Heo 
410daf8e166STejun Heo 	/* scx_alloc_and_add_sched() consumes @cgrp whether it succeeds or not */
411daf8e166STejun Heo 	sch = scx_alloc_and_add_sched(cmd, cgrp, parent);
412daf8e166STejun Heo 	kobject_put(&parent->kobj);
413daf8e166STejun Heo 	if (IS_ERR(sch)) {
414daf8e166STejun Heo 		ret = PTR_ERR(sch);
415daf8e166STejun Heo 		goto out_unlock;
416daf8e166STejun Heo 	}
417daf8e166STejun Heo 
418daf8e166STejun Heo 	ret = scx_link_sched(sch);
419daf8e166STejun Heo 	if (ret)
420daf8e166STejun Heo 		goto err_disable;
421daf8e166STejun Heo 
42280e6adaaSTejun Heo 	ret = scx_sched_sysfs_add(sch);
42380e6adaaSTejun Heo 	if (ret)
42480e6adaaSTejun Heo 		goto err_disable;
42580e6adaaSTejun Heo 
426daf8e166STejun Heo 	if (sch->level >= SCX_SUB_MAX_DEPTH) {
427daf8e166STejun Heo 		scx_error(sch, "max nesting depth %d violated",
428daf8e166STejun Heo 			  SCX_SUB_MAX_DEPTH);
429daf8e166STejun Heo 		goto err_disable;
430daf8e166STejun Heo 	}
431daf8e166STejun Heo 
432daf8e166STejun Heo 	if (sch->ops.init) {
433daf8e166STejun Heo 		ret = SCX_CALL_OP_RET(sch, init, NULL);
434daf8e166STejun Heo 		if (ret) {
435daf8e166STejun Heo 			ret = scx_ops_sanitize_err(sch, "init", ret);
436daf8e166STejun Heo 			scx_error(sch, "ops.init() failed (%d)", ret);
437daf8e166STejun Heo 			goto err_disable;
438daf8e166STejun Heo 		}
439daf8e166STejun Heo 		sch->exit_info->flags |= SCX_EFLAG_INITIALIZED;
440daf8e166STejun Heo 	}
441daf8e166STejun Heo 
442daf8e166STejun Heo 	ret = scx_arena_pool_init(sch);
443daf8e166STejun Heo 	if (ret)
444daf8e166STejun Heo 		goto err_disable;
445daf8e166STejun Heo 
446daf8e166STejun Heo 	ret = scx_set_cmask_scratch_alloc(sch);
447daf8e166STejun Heo 	if (ret)
448daf8e166STejun Heo 		goto err_disable;
449daf8e166STejun Heo 
450daf8e166STejun Heo 	if (scx_validate_ops(sch, ops))
451daf8e166STejun Heo 		goto err_disable;
452daf8e166STejun Heo 
453daf8e166STejun Heo 	struct scx_sub_attach_args sub_attach_args = {
454daf8e166STejun Heo 		.ops = &sch->ops,
455daf8e166STejun Heo 		.cgroup_path = sch->cgrp_path,
456daf8e166STejun Heo 	};
457daf8e166STejun Heo 
458daf8e166STejun Heo 	ret = SCX_CALL_OP_RET(parent, sub_attach, NULL,
459daf8e166STejun Heo 			      &sub_attach_args);
460daf8e166STejun Heo 	if (ret) {
461daf8e166STejun Heo 		ret = scx_ops_sanitize_err(sch, "sub_attach", ret);
462daf8e166STejun Heo 		scx_error(sch, "parent rejected (%d)", ret);
463daf8e166STejun Heo 		goto err_disable;
464daf8e166STejun Heo 	}
465daf8e166STejun Heo 	sch->sub_attached = true;
466daf8e166STejun Heo 
467daf8e166STejun Heo 	scx_bypass(sch, true);
468daf8e166STejun Heo 
469daf8e166STejun Heo 	for (i = SCX_OPI_BEGIN; i < SCX_OPI_END; i++)
470daf8e166STejun Heo 		if (((void (**)(void))ops)[i])
471daf8e166STejun Heo 			set_bit(i, sch->has_op);
472daf8e166STejun Heo 
473daf8e166STejun Heo 	percpu_down_write(&scx_fork_rwsem);
474daf8e166STejun Heo 	scx_cgroup_lock();
475daf8e166STejun Heo 
476daf8e166STejun Heo 	/*
477daf8e166STejun Heo 	 * Set cgroup->scx_sched's and check CSS_ONLINE. Either we see
478daf8e166STejun Heo 	 * !CSS_ONLINE or scx_cgroup_lifetime_notify() sees and shoots us down.
479daf8e166STejun Heo 	 */
480daf8e166STejun Heo 	set_cgroup_sched(sch_cgroup(sch), sch);
481daf8e166STejun Heo 	if (!(cgrp->self.flags & CSS_ONLINE)) {
482daf8e166STejun Heo 		scx_error(sch, "cgroup is not online");
483daf8e166STejun Heo 		goto err_unlock_and_disable;
484daf8e166STejun Heo 	}
485daf8e166STejun Heo 
486daf8e166STejun Heo 	/*
487daf8e166STejun Heo 	 * Initialize tasks for the new child $sch without exiting them for
488daf8e166STejun Heo 	 * $parent so that the tasks can always be reverted back to $parent
489daf8e166STejun Heo 	 * sched on child init failure.
490daf8e166STejun Heo 	 */
491daf8e166STejun Heo 	WARN_ON_ONCE(scx_enabling_sub_sched);
492daf8e166STejun Heo 	scx_enabling_sub_sched = sch;
493daf8e166STejun Heo 
494daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
495daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
496daf8e166STejun Heo 		struct rq *rq;
497daf8e166STejun Heo 		struct rq_flags rf;
498daf8e166STejun Heo 
499daf8e166STejun Heo 		/*
500daf8e166STejun Heo 		 * Task iteration may visit the same task twice when racing
501daf8e166STejun Heo 		 * against exiting. Use %SCX_TASK_SUB_INIT to mark tasks which
502daf8e166STejun Heo 		 * finished __scx_init_task() and skip if set.
503daf8e166STejun Heo 		 *
504daf8e166STejun Heo 		 * A task may exit and get freed between __scx_init_task()
505daf8e166STejun Heo 		 * completion and scx_enable_task(). In such cases,
506daf8e166STejun Heo 		 * scx_disable_and_exit_task() must exit the task for both the
507daf8e166STejun Heo 		 * parent and child scheds.
508daf8e166STejun Heo 		 */
509daf8e166STejun Heo 		if (p->scx.flags & SCX_TASK_SUB_INIT)
510daf8e166STejun Heo 			continue;
511daf8e166STejun Heo 
512daf8e166STejun Heo 		/* @p is pinned by the iter; see scx_sub_disable() */
513daf8e166STejun Heo 		get_task_struct(p);
514daf8e166STejun Heo 
515daf8e166STejun Heo 		if (!assert_task_ready_or_enabled(p)) {
516daf8e166STejun Heo 			ret = -EINVAL;
517daf8e166STejun Heo 			goto abort;
518daf8e166STejun Heo 		}
519daf8e166STejun Heo 
520daf8e166STejun Heo 		scx_task_iter_unlock(&sti);
521daf8e166STejun Heo 
522daf8e166STejun Heo 		/*
523daf8e166STejun Heo 		 * As $p is still on $parent, it can't be transitioned to INIT.
524daf8e166STejun Heo 		 * Let's worry about task state later. Use __scx_init_task().
525daf8e166STejun Heo 		 */
526daf8e166STejun Heo 		ret = __scx_init_task(sch, p, false);
527daf8e166STejun Heo 		if (ret)
528daf8e166STejun Heo 			goto abort;
529daf8e166STejun Heo 
530daf8e166STejun Heo 		rq = task_rq_lock(p, &rf);
531daf8e166STejun Heo 
532daf8e166STejun Heo 		if (scx_get_task_state(p) == SCX_TASK_DEAD) {
533daf8e166STejun Heo 			/*
534daf8e166STejun Heo 			 * sched_ext_dead() raced us between __scx_init_task()
535daf8e166STejun Heo 			 * and this rq lock and ran exit_task() on $parent (the
536daf8e166STejun Heo 			 * sched @p was on at that point), not on @sch. @sch's
537daf8e166STejun Heo 			 * just-completed init is owed an exit_task() and we
538daf8e166STejun Heo 			 * issue it here.
539daf8e166STejun Heo 			 */
540daf8e166STejun Heo 			scx_sub_init_cancel_task(sch, p);
541daf8e166STejun Heo 			task_rq_unlock(rq, p, &rf);
542daf8e166STejun Heo 			put_task_struct(p);
543daf8e166STejun Heo 			continue;
544daf8e166STejun Heo 		}
545daf8e166STejun Heo 
546daf8e166STejun Heo 		p->scx.flags |= SCX_TASK_SUB_INIT;
547daf8e166STejun Heo 		task_rq_unlock(rq, p, &rf);
548daf8e166STejun Heo 
549daf8e166STejun Heo 		put_task_struct(p);
550daf8e166STejun Heo 	}
551daf8e166STejun Heo 	scx_task_iter_stop(&sti);
552daf8e166STejun Heo 
553daf8e166STejun Heo 	/*
554daf8e166STejun Heo 	 * All tasks are prepped. Disable/exit tasks for $parent and enable for
555daf8e166STejun Heo 	 * the new @sch.
556daf8e166STejun Heo 	 */
557daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
558daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
559daf8e166STejun Heo 		/*
560daf8e166STejun Heo 		 * Use clearing of %SCX_TASK_SUB_INIT to detect and skip
561daf8e166STejun Heo 		 * duplicate iterations.
562daf8e166STejun Heo 		 */
563daf8e166STejun Heo 		if (!(p->scx.flags & SCX_TASK_SUB_INIT))
564daf8e166STejun Heo 			continue;
565daf8e166STejun Heo 
566daf8e166STejun Heo 		scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) {
567daf8e166STejun Heo 			/*
568daf8e166STejun Heo 			 * $p must be either READY or ENABLED. If ENABLED,
569daf8e166STejun Heo 			 * __scx_disabled_and_exit_task() first disables and
570daf8e166STejun Heo 			 * makes it READY. However, after exiting $p, it will
571daf8e166STejun Heo 			 * leave $p as READY.
572daf8e166STejun Heo 			 */
573daf8e166STejun Heo 			assert_task_ready_or_enabled(p);
574daf8e166STejun Heo 			__scx_disable_and_exit_task(parent, p);
575daf8e166STejun Heo 
576daf8e166STejun Heo 			/*
577daf8e166STejun Heo 			 * $p is now only initialized for @sch and READY, which
578daf8e166STejun Heo 			 * is what we want. Assign it to @sch and enable.
579daf8e166STejun Heo 			 */
580daf8e166STejun Heo 			scx_set_task_sched(p, sch);
581daf8e166STejun Heo 			scx_enable_task(sch, p);
582daf8e166STejun Heo 
583daf8e166STejun Heo 			p->scx.flags &= ~SCX_TASK_SUB_INIT;
584daf8e166STejun Heo 		}
585daf8e166STejun Heo 	}
586daf8e166STejun Heo 	scx_task_iter_stop(&sti);
587daf8e166STejun Heo 
588daf8e166STejun Heo 	scx_enabling_sub_sched = NULL;
589daf8e166STejun Heo 
590daf8e166STejun Heo 	scx_cgroup_unlock();
591daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
592daf8e166STejun Heo 
593daf8e166STejun Heo 	scx_bypass(sch, false);
594daf8e166STejun Heo 
595daf8e166STejun Heo 	pr_info("sched_ext: BPF sub-scheduler \"%s\" enabled\n", sch->ops.name);
596daf8e166STejun Heo 	kobject_uevent(&sch->kobj, KOBJ_ADD);
597daf8e166STejun Heo 	ret = 0;
598daf8e166STejun Heo 	goto out_unlock;
599daf8e166STejun Heo 
600daf8e166STejun Heo out_put_cgrp:
601daf8e166STejun Heo 	cgroup_put(cgrp);
602daf8e166STejun Heo out_unlock:
603daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
604daf8e166STejun Heo 	cmd->ret = ret;
605daf8e166STejun Heo 	return;
606daf8e166STejun Heo 
607daf8e166STejun Heo abort:
608daf8e166STejun Heo 	put_task_struct(p);
609daf8e166STejun Heo 	scx_task_iter_stop(&sti);
610daf8e166STejun Heo 
611daf8e166STejun Heo 	/*
612daf8e166STejun Heo 	 * Undo __scx_init_task() for tasks we marked. scx_enable_task() never
613daf8e166STejun Heo 	 * ran for @sch on them, so calling scx_disable_task() here would invoke
614daf8e166STejun Heo 	 * ops.disable() without a matching ops.enable(). scx_enabling_sub_sched
615daf8e166STejun Heo 	 * must stay set until SUB_INIT is cleared from every marked task -
616daf8e166STejun Heo 	 * scx_disable_and_exit_task() reads it when a task exits concurrently.
617daf8e166STejun Heo 	 */
618daf8e166STejun Heo 	scx_task_iter_start(&sti, sch->cgrp);
619daf8e166STejun Heo 	while ((p = scx_task_iter_next_locked(&sti))) {
620daf8e166STejun Heo 		if (p->scx.flags & SCX_TASK_SUB_INIT) {
621daf8e166STejun Heo 			scx_sub_init_cancel_task(sch, p);
622daf8e166STejun Heo 			p->scx.flags &= ~SCX_TASK_SUB_INIT;
623daf8e166STejun Heo 		}
624daf8e166STejun Heo 	}
625daf8e166STejun Heo 	scx_task_iter_stop(&sti);
626daf8e166STejun Heo 	scx_enabling_sub_sched = NULL;
627daf8e166STejun Heo err_unlock_and_disable:
628daf8e166STejun Heo 	/* we'll soon enter disable path, keep bypass on */
629daf8e166STejun Heo 	scx_cgroup_unlock();
630daf8e166STejun Heo 	percpu_up_write(&scx_fork_rwsem);
631daf8e166STejun Heo err_disable:
632daf8e166STejun Heo 	mutex_unlock(&scx_enable_mutex);
633ad45691dSTejun Heo 	/*
634ad45691dSTejun Heo 	 * Some enable failures only return an errno (e.g. -ENOMEM from an
635ad45691dSTejun Heo 	 * allocation) without calling scx_error(). Record it so
636ad45691dSTejun Heo 	 * scx_flush_disable_work() runs the disable and ops.exit() fires.
637ad45691dSTejun Heo 	 */
638ad45691dSTejun Heo 	scx_error(sch, "scx_sub_enable() failed (%d)", ret);
639daf8e166STejun Heo 	scx_flush_disable_work(sch);
640daf8e166STejun Heo 	cmd->ret = 0;
641daf8e166STejun Heo }
642daf8e166STejun Heo 
643daf8e166STejun Heo static s32 scx_cgroup_lifetime_notify(struct notifier_block *nb,
644daf8e166STejun Heo 				      unsigned long action, void *data)
645daf8e166STejun Heo {
646daf8e166STejun Heo 	struct cgroup *cgrp = data;
647daf8e166STejun Heo 	struct cgroup *parent = cgroup_parent(cgrp);
648daf8e166STejun Heo 
649daf8e166STejun Heo 	if (!cgroup_on_dfl(cgrp))
650daf8e166STejun Heo 		return NOTIFY_OK;
651daf8e166STejun Heo 
652daf8e166STejun Heo 	switch (action) {
653daf8e166STejun Heo 	case CGROUP_LIFETIME_ONLINE:
654daf8e166STejun Heo 		/* inherit ->scx_sched from $parent */
655daf8e166STejun Heo 		if (parent)
656daf8e166STejun Heo 			rcu_assign_pointer(cgrp->scx_sched, parent->scx_sched);
657daf8e166STejun Heo 		break;
658daf8e166STejun Heo 	case CGROUP_LIFETIME_OFFLINE:
659daf8e166STejun Heo 		/* if there is a sched attached, shoot it down */
660daf8e166STejun Heo 		if (cgrp->scx_sched && cgrp->scx_sched->cgrp == cgrp)
661daf8e166STejun Heo 			scx_exit(cgrp->scx_sched, SCX_EXIT_UNREG_KERN,
662daf8e166STejun Heo 				 SCX_ECODE_RSN_CGROUP_OFFLINE,
663daf8e166STejun Heo 				 "cgroup %llu going offline", cgroup_id(cgrp));
664daf8e166STejun Heo 		break;
665daf8e166STejun Heo 	}
666daf8e166STejun Heo 
667daf8e166STejun Heo 	return NOTIFY_OK;
668daf8e166STejun Heo }
669daf8e166STejun Heo 
670daf8e166STejun Heo static struct notifier_block scx_cgroup_lifetime_nb = {
671daf8e166STejun Heo 	.notifier_call = scx_cgroup_lifetime_notify,
672daf8e166STejun Heo };
673daf8e166STejun Heo 
674daf8e166STejun Heo static s32 __init scx_cgroup_lifetime_notifier_init(void)
675daf8e166STejun Heo {
676daf8e166STejun Heo 	return blocking_notifier_chain_register(&cgroup_lifetime_notifier,
677daf8e166STejun Heo 						&scx_cgroup_lifetime_nb);
678daf8e166STejun Heo }
679daf8e166STejun Heo core_initcall(scx_cgroup_lifetime_notifier_init);
680daf8e166STejun Heo 
681daf8e166STejun Heo void scx_pstack_recursion_on_dispatch(struct bpf_prog *prog)
682daf8e166STejun Heo {
683daf8e166STejun Heo 	struct scx_sched *sch;
684daf8e166STejun Heo 
685daf8e166STejun Heo 	guard(rcu)();
686daf8e166STejun Heo 	sch = scx_prog_sched(prog->aux);
687daf8e166STejun Heo 	if (unlikely(!sch))
688daf8e166STejun Heo 		return;
689daf8e166STejun Heo 
690daf8e166STejun Heo 	scx_error(sch, "dispatch recursion detected");
691daf8e166STejun Heo }
692daf8e166STejun Heo 
693daf8e166STejun Heo __bpf_kfunc_start_defs();
694daf8e166STejun Heo 
695daf8e166STejun Heo /**
696daf8e166STejun Heo  * scx_bpf_sub_dispatch - Trigger dispatching on a child scheduler
697daf8e166STejun Heo  * @cgroup_id: cgroup ID of the child scheduler to dispatch
698daf8e166STejun Heo  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
699daf8e166STejun Heo  *
700daf8e166STejun Heo  * Allows a parent scheduler to trigger dispatching on one of its direct
701daf8e166STejun Heo  * child schedulers. The child scheduler runs its dispatch operation to
702daf8e166STejun Heo  * move tasks from dispatch queues to the local runqueue.
703daf8e166STejun Heo  *
704daf8e166STejun Heo  * Returns: true on success, false if cgroup_id is invalid, not a direct
705daf8e166STejun Heo  * child, or caller lacks dispatch permission.
706daf8e166STejun Heo  */
707daf8e166STejun Heo __bpf_kfunc bool scx_bpf_sub_dispatch(u64 cgroup_id, const struct bpf_prog_aux *aux)
708daf8e166STejun Heo {
709daf8e166STejun Heo 	struct rq *this_rq = this_rq();
710daf8e166STejun Heo 	struct scx_sched *parent, *child;
711daf8e166STejun Heo 
712daf8e166STejun Heo 	guard(rcu)();
713daf8e166STejun Heo 	parent = scx_prog_sched(aux);
714daf8e166STejun Heo 	if (unlikely(!parent))
715daf8e166STejun Heo 		return false;
716daf8e166STejun Heo 
717daf8e166STejun Heo 	child = scx_find_sub_sched(cgroup_id);
718daf8e166STejun Heo 
719daf8e166STejun Heo 	if (unlikely(!child))
720daf8e166STejun Heo 		return false;
721daf8e166STejun Heo 
722daf8e166STejun Heo 	if (unlikely(scx_parent(child) != parent)) {
723daf8e166STejun Heo 		scx_error(parent, "trying to dispatch a distant sub-sched on cgroup %llu",
724daf8e166STejun Heo 			  cgroup_id);
725daf8e166STejun Heo 		return false;
726daf8e166STejun Heo 	}
727daf8e166STejun Heo 
728daf8e166STejun Heo 	return scx_dispatch_sched(child, this_rq, this_rq->scx.sub_dispatch_prev,
729daf8e166STejun Heo 				  true);
730daf8e166STejun Heo }
731daf8e166STejun Heo 
732daf8e166STejun Heo __bpf_kfunc_end_defs();
733daf8e166STejun Heo 
734daf8e166STejun Heo #endif	/* CONFIG_EXT_SUB_SCHED */
735