Skip to contentSkip to conversation

driver core: Introduce device_find_any_child() helper

PATCH In review Andy Shevchenko 4 participants 5 messages Jun 10, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 driver core: Introduce device_find_any_child() helper

There are several places in the kernel where this kind of functionality is being used. Provide a generic helper for such cases.

---
v2: renamed method (Greg), refactored it (Rafael)

 drivers/base/core.c    | 20 ++++++++++++++++++++
 include/linux/device.h |  2 ++
 2 files changed, 22 insertions(+)
drivers/base/core.c +20 -0
index 7cd789c4985d..a519307fda60 100644
@@ -3832,6 +3832,26 @@ struct device *device_find_child_by_name(struct device *parent,
 }
 EXPORT_SYMBOL_GPL(device_find_child_by_name);
 
+static int match_any(struct device *dev, void *unused)
+{
+	return 1;
+}
+
+/**
+ * device_find_any_child - device iterator for locating a child device, if any.
+ * @parent: parent struct device
+ *
+ * This is similar to the device_find_child() function above, but it
+ * returns a reference to a child device, if any.
+ *
+ * NOTE: you will need to drop the reference with put_device() after use.
+ */
+struct device *device_find_any_child(struct device *parent)
+{
+	return device_find_child(parent, NULL, match_any);
+}
+EXPORT_SYMBOL_GPL(device_find_any_child);
+
 int __init devices_init(void)
 {
 	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
include/linux/device.h +2 -0
index dc941997795c..424b55df0272 100644
@@ -905,6 +905,8 @@ 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,
 					 const char *name);
+struct device *device_find_any_child(struct device *parent);
+
 int device_rename(struct device *dev, const char *new_name);
 int device_move(struct device *dev, struct device *new_parent,
 		enum dpm_order dpm_order);
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 2/2 spi: Use device_find_any_child() instead of custom approach

We have already a helper to get the first child device, use it and drop custom approach.

---
v2: added tag (Rafael)

 drivers/spi/spi.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/spi/spi.c +2 -7
index ea09d1b42bf6..b04be04dddfa 100644
@@ -2613,11 +2613,6 @@ int spi_slave_abort(struct spi_device *spi)
 }
 EXPORT_SYMBOL_GPL(spi_slave_abort);
 
-static int match_true(struct device *dev, void *data)
-{
-	return 1;
-}
-
 static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
 			  char *buf)
 {
@@ -2625,7 +2620,7 @@ static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
 						   dev);
 	struct device *child;
 
-	child = device_find_child(&ctlr->dev, NULL, match_true);
+	child = device_find_any_child(&ctlr->dev);
 	return sprintf(buf, "%s\n",
 		       child ? to_spi_device(child)->modalias : NULL);
 }
@@ -2644,7 +2639,7 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
 	if (rc != 1 || !name[0])
 		return -EINVAL;
 
-	child = device_find_child(&ctlr->dev, NULL, match_true);
+	child = device_find_any_child(&ctlr->dev);
 	if (child) {
 		/* Remove registered slave */
 		device_unregister(child);
Signature · 1 line
2.35.1
maintainer Acked-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] spi: Use device_find_any_child() instead of custom approach

Quoted 2 lines · Andy Shevchenko

We have already a helper to get the first child device, use it and drop custom approach.

maintainer Reviewed-by on v2 1/2
#3 lore raw
In reply to Andy Shevchenko
Quoted 28 lines · Andy Shevchenko

There are several places in the kernel where this kind of functionality is being used. Provide a generic helper for such cases.

---
v2: renamed method (Greg), refactored it (Rafael)

 drivers/base/core.c    | 20 ++++++++++++++++++++
 include/linux/device.h |  2 ++
 2 files changed, 22 insertions(+)
drivers/base/core.c +9 -0
index 7cd789c4985d..a519307fda60 100644
@@ -3832,6 +3832,26 @@ struct device *device_find_child_by_name(struct device *parent,
 }
 EXPORT_SYMBOL_GPL(device_find_child_by_name);
 
+static int match_any(struct device *dev, void *unused)
+{
+       return 1;
+}
+
+/**
+ * device_find_any_child - device iterator for locating a child device, if any.
+ * @parent: parent struct device
+ *

drivers/base/core.c struct device *device_find_child_by_name(struct device *parent, · line 3853

Quoted 10 lines
drivers/base/core.c +10 -0
+ * This is similar to the device_find_child() function above, but it
+ * returns a reference to a child device, if any.
+ *
+ * NOTE: you will need to drop the reference with put_device() after use.
+ */
+struct device *device_find_any_child(struct device *parent)
+{
+       return device_find_child(parent, NULL, match_any);
+}
+EXPORT_SYMBOL_GPL(device_find_any_child);

I would call it device_get_any_child() to indicate the reference counting, but that's a very minor nit.

Regardless of the above

Quoted 20 lines
drivers/base/core.c +1 -0
+
 int __init devices_init(void)
 {
        devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
include/linux/device.h +2 -1
index dc941997795c..424b55df0272 100644
@@ -905,6 +905,8 @@ 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,
                                         const char *name);
+struct device *device_find_any_child(struct device *parent);
+
 int device_rename(struct device *dev, const char *new_name);
 int device_move(struct device *dev, struct device *new_parent,
                enum dpm_order dpm_order);
--

2.35.1