[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20231213223020.2713164-4-gnstark@salutedevices.com>
Date: Thu, 14 Dec 2023 01:30:12 +0300
From: George Stark <gnstark@...utedevices.com>
To: <andy.shevchenko@...il.com>, <pavel@....cz>, <lee@...nel.org>,
<vadimp@...dia.com>, <mpe@...erman.id.au>, <npiggin@...il.com>,
<christophe.leroy@...roup.eu>, <hdegoede@...hat.com>,
<mazziesaccount@...il.com>, <peterz@...radead.org>,
<mingo@...hat.com>, <will@...nel.org>, <longman@...hat.com>,
<boqun.feng@...il.com>, <nikitos.tr@...il.com>
CC: <linux-leds@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
<linuxppc-dev@...ts.ozlabs.org>, <kernel@...utedevices.com>,
George Stark <gnstark@...utedevices.com>
Subject: [PATCH v3 03/11] devm-helpers: introduce devm_mutex_init
Using of devm API leads to a certain order of releasing resources.
So all dependent resources which are not devm-wrapped should be deleted
with respect to devm-release order. Mutex is one of such objects that
often is bound to other resources and has no own devm wrapper.
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().
Signed-off-by: George Stark <gnstark@...utedevices.com>
---
include/linux/devm-helpers.h | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/include/linux/devm-helpers.h b/include/linux/devm-helpers.h
index 74891802200d..4043c3481d2e 100644
--- a/include/linux/devm-helpers.h
+++ b/include/linux/devm-helpers.h
@@ -24,6 +24,7 @@
*/
#include <linux/device.h>
+#include <linux/mutex.h>
#include <linux/workqueue.h>
static inline void devm_delayed_work_drop(void *res)
@@ -76,4 +77,30 @@ static inline int devm_work_autocancel(struct device *dev,
return devm_add_action(dev, devm_work_drop, w);
}
+#ifdef mutex_destroy
+static inline void devm_mutex_release(void *res)
+{
+ mutex_destroy(res);
+}
+#endif
+
+/**
+ * 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.
+ */
+static inline int devm_mutex_init(struct device *dev, struct mutex *lock)
+{
+ mutex_init(lock);
+#ifdef mutex_destroy
+ return devm_add_action_or_reset(dev, devm_mutex_release, lock);
+#else
+ return 0;
+#endif
+}
+
#endif
--
2.25.1
Powered by blists - more mailing lists