All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register}
@ 2022-02-07 16:15 Vladimir Oltean
  2022-02-07 16:15 ` [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus Vladimir Oltean
                   ` (7 more replies)
  0 siblings, 8 replies; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

The initial patch series "[net,0/2] Fix mdiobus users with devres"
https://patchwork.kernel.org/project/netdevbpf/cover/20210920214209.1733768-1-vladimir.oltean@nxp.com/
fixed some instances where DSA drivers on slow buses (SPI, I2C) trigger
a panic (changed since then to a warn) in mdiobus_free. That was due to
devres calling mdiobus_free() with no prior mdiobus_unregister(), which
again was due to commit ac3a68d56651 ("net: phy: don't abuse devres in
devm_mdiobus_register()") by Bartosz Golaszewski.

Rafael Richter and Daniel Klauer report yet another variation on that
theme, but this time it applies to any DSA switch driver, not just those
on buses which have a "->shutdown() calls ->remove() which unregisters
children" sequence.

Their setup is that of an LX2160A DPAA2 SoC driving a Marvell DSA switch
(MDIO). DPAA2 Ethernet drivers probe on the "fsl-mc" bus
(drivers/bus/fsl-mc/fsl-mc-bus.c). This bus is meant to be the
kernel-side representation of the networking objects kept by the
Management Complex (MC) firmware.

The fsl-mc bus driver has this pattern:

static void fsl_mc_bus_shutdown(struct platform_device *pdev)
{
	fsl_mc_bus_remove(pdev);
}

which proceeds to remove the children on the bus. Among those children,
the dpaa2-eth network driver.

When dpaa2-eth is a DSA master, this removal of the master on shutdown
trips up the device link created by dsa_master_setup(), and as such, the
Marvell switch is also removed.

From this point on, readers can revisit the description of commits
74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")

since the prerequisites for the BUG_ON in mdiobus_free() have been
accomplished if there is a devres mismatch between mdiobus_alloc() and
mdiobus_register().

Most DSA drivers have this kind of mismatch, and upon my initial
assessment I had not realized the possibility described above, so I
didn't fix it. This patch series walks through all drivers and makes
them use either fully devres, or no devres.

I am aware that there are DSA drivers that are only known to be tested
with a single DSA master, so some patches are probably overkill for
them. But code is copy-pasted from so many sources without fully
understanding the differences, that I think it's better to not leave an
in-tree source of inspiration that may lead to subtle breakage if not
adapted properly.

Vladimir Oltean (7):
  net: dsa: mv88e6xxx: don't use devres for mdiobus
  net: dsa: ar9331: register the mdiobus under devres
  net: dsa: bcm_sf2: don't use devres for mdiobus
  net: dsa: felix: don't use devres for mdiobus
  net: dsa: seville: register the mdiobus under devres
  net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding
  net: dsa: lantiq_gswip: don't use devres for mdiobus

 drivers/net/dsa/bcm_sf2.c                |  7 +++++--
 drivers/net/dsa/lantiq_gswip.c           | 14 +++++++++++---
 drivers/net/dsa/mt7530.c                 |  2 +-
 drivers/net/dsa/mv88e6xxx/chip.c         | 11 ++++++++---
 drivers/net/dsa/ocelot/felix_vsc9959.c   |  4 +++-
 drivers/net/dsa/ocelot/seville_vsc9953.c |  5 +++--
 drivers/net/dsa/qca/ar9331.c             |  3 +--
 7 files changed, 32 insertions(+), 14 deletions(-)

-- 
2.25.1


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

* [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
@ 2022-02-07 16:15 ` Vladimir Oltean
  2022-02-07 17:38   ` Daniel Klauer
                     ` (2 more replies)
  2022-02-07 16:15 ` [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres Vladimir Oltean
                   ` (6 subsequent siblings)
  7 siblings, 3 replies; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer, Rafael Richter

As explained in commits:
74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")

mdiobus_free() will panic when called from devm_mdiobus_free() <-
devres_release_all() <- __device_release_driver(), and that mdiobus was
not previously unregistered.

The mv88e6xxx is an MDIO device, so the initial set of constraints that
I thought would cause this (I2C or SPI buses which call ->remove on
->shutdown) do not apply. But there is one more which applies here.

If the DSA master itself is on a bus that calls ->remove from ->shutdown
(like dpaa2-eth, which is on the fsl-mc bus), there is a device link
between the switch and the DSA master, and device_links_unbind_consumers()
will unbind the Marvell switch driver on shutdown.

systemd-shutdown[1]: Powering off.
mv88e6085 0x0000000008b96000:00 sw_gl0: Link is Down
fsl-mc dpbp.9: Removing from iommu group 7
fsl-mc dpbp.8: Removing from iommu group 7
------------[ cut here ]------------
kernel BUG at drivers/net/phy/mdio_bus.c:677!
Internal error: Oops - BUG: 0 [#1] PREEMPT SMP
Modules linked in:
CPU: 0 PID: 1 Comm: systemd-shutdow Not tainted 5.16.5-00040-gdc05f73788e5 #15
pc : mdiobus_free+0x44/0x50
lr : devm_mdiobus_free+0x10/0x20
Call trace:
 mdiobus_free+0x44/0x50
 devm_mdiobus_free+0x10/0x20
 devres_release_all+0xa0/0x100
 __device_release_driver+0x190/0x220
 device_release_driver_internal+0xac/0xb0
 device_links_unbind_consumers+0xd4/0x100
 __device_release_driver+0x4c/0x220
 device_release_driver_internal+0xac/0xb0
 device_links_unbind_consumers+0xd4/0x100
 __device_release_driver+0x94/0x220
 device_release_driver+0x28/0x40
 bus_remove_device+0x118/0x124
 device_del+0x174/0x420
 fsl_mc_device_remove+0x24/0x40
 __fsl_mc_device_remove+0xc/0x20
 device_for_each_child+0x58/0xa0
 dprc_remove+0x90/0xb0
 fsl_mc_driver_remove+0x20/0x5c
 __device_release_driver+0x21c/0x220
 device_release_driver+0x28/0x40
 bus_remove_device+0x118/0x124
 device_del+0x174/0x420
 fsl_mc_bus_remove+0x80/0x100
 fsl_mc_bus_shutdown+0xc/0x1c
 platform_shutdown+0x20/0x30
 device_shutdown+0x154/0x330
 kernel_power_off+0x34/0x6c
 __do_sys_reboot+0x15c/0x250
 __arm64_sys_reboot+0x20/0x30
 invoke_syscall.constprop.0+0x4c/0xe0
 do_el0_svc+0x4c/0x150
 el0_svc+0x24/0xb0
 el0t_64_sync_handler+0xa8/0xb0
 el0t_64_sync+0x178/0x17c

So the same treatment must be applied to all DSA switch drivers, which
is: either use devres for both the mdiobus allocation and registration,
or don't use devres at all.

The Marvell driver already has a good structure for mdiobus removal, so
just plug in mdiobus_free and get rid of devres.

Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
Reported-by: Rafael Richter <Rafael.Richter@gin.de>
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 drivers/net/dsa/mv88e6xxx/chip.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
index 7670796c2aa1..8986dafb892a 100644
--- a/drivers/net/dsa/mv88e6xxx/chip.c
+++ b/drivers/net/dsa/mv88e6xxx/chip.c
@@ -3566,7 +3566,7 @@ static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
 			return err;
 	}
 
-	bus = devm_mdiobus_alloc_size(chip->dev, sizeof(*mdio_bus));
+	bus = mdiobus_alloc_size(sizeof(*mdio_bus));
 	if (!bus)
 		return -ENOMEM;
 
@@ -3591,14 +3591,14 @@ static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
 	if (!external) {
 		err = mv88e6xxx_g2_irq_mdio_setup(chip, bus);
 		if (err)
-			return err;
+			goto out;
 	}
 
 	err = of_mdiobus_register(bus, np);
 	if (err) {
 		dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
 		mv88e6xxx_g2_irq_mdio_free(chip, bus);
-		return err;
+		goto out;
 	}
 
 	if (external)
@@ -3607,6 +3607,10 @@ static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
 		list_add(&mdio_bus->list, &chip->mdios);
 
 	return 0;
+
+out:
+	mdiobus_free(bus);
+	return err;
 }
 
 static void mv88e6xxx_mdios_unregister(struct mv88e6xxx_chip *chip)
@@ -3622,6 +3626,7 @@ static void mv88e6xxx_mdios_unregister(struct mv88e6xxx_chip *chip)
 			mv88e6xxx_g2_irq_mdio_free(chip, bus);
 
 		mdiobus_unregister(bus);
+		mdiobus_free(bus);
 	}
 }
 
-- 
2.25.1


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

* [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
  2022-02-07 16:15 ` [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus Vladimir Oltean
@ 2022-02-07 16:15 ` Vladimir Oltean
  2022-02-08  2:46   ` Florian Fainelli
  2022-02-08  7:20   ` Oleksij Rempel
  2022-02-07 16:15 ` [PATCH net 3/7] net: dsa: bcm_sf2: don't use devres for mdiobus Vladimir Oltean
                   ` (5 subsequent siblings)
  7 siblings, 2 replies; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

As explained in commits:
74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")

mdiobus_free() will panic when called from devm_mdiobus_free() <-
devres_release_all() <- __device_release_driver(), and that mdiobus was
not previously unregistered.

The ar9331 is an MDIO device, so the initial set of constraints that I
thought would cause this (I2C or SPI buses which call ->remove on
->shutdown) do not apply. But there is one more which applies here.

If the DSA master itself is on a bus that calls ->remove from ->shutdown
(like dpaa2-eth, which is on the fsl-mc bus), there is a device link
between the switch and the DSA master, and device_links_unbind_consumers()
will unbind the ar9331 switch driver on shutdown.

So the same treatment must be applied to all DSA switch drivers, which
is: either use devres for both the mdiobus allocation and registration,
or don't use devres at all.

The ar9331 driver doesn't have a complex code structure for mdiobus
removal, so just replace of_mdiobus_register with the devres variant in
order to be all-devres and ensure that we don't free a still-registered
bus.

Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 drivers/net/dsa/qca/ar9331.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/net/dsa/qca/ar9331.c b/drivers/net/dsa/qca/ar9331.c
index 3bda7015f0c1..e5098cfe44bc 100644
--- a/drivers/net/dsa/qca/ar9331.c
+++ b/drivers/net/dsa/qca/ar9331.c
@@ -378,7 +378,7 @@ static int ar9331_sw_mbus_init(struct ar9331_sw_priv *priv)
 	if (!mnp)
 		return -ENODEV;
 
-	ret = of_mdiobus_register(mbus, mnp);
+	ret = devm_of_mdiobus_register(dev, mbus, mnp);
 	of_node_put(mnp);
 	if (ret)
 		return ret;
@@ -1066,7 +1066,6 @@ static void ar9331_sw_remove(struct mdio_device *mdiodev)
 	}
 
 	irq_domain_remove(priv->irqdomain);
-	mdiobus_unregister(priv->mbus);
 	dsa_unregister_switch(&priv->ds);
 
 	reset_control_assert(priv->sw_reset);
-- 
2.25.1


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

* [PATCH net 3/7] net: dsa: bcm_sf2: don't use devres for mdiobus
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
  2022-02-07 16:15 ` [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus Vladimir Oltean
  2022-02-07 16:15 ` [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres Vladimir Oltean
@ 2022-02-07 16:15 ` Vladimir Oltean
  2022-02-08  2:45   ` Florian Fainelli
  2022-02-07 16:15 ` [PATCH net 4/7] net: dsa: felix: " Vladimir Oltean
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

As explained in commits:
74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")

mdiobus_free() will panic when called from devm_mdiobus_free() <-
devres_release_all() <- __device_release_driver(), and that mdiobus was
not previously unregistered.

The Starfighter 2 is a platform device, so the initial set of
constraints that I thought would cause this (I2C or SPI buses which call
->remove on ->shutdown) do not apply. But there is one more which
applies here.

If the DSA master itself is on a bus that calls ->remove from ->shutdown
(like dpaa2-eth, which is on the fsl-mc bus), there is a device link
between the switch and the DSA master, and device_links_unbind_consumers()
will unbind the bcm_sf2 switch driver on shutdown.

So the same treatment must be applied to all DSA switch drivers, which
is: either use devres for both the mdiobus allocation and registration,
or don't use devres at all.

The bcm_sf2 driver has the code structure in place for orderly mdiobus
removal, so just replace devm_mdiobus_alloc() with the non-devres
variant, and add manual free where necessary, to ensure that we don't
let devres free a still-registered bus.

Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 drivers/net/dsa/bcm_sf2.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/bcm_sf2.c b/drivers/net/dsa/bcm_sf2.c
index 9161ce4ca352..cf82b1fa9725 100644
--- a/drivers/net/dsa/bcm_sf2.c
+++ b/drivers/net/dsa/bcm_sf2.c
@@ -621,7 +621,7 @@ static int bcm_sf2_mdio_register(struct dsa_switch *ds)
 	get_device(&priv->master_mii_bus->dev);
 	priv->master_mii_dn = dn;
 
-	priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
+	priv->slave_mii_bus = mdiobus_alloc();
 	if (!priv->slave_mii_bus) {
 		of_node_put(dn);
 		return -ENOMEM;
@@ -681,8 +681,10 @@ static int bcm_sf2_mdio_register(struct dsa_switch *ds)
 	}
 
 	err = mdiobus_register(priv->slave_mii_bus);
-	if (err && dn)
+	if (err && dn) {
+		mdiobus_free(priv->slave_mii_bus);
 		of_node_put(dn);
+	}
 
 	return err;
 }
@@ -690,6 +692,7 @@ static int bcm_sf2_mdio_register(struct dsa_switch *ds)
 static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
 {
 	mdiobus_unregister(priv->slave_mii_bus);
+	mdiobus_free(priv->slave_mii_bus);
 	of_node_put(priv->master_mii_dn);
 }
 
-- 
2.25.1


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

* [PATCH net 4/7] net: dsa: felix: don't use devres for mdiobus
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
                   ` (2 preceding siblings ...)
  2022-02-07 16:15 ` [PATCH net 3/7] net: dsa: bcm_sf2: don't use devres for mdiobus Vladimir Oltean
@ 2022-02-07 16:15 ` Vladimir Oltean
  2022-02-08  2:48   ` Florian Fainelli
  2022-02-07 16:15 ` [PATCH net 5/7] net: dsa: seville: register the mdiobus under devres Vladimir Oltean
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

As explained in commits:
74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")

mdiobus_free() will panic when called from devm_mdiobus_free() <-
devres_release_all() <- __device_release_driver(), and that mdiobus was
not previously unregistered.

The Felix VSC9959 switch is a PCI device, so the initial set of
constraints that I thought would cause this (I2C or SPI buses which call
->remove on ->shutdown) do not apply. But there is one more which
applies here.

If the DSA master itself is on a bus that calls ->remove from ->shutdown
(like dpaa2-eth, which is on the fsl-mc bus), there is a device link
between the switch and the DSA master, and device_links_unbind_consumers()
will unbind the felix switch driver on shutdown.

So the same treatment must be applied to all DSA switch drivers, which
is: either use devres for both the mdiobus allocation and registration,
or don't use devres at all.

The felix driver has the code structure in place for orderly mdiobus
removal, so just replace devm_mdiobus_alloc_size() with the non-devres
variant, and add manual free where necessary, to ensure that we don't
let devres free a still-registered bus.

Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 drivers/net/dsa/ocelot/felix_vsc9959.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/dsa/ocelot/felix_vsc9959.c b/drivers/net/dsa/ocelot/felix_vsc9959.c
index bf8d38239e7e..33f0ceae381d 100644
--- a/drivers/net/dsa/ocelot/felix_vsc9959.c
+++ b/drivers/net/dsa/ocelot/felix_vsc9959.c
@@ -1061,7 +1061,7 @@ static int vsc9959_mdio_bus_alloc(struct ocelot *ocelot)
 		return PTR_ERR(hw);
 	}
 
-	bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
+	bus = mdiobus_alloc_size(sizeof(*mdio_priv));
 	if (!bus)
 		return -ENOMEM;
 
@@ -1081,6 +1081,7 @@ static int vsc9959_mdio_bus_alloc(struct ocelot *ocelot)
 	rc = mdiobus_register(bus);
 	if (rc < 0) {
 		dev_err(dev, "failed to register MDIO bus\n");
+		mdiobus_free(bus);
 		return rc;
 	}
 
@@ -1132,6 +1133,7 @@ static void vsc9959_mdio_bus_free(struct ocelot *ocelot)
 		lynx_pcs_destroy(phylink_pcs);
 	}
 	mdiobus_unregister(felix->imdio);
+	mdiobus_free(felix->imdio);
 }
 
 static void vsc9959_sched_speed_set(struct ocelot *ocelot, int port,
-- 
2.25.1


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

* [PATCH net 5/7] net: dsa: seville: register the mdiobus under devres
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
                   ` (3 preceding siblings ...)
  2022-02-07 16:15 ` [PATCH net 4/7] net: dsa: felix: " Vladimir Oltean
@ 2022-02-07 16:15 ` Vladimir Oltean
  2022-02-08  2:48   ` Florian Fainelli
  2022-02-07 16:15 ` [PATCH net 6/7] net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding Vladimir Oltean
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

As explained in commits:
74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")

mdiobus_free() will panic when called from devm_mdiobus_free() <-
devres_release_all() <- __device_release_driver(), and that mdiobus was
not previously unregistered.

The Seville VSC9959 switch is a platform device, so the initial set of
constraints that I thought would cause this (I2C or SPI buses which call
->remove on ->shutdown) do not apply. But there is one more which
applies here.

If the DSA master itself is on a bus that calls ->remove from ->shutdown
(like dpaa2-eth, which is on the fsl-mc bus), there is a device link
between the switch and the DSA master, and device_links_unbind_consumers()
will unbind the seville switch driver on shutdown.

So the same treatment must be applied to all DSA switch drivers, which
is: either use devres for both the mdiobus allocation and registration,
or don't use devres at all.

The seville driver has a code structure that could accommodate both the
mdiobus_unregister and mdiobus_free calls, but it has an external
dependency upon mscc_miim_setup() from mdio-mscc-miim.c, which calls
devm_mdiobus_alloc_size() on its behalf. So rather than restructuring
that, and exporting yet one more symbol mscc_miim_teardown(), let's work
with devres and replace of_mdiobus_register with the devres variant.
When we use all-devres, we can ensure that devres doesn't free a
still-registered bus (it either runs both callbacks, or none).

Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 drivers/net/dsa/ocelot/seville_vsc9953.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/ocelot/seville_vsc9953.c b/drivers/net/dsa/ocelot/seville_vsc9953.c
index 8c1c9da61602..f2f1608a476c 100644
--- a/drivers/net/dsa/ocelot/seville_vsc9953.c
+++ b/drivers/net/dsa/ocelot/seville_vsc9953.c
@@ -1029,7 +1029,7 @@ static int vsc9953_mdio_bus_alloc(struct ocelot *ocelot)
 	}
 
 	/* Needed in order to initialize the bus mutex lock */
-	rc = of_mdiobus_register(bus, NULL);
+	rc = devm_of_mdiobus_register(dev, bus, NULL);
 	if (rc < 0) {
 		dev_err(dev, "failed to register MDIO bus\n");
 		return rc;
@@ -1083,7 +1083,8 @@ static void vsc9953_mdio_bus_free(struct ocelot *ocelot)
 		mdio_device_free(mdio_device);
 		lynx_pcs_destroy(phylink_pcs);
 	}
-	mdiobus_unregister(felix->imdio);
+
+	/* mdiobus_unregister and mdiobus_free handled by devres */
 }
 
 static const struct felix_info seville_info_vsc9953 = {
-- 
2.25.1


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

* [PATCH net 6/7] net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
                   ` (4 preceding siblings ...)
  2022-02-07 16:15 ` [PATCH net 5/7] net: dsa: seville: register the mdiobus under devres Vladimir Oltean
@ 2022-02-07 16:15 ` Vladimir Oltean
  2022-02-08  2:49   ` Florian Fainelli
  2022-02-07 16:15 ` [PATCH net 7/7] net: dsa: lantiq_gswip: don't use devres for mdiobus Vladimir Oltean
  2022-02-09  5:20 ` [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} patchwork-bot+netdevbpf
  7 siblings, 1 reply; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

Nobody in this driver calls mdiobus_unregister(), which is necessary if
mdiobus_register() completes successfully. So if the devres callbacks
that free the mdiobus get invoked (this is the case when unbinding the
driver), mdiobus_free() will BUG if the mdiobus is still registered,
which it is.

My speculation is that this is due to the fact that prior to commit
ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
from June 2020, _devm_mdiobus_free() used to call mdiobus_unregister().
But at the time that the mt7530 support was introduced in May 2021, the
API was already changed. It's therefore likely that the blamed patch was
developed on an older tree, and incorrectly adapted to net-next. This
makes the Fixes: tag correct.

Fix the problem by using the devres variant of mdiobus_register.

Fixes: ba751e28d442 ("net: dsa: mt7530: add interrupt support")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 drivers/net/dsa/mt7530.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index bc77a26c825a..f74f25f479ed 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -2074,7 +2074,7 @@ mt7530_setup_mdio(struct mt7530_priv *priv)
 	if (priv->irq)
 		mt7530_setup_mdio_irq(priv);
 
-	ret = mdiobus_register(bus);
+	ret = devm_mdiobus_register(dev, bus);
 	if (ret) {
 		dev_err(dev, "failed to register MDIO bus: %d\n", ret);
 		if (priv->irq)
-- 
2.25.1


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

* [PATCH net 7/7] net: dsa: lantiq_gswip: don't use devres for mdiobus
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
                   ` (5 preceding siblings ...)
  2022-02-07 16:15 ` [PATCH net 6/7] net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding Vladimir Oltean
@ 2022-02-07 16:15 ` Vladimir Oltean
  2022-02-08  2:49   ` Florian Fainelli
  2022-02-09  5:20 ` [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} patchwork-bot+netdevbpf
  7 siblings, 1 reply; 19+ messages in thread
From: Vladimir Oltean @ 2022-02-07 16:15 UTC (permalink / raw)
  To: netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

As explained in commits:
74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")

mdiobus_free() will panic when called from devm_mdiobus_free() <-
devres_release_all() <- __device_release_driver(), and that mdiobus was
not previously unregistered.

The GSWIP switch is a platform device, so the initial set of constraints
that I thought would cause this (I2C or SPI buses which call ->remove on
->shutdown) do not apply. But there is one more which applies here.

If the DSA master itself is on a bus that calls ->remove from ->shutdown
(like dpaa2-eth, which is on the fsl-mc bus), there is a device link
between the switch and the DSA master, and device_links_unbind_consumers()
will unbind the GSWIP switch driver on shutdown.

So the same treatment must be applied to all DSA switch drivers, which
is: either use devres for both the mdiobus allocation and registration,
or don't use devres at all.

The gswip driver has the code structure in place for orderly mdiobus
removal, so just replace devm_mdiobus_alloc() with the non-devres
variant, and add manual free where necessary, to ensure that we don't
let devres free a still-registered bus.

Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 drivers/net/dsa/lantiq_gswip.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/net/dsa/lantiq_gswip.c b/drivers/net/dsa/lantiq_gswip.c
index 46ed953e787e..320ee7fe91a8 100644
--- a/drivers/net/dsa/lantiq_gswip.c
+++ b/drivers/net/dsa/lantiq_gswip.c
@@ -498,8 +498,9 @@ static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
 static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np)
 {
 	struct dsa_switch *ds = priv->ds;
+	int err;
 
-	ds->slave_mii_bus = devm_mdiobus_alloc(priv->dev);
+	ds->slave_mii_bus = mdiobus_alloc();
 	if (!ds->slave_mii_bus)
 		return -ENOMEM;
 
@@ -512,7 +513,11 @@ static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np)
 	ds->slave_mii_bus->parent = priv->dev;
 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
 
-	return of_mdiobus_register(ds->slave_mii_bus, mdio_np);
+	err = of_mdiobus_register(ds->slave_mii_bus, mdio_np);
+	if (err)
+		mdiobus_free(ds->slave_mii_bus);
+
+	return err;
 }
 
 static int gswip_pce_table_entry_read(struct gswip_priv *priv,
@@ -2145,8 +2150,10 @@ static int gswip_probe(struct platform_device *pdev)
 	gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
 	dsa_unregister_switch(priv->ds);
 mdio_bus:
-	if (mdio_np)
+	if (mdio_np) {
 		mdiobus_unregister(priv->ds->slave_mii_bus);
+		mdiobus_free(priv->ds->slave_mii_bus);
+	}
 put_mdio_node:
 	of_node_put(mdio_np);
 	for (i = 0; i < priv->num_gphy_fw; i++)
@@ -2169,6 +2176,7 @@ static int gswip_remove(struct platform_device *pdev)
 
 	if (priv->ds->slave_mii_bus) {
 		mdiobus_unregister(priv->ds->slave_mii_bus);
+		mdiobus_free(priv->ds->slave_mii_bus);
 		of_node_put(priv->ds->slave_mii_bus->dev.of_node);
 	}
 
-- 
2.25.1


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

* Re: [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus
  2022-02-07 16:15 ` [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus Vladimir Oltean
@ 2022-02-07 17:38   ` Daniel Klauer
  2022-02-08  1:42   ` Andrew Lunn
  2022-02-08  2:45   ` Florian Fainelli
  2 siblings, 0 replies; 19+ messages in thread
From: Daniel Klauer @ 2022-02-07 17:38 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Florian Fainelli, Andrew Lunn, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Richter, Rafael

Thanks for this patch, with it the 5.16 kernel no longer BUGs on 
shutdown on our board (LX2160A + mv88e6xxx).

Tested-by: Daniel Klauer <daniel.klauer@gin.de>

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

* Re: [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus
  2022-02-07 16:15 ` [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus Vladimir Oltean
  2022-02-07 17:38   ` Daniel Klauer
@ 2022-02-08  1:42   ` Andrew Lunn
  2022-02-08  2:45   ` Florian Fainelli
  2 siblings, 0 replies; 19+ messages in thread
From: Andrew Lunn @ 2022-02-08  1:42 UTC (permalink / raw)
  To: Vladimir Oltean
  Cc: netdev, Florian Fainelli, Vivien Didelot, Vladimir Oltean,
	David S. Miller, Jakub Kicinski, Hauke Mehrtens, Sean Wang,
	Landen Chao, DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer

On Mon, Feb 07, 2022 at 06:15:47PM +0200, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.

...
 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Reported-by: Rafael Richter <Rafael.Richter@gin.de>
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew

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

* Re: [PATCH net 3/7] net: dsa: bcm_sf2: don't use devres for mdiobus
  2022-02-07 16:15 ` [PATCH net 3/7] net: dsa: bcm_sf2: don't use devres for mdiobus Vladimir Oltean
@ 2022-02-08  2:45   ` Florian Fainelli
  0 siblings, 0 replies; 19+ messages in thread
From: Florian Fainelli @ 2022-02-08  2:45 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S. Miller,
	Jakub Kicinski, Hauke Mehrtens, Sean Wang, Landen Chao,
	DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer



On 2/7/2022 8:15 AM, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.
> 
> The Starfighter 2 is a platform device, so the initial set of
> constraints that I thought would cause this (I2C or SPI buses which call
> ->remove on ->shutdown) do not apply. But there is one more which
> applies here.
> 
> If the DSA master itself is on a bus that calls ->remove from ->shutdown
> (like dpaa2-eth, which is on the fsl-mc bus), there is a device link
> between the switch and the DSA master, and device_links_unbind_consumers()
> will unbind the bcm_sf2 switch driver on shutdown.
> 
> So the same treatment must be applied to all DSA switch drivers, which
> is: either use devres for both the mdiobus allocation and registration,
> or don't use devres at all.
> 
> The bcm_sf2 driver has the code structure in place for orderly mdiobus
> removal, so just replace devm_mdiobus_alloc() with the non-devres
> variant, and add manual free where necessary, to ensure that we don't
> let devres free a still-registered bus.
> 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

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

* Re: [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus
  2022-02-07 16:15 ` [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus Vladimir Oltean
  2022-02-07 17:38   ` Daniel Klauer
  2022-02-08  1:42   ` Andrew Lunn
@ 2022-02-08  2:45   ` Florian Fainelli
  2 siblings, 0 replies; 19+ messages in thread
From: Florian Fainelli @ 2022-02-08  2:45 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S. Miller,
	Jakub Kicinski, Hauke Mehrtens, Sean Wang, Landen Chao,
	DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer



On 2/7/2022 8:15 AM, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.
> 
> The mv88e6xxx is an MDIO device, so the initial set of constraints that
> I thought would cause this (I2C or SPI buses which call ->remove on
> ->shutdown) do not apply. But there is one more which applies here.
> 
> If the DSA master itself is on a bus that calls ->remove from ->shutdown
> (like dpaa2-eth, which is on the fsl-mc bus), there is a device link
> between the switch and the DSA master, and device_links_unbind_consumers()
> will unbind the Marvell switch driver on shutdown.
> 
> systemd-shutdown[1]: Powering off.
> mv88e6085 0x0000000008b96000:00 sw_gl0: Link is Down
> fsl-mc dpbp.9: Removing from iommu group 7
> fsl-mc dpbp.8: Removing from iommu group 7
> ------------[ cut here ]------------
> kernel BUG at drivers/net/phy/mdio_bus.c:677!
> Internal error: Oops - BUG: 0 [#1] PREEMPT SMP
> Modules linked in:
> CPU: 0 PID: 1 Comm: systemd-shutdow Not tainted 5.16.5-00040-gdc05f73788e5 #15
> pc : mdiobus_free+0x44/0x50
> lr : devm_mdiobus_free+0x10/0x20
> Call trace:
>   mdiobus_free+0x44/0x50
>   devm_mdiobus_free+0x10/0x20
>   devres_release_all+0xa0/0x100
>   __device_release_driver+0x190/0x220
>   device_release_driver_internal+0xac/0xb0
>   device_links_unbind_consumers+0xd4/0x100
>   __device_release_driver+0x4c/0x220
>   device_release_driver_internal+0xac/0xb0
>   device_links_unbind_consumers+0xd4/0x100
>   __device_release_driver+0x94/0x220
>   device_release_driver+0x28/0x40
>   bus_remove_device+0x118/0x124
>   device_del+0x174/0x420
>   fsl_mc_device_remove+0x24/0x40
>   __fsl_mc_device_remove+0xc/0x20
>   device_for_each_child+0x58/0xa0
>   dprc_remove+0x90/0xb0
>   fsl_mc_driver_remove+0x20/0x5c
>   __device_release_driver+0x21c/0x220
>   device_release_driver+0x28/0x40
>   bus_remove_device+0x118/0x124
>   device_del+0x174/0x420
>   fsl_mc_bus_remove+0x80/0x100
>   fsl_mc_bus_shutdown+0xc/0x1c
>   platform_shutdown+0x20/0x30
>   device_shutdown+0x154/0x330
>   kernel_power_off+0x34/0x6c
>   __do_sys_reboot+0x15c/0x250
>   __arm64_sys_reboot+0x20/0x30
>   invoke_syscall.constprop.0+0x4c/0xe0
>   do_el0_svc+0x4c/0x150
>   el0_svc+0x24/0xb0
>   el0t_64_sync_handler+0xa8/0xb0
>   el0t_64_sync+0x178/0x17c
> 
> So the same treatment must be applied to all DSA switch drivers, which
> is: either use devres for both the mdiobus allocation and registration,
> or don't use devres at all.
> 
> The Marvell driver already has a good structure for mdiobus removal, so
> just plug in mdiobus_free and get rid of devres.
> 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Reported-by: Rafael Richter <Rafael.Richter@gin.de>
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

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

* Re: [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres
  2022-02-07 16:15 ` [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres Vladimir Oltean
@ 2022-02-08  2:46   ` Florian Fainelli
  2022-02-08  7:20   ` Oleksij Rempel
  1 sibling, 0 replies; 19+ messages in thread
From: Florian Fainelli @ 2022-02-08  2:46 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S. Miller,
	Jakub Kicinski, Hauke Mehrtens, Sean Wang, Landen Chao,
	DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer



On 2/7/2022 8:15 AM, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.
> 
> The ar9331 is an MDIO device, so the initial set of constraints that I
> thought would cause this (I2C or SPI buses which call ->remove on
> ->shutdown) do not apply. But there is one more which applies here.
> 
> If the DSA master itself is on a bus that calls ->remove from ->shutdown
> (like dpaa2-eth, which is on the fsl-mc bus), there is a device link
> between the switch and the DSA master, and device_links_unbind_consumers()
> will unbind the ar9331 switch driver on shutdown.
> 
> So the same treatment must be applied to all DSA switch drivers, which
> is: either use devres for both the mdiobus allocation and registration,
> or don't use devres at all.
> 
> The ar9331 driver doesn't have a complex code structure for mdiobus
> removal, so just replace of_mdiobus_register with the devres variant in
> order to be all-devres and ensure that we don't free a still-registered
> bus.
> 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

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

* Re: [PATCH net 4/7] net: dsa: felix: don't use devres for mdiobus
  2022-02-07 16:15 ` [PATCH net 4/7] net: dsa: felix: " Vladimir Oltean
@ 2022-02-08  2:48   ` Florian Fainelli
  0 siblings, 0 replies; 19+ messages in thread
From: Florian Fainelli @ 2022-02-08  2:48 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S. Miller,
	Jakub Kicinski, Hauke Mehrtens, Sean Wang, Landen Chao,
	DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer



On 2/7/2022 8:15 AM, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.
> 
> The Felix VSC9959 switch is a PCI device, so the initial set of
> constraints that I thought would cause this (I2C or SPI buses which call
> ->remove on ->shutdown) do not apply. But there is one more which
> applies here.
> 
> If the DSA master itself is on a bus that calls ->remove from ->shutdown
> (like dpaa2-eth, which is on the fsl-mc bus), there is a device link
> between the switch and the DSA master, and device_links_unbind_consumers()
> will unbind the felix switch driver on shutdown.
> 
> So the same treatment must be applied to all DSA switch drivers, which
> is: either use devres for both the mdiobus allocation and registration,
> or don't use devres at all.
> 
> The felix driver has the code structure in place for orderly mdiobus
> removal, so just replace devm_mdiobus_alloc_size() with the non-devres
> variant, and add manual free where necessary, to ensure that we don't
> let devres free a still-registered bus.
> 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

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

* Re: [PATCH net 5/7] net: dsa: seville: register the mdiobus under devres
  2022-02-07 16:15 ` [PATCH net 5/7] net: dsa: seville: register the mdiobus under devres Vladimir Oltean
@ 2022-02-08  2:48   ` Florian Fainelli
  0 siblings, 0 replies; 19+ messages in thread
From: Florian Fainelli @ 2022-02-08  2:48 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S. Miller,
	Jakub Kicinski, Hauke Mehrtens, Sean Wang, Landen Chao,
	DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer



On 2/7/2022 8:15 AM, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.
> 
> The Seville VSC9959 switch is a platform device, so the initial set of
> constraints that I thought would cause this (I2C or SPI buses which call
> ->remove on ->shutdown) do not apply. But there is one more which
> applies here.
> 
> If the DSA master itself is on a bus that calls ->remove from ->shutdown
> (like dpaa2-eth, which is on the fsl-mc bus), there is a device link
> between the switch and the DSA master, and device_links_unbind_consumers()
> will unbind the seville switch driver on shutdown.
> 
> So the same treatment must be applied to all DSA switch drivers, which
> is: either use devres for both the mdiobus allocation and registration,
> or don't use devres at all.
> 
> The seville driver has a code structure that could accommodate both the
> mdiobus_unregister and mdiobus_free calls, but it has an external
> dependency upon mscc_miim_setup() from mdio-mscc-miim.c, which calls
> devm_mdiobus_alloc_size() on its behalf. So rather than restructuring
> that, and exporting yet one more symbol mscc_miim_teardown(), let's work
> with devres and replace of_mdiobus_register with the devres variant.
> When we use all-devres, we can ensure that devres doesn't free a
> still-registered bus (it either runs both callbacks, or none).
> 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

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

* Re: [PATCH net 6/7] net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding
  2022-02-07 16:15 ` [PATCH net 6/7] net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding Vladimir Oltean
@ 2022-02-08  2:49   ` Florian Fainelli
  0 siblings, 0 replies; 19+ messages in thread
From: Florian Fainelli @ 2022-02-08  2:49 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S. Miller,
	Jakub Kicinski, Hauke Mehrtens, Sean Wang, Landen Chao,
	DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer



On 2/7/2022 8:15 AM, Vladimir Oltean wrote:
> Nobody in this driver calls mdiobus_unregister(), which is necessary if
> mdiobus_register() completes successfully. So if the devres callbacks
> that free the mdiobus get invoked (this is the case when unbinding the
> driver), mdiobus_free() will BUG if the mdiobus is still registered,
> which it is.
> 
> My speculation is that this is due to the fact that prior to commit
> ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> from June 2020, _devm_mdiobus_free() used to call mdiobus_unregister().
> But at the time that the mt7530 support was introduced in May 2021, the
> API was already changed. It's therefore likely that the blamed patch was
> developed on an older tree, and incorrectly adapted to net-next. This
> makes the Fixes: tag correct.
> 
> Fix the problem by using the devres variant of mdiobus_register.
> 
> Fixes: ba751e28d442 ("net: dsa: mt7530: add interrupt support")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

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

* Re: [PATCH net 7/7] net: dsa: lantiq_gswip: don't use devres for mdiobus
  2022-02-07 16:15 ` [PATCH net 7/7] net: dsa: lantiq_gswip: don't use devres for mdiobus Vladimir Oltean
@ 2022-02-08  2:49   ` Florian Fainelli
  0 siblings, 0 replies; 19+ messages in thread
From: Florian Fainelli @ 2022-02-08  2:49 UTC (permalink / raw)
  To: Vladimir Oltean, netdev
  Cc: Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S. Miller,
	Jakub Kicinski, Hauke Mehrtens, Sean Wang, Landen Chao,
	DENG Qingfang, Matthias Brugger, Claudiu Manoil,
	Alexandre Belloni, UNGLinuxDriver, Oleksij Rempel,
	Bartosz Golaszewski, Laurentiu Tudor, Rafael Richter,
	Daniel Klauer



On 2/7/2022 8:15 AM, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.
> 
> The GSWIP switch is a platform device, so the initial set of constraints
> that I thought would cause this (I2C or SPI buses which call ->remove on
> ->shutdown) do not apply. But there is one more which applies here.
> 
> If the DSA master itself is on a bus that calls ->remove from ->shutdown
> (like dpaa2-eth, which is on the fsl-mc bus), there is a device link
> between the switch and the DSA master, and device_links_unbind_consumers()
> will unbind the GSWIP switch driver on shutdown.
> 
> So the same treatment must be applied to all DSA switch drivers, which
> is: either use devres for both the mdiobus allocation and registration,
> or don't use devres at all.
> 
> The gswip driver has the code structure in place for orderly mdiobus
> removal, so just replace devm_mdiobus_alloc() with the non-devres
> variant, and add manual free where necessary, to ensure that we don't
> let devres free a still-registered bus.
> 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

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

* Re: [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres
  2022-02-07 16:15 ` [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres Vladimir Oltean
  2022-02-08  2:46   ` Florian Fainelli
@ 2022-02-08  7:20   ` Oleksij Rempel
  1 sibling, 0 replies; 19+ messages in thread
From: Oleksij Rempel @ 2022-02-08  7:20 UTC (permalink / raw)
  To: Vladimir Oltean
  Cc: netdev, Florian Fainelli, Andrew Lunn, Vivien Didelot,
	Vladimir Oltean, David S. Miller, Jakub Kicinski, Hauke Mehrtens,
	Sean Wang, Landen Chao, DENG Qingfang, Matthias Brugger,
	Claudiu Manoil, Alexandre Belloni, UNGLinuxDriver,
	Oleksij Rempel, Bartosz Golaszewski, Laurentiu Tudor,
	Rafael Richter, Daniel Klauer

On Mon, Feb 07, 2022 at 06:15:48PM +0200, Vladimir Oltean wrote:
> As explained in commits:
> 74b6d7d13307 ("net: dsa: realtek: register the MDIO bus under devres")
> 5135e96a3dd2 ("net: dsa: don't allocate the slave_mii_bus using devres")
> 
> mdiobus_free() will panic when called from devm_mdiobus_free() <-
> devres_release_all() <- __device_release_driver(), and that mdiobus was
> not previously unregistered.
> 
> The ar9331 is an MDIO device, so the initial set of constraints that I
> thought would cause this (I2C or SPI buses which call ->remove on
> ->shutdown) do not apply. But there is one more which applies here.
> 
> If the DSA master itself is on a bus that calls ->remove from ->shutdown
> (like dpaa2-eth, which is on the fsl-mc bus), there is a device link
> between the switch and the DSA master, and device_links_unbind_consumers()
> will unbind the ar9331 switch driver on shutdown.
> 
> So the same treatment must be applied to all DSA switch drivers, which
> is: either use devres for both the mdiobus allocation and registration,
> or don't use devres at all.
> 
> The ar9331 driver doesn't have a complex code structure for mdiobus
> removal, so just replace of_mdiobus_register with the devres variant in
> order to be all-devres and ensure that we don't free a still-registered
> bus.
> 
> Fixes: ac3a68d56651 ("net: phy: don't abuse devres in devm_mdiobus_register()")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>

Tested-by: Oleksij Rempel <o.rempel@pengutronix.de>

Thank you!

> ---
>  drivers/net/dsa/qca/ar9331.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/net/dsa/qca/ar9331.c b/drivers/net/dsa/qca/ar9331.c
> index 3bda7015f0c1..e5098cfe44bc 100644
> --- a/drivers/net/dsa/qca/ar9331.c
> +++ b/drivers/net/dsa/qca/ar9331.c
> @@ -378,7 +378,7 @@ static int ar9331_sw_mbus_init(struct ar9331_sw_priv *priv)
>  	if (!mnp)
>  		return -ENODEV;
>  
> -	ret = of_mdiobus_register(mbus, mnp);
> +	ret = devm_of_mdiobus_register(dev, mbus, mnp);
>  	of_node_put(mnp);
>  	if (ret)
>  		return ret;
> @@ -1066,7 +1066,6 @@ static void ar9331_sw_remove(struct mdio_device *mdiodev)
>  	}
>  
>  	irq_domain_remove(priv->irqdomain);
> -	mdiobus_unregister(priv->mbus);
>  	dsa_unregister_switch(&priv->ds);
>  
>  	reset_control_assert(priv->sw_reset);
> -- 
> 2.25.1
> 
> 

-- 
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 |

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

* Re: [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register}
  2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
                   ` (6 preceding siblings ...)
  2022-02-07 16:15 ` [PATCH net 7/7] net: dsa: lantiq_gswip: don't use devres for mdiobus Vladimir Oltean
@ 2022-02-09  5:20 ` patchwork-bot+netdevbpf
  7 siblings, 0 replies; 19+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-02-09  5:20 UTC (permalink / raw)
  To: Vladimir Oltean
  Cc: netdev, f.fainelli, andrew, vivien.didelot, olteanv, davem, kuba,
	hauke, sean.wang, Landen.Chao, dqfext, matthias.bgg,
	claudiu.manoil, alexandre.belloni, UNGLinuxDriver, linux,
	bgolaszewski, laurentiu.tudor, rafael.richter, daniel.klauer

Hello:

This series was applied to netdev/net.git (master)
by Jakub Kicinski <kuba@kernel.org>:

On Mon,  7 Feb 2022 18:15:46 +0200 you wrote:
> The initial patch series "[net,0/2] Fix mdiobus users with devres"
> https://patchwork.kernel.org/project/netdevbpf/cover/20210920214209.1733768-1-vladimir.oltean@nxp.com/
> fixed some instances where DSA drivers on slow buses (SPI, I2C) trigger
> a panic (changed since then to a warn) in mdiobus_free. That was due to
> devres calling mdiobus_free() with no prior mdiobus_unregister(), which
> again was due to commit ac3a68d56651 ("net: phy: don't abuse devres in
> devm_mdiobus_register()") by Bartosz Golaszewski.
> 
> [...]

Here is the summary with links:
  - [net,1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus
    https://git.kernel.org/netdev/net/c/f53a2ce893b2
  - [net,2/7] net: dsa: ar9331: register the mdiobus under devres
    https://git.kernel.org/netdev/net/c/50facd86e9fb
  - [net,3/7] net: dsa: bcm_sf2: don't use devres for mdiobus
    https://git.kernel.org/netdev/net/c/08f1a2082234
  - [net,4/7] net: dsa: felix: don't use devres for mdiobus
    https://git.kernel.org/netdev/net/c/209bdb7ec6a2
  - [net,5/7] net: dsa: seville: register the mdiobus under devres
    https://git.kernel.org/netdev/net/c/bd488afc3b39
  - [net,6/7] net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding
    https://git.kernel.org/netdev/net/c/9ffe3d09e32d
  - [net,7/7] net: dsa: lantiq_gswip: don't use devres for mdiobus
    https://git.kernel.org/netdev/net/c/0d120dfb5d67

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2022-02-09  5:30 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-07 16:15 [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} Vladimir Oltean
2022-02-07 16:15 ` [PATCH net 1/7] net: dsa: mv88e6xxx: don't use devres for mdiobus Vladimir Oltean
2022-02-07 17:38   ` Daniel Klauer
2022-02-08  1:42   ` Andrew Lunn
2022-02-08  2:45   ` Florian Fainelli
2022-02-07 16:15 ` [PATCH net 2/7] net: dsa: ar9331: register the mdiobus under devres Vladimir Oltean
2022-02-08  2:46   ` Florian Fainelli
2022-02-08  7:20   ` Oleksij Rempel
2022-02-07 16:15 ` [PATCH net 3/7] net: dsa: bcm_sf2: don't use devres for mdiobus Vladimir Oltean
2022-02-08  2:45   ` Florian Fainelli
2022-02-07 16:15 ` [PATCH net 4/7] net: dsa: felix: " Vladimir Oltean
2022-02-08  2:48   ` Florian Fainelli
2022-02-07 16:15 ` [PATCH net 5/7] net: dsa: seville: register the mdiobus under devres Vladimir Oltean
2022-02-08  2:48   ` Florian Fainelli
2022-02-07 16:15 ` [PATCH net 6/7] net: dsa: mt7530: fix kernel bug in mdiobus_free() when unbinding Vladimir Oltean
2022-02-08  2:49   ` Florian Fainelli
2022-02-07 16:15 ` [PATCH net 7/7] net: dsa: lantiq_gswip: don't use devres for mdiobus Vladimir Oltean
2022-02-08  2:49   ` Florian Fainelli
2022-02-09  5:20 ` [PATCH net 0/7] More DSA fixes for devres + mdiobus_{alloc,register} patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.