Re: [PATCH] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu()

Valentin Schneider
2020-08-10T11:18:36Z

On 10/08/20 09:30, Lukasz Luba wrote:
> In find_energy_efficient_cpu() 'cpu_cap' could be less that 'util'.
> It might be because of RT, DL (so higher sched class than CFS), irq or
> thermal pressure signal, which reduce the capacity value.
> In such situation the result of 'cpu_cap - util' might be negative but
> stored in the unsigned long. Then it might be compared with other unsigned
> long when uclamp_rq_util_with() reduced the 'util' such that is passes the
> fits_capacity() check.
>
> Prevent this situation and make the arithmetic more safe.
>
> Fixes: 1d42509e475cd ("sched/fair: Make EAS wakeup placement consider
> uclamp restrictions")

I was going to say that might even go as far back as:

732cd75b8c92 ("sched/fair: Select an energy-efficient CPU on task wake-up")

but we had a capacity fitness check in the right place back then, which I
screwed over with that uclamp_rq_util_with() :/

LGTM, thanks for figuring that one out.

Reviewed-by: Valentin Schneider <valentin.schneider@arm.com>

> Signed-off-by: Lukasz Luba <lukasz.luba@arm.com>
> ---
> kernel/sched/fair.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 1a68a0536add..51408ebd76c2 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -6594,7 +6594,8 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu)
>
> util = cpu_util_next(cpu, p, cpu);
> cpu_cap = capacity_of(cpu);
> - spare_cap = cpu_cap - util;
> + spare_cap = cpu_cap;
> + lsub_positive(&spare_cap, util);
>
> /*
> * Skip CPUs that cannot satisfy the capacity request.