Re: [PATCH v3 1/2] sched: Fix the check of nr_running at queue wakelist

From: Valentin Schneider
Date: Mon Jun 06 2022 - 06:39:24 EST


On 02/06/22 12:06, Tianchen Ding wrote:
> The commit 2ebb17717550 ("sched/core: Offload wakee task activation if it
> the wakee is descheduling") checked nr_running <= 1 to avoid task
> stacking when WF_ON_CPU. Consider the order of p->on_rq and p->on_cpu,
> if we have WF_ON_CPU here then we must have !p->on_rq, so the deactivate
> has happened, thus the task being alone on the rq implies nr_running==0.
> Change the check to !cpu_rq(cpu)->nr_running to fix it.
>

I'd flesh this out a bit as in the below:

"""
The commit 2ebb17717550 ("sched/core: Offload wakee task activation if it
the wakee is descheduling") checked rq->nr_running <= 1 to avoid task
stacking when WF_ON_CPU.

Per the ordering of writes to p->on_rq and p->on_cpu, observing p->on_cpu
(WF_ON_CPU) in ttwu_queue_cond() implies !p->on_rq, IOW p has gone through
the deactivate_task() in __schedule(), thus p has been accounted out of
rq->nr_running. As such, the task being the only runnable task on the rq
implies reading rq->nr_running == 0 at that point.

Change the check to !cpu_rq(cpu)->nr_running.
"""

Also, this is lacking some mention of tests that have been run to verify
this isn't causing a regression. This does however make sense to me, so as
long as nothing gets hurt by the change:

Reviewed-by: Valentin Schneider <vschneid@xxxxxxxxxx>

> Signed-off-by: Tianchen Ding <dtcccc@xxxxxxxxxxxxxxxxx>
> ---
> kernel/sched/core.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index bfa7452ca92e..a9efe134fbe5 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3830,7 +3830,7 @@ static inline bool ttwu_queue_cond(int cpu, int wake_flags)
> * the soon-to-be-idle CPU as the current CPU is likely busy.
> * nr_running is checked to avoid unnecessary task stacking.
> */
> - if ((wake_flags & WF_ON_CPU) && cpu_rq(cpu)->nr_running <= 1)
> + if ((wake_flags & WF_ON_CPU) && !cpu_rq(cpu)->nr_running)
> return true;
>
> return false;
> --
> 2.27.0