Skip to contentSkip to conversation

driver core: Simplify driver API device_find_child_by_name() implementation

PATCH Superseded Zijun Hu 2 participants 5 messages Jul 20, 2024 – Aug 1, 2024 Driver core

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zijun Hu

author
#0 lore raw

PATCH 1/1 driver core: Simplify driver API device_find_child_by_name() implementation

From: Zijun Hu <[email protected]>

Simplify device_find_child_by_name() implementation by using present driver APIs device_find_child() and device_match_name().

---
 drivers/base/core.c    | 15 +++------------
 include/linux/device.h |  4 ++++
 2 files changed, 7 insertions(+), 12 deletions(-)
drivers/base/core.c +3 -12
index 730cae66607c..22ab4b8a2bcd 100644
@@ -4089,18 +4089,9 @@ EXPORT_SYMBOL_GPL(device_find_child);
 struct device *device_find_child_by_name(struct device *parent,
 					 const char *name)
 {
-	struct klist_iter i;
-	struct device *child;
-
-	if (!parent)
-		return NULL;
-
-	klist_iter_init(&parent->p->klist_children, &i);
-	while ((child = next_device(&i)))
-		if (sysfs_streq(dev_name(child), name) && get_device(child))
-			break;
-	klist_iter_exit(&i);
-	return child;
+	/* TODO: remove type cast after const device_find_child() prototype */
+	return device_find_child(parent, (void *)name,
+				 (int (*)(struct device *, void *))device_match_name);
 }
 EXPORT_SYMBOL_GPL(device_find_child_by_name);
 
include/linux/device.h +4 -0
index 34eb20f5966f..685ffd2dc867 100644
@@ -47,6 +47,9 @@ struct dev_pin_info;
 struct dev_iommu;
 struct msi_device_data;
 
+/* TODO: unify device match() parameter of driver APIs to this signature */
+typedef int (*device_match_t)(struct device *dev, const void *data);
+
 /**
  * struct subsys_interface - interfaces to device functions
  * @name:       name of the device function
@@ -1073,6 +1076,7 @@ int device_for_each_child(struct device *dev, void *data,
 			  int (*fn)(struct device *dev, void *data));
 int device_for_each_child_reverse(struct device *dev, void *data,
 				  int (*fn)(struct device *dev, void *data));
+/* TODO: change type of @data to const void * and @match to device_match_t */
 struct device *device_find_child(struct device *dev, void *data,
 				 int (*match)(struct device *dev, void *data));
 struct device *device_find_child_by_name(struct device *parent,

---
base-commit: b57d5ffc3ab507d0e19fc8b90b19c76af43fb790
change-id: 20240720-simplify_drv_api-058f9f3584c2

Best regards,

Signature · 1 line
Zijun Hu <[email protected]>
#1 lore raw
Quoted 22 lines · Zijun Hu

From: Zijun Hu <[email protected]>

Simplify device_find_child_by_name() implementation by using present driver APIs device_find_child() and device_match_name().

---
 drivers/base/core.c    | 15 +++------------
 include/linux/device.h |  4 ++++
 2 files changed, 7 insertions(+), 12 deletions(-)
drivers/base/core.c +0 -3
index 730cae66607c..22ab4b8a2bcd 100644
@@ -4089,18 +4089,9 @@ EXPORT_SYMBOL_GPL(device_find_child);
 struct device *device_find_child_by_name(struct device *parent,
 					 const char *name)
 {
-	struct klist_iter i;
-	struct device *child;
-

drivers/base/core.c EXPORT_SYMBOL_GPL(device_find_child); · line 4092

Quoted 10 lines
drivers/base/core.c +1 -9
-	if (!parent)
-		return NULL;
-
-	klist_iter_init(&parent->p->klist_children, &i);
-	while ((child = next_device(&i)))
-		if (sysfs_streq(dev_name(child), name) && get_device(child))
-			break;
-	klist_iter_exit(&i);
-	return child;
+	/* TODO: remove type cast after const device_find_child() prototype */

I do not understand the TODO here. Why is it needed? Why not fix it up now?

Quoted 10 lines
drivers/base/core.c +2 -0
+	return device_find_child(parent, (void *)name,
+				 (int (*)(struct device *, void *))device_match_name);
 }
 EXPORT_SYMBOL_GPL(device_find_child_by_name);
 
include/linux/device.h +0 -0
index 34eb20f5966f..685ffd2dc867 100644

include/linux/device.h struct dev_pin_info; · line 50

Quoted 4 lines
include/linux/device.h +1 -0
@@ -47,6 +47,9 @@ struct dev_pin_info;
 struct dev_iommu;
 struct msi_device_data;
 
+/* TODO: unify device match() parameter of driver APIs to this signature */

Same here, why have this? Why not unify them and then fix this up?

include/linux/device.h int device_for_each_child(struct device *dev, void *data, · line 1079

Quoted 10 lines
include/linux/device.h +3 -0
+typedef int (*device_match_t)(struct device *dev, const void *data);
+
 /**
  * struct subsys_interface - interfaces to device functions
  * @name:       name of the device function
@@ -1073,6 +1076,7 @@ int device_for_each_child(struct device *dev, void *data,
 			  int (*fn)(struct device *dev, void *data));
 int device_for_each_child_reverse(struct device *dev, void *data,
 				  int (*fn)(struct device *dev, void *data));
+/* TODO: change type of @data to const void * and @match to device_match_t */

Again, why leave this here?

thanks,

greg k-h

author
#2 lore raw
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Zijun Hu

From: Zijun Hu <[email protected]>

Simplify device_find_child_by_name() implementation by using present driver APIs device_find_child() and device_match_name().

---
 drivers/base/core.c    | 15 +++------------
 include/linux/device.h |  4 ++++
 2 files changed, 7 insertions(+), 12 deletions(-)
drivers/base/core.c +0 -8
index 730cae66607c..22ab4b8a2bcd 100644
@@ -4089,18 +4089,9 @@ EXPORT_SYMBOL_GPL(device_find_child);
 struct device *device_find_child_by_name(struct device *parent,
 					 const char *name)
 {
-	struct klist_iter i;
-	struct device *child;
-
-	if (!parent)
-		return NULL;
-
-	klist_iter_init(&parent->p->klist_children, &i);
-	while ((child = next_device(&i)))
Quoted 9 lines

drivers/base/core.c EXPORT_SYMBOL_GPL(device_find_child); · line 4092

Quoted 5 lines
drivers/base/core.c +1 -4
-		if (sysfs_streq(dev_name(child), name) && get_device(child))
-			break;
-	klist_iter_exit(&i);
-	return child;
+	/* TODO: remove type cast after const device_find_child() prototype */

I do not understand the TODO here. Why is it needed? Why not fix it up now?

i have below findings during trying to simplify this API.

there are a type of driver APIs for finding device, for example, (bus|driver|class)_find_device() which all take below type for parameter @match:
int (*match)(struct device *, const void *match_data)
but device_find_child() take below type with void * for @match_data:
int (*match)(struct device *, void *match_data).

@match's type of device_find_child() is not good as other finding APIs since nothing will be touched for finding operations normally.

i want to introduce a dedicate type for device match. typedef int (*device_match_t)(struct device *dev, const void *data);

advantages:
1) device_match_t is simpler for finding APIs declarations and definitions
2) maybe stop further driver APIs from using bad match type as device_find_child()

TODO:
1) introduce device_match_t and use it for current present APIs (bus|driver|class)_find_device()
2) change API device_find_child() to take device_match_t, more jobs to do since need to touch many drivers
3) correct this change by by remove all TODO inline comments and force cast.

not sure if my ideas is good, what is your opinions?

Quoted 33 lines
Quoted 10 lines
drivers/base/core.c +2 -0
+	return device_find_child(parent, (void *)name,
+				 (int (*)(struct device *, void *))device_match_name);
 }
 EXPORT_SYMBOL_GPL(device_find_child_by_name);
 
include/linux/device.h +0 -0
index 34eb20f5966f..685ffd2dc867 100644

include/linux/device.h struct dev_pin_info; · line 50

Quoted 4 lines
include/linux/device.h +1 -0
@@ -47,6 +47,9 @@ struct dev_pin_info;
 struct dev_iommu;
 struct msi_device_data;
 
+/* TODO: unify device match() parameter of driver APIs to this signature */

Same here, why have this? Why not unify them and then fix this up?

include/linux/device.h int device_for_each_child(struct device *dev, void *data, · line 1079

Quoted 10 lines
include/linux/device.h +3 -0
+typedef int (*device_match_t)(struct device *dev, const void *data);
+
 /**
  * struct subsys_interface - interfaces to device functions
  * @name:       name of the device function
@@ -1073,6 +1076,7 @@ int device_for_each_child(struct device *dev, void *data,
 			  int (*fn)(struct device *dev, void *data));
 int device_for_each_child_reverse(struct device *dev, void *data,
 				  int (*fn)(struct device *dev, void *data));
+/* TODO: change type of @data to const void * and @match to device_match_t */

Again, why leave this here?

thanks,

greg k-h

#3 lore raw
Quoted 48 lines · Zijun Hu
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Zijun Hu

From: Zijun Hu <[email protected]>

Simplify device_find_child_by_name() implementation by using present driver APIs device_find_child() and device_match_name().

---
 drivers/base/core.c    | 15 +++------------
 include/linux/device.h |  4 ++++
 2 files changed, 7 insertions(+), 12 deletions(-)
drivers/base/core.c +0 -8
index 730cae66607c..22ab4b8a2bcd 100644
@@ -4089,18 +4089,9 @@ EXPORT_SYMBOL_GPL(device_find_child);
 struct device *device_find_child_by_name(struct device *parent,
 					 const char *name)
 {
-	struct klist_iter i;
-	struct device *child;
-
-	if (!parent)
-		return NULL;
-
-	klist_iter_init(&parent->p->klist_children, &i);
-	while ((child = next_device(&i)))
Quoted 9 lines

drivers/base/core.c EXPORT_SYMBOL_GPL(device_find_child); · line 4092

Quoted 5 lines
drivers/base/core.c +1 -4
-		if (sysfs_streq(dev_name(child), name) && get_device(child))
-			break;
-	klist_iter_exit(&i);
-	return child;
+	/* TODO: remove type cast after const device_find_child() prototype */

I do not understand the TODO here. Why is it needed? Why not fix it up now?

i have below findings during trying to simplify this API.

there are a type of driver APIs for finding device, for example, (bus|driver|class)_find_device() which all take below type for parameter @match:
int (*match)(struct device *, const void *match_data)
but device_find_child() take below type with void * for @match_data:
int (*match)(struct device *, void *match_data).

Quoted 5 lines

@match's type of device_find_child() is not good as other finding APIs since nothing will be touched for finding operations normally.

i want to introduce a dedicate type for device match. typedef int (*device_match_t)(struct device *dev, const void *data);

Yes, that would be good.

Quoted 5 lines

advantages:
1) device_match_t is simpler for finding APIs declarations and definitions
2) maybe stop further driver APIs from using bad match type as device_find_child()

Quoted 8 lines

TODO:
1) introduce device_match_t and use it for current present APIs (bus|driver|class)_find_device()
2) change API device_find_child() to take device_match_t, more jobs to do since need to touch many drivers
3) correct this change by by remove all TODO inline comments and force cast.

not sure if my ideas is good, what is your opinions?

That's great, but evolve it properly, don't add TODO lines here, there's no real need for that. Should end up with a lot of good cleanup changes, and might not be all that bad overall.

thanks,

greg k-h

author
#4 lore raw
Quoted 69 lines · Greg Kroah-Hartman
Quoted 48 lines · Zijun Hu
Quoted 28 lines · Greg Kroah-Hartman
Quoted 27 lines · Zijun Hu

From: Zijun Hu <[email protected]>

Simplify device_find_child_by_name() implementation by using present driver APIs device_find_child() and device_match_name().

---
 drivers/base/core.c    | 15 +++------------
 include/linux/device.h |  4 ++++
 2 files changed, 7 insertions(+), 12 deletions(-)
drivers/base/core.c +0 -8
index 730cae66607c..22ab4b8a2bcd 100644
@@ -4089,18 +4089,9 @@ EXPORT_SYMBOL_GPL(device_find_child);
 struct device *device_find_child_by_name(struct device *parent,
 					 const char *name)
 {
-	struct klist_iter i;
-	struct device *child;
-
-	if (!parent)
-		return NULL;
-
-	klist_iter_init(&parent->p->klist_children, &i);
-	while ((child = next_device(&i)))
Quoted 9 lines

drivers/base/core.c EXPORT_SYMBOL_GPL(device_find_child); · line 4092

Quoted 5 lines
drivers/base/core.c +1 -4
-		if (sysfs_streq(dev_name(child), name) && get_device(child))
-			break;
-	klist_iter_exit(&i);
-	return child;
+	/* TODO: remove type cast after const device_find_child() prototype */

I do not understand the TODO here. Why is it needed? Why not fix it up now?

i have below findings during trying to simplify this API.

there are a type of driver APIs for finding device, for example, (bus|driver|class)_find_device() which all take below type for parameter @match:
int (*match)(struct device *, const void *match_data)
but device_find_child() take below type with void * for @match_data:
int (*match)(struct device *, void *match_data).

Quoted 5 lines

@match's type of device_find_child() is not good as other finding APIs since nothing will be touched for finding operations normally.

i want to introduce a dedicate type for device match. typedef int (*device_match_t)(struct device *dev, const void *data);

Yes, that would be good.

Quoted 12 lines

advantages:
1) device_match_t is simpler for finding APIs declarations and definitions
2) maybe stop further driver APIs from using bad match type as device_find_child()

TODO:
1) introduce device_match_t and use it for current present APIs (bus|driver|class)_find_device()
2) change API device_find_child() to take device_match_t, more jobs to do since need to touch many drivers
3) correct this change by by remove all TODO inline comments and force cast.

Quoted 6 lines
Quoted 1 line

not sure if my ideas is good, what is your opinions?

That's great, but evolve it properly, don't add TODO lines here, there's no real need for that. Should end up with a lot of good cleanup changes, and might not be all that bad overall.

okay, thanks for these good suggestions.

let me mark this patch as abandoned now since i would like to do it by another better way.

Quoted 3 lines

thanks,

greg k-h