Skip to contentSkip to conversation

net: dsa: microchip: fix mdio parent bus reference leak

PATCH In review Ma Ke 3 participants 3 messages Nov 21, 2025 – Nov 23, 2025

Subsystems (5)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH 1/1 net: dsa: microchip: fix mdio parent bus reference leak

In ksz_mdio_register(), when of_mdio_find_bus() is called to get the parent MDIO bus, it increments the reference count of the underlying device. However, the reference are not released in error paths or during switch teardown, causing a reference leak.

Add put_device() in the error path of ksz_mdio_register() and ksz_teardown() to release the parent bus.

Found by code review.

Fixes 9afaf0eec2ab ("net: dsa: microchip: Refactor MDIO handling for side MDIO access")
---
 drivers/net/dsa/microchip/ksz_common.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/net/dsa/microchip/ksz_common.c +10 -0
index 933ae8dc6337..49c0420a6df8 100644
@@ -2795,6 +2795,11 @@ static int ksz_mdio_register(struct ksz_device *dev)
 	}
 
 put_mdio_node:
+	if (ret && dev->parent_mdio_bus) {
+		put_device(&dev->parent_mdio_bus->dev);
+		dev->parent_mdio_bus = NULL;
+	}
+
 	of_node_put(mdio_np);
 	of_node_put(parent_bus_node);
 
@@ -3110,6 +3115,11 @@ static void ksz_teardown(struct dsa_switch *ds)
 		ksz_irq_free(&dev->girq);
 	}
 
+	if (dev->parent_mdio_bus) {
+		put_device(&dev->parent_mdio_bus->dev);
+		dev->parent_mdio_bus = NULL;
+	}
+
 	if (dev->dev_ops->teardown)
 		dev->dev_ops->teardown(ds);
 }
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 5 lines · Ma Ke

In ksz_mdio_register(), when of_mdio_find_bus() is called to get the parent MDIO bus, it increments the reference count of the underlying device. However, the reference are not released in error paths or during switch teardown, causing a reference leak.

Quoted 8 lines

Add put_device() in the error path of ksz_mdio_register() and ksz_teardown() to release the parent bus.

Found by code review.

Fixes 9afaf0eec2ab ("net: dsa: microchip: Refactor MDIO handling for side MDIO access")

Thank you!

Best Regards,
Oleksij

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
maintainer
#2 lore raw
In reply to Ma Ke
Quoted 48 lines · Ma Ke

In ksz_mdio_register(), when of_mdio_find_bus() is called to get the parent MDIO bus, it increments the reference count of the underlying device. However, the reference are not released in error paths or during switch teardown, causing a reference leak.

Add put_device() in the error path of ksz_mdio_register() and ksz_teardown() to release the parent bus.

Found by code review.

Fixes 9afaf0eec2ab ("net: dsa: microchip: Refactor MDIO handling for side MDIO access")
---
 drivers/net/dsa/microchip/ksz_common.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/net/dsa/microchip/ksz_common.c +10 -0
index 933ae8dc6337..49c0420a6df8 100644
@@ -2795,6 +2795,11 @@ static int ksz_mdio_register(struct ksz_device *dev)
 	}
 
 put_mdio_node:
+	if (ret && dev->parent_mdio_bus) {
+		put_device(&dev->parent_mdio_bus->dev);
+		dev->parent_mdio_bus = NULL;
+	}
+
 	of_node_put(mdio_np);
 	of_node_put(parent_bus_node);
 
@@ -3110,6 +3115,11 @@ static void ksz_teardown(struct dsa_switch *ds)
 		ksz_irq_free(&dev->girq);
 	}
 
+	if (dev->parent_mdio_bus) {
+		put_device(&dev->parent_mdio_bus->dev);
+		dev->parent_mdio_bus = NULL;
+	}
+
 	if (dev->dev_ops->teardown)
 		dev->dev_ops->teardown(ds);
 }
Signature · 1 line
2.17.1

Thank you for the patch.

I see 2 problems:

1. I'm not sure that releasing the reference to the parent_mdio_bus device is compatible with devres, since the MDIO bus created by ksz_mdio_register() will continue to exist after we release the parent_mdio_bus reference. I think it would be better to remove devres, introduce ksz_mdio_unregister(), and only release the reference once our MDIO bus is unregistered.

2. Error path handling is incomplete:

	ret = ksz_mdio_register(dev);
	if (ret < 0) {
		dev_err(dev->dev, "failed to register the mdio");
		goto out_ptp_clock_unregister;
	}

	ret = ksz_dcb_init(dev);
	if (ret)
		goto out_ptp_clock_unregister; // needs to call ksz_mdio_unregister()