sched/fair: Stop core-scheduled siblings trading rescheduling IPIs
cover sched/fair: Stop core-scheduled siblings trading rescheduling IPIs
Since v6.17, two SMT siblings under core scheduling can keep sending each other rescheduling IPIs, thousands per CPU per second, while each keeps running the same task.
Commit 0b9ca2dcabc3 ("sched/fair: Always trigger resched at the end of a protected period") makes update_curr() ask for a reschedule once the running entity's protected period has expired. set_protect_slice() only rearms that period on a fresh pick (SNT_PICK), so an entity that is picked again keeps an expired vprot and every later update_curr() on it asks again. Under core scheduling the ask is remote: a core wide pick calls pick_task() - hence update_curr() - on every sibling's runqueue, so each sibling IPIs the other, which schedules, re-picks the same entity, renews nothing, and IPIs back.
Patch 1 renews the protection on a repick. Patch 2 adds a selftest that cookies one SMT pair, runs two spinners on each sibling and fails above 1000 rescheduling IPIs per CPU per second. It reproduces in a 4 vCPU QEMU guest (-smp 4,sockets=1,cores=2,threads=2) with CONFIG_SCHED_CORE=y and CONFIG_PREEMPT_LAZY=n: without patch 1 it fails 6 runs out of 7, at 808-2123 IPIs/CPU/s; with it all 7 pass, at 224-271.
This series, including the changelogs, was prepared with the help of an AI coding assistant; I have reviewed and tested all of it.
Fernand Sieber (2):
sched/fair: Renew slice protection when current is picked again selftests/sched: Add a core scheduling rescheduling IPI test
kernel/sched/fair.c | 7 + tools/testing/selftests/sched/.gitignore | 1 + tools/testing/selftests/sched/Makefile | 4 +- tools/testing/selftests/sched/config | 2 +- .../selftests/sched/cs_resched_ipi_test.c | 377 ++++++++++++++++++ 5 files changed, 388 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/sched/cs_resched_ipi_test.c
Signature · 8 lines
2.47.3 Amazon Development Centre (South Africa) (Proprietary) Limited 29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa Registration Number: 2004 / 034463 / 07
PATCH 1/2 sched/fair: Renew slice protection when current is picked again
Commit 0b9ca2dcabc3 ("sched/fair: Always trigger resched at the end of a protected period") made update_curr() request a reschedule as soon as the running entity's protected period has run out:
if (resched || !protect_slice(curr)) {
resched_curr_lazy(rq);
clear_buddies(cfs_rq, curr);
}se->vprot, which protect_slice() tests against, is armed by set_protect_slice() in set_next_task_fair() only for a fresh pick (SNT_PICK). An entity that is picked again - prev == next, so put_prev_set_next_task() uses SNT_REPICK, see commit c72945693b90 ("sched: Restart fair hrtick after same-task repicks") - keeps its expired vprot. Every later update_curr() on it therefore sets TIF_NEED_RESCHED again, and keeps doing so until the entity is switched out.
On a CPU that picks only for itself this is mostly harmless, because the pick runs inside __schedule(), which clears TIF_NEED_RESCHED right after it.
With core scheduling it is not. pick_next_task() does a core wide pick and calls pick_task(rq_i) - hence pick_task_fair() and update_curr() - on every SMT sibling's runqueue. resched_curr_lazy() on a remote runqueue ends in smp_send_reschedule(), a real rescheduling IPI to that sibling. The sibling schedules, re-picks the same task, renews nothing, and its own core wide pick sends the IPI straight back. The two siblings then trade rescheduling IPIs for as long as both run a task whose protected period has expired.
This needs CONFIG_PREEMPT_LAZY=n. __resched_curr() only calls smp_send_reschedule() for the eager TIF_NEED_RESCHED, and resched_curr_lazy() asks for TIF_NEED_RESCHED_LAZY instead once PREEMPT_LAZY is built in, so a lazy kernel sets a remote flag and sends no IPI.
To reproduce on any SMT machine built with CONFIG_SCHED_CORE=y and CONFIG_PREEMPT_LAZY=n:
1) take an SMT sibling pair from
/sys/devices/system/cpu/cpu0/topology/thread_siblings_list;
2) give the process group a core scheduling cookie with
prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_PGID, 0); 3) fork four children that spin, two pinned to each sibling. Two per runqueue is the minimum: update_curr() returns early while cfs_rq->h_nr_queued == 1;
4) read the RES row of /proc/interrupts for both siblings, wait ten seconds and read it again.
Renewing on a repick does not undo 0b9ca2dcabc3: the resched leg of the condition is untouched, so the reschedule at the end of a protected period still happens. A repicked entity gets the same bounded protection a fresh pick gets - set_protect_slice() caps vprot at the entity's own deadline, at the shortest slice enqueued on the cfs_rq under RUN_TO_PARITY, and at ineligible_vruntime() under PREEMPT_SHORT - and only once its previous protection has expired.
--- kernel/sched/fair.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/sched/fair.c +7 -0
@@ -15645,6 +15645,13 @@ static void set_next_task_fair(struct rq *rq, struct task_struct *p, enum snt_esched_fair_update_stop_tick (rq, p); repick: +/* +* A repick is a pick: renew a protected period that ran out, else +* update_curr() reschedules on every update until p is switched out. +*/ +if (type ==SNT_REPICK && on_rq && !protect_slice (se)) +set_protect_slice (cfs_rq, se); +/* * A same-task repick skips put_prev_task_fair(), but * pick_task_fair() refreshed the entity hrtick_start_fair() reads
Signature · 8 lines
2.47.3 Amazon Development Centre (South Africa) (Proprietary) Limited 29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa Registration Number: 2004 / 034463 / 07
PATCH 2/2 selftests/sched: Add a core scheduling rescheduling IPI test
In reply to Fernand SieberA core wide pick runs pick_task() on every sibling of the core, so update_curr() runs on a remote runqueue and can reschedule it, which costs a rescheduling IPI. Two siblings whose current entity has lost its slice protection and never gets it back then trade those IPIs for as long as both stay busy.
Add a test that counts them. It takes one SMT pair, gives its process group a cookie, runs two spinners on each sibling - update_curr() returns early while cfs_rq->h_nr_queued == 1 - and reads the rescheduling row of /proc/interrupts around a five second window. A kernel that renews the protection on a repick stays near 240 IPIs/CPU/s, one that does not reaches thousands; the test fails above 1000.
The symptom needs an eager remote reschedule, so the test first checks that preempting a task on a remote CPU costs an IPI at all and skips if it does not, as it will under CONFIG_PREEMPT_LAZY=y. It also skips without an SMT pair or without CONFIG_SCHED_CORE.
--- tools/testing/selftests/sched/.gitignore | 1 + tools/testing/selftests/sched/Makefile | 4 +- tools/testing/selftests/sched/config | 2 +- .../selftests/sched/cs_resched_ipi_test.c | 377 ++++++++++++++++++ 4 files changed, 381 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/sched/cs_resched_ipi_test.c
tools/testing/selftests/sched/.gitignore +1 -0
@@ -1 +1,2 @@ cs_prctl_test +cs_resched_ipi_test
tools/testing/selftests/sched/Makefile +2 -2
@@ -8,7 +8,7 @@ CFLAGS += -O2 -Wall -g -I./ $(KHDR_INCLUDES) -Wl,-rpath=./ \ $(CLANG_FLAGS) LDLIBS += -lpthread -TEST_GEN_FILES := cs_prctl_test -TEST_PROGS := cs_prctl_test +TEST_GEN_FILES := cs_prctl_test cs_resched_ipi_test +TEST_PROGS := cs_prctl_test cs_resched_ipi_test include ../lib.mk
tools/testing/selftests/sched/config +1 -1
@@ -1 +1 @@ -# empty +CONFIG_SCHED_CORE=y
tools/testing/selftests/sched/cs_resched_ipi_test.c new file +377 -0
377 lines not on this page · raw message
Signature · 8 lines
2.47.3 Amazon Development Centre (South Africa) (Proprietary) Limited 29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa Registration Number: 2004 / 034463 / 07