Skip to contentSkip to conversation

devres: A couple of cleanups

PATCH Superseded Andy Shevchenko 2 participants 6 messages Feb 27, 2024 – Feb 29, 2024 Library code

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover devres: A couple of cleanups

A couple of ad-hoc cleanups. No functional changes intended.

Andy Shevchenko (2):
devres: Switch to use dev_err_probe() for unification
devres: Don't use "proxy" headers

 lib/devres.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
#1 lore raw

PATCH 1/2 devres: Switch to use dev_err_probe() for unification

The devm_*() APIs are supposed to be called during the ->probe() stage. Many drivers (especially new ones) has switched to use dev_err_probe() for error messaging for the sake of unification. Let's do the same in the devres APIs.

---
 lib/devres.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
lib/devres.c +9 -8
index fe0c63caeb68..27f280a39dca 100644
@@ -125,12 +125,13 @@ __devm_ioremap_resource(struct device *dev, const struct resource *res,
 	resource_size_t size;
 	void __iomem *dest_ptr;
 	char *pretty_name;
+	int ret;
 
 	BUG_ON(!dev);
 
 	if (!res || resource_type(res) != IORESOURCE_MEM) {
-		dev_err(dev, "invalid resource %pR\n", res);
-		return IOMEM_ERR_PTR(-EINVAL);
+		ret = dev_err_probe(dev, -EINVAL, "invalid resource %pR\n", res);
+		return IOMEM_ERR_PTR(ret);
 	}
 
 	if (type == DEVM_IOREMAP && res->flags & IORESOURCE_MEM_NONPOSTED)
@@ -144,20 +145,20 @@ __devm_ioremap_resource(struct device *dev, const struct resource *res,
 	else
 		pretty_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
 	if (!pretty_name) {
-		dev_err(dev, "can't generate pretty name for resource %pR\n", res);
-		return IOMEM_ERR_PTR(-ENOMEM);
+		ret = dev_err_probe(dev, -ENOMEM, "can't generate pretty name for resource %pR\n", res);
+		return IOMEM_ERR_PTR(ret);
 	}
 
 	if (!devm_request_mem_region(dev, res->start, size, pretty_name)) {
-		dev_err(dev, "can't request region for resource %pR\n", res);
-		return IOMEM_ERR_PTR(-EBUSY);
+		ret = dev_err_probe(dev, -EBUSY, "can't request region for resource %pR\n", res);
+		return IOMEM_ERR_PTR(ret);
 	}
 
 	dest_ptr = __devm_ioremap(dev, res->start, size, type);
 	if (!dest_ptr) {
-		dev_err(dev, "ioremap failed for resource %pR\n", res);
 		devm_release_mem_region(dev, res->start, size);
-		dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
+		ret = dev_err_probe(dev, -ENOMEM, "ioremap failed for resource %pR\n", res);
+		return IOMEM_ERR_PTR(ret);
 	}
 
 	return dest_ptr;
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
#2 lore raw

Re: [PATCH v1 1/2] devres: Switch to use dev_err_probe() for unification

Quoted 3 lines · Andy Shevchenko

The devm_*() APIs are supposed to be called during the ->probe() stage.
Many drivers (especially new ones) has switched to use

has -> have

Quoted 3 lines

dev_err_probe()
for error messaging for the sake of unification. Let's do the same in the devres APIs.

No objections on principle. Just one thing about the implementation:

Quoted 17 lines

---
 lib/devres.c | 17 +++++++++--------

 1 file changed, 9 insertions(+), 8 deletions(-)
lib/devres.c +1 -0
index fe0c63caeb68..27f280a39dca 100644
@@ -125,12 +125,13 @@ __devm_ioremap_resource(struct device *dev,
 const struct resource *res,
         resource_size_t size;
         void __iomem *dest_ptr;
         char *pretty_name;
+       int ret;

Quoted 8 lines

        BUG_ON(!dev);

        if (!res || resource_type(res) != IORESOURCE_MEM) { -               dev_err(dev, "invalid resource %pR\n", res); -               return IOMEM_ERR_PTR(-EINVAL);
+               ret = dev_err_probe(dev, -EINVAL, "invalid resource %pR\n", res);
+               return IOMEM_ERR_PTR(ret);

So as I see it -EINVAL is just piped through dev_err_probe() and is never changed.
Don't you think it would be better to drop variable 'ret' and just do return IOMEM_ERR_PTR(-EINVAL);
as before?

That way it would be obvious that the error code is never changed and it will always return -EINVAL. Otherwise you have to look up the function definition of dev_err_probe().

The same would apply below.

Regards,
P.

Quoted 40 lines

        }

        if (type == DEVM_IOREMAP && res->flags &
IORESOURCE_MEM_NONPOSTED)

lib/devres.c +0 -0
@@ -144,20 +145,20 @@ __devm_ioremap_resource(struct device *dev,
 const struct resource *res,

        else
                pretty_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
        if (!pretty_name) {
-               dev_err(dev, "can't generate pretty name for resource %pR\n", res);
-               return IOMEM_ERR_PTR(-ENOMEM);
+               ret = dev_err_probe(dev, -ENOMEM, "can't generate pretty name for resource %pR\n", res);
+               return IOMEM_ERR_PTR(ret);
        }

        if (!devm_request_mem_region(dev, res->start, size, pretty_name)) {
-               dev_err(dev, "can't request region for resource %pR\n", res);
-               return IOMEM_ERR_PTR(-EBUSY);
+               ret = dev_err_probe(dev, -EBUSY, "can't request region for resource %pR\n", res);
+               return IOMEM_ERR_PTR(ret);
        }

        dest_ptr = __devm_ioremap(dev, res->start, size, type); if (!dest_ptr) {
-               dev_err(dev, "ioremap failed for resource %pR\n", res);
                devm_release_mem_region(dev, res->start, size); -               dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
+               ret = dev_err_probe(dev, -ENOMEM, "ioremap failed for resource %pR\n", res);
+               return IOMEM_ERR_PTR(ret);
        }

        return dest_ptr;

#3 lore raw

Re: [PATCH v1 1/2] devres: Switch to use dev_err_probe() for unification

Quoted 6 lines · Philipp Stanner
Quoted 3 lines · Andy Shevchenko

The devm_*() APIs are supposed to be called during the ->probe() stage.
Many drivers (especially new ones) has switched to use

has -> have

Thanks, will fix.

Quoted 5 lines
Quoted 3 lines

dev_err_probe()
for error messaging for the sake of unification. Let's do the same in the devres APIs.

No objections on principle. Just one thing about the implementation:

...

Quoted 9 lines
Quoted 3 lines

+               ret = dev_err_probe(dev, -EINVAL, "invalid resource %pR\n", res);
+               return IOMEM_ERR_PTR(ret);

So as I see it -EINVAL is just piped through dev_err_probe() and is never changed.
Don't you think it would be better to drop variable 'ret' and just do return IOMEM_ERR_PTR(-EINVAL);
as before?

dev_err_probe() requires error code as a parameter. Are you suggesting to have a duplication?

	dev_err_probe(-EINVAL);
	return -EINVAL;

I don't think it's a good suggestion, so the answer is "No, I don't think it would be better."

Quoted 3 lines

That way it would be obvious that the error code is never changed and it will always return -EINVAL. Otherwise you have to look up the function definition of dev_err_probe().

...

Quoted 1 line

The same would apply below.

Same answer.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v1 1/2] devres: Switch to use dev_err_probe() for unification

Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Philipp Stanner

On Tue, 2024-02-27 at 19:58 +0200, Andy Shevchenko wrote:

...

Quoted 3 lines
Quoted 3 lines

That way it would be obvious that the error code is never changed and it will always return -EINVAL. Otherwise you have to look up the function definition of dev_err_probe().

And to this, it's a common pattern in some cases, esp. when you have a chain of the similar calls and you want to simplify the code (see CCI accessors in Video4Linux2 as an example). So, I also don't think it's something unusual.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

PATCH 2/2 devres: Don't use "proxy" headers

In reply to Andy Shevchenko

Update header inclusions to follow IWYU (Include What You Use) principle.

---
 lib/devres.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
lib/devres.c +6 -3
index 27f280a39dca..4fc152de6d8b 100644
@@ -1,10 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <linux/bug.h>
 #include <linux/device.h>
-#include <linux/err.h>
-#include <linux/io.h>
-#include <linux/gfp.h>
+#include <linux/errno.h>
 #include <linux/export.h>
+#include <linux/gfp_types.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
 #include <linux/of_address.h>
+#include <linux/types.h>
 
 enum devm_ioremap_type {
 	DEVM_IOREMAP = 0,
Signature · 1 line
2.43.0.rc1.1.gbec44491f096