Skip to contentSkip to conversation

sched_ext: Reset runnable_at when a task leaves its scheduler

PATCH Applied Michal Blaszczyk 3 participants 6 messages Oct 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover sched_ext: Reset runnable_at when a task leaves its scheduler

A task that is switched out of SCHED_EXT while it's waiting keeps its p->scx.runnable_at. When it comes back, the watchdog counts the time it spent in another class as waiting, and can abort the scheduler for a stall that didn't happen. Patch 1 restarts the wait when a task leaves its scheduler. Patch 2 adds a selftest that reproduces the spurious stall.

The bug goes back to the watchdog's introduction in v6.12. The series is based on sched_ext/for-7.4, and patch 1 also applies to for-7.3-fixes.

Tested in a virtme-ng VM (16 CPUs, PROVE_LOCKING): class_switch_stall fails every time without patch 1 and passes with it. The other sched_ext selftests give the same results with and without the series (enq_blocked fails in this VM either way).

I used an AI coding assistant to find the bug and to write and test both patches.

Michal Blaszczyk (2):
sched_ext: Reset runnable_at when a task leaves its scheduler selftests/sched_ext: Add class_switch_stall test

 kernel/sched/ext/ext.c                        |  14 +-
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../sched_ext/class_switch_stall.bpf.c        | 121 +++++++++
 .../selftests/sched_ext/class_switch_stall.c  | 240 ++++++++++++++++++
 4 files changed, 375 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/sched_ext/class_switch_stall.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/class_switch_stall.c

base-commit: 1f5dde349ac2fca1f119ae8de3f0d2f1c2a8d338

Signature · 1 line
2.56.0.176.g3575e65682-goog
#1 lore raw

PATCH 1/2 sched_ext: Reset runnable_at when a task leaves its scheduler

The watchdog measures how long a task has been waiting from p->scx.runnable_at, which set_task_runnable() resets when the task becomes runnable after it last ran. ops_dequeue() never asks for a reset, as the dequeue of a waiting task is temporary, e.g. for a migration or an attribute change.

Leaving the scheduler isn't temporary. A task which is switched out of SCHED_EXT while it's waiting keeps its runnable_at. When it's switched back and becomes runnable, its wait appears to have started before it left, and the time it spent in another class counts towards the timeout. If the watchdog checks before the task runs, it aborts the scheduler:

sched_ext: class_switch_stall: runner[418] failed to run for 3.010s

Here, the task waited for a few milliseconds before it was switched to SCHED_OTHER, slept outside of SCHED_EXT for three seconds, and was charged for all of it as soon as it woke up on SCHED_EXT again.

The same happens when scx_sub_enable_workfn() hands a waiting task over to a new sub-scheduler, which can then be blamed for time the task waited under the parent. scx_rehome_task() isn't affected, as it moves the task through SCX_TASK_INIT, which resets runnable_at. The stale runnable_at also skews the default core-sched ordering in scx_prio_less().

Reset runnable_at in scx_disable_task(), when the task leaves the control of its scheduler.

Fixes 8a010b81b3a5 ("sched_ext: Implement runnable task stall watchdog")
Assisted-by LLM
---
 kernel/sched/ext/ext.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
kernel/sched/ext/ext.c +13 -1
index 07857039aee69..120df09b9e0af 100644
@@ -2424,7 +2424,11 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
 	struct scx_sched *sch = scx_task_sched(p);
 	unsigned long opss;
 
-	/* dequeue is always temporary, don't reset runnable_at */
+	/*
+	 * Don't reset runnable_at. If @p is waiting, the wait continues across
+	 * temporary dequeues, e.g. for a migration or an attribute change.
+	 * scx_disable_task() ends it if @p leaves its scheduler.
+	 */
 	clr_task_runnable(p, false);
 
 retry:
@@ -4517,6 +4521,14 @@ static void scx_disable_task(struct scx_sched *sch, struct task_struct *p)
 	scx_set_task_slice(p, 0);
 	p->scx.reenq_cnt = 0;
 
