Re: [PATCH v2] PM: EM: Fix potential division-by-zero error in em_compute_costs()

From: kernel test robot
Date: Fri Apr 11 2025 - 21:56:08 EST


Hi Yaxiong,

kernel test robot noticed the following build warnings:

[auto build test WARNING on amd-pstate/linux-next]
[also build test WARNING on amd-pstate/bleeding-edge linus/master v6.15-rc1 next-20250411]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Yaxiong-Tian/PM-EM-Fix-potential-division-by-zero-error-in-em_compute_costs/20250411-093452
base: https://git.kernel.org/pub/scm/linux/kernel/git/superm1/linux.git linux-next
patch link: https://lore.kernel.org/r/tencent_EE27C7D1D6BDB3EE57A2C467CC59A866C405%40qq.com
patch subject: [PATCH v2] PM: EM: Fix potential division-by-zero error in em_compute_costs()
config: i386-buildonly-randconfig-003-20250412 (https://download.01.org/0day-ci/archive/20250412/202504120921.Wqmipx6e-lkp@xxxxxxxxx/config)
compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250412/202504120921.Wqmipx6e-lkp@xxxxxxxxx/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@xxxxxxxxx>
| Closes: https://lore.kernel.org/oe-kbuild-all/202504120921.Wqmipx6e-lkp@xxxxxxxxx/

All warnings (new ones prefixed by >>):

>> kernel/power/energy_model.c:247:14: warning: variable 'cost' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
247 | } else if (_is_cpu_device(dev)) {
| ^~~~~~~~~~~~~~~~~~~
kernel/power/energy_model.c:253:19: note: uninitialized use occurs here
253 | table[i].cost = cost;
| ^~~~
kernel/power/energy_model.c:247:10: note: remove the 'if' if its condition is always true
247 | } else if (_is_cpu_device(dev)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~
kernel/power/energy_model.c:238:32: note: initialize the variable 'cost' to silence this warning
238 | unsigned long power_res, cost;
| ^
| = 0
1 warning generated.


vim +247 kernel/power/energy_model.c

228
229 static int em_compute_costs(struct device *dev, struct em_perf_state *table,
230 const struct em_data_callback *cb, int nr_states,
231 unsigned long flags)
232 {
233 unsigned long prev_cost = ULONG_MAX;
234 int i, ret;
235
236 /* Compute the cost of each performance state. */
237 for (i = nr_states - 1; i >= 0; i--) {
238 unsigned long power_res, cost;
239
240 if ((flags & EM_PERF_DOMAIN_ARTIFICIAL) && cb->get_cost) {
241 ret = cb->get_cost(dev, table[i].frequency, &cost);
242 if (ret || !cost || cost > EM_MAX_POWER) {
243 dev_err(dev, "EM: invalid cost %lu %d\n",
244 cost, ret);
245 return -EINVAL;
246 }
> 247 } else if (_is_cpu_device(dev)) {
248 /* increase resolution of 'cost' precision */
249 power_res = table[i].power * 10;
250 cost = power_res / table[i].performance;
251 }
252
253 table[i].cost = cost;
254
255 if (table[i].cost >= prev_cost) {
256 table[i].flags = EM_PERF_STATE_INEFFICIENT;
257 dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
258 table[i].frequency);
259 } else {
260 prev_cost = table[i].cost;
261 }
262 }
263
264 return 0;
265 }
266

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki