ARM Sunxi Platform Development
 help / color / mirror / Atom feed
* [PATCH -next 0/7] net: Simplified with scoped function
@ 2024-08-27  7:52 Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free() Jinjie Ruan
                   ` (7 more replies)
  0 siblings, 8 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

Simplify with scoped for each OF child loop and __free().

Jinjie Ruan (7):
  net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and
    __free()
  net: dsa: realtek: Use for_each_child_of_node_scoped() and __free()
  net: phy: Fix missing of_node_put() for leds
  net: mdio: mux-mmioreg: Simplified with scoped function and
    dev_err_probe()
  net: mv643xx_eth: Simplify with scoped for each OF child loop
  net: dsa: microchip: Use scoped function and __free() to simplfy code
  net: bcmasp: Simplify with scoped for each OF child loop

 drivers/net/dsa/microchip/ksz_common.c        | 12 ++---
 drivers/net/dsa/realtek/rtl8366rb.c           | 11 ++--
 drivers/net/ethernet/broadcom/asp2/bcmasp.c   | 18 +++----
 drivers/net/ethernet/marvell/mv643xx_eth.c    |  5 +-
 .../net/ethernet/stmicro/stmmac/dwmac-sun8i.c | 20 +++-----
 drivers/net/mdio/mdio-mux-mmioreg.c           | 51 ++++++++-----------
 drivers/net/phy/phy_device.c                  |  6 +--
 7 files changed, 46 insertions(+), 77 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free()
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
@ 2024-08-27  7:52 ` Jinjie Ruan
  2024-08-27 12:52   ` Andrew Lunn
  2024-08-27  7:52 ` [PATCH -next 2/7] net: dsa: realtek: " Jinjie Ruan
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

Avoid need to manually handle of_node_put() by using
for_each_child_of_node_scoped() and __free(), which can simplfy code.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 .../net/ethernet/stmicro/stmmac/dwmac-sun8i.c | 20 ++++++-------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
index cc93f73a380e..415a0d23b3a5 100644
--- a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
+++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
@@ -774,46 +774,38 @@ static int sun8i_dwmac_reset(struct stmmac_priv *priv)
 static int get_ephy_nodes(struct stmmac_priv *priv)
 {
 	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
-	struct device_node *mdio_mux, *iphynode;
-	struct device_node *mdio_internal;
 	int ret;
 
-	mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux");
+	struct device_node *mdio_mux __free(device_node) =
+		of_get_child_by_name(priv->device->of_node, "mdio-mux");
 	if (!mdio_mux) {
 		dev_err(priv->device, "Cannot get mdio-mux node\n");
 		return -ENODEV;
 	}
 
-	mdio_internal = of_get_compatible_child(mdio_mux,
-						"allwinner,sun8i-h3-mdio-internal");
-	of_node_put(mdio_mux);
+	struct device_node *mdio_internal __free(device_node) =
+		of_get_compatible_child(mdio_mux, "allwinner,sun8i-h3-mdio-internal");
 	if (!mdio_internal) {
 		dev_err(priv->device, "Cannot get internal_mdio node\n");
 		return -ENODEV;
 	}
 
 	/* Seek for internal PHY */
-	for_each_child_of_node(mdio_internal, iphynode) {
+	for_each_child_of_node_scoped(mdio_internal, iphynode) {
 		gmac->ephy_clk = of_clk_get(iphynode, 0);
 		if (IS_ERR(gmac->ephy_clk))
 			continue;
 		gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL);
 		if (IS_ERR(gmac->rst_ephy)) {
 			ret = PTR_ERR(gmac->rst_ephy);
-			if (ret == -EPROBE_DEFER) {
-				of_node_put(iphynode);
-				of_node_put(mdio_internal);
+			if (ret == -EPROBE_DEFER)
 				return ret;
-			}
 			continue;
 		}
 		dev_info(priv->device, "Found internal PHY node\n");
-		of_node_put(iphynode);
-		of_node_put(mdio_internal);
 		return 0;
 	}
 
-	of_node_put(mdio_internal);
 	return -ENODEV;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH -next 2/7] net: dsa: realtek: Use for_each_child_of_node_scoped() and __free()
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free() Jinjie Ruan
@ 2024-08-27  7:52 ` Jinjie Ruan
  2024-08-30 22:17   ` Linus Walleij
  2024-08-27  7:52 ` [PATCH -next 3/7] net: phy: Fix missing of_node_put() for leds Jinjie Ruan
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

Avoid need to manually handle of_node_put() by using
for_each_child_of_node_scoped() and __free(), which can simplfy code.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/net/dsa/realtek/rtl8366rb.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/net/dsa/realtek/rtl8366rb.c b/drivers/net/dsa/realtek/rtl8366rb.c
index 9e821b42e5f3..0acdcdd93ea2 100644
--- a/drivers/net/dsa/realtek/rtl8366rb.c
+++ b/drivers/net/dsa/realtek/rtl8366rb.c
@@ -1009,7 +1009,6 @@ static int rtl8366rb_setup_all_leds_off(struct realtek_priv *priv)
 
 static int rtl8366rb_setup_leds(struct realtek_priv *priv)
 {
-	struct device_node *leds_np, *led_np;
 	struct dsa_switch *ds = &priv->ds;
 	struct dsa_port *dp;
 	int ret = 0;
@@ -1018,23 +1017,21 @@ static int rtl8366rb_setup_leds(struct realtek_priv *priv)
 		if (!dp->dn)
 			continue;
 
-		leds_np = of_get_child_by_name(dp->dn, "leds");
+		struct device_node *leds_np __free(device_node) =
+			of_get_child_by_name(dp->dn, "leds");
 		if (!leds_np) {
 			dev_dbg(priv->dev, "No leds defined for port %d",
 				dp->index);
 			continue;
 		}
 
-		for_each_child_of_node(leds_np, led_np) {
+		for_each_child_of_node_scoped(leds_np, led_np) {
 			ret = rtl8366rb_setup_led(priv, dp,
 						  of_fwnode_handle(led_np));
-			if (ret) {
-				of_node_put(led_np);
+			if (ret)
 				break;
-			}
 		}
 
-		of_node_put(leds_np);
 		if (ret)
 			return ret;
 	}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH -next 3/7] net: phy: Fix missing of_node_put() for leds
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free() Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 2/7] net: dsa: realtek: " Jinjie Ruan
@ 2024-08-27  7:52 ` Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 4/7] net: mdio: mux-mmioreg: Simplified with scoped function and dev_err_probe() Jinjie Ruan
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

