hwmon: (isl28022) Don't check for specific errors when parsing properties
Subsystems (2)
PATCH 1/1 hwmon: (isl28022) Don't check for specific errors when parsing properties
Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.
--- drivers/hwmon/isl28022.c | 42 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-)
drivers/hwmon/isl28022.c +24 -18
@@ -337,21 +337,28 @@ DEFINE_SHOW_ATTRIBUTE(shunt_voltage);*/ static int isl28022_read_properties (struct device *dev,struct isl28022_data *data) { +const char *propname;u32 val;int err; - err =device_property_read_u32 (dev,"shunt-resistor-micro-ohms" , &val); -if (err == -EINVAL ) + propname ="shunt-resistor-micro-ohms" ; +if (device_property_present (dev, propname)) { + err =device_property_read_u32 (dev, propname, &val); +if (err) +return err; + }else { val =10000 ; -else if (err <0 ) -return err; + } data->shunt = val; - err =device_property_read_u32 (dev,"renesas,shunt-range-microvolt" , &val); -if (err == -EINVAL ) + propname ="renesas,shunt-range-microvolt" ; +if (device_property_present (dev, propname)) { + err =device_property_read_u32 (dev, propname, &val); +if (err) +return err; + }else { val =320000 ; -else if (err <0 ) -return err; + }switch (val) {case 40000 :
@@ -375,20 +382,19 @@ static int isl28022_read_properties(struct device *dev, struct isl28022_data *da goto shunt_invalid;break ;default : -return dev_err_probe (dev, -EINVAL , -"renesas,shunt-range-microvolt invalid value %d\n" , - val); +return dev_err_probe (dev, -EINVAL ,"%s invalid value %u\n" , propname, val); } - err =device_property_read_u32 (dev,"renesas,average-samples" , &val); -if (err == -EINVAL ) + propname ="renesas,average-samples" ; +if (device_property_present (dev, propname)) { + err =device_property_read_u32 (dev, propname, &val); +if (err) +return err; + }else { val =1 ; -else if (err <0 ) -return err; + }if (val >128 ||hweight32 (val) !=1 ) -return dev_err_probe (dev, -EINVAL , -"renesas,average-samples invalid value %d\n" , - val); +return dev_err_probe (dev, -EINVAL ,"%s invalid value %u\n" , propname, val); data->average = val;
Signature · 1 line
2.50.1
Hi Andy,
CC devicetree
Quoted 5 lines · Andy Shevchenko
Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.
Signed-off-by Andy Shevchenko <[email protected]>
Thanks for your patch!
Quoted 13 lines
drivers/hwmon/isl28022.c +2
-2@@ -337,21 +337,28 @@ DEFINE_SHOW_ATTRIBUTE(shunt_voltage);*/ static int isl28022_read_properties (struct device *dev,struct isl28022_data *data) { +const char *propname;u32 val;int err; - err =device_property_read_u32 (dev,"shunt-resistor-micro-ohms" , &val); -if (err == -EINVAL ) + propname ="shunt-resistor-micro-ohms" ;
drivers/hwmon/isl28022.c DEFINE_SHOW_ATTRIBUTE(shunt_voltage); · line 352
Quoted 10 lines
drivers/hwmon/isl28022.c +6
-2+if (device_property_present (dev, propname)) { + err =device_property_read_u32 (dev, propname, &val); +if (err) +return err; + }else { val =10000 ; - else if (err < 0) - return err; + } data->shunt = val;
IIRC, we have removed superfluous presence checks all over the tree during the past few years? E.g. of_property_read_*() is documented to return -EINVAL if a property does not exist.
So this patch looks like a step back to me...
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 9 lines · Geert Uytterhoeven
Quoted 3 lines · Andy Shevchenko
Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.
IIRC, we have removed superfluous presence checks all over the tree during the past few years? E.g. of_property_read_*() is documented to return
-EINVALif a property does not exist.
Even though, it's still fragile. When we have a check for explicit device presence, we wouldn't care of the error code we get in case of unsuccessful parsing.
Quoted 1 line
So this patch looks like a step back to me...
Obviously I have a disagreement here, this is step forward to weaken the dependency on the certain error code in the cases when we can avoid that. Motivation is mentioned in the commit message.
Also note, -EINVAL can sneak in tons of mysterious ways as it's one of the most overloaded error code in the kernel, its semantic is basically equals to "an error happened".
Having the code above, we make it robust against some subtle nuances which may not be discovered in time.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Guenter Roeck
maintainerQuoted 21 lines · Andy Shevchenko
Quoted 9 lines · Geert Uytterhoeven
Quoted 3 lines · Andy Shevchenko
Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.
IIRC, we have removed superfluous presence checks all over the tree during the past few years? E.g. of_property_read_*() is documented to return
-EINVALif a property does not exist.Even though, it's still fragile. When we have a check for explicit device presence, we wouldn't care of the error code we get in case of unsuccessful parsing.
Quoted 1 line
So this patch looks like a step back to me...
Obviously I have a disagreement here, this is step forward to weaken the dependency on the certain error code in the cases when we can avoid that. Motivation is mentioned in the commit message.
Quoted 7 lines
Also note,
-EINVALcan sneak in tons of mysterious ways as it's one of the most overloaded error code in the kernel, its semantic is basically equals to "an error happened".Having the code above, we make it robust against some subtle nuances which may not be discovered in time.
Is that documented somewhere ? I have been asking people to use the current approach to reduce code size. device_property_present() isn't even mentioned as API in Documentation/. If "do not rely on error codes from device property API functions to determine if a default should be applied" or similar is a new rule or guidance, it should be clearly documented somewhere.
Thanks,
Guenter
Quoted 30 lines · Guenter Roeck
Quoted 21 lines · Andy Shevchenko
Quoted 9 lines · Geert Uytterhoeven
Quoted 3 lines · Andy Shevchenko
Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.
IIRC, we have removed superfluous presence checks all over the tree during the past few years? E.g. of_property_read_*() is documented to return
-EINVALif a property does not exist.Even though, it's still fragile. When we have a check for explicit device presence, we wouldn't care of the error code we get in case of unsuccessful parsing.
Quoted 1 line
So this patch looks like a step back to me...
Obviously I have a disagreement here, this is step forward to weaken the dependency on the certain error code in the cases when we can avoid that. Motivation is mentioned in the commit message.
Quoted 7 lines
Also note,
-EINVALcan sneak in tons of mysterious ways as it's one of the most overloaded error code in the kernel, its semantic is basically equals to "an error happened".Having the code above, we make it robust against some subtle nuances which may not be discovered in time.
Quoted 5 lines
Is that documented somewhere ? I have been asking people to use the current approach to reduce code size.
device_property_present()isn't even mentioned as API inDocumentation/. If "do not rely on error codes from device property API functions to determine if a default should be applied" or similar is a new rule or guidance, it should be clearly documented somewhere.
Fair enough.
Currently if one reads kernel-doc for fwnode_property_*() / device_property_*() APIs, there is no clear specification that -EINVAL is equal to "property not present". It's documented as
* %-EINVAL if given arguments are not valid,
which may be the case, if the parameter itself is wrong, for example fwnode is invalid to begin with. Checking against -EINVAL for 'property not present' is layering violation and relying on deep implementation detail.
I will craft a change to put something like this to the fwnode_property_present() and device_property_present() kernel-docs.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · Geert Uytterhoeven
Quoted 3 lines · Andy Shevchenko
Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.
IIRC, we have removed superfluous presence checks all over the tree during the past few years? E.g. of_property_read_*() is documented to return
-EINVALif a property does not exist.
Implementation detail and actually not accurate.
Quoted 1 line
So this patch looks like a step back to me...
Not to me, just sent [email protected] to clarify in the documentation what's this about.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.
Signed-off-by Andy Shevchenko <[email protected]>
Applied.
Thanks,
Guenter