Skip to contentSkip to conversation

locking/mutex: Mark devm_mutex_init() as __must_check

PATCH Applied Thomas Weißschuh 5 participants 15 messages Jun 17, 2025 – Jul 19, 2025

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

v7 cover locking/mutex: Mark devm_mutex_init() as __must_check

devm_mutex_init() can fail. Make sure everybody checks the return value. All patches should go through the mutex tree together.

It would be great if this could go in through a single tree at once.

---
Changes in v7:
- Pick up Ack from Andrew
- Reword commit messages
- Link to v6: https://lore.kernel.org/r/20250609-must_check-devm_mutex_init-v6-0-9540d5df9704@weissschuh.net

Changes in v6:
- Rebase on v6.16-rc1
- Pick up review tag from Bartosz
- Fix up spi-nxp-fspi
- Fix up leds-lp8860
- Link to v5: https://lore.kernel.org/r/20250505-must_check-devm_mutex_init-v5-1-92fa4b793c6e@weissschuh.net

Changes in v5:
- Pick up review tag from Andy
- Link to v4: https://lore.kernel.org/r/20250407-must_check-devm_mutex_init-v4-1-587bacc9f6b3@weissschuh.net

Changes in v4:
- Drop already applied leds-1202 driver patch
- Rebase on v6.15-rc1
- Link to v3: https://lore.kernel.org/r/20250208-must_check-devm_mutex_init-v3-0-245e417dcc9e@weissschuh.net

Changes in v3:
- Introduce and use helper macro __mutex_init_ret()
- Link to v2: https://lore.kernel.org/r/20250204-must_check-devm_mutex_init-v2-0-7b6271c4b7e6@weissschuh.net

Changes in v2:
- Rebase on 6.14-rc1
- Fix up leds-1202 driver
- Link to v1: https://lore.kernel.org/r/20241202-must_check-devm_mutex_init-v1-1-e60eb97b8c72@weissschuh.net

---
Thomas Weißschuh (3):
spi: spi-nxp-fspi: check return value of devm_mutex_init()
leds: lp8860: Check return value of devm_mutex_init() locking/mutex: Mark devm_mutex_init() as __must_check

 drivers/leds/leds-lp8860.c |  4 +++-
 drivers/spi/spi-nxp-fspi.c |  4 +++-
 include/linux/mutex.h      | 11 +++++++----
 3 files changed, 13 insertions(+), 6 deletions(-)

---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20241031-must_check-devm_mutex_init-cac583bda8fe

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
#1 lore raw

PATCH v7 1/3 spi: spi-nxp-fspi: check return value of devm_mutex_init()

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

Fixes 48900813abd2 ("spi: spi-nxp-fspi: remove the goto in probe")
---
 drivers/spi/spi-nxp-fspi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-nxp-fspi.c +3 -1
index e63c77e418231cd0698ffb73eeeebfbe63cc3065..f3d5765054132cd18b7257439ece971f58e9ceb2 100644
@@ -1273,7 +1273,9 @@ static int nxp_fspi_probe(struct platform_device *pdev)
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to request irq\n");
 
-	devm_mutex_init(dev, &f->lock);
+	ret = devm_mutex_init(dev, &f->lock);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to initialize lock\n");
 
 	ctlr->bus_num = -1;
 	ctlr->num_chipselect = NXP_FSPI_MAX_CHIPSELECT;
Signature · 1 line
2.50.0
maintainer Reviewed-by on v7 1/3
#2 lore raw

Re: [PATCH v7 1/3] spi: spi-nxp-fspi: check return value of devm_mutex_init()

Quoted 4 lines · Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

#3 lore raw

[tip: locking/core] spi: spi-nxp-fspi: Check return value of devm_mutex_init()

In reply to Thomas Weißschuh
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     d24a54e032021cf381af3c3cf119cc5cf6b3c1be
Gitweb:        https://git.kernel.org/tip/d24a54e032021cf381af3c3cf119cc5cf6b3c1be
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Tue, 17 Jun 2025 19:08:12 +02:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Fri, 11 Jul 2025 15:02:07 -07:00

spi: spi-nxp-fspi: Check return value of devm_mutex_init()

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

---
 drivers/spi/spi-nxp-fspi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-nxp-fspi.c +3 -1
index e63c77e..f3d5765 100644
@@ -1273,7 +1273,9 @@ static int nxp_fspi_probe(struct platform_device *pdev)
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to request irq\n");
 
-	devm_mutex_init(dev, &f->lock);
+	ret = devm_mutex_init(dev, &f->lock);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to initialize lock\n");
 
 	ctlr->bus_num = -1;
 	ctlr->num_chipselect = NXP_FSPI_MAX_CHIPSELECT;
#4 lore raw

PATCH v7 2/3 leds: lp8860: Check return value of devm_mutex_init()

In reply to Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

Fixes 87a59548af95 ("leds: lp8860: Use new mutex guards to cleanup function exits")
---
 drivers/leds/leds-lp8860.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/leds/leds-lp8860.c +3 -1
index 52b97c9f2a03567aa12d4f63a951593a5e7017d5..0962c00c215a11f555a7878a3b65824b5219a1fa 100644
@@ -307,7 +307,9 @@ static int lp8860_probe(struct i2c_client *client)
 	led->client = client;
 	led->led_dev.brightness_set_blocking = lp8860_brightness_set;
 
-	devm_mutex_init(&client->dev, &led->lock);
+	ret = devm_mutex_init(&client->dev, &led->lock);
+	if (ret)
+		return dev_err_probe(&client->dev, ret, "Failed to initialize lock\n");
 
 	led->regmap = devm_regmap_init_i2c(client, &lp8860_regmap_config);
 	if (IS_ERR(led->regmap)) {
Signature · 1 line
2.50.0
maintainer Applied
#5 lore raw

Re: (subset) [PATCH v7 2/3] leds: lp8860: Check return value of devm_mutex_init()

Quoted 6 lines · Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

Applied, thanks!

[2/3] leds: lp8860: Check return value of devm_mutex_init()

      commit: 426e0c8e8eed26b67bbbd138483bb5973724adae

--
Lee Jones [李琼斯]

#6 lore raw

Re: (subset) [PATCH v7 2/3] leds: lp8860: Check return value of devm_mutex_init()

Hi Lee,

Quoted 10 lines · Lee Jones
Quoted 4 lines · Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

Applied, thanks!

[2/3] leds: lp8860: Check return value of devm_mutex_init()

      commit: 426e0c8e8eed26b67bbbd138483bb5973724adae

Thanks, but (as mentioned in the cover letter) these patches should go together through the mutex/locking tree.
Could you drop it on your side and give an Ack instead?

Thomas

maintainer
#7 lore raw

Re: (subset) [PATCH v7 2/3] leds: lp8860: Check return value of devm_mutex_init()

Quoted 9 lines · Thomas Weißschuh

Hi Lee,

Quoted 6 lines · Lee Jones
Quoted 4 lines · Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

Quoted 8 lines
Quoted 4 lines

Applied, thanks!

[2/3] leds: lp8860: Check return value of devm_mutex_init()

      commit: 426e0c8e8eed26b67bbbd138483bb5973724adae

Thanks, but (as mentioned in the cover letter) these patches should go together through the mutex/locking tree.
Could you drop it on your side and give an Ack instead?

There has to be good reasons to do this.

I didn't see any dependents or dependencies in this patch.

Signature · 1 line
Lee Jones [李琼斯]
#8 lore raw

Re: (subset) [PATCH v7 2/3] leds: lp8860: Check return value of devm_mutex_init()

Quoted 13 lines · Lee Jones
Quoted 12 lines · Thomas Weißschuh
Quoted 10 lines · Lee Jones
Quoted 4 lines · Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

Applied, thanks!

[2/3] leds: lp8860: Check return value of devm_mutex_init()

      commit: 426e0c8e8eed26b67bbbd138483bb5973724adae
Quoted 7 lines
Quoted 3 lines

Thanks, but (as mentioned in the cover letter) these patches should go together through the mutex/locking tree.
Could you drop it on your side and give an Ack instead?

There has to be good reasons to do this.

I didn't see any dependents or dependencies in this patch.

Patch 3 depends on patch 1 and 2.

It will break the build for each instance of an ignored return value of devm_mutex_init(). Therefore all such instances need to be resolved before the patch can be applied.
So the patches can't go through different trees.

In theory we could fix the drivers in this cycle and then change devm_mutex_init() in the next one. But new regressions are introduced over and over. This patch is already in the third cycle...

Thomas

maintainer Acked-by
#9 lore raw

Re: (subset) [PATCH v7 2/3] leds: lp8860: Check return value of devm_mutex_init()

Quoted 24 lines · Thomas Weißschuh
Quoted 13 lines · Lee Jones
Quoted 12 lines · Thomas Weißschuh
Quoted 10 lines · Lee Jones
Quoted 4 lines · Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

Applied, thanks!

[2/3] leds: lp8860: Check return value of devm_mutex_init()

      commit: 426e0c8e8eed26b67bbbd138483bb5973724adae
Quoted 7 lines
Quoted 3 lines

Thanks, but (as mentioned in the cover letter) these patches should go together through the mutex/locking tree.
Could you drop it on your side and give an Ack instead?

There has to be good reasons to do this.

I didn't see any dependents or dependencies in this patch.

Patch 3 depends on patch 1 and 2.

Quoted 8 lines

It will break the build for each instance of an ignored return value of devm_mutex_init(). Therefore all such instances need to be resolved before the patch can be applied.
So the patches can't go through different trees.

In theory we could fix the drivers in this cycle and then change devm_mutex_init() in the next one. But new regressions are introduced over and over. This patch is already in the third cycle...

Fair point.

And patch removed from LEDs.

Signature · 1 line
Lee Jones [李琼斯]
#10 lore raw

[tip: locking/core] leds: lp8860: Check return value of devm_mutex_init()

In reply to Thomas Weißschuh
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     3b07bb900af7f43f13f9ff398b4c6ca1dee217cd
Gitweb:        https://git.kernel.org/tip/3b07bb900af7f43f13f9ff398b4c6ca1dee217cd
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Tue, 17 Jun 2025 19:08:13 +02:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Fri, 11 Jul 2025 15:11:19 -07:00

leds: lp8860: Check return value of devm_mutex_init()

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.

Add the missed check.

---
 drivers/leds/leds-lp8860.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/leds/leds-lp8860.c +3 -1
index 52b97c9..0962c00 100644
@@ -307,7 +307,9 @@ static int lp8860_probe(struct i2c_client *client)
 	led->client = client;
 	led->led_dev.brightness_set_blocking = lp8860_brightness_set;
 
-	devm_mutex_init(&client->dev, &led->lock);
+	ret = devm_mutex_init(&client->dev, &led->lock);
+	if (ret)
+		return dev_err_probe(&client->dev, ret, "Failed to initialize lock\n");
 
 	led->regmap = devm_regmap_init_i2c(client, &lp8860_regmap_config);
 	if (IS_ERR(led->regmap)) {
#11 lore raw

PATCH v7 3/3 locking/mutex: Mark devm_mutex_init() as __must_check

In reply to Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.
Enforce all callers check the return value through the compiler.

As devm_mutex_init() itself is a macro, it can not be annotated directly. Annotate __devm_mutex_init() instead. Unfortunately __must_check/warn_unused_result don't propagate through statement expression. So move the statement expression into the argument list of the call to __devm_mutex_init() through a helper macro.

---
 include/linux/mutex.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
include/linux/mutex.h +7 -4
index a039fa8c17807c700d3b61193feac0418cad1243..00afd341d293ddfcc0a427b576efdce044955e38 100644
@@ -126,11 +126,11 @@ do {							\
 
 #ifdef CONFIG_DEBUG_MUTEXES
 
-int __devm_mutex_init(struct device *dev, struct mutex *lock);
+int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock);
 
 #else
 
-static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
+static inline int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock)
 {
 	/*
 	 * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
@@ -141,14 +141,17 @@ static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
 
 #endif
 
-#define devm_mutex_init(dev, mutex)			\
+#define __mutex_init_ret(mutex)				\
 ({							\
 	typeof(mutex) mutex_ = (mutex);			\
 							\
 	mutex_init(mutex_);				\
-	__devm_mutex_init(dev, mutex_);			\
+	mutex_;						\
 })
 
+#define devm_mutex_init(dev, mutex) \
+	__devm_mutex_init(dev, __mutex_init_ret(mutex))
+
 /*
  * See kernel/locking/mutex.c for detailed documentation of these APIs.
  * Also see Documentation/locking/mutex-design.rst.
Signature · 1 line
2.50.0
#12 lore raw
Quoted 10 lines · Thomas Weißschuh

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.
Enforce all callers check the return value through the compiler.

As devm_mutex_init() itself is a macro, it can not be annotated directly. Annotate __devm_mutex_init() instead. Unfortunately __must_check/warn_unused_result don't propagate through statement expression. So move the statement expression into the argument list of the call to __devm_mutex_init() through a helper macro.

Given it's Peter's suggestion [1] for __mutex_init_ret(), I think a

should be added here. No need for a new version just for that. If patch #1 got some reviews, I would queue this in my lockdep-for-tip branch. Hopefully these will land v6.17.

[1]: https://lore.kernel.org/all/[email protected]/

Regards,
Boqun

Quoted 49 lines
---
 include/linux/mutex.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
include/linux/mutex.h +7 -4
index a039fa8c17807c700d3b61193feac0418cad1243..00afd341d293ddfcc0a427b576efdce044955e38 100644
@@ -126,11 +126,11 @@ do {							\
 
 #ifdef CONFIG_DEBUG_MUTEXES
 
-int __devm_mutex_init(struct device *dev, struct mutex *lock);
+int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock);
 
 #else
 
-static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
+static inline int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock)
 {
 	/*
 	 * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
@@ -141,14 +141,17 @@ static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
 
 #endif
 
-#define devm_mutex_init(dev, mutex)			\
+#define __mutex_init_ret(mutex)				\
 ({							\
 	typeof(mutex) mutex_ = (mutex);			\
 							\
 	mutex_init(mutex_);				\
-	__devm_mutex_init(dev, mutex_);			\
+	mutex_;						\
 })
 
+#define devm_mutex_init(dev, mutex) \
+	__devm_mutex_init(dev, __mutex_init_ret(mutex))
+
 /*
  * See kernel/locking/mutex.c for detailed documentation of these APIs.
  * Also see Documentation/locking/mutex-design.rst.
Signature · 2 lines
-- 
2.50.0
#13 lore raw
In reply to Thomas Weißschuh
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     daec29dcc8731b7596690ab9f647839e4584a86d
Gitweb:        https://git.kernel.org/tip/daec29dcc8731b7596690ab9f647839e4584a86d
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Tue, 17 Jun 2025 19:08:14 +02:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Fri, 11 Jul 2025 15:11:54 -07:00

locking/mutex: Mark devm_mutex_init() as __must_check

devm_mutex_init() can fail. With CONFIG_DEBUG_MUTEXES=y the mutex will be marked as unusable and trigger errors on usage.
Enforce all callers check the return value through the compiler.

As devm_mutex_init() itself is a macro, it can not be annotated directly. Annotate __devm_mutex_init() instead. Unfortunately __must_check/warn_unused_result don't propagate through statement expression. So move the statement expression into the argument list of the call to __devm_mutex_init() through a helper macro.

---
 include/linux/mutex.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
include/linux/mutex.h +7 -4
index a039fa8..00afd34 100644
@@ -126,11 +126,11 @@ do {							\
 
 #ifdef CONFIG_DEBUG_MUTEXES
 
-int __devm_mutex_init(struct device *dev, struct mutex *lock);
+int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock);
 
 #else
 
-static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
+static inline int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock)
 {
 	/*
 	 * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
@@ -141,14 +141,17 @@ static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
 
 #endif
 
-#define devm_mutex_init(dev, mutex)			\
+#define __mutex_init_ret(mutex)				\
 ({							\
 	typeof(mutex) mutex_ = (mutex);			\
 							\
 	mutex_init(mutex_);				\
-	__devm_mutex_init(dev, mutex_);			\
+	mutex_;						\
 })
 
+#define devm_mutex_init(dev, mutex) \
+	__devm_mutex_init(dev, __mutex_init_ret(mutex))
+
 /*
  * See kernel/locking/mutex.c for detailed documentation of these APIs.
  * Also see Documentation/locking/mutex-design.rst.