sched/fair: Fix inaccurate h_nr_runnable accounting with delayed dequeue
K Prateek Nayak
reviewerPATCH 1/1 sched/fair: Fix inaccurate h_nr_runnable accounting with delayed dequeue
set_delayed() adjusts cfs_rq->h_nr_runnable for the hierarchy when an entity is delayed irrespective of whether the entity corresponds to a task or a cfs_rq.
Consider the following scenario:
root
/ \
A B (*) delayed since B is no longer eligible on root
| |
Task0 Task1 <--- dequeue_task_fair() - task blocksWhen Task1 blocks (dequeue_entity() for task's se returns true), dequeue_entities() will continue adjusting cfs_rq->h_nr_* for the hierarchy of Task1. However, when the sched_entity corresponding to cfs_rq B is delayed, set_delayed() will adjust the h_nr_runnable for the hierarchy too leading to both dequeue_entity() and set_delayed() decrementing h_nr_runnable for the dequeue of the same task.
A SCHED_WARN_ON() to inspect h_nr_runnable post its update in dequeue_entities() like below:
cfs_rq->h_nr_runnable -= h_nr_runnable;
SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
This error is self correcting since cfs_rq are per-cpu and cannot migrate. The entitiy is either picked for full dequeue or is requeued when a task wakes up below it. Both those paths call clear_delayed() which again increments h_nr_runnable of the hierarchy without considering if the entity corresponds to a task or not.
h_nr_runnable will eventually reflect the correct value however in the interim, the incorrect values can still influence PELT calculation which uses se->runnable_weight or cfs_rq->h_nr_runnable.
Since only delayed tasks take the early return path in dequeue_entities() and enqueue_task_fair(), adjust the h_nr_runnable in {set,clear}_delayed() only when a task is delayed as this path skips the h_nr_* update loops and returns early.
For entities corresponding to cfs_rq, the h_nr_* update loop in the caller will do the right thing.
--- kernel/sched/fair.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
kernel/sched/fair.c +19 -0
@@ -5481,6 +5481,15 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);static void set_delayed (struct sched_entity *se) { se->sched_delayed =1 ; + +/* +* Delayed se of cfs_rq have no tasks queued on them. +* Do not adjust h_nr_runnable since dequeue_entities() +* will account it for blocked tasks. +*/ +if (!entity_is_task (se)) +return ; +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
@@ -5493,6 +5502,16 @@ static void set_delayed(struct sched_entity *se)static void clear_delayed (struct sched_entity *se) { se->sched_delayed =0 ; + +/* +* Delayed se of cfs_rq have no tasks queued on them. +* Do not adjust h_nr_runnable since a dequeue has +* already accounted for it or an enqueue of a task +* below it will account for it in enqueue_task_fair(). +*/ +if (!entity_is_task (se)) +return ; +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
base-commit: 7d9da040575b343085287686fa902a5b2d43c7ca
Signature · 1 line
2.34.1
Vincent Guittot
maintainerHi Prateek,
Quoted 44 lines · K Prateek Nayak
set_delayed()adjustscfs_rq->h_nr_runnablefor the hierarchy when an entity is delayed irrespective of whether the entity corresponds to a task or acfs_rq.Consider the following scenario:
root
/ \
A B (*) delayed since B is no longer eligible on root
| |
Task0 Task1 <---dequeue_task_fair()- task blocksWhen Task1 blocks (
dequeue_entity()for task's se returns true),dequeue_entities()will continue adjusting cfs_rq->h_nr_* for the hierarchy of Task1. However, when thesched_entitycorresponding tocfs_rqB is delayed,set_delayed()will adjust theh_nr_runnablefor the hierarchy too leading to bothdequeue_entity()andset_delayed()decrementingh_nr_runnablefor the dequeue of the same task.A
SCHED_WARN_ON()to inspecth_nr_runnablepost its update indequeue_entities()like below:cfs_rq->h_nr_runnable -= h_nr_runnable; SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
This error is self correcting since
cfs_rqare per-cpu and cannot migrate. The entitiy is either picked for full dequeue or is requeued when a task wakes up below it. Both those paths callclear_delayed()which again incrementsh_nr_runnableof the hierarchy without considering if the entity corresponds to a task or not.
h_nr_runnablewill eventually reflect the correct value however in the interim, the incorrect values can still influence PELT calculation which usesse->runnable_weightorcfs_rq->h_nr_runnable.Since only delayed tasks take the early return path in
dequeue_entities()andenqueue_task_fair(), adjust theh_nr_runnablein {set,clear}_delayed() only when a task is delayed as this path skips the h_nr_* update loops and returns early.
Quoted 4 lines
For entities corresponding to
cfs_rq, the h_nr_* update loop in the caller will do the right thing.Fixes 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")
You probably mean c2a295bffeaf ("sched/fair: Add new cfs_rq.h_nr_runnable") Before we were tracking the opposite h_nr_delayed. Did you see the problem only on tip/sched/core or also before the rework which added h_nr_runnable and removed h_nr_delayed
I'm going to have a closer look
Quoted 49 lines
Reviewed-by Gautham R. Shenoy <[email protected]>Tested-by Swapnil Sapkal <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>--- kernel/sched/fair.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)kernel/sched/fair.c +19
-0@@ -5481,6 +5481,15 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);static void set_delayed (struct sched_entity *se) { se->sched_delayed =1 ; + +/* +* Delayed se of cfs_rq have no tasks queued on them. +* Do not adjust h_nr_runnable since dequeue_entities() +* will account it for blocked tasks. +*/ +if (!entity_is_task (se)) +return ; +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);@@ -5493,6 +5502,16 @@ static void set_delayed(struct sched_entity *se)static void clear_delayed (struct sched_entity *se) { se->sched_delayed =0 ; + +/* +* Delayed se of cfs_rq have no tasks queued on them. +* Do not adjust h_nr_runnable since a dequeue has +* already accounted for it or an enqueue of a task +* below it will account for it in enqueue_task_fair(). +*/ +if (!entity_is_task (se)) +return ; +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);base-commit:
7d9da040575b343085287686fa902a5b2d43c7ca--
2.34.1
K Prateek Nayak
reviewerHello Vincent,
Quoted 47 lines · Vincent Guittot
Hi Prateek,
Quoted 44 lines · K Prateek Nayak
set_delayed()adjustscfs_rq->h_nr_runnablefor the hierarchy when an entity is delayed irrespective of whether the entity corresponds to a task or acfs_rq.Consider the following scenario:
root
/ \
A B (*) delayed since B is no longer eligible on root
| |
Task0 Task1 <---dequeue_task_fair()- task blocksWhen Task1 blocks (
dequeue_entity()for task's se returns true),dequeue_entities()will continue adjusting cfs_rq->h_nr_* for the hierarchy of Task1. However, when thesched_entitycorresponding tocfs_rqB is delayed,set_delayed()will adjust theh_nr_runnablefor the hierarchy too leading to bothdequeue_entity()andset_delayed()decrementingh_nr_runnablefor the dequeue of the same task.A
SCHED_WARN_ON()to inspecth_nr_runnablepost its update indequeue_entities()like below:cfs_rq->h_nr_runnable -= h_nr_runnable; SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
This error is self correcting since
cfs_rqare per-cpu and cannot migrate. The entitiy is either picked for full dequeue or is requeued when a task wakes up below it. Both those paths callclear_delayed()which again incrementsh_nr_runnableof the hierarchy without considering if the entity corresponds to a task or not.
h_nr_runnablewill eventually reflect the correct value however in the interim, the incorrect values can still influence PELT calculation which usesse->runnable_weightorcfs_rq->h_nr_runnable.Since only delayed tasks take the early return path in
dequeue_entities()andenqueue_task_fair(), adjust theh_nr_runnablein {set,clear}_delayed() only when a task is delayed as this path skips the h_nr_* update loops and returns early.
Quoted 6 lines
Quoted 4 lines
For entities corresponding to
cfs_rq, the h_nr_* update loop in the caller will do the right thing.Fixes 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")You probably mean
c2a295bffeaf("sched/fair: Add new cfs_rq.h_nr_runnable")
You are right! I had done a git blame on set_delayed() ad landed at commit 76f2f783294d but you are right, it should be c2a295bffeaf ("sched/fair: Add new cfs_rq.h_nr_runnable") when the accounting was inverted to account runnable tasks. Thank you for pointing that out.
Quoted 3 lines
Before we were tracking the opposite
h_nr_delayed. Did you see the problem only on tip/sched/core or also before the rework which addedh_nr_runnableand removedh_nr_delayed
The problem is on tip:sched/core. I did not encounter any anomalies on 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")
"h_nr_delayed" was only adjusted in dequeue_entities() for "!seep && !delayed" which would imply migration or a save + restore type operation and the whole "h_nr_delayed" adjusting was contained in {set,clear}_delayed() for delayed dequeue, finish delayed dequeue, and requeue.
Quoted 2 lines
I'm going to have a closer look
Thank you!
Quoted 9 lines
Quoted 9 lines
Reviewed-by Gautham R. Shenoy <[email protected]>Tested-by Swapnil Sapkal <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>---
[..snip..]
Signature · 2 lines
Thanks and Regards, Prateek
Quoted 67 lines · K Prateek Nayak
Hello Vincent,
Quoted 47 lines · Vincent Guittot
Hi Prateek,
Quoted 44 lines · K Prateek Nayak
set_delayed()adjustscfs_rq->h_nr_runnablefor the hierarchy when an entity is delayed irrespective of whether the entity corresponds to a task or acfs_rq.Consider the following scenario:
root
/ \
A B (*) delayed since B is no longer eligible on root
| |
Task0 Task1 <---dequeue_task_fair()- task blocksWhen Task1 blocks (
dequeue_entity()for task's se returns true),dequeue_entities()will continue adjusting cfs_rq->h_nr_* for the hierarchy of Task1. However, when thesched_entitycorresponding tocfs_rqB is delayed,set_delayed()will adjust theh_nr_runnablefor the hierarchy too leading to bothdequeue_entity()andset_delayed()decrementingh_nr_runnablefor the dequeue of the same task.A
SCHED_WARN_ON()to inspecth_nr_runnablepost its update indequeue_entities()like below:cfs_rq->h_nr_runnable -= h_nr_runnable; SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
This error is self correcting since
cfs_rqare per-cpu and cannot migrate. The entitiy is either picked for full dequeue or is requeued when a task wakes up below it. Both those paths callclear_delayed()which again incrementsh_nr_runnableof the hierarchy without considering if the entity corresponds to a task or not.
h_nr_runnablewill eventually reflect the correct value however in the interim, the incorrect values can still influence PELT calculation which usesse->runnable_weightorcfs_rq->h_nr_runnable.Since only delayed tasks take the early return path in
dequeue_entities()andenqueue_task_fair(), adjust theh_nr_runnablein {set,clear}_delayed() only when a task is delayed as this path skips the h_nr_* update loops and returns early.Quoted 6 lines
Quoted 4 lines
For entities corresponding to
cfs_rq, the h_nr_* update loop in the caller will do the right thing.Fixes 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")You probably mean
c2a295bffeaf("sched/fair: Add new cfs_rq.h_nr_runnable")You are right! I had done a git blame on
set_delayed()ad landed at commit76f2f783294dbut you are right, it should bec2a295bffeaf("sched/fair: Add new cfs_rq.h_nr_runnable") when the accounting was inverted to account runnable tasks. Thank you for pointing that out.Quoted 3 lines
Before we were tracking the opposite
h_nr_delayed. Did you see the problem only on tip/sched/core or also before the rework which addedh_nr_runnableand removedh_nr_delayed
Quoted 11 lines
The problem is on tip:sched/core. I did not encounter any anomalies on
76f2f783294d("sched/eevdf: More PELT vsDELAYED_DEQUEUE")"
h_nr_delayed" was only adjusted indequeue_entities()for "!seep && !delayed" which would imply migration or a save + restore type operation and the whole "h_nr_delayed" adjusting was contained in {set,clear}_delayed() for delayed dequeue, finish delayed dequeue, and requeue.Quoted 2 lines
I'm going to have a closer look
Your fix looks good to me. I also run some tests after re-adding h_nr_delayed and checking that h_nr_queued = h_nr_runnable + h_nr_delayed after each update and I didn't get any warning with your patch whereas I got one during boot without it (but none after that during my tests)
Thanks for catching this
Quoted 17 lines
Thank you!
Quoted 9 lines
Quoted 9 lines
Reviewed-by Gautham R. Shenoy <[email protected]>Tested-by Swapnil Sapkal <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>---
[..snip..]
--
Thanks and Regards,
Prateek
K Prateek Nayak
reviewerHello Vincent,
Quoted 68 lines · Vincent Guittot
Quoted 67 lines · K Prateek Nayak
Hello Vincent,
Quoted 47 lines · Vincent Guittot
Hi Prateek,
Quoted 44 lines · K Prateek Nayak
set_delayed()adjustscfs_rq->h_nr_runnablefor the hierarchy when an entity is delayed irrespective of whether the entity corresponds to a task or acfs_rq.Consider the following scenario:
root
/ \
A B (*) delayed since B is no longer eligible on root
| |
Task0 Task1 <---dequeue_task_fair()- task blocksWhen Task1 blocks (
dequeue_entity()for task's se returns true),dequeue_entities()will continue adjusting cfs_rq->h_nr_* for the hierarchy of Task1. However, when thesched_entitycorresponding tocfs_rqB is delayed,set_delayed()will adjust theh_nr_runnablefor the hierarchy too leading to bothdequeue_entity()andset_delayed()decrementingh_nr_runnablefor the dequeue of the same task.A
SCHED_WARN_ON()to inspecth_nr_runnablepost its update indequeue_entities()like below:cfs_rq->h_nr_runnable -= h_nr_runnable; SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
This error is self correcting since
cfs_rqare per-cpu and cannot migrate. The entitiy is either picked for full dequeue or is requeued when a task wakes up below it. Both those paths callclear_delayed()which again incrementsh_nr_runnableof the hierarchy without considering if the entity corresponds to a task or not.
h_nr_runnablewill eventually reflect the correct value however in the interim, the incorrect values can still influence PELT calculation which usesse->runnable_weightorcfs_rq->h_nr_runnable.Since only delayed tasks take the early return path in
dequeue_entities()andenqueue_task_fair(), adjust theh_nr_runnablein {set,clear}_delayed() only when a task is delayed as this path skips the h_nr_* update loops and returns early.Quoted 6 lines
Quoted 4 lines
For entities corresponding to
cfs_rq, the h_nr_* update loop in the caller will do the right thing.Fixes 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")You probably mean
c2a295bffeaf("sched/fair: Add new cfs_rq.h_nr_runnable")You are right! I had done a git blame on
set_delayed()ad landed at commit76f2f783294dbut you are right, it should bec2a295bffeaf("sched/fair: Add new cfs_rq.h_nr_runnable") when the accounting was inverted to account runnable tasks. Thank you for pointing that out.Quoted 3 lines
Before we were tracking the opposite
h_nr_delayed. Did you see the problem only on tip/sched/core or also before the rework which addedh_nr_runnableand removedh_nr_delayed
Quoted 8 lines
Quoted 8 lines
The problem is on tip:sched/core. I did not encounter any anomalies on
76f2f783294d("sched/eevdf: More PELT vsDELAYED_DEQUEUE")"
h_nr_delayed" was only adjusted indequeue_entities()for "!seep && !delayed" which would imply migration or a save + restore type operation and the whole "h_nr_delayed" adjusting was contained in {set,clear}_delayed() for delayed dequeue, finish delayed dequeue, and requeue.
So I was looking at it wrong when I was investigating on commit 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE") h_nr_delayed can never be larger than h_nr_running (h_nr_queued upstream) since the number of delayed tasks can never cross number of tasks queued below the given cfs_rq but with the following:
kernel/sched/fair.c +3 -0
@@ -7145,6 +7145,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->h_nr_running -= h_nr_running; cfs_rq->idle_h_nr_running -= idle_h_nr_running; cfs_rq->h_nr_delayed -= h_nr_delayed; +SCHED_WARN_ON (cfs_rq->h_nr_delayed > cfs_rq->h_nr_running);if (cfs_rq_is_idle (cfs_rq)) idle_h_nr_running = h_nr_running;
@@ -7185,6 +7186,8 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->idle_h_nr_running -= idle_h_nr_running; cfs_rq->h_nr_delayed -= h_nr_delayed; +SCHED_WARN_ON (cfs_rq->h_nr_delayed > cfs_rq->h_nr_running); +if (cfs_rq_is_idle (cfs_rq)) idle_h_nr_running = h_nr_running;
--
I can again consistently hit the warning without the fix on 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")
I think that the original "Fixes:" tag is indeed right.
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
I'm going to have a closer look
Your fix looks good to me. I also run some tests after re-adding
h_nr_delayedand checking thath_nr_queued=h_nr_runnable+h_nr_delayedafter each update and I didn't get any warning with your patch whereas I got one during boot without it (but none after that during my tests)
Could it be the case that h_nr_delayed counts a tiny bit higher than the actual number and h_nr_runnable counts a tiny bit lower by the same amount and they both correct each other to give the correct h_nr_queued?
Quoted 4 lines
Thanks for catching this
Reviewed-by Vincent Guittot <[email protected]>
Thank you for reviewing the patch!
Quoted 18 lines
Quoted 18 lines
Thank you!
Quoted 9 lines
Quoted 9 lines
Reviewed-by Gautham R. Shenoy <[email protected]>Tested-by Swapnil Sapkal <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>---
[..snip..]
--
Thanks and Regards,
Prateek
Signature · 2 lines
Thanks and Regards, Prateek
Vincent Guittot
maintainerQuoted 110 lines · K Prateek Nayak
Hello Vincent,
Quoted 68 lines · Vincent Guittot
Quoted 67 lines · K Prateek Nayak
Hello Vincent,
Quoted 47 lines · Vincent Guittot
Hi Prateek,
Quoted 44 lines · K Prateek Nayak
set_delayed()adjustscfs_rq->h_nr_runnablefor the hierarchy when an entity is delayed irrespective of whether the entity corresponds to a task or acfs_rq.Consider the following scenario:
root
/ \
A B (*) delayed since B is no longer eligible on root
| |
Task0 Task1 <---dequeue_task_fair()- task blocksWhen Task1 blocks (
dequeue_entity()for task's se returns true),dequeue_entities()will continue adjusting cfs_rq->h_nr_* for the hierarchy of Task1. However, when thesched_entitycorresponding tocfs_rqB is delayed,set_delayed()will adjust theh_nr_runnablefor the hierarchy too leading to bothdequeue_entity()andset_delayed()decrementingh_nr_runnablefor the dequeue of the same task.A
SCHED_WARN_ON()to inspecth_nr_runnablepost its update indequeue_entities()like below:cfs_rq->h_nr_runnable -= h_nr_runnable; SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
This error is self correcting since
cfs_rqare per-cpu and cannot migrate. The entitiy is either picked for full dequeue or is requeued when a task wakes up below it. Both those paths callclear_delayed()which again incrementsh_nr_runnableof the hierarchy without considering if the entity corresponds to a task or not.
h_nr_runnablewill eventually reflect the correct value however in the interim, the incorrect values can still influence PELT calculation which usesse->runnable_weightorcfs_rq->h_nr_runnable.Since only delayed tasks take the early return path in
dequeue_entities()andenqueue_task_fair(), adjust theh_nr_runnablein {set,clear}_delayed() only when a task is delayed as this path skips the h_nr_* update loops and returns early.Quoted 6 lines
Quoted 4 lines
For entities corresponding to
cfs_rq, the h_nr_* update loop in the caller will do the right thing.Fixes 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")You probably mean
c2a295bffeaf("sched/fair: Add new cfs_rq.h_nr_runnable")You are right! I had done a git blame on
set_delayed()ad landed at commit76f2f783294dbut you are right, it should bec2a295bffeaf("sched/fair: Add new cfs_rq.h_nr_runnable") when the accounting was inverted to account runnable tasks. Thank you for pointing that out.Quoted 3 lines
Before we were tracking the opposite
h_nr_delayed. Did you see the problem only on tip/sched/core or also before the rework which addedh_nr_runnableand removedh_nr_delayedQuoted 8 lines
Quoted 8 lines
The problem is on tip:sched/core. I did not encounter any anomalies on
76f2f783294d("sched/eevdf: More PELT vsDELAYED_DEQUEUE")"
h_nr_delayed" was only adjusted indequeue_entities()for "!seep && !delayed" which would imply migration or a save + restore type operation and the whole "h_nr_delayed" adjusting was contained in {set,clear}_delayed() for delayed dequeue, finish delayed dequeue, and requeue.So I was looking at it wrong when I was investigating on commit
76f2f783294d("sched/eevdf: More PELT vsDELAYED_DEQUEUE")h_nr_delayedcan never be larger thanh_nr_running(h_nr_queuedupstream) since the number of delayed tasks can never cross number of tasks queued below the givencfs_rqbut with the following:kernel/sched/fair.c +3
-1@@ -7145,6 +7145,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->h_nr_running -= h_nr_running; cfs_rq->idle_h_nr_running -= idle_h_nr_running; cfs_rq->h_nr_delayed -= h_nr_delayed; +SCHED_WARN_ON (cfs_rq->h_nr_delayed > cfs_rq->h_nr_running);if (cfs_rq_is_idle (cfs_rq)) idle_h_nr_running = h_nr_running;@@ -7185,6 +7186,8 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->idle_h_nr_running -= idle_h_nr_running; cfs_rq->h_nr_delayed -= h_nr_delayed; +SCHED_WARN_ON (cfs_rq->h_nr_delayed > cfs_rq->h_nr_running); +if (cfs_rq_is_idle (cfs_rq)) idle_h_nr_running = h_nr_running; --
Quoted 2 lines
I can again consistently hit the warning without the fix on
76f2f783294d("sched/eevdf: More PELT vsDELAYED_DEQUEUE")
I updated my warning conditions with
h_nr_queued != h_nr_delayed + h_nr_runnable
h_nr_delayed < 0 or > h_nr_queued
h_nr_runnable < 0 or > h_nr_queued
In addition to h_nr_runnable < 0, h_nr_delayed > h_nr_queued has also triggered a warning so I confirm that the fix is on 76f2f783294d ("sched/eevdf: More PELT vs DELAYED_DEQUEUE")
No warning are triggered with your fix
Quoted 48 lines
I think that the original "Fixes:" tag is indeed right.
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
I'm going to have a closer look
Your fix looks good to me. I also run some tests after re-adding
h_nr_delayedand checking thath_nr_queued=h_nr_runnable+h_nr_delayedafter each update and I didn't get any warning with your patch whereas I got one during boot without it (but none after that during my tests)Could it be the case that
h_nr_delayedcounts a tiny bit higher than the actual number andh_nr_runnablecounts a tiny bit lower by the same amount and they both correct each other to give the correcth_nr_queued?Quoted 4 lines
Thanks for catching this
Reviewed-by Vincent Guittot <[email protected]>Thank you for reviewing the patch!
Quoted 11 lines
Quoted 11 lines
Thank you!
Quoted 7 lines
Quoted 7 lines
Reviewed-by Gautham R. Shenoy <[email protected]>Tested-by Swapnil Sapkal <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>---
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
[..snip..]
--
Thanks and Regards,
Prateek--
Thanks and Regards,
Prateek
Hi Prateek,
Quoted 8 lines
A
SCHED_WARN_ON()to inspecth_nr_runnablepost its update indequeue_entities()like below:cfs_rq->h_nr_runnable -= h_nr_runnable; SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
I observed that the WARN_ON is triggered during the boot process without the patch, and the patch resolves the issue.
However, I was unable to trigger the WARN_ON by running hackbench in a cgroup without the patch. Could you please share the specific test scenario or configuration you used to reproduce it?
For the boot process scenario:
Thanks,
Madadi Vineeth Reddy
Quoted 3 lines
However, I was unable to trigger the
WARN_ONby running hackbench in a cgroup without the patch. Could you please share the specific test scenario or configuration you used to reproduce it?
My mistake: SCHED_WARN_ON is a WARN_ONCE, so I had to clear it before running hackbench to ensure the WARN_ON could be triggered again.
Now I am able to trigger the WARN_ON in the unpatched kernel while running hackbench after executing echo 1 > /sys/kernel/debug/clear_warn_once following the initial warning during the boot process. I have verified that the patched kernel fixes the issue.
Thanks,
Madadi Vineeth Reddy
K Prateek Nayak
reviewerQuoted 6 lines · Madadi Vineeth Reddy
Quoted 3 lines
However, I was unable to trigger the
WARN_ONby running hackbench in a cgroup without the patch. Could you please share the specific test scenario or configuration you used to reproduce it?My mistake:
SCHED_WARN_ONis aWARN_ONCE, so I had to clear it before running hackbench to ensure theWARN_ONcould be triggered again.
Yup! A WARN_ON() is hit very consistently.
Quoted 5 lines
Now I am able to trigger the
WARN_ONin the unpatched kernel while running hackbench after executingecho 1 > /sys/kernel/debug/clear_warn_oncefollowing the initial warning during the boot process. I have verified that the patched kernel fixes the issue.
Thank you again for testing the fix. Much appreciated!
Quoted 3 lines
Thanks,
Madadi Vineeth Reddy
Signature · 2 lines
Thanks and Regards, Prateek
K Prateek Nayak
reviewerHello Madadi Vineeth Reddy,
Thank you for the review and test.
Quoted 11 lines · Madadi Vineeth Reddy
Hi Prateek,
Quoted 8 lines
A
SCHED_WARN_ON()to inspecth_nr_runnablepost its update indequeue_entities()like below:cfs_rq->h_nr_runnable -= h_nr_runnable; SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
Quoted 6 lines
I observed that the
WARN_ONis triggered during the boot process without the patch, and the patch resolves the issue.However, I was unable to trigger the
WARN_ONby running hackbench in a cgroup without the patch. Could you please share the specific test scenario or configuration you used to reproduce it?
Can you try converting the SCHED_WARN_ON() to a WARN_ON() and try again. I can consistently hit it to a point that it floods my console. With autogroup enabled on Ubuntu, I believe it is trivial to hit this issue.
Following is the exact diff I'm using on top of tip:sched/core that floods my console:
kernel/sched/fair.c +2 -0
@@ -7160,6 +7160,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->h_nr_runnable -= h_nr_runnable; cfs_rq->h_nr_queued -= h_nr_queued; +WARN_ON (((int ) cfs_rq->h_nr_runnable) <0 ); cfs_rq->h_nr_idle -= h_nr_idle;if (cfs_rq_is_idle (cfs_rq))
@@ -7199,6 +7200,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) cfs_rq->h_nr_runnable -= h_nr_runnable; cfs_rq->h_nr_queued -= h_nr_queued; +WARN_ON (((int ) cfs_rq->h_nr_runnable) <0 ); cfs_rq->h_nr_idle -= h_nr_idle;if (cfs_rq_is_idle (cfs_rq))
--
I tested this on a 32 vCPU VM and I get similar floods.
Quoted 3 lines
For the boot process scenario:
Tested-by Madadi Vineeth Reddy <[email protected]>
Thanks a ton for testing!
Quoted 3 lines
Thanks,
Madadi Vineeth Reddy
Signature · 2 lines
Thanks and Regards, Prateek
The following commit has been merged into the sched/urgent branch of tip:
The following commit has been merged into the sched/urgent branch of tip:
Commit-ID: 3429dd57f0deb1a602c2624a1dd7c4c11b6c4734 Gitweb: https://git.kernel.org/tip/3429dd57f0deb1a602c2624a1dd7c4c11b6c4734 Author: K Prateek Nayak <[email protected]> AuthorDate: Fri, 17 Jan 2025 10:58:52 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 21 Jan 2025 13:13:36 +01:00
sched/fair: Fix inaccurate h_nr_runnable accounting with delayed dequeue
set_delayed() adjusts cfs_rq->h_nr_runnable for the hierarchy when an entity is delayed irrespective of whether the entity corresponds to a task or a cfs_rq.
Consider the following scenario:
root
/ \
A B (*) delayed since B is no longer eligible on root
| |
Task0 Task1 <--- dequeue_task_fair() - task blocksWhen Task1 blocks (dequeue_entity() for task's se returns true), dequeue_entities() will continue adjusting cfs_rq->h_nr_* for the hierarchy of Task1. However, when the sched_entity corresponding to cfs_rq B is delayed, set_delayed() will adjust the h_nr_runnable for the hierarchy too leading to both dequeue_entity() and set_delayed() decrementing h_nr_runnable for the dequeue of the same task.
A SCHED_WARN_ON() to inspect h_nr_runnable post its update in dequeue_entities() like below:
cfs_rq->h_nr_runnable -= h_nr_runnable;
SCHED_WARN_ON(((int) cfs_rq->h_nr_runnable) < 0);is consistently tripped when running wakeup intensive workloads like hackbench in a cgroup.
This error is self correcting since cfs_rq are per-cpu and cannot migrate. The entitiy is either picked for full dequeue or is requeued when a task wakes up below it. Both those paths call clear_delayed() which again increments h_nr_runnable of the hierarchy without considering if the entity corresponds to a task or not.
h_nr_runnable will eventually reflect the correct value however in the interim, the incorrect values can still influence PELT calculation which uses se->runnable_weight or cfs_rq->h_nr_runnable.
Since only delayed tasks take the early return path in dequeue_entities() and enqueue_task_fair(), adjust the h_nr_runnable in {set,clear}_delayed() only when a task is delayed as this path skips the h_nr_* update loops and returns early.
For entities corresponding to cfs_rq, the h_nr_* update loop in the caller will do the right thing.
--- kernel/sched/fair.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
kernel/sched/fair.c +19 -0
@@ -5372,6 +5372,15 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);static void set_delayed (struct sched_entity *se) { se->sched_delayed =1 ; + +/* +* Delayed se of cfs_rq have no tasks queued on them. +* Do not adjust h_nr_runnable since dequeue_entities() +* will account it for blocked tasks. +*/ +if (!entity_is_task (se)) +return ; +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);
@@ -5384,6 +5393,16 @@ static void set_delayed(struct sched_entity *se)static void clear_delayed (struct sched_entity *se) { se->sched_delayed =0 ; + +/* +* Delayed se of cfs_rq have no tasks queued on them. +* Do not adjust h_nr_runnable since a dequeue has +* already accounted for it or an enqueue of a task +* below it will account for it in enqueue_task_fair(). +*/ +if (!entity_is_task (se)) +return ; +for_each_sched_entity (se) {struct cfs_rq *cfs_rq =cfs_rq_of (se);