cpufreq/amd-pstate: Fix the clamping of perf values
Subsystems (2)
PATCH 1/1 cpufreq/amd-pstate: Fix the clamping of perf values
The clamping in freq_to_perf() is broken right now, as we first typecast (read wraparound) the overflowing value into a u8 and then clamp it down. So, use a u32 to store the >255 value in certain edge cases and then clamp it down into a u8.
Also, use a "explicit typecast + clamp" instead of just a "clamp_t" as the latter typecasts first and then clamps between the limits, which defeats our purpose.
--- drivers/cpufreq/amd-pstate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpufreq/amd-pstate.c +2 -2
@@ -144,10 +144,10 @@ static struct quirk_entry quirk_amd_7k62 = {static inline u8 freq_to_perf (struct amd_cpudata *cpudata,unsigned int freq_val) { -u8 perf_val =DIV_ROUND_UP_ULL ((u64 )freq_val * cpudata->nominal_perf, +u32 perf_val =DIV_ROUND_UP_ULL ((u64 )freq_val * cpudata->nominal_perf, cpudata->nominal_freq); -return clamp_t (u8, perf_val, cpudata->lowest_perf, cpudata->highest_perf); +return (u8 )clamp (perf_val, cpudata->lowest_perf, cpudata->highest_perf); }static inline u32 perf_to_freq (struct amd_cpudata *cpudata,u8 perf_val)
Signature · 1 line
2.34.1
Quoted 11 lines · Dhananjay Ugwekar
The clamping in
freq_to_perf()is broken right now, as we first typecast (read wraparound) the overflowing value into a u8 and then clamp it down. So, use a u32 to store the >255 value in certain edge cases and then clamp it down into a u8.Also, use a "explicit typecast + clamp" instead of just a "
clamp_t" as the latter typecasts first and then clamps between the limits, which defeats our purpose.Fixes 305621eb6a8b ("cpufreq/amd-pstate: Modularize perf<->freq conversion")Signed-off-by Dhananjay Ugwekar <[email protected]>
I'll rebase my series on this for v5.
Quoted 21 lines
--- drivers/cpufreq/amd-pstate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/cpufreq/amd-pstate.c +2
-2@@ -144,10 +144,10 @@ static struct quirk_entry quirk_amd_7k62 = {static inline u8 freq_to_perf (struct amd_cpudata *cpudata,unsigned int freq_val) { -u8 perf_val =DIV_ROUND_UP_ULL ((u64 )freq_val * cpudata->nominal_perf, +u32 perf_val =DIV_ROUND_UP_ULL ((u64 )freq_val * cpudata->nominal_perf, cpudata->nominal_freq); -return clamp_t (u8, perf_val, cpudata->lowest_perf, cpudata->highest_perf); +return (u8 )clamp (perf_val, cpudata->lowest_perf, cpudata->highest_perf); }static inline u32 perf_to_freq (struct amd_cpudata *cpudata,u8 perf_val)