Message ID | 20181220102247.4911-2-a.hajda@samsung.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | driver core: add probe error check helper | expand |
On Thu, Dec 20, 2018 at 11:23 AM Andrzej Hajda <a.hajda@samsung.com> wrote: > > During probe every time driver gets resource it should usually check for error > printk some message if it is not -EPROBE_DEFER and return the error. This > pattern is simple but requires adding few lines after any resource acquisition > code, as a result it is often omited or implemented only partially. > probe_err helps to replace such code sequences with simple call, so code: > if (err != -EPROBE_DEFER) > dev_err(dev, ...); > return err; > becomes: > return probe_err(dev, err, ...); > > Signed-off-by: Andrzej Hajda <a.hajda@samsung.com> > Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> > Reviewed-by: Mark Brown <broonie@kernel.org> > Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> I'm not entirely convinced that the dev_err() log level is adequate for this purpose, but anyway it looks like this may reduce code duplication quite a bit, so Reviewed-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > --- > v3: > - added 'extern __printf(3, 4)' decorators to probe_err, > - changed error logging format - newline at the end, > - added empty lines in probe_err around dev_err, > - added R-b by Mark and Andy. > v2: > - added error value to log message, > - fixed code style, > - added EXPORT_SYMBOL_GPL, > - Added R-B by Javier (I hope the changes did not invalidate it). > --- > drivers/base/core.c | 39 +++++++++++++++++++++++++++++++++++++++ > include/linux/device.h | 3 +++ > 2 files changed, 42 insertions(+) > > diff --git a/drivers/base/core.c b/drivers/base/core.c > index 0073b09bb99f..7f644f3c41d3 100644 > --- a/drivers/base/core.c > +++ b/drivers/base/core.c > @@ -3099,6 +3099,45 @@ define_dev_printk_level(_dev_info, KERN_INFO); > > #endif > > +/** > + * probe_err - probe error check and log helper > + * @dev: the pointer to the struct device > + * @err: error value to test > + * @fmt: printf-style format string > + * @...: arguments as specified in the format string > + * > + * This helper implements common pattern present in probe functions for error > + * checking: print message if the error is not -EPROBE_DEFER and propagate it. > + * It replaces code sequence: > + * if (err != -EPROBE_DEFER) > + * dev_err(dev, ...); > + * return err; > + * with > + * return probe_err(dev, err, ...); > + * > + * Returns @err. > + * > + */ > +int probe_err(const struct device *dev, int err, const char *fmt, ...) > +{ > + struct va_format vaf; > + va_list args; > + > + if (err == -EPROBE_DEFER) > + return err; > + > + va_start(args, fmt); > + vaf.fmt = fmt; > + vaf.va = &args; > + > + dev_err(dev, "error %d: %pV", err, &vaf); > + > + va_end(args); > + > + return err; > +} > +EXPORT_SYMBOL_GPL(probe_err); > + > static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) > { > return fwnode && !IS_ERR(fwnode->secondary); > diff --git a/include/linux/device.h b/include/linux/device.h > index 6cb4640b6160..2d3a1cc6f5da 100644 > --- a/include/linux/device.h > +++ b/include/linux/device.h > @@ -1591,6 +1591,9 @@ do { \ > WARN_ONCE(condition, "%s %s: " format, \ > dev_driver_string(dev), dev_name(dev), ## arg) > > +extern __printf(3, 4) > +int probe_err(const struct device *dev, int err, const char *fmt, ...); > + > /* Create alias, so I can be autoloaded. */ > #define MODULE_ALIAS_CHARDEV(major,minor) \ > MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) > -- > 2.17.1 >
On Thu, Dec 20, 2018 at 11:22:45AM +0100, Andrzej Hajda wrote: > During probe every time driver gets resource it should usually check for error > printk some message if it is not -EPROBE_DEFER and return the error. This > pattern is simple but requires adding few lines after any resource acquisition > code, as a result it is often omited or implemented only partially. > probe_err helps to replace such code sequences with simple call, so code: > if (err != -EPROBE_DEFER) > dev_err(dev, ...); > return err; > becomes: > return probe_err(dev, err, ...); Can you show a driver being converted to use this to show if it really will save a bunch of lines and make things simpler? Usually you are requesting lots of resources so you need to do more than just return, you need to clean stuff up first. thanks, greg k-h
On 20.12.2018 12:14, Greg Kroah-Hartman wrote: > On Thu, Dec 20, 2018 at 11:22:45AM +0100, Andrzej Hajda wrote: >> During probe every time driver gets resource it should usually check for error >> printk some message if it is not -EPROBE_DEFER and return the error. This >> pattern is simple but requires adding few lines after any resource acquisition >> code, as a result it is often omited or implemented only partially. >> probe_err helps to replace such code sequences with simple call, so code: >> if (err != -EPROBE_DEFER) >> dev_err(dev, ...); >> return err; >> becomes: >> return probe_err(dev, err, ...); > Can you show a driver being converted to use this to show if it really > will save a bunch of lines and make things simpler? Usually you are > requesting lots of resources so you need to do more than just return, > you need to clean stuff up first. I have posted sample conversion patch (generated by cocci) in previous version of this patchset [1]. I did not re-posted it again as it is quite big patch and it will not be applied without prior splitting it per subsystem. Regarding stuff cleaning: devm_* usually makes it unnecessary, but also even with necessary cleaning you can profit from probe_err, you just calls it without leaving probe - you have still handled correctly probe deferring. Here is sample usage (taken from beginning of the mentioned patch): --- diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c index 4b900fc659f7..52e891fe1586 100644 --- a/drivers/ata/libahci_platform.c +++ b/drivers/ata/libahci_platform.c @@ -581,11 +581,8 @@ int ahci_platform_init_host(struct platform_device *pdev, int i, irq, n_ports, rc; irq = platform_get_irq(pdev, 0); - if (irq <= 0) { - if (irq != -EPROBE_DEFER) - dev_err(dev, "no irq\n"); - return irq; - } + if (irq <= 0) + return probe_err(dev, irq, "no irq\n"); --- And there is plenty of such places, with new version of cocci script I can locate about 2700 such cases, and it is still far from completeness. [1]: https://lore.kernel.org/lkml/20181016072244.1216-4-a.hajda@samsung.com/T/#u Regards Andrzej
On Thu, Dec 20, 2018 at 5:38 AM Andrzej Hajda <a.hajda@samsung.com> wrote: > > On 20.12.2018 12:14, Greg Kroah-Hartman wrote: > > On Thu, Dec 20, 2018 at 11:22:45AM +0100, Andrzej Hajda wrote: > >> During probe every time driver gets resource it should usually check for error > >> printk some message if it is not -EPROBE_DEFER and return the error. This > >> pattern is simple but requires adding few lines after any resource acquisition > >> code, as a result it is often omited or implemented only partially. > >> probe_err helps to replace such code sequences with simple call, so code: > >> if (err != -EPROBE_DEFER) > >> dev_err(dev, ...); > >> return err; > >> becomes: > >> return probe_err(dev, err, ...); > > Can you show a driver being converted to use this to show if it really > > will save a bunch of lines and make things simpler? Usually you are > > requesting lots of resources so you need to do more than just return, > > you need to clean stuff up first. > > > I have posted sample conversion patch (generated by cocci) in previous > version of this patchset [1]. > > I did not re-posted it again as it is quite big patch and it will not be > applied without prior splitting it per subsystem. > > Regarding stuff cleaning: devm_* usually makes it unnecessary, but also > even with necessary cleaning you can profit from probe_err, you just > calls it without leaving probe - you have still handled correctly probe > deferring. > > Here is sample usage (taken from beginning of the mentioned patch): > > --- > diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c > index 4b900fc659f7..52e891fe1586 100644 > --- a/drivers/ata/libahci_platform.c > +++ b/drivers/ata/libahci_platform.c > @@ -581,11 +581,8 @@ int ahci_platform_init_host(struct platform_device *pdev, > int i, irq, n_ports, rc; > > irq = platform_get_irq(pdev, 0); > - if (irq <= 0) { > - if (irq != -EPROBE_DEFER) > - dev_err(dev, "no irq\n"); > - return irq; > - } > + if (irq <= 0) > + return probe_err(dev, irq, "no irq\n"); Shouldn't platform_get_irq (or what it calls) print the error message (like we do for kmalloc), rather than every driver? We could get rid of lots of error strings that way. I guess there are cases where no irq is not an error and we wouldn't want to always print an error. In some cases like that, we have 2 versions of the function. Not what you're addressing here exactly, but what I'd like to see is the ability to print the exact locations generating errors in the first place. That would require wrapping all the error code assignments and returns (or at least the common sources). If we're going to make tree wide changes, then that might be the better place to put the effort. If we had that, then maybe we'd need a lot fewer error messages in drivers. I did a prototype implementation and coccinelle script a while back that I could dust off if there's interest. It was helpful in finding the source of errors, but did have some false positives printed. Rob
On 21.12.2018 23:47, Rob Herring wrote: > On Thu, Dec 20, 2018 at 5:38 AM Andrzej Hajda <a.hajda@samsung.com> wrote: >> On 20.12.2018 12:14, Greg Kroah-Hartman wrote: >>> On Thu, Dec 20, 2018 at 11:22:45AM +0100, Andrzej Hajda wrote: >>>> During probe every time driver gets resource it should usually check for error >>>> printk some message if it is not -EPROBE_DEFER and return the error. This >>>> pattern is simple but requires adding few lines after any resource acquisition >>>> code, as a result it is often omited or implemented only partially. >>>> probe_err helps to replace such code sequences with simple call, so code: >>>> if (err != -EPROBE_DEFER) >>>> dev_err(dev, ...); >>>> return err; >>>> becomes: >>>> return probe_err(dev, err, ...); >>> Can you show a driver being converted to use this to show if it really >>> will save a bunch of lines and make things simpler? Usually you are >>> requesting lots of resources so you need to do more than just return, >>> you need to clean stuff up first. >> >> I have posted sample conversion patch (generated by cocci) in previous >> version of this patchset [1]. >> >> I did not re-posted it again as it is quite big patch and it will not be >> applied without prior splitting it per subsystem. >> >> Regarding stuff cleaning: devm_* usually makes it unnecessary, but also >> even with necessary cleaning you can profit from probe_err, you just >> calls it without leaving probe - you have still handled correctly probe >> deferring. >> >> Here is sample usage (taken from beginning of the mentioned patch): >> >> --- >> diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c >> index 4b900fc659f7..52e891fe1586 100644 >> --- a/drivers/ata/libahci_platform.c >> +++ b/drivers/ata/libahci_platform.c >> @@ -581,11 +581,8 @@ int ahci_platform_init_host(struct platform_device *pdev, >> int i, irq, n_ports, rc; >> >> irq = platform_get_irq(pdev, 0); >> - if (irq <= 0) { >> - if (irq != -EPROBE_DEFER) >> - dev_err(dev, "no irq\n"); >> - return irq; >> - } >> + if (irq <= 0) >> + return probe_err(dev, irq, "no irq\n"); > Shouldn't platform_get_irq (or what it calls) print the error message > (like we do for kmalloc), rather than every driver? We could get rid > of lots of error strings that way. I guess there are cases where no > irq is not an error and we wouldn't want to always print an error. In > some cases like that, we have 2 versions of the function. kmalloc prints error and stack trace because it shows shortage of common resource used by everyone, quite different thing than irq specific for given device. Usually only device driver knows if error in irq acquiring should be reported to user, and how it should be reported. The example is for irq, but the question about the best way of reporting error stands for all other resource acquisitions: gpios, regulators, clocks,.... Alternative ways I see for now: 1. Do it in the consumer, like it is done now - in such case probe_err seems to be a good helper. 2. Do it in the provider's framework, in such case framework should know if the error should be printed: a) by calling special versions of all allocators, b) by adding extra argument to all allocators, c) adding extra flag to struct device (it is passed to most allocators) 3. By creating generic allocator for multiple resources, something similar to what I have proposed few years ago in "resource tracking" framework [1]. For example: ret = devm_resources_get(dev, res_irq_desc(&ctx->irq, 0), res_clk_desc(&ctx->clk, "bus_clk"), res_gpio_desc(&ctx->enable_gpio, "enable", GPIOD_OUT_HIGH), ... ); Error reporting would be performed in this universal allocator. If we want to perform brave changes I would opt for 3 - it is very common to allocate multiple resources in probe, compacting it into one helper should significantly simplify the code. Option 1 is the simplest one - we do not change existing practices - so it is the best in case of conservative approach. I have mixed feelings about 2c, practically it looks quite tempting - we get what we want with minimal effort, but I am not sure if polluting struct device with 'presentation' layer is a good solution. I do not like 2a neither 2b - alternatives between function namespace pollution and argument list pollution. [1]: https://lwn.net/Articles/625454/ > Not what you're addressing here exactly, but what I'd like to see is > the ability to print the exact locations generating errors in the > first place. That would require wrapping all the error code > assignments and returns (or at least the common sources). If we're > going to make tree wide changes, then that might be the better place > to put the effort. If we had that, then maybe we'd need a lot fewer > error messages in drivers. I did a prototype implementation and > coccinelle script a while back that I could dust off if there's > interest. It was helpful in finding the source of errors, but did have > some false positives printed. I guess that in case of resource acquisition it is usually easy to locate place the error was reported, if the error message is informative enough, exact line number/function name seems to me overkill. Regards Andrzej > > Rob > >
diff --git a/drivers/base/core.c b/drivers/base/core.c index 0073b09bb99f..7f644f3c41d3 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3099,6 +3099,45 @@ define_dev_printk_level(_dev_info, KERN_INFO); #endif +/** + * probe_err - probe error check and log helper + * @dev: the pointer to the struct device + * @err: error value to test + * @fmt: printf-style format string + * @...: arguments as specified in the format string + * + * This helper implements common pattern present in probe functions for error + * checking: print message if the error is not -EPROBE_DEFER and propagate it. + * It replaces code sequence: + * if (err != -EPROBE_DEFER) + * dev_err(dev, ...); + * return err; + * with + * return probe_err(dev, err, ...); + * + * Returns @err. + * + */ +int probe_err(const struct device *dev, int err, const char *fmt, ...) +{ + struct va_format vaf; + va_list args; + + if (err == -EPROBE_DEFER) + return err; + + va_start(args, fmt); + vaf.fmt = fmt; + vaf.va = &args; + + dev_err(dev, "error %d: %pV", err, &vaf); + + va_end(args); + + return err; +} +EXPORT_SYMBOL_GPL(probe_err); + static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) { return fwnode && !IS_ERR(fwnode->secondary); diff --git a/include/linux/device.h b/include/linux/device.h index 6cb4640b6160..2d3a1cc6f5da 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1591,6 +1591,9 @@ do { \ WARN_ONCE(condition, "%s %s: " format, \ dev_driver_string(dev), dev_name(dev), ## arg) +extern __printf(3, 4) +int probe_err(const struct device *dev, int err, const char *fmt, ...); + /* Create alias, so I can be autoloaded. */ #define MODULE_ALIAS_CHARDEV(major,minor) \ MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))