[PATCH v3] net: dsa: mt7530: manage the core and io regulators with devm actions

From: Xiangwu Lai

Date: Sun Nov 22 2026 - 21:24:23 EST


mt7530_setup() enables the core and io regulators of the MT7530, but the
only matching regulator_disable() calls live in mt7530_remove(), which
never runs when the probe fails. A failure anywhere between the enables
and a fully registered switch - the remaining steps of mt7530_setup(),
the IRQ chip or MDIO bus registration in mt753x_setup() (both can also
return -EPROBE_DEFER), the SGMII PCS creation, or the DSA core itself -
therefore leaves both regulators enabled. The devm-acquired handles
are then released with a non-zero enable_count, which triggers the
WARN_ON() in _regulator_put(), and each probe deferral grows the count
further.

Move the enables to mt7530_probe(), right after the regulators are
acquired, and pair each regulator_enable() with a
devm_add_action_or_reset() that disables it again. This covers every
error path after the enables, and replaces the explicit disables in
mt7530_remove(), so the enables and disables stay balanced in all
combinations of probe, setup and unbind.

Found by code inspection, the issue has not been triggered at runtime
and no MT7530 hardware was available for testing. The visible symptom
of the leak would be both regulators staying powered after a failed
probe and a WARN_ON(enable_count) splat from the regulator core when
the devm-acquired handles are released with a nonzero enable count.

Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch")
Signed-off-by: Xiangwu Lai <laixiangwu@xxxxxxxxxxxxxxxxxxxx>
---

Changes in v3:
- Replaced the setup error-path unwinding of v2 with devm-managed regulator
enables in mt7530_probe(), and removed the explicit disables from
mt7530_remove(). The late error paths outside mt7530_setup() (IRQ chip
and MDIO bus registration, SGMII PCS creation, the DSA core) are now
covered too, as suggested in the review of v1 by the netdev AI bot.

Link: https://lore.kernel.org/r/20261119060424.1476-1-laixiangwu@xxxxxxxxxxxxxxxxxxxx
drivers/net/dsa/mt7530-mdio.c | 48 +++++++++++++++++++++++++----------
drivers/net/dsa/mt7530.c | 18 -------------
2 files changed, 35 insertions(+), 31 deletions(-)

diff --git a/drivers/net/dsa/mt7530-mdio.c b/drivers/net/dsa/mt7530-mdio.c
index 0be107dc6..99432d658 100644
--- a/drivers/net/dsa/mt7530-mdio.c
+++ b/drivers/net/dsa/mt7530-mdio.c
@@ -155,6 +155,14 @@ static const struct regmap_config regmap_config = {
.unlock = mt7530_mdio_regmap_unlock,
};

+static void
+mt7530_disable_regulator(void *data)
+{
+ struct regulator *regulator = data;
+
+ regulator_disable(regulator);
+}
+
static int
mt7530_probe(struct mdio_device *mdiodev)
{
@@ -210,6 +218,32 @@ mt7530_probe(struct mdio_device *mdiodev)
priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
if (IS_ERR(priv->io_pwr))
return PTR_ERR(priv->io_pwr);
+
+ regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
+ ret = regulator_enable(priv->core_pwr);
+ if (ret < 0) {
+ dev_err(&mdiodev->dev,
+ "Failed to enable core power: %d\n", ret);
+ return ret;
+ }
+ ret = devm_add_action_or_reset(&mdiodev->dev,
+ mt7530_disable_regulator,
+ priv->core_pwr);
+ if (ret)
+ return ret;
+
+ regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
+ ret = regulator_enable(priv->io_pwr);
+ if (ret < 0) {
+ dev_err(&mdiodev->dev,
+ "Failed to enable io pwr: %d\n", ret);
+ return ret;
+ }
+ ret = devm_add_action_or_reset(&mdiodev->dev,
+ mt7530_disable_regulator,
+ priv->io_pwr);
+ if (ret)
+ return ret;
}

rc.lock_arg = &priv->bus->mdio_lock;
@@ -228,23 +262,11 @@ static void
mt7530_remove(struct mdio_device *mdiodev)
{
struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
- int ret = 0, i;
+ int i;

if (!priv)
return;

- if (priv->id == ID_MT7530) {
- ret = regulator_disable(priv->core_pwr);
- if (ret < 0)
- dev_err(priv->dev,
- "Failed to disable core power: %d\n", ret);
-
- ret = regulator_disable(priv->io_pwr);
- if (ret < 0)
- dev_err(priv->dev, "Failed to disable io pwr: %d\n",
- ret);
- }
-
mt7530_remove_common(priv);

for (i = 0; i < 2; ++i)
diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 7781a63b4..b669a94d4 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -2411,24 +2411,6 @@ mt7530_setup(struct dsa_switch *ds)
ds->ageing_time_min = 2 * 1000;
ds->ageing_time_max = (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1) * 1000;

- if (priv->id == ID_MT7530) {
- regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
- ret = regulator_enable(priv->core_pwr);
- if (ret < 0) {
- dev_err(priv->dev,
- "Failed to enable core power: %d\n", ret);
- return ret;
- }
-
- regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
- ret = regulator_enable(priv->io_pwr);
- if (ret < 0) {
- dev_err(priv->dev, "Failed to enable io pwr: %d\n",
- ret);
- return ret;
- }
- }
-
/* Reset whole chip through gpio pin or memory-mapped registers for
* different type of hardware
*/
--
2.50.1.windows.1