The call of of_get_child_by_name() will cause refcount incremented for
leds, if it succeeds, it should call of_node_put() to decrease it. Fix
it by using __free(). Also use for_each_available_child_of_node_scoped()
to simplfy it.

Fixes: 01e5b728e9e4 ("net: phy: Add a binding for PHY LEDs")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/net/phy/phy_device.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 8f5314c1fecc..2fca33ff79e7 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -3407,7 +3407,6 @@ static int of_phy_led(struct phy_device *phydev,
 static int of_phy_leds(struct phy_device *phydev)
 {
 	struct device_node *node = phydev->mdio.dev.of_node;
-	struct device_node *leds, *led;
 	int err;
 
 	if (!IS_ENABLED(CONFIG_OF_MDIO))
@@ -3416,14 +3415,13 @@ static int of_phy_leds(struct phy_device *phydev)
 	if (!node)
 		return 0;
 
-	leds = of_get_child_by_name(node, "leds");
+	struct device_node *leds __free(device_node) = of_get_child_by_name(node, "leds");
 	if (!leds)
 		return 0;
 
-	for_each_available_child_of_node(leds, led) {
+	for_each_available_child_of_node_scoped(leds, led) {
 		err = of_phy_led(phydev, led);
 		if (err) {
-			of_node_put(led);
 			phy_leds_unregister(phydev);
 			return err;
 		}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH -next 4/7] net: mdio: mux-mmioreg: Simplified with scoped function and dev_err_probe()
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
                   ` (2 preceding siblings ...)
  2024-08-27  7:52 ` [PATCH -next 3/7] net: phy: Fix missing of_node_put() for leds Jinjie Ruan
@ 2024-08-27  7:52 ` Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 5/7] net: mv643xx_eth: Simplify with scoped for each OF child loop Jinjie Ruan
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

Avoids the need for manual cleanup of_node_put() in early exits
from the loop by using for_each_available_child_of_node_scoped().

And use the dev_err_probe() helper to simplify error handling during probe.
This also handle scenario, when EDEFER is returned and useless error
is printed.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/net/mdio/mdio-mux-mmioreg.c | 51 ++++++++++++-----------------
 1 file changed, 21 insertions(+), 30 deletions(-)

diff --git a/drivers/net/mdio/mdio-mux-mmioreg.c b/drivers/net/mdio/mdio-mux-mmioreg.c
index de08419d0c98..08c484ccdcbe 100644
--- a/drivers/net/mdio/mdio-mux-mmioreg.c
+++ b/drivers/net/mdio/mdio-mux-mmioreg.c
@@ -96,7 +96,7 @@ static int mdio_mux_mmioreg_switch_fn(int current_child, int desired_child,
 
 static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
 {
-	struct device_node *np2, *np = pdev->dev.of_node;
+	struct device_node *np = pdev->dev.of_node;
 	struct mdio_mux_mmioreg_state *s;
 	struct resource res;
 	const __be32 *iprop;
@@ -109,52 +109,43 @@ static int mdio_mux_mmioreg_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	ret = of_address_to_resource(np, 0, &res);
-	if (ret) {
-		dev_err(&pdev->dev, "could not obtain memory map for node %pOF\n",
-			np);
-		return ret;
-	}
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret,
+				     "could not obtain memory map for node %pOF\n", np);
 	s->phys = res.start;
 
 	s->iosize = resource_size(&res);
 	if (s->iosize != sizeof(uint8_t) &&
 	    s->iosize != sizeof(uint16_t) &&
 	    s->iosize != sizeof(uint32_t)) {
-		dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
-		return -EINVAL;
+		return dev_err_probe(&pdev->dev, -EINVAL,
+				     "only 8/16/32-bit registers are supported\n");
 	}
 
 	iprop = of_get_property(np, "mux-mask", &len);
-	if (!iprop || len != sizeof(uint32_t)) {
-		dev_err(&pdev->dev, "missing or invalid mux-mask property\n");
-		return -ENODEV;
-	}
-	if (be32_to_cpup(iprop) >= BIT(s->iosize * 8)) {
-		dev_err(&pdev->dev, "only 8/16/32-bit registers are supported\n");
-		return -EINVAL;
-	}
+	if (!iprop || len != sizeof(uint32_t))
+		return dev_err_probe(&pdev->dev, -ENODEV,
+				     "missing or invalid mux-mask property\n");
+	if (be32_to_cpup(iprop) >= BIT(s->iosize * 8))
+		return dev_err_probe(&pdev->dev, -EINVAL,
+				     "only 8/16/32-bit registers are supported\n");
 	s->mask = be32_to_cpup(iprop);
 
 	/*
 	 * Verify that the 'reg' property of each child MDIO bus does not
 	 * set any bits outside of the 'mask'.
 	 */
-	for_each_available_child_of_node(np, np2) {
+	for_each_available_child_of_node_scoped(np, np2) {
 		u64 reg;
 
-		if (of_property_read_reg(np2, 0, &reg, NULL)) {
-			dev_err(&pdev->dev, "mdio-mux child node %pOF is "
-				"missing a 'reg' property\n", np2);
-			of_node_put(np2);
-			return -ENODEV;
-		}
-		if ((u32)reg & ~s->mask) {
-			dev_err(&pdev->dev, "mdio-mux child node %pOF has "
-				"a 'reg' value with unmasked bits\n",
-				np2);
-			of_node_put(np2);
-			return -ENODEV;
-		}
+		if (of_property_read_reg(np2, 0, &reg, NULL))
+			return dev_err_probe(&pdev->dev, -ENODEV,
+					     "mdio-mux child node %pOF is missing a 'reg' property\n",
+					     np2);
+		if ((u32)reg & ~s->mask)
+			return dev_err_probe(&pdev->dev, -ENODEV,
+					     "mdio-mux child node %pOF has a 'reg' value with unmasked bits\n",
+					     np2);
 	}
 
 	ret = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH -next 5/7] net: mv643xx_eth: Simplify with scoped for each OF child loop
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
                   ` (3 preceding siblings ...)
  2024-08-27  7:52 ` [PATCH -next 4/7] net: mdio: mux-mmioreg: Simplified with scoped function and dev_err_probe() Jinjie Ruan
@ 2024-08-27  7:52 ` Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 6/7] net: dsa: microchip: Use scoped function and __free() to simplfy code Jinjie Ruan
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

Use scoped for_each_available_child_of_node_scoped() when iterating
over device nodes to make code a bit simpler.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/net/ethernet/marvell/mv643xx_eth.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c b/drivers/net/ethernet/marvell/mv643xx_eth.c
index f35ae2c88091..9e80899546d9 100644
--- a/drivers/net/ethernet/marvell/mv643xx_eth.c
+++ b/drivers/net/ethernet/marvell/mv643xx_eth.c
@@ -2802,7 +2802,7 @@ static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev,
 static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
 {
 	struct mv643xx_eth_shared_platform_data *pd;
-	struct device_node *pnp, *np = pdev->dev.of_node;
+	struct device_node *np = pdev->dev.of_node;
 	int ret;
 
 	/* bail out if not registered from DT */
@@ -2816,10 +2816,9 @@ static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
 
 	mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit);
 
-	for_each_available_child_of_node(np, pnp) {
+	for_each_available_child_of_node_scoped(np, pnp) {
 		ret = mv643xx_eth_shared_of_add_port(pdev, pnp);
 		if (ret) {
-			of_node_put(pnp);
 			mv643xx_eth_shared_of_remove();
 			return ret;
 		}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH -next 6/7] net: dsa: microchip: Use scoped function and __free() to simplfy code
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
                   ` (4 preceding siblings ...)
  2024-08-27  7:52 ` [PATCH -next 5/7] net: mv643xx_eth: Simplify with scoped for each OF child loop Jinjie Ruan
@ 2024-08-27  7:52 ` Jinjie Ruan
  2024-08-27  7:52 ` [PATCH -next 7/7] net: bcmasp: Simplify with scoped for each OF child loop Jinjie Ruan
  2024-08-27 12:44 ` [PATCH -next 0/7] net: Simplified with scoped function Andrew Lunn
  7 siblings, 0 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

Avoids the need for manual cleanup of_node_put() in early exits
from the loop by using for_each_available_child_of_node_scoped()
and __free().

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/net/dsa/microchip/ksz_common.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c
index cd3991792b69..8058a0b7c161 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -4595,7 +4595,6 @@ static int ksz_parse_drive_strength(struct ksz_device *dev)
 int ksz_switch_register(struct ksz_device *dev)
 {
 	const struct ksz_chip_data *info;
-	struct device_node *port, *ports;
 	phy_interface_t interface;
 	unsigned int port_num;
 	int ret;
@@ -4677,25 +4676,22 @@ int ksz_switch_register(struct ksz_device *dev)
 		ret = of_get_phy_mode(dev->dev->of_node, &interface);
 		if (ret == 0)
 			dev->compat_interface = interface;
-		ports = of_get_child_by_name(dev->dev->of_node, "ethernet-ports");
+		struct device_node *ports __free(device_node) =
+			of_get_child_by_name(dev->dev->of_node, "ethernet-ports");
 		if (!ports)
 			ports = of_get_child_by_name(dev->dev->of_node, "ports");
 		if (ports) {
-			for_each_available_child_of_node(ports, port) {
+			for_each_available_child_of_node_scoped(ports, port) {
 				if (of_property_read_u32(port, "reg",
 							 &port_num))
 					continue;
-				if (!(dev->port_mask & BIT(port_num))) {
-					of_node_put(port);
-					of_node_put(ports);
+				if (!(dev->port_mask & BIT(port_num)))
 					return -EINVAL;
-				}
 				of_get_phy_mode(port,
 						&dev->ports[port_num].interface);
 
 				ksz_parse_rgmii_delay(dev, port_num, port);
 			}
-			of_node_put(ports);
 		}
 		dev->synclko_125 = of_property_read_bool(dev->dev->of_node,
 							 "microchip,synclko-125");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH -next 7/7] net: bcmasp: Simplify with scoped for each OF child loop
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
                   ` (5 preceding siblings ...)
  2024-08-27  7:52 ` [PATCH -next 6/7] net: dsa: microchip: Use scoped function and __free() to simplfy code Jinjie Ruan
@ 2024-08-27  7:52 ` Jinjie Ruan
  2024-08-27 12:44 ` [PATCH -next 0/7] net: Simplified with scoped function Andrew Lunn
  7 siblings, 0 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-27  7:52 UTC (permalink / raw)
  To: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23
  Cc: ruanjinjie

Use scoped for_each_available_child_of_node_scoped() and __free()
when iterating over device nodes to make code a bit simpler.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/net/ethernet/broadcom/asp2/bcmasp.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/asp2/bcmasp.c b/drivers/net/ethernet/broadcom/asp2/bcmasp.c
index 20c6529ec135..73e767aada2f 100644
--- a/drivers/net/ethernet/broadcom/asp2/bcmasp.c
+++ b/drivers/net/ethernet/broadcom/asp2/bcmasp.c
@@ -1300,7 +1300,6 @@ static void bcmasp_remove_intfs(struct bcmasp_priv *priv)
 
 static int bcmasp_probe(struct platform_device *pdev)
 {
-	struct device_node *ports_node, *intf_node;
 	const struct bcmasp_plat_data *pdata;
 	struct device *dev = &pdev->dev;
 	struct bcmasp_priv *priv;
@@ -1367,21 +1366,20 @@ static int bcmasp_probe(struct platform_device *pdev)
 	bcmasp_core_init(priv);
 	bcmasp_core_init_filters(priv);
 
-	ports_node = of_find_node_by_name(dev->of_node, "ethernet-ports");
+	struct device_node *ports_node __free(device_node) =
+		of_find_node_by_name(dev->of_node, "ethernet-ports");
 	if (!ports_node) {
 		dev_warn(dev, "No ports found\n");
 		return -EINVAL;
 	}
 
 	i = 0;
-	for_each_available_child_of_node(ports_node, intf_node) {
+	for_each_available_child_of_node_scoped(ports_node, intf_node) {
 		intf = bcmasp_interface_create(priv, intf_node, i);
 		if (!intf) {
-			dev_err(dev, "Cannot create eth interface %d\n", i);
 			bcmasp_remove_intfs(priv);
-			of_node_put(intf_node);
-			ret = -ENOMEM;
-			goto of_put_exit;
+			return dev_err_probe(dev, -ENOMEM,
+					     "Cannot create eth interface %d\n", i);
 		}
 		list_add_tail(&intf->list, &priv->intfs);
 		i++;
@@ -1407,16 +1405,14 @@ static int bcmasp_probe(struct platform_device *pdev)
 				   "failed to register net_device: %d\n", ret);
 			priv->destroy_wol(priv);
 			bcmasp_remove_intfs(priv);
-			goto of_put_exit;
+			return ret;
 		}
 		count++;
 	}
 
 	dev_info(dev, "Initialized %d port(s)\n", count);
 
-of_put_exit:
-	of_node_put(ports_node);
-	return ret;
+	return 0;
 }
 
 static void bcmasp_remove(struct platform_device *pdev)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH -next 0/7] net: Simplified with scoped function
  2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
                   ` (6 preceding siblings ...)
  2024-08-27  7:52 ` [PATCH -next 7/7] net: bcmasp: Simplify with scoped for each OF child loop Jinjie Ruan
@ 2024-08-27 12:44 ` Andrew Lunn
  2024-08-28  1:59   ` Jinjie Ruan
  7 siblings, 1 reply; 13+ messages in thread
From: Andrew Lunn @ 2024-08-27 12:44 UTC (permalink / raw)
  To: Jinjie Ruan
  Cc: woojung.huh, f.fainelli, olteanv, davem, edumazet, kuba, pabeni,
	linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23

On Tue, Aug 27, 2024 at 03:52:12PM +0800, Jinjie Ruan wrote:
> Simplify with scoped for each OF child loop and __free().

The Subject: should be [PATCH net-next], not -next. The CI looks at
this to decide which tree it belongs to. It issues a warning because
what you used does not match anything.

	Andrew

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free()
  2024-08-27  7:52 ` [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free() Jinjie Ruan
@ 2024-08-27 12:52   ` Andrew Lunn
  2024-08-28  1:59     ` Jinjie Ruan
  0 siblings, 1 reply; 13+ messages in thread
From: Andrew Lunn @ 2024-08-27 12:52 UTC (permalink / raw)
  To: Jinjie Ruan
  Cc: woojung.huh, f.fainelli, olteanv, davem, edumazet, kuba, pabeni,
	linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23

On Tue, Aug 27, 2024 at 03:52:13PM +0800, Jinjie Ruan wrote:
> Avoid need to manually handle of_node_put() by using
> for_each_child_of_node_scoped() and __free(), which can simplfy code.

Please could you split this in two. for_each_child_of_node_scoped() is
fine, it solves a common bug, forgetting to do a node_put() on a early
exit from the loop.

I personally find __free() ugly, and would prefer to reject those
changes.

	Andrew


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH -next 0/7] net: Simplified with scoped function
  2024-08-27 12:44 ` [PATCH -next 0/7] net: Simplified with scoped function Andrew Lunn
@ 2024-08-28  1:59   ` Jinjie Ruan
  0 siblings, 0 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-28  1:59 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: woojung.huh, f.fainelli, olteanv, davem, edumazet, kuba, pabeni,
	linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23



On 2024/8/27 20:44, Andrew Lunn wrote:
> On Tue, Aug 27, 2024 at 03:52:12PM +0800, Jinjie Ruan wrote:
>> Simplify with scoped for each OF child loop and __free().
> 
> The Subject: should be [PATCH net-next], not -next. The CI looks at
> this to decide which tree it belongs to. It issues a warning because
> what you used does not match anything.

Thank you! I'll fix it.

> 
> 	Andrew

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free()
  2024-08-27 12:52   ` Andrew Lunn
@ 2024-08-28  1:59     ` Jinjie Ruan
  0 siblings, 0 replies; 13+ messages in thread
From: Jinjie Ruan @ 2024-08-28  1:59 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: woojung.huh, f.fainelli, olteanv, davem, edumazet, kuba, pabeni,
	linus.walleij, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23



On 2024/8/27 20:52, Andrew Lunn wrote:
> On Tue, Aug 27, 2024 at 03:52:13PM +0800, Jinjie Ruan wrote:
>> Avoid need to manually handle of_node_put() by using
>> for_each_child_of_node_scoped() and __free(), which can simplfy code.
> 
> Please could you split this in two. for_each_child_of_node_scoped() is
> fine, it solves a common bug, forgetting to do a node_put() on a early
> exit from the loop.

ok, that is nice.

> 
> I personally find __free() ugly, and would prefer to reject those
> changes.
> 
> 	Andrew
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH -next 2/7] net: dsa: realtek: Use for_each_child_of_node_scoped() and __free()
  2024-08-27  7:52 ` [PATCH -next 2/7] net: dsa: realtek: " Jinjie Ruan
@ 2024-08-30 22:17   ` Linus Walleij
  0 siblings, 0 replies; 13+ messages in thread
From: Linus Walleij @ 2024-08-30 22:17 UTC (permalink / raw)
  To: Jinjie Ruan
  Cc: woojung.huh, andrew, f.fainelli, olteanv, davem, edumazet, kuba,
	pabeni, alsi, justin.chen, sebastian.hesselbarth,
	alexandre.torgue, joabreu, mcoquelin.stm32, wens, jernej.skrabec,
	samuel, hkallweit1, linux, ansuelsmth, UNGLinuxDriver, netdev,
	linux-kernel, bcm-kernel-feedback-list, linux-arm-kernel,
	linux-sunxi, linux-stm32, krzk, jic23

On Tue, Aug 27, 2024 at 9:45 AM Jinjie Ruan <ruanjinjie@huawei.com> wrote:

> Avoid need to manually handle of_node_put() by using
> for_each_child_of_node_scoped() and __free(), which can simplfy code.
>
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>

Neat!
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-08-30 22:17 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-27  7:52 [PATCH -next 0/7] net: Simplified with scoped function Jinjie Ruan
2024-08-27  7:52 ` [PATCH -next 1/7] net: stmmac: dwmac-sun8i: Use for_each_child_of_node_scoped() and __free() Jinjie Ruan
2024-08-27 12:52   ` Andrew Lunn
2024-08-28  1:59     ` Jinjie Ruan
2024-08-27  7:52 ` [PATCH -next 2/7] net: dsa: realtek: " Jinjie Ruan
2024-08-30 22:17   ` Linus Walleij
2024-08-27  7:52 ` [PATCH -next 3/7] net: phy: Fix missing of_node_put() for leds Jinjie Ruan
2024-08-27  7:52 ` [PATCH -next 4/7] net: mdio: mux-mmioreg: Simplified with scoped function and dev_err_probe() Jinjie Ruan
2024-08-27  7:52 ` [PATCH -next 5/7] net: mv643xx_eth: Simplify with scoped for each OF child loop Jinjie Ruan
2024-08-27  7:52 ` [PATCH -next 6/7] net: dsa: microchip: Use scoped function and __free() to simplfy code Jinjie Ruan
2024-08-27  7:52 ` [PATCH -next 7/7] net: bcmasp: Simplify with scoped for each OF child loop Jinjie Ruan
2024-08-27 12:44 ` [PATCH -next 0/7] net: Simplified with scoped function Andrew Lunn
2024-08-28  1:59   ` Jinjie Ruan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox