[PATCH] locking/mutex: annotate __devm_mutex_init() with __must_check

Bartosz Golaszewski posted 1 patch 8 months, 1 week ago
include/linux/mutex.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] locking/mutex: annotate __devm_mutex_init() with __must_check
Posted by Bartosz Golaszewski 8 months, 1 week ago
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Unlike the non-managed mutex_init(), devm_mutex_init() can fail (for
instance: on OOM). Now that we've fixed all instances of users not
checking the return value, annotate it with __must_check to avoid this
problem in the future.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 include/linux/mutex.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index 2143d05116be..e194f8c22d72 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -126,7 +126,7 @@ 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
 

---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250326-devm-mutex-must-check-8c1b94ae151f

Best regards,
-- 
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Re: [PATCH] locking/mutex: annotate __devm_mutex_init() with __must_check
Posted by Thomas Weißschuh 8 months, 1 week ago
On Mon, Apr 07, 2025 at 09:03:46AM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> Unlike the non-managed mutex_init(), devm_mutex_init() can fail (for
> instance: on OOM). Now that we've fixed all instances of users not
> checking the return value, annotate it with __must_check to avoid this
> problem in the future.
> 
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
>  include/linux/mutex.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/mutex.h b/include/linux/mutex.h
> index 2143d05116be..e194f8c22d72 100644
> --- a/include/linux/mutex.h
> +++ b/include/linux/mutex.h
> @@ -126,7 +126,7 @@ 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);

This didn't work for me, see [0].

	"Unfortunately __must_check/warn_unused_result don't propagate through
	statement expression."

Also it is missing the !CONFIG_DEBUG_MUTEXES case.
I'll resend my patch once more.


[0] https://lore.kernel.org/lkml/20250208-must_check-devm_mutex_init-v3-2-245e417dcc9e@weissschuh.net/

>  
>  #else
>  
> 
> ---
> base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
> change-id: 20250326-devm-mutex-must-check-8c1b94ae151f
> 
> Best regards,
> -- 
> Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
Re: [PATCH] locking/mutex: annotate __devm_mutex_init() with __must_check
Posted by Bartosz Golaszewski 8 months, 1 week ago
On Mon, Apr 7, 2025 at 9:21 AM Thomas Weißschuh
<thomas.weissschuh@linutronix.de> wrote:
>
> On Mon, Apr 07, 2025 at 09:03:46AM +0200, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> >
> > Unlike the non-managed mutex_init(), devm_mutex_init() can fail (for
> > instance: on OOM). Now that we've fixed all instances of users not
> > checking the return value, annotate it with __must_check to avoid this
> > problem in the future.
> >
> > Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > ---
> >  include/linux/mutex.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/include/linux/mutex.h b/include/linux/mutex.h
> > index 2143d05116be..e194f8c22d72 100644
> > --- a/include/linux/mutex.h
> > +++ b/include/linux/mutex.h
> > @@ -126,7 +126,7 @@ 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);
>
> This didn't work for me, see [0].
>
>         "Unfortunately __must_check/warn_unused_result don't propagate through
>         statement expression."
>
> Also it is missing the !CONFIG_DEBUG_MUTEXES case.
> I'll resend my patch once more.
>
>
> [0] https://lore.kernel.org/lkml/20250208-must_check-devm_mutex_init-v3-2-245e417dcc9e@weissschuh.net/
>

Thanks, I should have actually tested it, duh.

Bartosz