Netdev List
 help / color / mirror / Atom feed
* [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup()
@ 2026-11-18  1:41 Xiangwu Lai
  2026-11-18  1:42 ` netdev-bot+sinfo
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Xiangwu Lai @ 2026-11-18  1:41 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 two error labels that disable what was enabled by then: the
io regulator failure path releases the already-enabled core
regulator, and all later failure paths disable both regulators.

MT7621 maps to the same sw_setup callback but skips the regulator
section entirely, so the cleanup at the common label is guarded
with a priv->id check to avoid unbalanced disables.

Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch")
Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
---
 drivers/net/dsa/mt7530.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 7781a63b4..2cf41282e 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,27 @@ 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_disable_core_pwr:
+	regulator_disable(priv->core_pwr);
+	return ret;
+
+err_powers:
+	if (priv->id == ID_MT7530) {
+		regulator_disable(priv->io_pwr);
+		regulator_disable(priv->core_pwr);
+	}
+	return ret;
 }
 
 static int
-- 
2.50.1.windows.1




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

* Re: [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup()
  2026-11-18  1:41 [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup() Xiangwu Lai
@ 2026-11-18  1:42 ` netdev-bot+sinfo
  2026-11-19  6:07   ` Xiangwu Lai
  2026-11-18 23:36 ` Andrew Lunn
  2026-11-22  2:06 ` netdev-bot+sashiko
  2 siblings, 1 reply; 6+ messages in thread
From: netdev-bot+sinfo @ 2026-11-18  1:42 UTC (permalink / raw)
  To: Xiangwu Lai
  Cc: chester.a.unal, daniel, andrew, olteanv, sean.wang, netdev,
	linux-kernel

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

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

* Re: [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup()
  2026-11-18  1:41 [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup() Xiangwu Lai
  2026-11-18  1:42 ` netdev-bot+sinfo
@ 2026-11-18 23:36 ` Andrew Lunn
  2026-11-19  6:05   ` Xiangwu Lai
  2026-11-22  2:06 ` netdev-bot+sashiko
  2 siblings, 1 reply; 6+ messages in thread
From: Andrew Lunn @ 2026-11-18 23:36 UTC (permalink / raw)
  To: Xiangwu Lai
  Cc: chester.a.unal, daniel, olteanv, sean.wang, netdev, linux-kernel

> Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch")

Does this bother anybody?

https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html

> +err_disable_core_pwr:
> +	regulator_disable(priv->core_pwr);
> +	return ret;
> +
> +err_powers:
> +	if (priv->id == ID_MT7530) {
> +		regulator_disable(priv->io_pwr);
> +		regulator_disable(priv->core_pwr);
> +	}
> +	return ret;

It is unusual to have two different goto labels with return. Normally
cleanup is structured similar to the code which went wrong.

So something like:

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


    Andrew

---
pw-bot: cr

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

* Re: [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup()
  2026-11-18 23:36 ` Andrew Lunn
@ 2026-11-19  6:05   ` Xiangwu Lai
  0 siblings, 0 replies; 6+ messages in thread
From: Xiangwu Lai @ 2026-11-19  6:05 UTC (permalink / raw)
  To: andrew
  Cc: Xiangwu Lai, chester.a.unal, daniel, olteanv, sean.wang, netdev,
	linux-kernel

Hi Andrew,

thanks for the review.  For the record, this replies to "[PATCH] net:
dsa: mt7530: fix regulator leak in mt7530_setup()":

https://lore.kernel.org/all/20261118014114.1855-1-laixiangwu@cmss.chinamobile.com/

On Thu, Nov 19, 2026 at 12:36:27AM +0100, Andrew Lunn wrote:
> > Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch")
>
> Does this bother anybody?
>
> https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html

The quoted subject is the exact title the commit has in mainline: the
"net-next: " prefix is part of the commit subject itself, Sean Wang's
2017 patch went in with the list prefix kept in the subject, and
"git log --format=%s b8f126a8d543" prints it that way.  I kept the
tag verbatim so that it matches the tree, which is what the stable
rules ask for.  There is nothing to change on my side here unless the
stable maintainers say otherwise.

> It is unusual to have two different goto labels with return. Normally
> cleanup is structured similar to the code which went wrong.
>
> So something like:
>
> > +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;

Agreed, that is the better structure.  v2 follows as a new thread
with exactly this fall-through cleanup and a Suggested-by: tag for
you:

https://lore.kernel.org/all/20261119060424.1476-1-laixiangwu@cmss.chinamobile.com/

Best regards,
Xiangwu Lai



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

* Re: [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup()
  2026-11-18  1:42 ` netdev-bot+sinfo
@ 2026-11-19  6:07   ` Xiangwu Lai
  0 siblings, 0 replies; 6+ messages in thread
From: Xiangwu Lai @ 2026-11-19  6:07 UTC (permalink / raw)
  To: netdev-bot+sinfo
  Cc: Xiangwu Lai, netdev, chester.a.unal, daniel, andrew, olteanv,
	sean.wang, linux-kernel

Hi,

On Wed, Nov 18, 2026 at 01:42:16AM +0000, netdev-bot+sinfo@kernel.org wrote:
> This is an automated message. This series looks like a fix, but its
> commit messages seem to be missing some information:
>
>  - How the issue was discovered, e.g. hit in production, hit during
>    development, syzbot report, manual code inspection, LLM or static
>    analysis tool scan.
>
>  - Whether the issue was actually triggered, or is only theoretical
>    (e.g. found by LLM and not proven at runtime). If it was triggered
>    please include the symptoms, like the stack trace or kernel
>    messages.

Please find the information below, for the patch "[PATCH] net: dsa:
mt7530: fix regulator leak in mt7530_setup()":

https://lore.kernel.org/all/20261118014114.1855-1-laixiangwu@cmss.chinamobile.com/

How the issue was discovered: manual code inspection of the error
paths of mt7530_setup(), while auditing DSA drivers for probe-time
resource leaks.  No scanner or LLM was involved.

Whether the issue was actually triggered: no, it is theoretical.  The
leak needs mt7530_setup() to fail after the two regulators have been
enabled (reset timeout, unsupported chip ID, 20MHz XTAL, VLAN setup,
PHY muxing, GPIO setup or FDB flush failure), which does not happen on
a working system.  If it did happen, the symptom would be both the
core and io supplies of the switch staying powered after the failed
probe, and a WARN_ON(enable_count) splat from the regulator core when
the devm-acquired regulator handles are released with a nonzero
enable count.  No such report is known to me.

Hardware the change was tested on: none, the patch was not tested on
real hardware.  The change only routes existing failure branches to
two new error labels calling regulator_disable(), mirroring what
mt7530_remove() already does on the removal path, so the behaviour on
a successful probe is unchanged.

The series is respun as v2 to address the review feedback received on
the list, and the same information is folded into the v2 commit
message:

https://lore.kernel.org/all/20261119060424.1476-1-laixiangwu@cmss.chinamobile.com/

Best regards,
Xiangwu Lai



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

* Re: [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup()
  2026-11-18  1:41 [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup() Xiangwu Lai
  2026-11-18  1:42 ` netdev-bot+sinfo
  2026-11-18 23:36 ` Andrew Lunn
@ 2026-11-22  2:06 ` netdev-bot+sashiko
  2 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22  2:06 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.
- [Medium] The new err_powers/err_disable_core_pwr paths in
  mt7530_setup() disable core_pwr/io_pwr, but mt7530_remove() was not…
- [Low] This is speculative and depends on the hardware.

--

net: dsa: mt7530: fix regulator leak in mt7530_setup()

This commit adds two error labels to mt7530_setup(). Error paths taken
after the core and io regulators are enabled now disable them again. The
common cleanup is guarded by a priv->id == ID_MT7530 check because MT7621
shares the sw_setup callback but never enables the regulators.

> diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
> index d5a199293f78a..46f827838a519 100644
> --- a/drivers/net/dsa/mt7530.c
> +++ b/drivers/net/dsa/mt7530.c

[ ... ]

> @@ -2579,16 +2581,27 @@ 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]
Is this fix complete? mt7530_setup() is only the sw_setup callback. On
success it returns 0 with both core_pwr and io_pwr enabled.

The real ds->ops->setup is mt753x_setup(), and it can still fail after
that without disabling either supply:

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() can return -EPROBE_DEFER from of_irq_get(), or fail in
devm_regmap_add_irq_chip_fwnode(). mt7530_setup_mdio() can fail in
devm_mdiobus_alloc() or devm_of_mdiobus_register(), and the latter can
also defer.

Later failures in the DSA core are not covered either. These include
dsa_switch_setup_tag_protocol(), user MII bus registration, and
dsa_tree_setup_ports/conduit/lags in dsa_tree_setup(). Those paths call
ds->ops->teardown, but mt753x_teardown() only cancels stats_work.

In all of these cases dsa_register_switch() fails and mt7530_probe()
returns the error. That means mt7530_remove() never runs, and it holds
the only matching regulator_disable() calls.

The devm regulator handles are then released with a non-zero
enable_count, which hits this warning:

drivers/regulator/core.c:_regulator_put() {
	/* Docs say you must disable before calling regulator_put() */
	WARN_ON(regulator->enable_count);
	...
}

The provider's use_count is also never dropped. Each -EPROBE_DEFER retry
enables the supplies again with a new handle, so the count keeps growing
and the switch stays powered and out of reset with no driver bound.

Would it work better to move ownership of the regulators? For example,
probe could enable them and register a devm action to disable them.
Another option is to unwind in mt753x_setup() and mt753x_teardown() and
change mt7530_remove() to match.

> +
> +err_disable_core_pwr:
> +	regulator_disable(priv->core_pwr);
> +	return ret;
> +
> +err_powers:
> +	if (priv->id == ID_MT7530) {
> +		regulator_disable(priv->io_pwr);
> +		regulator_disable(priv->core_pwr);
> +	}

[Severity: Medium]
Can this leave mt7530_remove() disabling the regulators a second time?
mt7530_remove() was not changed, and it 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);
		...
	}
	...
}

Consider a multi-switch DSA tree where the MT7530 probes first.
dsa_tree_setup() returns 0 while the tree is incomplete:

net/dsa/dsa.c:dsa_tree_setup() {
	...
	complete = dsa_tree_setup_routing_table(dst);
	if (!complete)
		return 0;
	...
}

So the MT7530 driver binds before ops->setup has run. When the last
switch of the tree probes, mt753x_setup()->mt7530_setup() runs for the
MT7530 that is already bound.

If mt7530_setup() then fails, err_powers drops the consumer enable_count
to zero. The error goes back to the other switch's probe, and the MT7530
stays bound.

On a later unbind, mt7530_remove() calls regulator_disable() again. That
triggers the "unbalanced disables for %s" WARN in _regulator_disable(),
which returns -EIO. The driver also logs "Failed to disable core power"
and "Failed to disable io pwr".

Before this patch, a failed setup left the regulators enabled, so the
remove path was balanced.

[Severity: Low]
This one is speculative and depends on the hardware, but should the chip
be put back into reset before the supplies are cut here? Every goto
err_powers happens after mt7530_setup() has already released reset:

	if (priv->mcm) {
		reset_control_assert(priv->rstc);
		usleep_range(5000, 5100);
		reset_control_deassert(priv->rstc);
	} else {
		gpiod_set_value_cansleep(priv->reset, 0);
		usleep_range(5000, 5100);
		gpiod_set_value_cansleep(priv->reset, 1);
	}

err_powers only disables io_pwr and core_pwr. Suppose the reset GPIO is
present and the supplies really turn off. The SoC would then keep driving
the reset input at its run level into an unpowered chip, which could
back-power it through the input protection diodes.

mt7530_remove() already disables the regulators in the same order. This
error path is new code, though, and could assert reset first.

> +	return ret;
>  }

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118014114.1855-1-laixiangwu%40cmss.chinamobile.com

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

end of thread, other threads:[~2026-11-22  2:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18  1:41 [PATCH] net: dsa: mt7530: fix regulator leak in mt7530_setup() Xiangwu Lai
2026-11-18  1:42 ` netdev-bot+sinfo
2026-11-19  6:07   ` Xiangwu Lai
2026-11-18 23:36 ` Andrew Lunn
2026-11-19  6:05   ` Xiangwu Lai
2026-11-22  2:06 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox