[PATCH 1/2] devm-helpers: Add resource managed version of mutex init
Hans de Goede
hdegoede at redhat.com
Thu Feb 22 18:33:15 UTC 2024
Hi,
On 2/22/24 17:44, Matthew Auld wrote:
> On 22/02/2024 14:58, Marek Behún wrote:
>> A few drivers are doing resource-managed mutex initialization by
>> implementing ad-hoc one-liner mutex dropping functions and using them
>> with devm_add_action_or_reset(). Help drivers avoid these repeated
>> one-liners by adding managed version of mutex initialization.
<snip>
>> index 74891802200d..70640fb96117 100644
>> --- a/include/linux/devm-helpers.h
>> +++ b/include/linux/devm-helpers.h
>> @@ -24,6 +24,8 @@
>> */
>> #include <linux/device.h>
>> +#include <linux/kconfig.h>
>> +#include <linux/mutex.h>
>> #include <linux/workqueue.h>
>> static inline void devm_delayed_work_drop(void *res)
>> @@ -76,4 +78,34 @@ static inline int devm_work_autocancel(struct device *dev,
>> return devm_add_action(dev, devm_work_drop, w);
>> }
>> +static inline void devm_mutex_drop(void *res)
>> +{
>> + mutex_destroy(res);
>> +}
>> +
>> +/**
>> + * devm_mutex_init - Resource managed mutex initialization
>> + * @dev: Device which lifetime mutex is bound to
>> + * @lock: Mutex to be initialized (and automatically destroyed)
>> + *
>> + * Initialize mutex which is automatically destroyed when driver is detached.
>> + * A few drivers initialize mutexes which they want destroyed before driver is
>> + * detached, for debugging purposes.
>> + * devm_mutex_init() can be used to omit the explicit mutex_destroy() call when
>> + * driver is detached.
>> + */
>> +static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
>> +{
>> + mutex_init(lock);
>
> Do you know if this this needs __always_inline? The static lockdep key in mutex_init() should be
> different for each caller class. See c21f11d182c2 ("drm: fix drmm_mutex_init()").
That is a very good point. I believe that this should mirror mutex_init() and
the actual static inline function should be __devm_mutex_init() which takes
the key as extra argument (and calls __mutex_init()) and then make
devm_mutex_init() itself a macro mirroring the mutex_init() macro.
Regards,
Hans
>
>> +
>> + /*
>> + * mutex_destroy() is an empty function if CONFIG_DEBUG_MUTEXES is
>> + * disabled. No need to allocate an action in that case.
>> + */
>> + if (IS_ENABLED(CONFIG_DEBUG_MUTEXES))
>> + return devm_add_action_or_reset(dev, devm_mutex_drop, lock);
>> + else
>> + return 0;
>> +}
>> +
>> #endif
>
More information about the dri-devel
mailing list