🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Igor Velkov via B4 Relay Fri, 13 Nov 2026 21:06
[PATCH net v6 4/4] net: phy: postpone PHY interrupts during sleep with MAC-managed PM
#15980
Author: Igor Velkov via B4 Relay
Date: Fri, 13 Nov 2026 21:06
129 lines
16724 bytes
From: Igor Velkov <iav@iav.lv>

mdio_bus_phy_suspend() and mdio_bus_phy_resume() return early when the
MAC driver manages PHY PM, so the PHY never gets irq_suspended and
phy_interrupt() runs the PHY driver's handler at any point of the sleep
transition. If the MAC driver powers the MDIO bus down, the MDIO access
in that handler can stall the CPU. This happens on both sides:

- suspend: the MAC's suspend callback stops the PHY, the link goes
  down, and the handler runs while the MAC powers down;
- resume: resume_device_irqs() re-enables a pending interrupt before
  the MAC resumes.

On Helios64 (dwmac-rk, RTL8211F with its interrupt line described in DT)
both hang the board: the handler reads INSR while the GMAC clocks are
off, after rk_gmac_suspend() or before rk_gmac_resume(). The commit in
the Fixes tag added the postponing after the early return for
MAC-managed PHY PM, and stmmac has set mac_managed_pm since commit
f151c147b3af ("net: stmmac: Enable mac_managed_pm phylink config").

Set irq_suspended for these PHYs too. The PHY device is a child of its
MDIO bus, so mdio_bus_phy_resume() runs after the resume callbacks of
the bus and its parents (with stmmac, the MAC) and replays the postponed
interrupt then. The PM core resumes children of a device whose resume
callback failed, and phylib cannot tell: if the MAC fails to power the
bus up, the replay still reaches the bus. phy_suspend(), phy_resume()
and the state machine stay with the MAC driver, and no MAC driver needs
a change. Doing this in phylink_suspend() instead would miss macb, which
sets mac_managed_pm but suspends with phylink_stop().

A postponed interrupt cannot read the PHY, so it cannot tell Wake-on-LAN
from a link change. A PHY without MAC-managed PM reports what
phy_interrupt() reports today. A PHY with MAC-managed PM reports no
wakeup: the MAC can change the link in its suspend callback, as stmmac
lowers the link speed through phylink_suspend() when the PHY may wake
the system, and a PHY that leaves its link interrupt unmasked would
abort every suspend. Its postponed interrupt still disables the line,
which may then stay masked until resume, so such a PHY may not wake the
system from that sleep, and a magic packet that arrives during the
transition does not abort it.

Tested on Helios64 with 7.3.4, s2idle with the link up:
- no Wake-on-LAN, GMAC clocks off in suspend: 10 of 10 cycles suspended
  and resumed; on 7.3 without this series the same test hung or reset
  the board after the link went down 10 of 10 times;
- MAC power/wakeup enabled, no Wake-on-LAN: 3 of 3 slept;
- Wake-on-LAN in the PHY, so phylink_suspend() also drops the link
  speed: a magic packet woke the board 4 of 4 times, no suspend
  aborted.

