Skip to contentSkip to conversation

sched/core: Don't count voluntary idle picks as forced idle

PATCH In review Kuba Piecuch 3 participants 6 messages Dec 1

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

cover sched/core: Don't count voluntary idle picks as forced idle

In pick_next_task(), core scheduling increments core_forceidle_count whenever a sibling picks idle with rq_i->nr_running > 0. That mistakes a voluntary idle pick -- such as a sched_ext task parked on a non-local DSQ while pick_task() returns NULL -- for cookie-mismatch forced idle. When all siblings voluntarily pick idle, core_forceidle_count > 0 with core_forceidle_occupation == 0 triggers the warning in __sched_core_account_forceidle(); when another sibling is busy, voluntary idle is billed as forced idle and triggers forced-idle-only paths (queue_core_balance(), forced_idle_rq(), sched_core_idle_needs_pick()).

Patch 1 moves the core_forceidle_count and core_forceidle_seq increments into the !cookie_equals(p, cookie) fallback when sched_core_find() returns NULL.

Patch 2 adds a core_forceidle sched_ext selftest covering voluntary idle with an idle sibling, voluntary idle with an untagged busy sibling, and a sanity check that genuine cookie-mismatch forced idle is still counted. Without patch 1, the selftest triggers:

  WARNING: kernel/sched/core_sched.c:262 at __sched_core_account_forceidle+0x23e/0x260, CPU#0: swapper/0/0

and fails with:

ERR: core_forceidle.c:449
Phase 1: voluntary idle counted as forced idle (obs=1 fi=1 zero_occ=1)

This series is based on tip/sched/urgent because
commit d2583cb03113 ("sched/deadline: Clear stale rq->dl_server when core-sched replaces a pick") modifies the same loop in pick_next_task(). Patch 2 touches tools/testing/selftests/sched_ext/ only; please route through whichever tree is more convenient (or take patch 1 via tip and patch 2 via sched_ext once patch 1 lands).

The patches were written with the help of an AI coding assistant. I have reviewed the patches and verified the tests run by the tool.

Kuba Piecuch (2):
sched/core: Don't count voluntary idle picks as forced idle selftests/sched_ext: Add core_forceidle test for voluntary idle picks

 kernel/sched/core.c                           |  15 +-
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/core_forceidle.bpf.c  | 176 +++++++
 .../selftests/sched_ext/core_forceidle.c      | 490 ++++++++++++++++++
 4 files changed, 673 insertions(+), 9 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.c

base-commit: f4eb2ace3efdd8b6e3b0946bbd316166edb1a51e

Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw

PATCH 1/2 sched/core: Don't count voluntary idle picks as forced idle

In pick_next_task(), the per-sibling loop increments rq->core->core_forceidle_count whenever p == rq_i->idle and rq_i->nr_running > 0. That conflates a sibling forced idle by a cookie mismatch with a sibling whose pick_task() voluntarily returned idle while rq_i->nr_running > 0.

Voluntary idle with nr_running > 0 is legitimate whenever a scheduling class keeps queued tasks off the CPU and returns NULL from pick_task(), such as sched_ext tasks parked on a non-local DSQ. Counting those voluntary idle picks as forced idle has two effects:

- If all siblings pick idle (occ == 0), core_forceidle_count becomes non-zero while core_forceidle_occupation is 0, triggering WARN_ON_ONCE(!rq->core->core_forceidle_occupation) in __sched_core_account_forceidle() when schedstats is enabled.
- If another sibling is running (occ > 0), the voluntary idle time is spuriously billed as forced idle to the running sibling's cgroup and triggers forced-idle-only paths (queue_core_balance(), forced_idle_rq(), sched_core_idle_needs_pick()).

Increment core_forceidle_count and core_forceidle_seq only inside the !cookie_equals(p, cookie) branch when sched_core_find() returns NULL and replaces a non-idle pick with idle. Since cookie_equals() always returns true for rq_i->idle, voluntary idle picks are never counted as forced idle, and entering the mismatch branch guarantees a non-idle @max so core_forceidle_occupation is always non-zero whenever forced idle is recorded.

Fixes 4feee7d12603 ("sched/core: Forced idle accounting")
---
 kernel/sched/core.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
