Skip to content

Commit 4cd4722

Browse files
geo-starklag-linaro
authored andcommitted
locking/mutex: Introduce devm_mutex_init()
Using of devm API leads to a certain order of releasing resources. So all dependent resources which are not devm-wrapped should be deleted with respect to devm-release order. Mutex is one of such objects that often is bound to other resources and has no own devm wrapping. Since mutex_destroy() actually does nothing in non-debug builds frequently calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() will be extended so introduce devm_mutex_init(). Suggested-by: Christophe Leroy <[email protected]> Signed-off-by: George Stark <[email protected]> Reviewed-by: Christophe Leroy <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Reviewed-by: Marek Behún <[email protected]> Acked-by: Waiman Long <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent 4cece76 commit 4cd4722

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

include/linux/mutex.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/cleanup.h>
2323
#include <linux/mutex_types.h>
2424

25+
struct device;
26+
2527
#ifdef CONFIG_DEBUG_LOCK_ALLOC
2628
# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
2729
, .dep_map = { \
@@ -117,6 +119,31 @@ do { \
117119
} while (0)
118120
#endif /* CONFIG_PREEMPT_RT */
119121

122+
#ifdef CONFIG_DEBUG_MUTEXES
123+
124+
int __devm_mutex_init(struct device *dev, struct mutex *lock);
125+
126+
#else
127+
128+
static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
129+
{
130+
/*
131+
* When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
132+
* no really need to register it in the devm subsystem.
133+
*/
134+
return 0;
135+
}
136+
137+
#endif
138+
139+
#define devm_mutex_init(dev, mutex) \
140+
({ \
141+
typeof(mutex) mutex_ = (mutex); \
142+
\
143+
mutex_init(mutex_); \
144+
__devm_mutex_init(dev, mutex_); \
145+
})
146+
120147
/*
121148
* See kernel/locking/mutex.c for detailed documentation of these APIs.
122149
* Also see Documentation/locking/mutex-design.rst.

kernel/locking/mutex-debug.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
#include <linux/mutex.h>
1414
#include <linux/delay.h>
15+
#include <linux/device.h>
1516
#include <linux/export.h>
1617
#include <linux/poison.h>
1718
#include <linux/sched.h>
@@ -89,6 +90,17 @@ void debug_mutex_init(struct mutex *lock, const char *name,
8990
lock->magic = lock;
9091
}
9192

93+
static void devm_mutex_release(void *res)
94+
{
95+
mutex_destroy(res);
96+
}
97+
98+
int __devm_mutex_init(struct device *dev, struct mutex *lock)
99+
{
100+
return devm_add_action_or_reset(dev, devm_mutex_release, lock);
101+
}
102+
EXPORT_SYMBOL_GPL(__devm_mutex_init);
103+
92104
/***
93105
* mutex_destroy - mark a mutex unusable
94106
* @lock: the mutex to be destroyed

0 commit comments

Comments
 (0)