Skip to contentSkip to conversation

sched_ext: Clarify CPU context for running/stopping callbacks

PATCH Superseded Andrea Righi 2 participants 2 messages Apr 23, 2025 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: Clarify CPU context for running/stopping callbacks

The ops.running() and ops.stopping() callbacks can be invoked from a CPU other than the one the task is assigned to, particularly during affinity changes, as both scx_next_task_scx() and dequeue_task_scx() may run on CPUs different from the task's target CPU.

This behavior can lead to confusion or incorrect assumptions if not properly clarified, potentially resulting in bugs (see [1]).

Therefore, update the documentation to clarify this aspect and advise users to use scx_bpf_task_cpu() to determine the actual CPU the task will run on or was running on.

[1] https://github.com/sched-ext/scx/pull/1728

Cc Jake Hillion <[email protected]>
Cc Changwoo Min <[email protected]>
---
 kernel/sched/ext.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched/ext.c +8 -0
index a2380a6bba210..f146e678cc261 100644
@@ -373,6 +373,10 @@ struct sched_ext_ops {
 	 * @running: A task is starting to run on its associated CPU
 	 * @p: task starting to run
 	 *
+	 * Note that this callback may be called from a CPU other than the
+	 * one the task is going to run on. Use scx_bpf_task_cpu(@p) to
+	 * determine the target CPU the task is going to use.
+	 *
 	 * See ->runnable() for explanation on the task state notifiers.
 	 */
 	void (*running)(struct task_struct *p);
@@ -382,6 +386,10 @@ struct sched_ext_ops {
 	 * @p: task stopping to run
 	 * @runnable: is task @p still runnable?
 	 *
+	 * Note that this callback may be called from a CPU other than the
+	 * one the task was running on. Use scx_bpf_task_cpu(@p) to
+	 * retrieve the CPU used by the task.
+	 *
 	 * See ->runnable() for explanation on the task state notifiers. If
 	 * !@runnable, ->quiescent() will be invoked after this operation
 	 * returns.
Signature · 1 line
2.49.0
#1 lore raw
Quoted 26 lines · Andrea Righi

The ops.running() and ops.stopping() callbacks can be invoked from a CPU other than the one the task is assigned to, particularly during affinity changes, as both scx_next_task_scx() and dequeue_task_scx() may run on CPUs different from the task's target CPU.

This behavior can lead to confusion or incorrect assumptions if not properly clarified, potentially resulting in bugs (see [1]).

Therefore, update the documentation to clarify this aspect and advise users to use scx_bpf_task_cpu() to determine the actual CPU the task will run on or was running on.

[1] https://github.com/sched-ext/scx/pull/1728

Cc Jake Hillion <[email protected]>
Cc Changwoo Min <[email protected]>
---
 kernel/sched/ext.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched/ext.c +0 -0
index a2380a6bba210..f146e678cc261 100644

kernel/sched/ext.c struct sched_ext_ops { · line 378

Quoted 6 lines
kernel/sched/ext.c +3 -0
@@ -373,6 +373,10 @@ struct sched_ext_ops {
 	 * @running: A task is starting to run on its associated CPU
 	 * @p: task starting to run
 	 *
+	 * Note that this callback may be called from a CPU other than the
+	 * one the task is going to run on. Use scx_bpf_task_cpu(@p) to
+	 * determine the target CPU the task is going to use.

Can you briefly explain the scenario as a part of the comment? Just a couple sentences can go a long way against future head scratches.

Thanks.

Signature · 1 line
tejun