Skip to contentSkip to conversation

use device_for_each_child_node_scoped to access device child nodes

PATCH Applied Javier Carrasco 5 participants 12 messages Aug 1, 2024 – Aug 5, 2024

Subsystems (9)

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover use device_for_each_child_node_scoped to access device child nodes

This series removes accesses to the device fwnode to iterate over its own child nodes. Using the device_for_each_child_node macro provides direct access to the device child nodes, and given that in all cases they are only required within the loop, the scoped variant of the macro can be used.

It has been stated in previous discussions [1] that device_for_each_* should be used to access device child nodes, removing the need to access its internal fwnode, and restricting fwnode_for_each_* to traversing subnodes when required.

Note that device_for_each_* implies availability, which means that after this conversion, unavailable nodes will not be accessible within the loop. The affected drivers does not seem to have any reason to iterate over unavailable nodes, though. But if someone has a case where the affected drivers might require accessing unavailable nodes, please let me know.

---
Javier Carrasco (4):
coresight: cti: use device_* to iterate over device child nodes
iio: adc: ad7768-1: use device_* to iterate over device child nodes
iio: adc: xilinx-ams: use device_* to iterate over device child nodes
leds: as3645a: use device_* to iterate over device child nodes

 drivers/hwtracing/coresight/coresight-cti-platform.c | 10 +++-------
 drivers/iio/adc/ad7768-1.c                           |  5 +----
 drivers/iio/adc/xilinx-ams.c                         |  7 ++-----
 drivers/leds/flash/leds-as3645a.c                    |  8 +++-----
 4 files changed, 9 insertions(+), 21 deletions(-)

---
base-commit: cd19ac2f903276b820f5d0d89de0c896c27036ed
change-id: 20240725-device_child_node_access-442533910a6f

Best regards,

Signature · 1 line
Javier Carrasco <[email protected]>
#1 lore raw

PATCH 1/4 coresight: cti: use device_* to iterate over device child nodes

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that they are only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---
 drivers/hwtracing/coresight/coresight-cti-platform.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/hwtracing/coresight/coresight-cti-platform.c +3 -7
index ccef04f27f12..d0ae10bf6128 100644
@@ -416,20 +416,16 @@ static int cti_plat_create_impdef_connections(struct device *dev,
 					      struct cti_drvdata *drvdata)
 {
 	int rc = 0;
-	struct fwnode_handle *fwnode = dev_fwnode(dev);
-	struct fwnode_handle *child = NULL;
 
-	if (IS_ERR_OR_NULL(fwnode))
+	if (IS_ERR_OR_NULL(dev_fwnode(dev)))
 		return -EINVAL;
 
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(dev, child) {
 		if (cti_plat_node_name_eq(child, CTI_DT_CONNS))
-			rc = cti_plat_create_connection(dev, drvdata,
-							child);
+			rc = cti_plat_create_connection(dev, drvdata, child);
 		if (rc != 0)
 			break;
 	}
-	fwnode_handle_put(child);
 
 	return rc;
 }
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 1/4] coresight: cti: use device_* to iterate over device child nodes

Quoted 26 lines · Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that they are only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---
  drivers/hwtracing/coresight/coresight-cti-platform.c | 10 +++-------
  1 file changed, 3 insertions(+), 7 deletions(-)
drivers/hwtracing/coresight/coresight-cti-platform.c +1 -3
index ccef04f27f12..d0ae10bf6128 100644
@@ -416,20 +416,16 @@ static int cti_plat_create_impdef_connections(struct device *dev,
  					      struct cti_drvdata *drvdata)
  {
  	int rc = 0;
-	struct fwnode_handle *fwnode = dev_fwnode(dev);
-	struct fwnode_handle *child = NULL;
 
-	if (IS_ERR_OR_NULL(fwnode))
+	if (IS_ERR_OR_NULL(dev_fwnode(dev)))

drivers/hwtracing/coresight/coresight-cti-platform.c static int cti_plat_create_impdef_connections(struct device *dev, · line 427

Quoted 10 lines
drivers/hwtracing/coresight/coresight-cti-platform.c +2 -3
  		return -EINVAL;
 
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(dev, child) {
  		if (cti_plat_node_name_eq(child, CTI_DT_CONNS))
-			rc = cti_plat_create_connection(dev, drvdata,
-							child);
+			rc = cti_plat_create_connection(dev, drvdata, child);
  		if (rc != 0)
  			break;

Don't we need to fwnode_handle_put(child) here, since we removed the outer one ?

Suzuki

#3 lore raw

Re: [PATCH 1/4] coresight: cti: use device_* to iterate over device child nodes

Quoted 41 lines · Suzuki K Poulose
Quoted 26 lines · Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that they are only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---
  drivers/hwtracing/coresight/coresight-cti-platform.c | 10 +++-------

  1 file changed, 3 insertions(+), 7 deletions(-)
(quoted diff) renamed from drivers/hwtracing/coresight/coresight-cti-platform.c +0 -0

drivers/hwtracing/coresight/coresight-cti-platform.c index ccef04f27f12..d0ae10bf6128 100644

drivers/hwtracing/coresight/coresight-cti-platform.c +0 -2
@@ -416,20 +416,16 @@ static int
 cti_plat_create_impdef_connections(struct device *dev,
                             struct cti_drvdata *drvdata)
   {
       int rc = 0;
-    struct fwnode_handle *fwnode = dev_fwnode(dev);
-    struct fwnode_handle *child = NULL;
   -    if (IS_ERR_OR_NULL(fwnode))

drivers/hwtracing/coresight/coresight-cti-platform.c static int · line 428

Quoted 10 lines
drivers/hwtracing/coresight/coresight-cti-platform.c +3 -2
+    if (IS_ERR_OR_NULL(dev_fwnode(dev)))
           return -EINVAL;
   -    fwnode_for_each_child_node(fwnode, child) {
+    device_for_each_child_node_scoped(dev, child) {
           if (cti_plat_node_name_eq(child, CTI_DT_CONNS))
-            rc = cti_plat_create_connection(dev, drvdata,
-                            child);
+            rc = cti_plat_create_connection(dev, drvdata, child);
           if (rc != 0)
               break;

Don't we need to fwnode_handle_put(child) here, since we removed the outer one ?

Quoted 2 lines

Suzuki

Hi Suzuki,

No, we don't need fwnode_handle_put(child) anymore because the scoped variant of the macro is used.

Best regards,
Javier Carrasco

maintainer
#4 lore raw

Re: [PATCH 1/4] coresight: cti: use device_* to iterate over device child nodes

Quoted 42 lines · Javier Carrasco
Quoted 41 lines · Suzuki K Poulose
Quoted 26 lines · Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that they are only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---
   drivers/hwtracing/coresight/coresight-cti-platform.c | 10 +++-------
   1 file changed, 3 insertions(+), 7 deletions(-)
(quoted diff) renamed from drivers/hwtracing/coresight/coresight-cti-platform.c +0 -0

drivers/hwtracing/coresight/coresight-cti-platform.c index ccef04f27f12..d0ae10bf6128 100644

drivers/hwtracing/coresight/coresight-cti-platform.c +0 -2
@@ -416,20 +416,16 @@ static int
 cti_plat_create_impdef_connections(struct device *dev,
                             struct cti_drvdata *drvdata)
   {
       int rc = 0;
-    struct fwnode_handle *fwnode = dev_fwnode(dev);
-    struct fwnode_handle *child = NULL;
   -    if (IS_ERR_OR_NULL(fwnode))

drivers/hwtracing/coresight/coresight-cti-platform.c static int · line 428

Quoted 10 lines
drivers/hwtracing/coresight/coresight-cti-platform.c +3 -2
+    if (IS_ERR_OR_NULL(dev_fwnode(dev)))
           return -EINVAL;
   -    fwnode_for_each_child_node(fwnode, child) {
+    device_for_each_child_node_scoped(dev, child) {
           if (cti_plat_node_name_eq(child, CTI_DT_CONNS))
-            rc = cti_plat_create_connection(dev, drvdata,
-                            child);
+            rc = cti_plat_create_connection(dev, drvdata, child);
           if (rc != 0)
               break;

Don't we need to fwnode_handle_put(child) here, since we removed the outer one ?

Quoted 7 lines
Quoted 2 lines

Suzuki

Hi Suzuki,

No, we don't need fwnode_handle_put(child) anymore because the scoped variant of the macro is used.

Ah, apologies, was looking at the non-scoped version. I will queue this.

Suzuki

Quoted 3 lines

Best regards,
Javier Carrasco

#5 lore raw

PATCH 2/4 iio: adc: ad7768-1: use device_* to iterate over device child nodes

In reply to Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that they are only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---
 drivers/iio/adc/ad7768-1.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/adc/ad7768-1.c +1 -4
index 70a25949142c..721672fe84ab 100644
@@ -544,13 +544,10 @@ static int ad7768_set_channel_label(struct iio_dev *indio_dev,
 {
 	struct ad7768_state *st = iio_priv(indio_dev);
 	struct device *device = indio_dev->dev.parent;
-	struct fwnode_handle *fwnode;
-	struct fwnode_handle *child;
 	const char *label;
 	int crt_ch = 0;
 
-	fwnode = dev_fwnode(device);
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(device, child) {
 		if (fwnode_property_read_u32(child, "reg", &crt_ch))
 			continue;
 
Signature · 1 line
2.43.0
Reviewed-by Nuno Sa on v1 2/4
#6 lore raw

Re: [PATCH 2/4] iio: adc: ad7768-1: use device_* to iterate over device child nodes

Quoted 10 lines · Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that they are only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---

Quoted 24 lines

 drivers/iio/adc/ad7768-1.c | 5 +----

 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/adc/ad7768-1.c +0 -2
index 70a25949142c..721672fe84ab 100644
@@ -544,13 +544,10 @@ static int ad7768_set_channel_label(struct iio_dev
 *indio_dev,
  {
  	struct ad7768_state *st = iio_priv(indio_dev);
  	struct device *device = indio_dev->dev.parent;
-	struct fwnode_handle *fwnode;
-	struct fwnode_handle *child;
  	const char *label;
  	int crt_ch = 0;

drivers/iio/adc/ad7768-1.c +1 -2
-	fwnode = dev_fwnode(device);
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(device, child) {

  if (fwnode_property_read_u32(child, "reg", &crt_ch)) continue;

maintainer Applied
#7 lore raw

Re: [PATCH 2/4] iio: adc: ad7768-1: use device_* to iterate over device child nodes

Quoted 6 lines · On Thu, 01 Aug 2024 09:31:16 +0200 Nuno Sá <[email protected]> wrote:
Quoted 5 lines · Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that they are only required within the loop, the scoped variant of the macro can be used.

Quoted 7 lines
Quoted 5 lines

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---

Applied

Quoted 26 lines
Quoted 25 lines

 drivers/iio/adc/ad7768-1.c | 5 +----

 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/adc/ad7768-1.c +0 -2
index 70a25949142c..721672fe84ab 100644
@@ -544,13 +544,10 @@ static int ad7768_set_channel_label(struct iio_dev
 *indio_dev,
  {
  	struct ad7768_state *st = iio_priv(indio_dev);
  	struct device *device = indio_dev->dev.parent;
-	struct fwnode_handle *fwnode;
-	struct fwnode_handle *child;
  	const char *label;
  	int crt_ch = 0;

drivers/iio/adc/ad7768-1.c +1 -2
-	fwnode = dev_fwnode(device);
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(device, child) {

  if (fwnode_property_read_u32(child, "reg", &crt_ch)) continue;

#8 lore raw

PATCH 3/4 iio: adc: xilinx-ams: use device_* to iterate over device child nodes

In reply to Javier Carrasco

Use device_for_each_child_node_scoped() in ams_parse_firmware() to explicitly state device child node access, and simplify the child node handling as it is not required outside the loop.

---
 drivers/iio/adc/xilinx-ams.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/iio/adc/xilinx-ams.c +2 -5
index f051358d6b50..27d5e4a6f9d6 100644
@@ -1275,7 +1275,6 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 	struct ams *ams = iio_priv(indio_dev);
 	struct iio_chan_spec *ams_channels, *dev_channels;
 	struct device *dev = indio_dev->dev.parent;
-	struct fwnode_handle *child = NULL;
 	struct fwnode_handle *fwnode = dev_fwnode(dev);
 	size_t ams_size;
 	int ret, ch_cnt = 0, i, rising_off, falling_off;
@@ -1297,13 +1296,11 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 		num_channels += ret;
 	}
 
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(dev, child) {
 		if (fwnode_device_is_available(child)) {
 			ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
-			if (ret < 0) {
-				fwnode_handle_put(child);
+			if (ret < 0)
 				return ret;
-			}
 
 			num_channels += ret;
 		}
Signature · 1 line
2.43.0
maintainer
#9 lore raw

Re: [PATCH 3/4] iio: adc: xilinx-ams: use device_* to iterate over device child nodes

Quoted 22 lines · On Thu, 01 Aug 2024 08:13:52 +0200 Javier Carrasco…

Use device_for_each_child_node_scoped() in ams_parse_firmware() to explicitly state device child node access, and simplify the child node handling as it is not required outside the loop.

---
 drivers/iio/adc/xilinx-ams.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/iio/adc/xilinx-ams.c +0 -1
index f051358d6b50..27d5e4a6f9d6 100644
@@ -1275,7 +1275,6 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 	struct ams *ams = iio_priv(indio_dev);
 	struct iio_chan_spec *ams_channels, *dev_channels;
 	struct device *dev = indio_dev->dev.parent;
-	struct fwnode_handle *child = NULL;
 	struct fwnode_handle *fwnode = dev_fwnode(dev);
 	size_t ams_size;
 	int ret, ch_cnt = 0, i, rising_off, falling_off;

drivers/iio/adc/xilinx-ams.c static int ams_parse_firmware(struct iio_dev *indio_dev) · line 1300

Quoted 6 lines
drivers/iio/adc/xilinx-ams.c +1 -1
@@ -1297,13 +1296,11 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 		num_channels += ret;
 	}
 
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(dev, child) {
 		if (fwnode_device_is_available(child)) {

This is fun. should have been fwnode_for_each_available_child_node() in the first place rather than iterating over all nodes, and only doing stuff for the available ones.

Can use the device_for_each_child_node_scoped() but that only includes available nodes anyway, so my understanding is that we can also drop this if statement as it will always be true.

Quoted 10 lines
drivers/iio/adc/xilinx-ams.c +1 -3
 			ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
-			if (ret < 0) {
-				fwnode_handle_put(child);
+			if (ret < 0)
 				return ret;
-			}
 
 			num_channels += ret;
 		}
#10 lore raw

PATCH 4/4 leds: as3645a: use device_* to iterate over device child nodes

In reply to Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that the child variable is only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---
 drivers/leds/flash/leds-as3645a.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/leds/flash/leds-as3645a.c +3 -5
index 2c6ef321b7c8..8e6abedf6e00 100644
@@ -478,14 +478,12 @@ static int as3645a_detect(struct as3645a *flash)
 	return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
 }
 
-static int as3645a_parse_node(struct as3645a *flash,
-			      struct fwnode_handle *fwnode)
+static int as3645a_parse_node(struct as3645a *flash, struct device *dev)
 {
 	struct as3645a_config *cfg = &flash->cfg;
-	struct fwnode_handle *child;
 	int rval;
 
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(dev, child) {
 		u32 id = 0;
 
 		fwnode_property_read_u32(child, "reg", &id);
@@ -686,7 +684,7 @@ static int as3645a_probe(struct i2c_client *client)
 
 	flash->client = client;
 
-	rval = as3645a_parse_node(flash, dev_fwnode(&client->dev));
+	rval = as3645a_parse_node(flash, &client->dev);
 	if (rval < 0)
 		return rval;
 
Signature · 1 line
2.43.0
Acked-by on v1 4/4
#11 lore raw

Re: [PATCH 4/4] leds: as3645a: use device_* to iterate over device child nodes

Quoted 9 lines · Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that the child variable is only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

Thanks!

Signature · 1 line
Sakari Ailus