public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy
@ 2023-11-28  0:09 George Stark
  2023-11-28  1:55 ` Waiman Long
  2023-11-28 16:45 ` Peter Zijlstra
  0 siblings, 2 replies; 5+ messages in thread
From: George Stark @ 2023-11-28  0:09 UTC (permalink / raw)
  To: peterz, mingo, will, longman, boqun.feng, andriy.shevchenko,
	jic23, gnstark
  Cc: linux-kernel, kernel

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().

Signed-off-by: George Stark <gnstark@salutedevices.com>
---
 include/linux/mutex.h  |  3 +++
 kernel/locking/mutex.c | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index a33aa9eb9fc3..7f60cd842322 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -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
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index d973fe6041bf..a73124719dcb 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -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
-- 
2.38.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy
  2023-11-28  0:09 [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy George Stark
@ 2023-11-28  1:55 ` Waiman Long
  2023-11-28 16:35   ` Andy Shevchenko
  2023-11-28 16:45 ` Peter Zijlstra
  1 sibling, 1 reply; 5+ messages in thread
From: Waiman Long @ 2023-11-28  1:55 UTC (permalink / raw)
  To: George Stark, peterz, mingo, will, boqun.feng, andriy.shevchenko,
	jic23
  Cc: linux-kernel, kernel

On 11/27/23 19:09, George Stark wrote:
> 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().
>
> Signed-off-by: George Stark <gnstark@salutedevices.com>
> ---
>   include/linux/mutex.h  |  3 +++
>   kernel/locking/mutex.c | 37 +++++++++++++++++++++++++++++++++++++
>   2 files changed, 40 insertions(+)
>
> diff --git a/include/linux/mutex.h b/include/linux/mutex.h
> index a33aa9eb9fc3..7f60cd842322 100644
> --- a/include/linux/mutex.h
> +++ b/include/linux/mutex.h
> @@ -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
> diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
> index d973fe6041bf..a73124719dcb 100644
> --- a/kernel/locking/mutex.c
> +++ b/kernel/locking/mutex.c
> @@ -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

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy
  2023-11-28  1:55 ` Waiman Long
@ 2023-11-28 16:35   ` Andy Shevchenko
  2023-12-01 13:43     ` George Stark
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2023-11-28 16:35 UTC (permalink / raw)
  To: Waiman Long
  Cc: George Stark, peterz, mingo, will, boqun.feng, jic23,
	linux-kernel, kernel

On Mon, Nov 27, 2023 at 08:55:18PM -0500, Waiman Long wrote:
> On 11/27/23 19:09, George Stark wrote:

...

> 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.

+1. We have include/linux/devm-helpers.h.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy
  2023-11-28  0:09 [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy George Stark
  2023-11-28  1:55 ` Waiman Long
@ 2023-11-28 16:45 ` Peter Zijlstra
  1 sibling, 0 replies; 5+ messages in thread
From: Peter Zijlstra @ 2023-11-28 16:45 UTC (permalink / raw)
  To: George Stark
  Cc: mingo, will, longman, boqun.feng, andriy.shevchenko, jic23,
	linux-kernel, kernel

On Tue, Nov 28, 2023 at 03:09:10AM +0300, George Stark wrote:
> 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().
> 

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy
  2023-11-28 16:35   ` Andy Shevchenko
@ 2023-12-01 13:43     ` George Stark
  0 siblings, 0 replies; 5+ messages in thread
From: George Stark @ 2023-12-01 13:43 UTC (permalink / raw)
  To: Andy Shevchenko, Waiman Long
  Cc: peterz, mingo, will, boqun.feng, jic23, linux-kernel, kernel

Andy, Waiman

Thanks for the review.

I'll move the patch to the devm-helpers.h then.


On 11/28/23 19:35, Andy Shevchenko wrote:
> On Mon, Nov 27, 2023 at 08:55:18PM -0500, Waiman Long wrote:
>> On 11/27/23 19:09, George Stark wrote:
> 
> ...
> 
>> 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.
> 
> +1. We have include/linux/devm-helpers.h.
> 

-- 
Best regards
George

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-01 13:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-28  0:09 [RFC PATCH 1/1] locking: introduce devm_mutex_init and devm_mutex_destroy George Stark
2023-11-28  1:55 ` Waiman Long
2023-11-28 16:35   ` Andy Shevchenko
2023-12-01 13:43     ` George Stark
2023-11-28 16:45 ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox