[PATCH v2 01/12] device-core: Move device_lock() lockdep init to a helper

From: Dan Williams
Date: Wed Apr 13 2022 - 02:01:56 EST


In preparation for new infrastructure to support lockdep validation of
device_lock() usage across driver subsystems, add a
device_lockdep_init() helper to contain those updates.

Suggested-by: Pierre-Louis Bossart <pierre-louis.bossart@xxxxxxxxxxxxxxx>
Reviewed-by: Dave Jiang <dave.jiang@xxxxxxxxx>
Reviewed-by: Kevin Tian <kevin.tian@xxxxxxxxx>
Signed-off-by: Dan Williams <dan.j.williams@xxxxxxxxx>
---
drivers/base/core.c | 5 +----
include/linux/device.h | 13 +++++++++++++
2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 3d6430eb0c6a..cb782299ae44 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2864,10 +2864,7 @@ void device_initialize(struct device *dev)
kobject_init(&dev->kobj, &device_ktype);
INIT_LIST_HEAD(&dev->dma_pools);
mutex_init(&dev->mutex);
-#ifdef CONFIG_PROVE_LOCKING
- mutex_init(&dev->lockdep_mutex);
-#endif
- lockdep_set_novalidate_class(&dev->mutex);
+ device_lockdep_init(dev);
spin_lock_init(&dev->devres_lock);
INIT_LIST_HEAD(&dev->devres_head);
device_pm_init(dev);
diff --git a/include/linux/device.h b/include/linux/device.h
index 93459724dcde..af2576ace130 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -762,6 +762,19 @@ static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
return !!(dev->power.driver_flags & flags);
}

+#ifdef CONFIG_PROVE_LOCKING
+static inline void device_lockdep_init(struct device *dev)
+{
+ mutex_init(&dev->lockdep_mutex);
+ lockdep_set_novalidate_class(&dev->mutex);
+}
+#else
+static inline void device_lockdep_init(struct device *dev)
+{
+ lockdep_set_novalidate_class(&dev->mutex);
+}
+#endif
+
static inline void device_lock(struct device *dev)
{
mutex_lock(&dev->mutex);