linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu()
@ 2020-08-10  8:30 Lukasz Luba
  2020-08-10 11:18 ` Valentin Schneider
  2020-08-27  7:54 ` [tip: sched/core] " tip-bot2 for Lukasz Luba
  0 siblings, 2 replies; 4+ messages in thread
From: Lukasz Luba @ 2020-08-10  8:30 UTC (permalink / raw)
  To: linux-kernel, peterz, mingo, vincent.guittot, mgorman,
	dietmar.eggemann, valentin.schneider, lukasz.luba
  Cc: juri.lelli, patrick.bellasi, tglx, rostedt

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")
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.
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu()
  2020-08-10  8:30 [PATCH] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu() Lukasz Luba
@ 2020-08-10 11:18 ` Valentin Schneider
  2020-08-10 14:05   ` peterz
  2020-08-27  7:54 ` [tip: sched/core] " tip-bot2 for Lukasz Luba
  1 sibling, 1 reply; 4+ messages in thread
From: Valentin Schneider @ 2020-08-10 11:18 UTC (permalink / raw)
  To: Lukasz Luba
  Cc: linux-kernel, peterz, mingo, vincent.guittot, mgorman,
	dietmar.eggemann, juri.lelli, patrick.bellasi, tglx, rostedt


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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu()
  2020-08-10 11:18 ` Valentin Schneider
@ 2020-08-10 14:05   ` peterz
  0 siblings, 0 replies; 4+ messages in thread
From: peterz @ 2020-08-10 14:05 UTC (permalink / raw)
  To: Valentin Schneider
  Cc: Lukasz Luba, linux-kernel, mingo, vincent.guittot, mgorman,
	dietmar.eggemann, juri.lelli, patrick.bellasi, tglx, rostedt

On Mon, Aug 10, 2020 at 12:18:25PM +0100, Valentin Schneider wrote:
> 
> 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>

Thanks guys!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tip: sched/core] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu()
  2020-08-10  8:30 [PATCH] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu() Lukasz Luba
  2020-08-10 11:18 ` Valentin Schneider
@ 2020-08-27  7:54 ` tip-bot2 for Lukasz Luba
  1 sibling, 0 replies; 4+ messages in thread
From: tip-bot2 for Lukasz Luba @ 2020-08-27  7:54 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Lukasz Luba, Peter Zijlstra (Intel), Valentin Schneider, x86, LKML

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     da0777d35f47892f359c3f73ea155870bb595700
Gitweb:        https://git.kernel.org/tip/da0777d35f47892f359c3f73ea155870bb595700
Author:        Lukasz Luba <lukasz.luba@arm.com>
AuthorDate:    Mon, 10 Aug 2020 09:30:04 +01:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Wed, 26 Aug 2020 12:41:57 +02:00

sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu()

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")
Signed-off-by: Lukasz Luba <lukasz.luba@arm.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Valentin Schneider <valentin.schneider@arm.com>
Link: https://lkml.kernel.org/r/20200810083004.26420-1-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 abdb54e..90ebaa4 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.

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-27  7:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-10  8:30 [PATCH] sched/fair: Fix wrong negative conversion in find_energy_efficient_cpu() Lukasz Luba
2020-08-10 11:18 ` Valentin Schneider
2020-08-10 14:05   ` peterz
2020-08-27  7:54 ` [tip: sched/core] " tip-bot2 for Lukasz Luba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).