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