+	/*
+	 * If @p is runnable but not running, its wait started at runnable_at,
+	 * which ops_dequeue() didn't reset. Leaving @sch ends the wait. Reset
+	 * runnable_at the next time @p is enqueued on SCX, so that the watchdog
+	 * doesn't count the time @p spends elsewhere.
+	 */
+	p->scx.flags |= SCX_TASK_RESET_RUNNABLE_AT;
+
 	/*
 	 * Verify the task is not in BPF scheduler's custody. If flag
 	 * transitions are consistent, the flag should always be clear
Signature · 1 line
2.56.0.176.g3575e65682-goog
reviewer Reviewed-by on v1 1/2
#2 lore raw

Hi Michal,

Quoted 24 lines · Michal Blaszczyk

The watchdog measures how long a task has been waiting from p->scx.runnable_at, which set_task_runnable() resets when the task becomes runnable after it last ran. ops_dequeue() never asks for a reset, as the dequeue of a waiting task is temporary, e.g. for a migration or an attribute change.

Leaving the scheduler isn't temporary. A task which is switched out of SCHED_EXT while it's waiting keeps its runnable_at. When it's switched back and becomes runnable, its wait appears to have started before it left, and the time it spent in another class counts towards the timeout. If the watchdog checks before the task runs, it aborts the scheduler:

sched_ext: class_switch_stall: runner[418] failed to run for 3.010s

Here, the task waited for a few milliseconds before it was switched to SCHED_OTHER, slept outside of SCHED_EXT for three seconds, and was charged for all of it as soon as it woke up on SCHED_EXT again.

The same happens when scx_sub_enable_workfn() hands a waiting task over to a new sub-scheduler, which can then be blamed for time the task waited under the parent. scx_rehome_task() isn't affected, as it moves the task through SCX_TASK_INIT, which resets runnable_at. The stale runnable_at also skews the default core-sched ordering in scx_prio_less().

Quoted 6 lines

Reset runnable_at in scx_disable_task(), when the task leaves the control of its scheduler.

Fixes 8a010b81b3a5 ("sched_ext: Implement runnable task stall watchdog")
Assisted-by LLM

This makes sense to me and I reproduced the issue (thanks for providing a selftest).

-Andrea

Quoted 39 lines
---
 kernel/sched/ext/ext.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
kernel/sched/ext/ext.c +13 -1
index 07857039aee69..120df09b9e0af 100644
@@ -2424,7 +2424,11 @@ static void ops_dequeue(struct rq *rq, struct task_struct *p, u64 deq_flags)
 	struct scx_sched *sch = scx_task_sched(p);
 	unsigned long opss;
 
-	/* dequeue is always temporary, don't reset runnable_at */
+	/*
+	 * Don't reset runnable_at. If @p is waiting, the wait continues across
+	 * temporary dequeues, e.g. for a migration or an attribute change.
+	 * scx_disable_task() ends it if @p leaves its scheduler.
+	 */
 	clr_task_runnable(p, false);
 
 retry:
@@ -4517,6 +4521,14 @@ static void scx_disable_task(struct scx_sched *sch, struct task_struct *p)
 	scx_set_task_slice(p, 0);
 	p->scx.reenq_cnt = 0;
 
