Skip to contentSkip to conversation

devm_led_classdev_register() usage problem

PATCH Superseded George Stark 6 participants 36 messages Dec 4, 2023 – Dec 7, 2023

Subsystems (3)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover devm_led_classdev_register() usage problem

This patch series fixes the problem of devm_led_classdev_register misusing.

The basic problem is described in [1]. Shortly when devm_led_classdev_register() is used then led_classdev_unregister() called after driver's remove() callback. led_classdev_unregister() calls driver's brightness_set callback and that callback may use resources which were destroyed already in driver's remove().

After discussion with maintainers [2] [3] we decided:
1) don't touch led subsytem core code and don't remove led_set_brightness() from it but fix drivers
2) don't use devm_led_classdev_unregister

So the solution is to use devm wrappers for all resources driver's brightness_set() depends on. And introduce dedicated devm wrapper for mutex as it's often used resource.

[1] https://lore.kernel.org/lkml/[email protected]/T/
[2] https://lore.kernel.org/lkml/[email protected]/T/#mc132b9b350fa51931b4fcfe14705d9f06e91421f
[3] https://lore.kernel.org/lkml/[email protected]/T/#mdbf572a85c33f869a553caf986b6228bb65c8383

George Stark (10):
devm-helpers: introduce devm_mutex_init
leds: aw2013: unlock mutex before destroying it
leds: aw2013: use devm API to cleanup module's resources
leds: aw200xx: use devm API to cleanup module's resources
leds: lp3952: use devm API to cleanup module's resources
leds: lm3532: use devm API to cleanup module's resources
leds: nic78bx: use devm API to cleanup module's resources
leds: mlxreg: use devm_mutex_init for mutex initializtion
leds: an30259a: use devm_mutext_init for mutext initialization
leds: powernv: add LED_RETAIN_AT_SHUTDOWN flag for leds

 drivers/leds/leds-an30259a.c | 14 ++++----------
 drivers/leds/leds-aw200xx.c  | 36 +++++++++++++++++++++++++-----------
 drivers/leds/leds-aw2013.c   | 28 ++++++++++++++++------------
 drivers/leds/leds-lm3532.c   | 29 +++++++++++++++++------------
 drivers/leds/leds-lp3952.c   | 21 +++++++++++----------
 drivers/leds/leds-mlxreg.c   | 15 ++++-----------
 drivers/leds/leds-nic78bx.c  | 25 +++++++++++++------------
 drivers/leds/leds-powernv.c  | 23 ++++++++---------------
 include/linux/devm-helpers.h | 18 ++++++++++++++++++
 9 files changed, 116 insertions(+), 93 deletions(-)

--
2.38.4

#1 lore raw

PATCH v2 1/10 devm-helpers: introduce devm_mutex_init

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() is extended so introduce devm_mutex_init().

---
 include/linux/devm-helpers.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +18 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct device *dev,
 	return devm_add_action(dev, devm_work_drop, w);
 }
 
+static inline void devm_mutex_release(void *res)
+{
+	mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:	Device which lifetime work is bound to
+ * @lock:	Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
 #endif
Signature · 1 line
2.38.4
#2 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Quoted 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

...

Do you need to include mutex.h?

...

Quoted 6 lines

+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev: Device which lifetime work is bound to
+ * @lock: Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.

the driver

Have you run scripts/kernel-doc -v -Wall -none ... against this file? I'm pretty sure it will complain.

Quoted 1 line

+ */

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Hello Andy

Thanks for the review.

Quoted 11 lines · Andy Shevchenko
Quoted 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

Quoted 3 lines

...

Do you need to include mutex.h?

It's already included in linux/device.h which is included in devm-helpers. Should I include mutex.h explicitly?

Quoted 10 lines

...

Quoted 6 lines

+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev: Device which lifetime work is bound to
+ * @lock: Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.

Quoted 4 lines

the driver

Have you run scripts/kernel-doc -v -Wall -none ... against this file? I'm pretty sure it will complain.

It does with warning "No description found for return value". Fixed

Quoted 4 lines
Quoted 2 lines

+ */

Signature · 2 lines
Best regards
George
#4 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Hi,

Quoted 16 lines · George Stark

Hello Andy

Thanks for the review.

Quoted 11 lines · Andy Shevchenko
Quoted 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

Quoted 4 lines
Quoted 3 lines

...

Do you need to include mutex.h?

It's already included in linux/device.h which is included in devm-helpers. Should I include mutex.h explicitly?

Yes you must explicitly include all headers you use definitions from. Relying on other headers to do this for you is error prone.

Regards,

Hans

#5 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

In reply to George Stark

Le 04/12/2023 à 19:05, George Stark a écrit :

Quoted 8 lines

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() is extended so introduce devm_mutex_init().

This is not needed for patch 2. Should patch 2 go first ?

Quoted 33 lines
---
  include/linux/devm-helpers.h | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +18 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct device *dev,
  	return devm_add_action(dev, devm_work_drop, w);
  }
 
+static inline void devm_mutex_release(void *res)
+{
+	mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:	Device which lifetime work is bound to
+ * @lock:	Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
  #endif
#6 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

In reply to George Stark
Quoted 20 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
  include/linux/devm-helpers.h | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +0 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct device *dev,
  	return devm_add_action(dev, devm_work_drop, w);

include/linux/devm-helpers.h static inline int devm_work_autocancel(struct device *dev, · line 86

Quoted 10 lines
include/linux/devm-helpers.h +8 -0
  }
 
+static inline void devm_mutex_release(void *res)
+{
+	mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:	Device which lifetime work is bound to

Work? Copy-paste error?

include/linux/devm-helpers.h static inline int devm_work_autocancel(struct device *dev, · line 97

Quoted 11 lines
include/linux/devm-helpers.h +10 -0
+ * @lock:	Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
  #endif

Doesn't the mutex stuff need a header inclusion?

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#7 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

In reply to George Stark

Hi George,

Quoted 31 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
 include/linux/devm-helpers.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +9 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct device *dev,
 	return devm_add_action(dev, devm_work_drop, w);
 }
 
+static inline void devm_mutex_release(void *res)
+{
+	mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:	Device which lifetime work is bound to
+ * @lock:	Pointer to a mutex

include/linux/devm-helpers.h static inline int devm_work_autocancel(struct device *dev, · line 97

Quoted 10 lines
include/linux/devm-helpers.h +9 -0
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
 #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

{
	mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
#else
	return 0;

#endif
}

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Regards,

Hans

#8 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Hello Hans

Thanks for the review.

Quoted 59 lines · Hans de Goede

Hi George,

Quoted 31 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
  include/linux/devm-helpers.h | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +9 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct device *dev,
  	return devm_add_action(dev, devm_work_drop, w);
  }
 
+static inline void devm_mutex_release(void *res)
+{
+	mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:	Device which lifetime work is bound to
+ * @lock:	Pointer to a mutex

include/linux/devm-helpers.h static inline int devm_work_autocancel(struct device *dev, · line 97

Quoted 10 lines
include/linux/devm-helpers.h +9 -0
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
  #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

{
	mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
#else
	return 0;
Quoted 5 lines

#endif
}

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

I tried to put devm_mutex_init itself in mutex.h and it could've helped too but it's not the place for devm API.

Quoted 8 lines

Regards,

Hans

Signature · 2 lines
Best regards
George
#9 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Hi,

Quoted 70 lines · George Stark

Hello Hans

Thanks for the review.

Quoted 61 lines · Hans de Goede

Hi George,

Quoted 30 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
  include/linux/devm-helpers.h | 18 ++++++++++++++++++

  1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +8 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct device *dev,
       return devm_add_action(dev, devm_work_drop, w);
   }
   +static inline void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime work is bound to
+ * @lock:    Pointer to a mutex

include/linux/devm-helpers.h static inline int devm_work_autocancel(struct device *dev, · line 96

Quoted 10 lines
include/linux/devm-helpers.h +9 -0
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
   #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock) {
    mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
    return devm_add_action_or_reset(dev, devm_mutex_release, lock); #else
    return 0;
#endif
}

Quoted 2 lines

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Quoted 7 lines

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

That (a IS_MUTEX_DESTROY_REQUIRED define) is an interesting idea. Lets see for v3 if the mutex maintainers will accept that and if not then I guess we will just need to live with the unnecessary devres allocation.

Regards,

Hans

reviewer
#10 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Quoted 70 lines · Hans de Goede

Hi,

Quoted 67 lines · George Stark

Hello Hans

Thanks for the review.

Quoted 62 lines · Hans de Goede

Hi George,

Quoted 30 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
   include/linux/devm-helpers.h | 18 ++++++++++++++++++
   1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +8 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct device *dev,
       return devm_add_action(dev, devm_work_drop, w);
   }
   +static inline void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime work is bound to
+ * @lock:    Pointer to a mutex

include/linux/devm-helpers.h static inline int devm_work_autocancel(struct device *dev, · line 96

Quoted 10 lines
include/linux/devm-helpers.h +9 -0
+ *
+ * Initialize mutex which is automatically destroyed when driver is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
   #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock) {
    mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
#else
    return 0;
#endif
}

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Quoted 8 lines
Quoted 7 lines

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

That (a IS_MUTEX_DESTROY_REQUIRED define) is an interesting idea. Lets see for v3 if the mutex maintainers will accept that and if not then I guess we will just need to live with the unnecessary devres allocation.

The purpose of calling mutex_destroy() is to mark a mutex as being destroyed so that any subsequent call to mutex_lock/unlock will cause a warning to be printed when CONFIG_DEBUG_MUTEXES is defined. I would not say that mutex_destroy() is required. Rather it is a nice to have for catching programming error.

Cheers,
Longman

#11 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Hello Waiman

Thanks for the review.

Quoted 11 lines · Waiman Long
Quoted 10 lines · Hans de Goede

Hi,

Quoted 7 lines · George Stark

Hello Hans

Thanks for the review.

Quoted 2 lines · Hans de Goede

Hi George,

...

Quoted 41 lines
Quoted 41 lines
Quoted 19 lines
Quoted 17 lines

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock) {
     mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
     return devm_add_action_or_reset(dev, devm_mutex_release, lock); #else
     return 0;
#endif
}

Quoted 2 lines

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Quoted 7 lines

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

That (a  IS_MUTEX_DESTROY_REQUIRED define) is an interesting idea. Lets s>

Quoted 14 lines
Quoted 14 lines

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

{
     mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
     return devm_add_action_or_reset(dev, devm_mutex_release, lock);
#else
     return 0;

#endif
}
ee for v3 if the mutex maintainers will accept that and if not

Quoted 3 lines
Quoted 1 line

then I guess we will just need to live with the unnecessary devres allocation.

The purpose of calling mutex_destroy() is to mark a mutex as being destroyed so that any subsequent call to mutex_lock/unlock will cause a warning to be printed when CONFIG_DEBUG_MUTEXES is defined. I would not say that mutex_destroy() is required. Rather it is a nice to have for catching programming error.

This is quite understandable but probably mutex_destroy() is not the best name for an optional API. Questions are asked over and over again if it can be safely ignored taking into account that it could be extended in the future. Every maintainer makes decision on that question in his own way and it leads to inconsistency.

devm_mutex_init could take responsibility for calling/dropping mutex_destroy() on its own.

Quoted 3 lines

Cheers,
Longman

Signature · 2 lines
Best regards
George
reviewer
#12 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Quoted 60 lines · George Stark

Hello Waiman

Thanks for the review.

Quoted 11 lines · Waiman Long
Quoted 10 lines · Hans de Goede

Hi,

Quoted 7 lines · George Stark

Hello Hans

Thanks for the review.

Quoted 2 lines · Hans de Goede

Hi George,

...

Quoted 43 lines
Quoted 42 lines
Quoted 19 lines
Quoted 17 lines

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock) {
     mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
     return devm_add_action_or_reset(dev, devm_mutex_release, lock); #else
     return 0;
#endif
}

Quoted 2 lines

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Quoted 7 lines

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

That (a  IS_MUTEX_DESTROY_REQUIRED define) is an interesting idea. Lets s>

Quoted 4 lines
Quoted 4 lines

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

Quoted 10 lines
Quoted 10 lines

static inline int devm_mutex_init(struct device *dev, struct mutex *lock) {
     mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
     return devm_add_action_or_reset(dev, devm_mutex_release, lock); #else
     return 0;
#endif
}
ee for v3 if the mutex maintainers will accept that and if not

then I guess we will just need to live with the unnecessary devres allocation.

Quoted 7 lines
Quoted 1 line

The purpose of calling mutex_destroy() is to mark a mutex as being destroyed so that any subsequent call to mutex_lock/unlock will cause a warning to be printed when CONFIG_DEBUG_MUTEXES is defined. I would not say that mutex_destroy() is required. Rather it is a nice to have for catching programming error.

This is quite understandable but probably mutex_destroy() is not the best name for an optional API. Questions are asked over and over again if it can be safely ignored taking into account that it could be extended in the future. Every maintainer makes decision on that question in his own way and it leads to inconsistency.

devm_mutex_init could take responsibility for calling/dropping mutex_destroy() on its own.

The DEBUG_MUTEXES code is relatively old and there was no major change to it for a number of years. I don't see we will make major change to it in the near future. Of course, thing may change if there are new requirement that may affect the DEBUG_MUTEXES code.

Cheers,
Longman

Quoted 5 lines
Quoted 4 lines

Cheers,
Longman

reviewer
#13 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

In reply to Waiman Long
Quoted 76 lines · Waiman Long
Quoted 75 lines · Hans de Goede

Hi,

Quoted 72 lines · George Stark

Hello Hans

Thanks for the review.

Quoted 63 lines · Hans de Goede

Hi George,

Quoted 42 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
   include/linux/devm-helpers.h | 18 ++++++++++++++++++

   1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +10 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct 
 device *dev,
        return devm_add_action(dev, devm_work_drop, w);
    }
    +static inline void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime work is bound to
+ * @lock:    Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver 

is detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

include/linux/devm-helpers.h +5 -0
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
Quoted 1 line

   #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock) {
     mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
     return devm_add_action_or_reset(dev, devm_mutex_release, lock); #else
     return 0;
#endif
}

Quoted 2 lines

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

That (a  IS_MUTEX_DESTROY_REQUIRED define) is an interesting idea. Lets see for v3 if the mutex maintainers will accept that and if not then I guess we will just need to live with the unnecessary devres allocation.

The purpose of calling mutex_destroy() is to mark a mutex as being destroyed so that any subsequent call to mutex_lock/unlock will cause a warning to be printed when CONFIG_DEBUG_MUTEXES is defined. I would not say that mutex_destroy() is required. Rather it is a nice to have for catching programming error.

OTOH, one thing that we can probably do in mutex.h is something like

include/linux/mutex.h +0 -0
index a33aa9eb9fc3..7db7862de3f1 100644
@@ -83,6 +83,9 @@ struct mutex {
 

 extern void mutex_destroy(struct mutex *lock);

+/* mutex_destroy() is a real function, not a NOP */ +#define mutex_destroy  mutex_destroy
+
 #else

----------------------------------------

Now in some devm files, you can use the absense/presence of mutex_destroy macro to decide on what to do.

Cheers,
Longman

#14 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

In reply to George Stark

Le 06/12/2023 à 19:58, George Stark a écrit :

Quoted 88 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à
https://aka.ms/LearnAboutSenderIdentification ]

Hello Hans

Thanks for the review.

Quoted 65 lines · Hans de Goede

Hi George,

Quoted 43 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 calling mutex_destroy() is just ignored which is safe for now but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
  include/linux/devm-helpers.h | 18 ++++++++++++++++++

  1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +11 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct 
 device *dev,
      return devm_add_action(dev, devm_work_drop, w);
   }
 
+static inline void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime work is bound to
+ * @lock:   Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is 

detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

include/linux/devm-helpers.h +5 -0
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
Quoted 1 line

  #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock) {
      mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
      return devm_add_action_or_reset(dev, devm_mutex_release, lock); #else
      return 0;
#endif
}

Quoted 2 lines

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

Quoted 8 lines

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

I tried to put devm_mutex_init itself in mutex.h and it could've helped too but it's not the place for devm API.

What do you mean by "it's not the place for devm API" ?

If you do a 'grep devm_ include/linux/' you'll find devm_ functions in almost 100 .h files. Why wouldn't mutex.h be the place for devm_mutex_init() ?

Christophe

#15 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Le 06/12/2023 à 23:14, Christophe Leroy a écrit :

Quoted 103 lines

Le 06/12/2023 à 19:58, George Stark a écrit :

Quoted 91 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à
https://aka.ms/LearnAboutSenderIdentification ]

Hello Hans

Thanks for the review.

Quoted 68 lines · Hans de Goede

Hi George,

Quoted 45 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 calling mutex_destroy() is just ignored which is safe for now
but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
  include/linux/devm-helpers.h | 18 ++++++++++++++++++

  1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +11 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct 
 device *dev,
      return devm_add_action(dev, devm_work_drop, w);
   }
 
+static inline void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime work is bound to
+ * @lock:   Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is 

detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

include/linux/devm-helpers.h +5 -0
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
Quoted 1 line

  #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
{
      mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
      return devm_add_action_or_reset(dev, devm_mutex_release, lock); #else
      return 0;
#endif
}

Quoted 2 lines

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

Quoted 8 lines

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

I tried to put devm_mutex_init itself in mutex.h and it could've helped too but it's not the place for devm API.

Quoted 5 lines

What do you mean by "it's not the place for devm API" ?

If you do a 'grep devm_ include/linux/' you'll find devm_ functions in almost 100 .h files. Why wouldn't mutex.h be the place for devm_mutex_init() ?

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Wouldn't that work ?

Christophe

#16 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Hello Christophe

Quoted 111 lines · Christophe Leroy

Le 06/12/2023 à 23:14, Christophe Leroy a écrit :

Quoted 108 lines

Le 06/12/2023 à 19:58, George Stark a écrit :

Quoted 91 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à
https://aka.ms/LearnAboutSenderIdentification ]

Hello Hans

Thanks for the review.

Quoted 68 lines · Hans de Goede

Hi George,

Quoted 40 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 calling mutex_destroy() is just ignored which is safe for now
but wrong formally and can lead to a problem if mutex_destroy() is extended so introduce devm_mutex_init().

---
   include/linux/devm-helpers.h | 18 ++++++++++++++++++
   1 file changed, 18 insertions(+)
include/linux/devm-helpers.h +11 -0
index 74891802200d..2f56e476776f 100644
@@ -76,4 +76,22 @@ static inline int devm_work_autocancel(struct
 device *dev,
      return devm_add_action(dev, devm_work_drop, w);
   }
 
+static inline void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime work is bound to
+ * @lock:   Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when driver is

detached.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

include/linux/devm-helpers.h static inline int devm_work_autocancel(struct device *dev, · line 97

Quoted 6 lines
include/linux/devm-helpers.h +5 -0
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
   #endif

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
{
      mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
      return devm_add_action_or_reset(dev, devm_mutex_release, lock);
#else
      return 0;
#endif
}

Quoted 2 lines

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using  indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

Quoted 8 lines

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

I tried to put devm_mutex_init itself in mutex.h and it could've helped too but it's not the place for devm API.

What do you mean by "it's not the place for devm API" ?

If you do a 'grep devm_ include/linux/' you'll find devm_ functions in almost 100 .h files. Why wouldn't mutex.h be the place for devm_mutex_init() ?

mutex.h's maintainers believe so.

https://lore.kernel.org/lkml/[email protected]/T/#mb42e1d7760816b0cedd3130e08f29690496b5ac2

Quoted 7 lines

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h
we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

Quoted 4 lines

Wouldn't that work ?

Christophe

Signature · 2 lines
Best regards
George
#17 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Quoted 5 lines · George Stark
Quoted 4 lines · Christophe Leroy
Quoted 3 lines · Christophe Leroy
Quoted 2 lines · George Stark
Quoted 1 line · Hans de Goede

On 12/4/23 19:05, George Stark wrote:

...

Quoted 53 lines
Quoted 41 lines
Quoted 41 lines
Quoted 40 lines
Quoted 15 lines

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

{
       mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
       return devm_add_action_or_reset(dev, devm_mutex_release, lock);
#else
       return 0;
Quoted 5 lines

#endif
}

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

I tried to put devm_mutex_init itself in mutex.h and it could've helped too but it's not the place for devm API.

Quoted 5 lines
Quoted 5 lines

What do you mean by "it's not the place for devm API" ?

If you do a 'grep devm_ include/linux/' you'll find devm_ functions in almost 100 .h files. Why wouldn't mutex.h be the place for devm_mutex_init() ?

mutex.h's maintainers believe so.

https://lore.kernel.org/lkml/[email protected]/T/#mb42e1d7760816b0cedd3130e08f29690496b5ac2

Quoted 4 lines

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

Quoted 9 lines
Quoted 3 lines

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

Quoted 1 line

Wouldn't that work ?

No. It will require inclusion of device.h (which is a twisted hell from the header perspective) into mutex.h. Completely unappreciated move.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Le 07/12/2023 à 12:59, Andy Shevchenko a écrit :

Quoted 70 lines · George Stark
Quoted 5 lines · Christophe Leroy
Quoted 4 lines · Christophe Leroy
Quoted 3 lines · George Stark
Quoted 2 lines · Hans de Goede
Quoted 1 line · George Stark

...

Quoted 61 lines
Quoted 41 lines
Quoted 41 lines
Quoted 40 lines
Quoted 15 lines

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

{
        mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
        return devm_add_action_or_reset(dev, devm_mutex_release, lock);
#else
        return 0;
Quoted 5 lines

#endif
}

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

I tried to put devm_mutex_init itself in mutex.h and it could've helped too but it's not the place for devm API.

Quoted 5 lines
Quoted 5 lines

What do you mean by "it's not the place for devm API" ?

If you do a 'grep devm_ include/linux/' you'll find devm_ functions in almost 100 .h files. Why wouldn't mutex.h be the place for devm_mutex_init() ?

mutex.h's maintainers believe so.

https://lore.kernel.org/lkml/[email protected]/T/#mb42e1d7760816b0cedd3130e08f29690496b5ac2

Quoted 7 lines

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Wouldn't that work ?

No. It will require inclusion of device.h (which is a twisted hell from the header perspective) into mutex.h. Completely unappreciated move.

I see no reason for including device.h, I think a forward declaration of struct device would be enough, as done in linux/gpio.h

Am I missing something ?

#19 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Quoted 7 lines · Christophe Leroy
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · George Stark
Quoted 4 lines · Christophe Leroy
Quoted 3 lines · Christophe Leroy
Quoted 2 lines · George Stark
Quoted 1 line · Hans de Goede

On 12/4/23 19:05, George Stark wrote:

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

mutex_destroy() only actually does anything if CONFIG_DEBUG_MUTEXES is set, otherwise it is an empty inline-stub.

Adding a devres resource to the device just to call an empty inline stub which is a no-op seems like a waste of resources. IMHO it would be better to change this to:

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

static inline int devm_mutex_init(struct device *dev, struct mutex *lock)

{
        mutex_init(lock);
#ifdef CONFIG_DEBUG_MUTEXES
        return devm_add_action_or_reset(dev, devm_mutex_release, lock);

^^^^ (1)

Quoted 50 lines
Quoted 50 lines
Quoted 49 lines
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 7 lines
#else
        return 0;

#endif
}

To avoid the unnecessary devres allocation when
CONFIG_DEBUG_MUTEXES is not set.

Honestly saying I don't like unnecessary devres allocation either but the proposed approach has its own price:

1) we'll have more than one place with branching if mutex_destroy is empty or not using indirect condition. If suddenly mutex_destroy is extended for non-debug code (in upstream branch or e.g. by someone for local debug) than there'll be a problem.

2) If mutex_destroy is empty or not depends on CONFIG_PREEMPT_RT option too. When CONFIG_PREEMPT_RT is on mutex_destroy is always empty.

As I see it only the mutex interface (mutex.h) has to say definitely if mutex_destroy must be called. Probably we could add some define to include/linux/mutex.h,like IS_MUTEX_DESTROY_REQUIRED and declare it near mutex_destroy definition itself.

I tried to put devm_mutex_init itself in mutex.h and it could've helped too but it's not the place for devm API.

Quoted 5 lines
Quoted 5 lines

What do you mean by "it's not the place for devm API" ?

If you do a 'grep devm_ include/linux/' you'll find devm_ functions in almost 100 .h files. Why wouldn't mutex.h be the place for devm_mutex_init() ?

mutex.h's maintainers believe so.

https://lore.kernel.org/lkml/[email protected]/T/#mb42e1d7760816b0cedd3130e08f29690496b5ac2

Quoted 7 lines

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

Quoted 1 line

Wouldn't that work ?

Quoted 8 lines
Quoted 3 lines

No. It will require inclusion of device.h (which is a twisted hell from the header perspective) into mutex.h. Completely unappreciated move.

I see no reason for including device.h, I think a forward declaration of struct device would be enough, as done in linux/gpio.h

Am I missing something ?

Yes, see (1) above. If you want to have it in the header, you must provide an API, which is located in device.h. The idea about mutex-debug.c is interesting, but the file naming and the devm_*() API for _initing_ the mutex seems confusing.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

In reply to George Stark
Quoted 2 lines · George Stark
Quoted 1 line · Christophe Leroy

Le 06/12/2023 à 23:14, Christophe Leroy a écrit :

...

Quoted 10 lines
Quoted 6 lines

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

In case you place it into a C-file. Otherwise you need a header for the API and that is not acceptable for mutex.h.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#21 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Le 07/12/2023 à 13:02, Andy Shevchenko a écrit :

Quoted 13 lines · George Stark
Quoted 2 lines · Christophe Leroy
Quoted 1 line · Christophe Leroy

...

Quoted 7 lines
Quoted 6 lines

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Quoted 7 lines
Quoted 3 lines

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

In case you place it into a C-file. Otherwise you need a header for the API and that is not acceptable for mutex.h.

Right, that's the reason why I'm suggesting to define devm_mutex_init() in kernel/locking/mutex-debug.c.

In linux/mutex.h, you define a stub for when CONFIG_DEBUG_MUTEXES is not set, and the prototype of devm_mutex_init() when CONFIG_DEBUG_MUTEXES is set.

#22 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Quoted 24 lines · Christophe Leroy

Le 07/12/2023 à 13:02, Andy Shevchenko a écrit :

Quoted 13 lines · George Stark
Quoted 2 lines · Christophe Leroy
Quoted 1 line · Christophe Leroy

...

Quoted 7 lines
Quoted 6 lines

Looking at it closer, I have the feeling that you want to do similar to devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Quoted 7 lines
Quoted 3 lines

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

In case you place it into a C-file. Otherwise you need a header for the API and that is not acceptable for mutex.h.

Quoted 6 lines

Right, that's the reason why I'm suggesting to define devm_mutex_init() in kernel/locking/mutex-debug.c.

In linux/mutex.h, you define a stub for when CONFIG_DEBUG_MUTEXES is not set, and the prototype of devm_mutex_init() when CONFIG_DEBUG_MUTEXES is set.

Something like this:

include/linux/mutex.h +23 -0
index a33aa9eb9fc3..4a6041a7fd44 100644
@@ -21,6 +21,8 @@
 #include <linux/debug_locks.h>
 #include <linux/cleanup.h>
 
+struct device;
+
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)			\
 		, .dep_map = {					\
@@ -127,6 +129,20 @@ extern void __mutex_init(struct mutex *lock, const char *name,
  */
 extern bool mutex_is_locked(struct mutex *lock);
 
+#ifdef CONFIG_DEBUG_MUTEXES
+
+extern int devm_mutex_init(struct device *dev, struct mutex *lock);
+
+#else
+
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return 0;
+}
+
+#endif
+
 #else /* !CONFIG_PREEMPT_RT */
 /*
  * Preempt-RT variant based on rtmutexes.
@@ -169,6 +185,13 @@ do {							\
 							\
 	__mutex_init((mutex), #mutex, &__key);		\
 } while (0)
+
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return 0;
+}
+
 #endif /* CONFIG_PREEMPT_RT */
 
 /*
kernel/locking/mutex-debug.c +23 -0
index bc8abb8549d2..d50dfa06e82c 100644
@@ -19,6 +19,7 @@
 #include <linux/kallsyms.h>
 #include <linux/interrupt.h>
 #include <linux/debug_locks.h>
+#include <linux/device.h>
 
 #include "mutex.h"
 
@@ -104,3 +105,25 @@ void mutex_destroy(struct mutex *lock)
 }
 
 EXPORT_SYMBOL_GPL(mutex_destroy);
+
+static void devm_mutex_release(void *res)
+{
+	mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:	Device which lifetime mutex is bound to
+ * @lock:	Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when the driver is detached.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+	return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
+EXPORT_SYMBOL_GPL(devm_mutex_init);
\ No newline at end of file
Signature · 2 lines
Best regards
George
#23 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Le 07/12/2023 à 13:51, George Stark a écrit :

Quoted 52 lines · Christophe Leroy
Quoted 26 lines

Le 07/12/2023 à 13:02, Andy Shevchenko a écrit :

Quoted 15 lines
Quoted 2 lines · George Stark
Quoted 1 line · Christophe Leroy

Le 06/12/2023 à 23:14, Christophe Leroy a écrit :

...

Quoted 8 lines
Quoted 7 lines

Looking at it closer, I have the feeling that you want to do similar to
devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Quoted 7 lines
Quoted 3 lines

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

In case you place it into a C-file. Otherwise you need a header for the API and that is not acceptable for mutex.h.

Quoted 6 lines

Right, that's the reason why I'm suggesting to define devm_mutex_init() in kernel/locking/mutex-debug.c.

In linux/mutex.h, you define a stub for when CONFIG_DEBUG_MUTEXES is not set, and the prototype of devm_mutex_init() when CONFIG_DEBUG_MUTEXES is set.

Something like this:

include/linux/mutex.h +2 -0
index a33aa9eb9fc3..4a6041a7fd44 100644
@@ -21,6 +21,8 @@
  #include <linux/debug_locks.h>
  #include <linux/cleanup.h>
 
+struct device;
+
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  # define __DEP_MAP_MUTEX_INITIALIZER(lockname)            \
          , .dep_map = {                    \

include/linux/mutex.h extern void __mutex_init(struct mutex *lock, const · line 133

Quoted 5 lines
include/linux/mutex.h +1 -0
@@ -127,6 +129,20 @@ extern void __mutex_init(struct mutex *lock, const 
 char *name,
   */
  extern bool mutex_is_locked(struct mutex *lock);
 
+#ifdef CONFIG_DEBUG_MUTEXES

There is already a CONFIG_DEBUG_MUTEXES block, can you re-use it ?

include/linux/mutex.h extern void __mutex_init(struct mutex *lock, const char *name, · line 134

Quoted 2 lines
include/linux/mutex.h +2 -0
+
+extern int devm_mutex_init(struct device *dev, struct mutex *lock);

'extern' is pointless and deprecated for function prototypes. I know the kernel is full of them, but it is not a good reason to add new ones.

Quoted 70 lines
include/linux/mutex.h +11 -0
+
+#else
+
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return 0;
+}
+
+#endif
+
  #else /* !CONFIG_PREEMPT_RT */
  /*

  * Preempt-RT variant based on rtmutexes.

include/linux/mutex.h +7 -0
@@ -169,6 +185,13 @@ do {                            \
                              \
      __mutex_init((mutex), #mutex, &__key);        \
  } while (0)
+
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return 0;
+}
+
  #endif /* CONFIG_PREEMPT_RT */
 
  /*
kernel/locking/mutex-debug.c +13 -0
index bc8abb8549d2..d50dfa06e82c 100644
@@ -19,6 +19,7 @@
  #include <linux/kallsyms.h>
  #include <linux/interrupt.h>
  #include <linux/debug_locks.h>
+#include <linux/device.h>
 
  #include "mutex.h"
 
@@ -104,3 +105,25 @@ void mutex_destroy(struct mutex *lock)
  }
 
  EXPORT_SYMBOL_GPL(mutex_destroy);
+
+static void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime mutex is bound to
+ * @lock:    Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when the driver is 

detached.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */

kernel/locking/mutex-debug.c +7 -0
+int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
+EXPORT_SYMBOL_GPL(devm_mutex_init);
\ No newline at end of file
#24 lore raw

Re: [PATCH v2 01/10] devm-helpers: introduce devm_mutex_init

Quoted 59 lines · Christophe Leroy

Le 07/12/2023 à 13:51, George Stark a écrit :

Quoted 56 lines · Christophe Leroy
Quoted 26 lines

Le 07/12/2023 à 13:02, Andy Shevchenko a écrit :

Quoted 15 lines
Quoted 2 lines · George Stark
Quoted 1 line · Christophe Leroy

Le 06/12/2023 à 23:14, Christophe Leroy a écrit :

...

Quoted 8 lines
Quoted 7 lines

Looking at it closer, I have the feeling that you want to do similar to
devm_gpio_request() in linux/gpio.h :

In linux/mutex.h, add a prototype for devm_mutex_init() when CONFIG_DEBUG_MUTEXES is defined and an empty static inline otherwise. Then define devm_mutex_init() in kernel/locking/mutex-debug.c

Quoted 7 lines
Quoted 3 lines

Yes, this would be almost perfect decision. BTW just as in linux/gpio.h we wouldn't have to include whole "linux/device.h" into mutex.h, only add forward declaration of struct device;

In case you place it into a C-file. Otherwise you need a header for the API and that is not acceptable for mutex.h.

Quoted 6 lines

Right, that's the reason why I'm suggesting to define devm_mutex_init() in kernel/locking/mutex-debug.c.

In linux/mutex.h, you define a stub for when CONFIG_DEBUG_MUTEXES is not set, and the prototype of devm_mutex_init() when CONFIG_DEBUG_MUTEXES is set.

Something like this:

include/linux/mutex.h +2 -0
index a33aa9eb9fc3..4a6041a7fd44 100644
@@ -21,6 +21,8 @@
   #include <linux/debug_locks.h>
   #include <linux/cleanup.h>
 
+struct device;
+
   #ifdef CONFIG_DEBUG_LOCK_ALLOC
   # define __DEP_MAP_MUTEX_INITIALIZER(lockname)            \
           , .dep_map = {                    \
@@ -127,6 +129,20 @@ extern void __mutex_init(struct mutex *lock, const
 char *name,
    */
   extern bool mutex_is_locked(struct mutex *lock);
 
Quoted 3 lines

include/linux/mutex.h extern void __mutex_init(struct mutex *lock, const char *name, · line 132

Quoted 1 line
include/linux/mutex.h +1 -0
+#ifdef CONFIG_DEBUG_MUTEXES

There is already a CONFIG_DEBUG_MUTEXES block, can you re-use it ?

those CONFIG_DEBUG_MUTEXES blockd are declared before mutex_init macro :(

Quoted 7 lines

include/linux/mutex.h extern void __mutex_init(struct mutex *lock, const char *name, · line 134

Quoted 3 lines
include/linux/mutex.h +2 -0
+
+extern int devm_mutex_init(struct device *dev, struct mutex *lock);

'extern' is pointless and deprecated for function prototypes. I know the kernel is full of them, but it is not a good reason to add new ones.

Ok

Sure I will send this patch in the right way and then we could have proper review but firstly I'd like to hear from Andy and mutex.h's maintainers is it acceptable at all?

Quoted 71 lines
Quoted 71 lines
include/linux/mutex.h +11 -0
+
+#else
+
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return 0;
+}
+
+#endif
+
   #else /* !CONFIG_PREEMPT_RT */
   /*

  * Preempt-RT variant based on rtmutexes.

include/linux/mutex.h +7 -0
@@ -169,6 +185,13 @@ do {                            \
                               \
       __mutex_init((mutex), #mutex, &__key);        \
   } while (0)
+
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return 0;
+}
+
   #endif /* CONFIG_PREEMPT_RT */
 
   /*
kernel/locking/mutex-debug.c +13 -0
index bc8abb8549d2..d50dfa06e82c 100644
@@ -19,6 +19,7 @@
   #include <linux/kallsyms.h>
   #include <linux/interrupt.h>
   #include <linux/debug_locks.h>
+#include <linux/device.h>
 
   #include "mutex.h"
 
@@ -104,3 +105,25 @@ void mutex_destroy(struct mutex *lock)
   }
 
   EXPORT_SYMBOL_GPL(mutex_destroy);
+
+static void devm_mutex_release(void *res)
+{
+    mutex_destroy(res);
+}
+
+/**
+ * devm_mutex_init - Resource-managed mutex initialization
+ * @dev:    Device which lifetime mutex is bound to
+ * @lock:    Pointer to a mutex
+ *
+ * Initialize mutex which is automatically destroyed when the driver is

detached.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */

kernel/locking/mutex-debug.c +7 -0
+int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+    mutex_init(lock);
+    return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+}
+
+EXPORT_SYMBOL_GPL(devm_mutex_init);
\ No newline at end of file
Signature · 2 lines
Best regards
George
#25 lore raw

PATCH v2 2/10 leds: aw2013: unlock mutex before destroying it

In reply to George Stark

In the probe() callback in case of error mutex is destroyed being locked which is not allowed so unlock the mute before destroying.

---
 drivers/leds/leds-aw2013.c | 1 +
 1 file changed, 1 insertion(+)
drivers/leds/leds-aw2013.c +1 -0
index 59765640b70f..c2bc0782c0cd 100644
@@ -397,6 +397,7 @@ static int aw2013_probe(struct i2c_client *client)
 	regulator_disable(chip->vcc_regulator);
 
 error:
+	mutex_unlock(&chip->mutex);
 	mutex_destroy(&chip->mutex);
 	return ret;
 }
Signature · 1 line
2.38.4
Reviewed-by on v2 2/10
#26 lore raw

Re: [PATCH v2 02/10] leds: aw2013: unlock mutex before destroying it

Quoted 3 lines · George Stark

In the probe() callback in case of error mutex is destroyed being locked which is not allowed so unlock the mute before destroying.

Sounds to me like this is a real fix, hence the Fixes tag and being first in the series.

You free to add

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#27 lore raw

Re: [PATCH v2 02/10] leds: aw2013: unlock mutex before destroying it

In reply to George Stark

Le 04/12/2023 à 19:05, George Stark a écrit :

Quoted 2 lines

In the probe() callback in case of error mutex is destroyed being locked which is not allowed so unlock the mute before destroying.

Should there be a fixes: tag ? For instance on 59ea3c9faf32 ("leds: add aw2013 driver") ?

Quoted 18 lines
---
  drivers/leds/leds-aw2013.c | 1 +
  1 file changed, 1 insertion(+)
drivers/leds/leds-aw2013.c +1 -0
index 59765640b70f..c2bc0782c0cd 100644
@@ -397,6 +397,7 @@ static int aw2013_probe(struct i2c_client *client)
  	regulator_disable(chip->vcc_regulator);
 
  error:
+	mutex_unlock(&chip->mutex);
  	mutex_destroy(&chip->mutex);
  	return ret;
  }
#28 lore raw

Re: [PATCH v2 02/10] leds: aw2013: unlock mutex before destroying it

Hello Christophe

Thanks for the review

Quoted 8 lines · Christophe Leroy

Le 04/12/2023 à 19:05, George Stark a écrit :

Quoted 2 lines

In the probe() callback in case of error mutex is destroyed being locked which is not allowed so unlock the mute before destroying.

Should there be a fixes: tag ? For instance on 59ea3c9faf32 ("leds: add aw2013 driver") ?

Ack

Quoted 19 lines
Quoted 19 lines
---
   drivers/leds/leds-aw2013.c | 1 +
   1 file changed, 1 insertion(+)
drivers/leds/leds-aw2013.c +1 -0
index 59765640b70f..c2bc0782c0cd 100644
@@ -397,6 +397,7 @@ static int aw2013_probe(struct i2c_client *client)
   	regulator_disable(chip->vcc_regulator);
 
   error:
+	mutex_unlock(&chip->mutex);
   	mutex_destroy(&chip->mutex);
   	return ret;
   }
Signature · 2 lines
Best regards
George
#29 lore raw

PATCH v2 3/10 leds: aw2013: use devm API to cleanup module's resources

In reply to George Stark

In this driver LEDs are registered using devm_led_classdev_register() so they are automatically unregistered after module's remove() is done. led_classdev_unregister() calls module's led_set_brightness() to turn off the LEDs and that callback uses resources which were destroyed already in module's remove() so use devm API instead of remove().

---
 drivers/leds/leds-aw2013.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
drivers/leds/leds-aw2013.c +15 -12
index c2bc0782c0cd..1a8acf303548 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0+
 // Driver for Awinic AW2013 3-channel LED driver
 
+#include <linux/devm-helpers.h>
 #include <linux/i2c.h>
 #include <linux/leds.h>
 #include <linux/module.h>
@@ -318,6 +319,13 @@ static int aw2013_probe_dt(struct aw2013 *chip)
 	return 0;
 }
 
