Skip to contentSkip to conversation

iio: chemical: scd30: Use devm_mutex_init() over non-devm mutex_init()

PATCH Superseded Maxwell Doose 3 participants 6 messages Jun 3 – Jun 4

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: chemical: scd30: Use devm_mutex_init() over non-devm mutex_init()

The current code uses mutex_init() instead of devm_mutex_init(), which is incorrect as the rest of the file uses the devm automatic resource management API. Fix this so that the mutex is set up in the same way as the rest of the device data structure.

Fixes 64b3d8b1b0f5c ("iio: chemical: scd30: add core driver")
---
 drivers/iio/chemical/scd30_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +4 -1
index db5cc295aeab..f00979c0c196 100644
@@ -714,7 +714,10 @@ int scd30_probe(struct device *dev, int irq, const char *name, void *priv,
 	state->pressure_comp = SCD30_PRESSURE_COMP_DEFAULT;
 	state->meas_interval = SCD30_MEAS_INTERVAL_DEFAULT;
 	state->command = command;
-	mutex_init(&state->lock);
+	ret = devm_mutex_init(dev, &state->lock);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to init mutex\n");
+
 	init_completion(&state->meas_ready);
 
 	dev_set_drvdata(dev, indio_dev);
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 6 lines · On Wed, 3 Jun 2026 09:51:33 -0500 Maxwell Doose <[email protected]> wrote:

The current code uses mutex_init() instead of devm_mutex_init(), which is incorrect as the rest of the file uses the devm automatic resource management API. Fix this so that the mutex is set up in the same way as the rest of the device data structure.

Fixes 64b3d8b1b0f5c ("iio: chemical: scd30: add core driver")

Hi Maxwell,

No to this being a fix. This enhances one corner case of debug, slightly. So to me it isn't worth a fixes tag.

Quoted 10 lines
---
 drivers/iio/chemical/scd30_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +0 -0
index db5cc295aeab..f00979c0c196 100644

drivers/iio/chemical/scd30_core.c int scd30_probe(struct device *dev, int irq, const char *name, void *priv, · line 719

Quoted 7 lines
drivers/iio/chemical/scd30_core.c +3 -1
@@ -714,7 +714,10 @@ int scd30_probe(struct device *dev, int irq, const char *name, void *priv,
 	state->pressure_comp = SCD30_PRESSURE_COMP_DEFAULT;
 	state->meas_interval = SCD30_MEAS_INTERVAL_DEFAULT;
 	state->command = command;
-	mutex_init(&state->lock);
+	ret = devm_mutex_init(dev, &state->lock);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to init mutex\n");

The only thing it can return is -ENOMEM I think. Which doesn't print anything so

	if (ret)
		return ret;

is enough.

Quoted 4 lines
drivers/iio/chemical/scd30_core.c +1 -0
+
 	init_completion(&state->meas_ready);
 
 	dev_set_drvdata(dev, indio_dev);
#2 lore raw
Quoted 9 lines · Jonathan Cameron
Quoted 5 lines · On Wed, 3 Jun 2026 09:51:33 -0500 Maxwell Doose <[email protected]> wrote:

The current code uses mutex_init() instead of devm_mutex_init(), which is incorrect as the rest of the file uses the devm automatic resource management API. Fix this so that the mutex is set up in the same way as the rest of the device data structure.

Quoted 6 lines
Quoted 1 line
Fixes 64b3d8b1b0f5c ("iio: chemical: scd30: add core driver")

Hi Maxwell,

No to this being a fix. This enhances one corner case of debug, slightly. So to me it isn't worth a fixes tag.

Can drop. I guess I was just erring on the side of "we're fixing the original maintainer's/contributor's mistake when they did devm" but I guess that's a fallacy since the only way any issues could arise is if the device gets deregistered and reregistered.

Quoted 19 lines
Quoted 10 lines
---
 drivers/iio/chemical/scd30_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +0 -0
index db5cc295aeab..f00979c0c196 100644

drivers/iio/chemical/scd30_core.c int scd30_probe(struct device *dev, int irq, const char *name, void *priv, · line 719

Quoted 7 lines
drivers/iio/chemical/scd30_core.c +3 -1
@@ -714,7 +714,10 @@ int scd30_probe(struct device *dev, int irq, const char *name, void *priv,
      state->pressure_comp = SCD30_PRESSURE_COMP_DEFAULT;
      state->meas_interval = SCD30_MEAS_INTERVAL_DEFAULT;
      state->command = command;
-     mutex_init(&state->lock);
+     ret = devm_mutex_init(dev, &state->lock);
+     if (ret)
+             return dev_err_probe(dev, ret, "Failed to init mutex\n");

The only thing it can return is -ENOMEM I think. Which doesn't print anything so

Quoted 5 lines
        if (ret)
                return ret;

is enough.

If I'm not wrong (and after a quick google search), I think it also may return -EINVAL but then that means the device struct is invalid. Still may be worth doing dev_err_probe(dev, ret, "Failed to init mutex with error %d", ret); or something along those lines. If you want I can get this out today or I can also wait for more reviewer feedback, but your recommended changes seem pretty simple. TBH though I'd still argue for the case of either dev_err_probe() or dev_warn() since not having an initialized mutex is certainly an error.

Signature · 2 lines
best regards,
max
#3 lore raw
Quoted 3 lines · Maxwell Doose
Quoted 2 lines · Jonathan Cameron

On Wed, 3 Jun 2026 09:51:33 -0500
Maxwell Doose <[email protected]> wrote:

...

Quoted 8 lines
Quoted 8 lines

drivers/iio/chemical/scd30_core.c int scd30_probe(struct device *dev, int irq, const char *name, void *priv, · line 719

Quoted 3 lines
drivers/iio/chemical/scd30_core.c +3 -0
+     ret = devm_mutex_init(dev, &state->lock);
+     if (ret)
+             return dev_err_probe(dev, ret, "Failed to init mutex\n");

The only thing it can return is -ENOMEM I think. Which doesn't print anything so

        if (ret)
                return ret;
Quoted 4 lines
Quoted 1 line

is enough.

If I'm not wrong (and after a quick google search), I think it also may return -EINVAL but then that means the device struct is invalid.

What?!

Quoted 6 lines

Still may be worth doing dev_err_probe(dev, ret, "Failed to init mutex with error %d", ret); or something along those lines. If you want I can get this out today or I can also wait for more reviewer feedback, but your recommended changes seem pretty simple. TBH though I'd still argue for the case of either dev_err_probe() or dev_warn() since not having an initialized mutex is certainly an error.

I am on the side of dropping the message altogether. Invalid dev here is nonsense. Just read the code.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 16 lines · Andy Shevchenko
Quoted 3 lines · Maxwell Doose
Quoted 2 lines · Jonathan Cameron

On Wed, 3 Jun 2026 09:51:33 -0500
Maxwell Doose <[email protected]> wrote:

...

Quoted 8 lines
Quoted 8 lines

drivers/iio/chemical/scd30_core.c int scd30_probe(struct device *dev, int irq, const char *name, void *priv, · line 719

Quoted 3 lines
drivers/iio/chemical/scd30_core.c +3 -0
+     ret = devm_mutex_init(dev, &state->lock);
+     if (ret)
+             return dev_err_probe(dev, ret, "Failed to init mutex\n");

The only thing it can return is -ENOMEM I think. Which doesn't print anything so

        if (ret)
                return ret;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

is enough.

If I'm not wrong (and after a quick google search), I think it also may return -EINVAL but then that means the device struct is invalid.

What?!

Then I was either misled or I'm just stupid :(

Quoted 10 lines
Quoted 6 lines

Still may be worth doing dev_err_probe(dev, ret, "Failed to init mutex with error %d", ret); or something along those lines. If you want I can get this out today or I can also wait for more reviewer feedback, but your recommended changes seem pretty simple. TBH though I'd still argue for the case of either dev_err_probe() or dev_warn() since not having an initialized mutex is certainly an error.

I am on the side of dropping the message altogether. Invalid dev here is nonsense. Just read the code.

All of these changes have been made in v2.

Signature · 2 lines
best regards,
max
#5 lore raw
Quoted 6 lines · Maxwell Doose
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Maxwell Doose
Quoted 2 lines · Jonathan Cameron

On Wed, 3 Jun 2026 09:51:33 -0500
Maxwell Doose <[email protected]> wrote:

...

Quoted 25 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/chemical/scd30_core.c int scd30_probe(struct device *dev, int irq, const char *name, void *priv, · line 719

Quoted 3 lines
drivers/iio/chemical/scd30_core.c +3 -0
+     ret = devm_mutex_init(dev, &state->lock);
+     if (ret)
+             return dev_err_probe(dev, ret, "Failed to init mutex\n");

The only thing it can return is -ENOMEM I think. Which doesn't print anything so

        if (ret)
                return ret;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

is enough.

If I'm not wrong (and after a quick google search), I think it also may return -EINVAL but then that means the device struct is invalid.

What?!

Then I was either misled or I'm just stupid :(

Quoted 7 lines
Quoted 6 lines

Still may be worth doing dev_err_probe(dev, ret, "Failed to init mutex with error %d", ret); or something along those lines. If you want I can get this out today or I can also wait for more reviewer feedback, but your recommended changes seem pretty simple. TBH though I'd still argue for the case of either dev_err_probe() or dev_warn() since not having an initialized mutex is certainly an error.

Quoted 4 lines
Quoted 2 lines

I am on the side of dropping the message altogether. Invalid dev here is nonsense. Just read the code.

All of these changes have been made in v2.

I am not sure I follow. I was specifically talking about implementation of dev_mutex_init().

Signature · 2 lines
With Best Regards,
Andy Shevchenko