+	/*
+	 * If @p is runnable but not running, its wait started at runnable_at,
+	 * which ops_dequeue() didn't reset. Leaving @sch ends the wait. Reset
+	 * runnable_at the next time @p is enqueued on SCX, so that the watchdog
+	 * doesn't count the time @p spends elsewhere.
+	 */
+	p->scx.flags |= SCX_TASK_RESET_RUNNABLE_AT;
+
 	/*
 	 * Verify the task is not in BPF scheduler's custody. If flag
 	 * transitions are consistent, the flag should always be clear
Signature · 1 line
2.56.0.176.g3575e65682-goog
#4 lore raw

PATCH 2/2 selftests/sched_ext: Add class_switch_stall test

In reply to Michal Blaszczyk

Add a test for a task which leaves SCHED_EXT while it's waiting. Its scheduler keeps the task waiting on a DSQ after it wakes up. The test switches the task to SCHED_OTHER while it's waiting, lets it sleep outside of SCHED_EXT for three times the watchdog timeout, then switches it back and wakes it up. The scheduler keeps it waiting again, for longer than the watchdog interval but shorter than the timeout, which the watchdog must not report as a stall.

Without the previous patch, the watchdog charges the task for the time it spent outside of SCHED_EXT and aborts the scheduler.

Assisted-by LLM
---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../sched_ext/class_switch_stall.bpf.c        | 121 +++++++++
 .../selftests/sched_ext/class_switch_stall.c  | 240 ++++++++++++++++++
 3 files changed, 362 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/class_switch_stall.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/class_switch_stall.c
tools/testing/selftests/sched_ext/Makefile +1 -0
index c5d3a2eaea7da..44083f1c65969 100644
@@ -165,6 +165,7 @@ all_test_bpfprogs := $(foreach prog,$(wildcard *.bpf.c),$(INCLUDE_DIR)/$(patsubs
 
 auto-test-targets :=			\
 	cgroup_nr_cpus			\
+	class_switch_stall		\
 	create_dsq			\
 	dequeue				\
 	dequeue_iter			\
tools/testing/selftests/sched_ext/class_switch_stall.bpf.c new file +121 -0

121 lines not on this page · raw message

tools/testing/selftests/sched_ext/class_switch_stall.c new file +240 -0

240 lines not on this page · raw message

Signature · 1 line
2.56.0.176.g3575e65682-goog
reviewer
#5 lore raw

Re: [PATCH 2/2] selftests/sched_ext: Add class_switch_stall test

Hi Michal,

Quoted 13 lines · Michal Blaszczyk

Add a test for a task which leaves SCHED_EXT while it's waiting. Its scheduler keeps the task waiting on a DSQ after it wakes up. The test switches the task to SCHED_OTHER while it's waiting, lets it sleep outside of SCHED_EXT for three times the watchdog timeout, then switches it back and wakes it up. The scheduler keeps it waiting again, for longer than the watchdog interval but shorter than the timeout, which the watchdog must not report as a stall.

Without the previous patch, the watchdog charges the task for the time it spent outside of SCHED_EXT and aborts the scheduler.

Assisted-by LLM

...

Quoted 29 lines
tools/testing/selftests/sched_ext/class_switch_stall.c new file +23 -0
new file mode 100644
index 0000000000000..489d23c777f05
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Check that a task which leaves SCHED_EXT while it's waiting doesn't carry
+ * that wait over to its next stay on SCHED_EXT. If it did, the watchdog would
+ * count the time the task spent outside SCHED_EXT as waiting and abort the
+ * scheduler.
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+#define _GNU_SOURCE
+
+#include <bpf/bpf.h>
+#include <poll.h>
+#include <sched.h>
+#include <scx/common.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include "class_switch_stall.bpf.skel.h"
+#include "scx_test.h"
+

tools/testing/selftests/sched_ext/class_switch_stall.c line 33

Quoted 10 lines
tools/testing/selftests/sched_ext/class_switch_stall.c new file +10 -0
+#ifndef SCHED_EXT
+#define SCHED_EXT 7
+#endif
+
+/* class_switch_stall_ops.timeout_ms, the watchdog checks every half of it */
+#define TIMEOUT_MS 1000
+/* longer than the watchdog interval, but shorter than the timeout */
+#define PARK_MS 750
+/* time spent outside of SCHED_EXT, a stale wait would exceed the timeout */
+#define OUTSIDE_MS (3 * TIMEOUT_MS)

Could we increase the watchdog timeout to 3s and the park duration to 2s, updating timeout_ms accordingly?

With the current values, there is only about 250ms of headroom for timer delivery, dispatch, and the child actually getting CPU time before it becomes eligible for a watchdog stall The first phase also depends on userspace switching the parked child out within 1s.

I haven't observed a false failure, so this is a robustness suggestion (I'm thinking at loaded CI VMs) rather than an observed problem. A 2s park would still span the nominal 1.5s watchdog interval while leaving more headroom.

Thanks,
-Andrea