diff mbox

hwmon: Deal with errors from the thermal subsystem

Message ID 20171204222029.11956-1-linus.walleij@linaro.org (mailing list archive)
State Superseded
Headers show

Commit Message

Linus Walleij Dec. 4, 2017, 10:20 p.m. UTC
If the thermal subsystem returne -EPROBE_DEFER or any other error
when hwmon calls devm_thermal_zone_of_sensor_register(), this is
silently ignored.

I ran into this with an incorrectly defined thermal zone, making
it non-existing and thus this call failed with -EPROBE_DEFER
assuming it would appear later. The sensor was still added
which is incorrect: sensors must strictly be added after the
thermal zones, so deferred probe must be respected.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/hwmon/hwmon.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

Comments

Guenter Roeck Dec. 4, 2017, 11:52 p.m. UTC | #1
On Mon, Dec 04, 2017 at 11:20:29PM +0100, Linus Walleij wrote:
> If the thermal subsystem returne -EPROBE_DEFER or any other error
> when hwmon calls devm_thermal_zone_of_sensor_register(), this is
> silently ignored.
> 
> I ran into this with an incorrectly defined thermal zone, making
> it non-existing and thus this call failed with -EPROBE_DEFER
> assuming it would appear later. The sensor was still added
> which is incorrect: sensors must strictly be added after the
> thermal zones, so deferred probe must be respected.
> 
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  drivers/hwmon/hwmon.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index c9790e2c3440..6d1cb6b47490 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c
> @@ -143,6 +143,7 @@ static int hwmon_thermal_add_sensor(struct device *dev,
>  				    struct hwmon_device *hwdev, int index)
>  {
>  	struct hwmon_thermal_data *tdata;
> +	struct thermal_zone_device *tzd;
>  
>  	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
>  	if (!tdata)
> @@ -151,8 +152,10 @@ static int hwmon_thermal_add_sensor(struct device *dev,
>  	tdata->hwdev = hwdev;
>  	tdata->index = index;
>  
> -	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> -					     &hwmon_thermal_ops);
> +	tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
> +						   &hwmon_thermal_ops);
> +	if (IS_ERR(tzd))
> +		return PTR_ERR(tzd);

This won't work because devm_thermal_zone_of_sensor_register() returns
-ENODEV if CONFIG_THERMAL_OF is disabled. We'll have to check for
-EPROBE_DEFER and only return this specific error, but ignore all others.

Thanks,
Guenter

>  
>  	return 0;
>  }
> @@ -621,14 +624,20 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
>  				if (!chip->ops->is_visible(drvdata, hwmon_temp,
>  							   hwmon_temp_input, j))
>  					continue;
> -				if (info[i]->config[j] & HWMON_T_INPUT)
> -					hwmon_thermal_add_sensor(dev, hwdev, j);
> +				if (info[i]->config[j] & HWMON_T_INPUT) {
> +					err = hwmon_thermal_add_sensor(dev,
> +								hwdev, j);
> +					if (err)
> +						goto free_device;
> +				}
>  			}
>  		}
>  	}
>  
>  	return hdev;
>  
> +free_device:
> +	device_unregister(hdev);
>  free_hwmon:
>  	kfree(hwdev);
>  ida_remove:
> -- 
> 2.14.3
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Linus Walleij Dec. 5, 2017, 8:31 a.m. UTC | #2
On Tue, Dec 5, 2017 at 12:52 AM, Guenter Roeck <linux@roeck-us.net> wrote:

>> -     devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
>> -                                          &hwmon_thermal_ops);
>> +     tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
>> +                                                &hwmon_thermal_ops);
>> +     if (IS_ERR(tzd))
>> +             return PTR_ERR(tzd);
>
> This won't work because devm_thermal_zone_of_sensor_register() returns
> -ENODEV if CONFIG_THERMAL_OF is disabled. We'll have to check for
> -EPROBE_DEFER and only return this specific error, but ignore all others.

Sounds more like we should ignore -ENODEV and respect all other
errors? It will be more robust if some code changes and some
new error code appears again.

I will cook a patch like so.

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Guenter Roeck Dec. 5, 2017, 11:04 a.m. UTC | #3
On 12/05/2017 12:31 AM, Linus Walleij wrote:
> On Tue, Dec 5, 2017 at 12:52 AM, Guenter Roeck <linux@roeck-us.net> wrote:
> 
>>> -     devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
>>> -                                          &hwmon_thermal_ops);
>>> +     tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
>>> +                                                &hwmon_thermal_ops);
>>> +     if (IS_ERR(tzd))
>>> +             return PTR_ERR(tzd);
>>
>> This won't work because devm_thermal_zone_of_sensor_register() returns
>> -ENODEV if CONFIG_THERMAL_OF is disabled. We'll have to check for
>> -EPROBE_DEFER and only return this specific error, but ignore all others.
> 
> Sounds more like we should ignore -ENODEV and respect all other
> errors? It will be more robust if some code changes and some
> new error code appears again.
> 

Agreed, makes sense.

Guenter

> I will cook a patch like so.
> 
> Yours,
> Linus Walleij
> --
> To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index c9790e2c3440..6d1cb6b47490 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -143,6 +143,7 @@  static int hwmon_thermal_add_sensor(struct device *dev,
 				    struct hwmon_device *hwdev, int index)
 {
 	struct hwmon_thermal_data *tdata;
+	struct thermal_zone_device *tzd;
 
 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
 	if (!tdata)
@@ -151,8 +152,10 @@  static int hwmon_thermal_add_sensor(struct device *dev,
 	tdata->hwdev = hwdev;
 	tdata->index = index;
 
-	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
-					     &hwmon_thermal_ops);
+	tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
+						   &hwmon_thermal_ops);
+	if (IS_ERR(tzd))
+		return PTR_ERR(tzd);
 
 	return 0;
 }
@@ -621,14 +624,20 @@  __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 				if (!chip->ops->is_visible(drvdata, hwmon_temp,
 							   hwmon_temp_input, j))
 					continue;
-				if (info[i]->config[j] & HWMON_T_INPUT)
-					hwmon_thermal_add_sensor(dev, hwdev, j);
+				if (info[i]->config[j] & HWMON_T_INPUT) {
+					err = hwmon_thermal_add_sensor(dev,
+								hwdev, j);
+					if (err)
+						goto free_device;
+				}
 			}
 		}
 	}
 
 	return hdev;
 
+free_device:
+	device_unregister(hdev);
 free_hwmon:
 	kfree(hwdev);
 ida_remove: