Skip to contentSkip to conversation

sched_ext: Fix finite-slice ticks on nohz_full

PATCH Superseded Andrea Righi 3 participants 7 messages Jul 6 – Jul 7

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: Fix finite-slice ticks on nohz_full

When an EXT task with a finite slice is scheduled on a nohz_full CPU, the scheduler may incorrectly keep the tick disabled because it still observes the outgoing task (e.g., the idle task) during the context switch. As a result, the incoming task can run past its assigned slice.

Fix this by always enabling the scheduler tick for finite-slice EXT tasks and provide a kselftest that reproduces the scenario and verifies that scheduler ticks are delivered correctly. Test is skipped in kernels without available nohz_full CPUS.

Test case (using virtme-ng):

  $ vng -vb --config tools/testing/selftests/sched_ext/config \
        --configitem CONFIG_NO_HZ_FULL=y
  $ vng --append "nohz_full=all" -- \
        tools/testing/selftests/sched_ext/runner -t nohz_tick

Before:

  $ vng --append "nohz_full=all" -- tools/testing/selftests/sched_ext/runner -t nohz_tick
  ===== START =====
  TEST: nohz_tick
  DESCRIPTION: Verify finite EXT slices restart the NOHZ_FULL tick
  OUTPUT:
  ERR: nohz_tick.c:270
  Finite-slice worker received only 1 scheduler ticks
  not ok 1 nohz_tick #
  =====  END  =====

=============================

RESULTS:

PASSED: 0
SKIPPED: 0
FAILED: 1

Failed tests:
- nohz_tick

After:

  $ vng --append "nohz_full=all" -- tools/testing/selftests/sched_ext/runner -t nohz_tick
  ===== START =====
  TEST: nohz_tick
  DESCRIPTION: Verify finite EXT slices restart the NOHZ_FULL tick
  OUTPUT:
  CPU 1 received 4 finite-slice ticks
  ok 1 nohz_tick #
  =====  END  =====

=============================

RESULTS:

PASSED: 1
SKIPPED: 0
FAILED: 0

Andrea Righi (2):
sched_ext: Enable tick for finite slices on nohz_full selftests/sched_ext: Verify nohz_full tick behavior

 kernel/sched/ext/ext.c                            |  14 +-
 tools/testing/selftests/sched_ext/Makefile        |   1 +
 tools/testing/selftests/sched_ext/nohz_tick.bpf.c |  65 +++++
 tools/testing/selftests/sched_ext/nohz_tick.c     | 309 ++++++++++++++++++++++
 4 files changed, 386 insertions(+), 3 deletions(-)
author reviewer
#1 lore raw

PATCH 1/2 sched_ext: Enable tick for finite slices on nohz_full

set_next_task_scx() updates the tick dependency before __schedule() updates rq->curr. When switching from a non-EXT task, such as idle, to an EXT task with a finite slice, sched_update_tick_dependency() checks the outgoing task and can allow the tick to remain stopped. The incoming task can then fail to receive the ticks needed to expire its slice.

A finite slice is sufficient by itself to require the tick, so set TICK_DEP_BIT_SCHED directly when transitioning to a finite slice. Keep using sched_update_tick_dependency() when transitioning to an infinite slice so that the scheduler evaluates all runqueue requirements before clearing the dependency.

Fixes 22a920209ab6 ("sched_ext: Implement tickless support")
---
 kernel/sched/ext/ext.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
kernel/sched/ext/ext.c +11 -3
index e75e2fd5ab7e3..dc20a8eed8f6c 100644
@@ -2982,12 +2982,20 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
 	 */
 	if ((p->scx.slice == SCX_SLICE_INF) !=
 	    (bool)(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) {
-		if (p->scx.slice == SCX_SLICE_INF)
+		if (p->scx.slice == SCX_SLICE_INF) {
 			rq->scx.flags |= SCX_RQ_CAN_STOP_TICK;
-		else
+			sched_update_tick_dependency(rq);
+		} else {
 			rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
 
-		sched_update_tick_dependency(rq);
+			/*
+			 * The rq still references the outgoing scheduling context.
+			 * A finite slice is sufficient by itself to require the tick.
+			 */
+			if (tick_nohz_full_cpu(cpu_of(rq)))
+				tick_nohz_dep_set_cpu(cpu_of(rq),
+							TICK_DEP_BIT_SCHED);
+		}
 
 		/*
 		 * For now, let's refresh the load_avgs just when transitioning
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/2] sched_ext: Enable tick for finite slices on nohz_full

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] The patch's fix for finite slices is incomplete: it fails to restore the tick dependency when switching from a tick-stopped non-EXT task (such as idle) to an EXT task with a finite slice, if the last EXT task on that CPU also had a finite slice.

Pre-existing issues:
- [High] The patch manually sets the tick dependency when transitioning to a finite-slice SCX task but fails to ensure it is cleared when transitioning out to a non-EXT task (such as CFS or idle), causing a persistent leak that breaks nohz_full isolation.
- [High] When transitioning to an infinite-slice task, the patch continues to call sched_update_tick_dependency(rq). Because scx_can_stop_tick() evaluates the outgoing task (rq->curr), transitioning from a non-EXT task to an infinite-slice EXT task while in bypass mode will incorrectly clear the tick dependency. --

commit ed44446cf6110ce75b933d2ec6430b4cf1e80f2a
Author: Andrea Righi <[email protected]>

sched_ext: Enable tick for finite slices on nohz_full

This commit addresses an issue where an incoming SCHED_EXT task with a finite slice could fail to receive the ticks needed to expire its slice. It changes set_next_task_scx() to directly set the TICK_DEP_BIT_SCHED dependency for finite-slice tasks, bypassing the normal sched_update_tick_dependency() check that relies on the outgoing task state. For infinite-slice tasks, it continues to use sched_update_tick_dependency().

kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 2976

Quoted 8 lines
kernel/sched/ext/ext.c +0 -0
index 691d53fe0f648..9b2399a1e9656 100644
@@ -2974,12 +2974,20 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
 	 */
 	if ((p->scx.slice == SCX_SLICE_INF) !=
 	    (bool)(rq->scx.flags & SCX_RQ_CAN_STOP_TICK)) {

[Severity: High]
Does this condition prevent the tick dependency from being restored when switching from a tick-stopped non-EXT task (such as idle) to an EXT task with a finite slice, if the last EXT task on that CPU also had a finite slice?

If a finite-slice EXT task previously cleared SCX_RQ_CAN_STOP_TICK, and the CPU then transitions to a non-EXT state that clears the global tick dependency, the false != false evaluation here will bypass this entire block. This would cause the new finite-slice EXT task to run without a tick, potentially causing it to monopolize the CPU since its slice won't expire.

kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 2987

Quoted 5 lines
kernel/sched/ext/ext.c +2 -2
-		if (p->scx.slice == SCX_SLICE_INF)
+		if (p->scx.slice == SCX_SLICE_INF) {
 			rq->scx.flags |= SCX_RQ_CAN_STOP_TICK;
-		else
+			sched_update_tick_dependency(rq);

[Severity: High]
This isn't a bug introduced by this patch, but when transitioning from a non-EXT task to an infinite-slice EXT task while in bypass mode, could calling sched_update_tick_dependency() here incorrectly clear the tick dependency?

Because scx_can_stop_tick() evaluates the outgoing task:

kernel/sched/ext/ext.c:scx_can_stop_tick() {
    ...
	if (p->sched_class != &ext_sched_class)
		return true;
    ...
}

It will return true early for the outgoing non-EXT task. This bypasses the scx_bypassing() check entirely, which could stall tasks queued in the bypass DSQ indefinitely by incorrectly stopping the scheduler tick.

kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 2997

Quoted 11 lines
kernel/sched/ext/ext.c +8 -1
+		} else {
 			rq->scx.flags &= ~SCX_RQ_CAN_STOP_TICK;
 
-		sched_update_tick_dependency(rq);
+			/*
+			 * The rq still references the outgoing scheduling context.
+			 * A finite slice is sufficient by itself to require the tick.
+			 */
+			if (tick_nohz_full_cpu(cpu_of(rq)))
+				tick_nohz_dep_set_cpu(cpu_of(rq),
+							TICK_DEP_BIT_SCHED);

[Severity: High]
This is a pre-existing issue, but does setting the tick dependency here leave a persistent leak when the task exits the SCX class?

When an EXT task with a finite slice blocks or exits on a nohz_full CPU, dequeue_task() calls sub_nr_running(), which invokes sched_update_tick_dependency(). At that moment, __schedule() hasn't updated rq->curr, so scx_can_stop_tick() evaluates the outgoing finite-slice EXT task and returns false.

This leaves the tick dependency set. Subsequent transitions to non-EXT tasks like idle or CFS do not re-evaluate or clear this dependency, meaning the tick could run persistently on a nohz_full CPU, breaking isolation.

Quoted 1 line
kernel/sched/ext/ext.c +1 -0
+		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#3 lore raw

Re: [PATCH 1/2] sched_ext: Enable tick for finite slices on nohz_full

In reply to Andrea Righi

Hello, Andrea.

Sashiko's first point looks real to me. The fix covers infinite->finite, but a finite slice landing on a nohz_full CPU after idle has the same issue whenever the previous run wasn't infinite (finite->idle->finite):
the enqueue path already cleared TICK_DEP_BIT_SCHED against the idle rq->curr, and set_next_task_scx() re-asserts only on a slice-type transition, so with SCX_RQ_CAN_STOP_TICK unchanged the tick stays stopped.

set_next_task_scx() knows the incoming task, so it can assert directly rather than keying off the transition, like sched_fair_update_stop_tick() does for CFS. Moving the finite assertion out of the transition branch should do it:

	if (p->scx.slice != SCX_SLICE_INF && tick_nohz_full_cpu(cpu_of(rq)))
		tick_nohz_dep_set_cpu(cpu_of(rq), TICK_DEP_BIT_SCHED);

Can you fold that in and add a finite->idle->finite selftest case for v2?

Thanks.

--
tejun

author reviewer
#4 lore raw

Re: [PATCH 1/2] sched_ext: Enable tick for finite slices on nohz_full

Quoted 15 lines · Tejun Heo

Hello, Andrea.

Sashiko's first point looks real to me. The fix covers infinite->finite, but a finite slice landing on a nohz_full CPU after idle has the same issue whenever the previous run wasn't infinite (finite->idle->finite):
the enqueue path already cleared TICK_DEP_BIT_SCHED against the idle rq->curr, and set_next_task_scx() re-asserts only on a slice-type transition, so with SCX_RQ_CAN_STOP_TICK unchanged the tick stays stopped.

set_next_task_scx() knows the incoming task, so it can assert directly rather than keying off the transition, like sched_fair_update_stop_tick() does for CFS. Moving the finite assertion out of the transition branch should do it:

Quoted 4 lines
	if (p->scx.slice != SCX_SLICE_INF && tick_nohz_full_cpu(cpu_of(rq)))
		tick_nohz_dep_set_cpu(cpu_of(rq), TICK_DEP_BIT_SCHED);

Can you fold that in and add a finite->idle->finite selftest case for v2?

Yep, I'm currently looking/fixing at the issues found by Sashiko. I'll send a v2 in a bit.

Thanks,
-Andrea

author reviewer
#5 lore raw

PATCH 2/2 selftests/sched_ext: Verify nohz_full tick behavior

In reply to Andrea Righi

A finite-slice EXT task needs the periodic scheduler tick to expire its slice even when nohz_full is enabled.

Add a regression test to validate the proper nohz_full behavior:
- the test selects a nohz_full CPU and first runs an EXT task with an infinite slice,
- the task then is stopped, leaving the CPU idle with the tick stopped,
- then run an EXT task with a finite slice and verify that its ops.tick() callback is invoked.

Skip the test when an allowed nohz_full CPU and a separate housekeeping CPU are not available.

---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/nohz_tick.bpf.c       |  65 ++++
 tools/testing/selftests/sched_ext/nohz_tick.c | 309 ++++++++++++++++++
 3 files changed, 375 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/nohz_tick.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/nohz_tick.c
tools/testing/selftests/sched_ext/Makefile +1 -0
index 5d2dffca0e918..3cfe90e0f34fa 100644
@@ -176,6 +176,7 @@ auto-test-targets :=			\
 	maybe_null			\
 	minimal				\
 	non_scx_kfunc_deny		\
+	nohz_tick			\
 	numa				\
 	allowed_cpus			\
 	peek_dsq			\
tools/testing/selftests/sched_ext/nohz_tick.bpf.c new file +65 -0

65 lines not on this page · raw message

tools/testing/selftests/sched_ext/nohz_tick.c new file +309 -0

309 lines not on this page · raw message

Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH 2/2] selftests/sched_ext: Verify nohz_full tick behavior

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Worker processes will spin indefinitely if the test terminates abnormally. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Worker processes will spin indefinitely if the test terminates abnormally. --

commit 5f5f7d1cf8a2f7b2edb91658dc637840224ea1dd
Author: Andrea Righi <[email protected]>

selftests/sched_ext: Verify nohz_full tick behavior

This commit adds a regression test to validate proper nohz_full behavior in sched_ext. It verifies that a finite-slice EXT task restarts the periodic scheduler tick when it follows an infinite-slice task on a NOHZ_FULL CPU.

Quoted 4 lines
tools/testing/selftests/sched_ext/nohz_tick.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 13 lines
tools/testing/selftests/sched_ext/nohz_tick.c new file +13 -0
+static pid_t start_worker(int cpu)
+{
+	struct sched_param param = {};
+	cpu_set_t mask;
+	pid_t pid;
+
+	pid = fork();
+	if (pid != 0)
+		return pid;
+
+	/*
+	 * Become EXT before touching the target so it stays idle until wakeup.
+	 */

tools/testing/selftests/sched_ext/nohz_tick.c line 118

Quoted 10 lines
tools/testing/selftests/sched_ext/nohz_tick.c new file +10 -0
+	if (sched_setscheduler(0, SCHED_EXT, &param))
+		_exit(1);
+
+	CPU_ZERO(&mask);
+	CPU_SET(cpu, &mask);
+	if (sched_setaffinity(0, sizeof(mask), &mask))
+		_exit(1);
+
+	for (;;)
+		asm volatile("" ::: "memory");

[Severity: Medium]
If the test runner terminates abnormally before reaching stop_worker(), will this orphaned child process continue to spin indefinitely?

Since the child runs under the SCHED_EXT policy and is pinned to a specific CPU, an orphaned child would consume 100% of that CPU indefinitely and potentially interfere with subsequent tests.

Would it be safer to use prctl(PR_SET_PDEATHSIG, SIGKILL) after forking so the worker is automatically killed if the parent test process exits unexpectedly?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2