Skip to contentSkip to conversation

driver core: bus: move lock_class_key into dynamic structure

PATCH In review Greg Kroah-Hartman 2 participants 3 messages Feb 1, 2023 Driver core

lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH 1/1 driver core: bus: move lock_class_key into dynamic structure

Move the lock_class_key structure out of struct bus_type and into the dynamic structure we create already for all bus_types registered with the kernel. This saves on static space and removes one more writable field in struct bus_type.

In the future, the same field can be moved out of the struct class logic because it shares this same private structure.

Most everyone will never notice this change, as lockdep is not enabled in real systems so no memory or logic changes are happening for them.

Cc "Rafael J. Wysocki" <[email protected]>
---
 drivers/base/base.h        | 2 ++
 drivers/base/bus.c         | 5 ++++-
 include/linux/device/bus.h | 1 -
 3 files changed, 6 insertions(+), 2 deletions(-)
drivers/base/base.h +2 -0
index 2208af509ce8..0e806f641079 100644
@@ -52,6 +52,8 @@ struct subsys_private {
 
 	struct kset glue_dirs;
 	struct class *class;
+
+	struct lock_class_key lock_key;
 };
 #define to_subsys_private(obj) container_of_const(obj, struct subsys_private, subsys.kobj)
 
drivers/base/bus.c +4 -1
index cf1b8f00b4c0..aa70b3a7d778 100644
@@ -154,6 +154,7 @@ static void bus_release(struct kobject *kobj)
 	struct subsys_private *priv = to_subsys_private(kobj);
 	struct bus_type *bus = priv->bus;
 
+	lockdep_unregister_key(&priv->lock_key);
 	kfree(priv);
 	bus->p = NULL;
 }
@@ -743,7 +744,7 @@ int bus_register(struct bus_type *bus)
 {
 	int retval;
 	struct subsys_private *priv;
-	struct lock_class_key *key = &bus->lock_key;
+	struct lock_class_key *key;
 
 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
 	if (!priv)
@@ -785,6 +786,8 @@ int bus_register(struct bus_type *bus)
 	}
 
 	INIT_LIST_HEAD(&priv->interfaces);
+	key = &priv->lock_key;
+	lockdep_register_key(key);
 	__mutex_init(&priv->mutex, "subsys mutex", key);
 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
 	klist_init(&priv->klist_drivers, NULL, NULL);
include/linux/device/bus.h +0 -1
index 87e4d029c915..e3094db1e9fa 100644
@@ -112,7 +112,6 @@ struct bus_type {
 	const struct iommu_ops *iommu_ops;
 
 	struct subsys_private *p;
-	struct lock_class_key lock_key;
 
 	bool need_parent_lock;
 };
Signature · 1 line
2.39.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Greg Kroah-Hartman

Move the lock_class_key structure out of struct bus_type and into the dynamic structure we create already for all bus_types registered with the kernel. This saves on static space and removes one more writable field in struct bus_type.

Quoted 8 lines

In the future, the same field can be moved out of the struct class logic because it shares this same private structure.

Most everyone will never notice this change, as lockdep is not enabled in real systems so no memory or logic changes are happening for them.

Cc "Rafael J. Wysocki" <[email protected]>

This looks reasonable to me, so

Quoted 64 lines
---
 drivers/base/base.h        | 2 ++
 drivers/base/bus.c         | 5 ++++-
 include/linux/device/bus.h | 1 -
 3 files changed, 6 insertions(+), 2 deletions(-)
drivers/base/base.h +2 -0
index 2208af509ce8..0e806f641079 100644
@@ -52,6 +52,8 @@ struct subsys_private {
 
        struct kset glue_dirs;
        struct class *class;
+
+       struct lock_class_key lock_key;
 };
 #define to_subsys_private(obj) container_of_const(obj, struct subsys_private, subsys.kobj)
 
drivers/base/bus.c +4 -1
index cf1b8f00b4c0..aa70b3a7d778 100644
@@ -154,6 +154,7 @@ static void bus_release(struct kobject *kobj)
        struct subsys_private *priv = to_subsys_private(kobj);
        struct bus_type *bus = priv->bus;
 
+       lockdep_unregister_key(&priv->lock_key);
        kfree(priv);
        bus->p = NULL;
 }
@@ -743,7 +744,7 @@ int bus_register(struct bus_type *bus)
 {
        int retval;
        struct subsys_private *priv;
-       struct lock_class_key *key = &bus->lock_key;
+       struct lock_class_key *key;
 
        priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
        if (!priv)
@@ -785,6 +786,8 @@ int bus_register(struct bus_type *bus)
        }
 
        INIT_LIST_HEAD(&priv->interfaces);
+       key = &priv->lock_key;
+       lockdep_register_key(key);
        __mutex_init(&priv->mutex, "subsys mutex", key);
        klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
        klist_init(&priv->klist_drivers, NULL, NULL);
include/linux/device/bus.h +0 -2
index 87e4d029c915..e3094db1e9fa 100644
@@ -112,7 +112,6 @@ struct bus_type {
        const struct iommu_ops *iommu_ops;
 
        struct subsys_private *p;
-       struct lock_class_key lock_key;
 
        bool need_parent_lock;
 };
--

2.39.1

author maintainer
#2 lore raw
Quoted 14 lines · Rafael J. Wysocki
Quoted 12 lines · Greg Kroah-Hartman

Move the lock_class_key structure out of struct bus_type and into the dynamic structure we create already for all bus_types registered with the kernel. This saves on static space and removes one more writable field in struct bus_type.

In the future, the same field can be moved out of the struct class logic because it shares this same private structure.

Most everyone will never notice this change, as lockdep is not enabled in real systems so no memory or logic changes are happening for them.

Quoted 6 lines
Quoted 2 lines
Cc "Rafael J. Wysocki" <[email protected]>

This looks reasonable to me, so

Thanks for the review.