| Message ID | 54G.ZqHm.6kfJQ4BsLim.1gwlr2@seznam.cz |
|---|---|
| State | Accepted |
| Delegated to: | Fabio Estevam |
| Headers | show |
| Series | net: fec_mxc: fix double free of shared MDIO bus | expand |
On Wed, Nov 04, 2026 at 10:39:14AM +0100, Tomas Paukrt wrote: >When two FEC instances share one MDIO bus without DM_MDIO, the bus >is allocated by the instance which owns the MDIO pins and the other >instance gets it from the eth-phy uclass. However, both instances >unregister and free the bus in fecmxc_remove(), which results in >a double free when all devices are removed before booting the OS. Hi Tomas, Could you explain a bit more about non-DM_MDIO being used here? I think DM MDIO should be adopted for a new board enablement. Thanks Peng > >Fix it by remembering whether the instance allocated the bus itself >and free the bus only in that case. > >Fixes: 6a895d039ba7 ("net: Update eQos driver and FEC driver to use eth phy interfaces") >Signed-off-by: Tomas Paukrt <tomaspaukrt@email.cz> >--- > drivers/net/fec_mxc.c | 9 +++++++-- > drivers/net/fec_mxc.h | 1 + > 2 files changed, 8 insertions(+), 2 deletions(-) > >diff --git a/drivers/net/fec_mxc.c b/drivers/net/fec_mxc.c >index 3d32bad0831..fdd304b67de 100644 >--- a/drivers/net/fec_mxc.c >+++ b/drivers/net/fec_mxc.c >@@ -1430,6 +1430,9 @@ static int fecmxc_probe(struct udevice *dev) > if (ret) > goto err_phy; > >+ /* only the instance which allocated a shared bus may free it */ >+ priv->bus_owner = !dm_mii_bus; >+ > return 0; > > err_phy: >@@ -1455,8 +1458,10 @@ static int fecmxc_remove(struct udevice *dev) > device_unbind(priv->mdio_bus); > } > #else >- mdio_unregister(priv->bus); >- mdio_free(priv->bus); >+ if (priv->bus_owner) { >+ mdio_unregister(priv->bus); >+ mdio_free(priv->bus); >+ } > #endif > > #ifdef CONFIG_DM_REGULATOR >diff --git a/drivers/net/fec_mxc.h b/drivers/net/fec_mxc.h >index 8e95b539cd6..c4f63418f71 100644 >--- a/drivers/net/fec_mxc.h >+++ b/drivers/net/fec_mxc.h >@@ -248,6 +248,7 @@ struct fec_priv { > uint8_t *tdb_ptr; > int dev_id; > struct mii_dev *bus; >+ bool bus_owner; > #ifdef CONFIG_PHYLIB > struct phy_device *phydev; > ofnode phy_of_node; >-- >2.43.0 >
> Hi Tomas, > > Could you explain a bit more about non-DM_MDIO being used here? > I think DM MDIO should be adopted for a new board enablement. > > Thanks > Peng Hi Peng, the non-DM_MDIO configuration is inherited from an older defconfig where DM_MDIO was not enabled. The board can use DM_MDIO, but the legacy non-DM_MDIO code path is still present and currently has a double-free issue when both FEC instances share the same MDIO bus. I agree that DM_MDIO should be preferred for new board enablement. However, I think the existing non-DM_MDIO path should also handle resource ownership correctly, rather than leaving a known issue in the legacy implementation. Best regards Tomas
When two FEC instances share one MDIO bus without DM_MDIO, the bus
is allocated by the instance which owns the MDIO pins and the other
instance gets it from the eth-phy uclass. However, both instances
unregister and free the bus in fecmxc_remove(), which results in
a double free when all devices are removed before booting the OS.
Fix it by remembering whether the instance allocated the bus itself
and free the bus only in that case.
Fixes: 6a895d039ba7 ("net: Update eQos driver and FEC driver to use eth phy interfaces")
Signed-off-by: Tomas Paukrt <tomaspaukrt@email.cz>
---
drivers/net/fec_mxc.c | 9 +++++++--
drivers/net/fec_mxc.h | 1 +
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/net/fec_mxc.c b/drivers/net/fec_mxc.c
index 3d32bad0831..fdd304b67de 100644
--- a/drivers/net/fec_mxc.c
+++ b/drivers/net/fec_mxc.c
@@ -1430,6 +1430,9 @@ static int fecmxc_probe(struct udevice *dev)
if (ret)
goto err_phy;
+ /* only the instance which allocated a shared bus may free it */
+ priv->bus_owner = !dm_mii_bus;
+
return 0;
err_phy:
@@ -1455,8 +1458,10 @@ static int fecmxc_remove(struct udevice *dev)
device_unbind(priv->mdio_bus);
}
#else
- mdio_unregister(priv->bus);
- mdio_free(priv->bus);
+ if (priv->bus_owner) {
+ mdio_unregister(priv->bus);
+ mdio_free(priv->bus);
+ }
#endif
#ifdef CONFIG_DM_REGULATOR
diff --git a/drivers/net/fec_mxc.h b/drivers/net/fec_mxc.h
index 8e95b539cd6..c4f63418f71 100644
--- a/drivers/net/fec_mxc.h
+++ b/drivers/net/fec_mxc.h
@@ -248,6 +248,7 @@ struct fec_priv {
uint8_t *tdb_ptr;
int dev_id;
struct mii_dev *bus;
+ bool bus_owner;
#ifdef CONFIG_PHYLIB
struct phy_device *phydev;
ofnode phy_of_node;
--
2.43.0