kernel/sched/core.c +6 -9
index c9890ed35df7..8cd30021721f 100644
@@ -6470,8 +6470,12 @@ pick_next_task(struct rq *rq, struct rq_flags *rf)
 			p = NULL;
 			if (cookie)
 				p = sched_core_find(rq_i, cookie);
-			if (!p)
+			if (!p) {
 				p = idle_sched_class.pick_task(rq_i, rf);
+				rq->core->core_forceidle_count++;
+				if (!fi_before)
+					rq->core->core_forceidle_seq++;
+			}
 
 			/*
 			 * Neither a core tree pick nor idle comes through a
@@ -6482,15 +6486,8 @@ pick_next_task(struct rq *rq, struct rq_flags *rf)
 
 		rq_i->core_pick = p;
 
-		if (p == rq_i->idle) {
-			if (rq_i->nr_running) {
-				rq->core->core_forceidle_count++;
-				if (!fi_before)
-					rq->core->core_forceidle_seq++;
-			}
-		} else {
+		if (p != rq_i->idle)
 			occ++;
-		}
 	}
 
 	if (schedstat_enabled() && rq->core->core_forceidle_count) {
Signature · 1 line
2.56.1.839.g22a8290db9-goog
reviewer Acked-by on v1 1/2
#2 lore raw
Quoted 28 lines · Kuba Piecuch

In pick_next_task(), the per-sibling loop increments rq->core->core_forceidle_count whenever p == rq_i->idle and rq_i->nr_running > 0. That conflates a sibling forced idle by a cookie mismatch with a sibling whose pick_task() voluntarily returned idle while rq_i->nr_running > 0.

Voluntary idle with nr_running > 0 is legitimate whenever a scheduling class keeps queued tasks off the CPU and returns NULL from pick_task(), such as sched_ext tasks parked on a non-local DSQ. Counting those voluntary idle picks as forced idle has two effects:

- If all siblings pick idle (occ == 0), core_forceidle_count becomes non-zero while core_forceidle_occupation is 0, triggering WARN_ON_ONCE(!rq->core->core_forceidle_occupation) in __sched_core_account_forceidle() when schedstats is enabled.
- If another sibling is running (occ > 0), the voluntary idle time is spuriously billed as forced idle to the running sibling's cgroup and triggers forced-idle-only paths (queue_core_balance(), forced_idle_rq(), sched_core_idle_needs_pick()).

Increment core_forceidle_count and core_forceidle_seq only inside the !cookie_equals(p, cookie) branch when sched_core_find() returns NULL and replaces a non-idle pick with idle. Since cookie_equals() always returns true for rq_i->idle, voluntary idle picks are never counted as forced idle, and entering the mismatch branch guarantees a non-idle @max so core_forceidle_occupation is always non-zero whenever forced idle is recorded.

Quoted 3 lines
Fixes 4feee7d12603 ("sched/core: Forced idle accounting")

Thanks.

--
tejun

#3 lore raw

PATCH 2/2 selftests/sched_ext: Add core_forceidle test for voluntary idle picks

In reply to Kuba Piecuch

Add a core_forceidle selftest that exercises core-scheduling forced-idle accounting when a sched_ext task with a core-scheduling cookie is parked on a user DSQ so its CPU voluntarily picks idle with nr_running > 0:

- Phase 1: sibling is also idle (occ == 0), verifying core_forceidle_count stays 0 and __sched_core_account_forceidle() does not warn.
- Phase 2: sibling runs an untagged busy task (occ == 1), verifying the voluntary idle sibling is not counted as forced idle.
- Phase 3: unpark the cookied task against the untagged sibling, verifying genuine cookie-mismatch forced idle is still counted.

---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/core_forceidle.bpf.c  | 176 +++++++
 .../selftests/sched_ext/core_forceidle.c      | 490 ++++++++++++++++++
 3 files changed, 667 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.c
tools/testing/selftests/sched_ext/Makefile +1 -0
index c5d3a2eaea7d..af1182e9b074 100644
@@ -165,6 +165,7 @@ all_test_bpfprogs := $(foreach prog,$(wildcard *.bpf.c),$(INCLUDE_DIR)/$(patsubs
 
 auto-test-targets :=			\
 	cgroup_nr_cpus			\
+	core_forceidle			\
 	create_dsq			\
 	dequeue				\
 	dequeue_iter			\
tools/testing/selftests/sched_ext/core_forceidle.bpf.c new file +176 -0

176 lines not on this page · raw message

tools/testing/selftests/sched_ext/core_forceidle.c new file +490 -0

490 lines not on this page · raw message

Signature · 1 line
2.56.1.839.g22a8290db9-goog
#4 lore raw

Re: [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/sched_ext: Phase 2 in run() bypasses core-wide pick_next_task() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/sched_ext: Phase 2 in run() bypasses core-wide pick_next_task()
--

commit ad2b5e66c62d106475f2b8ad4083ca89741fc68e
Author: Kuba Piecuch <[email protected]>

selftests/sched_ext: Add core_forceidle test for voluntary idle picks

This commit adds a sched_ext selftest that verifies core-scheduling forced-idle accounting across three phases when a task with a core cookie is parked on a user DSQ and its CPU voluntarily picks idle.

Quoted 6 lines
tools/testing/selftests/sched_ext/core_forceidle.bpf.c new file +0 -0
new file mode 100644
index 0000000000000..12c8be46cc126
@@ -0,0 +1,176 @@

[ ... ]

Quoted 27 lines
tools/testing/selftests/sched_ext/core_forceidle.bpf.c new file +27 -0
+void BPF_STRUCT_OPS(core_forceidle_enqueue, struct task_struct *p, u64 enq_flags)
+{
+	u32 ph = phase;
+
+	if (p->pid == parked_pid) {
+		if ((ph == 1 || ph == 2) && (enq_flags & SCX_ENQ_LAST)) {
+			scx_bpf_dsq_insert(p, PARK_DSQ, 1000000ULL, enq_flags);
+			parked_in_dsq = 1;
+			return;
+		}
+		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, 1000000ULL, enq_flags);
+		return;
+	}
+
+	if (p->pid == busy_pid) {
+		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, enq_flags);
+		return;
+	}
+
+	scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
+}
+
+void BPF_STRUCT_OPS(core_forceidle_dispatch, s32 cpu, struct task_struct *prev)
+{
+	if (cpu == sib0 && phase != 1 && phase != 2)
+		scx_bpf_dsq_move_to_local(PARK_DSQ, 0);
+}

[ ... ]

Quoted 6 lines
tools/testing/selftests/sched_ext/core_forceidle.c new file +0 -0
new file mode 100644
index 0000000000000..962863b3c0c19
@@ -0,0 +1,490 @@

[ ... ]

Quoted 24 lines
tools/testing/selftests/sched_ext/core_forceidle.c new file +24 -0
+static enum scx_test_status run(void *ctx_ptr)
+{
+	struct core_forceidle_ctx *ctx = ctx_ptr;
+	struct core_forceidle *skel = ctx->skel;
+	struct worker_state parked_st = { .wake_pipe = { -1, -1 }, .ready_pipe = { -1, -1 } };
+	struct worker_state busy_st = { .wake_pipe = { -1, -1 }, .ready_pipe = { -1, -1 } };
+	struct bpf_link *link = NULL;
+	enum scx_test_status status = SCX_TEST_FAIL;
+	char c = 1;
+
+	if (start_worker(&parked_st, ctx->sib0, true, false)) {
+		SCX_ERR("Failed to start parked worker");
+		goto out;
+	}
+	/*
+	 * Leave busy_st untagged (cookie 0) so unrelated untagged kernel
+	 * threads on sib0 match it in Phase 2 instead of causing genuine
+	 * forced idle, while Phase 3 still sees a cookie mismatch against
+	 * parked_st.
+	 */
+	if (start_worker(&busy_st, ctx->sib1, false, true)) {
+		SCX_ERR("Failed to start busy worker");
+		goto out;
+	}

