locking: introduce devm_mutex_init and devm_mutex_destroy
RFC PATCH 1/1 locking: introduce devm_mutex_init and devm_mutex_destroy
Using of devm API leads to 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() and devm_mutex_destroy().
--- include/linux/mutex.h | 3 +++ kernel/locking/mutex.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+)
include/linux/mutex.h +3 -0
@@ -119,6 +119,9 @@ do { \extern void __mutex_init (struct mutex *lock,const char *name,struct lock_class_key *key); +int devm_mutex_init (struct device *dev,struct mutex *lock); +void devm_mutex_destroy (struct device *dev,struct mutex *lock); +/** * mutex_is_locked - is the mutex locked * @lock: the mutex to be queried
kernel/locking/mutex.c +37 -0
@@ -56,6 +56,43 @@ __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) }EXPORT_SYMBOL (__mutex_init); +static void devm_mutex_release (struct device *dev,void *res) +{ +mutex_destroy (*(struct mutex **)res); +} + +static int devm_mutex_match (struct device *dev,void *res,void *data) +{ +struct mutex **r = res; + +if (WARN_ON (!r || !*r)) +return 0 ; + +return *r == data; +} + +int devm_mutex_init (struct device *dev,struct mutex *lock) +{ +struct mutex **ptr; + + ptr =devres_alloc (devm_mutex_release,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return -ENOMEM ; + +mutex_init (lock); + + *ptr = lock; +devres_add (dev, ptr); +return 0 ; +} +EXPORT_SYMBOL (devm_mutex_init); + +void devm_mutex_destroy (struct device *dev,struct mutex *lock) +{ +devres_release (dev, devm_mutex_release, devm_mutex_match, lock); +} +EXPORT_SYMBOL (devm_mutex_destroy); +/* * @owner: contains: 'struct task_struct *' to the current lock owner, * NULL means not owned. Since task_struct pointers are aligned at
Signature · 1 line
2.38.4
Waiman Long
reviewerQuoted 67 lines · George Stark
Using of devm API leads to 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 callingmutex_destroy()is just ignored which is safe for now but wrong formally and can lead to a problem ifmutex_destroy()will be extended so introducedevm_mutex_init()anddevm_mutex_destroy().Signed-off-by George Stark <[email protected]>--- include/linux/mutex.h | 3 +++ kernel/locking/mutex.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+)include/linux/mutex.h +3
-0@@ -119,6 +119,9 @@ do { \extern void __mutex_init (struct mutex *lock,const char *name,struct lock_class_key *key); +int devm_mutex_init (struct device *dev,struct mutex *lock); +void devm_mutex_destroy (struct device *dev,struct mutex *lock); +/** * mutex_is_locked - is the mutex locked * @lock: the mutex to be queried kernel/locking/mutex.c +30
-0@@ -56,6 +56,43 @@ __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) }EXPORT_SYMBOL (__mutex_init); +static void devm_mutex_release (struct device *dev,void *res) +{ +mutex_destroy (*(struct mutex **)res); +} + +static int devm_mutex_match (struct device *dev,void *res,void *data) +{ +struct mutex **r = res; + +if (WARN_ON (!r || !*r)) +return 0 ; + +return *r == data; +} + +int devm_mutex_init (struct device *dev,struct mutex *lock) +{ +struct mutex **ptr; + + ptr =devres_alloc (devm_mutex_release,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return -ENOMEM ; + +mutex_init (lock); + + *ptr = lock; +devres_add (dev, ptr); +return 0 ; +} +EXPORT_SYMBOL (devm_mutex_init);
kernel/locking/mutex.c __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) · line 98
Quoted 10 lines
kernel/locking/mutex.c +7
-0+ +void devm_mutex_destroy (struct device *dev,struct mutex *lock) +{ +devres_release (dev, devm_mutex_release, devm_mutex_match, lock); +} +EXPORT_SYMBOL (devm_mutex_destroy); +/* * @owner: contains: 'struct task_struct *' to the current lock owner, * NULL means not owned. Since task_struct pointers are aligned at
These APIs are specific to devres. I don't believe it is suitable to put them into the generic mutex.h header file. All devres_* functions are defined in include/linux/device.h which is probabably not included in mutex.h. You may consider putting these APIs into device.h instead.
Cheers,
Longman
Quoted 1 line · Waiman Long
On 11/27/23 19:09, George Stark wrote:
...
Quoted 4 lines
These APIs are specific to devres. I don't believe it is suitable to put them into the generic
mutex.hheader file. All devres_* functions are defined ininclude/linux/device.hwhich is probabably not included inmutex.h. You may consider putting these APIs intodevice.hinstead.
+1. We have include/linux/devm-helpers.h.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy, Waiman
Thanks for the review.
I'll move the patch to the devm-helpers.h then.
Quoted 12 lines · Andy Shevchenko
Quoted 1 line · Waiman Long
On 11/27/23 19:09, George Stark wrote:
...
Quoted 4 lines
These APIs are specific to devres. I don't believe it is suitable to put them into the generic
mutex.hheader file. All devres_* functions are defined ininclude/linux/device.hwhich is probabably not included inmutex.h. You may consider putting these APIs intodevice.hinstead.+1. We have
include/linux/devm-helpers.h.
Signature · 2 lines
Best regards George
Peter Zijlstra
maintainerQuoted 9 lines · George Stark
Using of devm API leads to 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 callingmutex_destroy()is just ignored which is safe for now but wrong formally and can lead to a problem ifmutex_destroy()will be extended so introducedevm_mutex_init()anddevm_mutex_destroy().
Aside of the issue raised by Waiman, it is also very undesirable to introduce EXPORT'ed symbols without a user. Please group this with a patch that actually makes use of it.
No in-tree users, no export.