diff mbox series

hwmon: (pt5161l) Fix invalid temperature reading

Message ID 20240818194401.172248-1-chou.cosmo@gmail.com (mailing list archive)
State Superseded
Headers show
Series hwmon: (pt5161l) Fix invalid temperature reading | expand

Commit Message

Cosmo Chou Aug. 18, 2024, 7:44 p.m. UTC
The temperature reading function was using a signed long for the ADC
code, which could lead to mishandling of invalid codes on 32-bit
platforms. This allowed out-of-range ADC codes to be incorrectly
interpreted as valid values and used in temperature calculations.

Change adc_code to u32 to ensure that invalid ADC codes are correctly
identified on all platforms.

Fixes: 1b2ca93cd059 ("hwmon: Add driver for Astera Labs PT5161L retimer")
Signed-off-by: Cosmo Chou <chou.cosmo@gmail.com>
---
 drivers/hwmon/pt5161l.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

kernel test robot Aug. 19, 2024, 3:22 a.m. UTC | #1
Hi Cosmo,

kernel test robot noticed the following build warnings:

[auto build test WARNING on groeck-staging/hwmon-next]
[also build test WARNING on linus/master v6.11-rc4 next-20240816]
[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/Cosmo-Chou/hwmon-pt5161l-Fix-invalid-temperature-reading/20240819-034826
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
patch link:    https://lore.kernel.org/r/20240818194401.172248-1-chou.cosmo%40gmail.com
patch subject: [PATCH] hwmon: (pt5161l) Fix invalid temperature reading
config: i386-buildonly-randconfig-001-20240819 (https://download.01.org/0day-ci/archive/20240819/202408191001.pTmfYpUn-lkp@intel.com/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240819/202408191001.pTmfYpUn-lkp@intel.com/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@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202408191001.pTmfYpUn-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/hwmon/pt5161l.c:452:43: warning: format specifies type 'unsigned long' but the argument has type 'u32' (aka 'unsigned int') [-Wformat]
     452 |                         dev_dbg(dev, "Invalid adc_code %lx\n", adc_code);
         |                                                        ~~~     ^~~~~~~~
         |                                                        %x
   include/linux/dev_printk.h:168:46: note: expanded from macro 'dev_dbg'
     168 |         dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
         |                                             ~~~     ^~~~~~~~~~~
   include/linux/dev_printk.h:129:34: note: expanded from macro 'dev_printk'
     129 |                 _dev_printk(level, dev, fmt, ##__VA_ARGS__);            \
         |                                         ~~~    ^~~~~~~~~~~
   1 warning generated.


vim +452 drivers/hwmon/pt5161l.c

1b2ca93cd0592b Cosmo Chou 2024-02-06  423  
1b2ca93cd0592b Cosmo Chou 2024-02-06  424  static int pt5161l_read(struct device *dev, enum hwmon_sensor_types type,
1b2ca93cd0592b Cosmo Chou 2024-02-06  425  			u32 attr, int channel, long *val)
1b2ca93cd0592b Cosmo Chou 2024-02-06  426  {
1b2ca93cd0592b Cosmo Chou 2024-02-06  427  	struct pt5161l_data *data = dev_get_drvdata(dev);
1b2ca93cd0592b Cosmo Chou 2024-02-06  428  	int ret;
1b2ca93cd0592b Cosmo Chou 2024-02-06  429  	u8 buf[8];
4aa2c2144fb2eb Cosmo Chou 2024-08-19  430  	u32 adc_code;
1b2ca93cd0592b Cosmo Chou 2024-02-06  431  
1b2ca93cd0592b Cosmo Chou 2024-02-06  432  	switch (attr) {
1b2ca93cd0592b Cosmo Chou 2024-02-06  433  	case hwmon_temp_input:
1b2ca93cd0592b Cosmo Chou 2024-02-06  434  		if (!data->init_done) {
1b2ca93cd0592b Cosmo Chou 2024-02-06  435  			ret = pt5161l_init_dev(data);
1b2ca93cd0592b Cosmo Chou 2024-02-06  436  			if (ret)
1b2ca93cd0592b Cosmo Chou 2024-02-06  437  				return ret;
1b2ca93cd0592b Cosmo Chou 2024-02-06  438  		}
1b2ca93cd0592b Cosmo Chou 2024-02-06  439  
1b2ca93cd0592b Cosmo Chou 2024-02-06  440  		mutex_lock(&data->lock);
1b2ca93cd0592b Cosmo Chou 2024-02-06  441  		ret = pt5161l_read_wide_reg(data,
1b2ca93cd0592b Cosmo Chou 2024-02-06  442  					    ARIES_CURRENT_AVG_TEMP_ADC_CSR, 4,
1b2ca93cd0592b Cosmo Chou 2024-02-06  443  					    buf);
1b2ca93cd0592b Cosmo Chou 2024-02-06  444  		mutex_unlock(&data->lock);
1b2ca93cd0592b Cosmo Chou 2024-02-06  445  		if (ret) {
1b2ca93cd0592b Cosmo Chou 2024-02-06  446  			dev_dbg(dev, "Read adc_code failed %d\n", ret);
1b2ca93cd0592b Cosmo Chou 2024-02-06  447  			return ret;
1b2ca93cd0592b Cosmo Chou 2024-02-06  448  		}
1b2ca93cd0592b Cosmo Chou 2024-02-06  449  
1b2ca93cd0592b Cosmo Chou 2024-02-06  450  		adc_code = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
1b2ca93cd0592b Cosmo Chou 2024-02-06  451  		if (adc_code == 0 || adc_code >= 0x3ff) {
1b2ca93cd0592b Cosmo Chou 2024-02-06 @452  			dev_dbg(dev, "Invalid adc_code %lx\n", adc_code);
1b2ca93cd0592b Cosmo Chou 2024-02-06  453  			return -EIO;
1b2ca93cd0592b Cosmo Chou 2024-02-06  454  		}
1b2ca93cd0592b Cosmo Chou 2024-02-06  455  
1b2ca93cd0592b Cosmo Chou 2024-02-06  456  		*val = 110000 +
1b2ca93cd0592b Cosmo Chou 2024-02-06  457  		       ((adc_code - (ARIES_TEMP_CAL_CODE_DEFAULT + 250)) *
1b2ca93cd0592b Cosmo Chou 2024-02-06  458  			-320);
1b2ca93cd0592b Cosmo Chou 2024-02-06  459  		break;
1b2ca93cd0592b Cosmo Chou 2024-02-06  460  	default:
1b2ca93cd0592b Cosmo Chou 2024-02-06  461  		return -EOPNOTSUPP;
1b2ca93cd0592b Cosmo Chou 2024-02-06  462  	}
1b2ca93cd0592b Cosmo Chou 2024-02-06  463  
1b2ca93cd0592b Cosmo Chou 2024-02-06  464  	return 0;
1b2ca93cd0592b Cosmo Chou 2024-02-06  465  }
1b2ca93cd0592b Cosmo Chou 2024-02-06  466
diff mbox series

Patch

diff --git a/drivers/hwmon/pt5161l.c b/drivers/hwmon/pt5161l.c
index b0d58a26d499..46dd5c1723cf 100644
--- a/drivers/hwmon/pt5161l.c
+++ b/drivers/hwmon/pt5161l.c
@@ -427,7 +427,7 @@  static int pt5161l_read(struct device *dev, enum hwmon_sensor_types type,
 	struct pt5161l_data *data = dev_get_drvdata(dev);
 	int ret;
 	u8 buf[8];
-	long adc_code;
+	u32 adc_code;
 
 	switch (attr) {
 	case hwmon_temp_input: