Skip to contentSkip to conversation

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

PATCH Applied Lukasz Luba 4 participants 4 messages Aug 10, 2020 – Aug 27, 2020 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index 1a68a0536add..51408ebd76c2 100644
@@ -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.
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Lukasz Luba

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.

Quoted 4 lines

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.

Quoted 19 lines
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index 1a68a0536add..51408ebd76c2 100644
@@ -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 lore raw
Quoted 17 lines · Valentin Schneider
Quoted 8 lines · Lukasz Luba

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.

Quoted 4 lines

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:

Quoted 8 lines

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.

Thanks guys!

merge bot Applied
#3 lore raw
In reply to Lukasz Luba
The following commit has been merged into the sched/core branch of tip:

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 <[email protected]>
AuthorDate:    Mon, 10 Aug 2020 09:30:04 +01:00
Committer:     Peter Zijlstra <[email protected]>
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")
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index abdb54e..90ebaa4 100644
@@ -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.