Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250617-must_check-devm_mutex_init-v7-3-d9e449f4d224@weissschuh.net

authored by

Thomas Weißschuh and committed by
Boqun Feng
daec29dc 3b07bb90

+7 -4
+7 -4
include/linux/mutex.h
··· 126 126 127 127 #ifdef CONFIG_DEBUG_MUTEXES 128 128 129 - int __devm_mutex_init(struct device *dev, struct mutex *lock); 129 + int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock); 130 130 131 131 #else 132 132 133 - static inline int __devm_mutex_init(struct device *dev, struct mutex *lock) 133 + static inline int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock) 134 134 { 135 135 /* 136 136 * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so ··· 141 141 142 142 #endif 143 143 144 - #define devm_mutex_init(dev, mutex) \ 144 + #define __mutex_init_ret(mutex) \ 145 145 ({ \ 146 146 typeof(mutex) mutex_ = (mutex); \ 147 147 \ 148 148 mutex_init(mutex_); \ 149 - __devm_mutex_init(dev, mutex_); \ 149 + mutex_; \ 150 150 }) 151 + 152 + #define devm_mutex_init(dev, mutex) \ 153 + __devm_mutex_init(dev, __mutex_init_ret(mutex)) 151 154 152 155 /* 153 156 * See kernel/locking/mutex.c for detailed documentation of these APIs.