+static void aw2013_chip_disable_action(void *data)
+{
+	struct aw2013 *chip = (struct aw2013 *)data;
+
+	aw2013_chip_disable(chip);
+}
+
 static const struct regmap_config aw2013_regmap_config = {
 	.reg_bits = 8,
 	.val_bits = 8,
@@ -334,7 +342,9 @@ static int aw2013_probe(struct i2c_client *client)
 	if (!chip)
 		return -ENOMEM;
 
-	mutex_init(&chip->mutex);
+	if (devm_mutex_init(&client->dev, &chip->mutex))
+		return -ENOMEM;
+
 	mutex_lock(&chip->mutex);
 
 	chip->client = client;
@@ -378,6 +388,10 @@ static int aw2013_probe(struct i2c_client *client)
 		goto error_reg;
 	}
 
+	ret = devm_add_action(&client->dev, aw2013_chip_disable_action, chip);
+	if (ret)
+		goto error_reg;
+
 	ret = aw2013_probe_dt(chip);
 	if (ret < 0)
 		goto error_reg;
@@ -398,19 +412,9 @@ static int aw2013_probe(struct i2c_client *client)
 
 error:
 	mutex_unlock(&chip->mutex);
-	mutex_destroy(&chip->mutex);
 	return ret;
 }
 
-static void aw2013_remove(struct i2c_client *client)
-{
-	struct aw2013 *chip = i2c_get_clientdata(client);
-
-	aw2013_chip_disable(chip);
-
-	mutex_destroy(&chip->mutex);
-}
-
 static const struct of_device_id aw2013_match_table[] = {
 	{ .compatible = "awinic,aw2013", },
 	{ /* sentinel */ },
@@ -424,7 +428,6 @@ static struct i2c_driver aw2013_driver = {
 		.of_match_table = of_match_ptr(aw2013_match_table),
 	},
 	.probe = aw2013_probe,
-	.remove = aw2013_remove,
 };
 
 module_i2c_driver(aw2013_driver);
Signature · 1 line
2.38.4
#30 lore raw

Re: [PATCH v2 03/10] leds: aw2013: use devm API to cleanup module's resources

Quoted 6 lines · George Stark

In this driver LEDs are registered using devm_led_classdev_register() so they are automatically unregistered after module's remove() is done. led_classdev_unregister() calls module's led_set_brightness() to turn off the LEDs and that callback uses resources which were destroyed already in module's remove() so use devm API instead of remove().

...

drivers/leds/leds-aw2013.c static int aw2013_probe_dt(struct aw2013 *chip) · line 327

Quoted 6 lines
drivers/leds/leds-aw2013.c +6 -0
+static void aw2013_chip_disable_action(void *data)
+{
+       struct aw2013 *chip = (struct aw2013 *)data;
+
+       aw2013_chip_disable(chip);
+}

As with mutex release, this also can be oneliner

static void aw2013_chip_disable_action(void *chip)

{
       aw2013_chip_disable(chip);
}

...

drivers/leds/leds-aw2013.c static int aw2013_probe(struct i2c_client *client) · line 346

Quoted 2 lines
drivers/leds/leds-aw2013.c +2 -0
+       if (devm_mutex_init(&client->dev, &chip->mutex))
+               return -ENOMEM;

Shouldn be

       ret = devm_mutex_init(&client->dev, &chip->mutex);
       if (ret)
           return ret;

?

Quoted 1 line

+ return -ENOMEM;

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#31 lore raw

Re: [PATCH v2 03/10] leds: aw2013: use devm API to cleanup module's resources

In reply to George Stark

Le 04/12/2023 à 19:05, George Stark a écrit :