Fixes: 1758bde2e4aa ("net: phy: Don't trigger state machine while in suspend")
Signed-off-by: Igor Velkov <iav@iav.lv>
---
Haoning Cheng's Tested-by on v5 is not carried: MAC-managed PHYs no
longer report a wakeup.
---
 drivers/net/phy/phy.c        |  5 ++++-
 drivers/net/phy/phy_device.c | 11 ++++++++---
 include/linux/phy.h          |  4 ++--
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 8e03c99fce8f..ccf0658c4d10 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -1419,7 +1419,10 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
 	if (IS_ENABLED(CONFIG_PM_SLEEP) && READ_ONCE(phydev->irq_suspended)) {
 		struct net_device *netdev = phydev->attached_dev;
 
-		if (netdev) {
+		/* With MAC-managed PM, a postponed interrupt cannot tell
+		 * Wake-on-LAN from a link change the MAC causes.
+		 */
+		if (netdev && !phydev->mac_managed_pm) {
 			struct device *parent = netdev->dev.parent;
 
 			if (netdev->ethtool->wol_enabled)
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 39e232a3144a..c043071784f5 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -369,11 +369,14 @@ static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
 	struct phy_device *phydev = to_phy_device(dev);
 	int ret;
 
+	/* An interrupt can fire while the MAC has the MDIO bus powered
+	 * down, whether or not the MAC manages PHY PM.
+	 */
+	mdio_bus_phy_suspend_irq(phydev);
+
 	if (phydev->mac_managed_pm)
 		return 0;
 
-	mdio_bus_phy_suspend_irq(phydev);
-
 	/* We must stop the state machine manually, otherwise it stops out of
 	 * control, possibly with the phydev->lock held. Upon resume, netdev
 	 * may call phy routines that try to grab the same lock, and that may
@@ -406,8 +409,10 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
 	struct phy_device *phydev = to_phy_device(dev);
 	int ret;
 
-	if (phydev->mac_managed_pm)
+	if (phydev->mac_managed_pm) {
+		mdio_bus_phy_resume_irq(phydev);
 		return 0;
+	}
 
 	if (!phydev->suspended_by_mdio_bus)
 		goto no_resume;
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 96d14b6b544f..3238432989c3 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -649,8 +649,8 @@ struct phy_oatc14_sqi_capability {
  * @mdix_ctrl: User setting of crossover
  * @pma_extable: Cached value of PMA/PMD Extended Abilities Register
  * @interrupts: Flag interrupts have been enabled
- * @irq_suspended: Flag indicating PHY is suspended and therefore interrupt
- *                 handling shall be postponed until PHY has resumed
+ * @irq_suspended: Flag indicating a system sleep transition, so interrupt
+ *                 handling shall be postponed until the PHY has resumed
  * @irq_rerun: Set by the postponed interrupt that disabled the line
  * @default_timestamp: Flag indicating whether we are using the phy
  *		       timestamp as the default one

-- 
2.43.0
Re: [PATCH net v6 4/4] net: phy: postpone PHY interrupts during sleep with MAC-managed PM
#17834
Author: netdev-bot+sashiko
Date: Tue, 17 Nov 2026 12:08
346 lines
21767 bytes
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 4 · Low: 0

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] net: phy: inverted PM IRQ ordering when consumer MAC links to
  PHY
- [Medium] PHY Wake-on-LAN can be lost for MAC-managed PHYs whose
  interrupt line is also their wake IRQ.
- [Medium] The postponed interrupt is lost for MAC-managed PHYs whose
  interrupt is a nested threaded IRQ (for example PHY INT routed through…
- [Medium] A shared IRQ line's disable depth can leak when a MAC-managed
  PHY's interrupt is re-requested from the MAC's resume callback.
- [Medium] net: phy: unbalanced enable_irq() in mdio_bus_phy_resume_irq()
  on shared IRQ

--

net: phy: postpone PHY interrupts during sleep with MAC-managed PM

This commit makes mdio_bus_phy_suspend() and mdio_bus_phy_resume() set
and clear irq_suspended for PHYs whose PM is managed by the MAC driver.
phy_interrupt() then postpones handling during the sleep transition
instead of touching a possibly powered-down MDIO bus. For MAC-managed
PHYs, the postponed interrupt no longer reports a wakeup event.

> diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
> index 8e03c99fce8f5..ccf0658c4d101 100644
> --- a/drivers/net/phy/phy.c
> +++ b/drivers/net/phy/phy.c
> @@ -1419,7 +1419,10 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
>  	if (IS_ENABLED(CONFIG_PM_SLEEP) && READ_ONCE(phydev->irq_suspended)) {
>  		struct net_device *netdev = phydev->attached_dev;
>  
> -		if (netdev) {
> +		/* With MAC-managed PM, a postponed interrupt cannot tell
> +		 * Wake-on-LAN from a link change the MAC causes.
> +		 */
> +		if (netdev && !phydev->mac_managed_pm) {
>  			struct device *parent = netdev->dev.parent;
>  
>  			if (netdev->ethtool->wol_enabled)

[Severity: Medium]
With this patch, a MAC-managed PHY gets irq_suspended set in
mdio_bus_phy_suspend() before the MAC's own suspend callback runs. Any
PHY interrupt from then until mdio_bus_phy_resume() takes this postponed
path. For mac_managed_pm no wakeup is reported, but the line is still
disabled:

		if (!phydev->irq_rerun) {
			phydev->irq_rerun = true;
			disable_irq_nosync(irq);
		}

Could this lose PHY Wake-on-LAN for the whole sleep on PHYs whose
interrupt line is also their wake IRQ? Both rtl8211f_probe() and
an8801r_probe() register phydev->irq as the wake IRQ:

		devm_pm_set_wake_irq(dev, phydev->irq);

In the noirq phase, suspend_device_irq() re-enables a disabled
wake-armed line only when the irqchip has
IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND:

kernel/irq/pm.c:suspend_device_irq() {
    ...
		if ((chipflags & IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND) &&
		     irqd_irq_disabled(irqd)) {
			...
			__enable_irq(desc);
    ...
}

On other irqchips the line would stay masked until resume. A later PME
or magic packet interrupt then could not wake the system.

One concrete case is stmmac with an RTL8211F and PHY WoL enabled. The
link partner changes the link after irq_suspended is set, but before
rtl8211f_suspend() switches INER to PME only:

	if (device_may_wakeup(&phydev->mdio.dev)) {
		ret = phy_write_paged(phydev, 0xa42, RTL821x_INER,
				      RTL8211F_INER_PME);

rtl8211f_suspend() then clears INTB. The software disable of the line
stays in place until mdio_bus_phy_resume_irq().

Before this patch, phy_interrupt() ran handle_interrupt() here and the
oneshot line was unmasked again. On boards whose MDIO bus stays
powered, WoL kept working in this case.

The phylink_speed_down() renegotiation in phylink_suspend() does not
appear to trigger this for RTL8211F or AN8801.
phylink_stop()->phy_stop()->phy_suspend() already restricts the
interrupt mask before that point. It would only matter for a wake IRQ
PHY that leaves link interrupts unmasked in suspend.

The commit message says the line "may then stay masked until resume, so
such a PHY may not wake the system from that sleep". It does not say
that any link event in this window disables WoL for the whole sleep
cycle.

The Helios64 WoL test also runs on gpio-rockchip, and
rockchip_irq_suspend() does this:

	irq_reg_writel(gc, ~gc->wake_active, bank->gpio_regs->int_mask);

That unmasks wake pins in hardware whatever the software disable state
is. Would that test be able to show this regression?

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 39e232a3144a2..c043071784f5b 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -369,11 +369,14 @@ static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
>  	struct phy_device *phydev = to_phy_device(dev);
>  	int ret;
>  
> +	/* An interrupt can fire while the MAC has the MDIO bus powered
> +	 * down, whether or not the MAC manages PHY PM.
> +	 */
> +	mdio_bus_phy_suspend_irq(phydev);
> +
>  	if (phydev->mac_managed_pm)
>  		return 0;

[Severity: Medium]
For a MAC-managed PHY, irq_suspended is now set here, before the MAC's
suspend callback runs. cpsw and cpsw_new set mac_managed_pm, and they
also disconnect the PHY from that callback. cpsw_suspend() calls
cpsw_ndo_stop(), which ends in phy_disconnect(), and cpsw_resume()
connects the PHY again.

Can this leave a stale irq_rerun and an unbalanced enable_irq() on a
board where the two slave PHYs share one interrupt line?

phy_request_interrupt() uses IRQF_ONESHOT | IRQF_SHARED, so phy0 and
phy1 each have an action on the same irq_desc. If the line fires after
both mdio_bus_phy_suspend() calls, both threads run. Each
phy_interrupt() takes the postponed path, sets its own irq_rerun and
calls disable_irq_nosync(). The depth is then 2.

cpsw_suspend() then disconnects phy0. phy_free_interrupt() keeps
phy0->irq_rerun set, because phy1 still has an action on the line:

	if (!irq_has_action(phydev->irq))
		phydev->irq_rerun = false;

Disconnecting phy1 removes the last action. free_irq() shuts the line
down and phy1->irq_rerun is cleared, but phy0->irq_rerun stays true.

On resume, cpsw_resume() runs before mdio_bus_phy_resume() and calls
phy_request_interrupt() again. With no actions left on the line,
__setup_irq() calls irq_startup() and the depth goes back to 0.
mdio_bus_phy_resume_irq() for phy0 still sees irq_rerun:

	if (phydev->irq_rerun) {
		phydev->irq_rerun = false;
		enable_irq(phydev->irq);
		irq_wake_thread(phydev->irq, phydev);
	}

Wouldn't that enable_irq() hit the unbalanced enable WARN in
__enable_irq()?

The stale flag also matters before that point. Suppose the line fires
after cpsw_resume() has requested phy0's IRQ but before
mdio_bus_phy_resume(). phy_interrupt() for phy0 sees irq_rerun already
set and skips disable_irq_nosync(). It returns IRQ_HANDLED without
touching the PHY. If phy1 has not been reconnected yet, nothing masks a
level-triggered line, so the oneshot thread keeps being woken.

MAC-managed PHYs could not get here before, because irq_suspended was
never set for them. Should phy_free_interrupt() undo this PHY's own
disable_irq_nosync() whenever irq_rerun is set, instead of basing the
cleanup on irq_has_action()?

[Severity: Medium]
For MAC-managed PHYs, irq_suspended is now set here and cleared only in
mdio_bus_phy_resume(). That runs after the MAC's resume callback, so
the flag is still true while the MAC resumes.

ravb and sh_eth set mac_managed_pm. They call phy_disconnect() in
suspend and of_phy_connect() in resume. So the PHY IRQ is requested
again while irq_suspended is true:

ravb_resume()->ravb_open()->...->phy_request_interrupt()

Can this leave a shared IRQ line disabled?

On an IRQF_SHARED line, the other device can interrupt after
request_threaded_irq(). phy_interrupt() then takes the postponed path,
sets irq_rerun and calls disable_irq_nosync(). Suppose
phy_enable_interrupts() then fails:

drivers/net/phy/phy.c:phy_request_interrupt() {
    ...
		if (phy_enable_interrupts(phydev)) {
			phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
			phy_free_interrupt(phydev);
			phydev->irq = PHY_POLL;
		}
    ...
}

phy_free_interrupt() clears irq_rerun only when the line has no other
actions left. On a shared line it still has one:

	if (!irq_has_action(phydev->irq))
		phydev->irq_rerun = false;

mdio_bus_phy_resume_irq() then returns early because the IRQ is now
PHY_POLL:

	WRITE_ONCE(phydev->irq_suspended, false);
	if (!phy_interrupt_is_valid(phydev))
		return;

The matching enable_irq() never runs. Wouldn't the other device on that
line stop getting interrupts, with irq_rerun left stale?

MAC-managed PHYs could not reach this path before, because irq_suspended
was never set for them.

>  
> -	mdio_bus_phy_suspend_irq(phydev);
> -
>  	/* We must stop the state machine manually, otherwise it stops out of
>  	 * control, possibly with the phydev->lock held. Upon resume, netdev
>  	 * may call phy routines that try to grab the same lock, and that may

[ ... ]

> @@ -406,8 +409,10 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
>  	struct phy_device *phydev = to_phy_device(dev);
>  	int ret;
>  
> -	if (phydev->mac_managed_pm)
> +	if (phydev->mac_managed_pm) {
> +		mdio_bus_phy_resume_irq(phydev);
>  		return 0;
> +	}

[Severity: High]
The ordering argument in the commit message needs the MAC to be the
MDIO bus parent or one of its ancestors. That does not hold when the
PHY sits on an MDIO bus owned by another device. In that case
phy_attach_direct() calls device_link_add() with dev->dev.parent as
consumer and &phydev->mdio.dev as supplier.

device_link_add() moves the consumer to the tail of dpm_list. The MAC
then suspends before mdio_bus_phy_suspend() and resumes after
mdio_bus_phy_resume(). Examples are two FEC instances with
FEC_QUIRK_SINGLE_MDIO, dual stmmac or macb sharing one MDIO bus, and
MACs behind an mdio-mux.

On resume, mdio_bus_phy_resume() for such a PHY clears irq_suspended
and replays the postponed interrupt before the consumer's resume
callback has run. handle_interrupt() then reads the PHY before
fec_resume() has re-enabled fep->reg_phy and fep->clk_enet_out. It
also runs before phy_resume() has turned the PHY driver's own clock
back on, as rtl821x_resume() does with priv->clk. Interrupts after this
point are not postponed either.

If that MDIO access fails, the handler calls phy_error() and the state
becomes PHY_ERROR. phy_start() from the MAC's resume only accepts
PHY_READY or PHY_HALTED, so it hits its WARN and returns without
starting the PHY. The link then stays down after resume.

If the access succeeds, phydev->state is still PHY_HALTED.
phy_trigger_machine() then queues a state machine run that takes the
PHY_STATE_WORK_SUSPEND path and calls phy_suspend() outside
phydev->lock. Can that race with the MAC's later
phy_start()->__phy_resume()?

The suspend side is not covered for this topology either. The
consumer's suspend callback runs before mdio_bus_phy_suspend() sets
irq_suspended. That callback may call phy_stop(), call
phylink_speed_down() as stmmac_suspend() does, or gate clocks and
regulators as fec_suspend() does. A link-down interrupt caused there
still runs the handler directly.

So for these PHYs the patch only moves the handler from
resume_device_irqs() to mdio_bus_phy_resume(), still ahead of the MAC.
Should the replay for a PHY with phydev->devlink wait until the
consumer has resumed? Or should the commit message say that this
topology is not covered?

[Severity: Medium]
For a MAC-managed PHY, this replay now runs without a phy_init_hw() call
before it:

	if (phydev->irq_rerun) {
		phydev->irq_rerun = false;
		enable_irq(phydev->irq);
		irq_wake_thread(phydev->irq, phydev);
	}

Is the postponed interrupt lost when the PHY interrupt is a nested
threaded IRQ? One example is a PHY INT routed through an I2C or SPI GPIO
expander such as pca953x.

For a nested IRQ, __setup_irq() creates no thread. irq_wake_thread()
only wakes a thread that exists:

kernel/irq/manage.c:irq_wake_thread() {
    ...
		if (action->dev_id == dev_id) {
			if (action->thread)
				__irq_wake_thread(desc, action);
			break;
		}
    ...
}

The postponed path in phy_interrupt() disabled the line without reading
the PHY's interrupt status. Getting there only takes the MAC's suspend
stopping the PHY and the link going down, as the commit message
describes.

After resume, nothing reads the latched status (for example RTL8211F
INSR), so INT stays asserted. An edge-triggered expander input would
then never see a new edge.

For non-MAC-managed PHYs this is hidden: mdio_bus_phy_resume() calls
phy_init_hw()->config_intr() before the replay. stmmac_resume() does not
call phy_init_hw(), while fec_resume() does.

The state machine is in interrupt mode and does not poll. On such stmmac
boards, could link up go unreported after resume, leaving the network
down until the interface is reinitialised?

>  
>  	if (!phydev->suspended_by_mdio_bus)
>  		goto no_resume;

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113-b4-phylib-mac-pm-irq-v3-v6-0-05af2503cb23%40iav.lv
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads