[rui:amd-pstate-dev-v1 4/19] drivers/cpufreq/amd-pstate.c:241 amd_get_max_freq() warn: should 'max_perf << 10' be a 64 bit type?

From: Dan Carpenter
Date: Tue Sep 14 2021 - 06:29:59 EST


tree: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git amd-pstate-dev-v1
head: abfcbc164c1aa0c63d5e256854bad977a9645586
commit: df9ad0b99a9f0e3371aa94e49fe92a2c2a9fa95d [4/19] cpufreq: amd: introduce a new amd pstate driver to support future processors
config: i386-randconfig-m021-20210912 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>

smatch warnings:
drivers/cpufreq/amd-pstate.c:241 amd_get_max_freq() warn: should 'max_perf << 10' be a 64 bit type?
drivers/cpufreq/amd-pstate.c:282 amd_get_lowest_nonlinear_freq() warn: should 'lowest_nonlinear_perf << 10' be a 64 bit type?

vim +241 drivers/cpufreq/amd-pstate.c

df9ad0b99a9f0e Huang Rui 2021-06-10 227 static int amd_get_max_freq(struct amd_cpudata *cpudata)
df9ad0b99a9f0e Huang Rui 2021-06-10 228 {
df9ad0b99a9f0e Huang Rui 2021-06-10 229 struct cppc_perf_caps cppc_perf;
df9ad0b99a9f0e Huang Rui 2021-06-10 230 u32 max_perf, max_freq, nominal_freq, nominal_perf;
df9ad0b99a9f0e Huang Rui 2021-06-10 231 u64 boost_ratio;
df9ad0b99a9f0e Huang Rui 2021-06-10 232
df9ad0b99a9f0e Huang Rui 2021-06-10 233 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
df9ad0b99a9f0e Huang Rui 2021-06-10 234 if (ret)
df9ad0b99a9f0e Huang Rui 2021-06-10 235 return ret;
df9ad0b99a9f0e Huang Rui 2021-06-10 236
df9ad0b99a9f0e Huang Rui 2021-06-10 237 nominal_freq = cppc_perf.nominal_freq;
df9ad0b99a9f0e Huang Rui 2021-06-10 238 nominal_perf = READ_ONCE(cpudata->nominal_perf);
df9ad0b99a9f0e Huang Rui 2021-06-10 239 max_perf = READ_ONCE(cpudata->highest_perf);
df9ad0b99a9f0e Huang Rui 2021-06-10 240
df9ad0b99a9f0e Huang Rui 2021-06-10 @241 boost_ratio = div_u64(max_perf << SCHED_CAPACITY_SHIFT,
df9ad0b99a9f0e Huang Rui 2021-06-10 242 nominal_perf);

Presumably the max_perf << SCHED_CAPACITY_SHIFT shift can wrap. The
"max_perf" variable should probably be declared as a u64. I will also
create a new static checker warning specifically for when div_u64() is
used on a u32.

df9ad0b99a9f0e Huang Rui 2021-06-10 243
df9ad0b99a9f0e Huang Rui 2021-06-10 244 max_freq = nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT;
df9ad0b99a9f0e Huang Rui 2021-06-10 245
df9ad0b99a9f0e Huang Rui 2021-06-10 246 /* Switch to khz */
df9ad0b99a9f0e Huang Rui 2021-06-10 247 return max_freq * 1000;
df9ad0b99a9f0e Huang Rui 2021-06-10 248 }
df9ad0b99a9f0e Huang Rui 2021-06-10 249
df9ad0b99a9f0e Huang Rui 2021-06-10 250 static int amd_get_nominal_freq(struct amd_cpudata *cpudata)
df9ad0b99a9f0e Huang Rui 2021-06-10 251 {
df9ad0b99a9f0e Huang Rui 2021-06-10 252 struct cppc_perf_caps cppc_perf;
df9ad0b99a9f0e Huang Rui 2021-06-10 253 u32 nominal_freq;
df9ad0b99a9f0e Huang Rui 2021-06-10 254
df9ad0b99a9f0e Huang Rui 2021-06-10 255 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
df9ad0b99a9f0e Huang Rui 2021-06-10 256 if (ret)
df9ad0b99a9f0e Huang Rui 2021-06-10 257 return ret;
df9ad0b99a9f0e Huang Rui 2021-06-10 258
df9ad0b99a9f0e Huang Rui 2021-06-10 259 nominal_freq = cppc_perf.nominal_freq;
df9ad0b99a9f0e Huang Rui 2021-06-10 260
df9ad0b99a9f0e Huang Rui 2021-06-10 261 /* Switch to khz */
df9ad0b99a9f0e Huang Rui 2021-06-10 262 return nominal_freq * 1000;
df9ad0b99a9f0e Huang Rui 2021-06-10 263 }
df9ad0b99a9f0e Huang Rui 2021-06-10 264
df9ad0b99a9f0e Huang Rui 2021-06-10 265 static int amd_get_lowest_nonlinear_freq(struct amd_cpudata *cpudata)
df9ad0b99a9f0e Huang Rui 2021-06-10 266 {
df9ad0b99a9f0e Huang Rui 2021-06-10 267 struct cppc_perf_caps cppc_perf;
df9ad0b99a9f0e Huang Rui 2021-06-10 268 u32 lowest_nonlinear_freq, lowest_nonlinear_perf,
df9ad0b99a9f0e Huang Rui 2021-06-10 269 nominal_freq, nominal_perf;
df9ad0b99a9f0e Huang Rui 2021-06-10 270 u64 lowest_nonlinear_ratio;
df9ad0b99a9f0e Huang Rui 2021-06-10 271
df9ad0b99a9f0e Huang Rui 2021-06-10 272 int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
df9ad0b99a9f0e Huang Rui 2021-06-10 273 if (ret)
df9ad0b99a9f0e Huang Rui 2021-06-10 274 return ret;
df9ad0b99a9f0e Huang Rui 2021-06-10 275
df9ad0b99a9f0e Huang Rui 2021-06-10 276 nominal_freq = cppc_perf.nominal_freq;
df9ad0b99a9f0e Huang Rui 2021-06-10 277 nominal_perf = READ_ONCE(cpudata->nominal_perf);
df9ad0b99a9f0e Huang Rui 2021-06-10 278
df9ad0b99a9f0e Huang Rui 2021-06-10 279 lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf;
df9ad0b99a9f0e Huang Rui 2021-06-10 280
df9ad0b99a9f0e Huang Rui 2021-06-10 281 lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf <<
df9ad0b99a9f0e Huang Rui 2021-06-10 @282 SCHED_CAPACITY_SHIFT, nominal_perf);

Same.

df9ad0b99a9f0e Huang Rui 2021-06-10 283
df9ad0b99a9f0e Huang Rui 2021-06-10 284 lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT;
df9ad0b99a9f0e Huang Rui 2021-06-10 285
df9ad0b99a9f0e Huang Rui 2021-06-10 286 /* Switch to khz */
df9ad0b99a9f0e Huang Rui 2021-06-10 287 return lowest_nonlinear_freq * 1000;
df9ad0b99a9f0e Huang Rui 2021-06-10 288 }

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@xxxxxxxxxxxx