Skip to contentSkip to conversation

iio: humidity: dht11: Don't warn on memory allocation failure

PATCH In review Uwe Kleine-König 3 participants 11 messages Jul 18, 2022 – Jul 19, 2022 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 iio: humidity: dht11: Don't warn on memory allocation failure

The kernel is already quite noisy if a memory allocation fails. So it's the usual policy to not add another message on the driver level.

Drop the error message accordingly.

---
Unchanged since (implicit) v1

 drivers/iio/humidity/dht11.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/humidity/dht11.c +1 -3
index c97e25448772..891b6bf0b4ca 100644
@@ -295,10 +295,8 @@ static int dht11_probe(struct platform_device *pdev)
 	struct iio_dev *iio;
 
 	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
-	if (!iio) {
-		dev_err(dev, "Failed to allocate IIO device\n");
+	if (!iio)
 		return -ENOMEM;
-	}
 
 	dht11 = iio_priv(iio);
 	dht11->dev = dev;

base-commit: f2906aa863381afb0015a9eb7fefad885d4e5a56

Signature · 1 line
2.36.1
#1 lore raw

PATCH v2 2/3 iio: humidity: dht11: Emit error messages for probe failures

There are two exit points in the driver's probe function that fail silently. From a user perspective this is unsatisfactory because the device is unusable but there is no hint in the kernel log about the actual problem which makes it unnecessarily hard to fix the problem.

Make use of dev_err_probe() to emit a problem indication which also does the right thing if requesting the gpio return -EPROBE_DEFER.

---
Changes since (implicit) v1:

- Make it actually compile. (It helps quite a lot to have the driver to be tested enabled in the config when doing compile tests *sigh*)
- Fix a typo I added when manually splitting the original patch

 drivers/iio/humidity/dht11.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/iio/humidity/dht11.c +8 -2
index 891b6bf0b4ca..0db4f7471319 100644
@@ -293,6 +293,7 @@ static int dht11_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct dht11 *dht11;
 	struct iio_dev *iio;
+	int ret;
 
 	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
 	if (!iio)
@@ -302,7 +303,8 @@ static int dht11_probe(struct platform_device *pdev)
 	dht11->dev = dev;
 	dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
 	if (IS_ERR(dht11->gpiod))
