xen/sched: fix race in RTDS scheduler
When a domain gets paused the unit runnable state can change to "not runnable" without the scheduling lock being involved. This means that a specific scheduler isn't involved in this change of runnable state.
In the RTDS scheduler this can result in an inconsistency in case a unit is losing its "runnable" capability while the RTDS scheduler's scheduling function is active. RTDS will remove the unit from the run queue, but doesn't do so for the replenish queue, leading to hitting an ASSERT() in replq_insert() later when the domain is unpaused again.
Fix that by removing the unit from the replenish queue as well in this case.
--- xen/common/sched/rt.c | 1 + 1 file changed, 1 insertion(+)
xen/common/sched/rt.c +1 -0
@@ -1087,6 +1087,7 @@ rt_schedule(const struct scheduler *ops, struct sched_unit *currunit,else if ( !unit_runnable_state (snext->unit) ) {q_remove (snext); +replq_remove (ops, snext); snext =rt_unit (sched_idle_unit (sched_cpu)); }
Signature · 1 line
2.35.3
Hi Juergen,
Quoted 14 lines
-----Original Message-----
From: Juergen Gross <[email protected]>
Subject: [PATCH-for-4.17] xen/sched: fix race in RTDS schedulerWhen a domain gets paused the unit runnable state can change to "not runnable" without the scheduling lock being involved. This means that a specific scheduler isn't involved in this change of runnable state.
In the RTDS scheduler this can result in an inconsistency in case a unit is losing its "runnable" capability while the RTDS scheduler's scheduling function is active. RTDS will remove the unit from the run queue, but doesn't do so for the replenish queue, leading to hitting an
ASSERT()inreplq_insert()later when the domain is unpaused again.
Quoted 5 lines
Fix that by removing the unit from the replenish queue as well in this case.
Fixes 7c7b407e7772 ("xen/sched: introduce unit_runnable_state()")Signed-off-by Juergen Gross <[email protected]>
Thanks for the quick fix.
Kind regards,
Henry
Quoted 19 lines
--- xen/common/sched/rt.c | 1 + 1 file changed, 1 insertion(+)xen/common/sched/rt.c +1
-1@@ -1087,6 +1087,7 @@ rt_schedule(const struct scheduler *ops, structsched_unit *currunit, else if ( !unit_runnable_state(snext->unit) ) {q_remove (snext ); +replq_remove (ops, snext); snext =rt_unit (sched_idle_unit (sched_cpu)); } --2.35.3
Dario Faggioli
Acked-byQuoted 11 lines · Juergen Gross
When a domain gets paused the unit runnable state can change to "not runnable" without the scheduling lock being involved. This means that a specific scheduler isn't involved in this change of runnable state.
In the RTDS scheduler this can result in an inconsistency in case a unit is losing its "runnable" capability while the RTDS scheduler's scheduling function is active. RTDS will remove the unit from the run queue, but doesn't do so for the replenish queue, leading to hitting an
ASSERT()inreplq_insert()later when the domain is unpaused again.
Quoted 4 lines
Fix that by removing the unit from the replenish queue as well in this
case.
Ah, ok... So, all is fine until what could happen during rt_schedule(), was "just" that the currently running task, not only is descheduled, but it also became !runnable.
In fact, in this case, the unit itself is not in the runq, but it can be in the replq. However, since it still has the RTDS_scheduled flag set, either:
1) we reach rt_context_saved(), which remove it from replq, before any replq_insert;
2) rt_unit_wake() is called, but due to RTDS_scheduled, it may only do replq_reinsert(), which is fine with the unit being already there.
However, what can also happen in rt_schedule() is that we remove from the runq an unit that was not running, and hence does not have the RTDS_scheduled flat set. In which case, rt_context_saved() doesn't do anything to it (of course!). And as soon as rt_unit_wake() happens, it does replq_insert(), which is not fine with finding the replenishment event in the queue already.
So, yes... And good catch! :-P
Quoted 3 lines
Fixes 7c7b407e7772 ("xen/sched: introduce unit_runnable_state()")Signed-off-by Juergen Gross <[email protected]>
Regards
Signature · 6 lines
Dario Faggioli, Ph.D http://about.me/dario.faggioli Virtualization Software Engineer SUSE Labs, SUSE https://www.suse.com/ ------------------------------------------------------------------- <<This happens because _I_ choose it to happen!>> (Raistlin Majere)
Ok, and now, something not really related to the bug being fixed here, but about the code that is being touched:
Quoted 12 lines · Juergen Gross
xen/common/sched/rt.c +1
-0@@ -1087,6 +1087,7 @@ rt_schedule(const struct scheduler *ops, struct sched_unit *currunit, else if ( !unit_runnable_state(snext->unit) ) { q_remove(snext); + replq_remove(ops, snext); snext = rt_unit(sched_idle_unit(sched_cpu)); }
Quoted 1 line
So, adding a few more context here, the code looks like this:
snext = runq_pick(ops, cpumask_of(sched_cpu), cur_cpu);
if ( snext == NULL )
snext = rt_unit(sched_idle_unit(sched_cpu));
else if ( !unit_runnable_state(snext->unit) )
{
q_remove(snext);
snext = rt_unit(sched_idle_unit(sched_cpu));
}Basically, we've tried to pick-up the highest priority task from the runqueue. If snext is NULL, the runqueue was just empty, so we pick up idle (and then, later, we'll check whether the currently running unit is still runnable; and if it is, we'll continue to run it, of course).
However, it can happen that --e.g., due to core-scheduling-- we picked up a unit that, despite being in the runqueue, is not runnable. At this point what we do is removing it from the runqueue (to avoid picking it up again) and we go for idle.
Now, I may be missing/misremembering something, but it looks to me that it's possible that there are other runnable units in the runqueue. And if that's the case, why do we just pick idle and move on, instead of continuing trying?
Juergen... Am I missing or misremembering any fundamental reason why we cannot continue to scan the runqueue until the first runnable unit (if any) is found?
Of course, this is not really related with the bug this patch is fixing, which is correct and should be applied, no matter what the outcome of this subthread will be. :-)
Thanks and Regards
Signature · 6 lines
Dario Faggioli, Ph.D http://about.me/dario.faggioli Virtualization Software Engineer SUSE Labs, SUSE https://www.suse.com/ ------------------------------------------------------------------- <<This happens because _I_ choose it to happen!>> (Raistlin Majere)
Quoted 39 lines · Dario Faggioli
Ok, and now, something not really related to the bug being fixed here, but about the code that is being touched:
xen/common/sched/rt.c rt_schedule(const struct scheduler *ops, struct · line 1093
Quoted 13 lines · Juergen Gross
xen/common/sched/rt.c +1
-0@@ -1087,6 +1087,7 @@ rt_schedule(const struct scheduler *ops, struct sched_unit *currunit, else if ( !unit_runnable_state(snext->unit) ) { q_remove(snext); + replq_remove(ops, snext); snext = rt_unit(sched_idle_unit(sched_cpu)); }So, adding a few more context here, the code looks like this:
snext = runq_pick(ops, cpumask_of(sched_cpu), cur_cpu); if ( snext == NULL ) snext = rt_unit(sched_idle_unit(sched_cpu)); else if ( !unit_runnable_state(snext->unit) ) { q_remove(snext); snext = rt_unit(sched_idle_unit(sched_cpu)); }Basically, we've tried to pick-up the highest priority task from the runqueue. If snext is NULL, the runqueue was just empty, so we pick up idle (and then, later, we'll check whether the currently running unit is still runnable; and if it is, we'll continue to run it, of course).
However, it can happen that --e.g., due to core-scheduling-- we picked up a unit that, despite being in the runqueue, is not runnable. At this point what we do is removing it from the runqueue (to avoid picking it up again) and we go for idle.
Quoted 8 lines
Now, I may be missing/misremembering something, but it looks to me that it's possible that there are other runnable units in the runqueue. And if that's the case, why do we just pick idle and move on, instead of continuing trying?
Juergen... Am I missing or misremembering any fundamental reason why we cannot continue to scan the runqueue until the first runnable unit (if any) is found?
No. This code was introduced in the RFC V2 series of core scheduling. And it was not the result of a previous discussion on xen-devel.
Quoted 3 lines
Of course, this is not really related with the bug this patch is fixing, which is correct and should be applied, no matter what the outcome of this subthread will be. :-)
I can write another patch trying to fix that, but that shouldn't be 4.17 material IMHO.
Juergen