Skip to contentSkip to conversation

driver core: move device->knode_class to device_private

PATCH Superseded Wei Yang 3 participants 8 messages Jan 17, 2019 – Jan 21, 2019 Driver core

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 driver core: move device->knode_class to device_private

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

This patch move device->knode_class to device_private to make it comply with code convention.

---
 drivers/base/base.h    |  4 ++++
 drivers/base/class.c   | 12 ++++++++----
 drivers/base/core.c    |  4 ++--
 include/linux/device.h |  1 -
 4 files changed, 14 insertions(+), 7 deletions(-)
drivers/base/base.h +4 -0
index 7a419a7a6235..37329a668935 100644
@@ -60,6 +60,7 @@ struct driver_private {
  * @knode_parent - node in sibling list
  * @knode_driver - node in driver list
  * @knode_bus - node in bus list
+ * @knode_class - node in class list
  * @deferred_probe - entry in deferred_probe_list which is used to retry the
  *	binding of drivers which were unable to get all the resources needed by
  *	the device; typically because it depends on another driver getting
@@ -74,6 +75,7 @@ struct device_private {
 	struct klist_node knode_parent;
 	struct klist_node knode_driver;
 	struct klist_node knode_bus;
+	struct klist_node knode_class;
 	struct list_head deferred_probe;
 	struct device *device;
 };
@@ -83,6 +85,8 @@ struct device_private {
 	container_of(obj, struct device_private, knode_driver)
 #define to_device_private_bus(obj)	\
 	container_of(obj, struct device_private, knode_bus)
+#define to_device_private_class(obj)	\
+	container_of(obj, struct device_private, knode_class)
 
 /* initialisation functions */
 extern int devices_init(void);
drivers/base/class.c +8 -4
index 54def4e02f00..df7fde599407 100644
@@ -119,14 +119,16 @@ static void class_put(struct class *cls)
 
 static void klist_class_dev_get(struct klist_node *n)
 {
-	struct device *dev = container_of(n, struct device, knode_class);
+	struct device_private *p = to_device_private_class(n);
+	struct device *dev = p->device;
 
 	get_device(dev);
 }
 
 static void klist_class_dev_put(struct klist_node *n)
 {
-	struct device *dev = container_of(n, struct device, knode_class);
+	struct device_private *p = to_device_private_class(n);
+	struct device *dev = p->device;
 
 	put_device(dev);
 }
@@ -277,7 +279,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
 	struct klist_node *start_knode = NULL;
 
 	if (start)
-		start_knode = &start->knode_class;
+		start_knode = &start->p->knode_class;
 	klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
 	iter->type = type;
 }
@@ -298,13 +300,15 @@ EXPORT_SYMBOL_GPL(class_dev_iter_init);
 struct device *class_dev_iter_next(struct class_dev_iter *iter)
 {
 	struct klist_node *knode;
+	struct device_private *p;
 	struct device *dev;
 
 	while (1) {
 		knode = klist_next(&iter->ki);
 		if (!knode)
 			return NULL;
-		dev = container_of(knode, struct device, knode_class);
+		p = to_device_private_class(knode);
+		dev = p->device;
 		if (!iter->type || iter->type == dev->type)
 			return dev;
 	}
drivers/base/core.c +2 -2
index 04bbcd779e11..a624d27d11c0 100644
@@ -1932,7 +1932,7 @@ int device_add(struct device *dev)
 	if (dev->class) {
 		mutex_lock(&dev->class->p->mutex);
 		/* tie the class to the device */
-		klist_add_tail(&dev->knode_class,
+		klist_add_tail(&dev->p->knode_class,
 			       &dev->class->p->klist_devices);
 
 		/* notify any interfaces that the device is here */
@@ -2069,7 +2069,7 @@ void device_del(struct device *dev)
 			if (class_intf->remove_dev)
 				class_intf->remove_dev(dev, class_intf);
 		/* remove the device from the class list */
-		klist_del(&dev->knode_class);
+		klist_del(&dev->p->knode_class);
 		mutex_unlock(&dev->class->p->mutex);
 	}
 	device_remove_file(dev, &dev_attr_uevent);
include/linux/device.h +0 -1
index 1b25c7a43f4c..040e97669f94 100644
@@ -1035,7 +1035,6 @@ struct device {
 	spinlock_t		devres_lock;
 	struct list_head	devres_head;
 
-	struct klist_node	knode_class;
 	struct class		*class;
 	const struct attribute_group **groups;	/* optional groups */
 
Signature · 1 line
2.19.1
#1 lore raw
Quoted 11 lines · Wei Yang

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

This patch move device->knode_class to device_private to make it comply with code convention.

As a rule, I'm not a big fan of changes like this, because the only reason for doing it seems to be aesthetics and it may be problematic for people doing various types of backports, live patching etc, but this particular one makes sense to me.

Quoted 41 lines
---
 drivers/base/base.h    |  4 ++++
 drivers/base/class.c   | 12 ++++++++----
 drivers/base/core.c    |  4 ++--
 include/linux/device.h |  1 -
 4 files changed, 14 insertions(+), 7 deletions(-)
drivers/base/base.h +4 -0
index 7a419a7a6235..37329a668935 100644
@@ -60,6 +60,7 @@ struct driver_private {
  * @knode_parent - node in sibling list
  * @knode_driver - node in driver list
  * @knode_bus - node in bus list
+ * @knode_class - node in class list
  * @deferred_probe - entry in deferred_probe_list which is used to retry the
  *     binding of drivers which were unable to get all the resources needed by
  *     the device; typically because it depends on another driver getting
@@ -74,6 +75,7 @@ struct device_private {
        struct klist_node knode_parent;
        struct klist_node knode_driver;
        struct klist_node knode_bus;
+       struct klist_node knode_class;
        struct list_head deferred_probe;
        struct device *device;
 };
@@ -83,6 +85,8 @@ struct device_private {
        container_of(obj, struct device_private, knode_driver)
 #define to_device_private_bus(obj)     \
        container_of(obj, struct device_private, knode_bus)
+#define to_device_private_class(obj)   \
+       container_of(obj, struct device_private, knode_class)
 
 /* initialisation functions */
 extern int devices_init(void);
drivers/base/class.c +0 -0
index 54def4e02f00..df7fde599407 100644

drivers/base/class.c static void class_put(struct class *cls) · line 123

Quoted 6 lines
drivers/base/class.c +2 -1
@@ -119,14 +119,16 @@ static void class_put(struct class *cls)
 
 static void klist_class_dev_get(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device_private *p = to_device_private_class(n);
+       struct device *dev = p->device;

the

p = to_device_private_class(n)
dev = p->device

pattern is repeated below for at least a couple of times.

Care to add a static helper for this to reduce the code duplication somewhat?

Quoted 75 lines
drivers/base/class.c +6 -3
        get_device(dev);
 }
 
 static void klist_class_dev_put(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device_private *p = to_device_private_class(n);
+       struct device *dev = p->device;
 
        put_device(dev);
 }
@@ -277,7 +279,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
        struct klist_node *start_knode = NULL;
 
        if (start)
-               start_knode = &start->knode_class;
+               start_knode = &start->p->knode_class;
        klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
        iter->type = type;
 }
@@ -298,13 +300,15 @@ EXPORT_SYMBOL_GPL(class_dev_iter_init);
 struct device *class_dev_iter_next(struct class_dev_iter *iter)
 {
        struct klist_node *knode;
+       struct device_private *p;
        struct device *dev;
 
        while (1) {
                knode = klist_next(&iter->ki);
                if (!knode)
                        return NULL;
-               dev = container_of(knode, struct device, knode_class);
+               p = to_device_private_class(knode);
+               dev = p->device;
                if (!iter->type || iter->type == dev->type)
                        return dev;
        }
drivers/base/core.c +2 -2
index 04bbcd779e11..a624d27d11c0 100644
@@ -1932,7 +1932,7 @@ int device_add(struct device *dev)
        if (dev->class) {
                mutex_lock(&dev->class->p->mutex);
                /* tie the class to the device */
-               klist_add_tail(&dev->knode_class,
+               klist_add_tail(&dev->p->knode_class,
                               &dev->class->p->klist_devices);
 
                /* notify any interfaces that the device is here */
@@ -2069,7 +2069,7 @@ void device_del(struct device *dev)
                        if (class_intf->remove_dev)
                                class_intf->remove_dev(dev, class_intf);
                /* remove the device from the class list */
-               klist_del(&dev->knode_class);
+               klist_del(&dev->p->knode_class);
                mutex_unlock(&dev->class->p->mutex);
        }
        device_remove_file(dev, &dev_attr_uevent);
include/linux/device.h +0 -2
index 1b25c7a43f4c..040e97669f94 100644
@@ -1035,7 +1035,6 @@ struct device {
        spinlock_t              devres_lock;
        struct list_head        devres_head;
 
-       struct klist_node       knode_class;
        struct class            *class;
        const struct attribute_group **groups;  /* optional groups */
 
--

2.19.1

author
#2 lore raw
Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Wei Yang

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

This patch move device->knode_class to device_private to make it comply with code convention.

Quoted 6 lines
Quoted 1 line

As a rule, I'm not a big fan of changes like this, because the only reason for doing it seems to be aesthetics and it may be problematic for people doing various types of backports, live patching etc, but this particular one makes sense to me.

Rafael,

Thanks for your comment.

To be honest, I didn't realized the trouble it will introduce to backport and live patching. :-)

Quoted 51 lines
Quoted 42 lines
---
 drivers/base/base.h    |  4 ++++
 drivers/base/class.c   | 12 ++++++++----
 drivers/base/core.c    |  4 ++--
 include/linux/device.h |  1 -
 4 files changed, 14 insertions(+), 7 deletions(-)
drivers/base/base.h +4 -0
index 7a419a7a6235..37329a668935 100644
@@ -60,6 +60,7 @@ struct driver_private {
  * @knode_parent - node in sibling list
  * @knode_driver - node in driver list
  * @knode_bus - node in bus list
+ * @knode_class - node in class list
  * @deferred_probe - entry in deferred_probe_list which is used to retry the
  *     binding of drivers which were unable to get all the resources needed by
  *     the device; typically because it depends on another driver getting
@@ -74,6 +75,7 @@ struct device_private {
        struct klist_node knode_parent;
        struct klist_node knode_driver;
        struct klist_node knode_bus;
+       struct klist_node knode_class;
        struct list_head deferred_probe;
        struct device *device;
 };
@@ -83,6 +85,8 @@ struct device_private {
        container_of(obj, struct device_private, knode_driver)
 #define to_device_private_bus(obj)     \
        container_of(obj, struct device_private, knode_bus)
+#define to_device_private_class(obj)   \
+       container_of(obj, struct device_private, knode_class)
 
 /* initialisation functions */
 extern int devices_init(void);
drivers/base/class.c +0 -0
index 54def4e02f00..df7fde599407 100644

drivers/base/class.c static void class_put(struct class *cls) · line 123

Quoted 6 lines
drivers/base/class.c +2 -1
@@ -119,14 +119,16 @@ static void class_put(struct class *cls)
 
 static void klist_class_dev_get(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device_private *p = to_device_private_class(n);
+       struct device *dev = p->device;

the

Quoted 7 lines

p = to_device_private_class(n)
dev = p->device

pattern is repeated below for at least a couple of times.

Care to add a static helper for this to reduce the code duplication somewhat?

Yep, this is reasonable. Let me put them in a helper.

Quoted 75 lines
Quoted 75 lines
drivers/base/class.c +6 -3
        get_device(dev);
 }
 
 static void klist_class_dev_put(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device_private *p = to_device_private_class(n);
+       struct device *dev = p->device;
 
        put_device(dev);
 }
@@ -277,7 +279,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
        struct klist_node *start_knode = NULL;
 
        if (start)
-               start_knode = &start->knode_class;
+               start_knode = &start->p->knode_class;
        klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
        iter->type = type;
 }
@@ -298,13 +300,15 @@ EXPORT_SYMBOL_GPL(class_dev_iter_init);
 struct device *class_dev_iter_next(struct class_dev_iter *iter)
 {
        struct klist_node *knode;
+       struct device_private *p;
        struct device *dev;
 
        while (1) {
                knode = klist_next(&iter->ki);
                if (!knode)
                        return NULL;
-               dev = container_of(knode, struct device, knode_class);
+               p = to_device_private_class(knode);
+               dev = p->device;
                if (!iter->type || iter->type == dev->type)
                        return dev;
        }
drivers/base/core.c +2 -2
index 04bbcd779e11..a624d27d11c0 100644
@@ -1932,7 +1932,7 @@ int device_add(struct device *dev)
        if (dev->class) {
                mutex_lock(&dev->class->p->mutex);
                /* tie the class to the device */
-               klist_add_tail(&dev->knode_class,
+               klist_add_tail(&dev->p->knode_class,
                               &dev->class->p->klist_devices);
 
                /* notify any interfaces that the device is here */
@@ -2069,7 +2069,7 @@ void device_del(struct device *dev)
                        if (class_intf->remove_dev)
                                class_intf->remove_dev(dev, class_intf);
                /* remove the device from the class list */
-               klist_del(&dev->knode_class);
+               klist_del(&dev->p->knode_class);
                mutex_unlock(&dev->class->p->mutex);
        }
        device_remove_file(dev, &dev_attr_uevent);
include/linux/device.h +0 -2
index 1b25c7a43f4c..040e97669f94 100644
@@ -1035,7 +1035,6 @@ struct device {
        spinlock_t              devres_lock;
        struct list_head        devres_head;
 
-       struct klist_node       knode_class;
        struct class            *class;
        const struct attribute_group **groups;  /* optional groups */
 
--

2.19.1

Signature · 2 lines
Wei Yang
Help you, Help me
author
#3 lore raw

PATCH v2 1/1 driver core: move device->knode_class to device_private

In reply to Wei Yang

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

This patch move device->knode_class to device_private to make it comply with code convention.

---
v2: add a helper to get dev from klist_class

---
 drivers/base/base.h    |  4 ++++
 drivers/base/class.c   | 14 ++++++++++----
 drivers/base/core.c    |  4 ++--
 include/linux/device.h |  1 -
 4 files changed, 16 insertions(+), 7 deletions(-)
drivers/base/base.h +4 -0
index 7a419a7a6235..37329a668935 100644
@@ -60,6 +60,7 @@ struct driver_private {
  * @knode_parent - node in sibling list
  * @knode_driver - node in driver list
  * @knode_bus - node in bus list
+ * @knode_class - node in class list
  * @deferred_probe - entry in deferred_probe_list which is used to retry the
  *	binding of drivers which were unable to get all the resources needed by
  *	the device; typically because it depends on another driver getting
@@ -74,6 +75,7 @@ struct device_private {
 	struct klist_node knode_parent;
 	struct klist_node knode_driver;
 	struct klist_node knode_bus;
+	struct klist_node knode_class;
 	struct list_head deferred_probe;
 	struct device *device;
 };
@@ -83,6 +85,8 @@ struct device_private {
 	container_of(obj, struct device_private, knode_driver)
 #define to_device_private_bus(obj)	\
 	container_of(obj, struct device_private, knode_bus)
+#define to_device_private_class(obj)	\
+	container_of(obj, struct device_private, knode_class)
 
 /* initialisation functions */
 extern int devices_init(void);
drivers/base/class.c +10 -4
index 54def4e02f00..d8a6a5864c2e 100644
@@ -117,16 +117,22 @@ static void class_put(struct class *cls)
 		kset_put(&cls->p->subsys);
 }
 
+static struct device *klist_class_to_dev(struct klist_node *n)
+{
+	struct device_private *p = to_device_private_class(n);
+	return p->device;
+}
+
 static void klist_class_dev_get(struct klist_node *n)
 {
-	struct device *dev = container_of(n, struct device, knode_class);
+	struct device *dev = klist_class_to_dev(n);
 
 	get_device(dev);
 }
 
 static void klist_class_dev_put(struct klist_node *n)
 {
-	struct device *dev = container_of(n, struct device, knode_class);
+	struct device *dev = klist_class_to_dev(n);
 
 	put_device(dev);
 }
@@ -277,7 +283,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
 	struct klist_node *start_knode = NULL;
 
 	if (start)
-		start_knode = &start->knode_class;
+		start_knode = &start->p->knode_class;
 	klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
 	iter->type = type;
 }
@@ -304,7 +310,7 @@ struct device *class_dev_iter_next(struct class_dev_iter *iter)
 		knode = klist_next(&iter->ki);
 		if (!knode)
 			return NULL;
-		dev = container_of(knode, struct device, knode_class);
+		dev = klist_class_to_dev(knode);
 		if (!iter->type || iter->type == dev->type)
 			return dev;
 	}
drivers/base/core.c +2 -2
index 04bbcd779e11..a624d27d11c0 100644
@@ -1932,7 +1932,7 @@ int device_add(struct device *dev)
 	if (dev->class) {
 		mutex_lock(&dev->class->p->mutex);
 		/* tie the class to the device */
-		klist_add_tail(&dev->knode_class,
+		klist_add_tail(&dev->p->knode_class,
 			       &dev->class->p->klist_devices);
 
 		/* notify any interfaces that the device is here */
@@ -2069,7 +2069,7 @@ void device_del(struct device *dev)
 			if (class_intf->remove_dev)
 				class_intf->remove_dev(dev, class_intf);
 		/* remove the device from the class list */
-		klist_del(&dev->knode_class);
+		klist_del(&dev->p->knode_class);
 		mutex_unlock(&dev->class->p->mutex);
 	}
 	device_remove_file(dev, &dev_attr_uevent);
include/linux/device.h +0 -1
index 1b25c7a43f4c..040e97669f94 100644
@@ -1035,7 +1035,6 @@ struct device {
 	spinlock_t		devres_lock;
 	struct list_head	devres_head;
 
-	struct klist_node	knode_class;
 	struct class		*class;
 	const struct attribute_group **groups;	/* optional groups */
 
Signature · 1 line
2.19.1
maintainer Reviewed-by on v2 1/1
#4 lore raw
Quoted 7 lines · Wei Yang

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

Quoted 7 lines

This patch move device->knode_class to device_private to make it comply with code convention.

---
v2: add a helper to get dev from klist_class

Looks better now.

Quoted 120 lines
---
 drivers/base/base.h    |  4 ++++
 drivers/base/class.c   | 14 ++++++++++----
 drivers/base/core.c    |  4 ++--
 include/linux/device.h |  1 -
 4 files changed, 16 insertions(+), 7 deletions(-)
drivers/base/base.h +4 -0
index 7a419a7a6235..37329a668935 100644
@@ -60,6 +60,7 @@ struct driver_private {
  * @knode_parent - node in sibling list
  * @knode_driver - node in driver list
  * @knode_bus - node in bus list
+ * @knode_class - node in class list
  * @deferred_probe - entry in deferred_probe_list which is used to retry the
  *     binding of drivers which were unable to get all the resources needed by
  *     the device; typically because it depends on another driver getting
@@ -74,6 +75,7 @@ struct device_private {
        struct klist_node knode_parent;
        struct klist_node knode_driver;
        struct klist_node knode_bus;
+       struct klist_node knode_class;
        struct list_head deferred_probe;
        struct device *device;
 };
@@ -83,6 +85,8 @@ struct device_private {
        container_of(obj, struct device_private, knode_driver)
 #define to_device_private_bus(obj)     \
        container_of(obj, struct device_private, knode_bus)
+#define to_device_private_class(obj)   \
+       container_of(obj, struct device_private, knode_class)
 
 /* initialisation functions */
 extern int devices_init(void);
drivers/base/class.c +10 -4
index 54def4e02f00..d8a6a5864c2e 100644
@@ -117,16 +117,22 @@ static void class_put(struct class *cls)
                kset_put(&cls->p->subsys);
 }
 
+static struct device *klist_class_to_dev(struct klist_node *n)
+{
+       struct device_private *p = to_device_private_class(n);
+       return p->device;
+}
+
 static void klist_class_dev_get(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device *dev = klist_class_to_dev(n);
 
        get_device(dev);
 }
 
 static void klist_class_dev_put(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device *dev = klist_class_to_dev(n);
 
        put_device(dev);
 }
@@ -277,7 +283,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
        struct klist_node *start_knode = NULL;
 
        if (start)
-               start_knode = &start->knode_class;
+               start_knode = &start->p->knode_class;
        klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
        iter->type = type;
 }
@@ -304,7 +310,7 @@ struct device *class_dev_iter_next(struct class_dev_iter *iter)
                knode = klist_next(&iter->ki);
                if (!knode)
                        return NULL;
-               dev = container_of(knode, struct device, knode_class);
+               dev = klist_class_to_dev(knode);
                if (!iter->type || iter->type == dev->type)
                        return dev;
        }
drivers/base/core.c +2 -2
index 04bbcd779e11..a624d27d11c0 100644
@@ -1932,7 +1932,7 @@ int device_add(struct device *dev)
        if (dev->class) {
                mutex_lock(&dev->class->p->mutex);
                /* tie the class to the device */
-               klist_add_tail(&dev->knode_class,
+               klist_add_tail(&dev->p->knode_class,
                               &dev->class->p->klist_devices);
 
                /* notify any interfaces that the device is here */
@@ -2069,7 +2069,7 @@ void device_del(struct device *dev)
                        if (class_intf->remove_dev)
                                class_intf->remove_dev(dev, class_intf);
                /* remove the device from the class list */
-               klist_del(&dev->knode_class);
+               klist_del(&dev->p->knode_class);
                mutex_unlock(&dev->class->p->mutex);
        }
        device_remove_file(dev, &dev_attr_uevent);
include/linux/device.h +0 -2
index 1b25c7a43f4c..040e97669f94 100644
@@ -1035,7 +1035,6 @@ struct device {
        spinlock_t              devres_lock;
        struct list_head        devres_head;
 
-       struct klist_node       knode_class;
        struct class            *class;
        const struct attribute_group **groups;  /* optional groups */
 
--

2.19.1

author
#5 lore raw
Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Wei Yang

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

This patch move device->knode_class to device_private to make it comply with code convention.

Quoted 8 lines
Quoted 4 lines

---
v2: add a helper to get dev from klist_class

Looks better now.

Thanks :-)

Quoted 121 lines
Quoted 121 lines
---
 drivers/base/base.h    |  4 ++++
 drivers/base/class.c   | 14 ++++++++++----
 drivers/base/core.c    |  4 ++--
 include/linux/device.h |  1 -
 4 files changed, 16 insertions(+), 7 deletions(-)
drivers/base/base.h +4 -0
index 7a419a7a6235..37329a668935 100644
@@ -60,6 +60,7 @@ struct driver_private {
  * @knode_parent - node in sibling list
  * @knode_driver - node in driver list
  * @knode_bus - node in bus list
+ * @knode_class - node in class list
  * @deferred_probe - entry in deferred_probe_list which is used to retry the
  *     binding of drivers which were unable to get all the resources needed by
  *     the device; typically because it depends on another driver getting
@@ -74,6 +75,7 @@ struct device_private {
        struct klist_node knode_parent;
        struct klist_node knode_driver;
        struct klist_node knode_bus;
+       struct klist_node knode_class;
        struct list_head deferred_probe;
        struct device *device;
 };
@@ -83,6 +85,8 @@ struct device_private {
        container_of(obj, struct device_private, knode_driver)
 #define to_device_private_bus(obj)     \
        container_of(obj, struct device_private, knode_bus)
+#define to_device_private_class(obj)   \
+       container_of(obj, struct device_private, knode_class)
 
 /* initialisation functions */
 extern int devices_init(void);
drivers/base/class.c +10 -4
index 54def4e02f00..d8a6a5864c2e 100644
@@ -117,16 +117,22 @@ static void class_put(struct class *cls)
                kset_put(&cls->p->subsys);
 }
 
+static struct device *klist_class_to_dev(struct klist_node *n)
+{
+       struct device_private *p = to_device_private_class(n);
+       return p->device;
+}
+
 static void klist_class_dev_get(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device *dev = klist_class_to_dev(n);
 
        get_device(dev);
 }
 
 static void klist_class_dev_put(struct klist_node *n)
 {
-       struct device *dev = container_of(n, struct device, knode_class);
+       struct device *dev = klist_class_to_dev(n);
 
        put_device(dev);
 }
@@ -277,7 +283,7 @@ void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
        struct klist_node *start_knode = NULL;
 
        if (start)
-               start_knode = &start->knode_class;
+               start_knode = &start->p->knode_class;
        klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
        iter->type = type;
 }
@@ -304,7 +310,7 @@ struct device *class_dev_iter_next(struct class_dev_iter *iter)
                knode = klist_next(&iter->ki);
                if (!knode)
                        return NULL;
-               dev = container_of(knode, struct device, knode_class);
+               dev = klist_class_to_dev(knode);
                if (!iter->type || iter->type == dev->type)
                        return dev;
        }
drivers/base/core.c +2 -2
index 04bbcd779e11..a624d27d11c0 100644
@@ -1932,7 +1932,7 @@ int device_add(struct device *dev)
        if (dev->class) {
                mutex_lock(&dev->class->p->mutex);
                /* tie the class to the device */
-               klist_add_tail(&dev->knode_class,
+               klist_add_tail(&dev->p->knode_class,
                               &dev->class->p->klist_devices);
 
                /* notify any interfaces that the device is here */
@@ -2069,7 +2069,7 @@ void device_del(struct device *dev)
                        if (class_intf->remove_dev)
                                class_intf->remove_dev(dev, class_intf);
                /* remove the device from the class list */
-               klist_del(&dev->knode_class);
+               klist_del(&dev->p->knode_class);
                mutex_unlock(&dev->class->p->mutex);
        }
        device_remove_file(dev, &dev_attr_uevent);
include/linux/device.h +0 -2
index 1b25c7a43f4c..040e97669f94 100644
@@ -1035,7 +1035,6 @@ struct device {
        spinlock_t              devres_lock;
        struct list_head        devres_head;
 
-       struct klist_node       knode_class;
        struct class            *class;
        const struct attribute_group **groups;  /* optional groups */
 
--

2.19.1

Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#6 lore raw
In reply to Wei Yang
Quoted 11 lines · Wei Yang

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

This patch move device->knode_class to device_private to make it comply with code convention.

Thanks for this. There are a number of other variables that should move into this structure, if you want to take a look into doing that.

greg k-h

author
#7 lore raw
Quoted 7 lines · Greg KH
Quoted 6 lines · Wei Yang

As the description of struct device_private says, it stores data which is private to driver core. And it already has similar fields like:
knode_parent, knode_driver, knode_driver and knode_bus. This look it is more proper to put knode_class together with those fields to make it private to driver core.

Quoted 8 lines
Quoted 5 lines

This patch move device->knode_class to device_private to make it comply with code convention.

Thanks for this. There are a number of other variables that should move into this structure, if you want to take a look into doing that.

Really? I may not notice that yet.

I may try to take a look into this, but not sure I would have enough time :-)

Quoted 2 lines

greg k-h

Signature · 2 lines
Wei Yang
Help you, Help me