Quoted 24 lines

In this driver LEDs are registered using devm_led_classdev_register() so they are automatically unregistered after module's remove() is done. led_classdev_unregister() calls module's led_set_brightness() to turn off the LEDs and that callback uses resources which were destroyed already in module's remove() so use devm API instead of remove().

---
  drivers/leds/leds-aw2013.c | 27 +++++++++++++++------------
  1 file changed, 15 insertions(+), 12 deletions(-)
drivers/leds/leds-aw2013.c +1 -0
index c2bc0782c0cd..1a8acf303548 100644
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0+
  // Driver for Awinic AW2013 3-channel LED driver
 
+#include <linux/devm-helpers.h>
  #include <linux/i2c.h>
  #include <linux/leds.h>
  #include <linux/module.h>

drivers/leds/leds-aw2013.c static int aw2013_probe_dt(struct aw2013 *chip) · line 324

Quoted 6 lines
drivers/leds/leds-aw2013.c +3 -0
@@ -318,6 +319,13 @@ static int aw2013_probe_dt(struct aw2013 *chip)
  	return 0;
  }
 
+static void aw2013_chip_disable_action(void *data)
+{
+	struct aw2013 *chip = (struct aw2013 *)data;

The cast is not needed because data is void*

And chip is not needed at all, you can pass data to aw2013_chip_disable() directly, without a cast either.

Quoted 8 lines
drivers/leds/leds-aw2013.c +4 -0
+
+	aw2013_chip_disable(chip);
+}
+
  static const struct regmap_config aw2013_regmap_config = {
  	.reg_bits = 8,
  	.val_bits = 8,

drivers/leds/leds-aw2013.c static int aw2013_probe(struct i2c_client *client) · line 346

Quoted 6 lines
drivers/leds/leds-aw2013.c +2 -1
@@ -334,7 +342,9 @@ static int aw2013_probe(struct i2c_client *client)
  	if (!chip)
  		return -ENOMEM;
 
-	mutex_init(&chip->mutex);
+	if (devm_mutex_init(&client->dev, &chip->mutex))
+		return -ENOMEM;

Why not return the value returned by devm_mutex_init() ?

Quoted 43 lines
drivers/leds/leds-aw2013.c +5 -11
+
  	mutex_lock(&chip->mutex);
 
  	chip->client = client;
@@ -378,6 +388,10 @@ static int aw2013_probe(struct i2c_client *client)
  		goto error_reg;
  	}
 
+	ret = devm_add_action(&client->dev, aw2013_chip_disable_action, chip);
+	if (ret)
+		goto error_reg;
+
  	ret = aw2013_probe_dt(chip);
  	if (ret < 0)
  		goto error_reg;
@@ -398,19 +412,9 @@ static int aw2013_probe(struct i2c_client *client)
 
  error:
  	mutex_unlock(&chip->mutex);
-	mutex_destroy(&chip->mutex);
  	return ret;
  }
 
-static void aw2013_remove(struct i2c_client *client)
-{
-	struct aw2013 *chip = i2c_get_clientdata(client);
-
-	aw2013_chip_disable(chip);
-
-	mutex_destroy(&chip->mutex);
-}
-
  static const struct of_device_id aw2013_match_table[] = {
  	{ .compatible = "awinic,aw2013", },
  	{ /* sentinel */ },
@@ -424,7 +428,6 @@ static struct i2c_driver aw2013_driver = {
  		.of_match_table = of_match_ptr(aw2013_match_table),
  	},
  	.probe = aw2013_probe,
-	.remove = aw2013_remove,
  };
 
  module_i2c_driver(aw2013_driver);
#32 lore raw

PATCH v2 4/10 leds: aw200xx: use devm API to cleanup module's resources

In reply to George Stark

In this driver LEDs are registered using devm_led_classdev_register() so they are automatically unregistered after module's remove() is done. led_classdev_unregister() calls module's led_set_brightness() to turn off the LEDs and that callback uses resources which were destroyed already in module's remove() so use devm API instead of remove().

---
 drivers/leds/leds-aw200xx.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)
drivers/leds/leds-aw200xx.c +25 -11
index 1d3943f86f7f..b1a097c7c879 100644
@@ -10,6 +10,7 @@
 #include <linux/bitfield.h>
 #include <linux/bits.h>
 #include <linux/container_of.h>
+#include <linux/devm-helpers.h>
 #include <linux/gpio/consumer.h>
 #include <linux/i2c.h>
 #include <linux/leds.h>
@@ -530,6 +531,20 @@ static const struct regmap_config aw200xx_regmap_config = {
 	.disable_locking = true,
 };
 
+static void aw200xx_chip_reset_action(void *data)
+{
+	const struct aw200xx *chip = (struct aw200xx *)data;
+
+	aw200xx_chip_reset(chip);
+}
+
+static void aw200xx_disable_action(void *data)
+{
+	const struct aw200xx *chip = (struct aw200xx *)data;
+
+	aw200xx_disable(chip);
+}
+
 static int aw200xx_probe(struct i2c_client *client)
 {
 	const struct aw200xx_chipdef *cdef;
@@ -568,11 +583,16 @@ static int aw200xx_probe(struct i2c_client *client)
 
 	aw200xx_enable(chip);
 
+	ret = devm_add_action(&client->dev, aw200xx_disable_action, chip);
+	if (ret)
+		return ret;
+
 	ret = aw200xx_chip_check(chip);
 	if (ret)
 		return ret;
 
-	mutex_init(&chip->mutex);
+	if (devm_mutex_init(&client->dev, &chip->mutex))
+		return -ENOMEM;
 
 	/* Need a lock now since after call aw200xx_probe_fw, sysfs nodes created */
 	mutex_lock(&chip->mutex);
@@ -581,6 +601,10 @@ static int aw200xx_probe(struct i2c_client *client)
 	if (ret)
 		goto out_unlock;
 
+	ret = devm_add_action(&client->dev, aw200xx_chip_reset_action, chip);
+	if (ret)
+		goto out_unlock;
+
 	ret = aw200xx_probe_fw(&client->dev, chip);
 	if (ret)
 		goto out_unlock;
@@ -595,15 +619,6 @@ static int aw200xx_probe(struct i2c_client *client)
 	return ret;
 }
 
-static void aw200xx_remove(struct i2c_client *client)
-{
-	struct aw200xx *chip = i2c_get_clientdata(client);
-
-	aw200xx_chip_reset(chip);
-	aw200xx_disable(chip);
-	mutex_destroy(&chip->mutex);
-}
-
 static const struct aw200xx_chipdef aw20036_cdef = {
 	.channels = 36,
 	.display_size_rows_max = 3,
@@ -652,7 +667,6 @@ static struct i2c_driver aw200xx_driver = {
 		.of_match_table = aw200xx_match_table,
 	},
 	.probe_new = aw200xx_probe,
-	.remove = aw200xx_remove,
 	.id_table = aw200xx_id,
 };
 module_i2c_driver(aw200xx_driver);
Signature · 1 line
2.38.4
#33 lore raw

Re: [PATCH v2 04/10] leds: aw200xx: use devm API to cleanup module's resources

Quoted 6 lines · George Stark

In this driver LEDs are registered using devm_led_classdev_register() so they are automatically unregistered after module's remove() is done. led_classdev_unregister() calls module's led_set_brightness() to turn off the LEDs and that callback uses resources which were destroyed already in module's remove() so use devm API instead of remove().

...

