linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: lm80: fix a missing check of return value
@ 2018-12-21  6:13 Kangjie Lu
  2018-12-21 14:43 ` Guenter Roeck
  2019-01-02 15:23 ` Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: Kangjie Lu @ 2018-12-21  6:13 UTC (permalink / raw)
  To: kjlu; +Cc: pakki001, Jean Delvare, Guenter Roeck, linux-hwmon, linux-kernel

If lm80_read_value() fails, it returns a negative number instead of the
correct read data. Therefore, we should avoid using the data if it
fails.

The fix checks if lm80_read_value() fails, and if so, returns with the
error number.

Signed-off-by: Kangjie Lu <kjlu@umn.edu>
---
 drivers/hwmon/lm80.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/hwmon/lm80.c b/drivers/hwmon/lm80.c
index 08e3945a6fbf..fca6363cd77f 100644
--- a/drivers/hwmon/lm80.c
+++ b/drivers/hwmon/lm80.c
@@ -360,9 +360,11 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
 	struct i2c_client *client = data->client;
 	unsigned long min, val;
 	u8 reg;
-	int err = kstrtoul(buf, 10, &val);
-	if (err < 0)
-		return err;
+	int ret;
+
+	ret = kstrtoul(buf, 10, &val);
+	if (ret < 0)
+		return ret;
 
 	/* Save fan_min */
 	mutex_lock(&data->update_lock);
@@ -390,8 +392,11 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
 		return -EINVAL;
 	}
 
-	reg = (lm80_read_value(client, LM80_REG_FANDIV) &
-	       ~(3 << (2 * (nr + 1)))) | (data->fan_div[nr] << (2 * (nr + 1)));
+	ret = lm80_read_value(client, LM80_REG_FANDIV);
+	if (ret < 0)
+		return ret;
+	reg = (ret & ~(3 << (2 * (nr + 1))))
+	      | (data->fan_div[nr] << (2 * (nr + 1)));
 	lm80_write_value(client, LM80_REG_FANDIV, reg);
 
 	/* Restore fan_min */
-- 
2.17.2 (Apple Git-113)

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: lm80: fix a missing check of return value
  2018-12-21  6:13 [PATCH] hwmon: lm80: fix a missing check of return value Kangjie Lu
@ 2018-12-21 14:43 ` Guenter Roeck
  2019-01-02 15:23 ` Dan Carpenter
  1 sibling, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2018-12-21 14:43 UTC (permalink / raw)
  To: Kangjie Lu; +Cc: pakki001, Jean Delvare, linux-hwmon, linux-kernel

On 12/20/18 10:13 PM, Kangjie Lu wrote:
> If lm80_read_value() fails, it returns a negative number instead of the
> correct read data. Therefore, we should avoid using the data if it
> fails.
> 
> The fix checks if lm80_read_value() fails, and if so, returns with the
> error number.
> 
> Signed-off-by: Kangjie Lu <kjlu@umn.edu>
> ---
>   drivers/hwmon/lm80.c | 15 ++++++++++-----
>   1 file changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/hwmon/lm80.c b/drivers/hwmon/lm80.c
> index 08e3945a6fbf..fca6363cd77f 100644
> --- a/drivers/hwmon/lm80.c
> +++ b/drivers/hwmon/lm80.c
> @@ -360,9 +360,11 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
>   	struct i2c_client *client = data->client;
>   	unsigned long min, val;
>   	u8 reg;
> -	int err = kstrtoul(buf, 10, &val);
> -	if (err < 0)
> -		return err;
> +	int ret;
> +

In the other patch you are using 'rv'. Please use consistent variable names.

Also, please use "hwmon: (lm80) <description>" as subject, and use different subjects
for both patches so we can distinguish them without looking into each patch itself.

This applies to the other patch as well.

Thanks,
Guenter

> +	ret = kstrtoul(buf, 10, &val);
> +	if (ret < 0)
> +		return ret;
>   
>   	/* Save fan_min */
>   	mutex_lock(&data->update_lock);
> @@ -390,8 +392,11 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
>   		return -EINVAL;
>   	}
>   
> -	reg = (lm80_read_value(client, LM80_REG_FANDIV) &
> -	       ~(3 << (2 * (nr + 1)))) | (data->fan_div[nr] << (2 * (nr + 1)));
> +	ret = lm80_read_value(client, LM80_REG_FANDIV);
> +	if (ret < 0)
> +		return ret;
> +	reg = (ret & ~(3 << (2 * (nr + 1))))
> +	      | (data->fan_div[nr] << (2 * (nr + 1)));
>   	lm80_write_value(client, LM80_REG_FANDIV, reg);
>   
>   	/* Restore fan_min */
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: lm80: fix a missing check of return value
  2018-12-21  6:13 [PATCH] hwmon: lm80: fix a missing check of return value Kangjie Lu
  2018-12-21 14:43 ` Guenter Roeck
@ 2019-01-02 15:23 ` Dan Carpenter
  2019-01-02 16:17   ` Guenter Roeck
  1 sibling, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2019-01-02 15:23 UTC (permalink / raw)
  To: kbuild, Kangjie Lu
  Cc: kbuild-all, kjlu, pakki001, Jean Delvare, Guenter Roeck,
	linux-hwmon, linux-kernel

Hi Kangjie,

Thank you for the patch! Perhaps something to improve:

url:    https://github.com/0day-ci/linux/commits/Kangjie-Lu/hwmon-lm80-fix-a-missing-check-of-return-value/20181222-023000
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next

smatch warnings:
drivers/hwmon/lm80.c:408 set_fan_div() warn: inconsistent returns 'mutex:&data->update_lock'.
  Locked on:   line 397
  Unlocked on: line 367

# https://github.com/0day-ci/linux/commit/7612ba0bef1defb6de3290accca07633ccfd3365
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 7612ba0bef1defb6de3290accca07633ccfd3365
vim +408 drivers/hwmon/lm80.c

^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  348  
1160631b1 drivers/hwmon/lm80.c     Guenter Roeck      2012-01-19  349  /*
1160631b1 drivers/hwmon/lm80.c     Guenter Roeck      2012-01-19  350   * Note: we save and restore the fan minimum here, because its value is
1160631b1 drivers/hwmon/lm80.c     Guenter Roeck      2012-01-19  351   * determined in part by the fan divisor.  This follows the principle of
1160631b1 drivers/hwmon/lm80.c     Guenter Roeck      2012-01-19  352   * least surprise; the user doesn't expect the fan minimum to change just
1160631b1 drivers/hwmon/lm80.c     Guenter Roeck      2012-01-19  353   * because the divisor changed.
1160631b1 drivers/hwmon/lm80.c     Guenter Roeck      2012-01-19  354   */
f8181762a drivers/hwmon/lm80.c     Jean Delvare       2008-01-05  355  static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
f8181762a drivers/hwmon/lm80.c     Jean Delvare       2008-01-05  356  	const char *buf, size_t count)
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  357  {
f8181762a drivers/hwmon/lm80.c     Jean Delvare       2008-01-05  358  	int nr = to_sensor_dev_attr(attr)->index;
118c9a61f drivers/hwmon/lm80.c     Guenter Roeck      2014-04-04  359  	struct lm80_data *data = dev_get_drvdata(dev);
118c9a61f drivers/hwmon/lm80.c     Guenter Roeck      2014-04-04  360  	struct i2c_client *client = data->client;
6a9e7c4c0 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-10  361  	unsigned long min, val;
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  362  	u8 reg;
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  363  	int ret;
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  364  
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  365  	ret = kstrtoul(buf, 10, &val);
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  366  	if (ret < 0)
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  367  		return ret;
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  368  
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  369  	/* Save fan_min */
9a61bf630 drivers/hwmon/lm80.c     Ingo Molnar        2006-01-18  370  	mutex_lock(&data->update_lock);
85b3ee07b drivers/hwmon/lm80.c     Guenter Roeck      2014-04-13  371  	min = FAN_FROM_REG(data->fan[f_min][nr],
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  372  			   DIV_FROM_REG(data->fan_div[nr]));
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  373  
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  374  	switch (val) {
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  375  	case 1:
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  376  		data->fan_div[nr] = 0;
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  377  		break;
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  378  	case 2:
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  379  		data->fan_div[nr] = 1;
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  380  		break;
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  381  	case 4:
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  382  		data->fan_div[nr] = 2;
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  383  		break;
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  384  	case 8:
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  385  		data->fan_div[nr] = 3;
66b3b1f75 drivers/hwmon/lm80.c     Frans Meulenbroeks 2012-01-04  386  		break;
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  387  	default:
118c9a61f drivers/hwmon/lm80.c     Guenter Roeck      2014-04-04  388  		dev_err(dev,
b55f37572 drivers/hwmon/lm80.c     Guenter Roeck      2013-01-10  389  			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
b55f37572 drivers/hwmon/lm80.c     Guenter Roeck      2013-01-10  390  			val);
9a61bf630 drivers/hwmon/lm80.c     Ingo Molnar        2006-01-18  391  		mutex_unlock(&data->update_lock);
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  392  		return -EINVAL;
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  393  	}
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  394  
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  395  	ret = lm80_read_value(client, LM80_REG_FANDIV);
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  396  	if (ret < 0)
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  397  		return ret;
                                                                                ^^^^^^^^^^
Need to drop the lock before returning.

7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  398  	reg = (ret & ~(3 << (2 * (nr + 1))))
7612ba0be drivers/hwmon/lm80.c     Kangjie Lu         2018-12-21  399  	      | (data->fan_div[nr] << (2 * (nr + 1)));
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  400  	lm80_write_value(client, LM80_REG_FANDIV, reg);
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  401  
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  402  	/* Restore fan_min */
85b3ee07b drivers/hwmon/lm80.c     Guenter Roeck      2014-04-13  403  	data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
85b3ee07b drivers/hwmon/lm80.c     Guenter Roeck      2014-04-13  404  	lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1),
85b3ee07b drivers/hwmon/lm80.c     Guenter Roeck      2014-04-13  405  			 data->fan[f_min][nr]);
9a61bf630 drivers/hwmon/lm80.c     Ingo Molnar        2006-01-18  406  	mutex_unlock(&data->update_lock);
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  407  
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16 @408  	return count;
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  409  }
^1da177e4 drivers/i2c/chips/lm80.c Linus Torvalds     2005-04-16  410  

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: lm80: fix a missing check of return value
  2019-01-02 15:23 ` Dan Carpenter
@ 2019-01-02 16:17   ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2019-01-02 16:17 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: kbuild, Kangjie Lu, kbuild-all, pakki001, Jean Delvare,
	linux-hwmon, linux-kernel

On Wed, Jan 02, 2019 at 06:23:59PM +0300, Dan Carpenter wrote:
> Hi Kangjie,
> 
> Thank you for the patch! Perhaps something to improve:
> 
> url:    https://github.com/0day-ci/linux/commits/Kangjie-Lu/hwmon-lm80-fix-a-missing-check-of-return-value/20181222-023000
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
> 
Fix already queued, waiting for end of commit window.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-01-02 16:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-21  6:13 [PATCH] hwmon: lm80: fix a missing check of return value Kangjie Lu
2018-12-21 14:43 ` Guenter Roeck
2019-01-02 15:23 ` Dan Carpenter
2019-01-02 16:17   ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).