[ ... ]

Quoted 12 lines
tools/testing/selftests/sched_ext/core_forceidle.c new file +12 -0
+	/*
+	 * Phase 1: sib0's cookied task is parked in PARK_DSQ on SCX_ENQ_LAST
+	 * while sib1 is idle (occ == 0). Neither sibling is forced idle.
+	 */
+	__atomic_store_n(&skel->bss->phase, 1, __ATOMIC_RELEASE);
+	if (!wait_u32_nonzero(&skel->bss->phase1_obs, WAIT_TIMEOUT_MS)) {
+		SCX_ERR("Phase 1: sib0 never entered voluntary idle");
+		goto out;
+	}
+	/* Let ticks fire so __sched_core_account_forceidle() runs if buggy. */
+	usleep(50000);
+

tools/testing/selftests/sched_ext/core_forceidle.c line 423

Quoted 10 lines
tools/testing/selftests/sched_ext/core_forceidle.c new file +10 -0
+	/*
+	 * Phase 2: sib0 stays voluntarily idle with parked_pid in PARK_DSQ
+	 * while busy_pid (different cookie) runs on sib1 (occ == 1).
+	 */
+	__atomic_store_n(&skel->bss->phase, 2, __ATOMIC_RELEASE);
+	__atomic_store_n(&busy_st.pause_flag, 0, __ATOMIC_RELEASE);
+	if (write(busy_st.wake_pipe[1], &c, 1) != 1) {
+		SCX_ERR("Phase 2: failed to wake busy worker");
+		goto out;
+	}

[Severity: Medium]
Does Phase 2 ever enter the core-wide selection loop in pick_next_task()?

When Phase 1 parks parked_st in PARK_DSQ and both siblings pick idle, pick_next_task() resets rq->core->core_cookie to 0 while rq->core->core_forceidle_count remains 0. Because core_forceidle_dispatch() keeps PARK_DSQ parked when phase is 2, parked_st never runs on sib0 between Phase 1 and Phase 2, and the ph == 2 branch in core_forceidle_enqueue() is never reached.

When Phase 2 wakes untagged busy_st (core_cookie == 0) on sib1, pick_next_task() sees !need_sync && !next->core_cookie and takes the single-rq fast path to out_set_next, bypassing the core-wide sibling loop where occ == 1 and core_forceidle_count are evaluated. Also, busy_running in core_forceidle.bpf.c is updated in running/stopping but never read.

Should parked_st be unparked once busy_st is running on sib1 so that sib0 enters Phase 2 with a non-zero core_cookie before re-parking on SCX_ENQ_LAST?

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

Re: [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks

Quoted 13 lines · On Tue, 01 Dec 2026 12:26, [email protected] wrote:

When Phase 1 parks parked_st in PARK_DSQ and both siblings pick idle, pick_next_task() resets rq->core->core_cookie to 0 while rq->core->core_forceidle_count remains 0. Because core_forceidle_dispatch() keeps PARK_DSQ parked when phase is 2, parked_st never runs on sib0 between Phase 1 and Phase 2, and the ph == 2 branch in core_forceidle_enqueue() is never reached.

When Phase 2 wakes untagged busy_st (core_cookie == 0) on sib1, pick_next_task() sees !need_sync && !next->core_cookie and takes the single-rq fast path to out_set_next, bypassing the core-wide sibling loop where occ == 1 and core_forceidle_count are evaluated. Also, busy_running in core_forceidle.bpf.c is updated in running/stopping but never read.

Quoted 3 lines

Should parked_st be unparked once busy_st is running on sib1 so that sib0 enters Phase 2 with a non-zero core_cookie before re-parking on SCX_ENQ_LAST?

Good catch, that is right. With patch 1 applied, Phase 1 leaves both rq->core->core_cookie and rq->core->core_forceidle_count at 0, so waking untagged busy_st in Phase 2 takes the !need_sync fast path and skips the core-wide sibling loop.

Keeping busy_st untagged avoids unrelated untagged kernel threads on sib0 causing genuine forced idle, so unparking parked_st once busy_st is running and letting it expire and re-park on SCX_ENQ_LAST is the right fix: the pick that parks parked_st then enters the core-wide loop with need_sync set and selects busy_st on sib1 (occ == 1) alongside voluntary idle on sib0.

I will update Phase 2 accordingly (and drop the unused busy_running field and stopping callback) in v2.