Skip to contentSkip to conversation

driver core: Annotate dev_err_probe() with __must_check

PATCH In review Andy Shevchenko 6 participants 11 messages Aug 26, 2020 – Sep 9, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver core: Annotate dev_err_probe() with __must_check

We have got already new users of this API which interpret it differently and miss the opportunity to optimize their code.

In order to avoid similar cases in the future, annotate dev_err_probe() with __must_check.

Fixes a787e5400a1c ("driver core: add device probe log helper")
---
 include/linux/device.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/device.h +1 -1
index ca18da4768e3..f9d2e5703bbf 100644
@@ -978,7 +978,7 @@ void device_links_supplier_sync_state_pause(void);
 void device_links_supplier_sync_state_resume(void);
 
 extern __printf(3, 4)
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
 
 /* Create alias, so I can be autoloaded. */
 #define MODULE_ALIAS_CHARDEV(major,minor) \
Signature · 1 line
2.28.0
#1 lore raw

Hi Andy,

Quoted 5 lines · Andy Shevchenko

We have got already new users of this API which interpret it differently and miss the opportunity to optimize their code.

In order to avoid similar cases in the future, annotate dev_err_probe() with __must_check.

There are many cases where __must_check can be annoying, for example:

ret = ...;

if (ret < 0) {

    dev_err_probe(...);

    goto cleanup;

}

Or (less frequently):

ptr = ...;

if (IS_ERR(ptr)) {

    dev_err_probe(...);

    return ptr;

}

Of course in both cases one can add workarounds, but I am not sure what is better.

Regards

Andrzej

#2 lore raw
Quoted 10 lines · Andrzej Hajda
Quoted 5 lines · Andy Shevchenko

We have got already new users of this API which interpret it differently and miss the opportunity to optimize their code.

In order to avoid similar cases in the future, annotate dev_err_probe() with __must_check.

There are many cases where __must_check can be annoying, for example:

Quoted 7 lines

ret = ...;

if (ret < 0) {

    dev_err_probe(...);

    goto cleanup;

Can be

	ret = dev_err_probe(...);
Quoted 5 lines

}

Or (less frequently):

Quoted 7 lines

ptr = ...;

if (IS_ERR(ptr)) {

    dev_err_probe(...);

    return ptr;

...which basically should be something like

	return dev_err_probe_ptr(...);
Quoted 5 lines

}

Of course in both cases one can add workarounds, but I am not sure what is better.

Me neither, but definitely API in current state allows to make code suboptimal. So, up to Greg and Rafael to decide.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Andy Shevchenko
Quoted 17 lines · Andy Shevchenko

We have got already new users of this API which interpret it differently and miss the opportunity to optimize their code.

In order to avoid similar cases in the future, annotate dev_err_probe() with __must_check.

Fixes a787e5400a1c ("driver core: add device probe log helper")
---
 include/linux/device.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/device.h +0 -0
index ca18da4768e3..f9d2e5703bbf 100644

include/linux/device.h void device_links_supplier_sync_state_pause(void); · line 981

Quoted 5 lines
include/linux/device.h +1 -1
@@ -978,7 +978,7 @@ void device_links_supplier_sync_state_pause(void);
 void device_links_supplier_sync_state_resume(void);
 
 extern __printf(3, 4)
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

Generally, the __must_check should go before the return type and the extern isn't necessary and is also generally not used in device.h, so I'd prefer:

__printf(3, 4)
__must_check int dev_err_probe(...);

#4 lore raw
Quoted 1 line · Joe Perches

On Wed, 2020-08-26 at 13:44 +0300, Andy Shevchenko wrote:

...

Quoted 9 lines

include/linux/device.h void device_links_supplier_sync_state_pause(void); · line 981

Quoted 2 lines
include/linux/device.h +1 -1
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

Generally, the __must_check should go before the return type and the extern isn't necessary and is also generally not used in device.h, so I'd prefer:

__printf(3, 4)
__must_check int dev_err_probe(...);

I grepped the current code... I don't see support of your preference. Maybe I missed something? (I'm talking about include/*)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 1 line · Joe Perches

On Wed, 2020-08-26 at 13:44 +0300, Andy Shevchenko wrote:

...

Quoted 7 lines

include/linux/device.h void device_links_supplier_sync_state_pause(void); · line 981

Quoted 2 lines
include/linux/device.h +1 -1
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

Generally, the __must_check should go before the return type and the extern isn't necessary and is also generally not used in device.h, so I'd prefer:

Quoted 5 lines
Quoted 2 lines

__printf(3, 4)
__must_check int dev_err_probe(...);

I grepped the current code... I don't see support of your preference. Maybe I missed something? (I'm talking about include/*)

Hey Andy.

The __must_check location is just personal belief
as it makes grep significantly easier

<qualifiers> type function(args...)

is a much easier grep pattern than

<qualifiers> type <qualifiers> function(args...)

extern function declarations are generally unnecessary and should be avoided.

cheers, Joe

#6 lore raw
Quoted 9 lines · Joe Perches
Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Joe Perches

On Wed, 2020-08-26 at 13:44 +0300, Andy Shevchenko wrote:

...

Quoted 2 lines

include/linux/device.h void device_links_supplier_sync_state_pause(void); · line 981

Quoted 2 lines
include/linux/device.h +1 -1
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

+Cc Stephen and Greg,

Hi Andy,

Did this patch ended up in next somehow? I am surprised because now I got warnings for perfectly fine code:
https://lore.kernel.org/linux-next/[email protected]/T/#u

This creates simply false warnings instead of hints for "optimization".

Best regards,
Krzysztof

#7 lore raw
Quoted 13 lines · Krzysztof Kozlowski
Quoted 9 lines · Joe Perches
Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Joe Perches

On Wed, 2020-08-26 at 13:44 +0300, Andy Shevchenko wrote:

...

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

+Cc Stephen and Greg,

Quoted 7 lines

Hi Andy,

Did this patch ended up in next somehow? I am surprised because now I got warnings for perfectly fine code:
https://lore.kernel.org/linux-next/[email protected]/T/#u

This creates simply false warnings instead of hints for "optimization".

Yes, it got merged into m y driver core tree.

I'll fix up the tty build warning, should be easy enough, the patch is below.

thanks,

greg k-h
------------------

drivers/tty/serial/8250/8250_bcm2835aux.c +1 -1
index fd95860cd661..cd1880715bad 100644
@@ -151,7 +151,7 @@ static int bcm2835aux_serial_probe(struct platform_device *pdev)
 	/* register the port */
 	ret = serial8250_register_8250_port(&up);
 	if (ret < 0) {
-		dev_err_probe(&pdev->dev, ret, "unable to register 8250 port\n");
+		ret = dev_err_probe(&pdev->dev, ret, "unable to register 8250 port\n");
 		goto dis_clk;
 	}
 	data->line = ret;
#8 lore raw
Quoted 21 lines · Greg Kroah-Hartman
Quoted 19 lines · Krzysztof Kozlowski
Quoted 9 lines · Joe Perches
Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Joe Perches

On Wed, 2020-08-26 at 13:44 +0300, Andy Shevchenko wrote:

...

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

+Cc Stephen and Greg,

Hi Andy,

Did this patch ended up in next somehow? I am surprised because now I got warnings for perfectly fine code:
https://lore.kernel.org/linux-next/[email protected]/T/#u

Quoted 6 lines
Quoted 1 line

This creates simply false warnings instead of hints for "optimization".

Yes, it got merged into m y driver core tree.

I'll fix up the tty build warning, should be easy enough, the patch is below.

Yes, this fix suppresses the warning but the question is whether we really want the warning?
Such fixes mean additional code which the compiler might not optimize (unless it inlines the dev_err_probe()). This additional code is purely for suppressing the warning, without any meaning on its own. Actually it might be even confusing for someone to see:
if (ret)
ret = dev_err_probe(ret);

warn_unused_result should point errors, not "optimization opportunities". If you want to have opportunity, add a coccinelle rule. Or a checkpatch rule. Not a compiler warning.

Best regards,
Krzysztof

#9 lore raw
Quoted 30 lines · Krzysztof Kozlowski
Quoted 21 lines · Greg Kroah-Hartman
Quoted 19 lines · Krzysztof Kozlowski
Quoted 9 lines · Joe Perches
Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Joe Perches

On Wed, 2020-08-26 at 13:44 +0300, Andy Shevchenko wrote:

...

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

+Cc Stephen and Greg,

Hi Andy,

Did this patch ended up in next somehow? I am surprised because now I got warnings for perfectly fine code:
https://lore.kernel.org/linux-next/[email protected]/T/#u

Quoted 6 lines
Quoted 1 line

This creates simply false warnings instead of hints for "optimization".

Yes, it got merged into m y driver core tree.

I'll fix up the tty build warning, should be easy enough, the patch is below.

Quoted 8 lines

Yes, this fix suppresses the warning but the question is whether we really want the warning?
Such fixes mean additional code which the compiler might not optimize (unless it inlines the dev_err_probe()). This additional code is purely for suppressing the warning, without any meaning on its own. Actually it might be even confusing for someone to see:
if (ret)
ret = dev_err_probe(ret);

Yeah, that is dumb, as the patch I made shows :(

Quoted 3 lines

warn_unused_result should point errors, not "optimization opportunities". If you want to have opportunity, add a coccinelle rule. Or a checkpatch rule. Not a compiler warning.

Ok, I now agree, I'll go revert this patch and trust that driver authors will "do the right thing" here...

thanks,

greg k-h

#10 lore raw
Quoted 29 lines · Greg Kroah-Hartman
Quoted 28 lines · Krzysztof Kozlowski
Quoted 25 lines · Greg Kroah-Hartman
Quoted 12 lines · Krzysztof Kozlowski
Quoted 8 lines · Joe Perches
Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Joe Perches

On Wed, 2020-08-26 at 13:44 +0300, Andy Shevchenko wrote:

...

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
+int __must_check dev_err_probe(const struct device *dev, int err, const char *fmt, ...);

+Cc Stephen and Greg,

Quoted 7 lines

Hi Andy,

Did this patch ended up in next somehow? I am surprised because now I got warnings for perfectly fine code:
https://lore.kernel.org/linux-next/[email protected]/T/#u

This creates simply false warnings instead of hints for "optimization".

Yes, it got merged into m y driver core tree.

I'll fix up the tty build warning, should be easy enough, the patch is below.

Quoted 8 lines
Quoted 8 lines

Yes, this fix suppresses the warning but the question is whether we really want the warning?
Such fixes mean additional code which the compiler might not optimize (unless it inlines the dev_err_probe()). This additional code is purely for suppressing the warning, without any meaning on its own. Actually it might be even confusing for someone to see:
if (ret)
ret = dev_err_probe(ret);

The problem here is that the dev_err_probe() returns int on purpose. In your patch what I can see it seems another issue is that the driver is semi converted to devm API and thus uses goto:s here and there.

Quoted 1 line

Yeah, that is dumb, as the patch I made shows :(

I agree.

Quoted 6 lines
Quoted 3 lines

warn_unused_result should point errors, not "optimization opportunities". If you want to have opportunity, add a coccinelle rule. Or a checkpatch rule. Not a compiler warning.

Ok, I now agree, I'll go revert this patch and trust that driver authors will "do the right thing" here...

I'm fine (as I stated during a review of that patch) to go either way, but I see it would be nice to have drivers be better thought about using devm APIs.

Signature · 2 lines
With Best Regards,
Andy Shevchenko