* [PATCH v2] net: dsa: mt7530: fix regulator leak in mt7530_setup()
@ 2026-11-19 6:04 Xiangwu Lai
2026-11-23 6:18 ` netdev-bot+sashiko
0 siblings, 1 reply; 3+ messages in thread
From: Xiangwu Lai @ 2026-11-19 6:04 UTC (permalink / raw)
To: chester.a.unal, daniel, andrew, olteanv
Cc: sean.wang, netdev, linux-kernel, Xiangwu Lai
mt7530_setup() enables the "core" and "io" regulators of the MT7530,
but none of the error paths that follow (reset timeout, unsupported
chip ID, 20MHz XTAL, VLAN setup, PHY muxing, GPIO setup, FDB flush)
disables them again, leaving both regulators enabled after a failed
setup.
Add error labels that disable what was enabled by then, chained in
fall-through style so that the unwind mirrors the enable order and
there is a single return (suggested by Andrew Lunn).
MT7621 maps to the same sw_setup callback but skips the regulator
section entirely, so both labels are guarded with a priv->id check to
avoid unbalanced disables.
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")
Suggested-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
---
Changes in v2:
- restructure the new error labels into chained fall-through cleanup
with a single return, as suggested by Andrew Lunn
- describe how the issue was found and its expected runtime symptom
in the commit message, as requested by the netdev sinfo bot
Link: https://lore.kernel.org/all/20261118014114.1855-1-laixiangwu@cmss.chinamobile.com/
drivers/net/dsa/mt7530.c | 26 ++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 7781a63b4..1637ea22f 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -2425,7 +2425,7 @@ mt7530_setup(struct dsa_switch *ds)
if (ret < 0) {
dev_err(priv->dev, "Failed to enable io pwr: %d\n",
ret);
- return ret;
+ goto err_disable_core_pwr;
}
}
@@ -2447,20 +2447,22 @@ mt7530_setup(struct dsa_switch *ds)
val != 0, 20, 1000000);
if (ret < 0) {
dev_err(priv->dev, "reset timeout\n");
- return ret;
+ goto err_powers;
}
regmap_read(priv->regmap, MT7530_CREV, &id);
id = FIELD_GET(CHIP_NAME_MASK, id);
if (id != MT7530_ID) {
dev_err(priv->dev, "chip %x can't be supported\n", id);
- return -ENODEV;
+ ret = -ENODEV;
+ goto err_powers;
}
if ((val & MT7530_XTAL_MASK) == MT7530_XTAL_20MHZ) {
dev_err(priv->dev,
"MT7530 with a 20MHz XTAL is not supported!\n");
- return -EINVAL;
+ ret = -EINVAL;
+ goto err_powers;
}
/* Reset the switch through internal reset */
@@ -2527,7 +2529,7 @@ mt7530_setup(struct dsa_switch *ds)
/* Setup VLAN ID 0 for VLAN-unaware bridges */
ret = mt7530_setup_vlan0(priv);
if (ret)
- return ret;
+ goto err_powers;
/* Check for PHY muxing on port 5 */
if (dsa_is_unused_port(ds, 5)) {
@@ -2554,7 +2556,7 @@ mt7530_setup(struct dsa_switch *ds)
if (ret && ret != -ENODEV) {
of_node_put(mac_np);
of_node_put(phy_node);
- return ret;
+ goto err_powers;
}
id = of_mdio_parse_addr(ds->dev, phy_node);
if (id == 0)
@@ -2579,16 +2581,24 @@ mt7530_setup(struct dsa_switch *ds)
if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
ret = mt7530_setup_gpio(priv);
if (ret)
- return ret;
+ goto err_powers;
}
#endif /* CONFIG_GPIOLIB */
/* Flush the FDB table */
ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
if (ret < 0)
- return ret;
+ goto err_powers;
return 0;
+
+err_powers:
+ if (priv->id == ID_MT7530)
+ regulator_disable(priv->io_pwr);
+err_disable_core_pwr:
+ if (priv->id == ID_MT7530)
+ regulator_disable(priv->core_pwr);
+ return ret;
}
static int
--
2.50.1.windows.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] net: dsa: mt7530: fix regulator leak in mt7530_setup()
2026-11-19 6:04 [PATCH v2] net: dsa: mt7530: fix regulator leak in mt7530_setup() Xiangwu Lai
@ 2026-11-23 6:18 ` netdev-bot+sashiko
2026-11-23 6:39 ` Xiangwu Lai
0 siblings, 1 reply; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 6:18 UTC (permalink / raw)
To: laixiangwu
Cc: chester.a.unal, daniel, andrew, olteanv, sean.wang, netdev,
linux-kernel, kuba
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix is incomplete: the regulator leak the commit message
describes can still happen.
- [Medium] Possible double disable: the new error labels in
mt7530_setup() disable core_pwr/io_pwr, but mt7530_remove() still…
- [Low] Possible power-down ordering problem (speculative): the new
err_powers / err_disable_core_pwr rollback in mt7530_setup() disables…
--
net: dsa: mt7530: fix regulator leak in mt7530_setup()
This patch adds chained error labels to mt7530_setup(). When a later step
in the function fails, the MT7530 core and io regulators are now disabled
again. Both labels check priv->id, so MT7621 is left alone because it
skips the regulator section.
Link: https://lore.kernel.org/all/20261118014114.1855-1-laixiangwu@cmss.chinamobile.com/
> diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
> index d5a199293f78a..988f236548dfc 100644
> --- a/drivers/net/dsa/mt7530.c
> +++ b/drivers/net/dsa/mt7530.c
[ ... ]
> @@ -2579,16 +2581,24 @@ mt7530_setup(struct dsa_switch *ds)
> if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) {
> ret = mt7530_setup_gpio(priv);
> if (ret)
> - return ret;
> + goto err_powers;
> }
> #endif /* CONFIG_GPIOLIB */
>
> /* Flush the FDB table */
> ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL);
> if (ret < 0)
> - return ret;
> + goto err_powers;
>
> return 0;
[Severity: High]
Does this fully close the leak described in the commit message?
mt7530_setup() is only the sw_setup step of mt753x_setup(). It returns 0
here with both regulators still enabled. After that, mt753x_setup() can
fail without disabling them:
drivers/net/dsa/mt7530.c:mt753x_setup() {
int ret = priv->info->sw_setup(ds);
...
ret = mt7530_setup_irq(priv);
if (ret)
return ret;
ret = mt7530_setup_mdio(priv);
if (ret)
return ret;
...
}
mt7530_setup_irq() fails in two cases:
- of_irq_get() returns <= 0. This includes -EPROBE_DEFER when the parent
irqchip has not probed yet.
- devm_regmap_add_irq_chip_fwnode() fails.
mt7530_setup_mdio() fails if devm_mdiobus_alloc() returns NULL or if
devm_of_mdiobus_register() returns an error.
If ops->setup fails, dsa_switch_setup() jumps to unregister_notifier and
does not call ->teardown.
If ops->setup succeeds but a later step fails (tag protocol setup, user
MII bus registration, port or conduit setup), the teardown that does run
only does this:
drivers/net/dsa/mt7530.c:mt753x_teardown() {
if (priv->bus)
cancel_delayed_work_sync(&priv->stats_work);
}
Either way, dsa_register_switch() fails and mt7530_probe() fails. That
means mt7530_remove() never runs, and it is the only other place that
disables these supplies.
devm then releases the regulator handles while their enable count is
still nonzero. This hits WARN_ON(regulator->enable_count) in
_regulator_put(). The supplies also stay on, because rdev->use_count is
never decremented.
With -EPROBE_DEFER, each deferred probe retry runs mt7530_setup() again
and leaks one more enable.
These look like the symptoms the commit message describes: "both
regulators staying powered after a failed probe and a WARN_ON(enable_count)
splat".
Would it be better to also unwind in mt753x_setup() and the teardown
path? Another option is to move the enable/disable into a properly paired
place, such as probe with a devm action, or setup/teardown.
> +
> +err_powers:
> + if (priv->id == ID_MT7530)
> + regulator_disable(priv->io_pwr);
> +err_disable_core_pwr:
> + if (priv->id == ID_MT7530)
> + regulator_disable(priv->core_pwr);
> + return ret;
> }
[Severity: Medium]
Can these labels combine with mt7530_remove() to disable the supplies
twice?
mt7530_remove() still disables both supplies unconditionally:
drivers/net/dsa/mt7530-mdio.c:mt7530_remove() {
if (priv->id == ID_MT7530) {
ret = regulator_disable(priv->core_pwr);
...
ret = regulator_disable(priv->io_pwr);
...
}
}
Take a multi-switch DSA tree where the MT7530 has DSA link ports and is
not the last member to probe. Its probe succeeds, because
dsa_tree_setup() returns early without running ops->setup:
net/dsa/dsa.c:dsa_tree_setup() {
complete = dsa_tree_setup_routing_table(dst);
if (!complete)
return 0;
...
}
When a later switch completes the tree, this runs:
dsa_tree_setup_switches()
dsa_switch_setup()
mt753x_setup()
mt7530_setup()
Suppose mt7530_setup() then fails after the regulators were enabled, for
example on a reset timeout, a chip ID mismatch, a 20MHz XTAL, or a VLAN0
or FDB command failure. These new labels bring the enable count back to
zero.
The other switch's probe fails, but the MT7530 stays bound. When it is
unbound later, mt7530_remove() disables both supplies again. That
triggers the "unbalanced disables" WARN in _regulator_disable(), and the
driver logs "Failed to disable core power" and "Failed to disable io
pwr".
Before this patch the sequence happened to balance: the failed setup
left the regulators enabled, and remove disabled them.
[Severity: Low]
This is speculative, but should the chip go back into reset before its
supplies are cut here?
Earlier in mt7530_setup(), reset is released:
drivers/net/dsa/mt7530.c:mt7530_setup() {
...
} else {
gpiod_set_value_cansleep(priv->reset, 0);
usleep_range(5000, 5100);
gpiod_set_value_cansleep(priv->reset, 1);
}
...
}
mt7530_probe() requests the line as GPIOD_OUT_LOW, which holds the chip
in reset. The new err_powers path, however, disables io_pwr and core_pwr
while reset is still released.
On boards with switchable supplies, the host would then drive RESET_N
into an unpowered IO domain. That could back-power the chip through its
input protection diodes.
Before this patch, these error paths left the chip fully powered.
mt7530_remove() also disables the supplies without asserting reset, so
this ordering already exists elsewhere in the driver.
Would it be safer to mirror the setup order? That would mean asserting
reset first, with gpiod_set_value_cansleep(priv->reset, 0) or
reset_control_assert() for MCM, and only then disabling the regulators.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119060424.1476-1-laixiangwu%40cmss.chinamobile.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] net: dsa: mt7530: fix regulator leak in mt7530_setup()
2026-11-23 6:18 ` netdev-bot+sashiko
@ 2026-11-23 6:39 ` Xiangwu Lai
0 siblings, 0 replies; 3+ messages in thread
From: Xiangwu Lai @ 2026-11-23 6:39 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: Xiangwu Lai, chester.a.unal, daniel, andrew, olteanv, sean.wang,
netdev, linux-kernel, kuba
Hi,
On Mon, 23 Nov 2026 06:18:18 +0000, netdev-bot+sashiko wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential
> issue(s) to consider.
>
> - [High] The fix is incomplete: the regulator leak the commit message
> describes can still happen.
> - [Medium] Possible double disable: the new error labels in
> mt7530_setup() disable core_pwr/io_pwr, but mt7530_remove() still...
> - [Low] Possible power-down ordering problem (speculative): the new
> err_powers / err_disable_core_pwr rollback in mt7530_setup() disables...
Thanks for the review. These are the same three issues that were found
in the review of v1 [1], and I agree with all of them for v2 as well:
* High: Correct. The chained error labels only unwind failures inside
mt7530_setup(). mt7530_setup_irq(), mt7530_setup_mdio() (both can
return -EPROBE_DEFER), the SGMII PCS creation and the DSA core setup
steps can still fail with the regulators left enabled, and each
deferred probe retry would leak one more enable.
* Medium: Correct. In a multi-switch tree, a switch can stay bound
without its ds->ops->setup having run; a later failing setup is
reported through the other switch's probe. With the v2 unwinding
bringing the enable count back to zero, the later unbind would then
hit the unbalanced regulator_disable() in mt7530_remove().
* Low: Point taken, it is speculative for a software-only fix, and
mt7530_remove() already disables the regulators in the same order.
v2 has been superseded by v3 [2], which adopts the structure suggested
in the review ("move the enable/disable into a properly paired place,
such as probe with a devm action"):
[PATCH v3] net: dsa: mt7530: manage the core and io regulators with devm actions
https://lore.kernel.org/r/20261123022415.433-1-laixiangwu@cmss.chinamobile.com
The enables now live in mt7530_probe(), each paired with a
devm_add_action_or_reset() callback, mt7530_setup() is back to its
original shape, and mt7530_remove() no longer touches the regulators.
This resolves all three findings: every later failure path (including
-EPROBE_DEFER retries and the bind-without-setup case) is covered by
the devm actions, and there is no second disable anywhere.
pw-bot: cr
[1] https://lore.kernel.org/r/179531318026.3135.909782780821579093@kernel.org
[2] https://lore.kernel.org/r/20261123022415.433-1-laixiangwu@cmss.chinamobile.com
Best regards,
Xiangwu Lai
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-23 6:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 6:04 [PATCH v2] net: dsa: mt7530: fix regulator leak in mt7530_setup() Xiangwu Lai
2026-11-23 6:18 ` netdev-bot+sashiko
2026-11-23 6:39 ` Xiangwu Lai
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®