-		return PTR_ERR(dht11->gpiod);
+		return dev_err_probe(dev, PTR_ERR(dht11->gpiod),
+				     "Failed to acquire GPIO\n");
 
 	dht11->irq = gpiod_to_irq(dht11->gpiod);
 	if (dht11->irq < 0) {
@@ -323,7 +325,11 @@ static int dht11_probe(struct platform_device *pdev)
 	iio->channels = dht11_chan_spec;
 	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
 
-	return devm_iio_device_register(dev, iio);
+	ret = devm_iio_device_register(dev, iio);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Failed to register iio device\n");
+
+	return 0;
 }
 
 static struct platform_driver dht11_driver = {
Signature · 1 line
2.36.1
#2 lore raw

Re: [PATCH v2 2/3] iio: humidity: dht11: Emit error messages for probe failures

Quoted 8 lines · Uwe Kleine-König

There are two exit points in the driver's probe function that fail silently. From a user perspective this is unsatisfactory because the device is unusable but there is no hint in the kernel log about the actual problem which makes it unnecessarily hard to fix the problem.

Make use of dev_err_probe() to emit a problem indication which also does the right thing if requesting the gpio return -EPROBE_DEFER.

...

drivers/iio/humidity/dht11.c static int dht11_probe(struct platform_device *pdev) · line 329

Quoted 2 lines
drivers/iio/humidity/dht11.c +2 -0
+       ret = devm_iio_device_register(dev, iio);
+       if (ret < 0)

Do we really need this ' < 0' part?

Quoted 1 line

+ return dev_err_probe(dev, ret, "Failed to register iio device\n");

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

Re: [PATCH v2 2/3] iio: humidity: dht11: Emit error messages for probe failures

Hello Andy,

Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Uwe Kleine-König

There are two exit points in the driver's probe function that fail silently. From a user perspective this is unsatisfactory because the device is unusable but there is no hint in the kernel log about the actual problem which makes it unnecessarily hard to fix the problem.

Make use of dev_err_probe() to emit a problem indication which also does the right thing if requesting the gpio return -EPROBE_DEFER.

Quoted 6 lines

...

drivers/iio/humidity/dht11.c static int dht11_probe(struct platform_device *pdev) · line 329

Quoted 2 lines
drivers/iio/humidity/dht11.c +2 -0
+       ret = devm_iio_device_register(dev, iio);
+       if (ret < 0)

Do we really need this ' < 0' part?

Not sure, I stumbled about that when I split the patch, too. devm_iio_device_register only returns a value <= 0 and I don't expect dev_err_probe to behave when the error is >= 0. So adding the check at least documents the expectations. But I don't have hard feelings and dropping " < 0" would be fine for me, too.

Quoted 1 line
Quoted 1 line

+ return dev_err_probe(dev, ret, "Failed to register iio device\n");

Best regards
Uwe

maintainer
#4 lore raw

Re: [PATCH v2 2/3] iio: humidity: dht11: Emit error messages for probe failures

Quoted 18 lines · On Mon, 18 Jul 2022 22:35:31 +0200 Uwe Kleine-König <[email protected]> wrote:

Hello Andy,

Quoted 15 lines · Andy Shevchenko
Quoted 8 lines · Uwe Kleine-König

There are two exit points in the driver's probe function that fail silently. From a user perspective this is unsatisfactory because the device is unusable but there is no hint in the kernel log about the actual problem which makes it unnecessarily hard to fix the problem.

Make use of dev_err_probe() to emit a problem indication which also does the right thing if requesting the gpio return -EPROBE_DEFER.

...

Quoted 2 lines
drivers/iio/humidity/dht11.c +2 -0
+       ret = devm_iio_device_register(dev, iio);
+       if (ret < 0)  
Quoted 7 lines
Quoted 1 line

Do we really need this ' < 0' part?

Not sure, I stumbled about that when I split the patch, too. devm_iio_device_register only returns a value <= 0 and I don't expect dev_err_probe to behave when the error is >= 0. So adding the check at least documents the expectations. But I don't have hard feelings and dropping " < 0" would be fine for me, too.

Slight preference for if (ret)

J

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

+ return dev_err_probe(dev, ret, "Failed to register iio device\n");

Best regards
Uwe

Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] iio: humidity: dht11: Emit error messages for probe failures

In reply to Uwe Kleine-König
Quoted 8 lines · Uwe Kleine-König

There are two exit points in the driver's probe function that fail silently. From a user perspective this is unsatisfactory because the device is unusable but there is no hint in the kernel log about the actual problem which makes it unnecessarily hard to fix the problem.

Make use of dev_err_probe() to emit a problem indication which also does the right thing if requesting the gpio return -EPROBE_DEFER.

After addressing the comment,

Quoted 49 lines

---
Changes since (implicit) v1:

- Make it actually compile. (It helps quite a lot to have the driver to be tested enabled in the config when doing compile tests *sigh*)
- Fix a typo I added when manually splitting the original patch

 drivers/iio/humidity/dht11.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/iio/humidity/dht11.c +8 -3
index 891b6bf0b4ca..0db4f7471319 100644
@@ -293,6 +293,7 @@ static int dht11_probe(struct platform_device *pdev)
        struct device *dev = &pdev->dev;
        struct dht11 *dht11;
        struct iio_dev *iio;
+       int ret;
 
        iio = devm_iio_device_alloc(dev, sizeof(*dht11));
        if (!iio)
@@ -302,7 +303,8 @@ static int dht11_probe(struct platform_device *pdev)
        dht11->dev = dev;
        dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
        if (IS_ERR(dht11->gpiod))
-               return PTR_ERR(dht11->gpiod);
+               return dev_err_probe(dev, PTR_ERR(dht11->gpiod),
+                                    "Failed to acquire GPIO\n");
 
        dht11->irq = gpiod_to_irq(dht11->gpiod);
        if (dht11->irq < 0) {
@@ -323,7 +325,11 @@ static int dht11_probe(struct platform_device *pdev)
        iio->channels = dht11_chan_spec;
        iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
 
-       return devm_iio_device_register(dev, iio);
+       ret = devm_iio_device_register(dev, iio);
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Failed to register iio device\n");
+
+       return 0;
 }
 
 static struct platform_driver dht11_driver = {
--

2.36.1

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

PATCH v2 3/3 iio: humidity: dht11: Use dev_err_probe consistently

In reply to Uwe Kleine-König

All but one error path use dev_err_probe() to emit a message. Use the same incarnation for the remaining exit point for consistency.

---
No changes since (implicit) v1

 drivers/iio/humidity/dht11.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/humidity/dht11.c +3 -4
index 0db4f7471319..d8b2cb3ef81e 100644
@@ -307,10 +307,9 @@ static int dht11_probe(struct platform_device *pdev)
 				     "Failed to acquire GPIO\n");
 
 	dht11->irq = gpiod_to_irq(dht11->gpiod);
-	if (dht11->irq < 0) {
-		dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
-		return -EINVAL;
-	}
+	if (dht11->irq < 0)
+		return dev_err_probe(dev, -EINVAL, "GPIO %d has no interrupt\n",
+				     desc_to_gpio(dht11->gpiod));
 
 	dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
Signature · 1 line
2.36.1
#7 lore raw

Re: [PATCH v2 3/3] iio: humidity: dht11: Use dev_err_probe consistently

Quoted 3 lines · Uwe Kleine-König

All but one error path use dev_err_probe() to emit a message. Use the same incarnation for the remaining exit point for consistency.

...

drivers/iio/humidity/dht11.c static int dht11_probe(struct platform_device *pdev) · line 312

Quoted 3 lines
drivers/iio/humidity/dht11.c +3 -0
+       if (dht11->irq < 0)
+               return dev_err_probe(dev, -EINVAL, "GPIO %d has no interrupt\n",
+                                    desc_to_gpio(dht11->gpiod));

Oh, what I missed is the error code shadowing. Can't we propagate the real error code?
-EINVAL --> dht11->irq

And to be honest I don't like this desc_to_gpio() usage. It's not for board files, it will bring confusing information to the user. What is important is the name of GPIO, i.o.w. "connection id".

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

Re: [PATCH v2 3/3] iio: humidity: dht11: Use dev_err_probe consistently

Quoted 1 line · Andy Shevchenko

On Mon, Jul 18, 2022 at 9:51 PM Uwe Kleine-König <[email protected]> wrote:

...

Quoted 3 lines

And to be honest I don't like this desc_to_gpio() usage. It's not for board files, it will bring confusing information to the user. What is important is the name of GPIO, i.o.w. "connection id".

Yes, I have noticed that this is in the original code, just if you want to make it better at the same time. Not sure if Jonathan wants all these in the single patch again, however it will touch almost the same line in all (three ?).

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

Re: [PATCH v2 3/3] iio: humidity: dht11: Use dev_err_probe consistently

Quoted 6 lines · On Mon, 18 Jul 2022 22:32:17 +0200 Andy Shevchenko <[email protected]>…
Quoted 1 line · Andy Shevchenko

On Mon, Jul 18, 2022 at 9:51 PM Uwe Kleine-König <[email protected]> wrote:

...

Quoted 9 lines
Quoted 3 lines

And to be honest I don't like this desc_to_gpio() usage. It's not for board files, it will bring confusing information to the user. What is important is the name of GPIO, i.o.w. "connection id".

Yes, I have noticed that this is in the original code, just if you want to make it better at the same time. Not sure if Jonathan wants all these in the single patch again, however it will touch almost the same line in all (three ?).

So ideal would be separate patches for the different change types, but meh, that's a pain as you say so given it's just a few lines I'm fine with whatever combining you want to do.

Reviewed-by on v2 1/3
#10 lore raw
In reply to Uwe Kleine-König
Quoted 5 lines · Uwe Kleine-König

The kernel is already quite noisy if a memory allocation fails. So it's the usual policy to not add another message on the driver level.

Drop the error message accordingly.

Quoted 28 lines

---
Unchanged since (implicit) v1

 drivers/iio/humidity/dht11.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/humidity/dht11.c +1 -3
index c97e25448772..891b6bf0b4ca 100644
@@ -295,10 +295,8 @@ static int dht11_probe(struct platform_device *pdev)
        struct iio_dev *iio;
 
        iio = devm_iio_device_alloc(dev, sizeof(*dht11));
-       if (!iio) {
-               dev_err(dev, "Failed to allocate IIO device\n");
+       if (!iio)
                return -ENOMEM;
-       }
 
        dht11 = iio_priv(iio);
        dht11->dev = dev;

base-commit: f2906aa863381afb0015a9eb7fefad885d4e5a56 --
2.36.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko