Skip to content

Commit daec29d

Browse files
t-8chfbq
authored andcommitted
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 <[email protected]> Signed-off-by: Thomas Weißschuh <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Reviewed-by: Bartosz Golaszewski <[email protected]> Signed-off-by: Boqun Feng <[email protected]> Link: https://lore.kernel.org/r/20250617-must_check-devm_mutex_init-v7-3-d9e449f4d224@weissschuh.net
1 parent 3b07bb9 commit daec29d

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

include/linux/mutex.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ do { \
126126

127127
#ifdef CONFIG_DEBUG_MUTEXES
128128

129-
int __devm_mutex_init(struct device *dev, struct mutex *lock);
129+
int __must_check __devm_mutex_init(struct device *dev, struct mutex *lock);
130130

131131
#else
132132

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)
134134
{
135135
/*
136136
* 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)
141141

142142
#endif
143143

144-
#define devm_mutex_init(dev, mutex) \
144+
#define __mutex_init_ret(mutex) \
145145
({ \
146146
typeof(mutex) mutex_ = (mutex); \
147147
\
148148
mutex_init(mutex_); \
149-
__devm_mutex_init(dev, mutex_); \
149+
mutex_; \
150150
})
151151

152+
#define devm_mutex_init(dev, mutex) \
153+
__devm_mutex_init(dev, __mutex_init_ret(mutex))
154+
152155
/*
153156
* See kernel/locking/mutex.c for detailed documentation of these APIs.
154157
* Also see Documentation/locking/mutex-design.rst.

0 commit comments

Comments
 (0)