[For-STABLE] thermal/drivers/cpufreq_cooling: Fix wrong frequency converted from power

From: Viresh Kumar
Date: Wed Jul 08 2020 - 03:54:00 EST


From: Finley Xiao <finley.xiao@xxxxxxxxxxxxxx>

commit 371a3bc79c11b707d7a1b7a2c938dc3cc042fffb upstream.

The function cpu_power_to_freq is used to find a frequency and set the
cooling device to consume at most the power to be converted. For example,
if the power to be converted is 80mW, and the em table is as follow.
struct em_cap_state table[] = {
/* KHz mW */
{ 1008000, 36, 0 },
{ 1200000, 49, 0 },
{ 1296000, 59, 0 },
{ 1416000, 72, 0 },
{ 1512000, 86, 0 },
};
The target frequency should be 1416000KHz, not 1512000KHz.

Fixes: 349d39dc5739 ("thermal: cpu_cooling: merge frequency and power tables")
Cc: <stable@xxxxxxxxxxxxxxx> # v4.13+
Signed-off-by: Finley Xiao <finley.xiao@xxxxxxxxxxxxxx>
Acked-by: Viresh Kumar <viresh.kumar@xxxxxxxxxx>
Reviewed-by: Amit Kucheria <amit.kucheria@xxxxxxxxxx>
Signed-off-by: Daniel Lezcano <daniel.lezcano@xxxxxxxxxx>
Link: https://lore.kernel.org/r/20200619090825.32747-1-finley.xiao@xxxxxxxxxxxxxx
Signed-off-by: Viresh Kumar <viresh.kumar@xxxxxxxxxx>
---
Hi Greg,

I am resending this as I got your emails of this failing on 4.14, 4.19
and 5.4. This should be applied to all three of them.

@Finley: I hope I have done it correctly, please do check it as this
required me to rewrite the code to adapt to previous kernels.

drivers/thermal/cpu_cooling.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
index 908a8014cf76..1f4387a5ceae 100644
--- a/drivers/thermal/cpu_cooling.c
+++ b/drivers/thermal/cpu_cooling.c
@@ -280,11 +280,11 @@ static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
int i;
struct freq_table *freq_table = cpufreq_cdev->freq_table;

- for (i = 1; i <= cpufreq_cdev->max_level; i++)
- if (power > freq_table[i].power)
+ for (i = 0; i < cpufreq_cdev->max_level; i++)
+ if (power >= freq_table[i].power)
break;

- return freq_table[i - 1].frequency;
+ return freq_table[i].frequency;
}

/**
--
2.25.0.rc1.19.g042ed3e048af