Skip to contentSkip to conversation

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

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

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
v2:
- Drop fixes tag per Jonathan's suggestion.
- Replace dev_err_probe() with return -ENOMEM per Jonathan and Andy's suggestions.

 drivers/iio/chemical/scd30_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +4 -1
index db5cc295aeab..198add58affd 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 -ENOMEM;
+
 	init_completion(&state->meas_ready);
 
 	dev_set_drvdata(dev, indio_dev);
Signature · 1 line
2.54.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Maxwell Doose

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.

---

reviewer
#2 lore raw
In reply to Maxwell Doose
Quoted 21 lines · Maxwell Doose

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.

---
v2:
- Drop fixes tag per Jonathan's suggestion.
- Replace dev_err_probe() with return -ENOMEM per Jonathan and Andy's suggestions.

 drivers/iio/chemical/scd30_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +0 -0
index db5cc295aeab..198add58affd 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 -ENOMEM;

Why are we ignoring ret?

I would expect:

              return ret;
Quoted 7 lines
drivers/iio/chemical/scd30_core.c +1 -1
+
        init_completion(&state->meas_ready);
 
        dev_set_drvdata(dev, indio_dev);
--

2.54.0

#3 lore raw
Quoted 33 lines · David Lechner
Quoted 21 lines · Maxwell Doose

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.

---
v2:
- Drop fixes tag per Jonathan's suggestion.
- Replace dev_err_probe() with return -ENOMEM per Jonathan and Andy's suggestions.

 drivers/iio/chemical/scd30_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +0 -0
index db5cc295aeab..198add58affd 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 -ENOMEM;

Why are we ignoring ret?

Quoted 4 lines

I would expect:

              return ret;

Gah, I must've Jonathan's + Andy's comments get to my head (He said it should likely only return -ENOMEM) :(
I don't know if he'll want to tweak while applying or if I should just go back and resubmit.

#4 lore raw
Quoted 2 lines · Maxwell Doose
Quoted 1 line · David Lechner

On Thu, Jun 4, 2026 at 3:17 PM Maxwell Doose <[email protected]> wrote:

...

Quoted 6 lines
Quoted 6 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 -ENOMEM;

Why are we ignoring ret?

Quoted 8 lines
Quoted 3 lines

I would expect:

              return ret;

Gah, I must've Jonathan's + Andy's comments get to my head (He said it should likely only return -ENOMEM) :(
I don't know if he'll want to tweak while applying or if I should just go back and resubmit.

What we meant is that:

- devm_mutex_init() may return only -ENOMEM as of today
- dev_err_probe() will ignore (print nothing) when err == -ENOMEM

It has nothing to do with the caller, we explained how callee do. TL;DR: it should be simple return ret; as Jonathan said already.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw
In reply to Maxwell Doose
Quoted 36 lines · On Thu, 4 Jun 2026 11:21:07 -0500 Maxwell Doose <[email protected]> wrote:
Quoted 35 lines · David Lechner
Quoted 21 lines · Maxwell Doose

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.

---
v2:
- Drop fixes tag per Jonathan's suggestion.
- Replace dev_err_probe() with return -ENOMEM per Jonathan and Andy's suggestions.

 drivers/iio/chemical/scd30_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +0 -0
index db5cc295aeab..198add58affd 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 -ENOMEM;  

Why are we ignoring ret?

I would expect:

Quoted 7 lines
Quoted 2 lines
              return ret;

Gah, I must've Jonathan's + Andy's comments get to my head (He said it should likely only return -ENOMEM) :(
I don't know if he'll want to tweak while applying or if I should just go back and resubmit.

IIO is effectively closed for this cycle so plenty of time and I'm lazy so please send a v3.

Jonathan