drivers/leds/leds-aw200xx.c static const struct regmap_config aw200xx_regmap_config = { · line 546

Quoted 13 lines
drivers/leds/leds-aw200xx.c +13 -0
+static void aw200xx_chip_reset_action(void *data)
+{
+       const struct aw200xx *chip = (struct aw200xx *)data;
+
+       aw200xx_chip_reset(chip);
+}
+
+static void aw200xx_disable_action(void *data)
+{
+       const struct aw200xx *chip = (struct aw200xx *)data;
+
+       aw200xx_disable(chip);
+}

They can be made oneliners.

...

drivers/leds/leds-aw200xx.c static int aw200xx_probe(struct i2c_client *client) · line 595

Quoted 2 lines
drivers/leds/leds-aw200xx.c +2 -0
+       if (devm_mutex_init(&client->dev, &chip->mutex))
+               return -ENOMEM;

Do not shadow the real error code.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#34 lore raw

Re: [PATCH v2 04/10] leds: aw200xx: use devm API to cleanup module's resources

In reply to George Stark

Le 04/12/2023 à 19:05, George Stark a écrit :

Quoted 24 lines

In this driver LEDs are registered using devm_led_classdev_register() so they are automatically unregistered after module's remove() is done. led_classdev_unregister() calls module's led_set_brightness() to turn off the LEDs and that callback uses resources which were destroyed already in module's remove() so use devm API instead of remove().

---
  drivers/leds/leds-aw200xx.c | 36 +++++++++++++++++++++++++-----------
  1 file changed, 25 insertions(+), 11 deletions(-)
drivers/leds/leds-aw200xx.c +1 -0
index 1d3943f86f7f..b1a097c7c879 100644
@@ -10,6 +10,7 @@
  #include <linux/bitfield.h>
  #include <linux/bits.h>
  #include <linux/container_of.h>
+#include <linux/devm-helpers.h>
  #include <linux/gpio/consumer.h>
  #include <linux/i2c.h>
  #include <linux/leds.h>

drivers/leds/leds-aw200xx.c static const struct regmap_config aw200xx_regmap_config = { · line 538

Quoted 8 lines
drivers/leds/leds-aw200xx.c +5 -0
@@ -530,6 +531,20 @@ static const struct regmap_config aw200xx_regmap_config = {
  	.disable_locking = true,
  };
 
+static void aw200xx_chip_reset_action(void *data)
+{
+	const struct aw200xx *chip = (struct aw200xx *)data;
+
+	aw200xx_chip_reset(chip);

Same as previous patch, no need to cast data and no need for chip at all, directly do aw200xx_chip_reset(data)

drivers/leds/leds-aw200xx.c static const struct regmap_config aw200xx_regmap_config = { · line 545

Quoted 7 lines
drivers/leds/leds-aw200xx.c +7 -0
+}
+
+static void aw200xx_disable_action(void *data)
+{
+	const struct aw200xx *chip = (struct aw200xx *)data;
+
+	aw200xx_disable(chip);

Same

Quoted 10 lines
drivers/leds/leds-aw200xx.c +3 -0
+}
+
  static int aw200xx_probe(struct i2c_client *client)
  {
  	const struct aw200xx_chipdef *cdef;
@@ -568,11 +583,16 @@ static int aw200xx_probe(struct i2c_client *client)
 
  	aw200xx_enable(chip);
 
+	ret = devm_add_action(&client->dev, aw200xx_disable_action, chip);

drivers/leds/leds-aw200xx.c static int aw200xx_probe(struct i2c_client *client) · line 595

Quoted 10 lines
drivers/leds/leds-aw200xx.c +5 -1
+	if (ret)
+		return ret;
+
  	ret = aw200xx_chip_check(chip);
  	if (ret)
  		return ret;
 
-	mutex_init(&chip->mutex);
+	if (devm_mutex_init(&client->dev, &chip->mutex))
+		return -ENOMEM;

Why not return value returned by dev_mutex_init() directly ?

Quoted 38 lines
drivers/leds/leds-aw200xx.c +4 -10
  	/* Need a lock now since after call aw200xx_probe_fw, sysfs nodes created */
  	mutex_lock(&chip->mutex);
@@ -581,6 +601,10 @@ static int aw200xx_probe(struct i2c_client *client)
  	if (ret)
  		goto out_unlock;
 
+	ret = devm_add_action(&client->dev, aw200xx_chip_reset_action, chip);
+	if (ret)
+		goto out_unlock;
+
  	ret = aw200xx_probe_fw(&client->dev, chip);
  	if (ret)
  		goto out_unlock;
@@ -595,15 +619,6 @@ static int aw200xx_probe(struct i2c_client *client)
  	return ret;
  }
 
-static void aw200xx_remove(struct i2c_client *client)
-{
-	struct aw200xx *chip = i2c_get_clientdata(client);
-
-	aw200xx_chip_reset(chip);
-	aw200xx_disable(chip);
-	mutex_destroy(&chip->mutex);
-}
-
  static const struct aw200xx_chipdef aw20036_cdef = {
  	.channels = 36,
  	.display_size_rows_max = 3,
@@ -652,7 +667,6 @@ static struct i2c_driver aw200xx_driver = {
  		.of_match_table = aw200xx_match_table,
  	},
  	.probe_new = aw200xx_probe,
-	.remove = aw200xx_remove,
  	.id_table = aw200xx_id,
  };
  module_i2c_driver(aw200xx_driver);
#35 lore raw

PATCH v2 5/10 leds: lp3952: use devm API to cleanup module's resources

In reply to George Stark

In this driver LEDs are registered using devm_led_classdev_register() so they are automatically unregistered after module's remove() is done. led_classdev_unregister() calls module's led_set_brightness() to turn off the LEDs and that callback uses resources which were destroyed already in module's remove() so use devm API instead of remove(). Also drop explicit turning LEDs off from remove() due to they will be off anyway by led_classdev_unregister().

---
 drivers/leds/leds-lp3952.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/leds/leds-lp3952.c +11 -10
index 3bd55652a706..fc0e02a9768f 100644
@@ -207,6 +207,13 @@ static const struct regmap_config lp3952_regmap = {
 	.cache_type = REGCACHE_RBTREE,
 };
 
+static void gpio_set_low_action(void *data)
+{
+	struct lp3952_led_array *priv = (struct lp3952_led_array *)data;
+
+	gpiod_set_value(priv->enable_gpio, 0);
+}
+
 static int lp3952_probe(struct i2c_client *client)
 {
 	int status;
@@ -226,6 +233,10 @@ static int lp3952_probe(struct i2c_client *client)
 		return status;
 	}
 
+	status = devm_add_action(&client->dev, gpio_set_low_action, priv);
+	if (status)
+		return status;
+
 	priv->regmap = devm_regmap_init_i2c(client, &lp3952_regmap);
 	if (IS_ERR(priv->regmap)) {
 		int err = PTR_ERR(priv->regmap);
@@ -254,15 +265,6 @@ static int lp3952_probe(struct i2c_client *client)
 	return 0;
 }
 
-static void lp3952_remove(struct i2c_client *client)
-{
-	struct lp3952_led_array *priv;
-
-	priv = i2c_get_clientdata(client);
-	lp3952_on_off(priv, LP3952_LED_ALL, false);
-	gpiod_set_value(priv->enable_gpio, 0);
-}
-
 static const struct i2c_device_id lp3952_id[] = {
 	{LP3952_NAME, 0},
 	{}
@@ -274,7 +276,6 @@ static struct i2c_driver lp3952_i2c_driver = {
 			.name = LP3952_NAME,
 	},
 	.probe = lp3952_probe,
-	.remove = lp3952_remove,
 	.id_table = lp3952_id,
 };
 
Signature · 1 line
2.38.4