Netdev List
 help / color / mirror / Atom feed
* [PATCH net v7 0/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM
@ 2026-11-19  7:23 Igor Velkov via B4 Relay
  2026-11-19  7:23 ` [PATCH net v7 1/5] net: phy: move irq_suspended and irq_rerun out of the bitfield Igor Velkov via B4 Relay
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-11-19  7:23 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Thomas Gleixner, linux-pm, Maxime Chevallier, Lukas Wunner,
	Rafael J. Wysocki, Wei Fang, Niklas Söderlund, Paul Barker,
	Florian Fainelli, Doug Berger, Thangaraj Samynathan, Shenwei Wang,
	Haoning Cheng, netdev, linux-arm-kernel, linux-rockchip,
	linux-kernel, Igor Velkov

When the MAC driver manages PHY PM, phylib never sets irq_suspended, and
a PHY interrupt during the sleep transition runs the PHY driver's
handler while the MDIO bus may be powered down. On Helios64 (dwmac-rk)
this hangs the board on suspend as well as on resume.

mac_managed_pm hands phy_suspend(), phy_resume() and the state machine
to the MAC driver, since commit fba863b81604 ("net: phy: make PHY PM
ops a no-op if MAC driver manages PHY PM"); the PHY interrupt stays
with phylib, and so does postponing it.

Patches 1-4 fix the postponing that phylib applies to other PHYs, so
that patch 5 can extend it to MAC-managed ones. Patch 1 moves the two
flags out of a bitfield they share with the link state written under
phydev->lock. Patch 2 fixes three paths that unbalance the line of a
postponed interrupt: an edge that reruns the IRQ thread, a PHY that
frees its interrupt during the transition so that the line restarts,
and a PHY that falls back to polling. Patch 3 undoes the postponing
when phy_suspend() fails. Patch 4 runs the postponed handler directly,
as a nested threaded interrupt has no thread to wake, and not for a PHY
that has freed its interrupt. Patch 5 sets irq_suspended for
MAC-managed PHYs too; their postponed interrupts report no wakeup.
Patch 5 also enables a wakeup line that a postponed interrupt left
disabled for the noirq phase, for any PHY.

Patch 5 fixes a hang seen on Helios64 (dwmac-rk, RTL8211F): in s2idle
without WoL the board hung or reset 10 of 10 times without the series.
The PHY IRQ thread stalls its CPU on an MDIO read while the GMAC clocks
are off, so that CPU never answers the NMI:

  watchdog: CPU0: Watchdog detected hard LOCKUP on cpu 1
  After 10 seconds, these CPUS still haven't responded to the NMI: 1

Patches 1-4 fix defects found by code inspection and Sashiko review;
they were not triggered.

With MAC-managed PM, a postponed interrupt cannot tell Wake-on-LAN from
a link change that the MAC causes, so it reports no wakeup. The line it
disabled may stay masked for the whole sleep on an irqchip without
IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND; patch 5 enables such a line for the
noirq phase when it is armed for wakeup, so an event the PHY still
signals wakes the system or aborts the suspend. On a level-triggered
line, a PHY that keeps a link interrupt asserted then aborts every
suspend with Wake-on-LAN enabled; on an edge-triggered line, a magic
packet that arrives during the transition does not abort it.

Not covered: MDIO accesses from closing or opening the interface during
the transition, and a MAC resume that fails to power the bus up. On a
shared line, a postponed PHY interrupt disables the line for its other
users until the PHY resumes. After all users have freed a shared line,
a PHY whose postponed interrupt disabled it still gets an unbalanced
enable_irq() at resume if another user requests the line before it
does; so does a PHY whose phy_free_interrupt() races with a request of
the line between free_irq() and irq_has_action(). When a PHY sits on an
MDIO bus its MAC does not own, the MAC suspends before and resumes after
the PHY: an interrupt in between still runs the handler, and the replay
still runs before the MAC resumes; ODROID-HC4 takes this path. When
mdio_bus_phy_resume() fails, the interrupts stay postponed.
mdio_bus_phy_may_suspend() does not ask phy_may_wakeup() about an
attached PHY.

Tested on Helios64 (dwmac-rk, RTL8211F), the noirq part of patch 5
under a flood of magic packets. Haoning Cheng tested v5 on i.MX6 (fec).

Changes in v7:
- patch 2: the postponed interrupt records the line it disabled;
  phy_request_interrupt() clears irq_rerun when the line has no users,
  so a line restarted after all its users freed it is not enabled once
  more at resume; mdio_bus_phy_resume() enables the recorded line also
  for a PHY that fell back to polling (Sashiko on v6)
- new patch 4: the postponed handler runs directly from
  mdio_bus_phy_resume(); irq_wake_thread() does nothing for a nested
  threaded interrupt (Sashiko on v6); the handler does not run for a PHY
  that has freed its interrupt, and phy_free_interrupt() waits for it
- patch 5: the commit message covers a PHY on an MDIO bus its MAC does
  not own (Sashiko on v6); noirq callbacks enable a wakeup line that a
  postponed interrupt left disabled
- tests rerun on v7
- Link to v6: https://lore.kernel.org/r/20261113-b4-phylib-mac-pm-irq-v3-v6-0-05af2503cb23@iav.lv

Changes in v6:
- patch 2: phy_free_interrupt() enables no line; it clears irq_rerun
  when the PHY was alone on the line, and mdio_bus_phy_resume()
  enables a shared line. Enabling it in phy_free_interrupt() could
  unmask a source that phy_disable_interrupts() failed to clear, or
  unbalance a wakeup line in the noirq phase (Sashiko on v5)
- patch 3: the resume error path is left as in net; clearing
  irq_suspended there let the handler reach a PHY that failed to
  resume (Sashiko on v5)
- patch 4: with mac_managed_pm, a postponed interrupt reports no
  wakeup, and other PHYs report what they report in net; irq_wake,
  wol_phy and the barriers are gone. A wakeup on Wake-on-LAN in the
  PHY let link interrupts that the MAC causes abort suspend on PHYs
  that leave them unmasked (Sashiko on v5)
- patch 1: the comment names the writers of the two flags (Sashiko on
  v5)
- patch 2: irq_pm_lock sits next to lock in struct phy_device
- comments say again that postponing lasts until the PHY has resumed
- the cover letter says what was reproduced (Jakub)
- Haoning Cheng's Tested-by on v5 kept on patch 1 only
- rebased on net
- tests rerun on v6
- Link to v5: https://lore.kernel.org/r/20261111-b4-phylib-mac-pm-irq-v3-v5-0-bbfdb5afb7d3@iav.lv

Changes in v5:
- v4 patch 2 alone could leave irq_rerun set across
  phy_free_interrupt() and a new request, after which a postponed
  level interrupt fired without end; it is merged with v4 patch 3 into
  patch 2. phy_free_interrupt() re-enables the line after free_irq(),
  not before, and only while other users of a shared line remain, so
  the handler cannot keep running on a source it cannot quiet;
  irq_rerun stays a bool (Sashiko on v4)
- patch 3: when the PHY fails to resume, the line a postponed
  interrupt disabled stays off, as before this series, instead of
  rerunning a handler that may not reach the PHY (Sashiko on v4)
- patch 4: with mac_managed_pm, only Wake-on-LAN in the PHY makes a
  postponed interrupt a wakeup; the ethtool flag may describe
  Wake-on-LAN in the MAC, as with fec (Sashiko on v4); for a PHY that
  phy_suspend() already suspended, the PHY part of its Wake-on-LAN
  state is kept in the new wol_phy
- patch 4: Fixes: now points at 1758bde2e4aa, which added the
  postponing after the early return for MAC-managed PHY PM
- comments say that postponing lasts until the transition ends
- Haoning Cheng's Tested-by on v4 kept on patch 1 only: patches 2-4
  changed
- rebased on net
- tests rerun on v5
- Link to v4: https://lore.kernel.org/r/20261031-b4-phylib-mac-pm-irq-v3-v4-0-5ba23b3ff9a4@iav.lv

Changes in v4:
- new patches 2-4, one pre-existing defect each
- patch 2: an edge that reruns the IRQ thread no longer disables the
  line twice
- patch 3: phy_free_interrupt() during the transition keeps the line
  balanced (Sashiko on v3)
- patch 4: a failing PHY PM callback no longer leaves the interrupt
  postponed (Sashiko on v2 and v3)
- patch 5: the wakeup event is decided in mdio_bus_phy_suspend(), where
  phy_may_wakeup() can still read the PHY (Sashiko on v3); Fixes: now
  points at the stmmac commit that took its PHYs out of postponing
- v3 asked phylib or phylink_suspend(): phylib, as macb sets
  mac_managed_pm but suspends with phylink_stop()
- tests rerun on v4
- Link to v3: https://lore.kernel.org/r/20261029-b4-phylib-mac-pm-irq-v3-v3-0-5907045f3210@iav.lv

Changes in v3:
- with mac_managed_pm, a postponed interrupt signals a wakeup event only
  with Wake-on-LAN; with MAC power/wakeup enabled and no WoL, v2 aborted
  s2idle on Helios64 3 of 3 times, v3 0 of 3 (reported by Sashiko)
- phy_free_interrupt(): stop postponing and re-enable the line before
  free_irq(), not after it (reported by Sashiko)
- mdio_bus_phy_suspend() comment narrowed to the MDIO bus (reported by
  Sashiko)
- patch 1: Andrew suggested taking phydev->lock in phy_interrupt();
  the commit message says why that does not close the race
- tests rerun on v3
- Link to v2: https://lore.kernel.org/r/20261026045343.837889-1-iav@iav.lv

Signed-off-by: Igor Velkov <iav@iav.lv>
---
Igor Velkov (5):
      net: phy: move irq_suspended and irq_rerun out of the bitfield
      net: phy: fix three paths that unbalance a postponed PHY interrupt line
      net: phy: stop postponing PHY interrupts when phy_suspend() fails
      net: phy: run a postponed PHY interrupt handler directly on resume
      net: phy: postpone PHY interrupts during sleep with MAC-managed PM

 drivers/net/phy/phy.c        |  32 +++++++++--
 drivers/net/phy/phy_device.c | 128 ++++++++++++++++++++++++++++++++++---------
 include/linux/phy.h          |  25 +++++++--
 3 files changed, 150 insertions(+), 35 deletions(-)
---
base-commit: ead6fa9a0ab194f02a2f36d634b193e9c5f9a1c6
change-id: 20261029-b4-phylib-mac-pm-irq-v3-f656d4e1c64f

Best regards,
-- 
Igor Velkov <iav@iav.lv>



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

* [PATCH net v7 1/5] net: phy: move irq_suspended and irq_rerun out of the bitfield
  2026-11-19  7:23 [PATCH net v7 0/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
@ 2026-11-19  7:23 ` Igor Velkov via B4 Relay
  2026-11-19  7:23 ` [PATCH net v7 2/5] net: phy: fix three paths that unbalance a postponed PHY interrupt line Igor Velkov via B4 Relay
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-11-19  7:23 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Thomas Gleixner, linux-pm, Maxime Chevallier, Lukas Wunner,
	Rafael J. Wysocki, Wei Fang, Niklas Söderlund, Paul Barker,
	Florian Fainelli, Doug Berger, Thangaraj Samynathan, Shenwei Wang,
	Haoning Cheng, netdev, linux-arm-kernel, linux-rockchip,
	linux-kernel, Igor Velkov

From: Igor Velkov <iav@iav.lv>

irq_suspended and irq_rerun share a word with link, autoneg_complete
and interrupts. phy_interrupt() sets irq_rerun from the IRQ thread
without phydev->lock, while the state machine writes link and
autoneg_complete under it and phy_request_interrupt()/
phy_free_interrupt() write interrupts, so one read-modify-write can
undo another. A lost irq_rerun leaves the PHY interrupt disabled after
resume, as phy_interrupt() has already disabled the line.

The last patch of this series sets irq_suspended for PHYs whose MAC
driver manages PHY PM; with stmmac that happens before the MAC stops
the PHY, which widens the window.

Taking phydev->lock in phy_interrupt() alone does not close it:
phy_suspend() stores suspended in the same word and cannot hold the
lock across phy_may_wakeup(), which may take it, and phy_init_hw() and
mdio_bus_phy_suspend() write the word unlocked too. Locking every
writer, MAC drivers included, is a wide audit, and one missed now or
added later reopens the race.

Make both flags bools.

Fixes: 1758bde2e4aa ("net: phy: Don't trigger state machine while in suspend")
Tested-by: Haoning Cheng <Haoning.CHENG@cn.bosch.com>
Signed-off-by: Igor Velkov <iav@iav.lv>
---
 drivers/net/phy/phy.c        | 2 +-
 drivers/net/phy/phy_device.c | 6 +++---
 include/linux/phy.h          | 7 +++++--
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index fce9bc7be330..9af303d60bd6 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -1430,7 +1430,7 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
 				pm_wakeup_dev_event(parent, 0, true);
 		}
 
-		phydev->irq_rerun = 1;
+		phydev->irq_rerun = true;
 		disable_irq_nosync(irq);
 		return IRQ_HANDLED;
 	}
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index b39e945cebd7..1791d578f4dd 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -344,7 +344,7 @@ static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
 	 * has resumed. Wait for concurrent interrupt handler to complete.
 	 */
 	if (phy_interrupt_is_valid(phydev)) {
-		phydev->irq_suspended = 1;
+		phydev->irq_suspended = true;
 		synchronize_irq(phydev->irq);
 	}
 
@@ -394,14 +394,14 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
 		return ret;
 no_resume:
 	if (phy_interrupt_is_valid(phydev)) {
-		phydev->irq_suspended = 0;
+		phydev->irq_suspended = false;
 		synchronize_irq(phydev->irq);
 
 		/* Rerun interrupts which were postponed by phy_interrupt()
 		 * because they occurred during the system sleep transition.
 		 */
 		if (phydev->irq_rerun) {
-			phydev->irq_rerun = 0;
+			phydev->irq_rerun = false;
 			enable_irq(phydev->irq);
 			irq_wake_thread(phydev->irq, phydev);
 		}
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 21afc2082b17..59c75296d535 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -726,8 +726,11 @@ struct phy_device {
 
 	/* Interrupts are enabled */
 	unsigned interrupts:1;
-	unsigned irq_suspended:1;
-	unsigned irq_rerun:1;
+	/* Not bitfields: phy_interrupt() and the PM callbacks write them
+	 * without phydev->lock
+	 */
+	bool irq_suspended;
+	bool irq_rerun;
 
 	unsigned default_timestamp:1;
 

-- 
2.43.0



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

* [PATCH net v7 2/5] net: phy: fix three paths that unbalance a postponed PHY interrupt line
  2026-11-19  7:23 [PATCH net v7 0/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
  2026-11-19  7:23 ` [PATCH net v7 1/5] net: phy: move irq_suspended and irq_rerun out of the bitfield Igor Velkov via B4 Relay
@ 2026-11-19  7:23 ` Igor Velkov via B4 Relay
  2026-11-22 22:03   ` netdev-bot+sashiko
  2026-11-19  7:23 ` [PATCH net v7 3/5] net: phy: stop postponing PHY interrupts when phy_suspend() fails Igor Velkov via B4 Relay
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-11-19  7:23 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Thomas Gleixner, linux-pm, Maxime Chevallier, Lukas Wunner,
	Rafael J. Wysocki, Wei Fang, Niklas Söderlund, Paul Barker,
	Florian Fainelli, Doug Berger, Thangaraj Samynathan, Shenwei Wang,
	Haoning Cheng, netdev, linux-arm-kernel, linux-rockchip,
	linux-kernel, Igor Velkov

From: Igor Velkov <iav@iav.lv>

phy_interrupt() postpones interrupts during a system sleep transition:
it sets irq_rerun and disables the line, and mdio_bus_phy_resume()
re-enables the line once and reruns the handler. Three paths leave the
line unbalanced:

- On an edge-triggered line the IRQ core does not mask a ONESHOT
  interrupt, so an edge that arrives before disable_irq_nosync() runs
  the thread again. That second run disables the line again, and the
  line stays disabled after resume.
- phy_free_interrupt() can run between the postponed interrupt and
  mdio_bus_phy_resume() and leaves irq_rerun set. Once the line has no
  users, free_irq() shuts it down and a new request starts it at depth
  0, so the enable_irq() in mdio_bus_phy_resume() is unbalanced. On a
  shared line this can happen once the other users free it too, as with
  the two PHYs of dra72-evm-revc behind cpsw.
- If phy_request_interrupt() then leaves the PHY polling,
  mdio_bus_phy_resume() returns before enable_irq(), and the other
  users of a shared line stay disabled.

MAC drivers that close the interface in their suspend callback, such as
cpsw, and ravb with Wake-on-LAN off, reach phy_free_interrupt() through
phy_disconnect(), and so does dev_close() in a kernel-internal reset of
the MAC such as stmmac_reset_subtask(), whose workqueue does not freeze.
These drivers set mac_managed_pm, which a later patch brings under
postponing.

Let only the run that sets irq_rerun disable the line, and record the
line it disabled. Clear irq_rerun where the line starts again at depth
0: in phy_free_interrupt() when free_irq() leaves the line without
users, and in phy_request_interrupt() when the line has no users. Otherwise
mdio_bus_phy_resume() enables the recorded line, also for a PHY that
fell back to polling, and reruns the handler only if the PHY still uses
that line. Enabling the line in phy_free_interrupt() instead would
unmask a source that phy_disable_interrupts() failed to clear, and
inside the noirq phase it would unbalance a wakeup line that
suspend_device_irqs() enabled (IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND).

Add irq_pm_lock, so that requesting and freeing the interrupt do not
change irq_rerun between the check and the enable_irq() of the rerun
after resume, and move the postponing and the rerun into helpers.
phy_interrupt() never takes the lock, so synchronize_irq() and
free_irq() can wait for the handler under it. irq_rerun and
irq_rerun_line are plain fields: one thread runs the handler, and the
other paths access them only under the lock, after free_irq() or
synchronize_irq(), or before this PHY has requested the line. Mark the
irq_suspended accesses with READ_ONCE() and WRITE_ONCE(): the handler
reads it while the PM callbacks write it.

Two cases on a shared line stay unbalanced: after all users have freed
the line, another user requests it first, so it has users when this PHY
requests it; or another user requests it between free_irq() and the
irq_has_action() check in phy_free_interrupt(). irq_rerun then stays,
and the enable after resume is unbalanced.

This does not keep close and open off the MDIO bus: on close,
phy_disconnect() calls phy_stop(), phy_free_interrupt() with its
phy_disable_interrupts() and, in phy_detach(), phy_suspend(); on open,
phy_attach_direct() calls phy_init_hw() and phy_request_interrupt()
calls phy_enable_interrupts(). A MAC driver that powers its MDIO bus
down during sleep must not close or open the interface until the bus is
back.

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: phy_free_interrupt()
no longer enables a shared line.
---
 drivers/net/phy/phy.c        | 24 ++++++++++++++++---
 drivers/net/phy/phy_device.c | 57 ++++++++++++++++++++++++++++----------------
 include/linux/phy.h          | 11 +++++++--
 3 files changed, 66 insertions(+), 26 deletions(-)

diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 9af303d60bd6..92ee4651bdbc 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -1416,7 +1416,7 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
 	/* Wakeup interrupts may occur during a system sleep transition.
 	 * Postpone handling until the PHY has resumed.
 	 */
-	if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
+	if (IS_ENABLED(CONFIG_PM_SLEEP) && READ_ONCE(phydev->irq_suspended)) {
 		struct net_device *netdev = phydev->attached_dev;
 
 		if (netdev) {
@@ -1430,8 +1430,12 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
 				pm_wakeup_dev_event(parent, 0, true);
 		}
 
-		phydev->irq_rerun = true;
-		disable_irq_nosync(irq);
+		/* Disable the line only once: an edge can rerun the thread. */
+		if (!phydev->irq_rerun) {
+			phydev->irq_rerun_line = irq;
+			phydev->irq_rerun = true;
+			disable_irq_nosync(irq);
+		}
 		return IRQ_HANDLED;
 	}
 
@@ -1480,6 +1484,12 @@ void phy_request_interrupt(struct phy_device *phydev)
 {
 	int err;
 
+	/* A line without users restarts at depth 0, so our disable is gone */
+	mutex_lock(&phydev->irq_pm_lock);
+	if (phydev->irq_rerun && !irq_has_action(phydev->irq_rerun_line))
+		phydev->irq_rerun = false;
+	mutex_unlock(&phydev->irq_pm_lock);
+
 	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
 				   IRQF_ONESHOT | IRQF_SHARED,
 				   phydev_name(phydev), phydev);
@@ -1507,7 +1517,15 @@ EXPORT_SYMBOL(phy_request_interrupt);
 void phy_free_interrupt(struct phy_device *phydev)
 {
 	phy_disable_interrupts(phydev);
+
+	mutex_lock(&phydev->irq_pm_lock);
 	free_irq(phydev->irq, phydev);
+	/* free_irq() shuts down a line left without users and the next request
+	 * resets its depth; mdio_bus_phy_resume_irq() enables a shared line.
+	 */
+	if (!irq_has_action(phydev->irq))
+		phydev->irq_rerun = false;
+	mutex_unlock(&phydev->irq_pm_lock);
 }
 EXPORT_SYMBOL(phy_free_interrupt);
 
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 1791d578f4dd..f2e2370b4be7 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -332,6 +332,39 @@ static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
 	return !phydev->suspended;
 }
 
+/* Wakeup interrupts may occur during the system sleep transition when
+ * the PHY is inaccessible. Set flag to postpone handling until the PHY
+ * has resumed. Wait for concurrent interrupt handler to complete.
+ */
+static __maybe_unused void mdio_bus_phy_suspend_irq(struct phy_device *phydev)
+{
+	if (!phy_interrupt_is_valid(phydev))
+		return;
+
+	WRITE_ONCE(phydev->irq_suspended, true);
+	synchronize_irq(phydev->irq);
+}
+
+/* Rerun interrupts which were postponed by phy_interrupt()
+ * because they occurred during the system sleep transition.
+ */
+static __maybe_unused void mdio_bus_phy_resume_irq(struct phy_device *phydev)
+{
+	WRITE_ONCE(phydev->irq_suspended, false);
+
+	mutex_lock(&phydev->irq_pm_lock);
+	if (phy_interrupt_is_valid(phydev))
+		synchronize_irq(phydev->irq);
+	/* The PHY may have fallen back to polling since it disabled the line */
+	if (phydev->irq_rerun) {
+		phydev->irq_rerun = false;
+		enable_irq(phydev->irq_rerun_line);
+		if (phydev->irq == phydev->irq_rerun_line)
+			irq_wake_thread(phydev->irq, phydev);
+	}
+	mutex_unlock(&phydev->irq_pm_lock);
+}
+
 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
 {
 	struct phy_device *phydev = to_phy_device(dev);
@@ -339,14 +372,7 @@ static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
 	if (phydev->mac_managed_pm)
 		return 0;
 
-	/* Wakeup interrupts may occur during the system sleep transition when
-	 * the PHY is inaccessible. Set flag to postpone handling until the PHY
-	 * has resumed. Wait for concurrent interrupt handler to complete.
-	 */
-	if (phy_interrupt_is_valid(phydev)) {
-		phydev->irq_suspended = true;
-		synchronize_irq(phydev->irq);
-	}
+	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
@@ -393,19 +419,7 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
 	if (ret < 0)
 		return ret;
 no_resume:
-	if (phy_interrupt_is_valid(phydev)) {
-		phydev->irq_suspended = false;
-		synchronize_irq(phydev->irq);
-
-		/* Rerun interrupts which were postponed by phy_interrupt()
-		 * because they occurred during the system sleep transition.
-		 */
-		if (phydev->irq_rerun) {
-			phydev->irq_rerun = false;
-			enable_irq(phydev->irq);
-			irq_wake_thread(phydev->irq, phydev);
-		}
-	}
+	mdio_bus_phy_resume_irq(phydev);
 
 	if (phy_uses_state_machine(phydev))
 		phy_start_machine(phydev);
@@ -711,6 +725,7 @@ struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
 	dev->max_n_ports = 1;
 
 	mutex_init(&dev->lock);
+	mutex_init(&dev->irq_pm_lock);
 	INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
 
 	/* Request the appropriate module unconditionally; don't
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 59c75296d535..f370690d62a7 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -651,8 +651,8 @@ struct phy_oatc14_sqi_capability {
  * @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_rerun: Flag indicating interrupts occurred while PHY was suspended,
- *             requiring a rerun of the interrupt handler after resume
+ * @irq_rerun: Set by the postponed interrupt that disabled the line
+ * @irq_rerun_line: The line the postponed interrupt disabled
  * @default_timestamp: Flag indicating whether we are using the phy
  *		       timestamp as the default one
  * @interface: enum phy_interface_t value
@@ -675,6 +675,8 @@ struct phy_oatc14_sqi_capability {
  * @n_ports: Number of ports currently attached to the PHY
  * @max_n_ports: Max number of ports this PHY can expose
  * @lock:  Mutex for serialization access to PHY
+ * @irq_pm_lock: Serializes requesting and freeing the interrupt against the
+ *               PM callbacks; phy_interrupt() never takes it
  * @state_queue: Work queue for state machine
  * @link_down_events: Number of times link was lost
  * @shared: Pointer to private data shared by phys in one package
@@ -731,6 +733,7 @@ struct phy_device {
 	 */
 	bool irq_suspended;
 	bool irq_rerun;
+	int irq_rerun_line;
 
 	unsigned default_timestamp:1;
 
@@ -808,6 +811,10 @@ struct phy_device {
 	struct delayed_work state_queue;
 
 	struct mutex lock;
+	/* Serializes requesting and freeing the interrupt against the PM
+	 * callbacks
+	 */
+	struct mutex irq_pm_lock;
 
 	/* This may be modified under the rtnl lock */
 	bool sfp_bus_attached;

-- 
2.43.0



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

* [PATCH net v7 3/5] net: phy: stop postponing PHY interrupts when phy_suspend() fails
  2026-11-19  7:23 [PATCH net v7 0/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
  2026-11-19  7:23 ` [PATCH net v7 1/5] net: phy: move irq_suspended and irq_rerun out of the bitfield Igor Velkov via B4 Relay
  2026-11-19  7:23 ` [PATCH net v7 2/5] net: phy: fix three paths that unbalance a postponed PHY interrupt line Igor Velkov via B4 Relay
@ 2026-11-19  7:23 ` Igor Velkov via B4 Relay
  2026-11-19  7:24 ` [PATCH net v7 4/5] net: phy: run a postponed PHY interrupt handler directly on resume Igor Velkov via B4 Relay
  2026-11-19  7:24 ` [PATCH net v7 5/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
  4 siblings, 0 replies; 9+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-11-19  7:23 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Thomas Gleixner, linux-pm, Maxime Chevallier, Lukas Wunner,
	Rafael J. Wysocki, Wei Fang, Niklas Söderlund, Paul Barker,
	Florian Fainelli, Doug Berger, Thangaraj Samynathan, Shenwei Wang,
	Haoning Cheng, netdev, linux-arm-kernel, linux-rockchip,
	linux-kernel, Igor Velkov

From: Igor Velkov <iav@iav.lv>

When phy_suspend() fails in mdio_bus_phy_suspend(), irq_suspended
stays set and the state machine stays stopped: the PM core does not
resume a device whose suspend callback failed, and the next PHY
interrupt disables the line until the next resume.

Undo the postponing and restart the state machine when phy_suspend()
fails. Set suspended_by_mdio_bus only after phy_suspend() succeeds.
Set before it, the flag outlived a failed suspend, and in a later cycle
where mdio_bus_phy_may_suspend() returns false, mdio_bus_phy_resume()
ran phy_init_hw() and phy_resume() on a PHY that the MDIO bus had not
suspended.

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: this patch no longer
changes the resume error path.
---
 drivers/net/phy/phy_device.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index f2e2370b4be7..2d2058c29f30 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -368,6 +368,7 @@ static __maybe_unused void mdio_bus_phy_resume_irq(struct phy_device *phydev)
 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
 {
 	struct phy_device *phydev = to_phy_device(dev);
+	int ret;
 
 	if (phydev->mac_managed_pm)
 		return 0;
@@ -385,9 +386,20 @@ static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
 	if (!mdio_bus_phy_may_suspend(phydev))
 		return 0;
 
+	ret = phy_suspend(phydev);
+	if (ret) {
+		/* The PM core skips the resume callback of a device whose
+		 * suspend callback failed.
+		 */
+		mdio_bus_phy_resume_irq(phydev);
+		if (phy_uses_state_machine(phydev))
+			phy_start_machine(phydev);
+		return ret;
+	}
+
 	phydev->suspended_by_mdio_bus = 1;
 
-	return phy_suspend(phydev);
+	return 0;
 }
 
 static __maybe_unused int mdio_bus_phy_resume(struct device *dev)

-- 
2.43.0



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

* [PATCH net v7 4/5] net: phy: run a postponed PHY interrupt handler directly on resume
  2026-11-19  7:23 [PATCH net v7 0/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
                   ` (2 preceding siblings ...)
  2026-11-19  7:23 ` [PATCH net v7 3/5] net: phy: stop postponing PHY interrupts when phy_suspend() fails Igor Velkov via B4 Relay
@ 2026-11-19  7:24 ` Igor Velkov via B4 Relay
  2026-11-22 22:03   ` netdev-bot+sashiko
  2026-11-19  7:24 ` [PATCH net v7 5/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
  4 siblings, 1 reply; 9+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-11-19  7:24 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Thomas Gleixner, linux-pm, Maxime Chevallier, Lukas Wunner,
	Rafael J. Wysocki, Wei Fang, Niklas Söderlund, Paul Barker,
	Florian Fainelli, Doug Berger, Thangaraj Samynathan, Shenwei Wang,
	Haoning Cheng, netdev, linux-arm-kernel, linux-rockchip,
	linux-kernel, Igor Velkov

From: Igor Velkov <iav@iav.lv>

mdio_bus_phy_resume_irq() reruns a postponed interrupt with
irq_wake_thread(). A PHY interrupt behind a nested threaded interrupt
controller, such as an I2C or SPI GPIO expander, has no interrupt
thread, so irq_wake_thread() does nothing and the postponed interrupt
is lost: the PHY keeps its status latched and its INT line asserted.

For a PHY without MAC-managed PM, mdio_bus_phy_resume() calls
phy_init_hw() before the rerun, and config_intr() there acknowledges
the latched status in drivers such as realtek. A later patch brings
PHYs with MAC-managed PM under postponing, and stmmac, for one, does
not call phy_init_hw() on resume.

Call the PHY driver's handler from mdio_bus_phy_resume_irq(), as
phy_interrupt() does, unless the PHY has freed its interrupt: on a
shared line irq_rerun outlives the free, and the handler would run on a
PHY with its interrupts disabled. Hold irq_pm_lock across the call and
around phy_disable_interrupts() in phy_free_interrupt(), so that the
free waits for the handler, as free_irq() waits for the thread.

Fixes: 1758bde2e4aa ("net: phy: Don't trigger state machine while in suspend")
Signed-off-by: Igor Velkov <iav@iav.lv>
---
 drivers/net/phy/phy.c        |  3 +--
 drivers/net/phy/phy_device.c | 13 ++++++++++---
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 92ee4651bdbc..6cc94b525e09 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -1516,9 +1516,8 @@ EXPORT_SYMBOL(phy_request_interrupt);
  */
 void phy_free_interrupt(struct phy_device *phydev)
 {
-	phy_disable_interrupts(phydev);
-
 	mutex_lock(&phydev->irq_pm_lock);
+	phy_disable_interrupts(phydev);
 	free_irq(phydev->irq, phydev);
 	/* free_irq() shuts down a line left without users and the next request
 	 * resets its depth; mdio_bus_phy_resume_irq() enables a shared line.
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 2d2058c29f30..8bc394ff7d2d 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -355,12 +355,19 @@ static __maybe_unused void mdio_bus_phy_resume_irq(struct phy_device *phydev)
 	mutex_lock(&phydev->irq_pm_lock);
 	if (phy_interrupt_is_valid(phydev))
 		synchronize_irq(phydev->irq);
-	/* The PHY may have fallen back to polling since it disabled the line */
+	/* The PHY may have fallen back to polling since it disabled the line,
+	 * or freed its interrupt while another user keeps the line.
+	 */
 	if (phydev->irq_rerun) {
 		phydev->irq_rerun = false;
 		enable_irq(phydev->irq_rerun_line);
-		if (phydev->irq == phydev->irq_rerun_line)
-			irq_wake_thread(phydev->irq, phydev);
+		/* A nested threaded line has no thread; we run the handler */
+		if (phydev->irq == phydev->irq_rerun_line &&
+		    phydev->interrupts) {
+			mutex_lock(&phydev->lock);
+			phydev->drv->handle_interrupt(phydev);
+			mutex_unlock(&phydev->lock);
+		}
 	}
 	mutex_unlock(&phydev->irq_pm_lock);
 }

-- 
2.43.0



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

* [PATCH net v7 5/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM
  2026-11-19  7:23 [PATCH net v7 0/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
                   ` (3 preceding siblings ...)
  2026-11-19  7:24 ` [PATCH net v7 4/5] net: phy: run a postponed PHY interrupt handler directly on resume Igor Velkov via B4 Relay
@ 2026-11-19  7:24 ` Igor Velkov via B4 Relay
  2026-11-22 22:03   ` netdev-bot+sashiko
  4 siblings, 1 reply; 9+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-11-19  7:24 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Thomas Gleixner, linux-pm, Maxime Chevallier, Lukas Wunner,
	Rafael J. Wysocki, Wei Fang, Niklas Söderlund, Paul Barker,
	Florian Fainelli, Doug Berger, Thangaraj Samynathan, Shenwei Wang,
	Haoning Cheng, netdev, linux-arm-kernel, linux-rockchip,
	linux-kernel, Igor Velkov

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. When the PHY sits on an MDIO
bus the MAC does not own, phy_attach_direct() links the MAC as a
consumer of the PHY, so the MAC suspends before and resumes after the
PHY; there the replay still runs before the MAC resumes, as the handler did
from resume_device_irqs() before this change, and until the PHY
suspends, an interrupt still runs the handler after the MAC has
suspended. ODROID-HC4 (stmmac, RTL8211F behind mdio-mux-meson-g12a)
takes this path. 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 from the postponed interrupt: 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.

A postponed interrupt leaves its line disabled. suspend_device_irqs()
runs before the noirq callbacks: it arms a wakeup line and leaves a
disabled one masked unless the irqchip has
IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND, so a PHY whose interrupt line is also
its wake IRQ would sleep through a wakeup. Enable the line a postponed
interrupt disabled in the noirq suspend callback while it is still
disabled and armed for wakeup, and disable it again in the noirq resume
callback, before resume_device_irqs(). The irq core turns an interrupt
on an armed line into a wakeup without running the handler, and clears
the armed state before it disables the line, so a line that already
woke the system is left alone, and so is a line that is no longer
disabled. An edge the postponed interrupt consumed does not repeat, so
on an edge-triggered line a magic packet that arrives during the
transition does not abort it; the next one wakes the system. On a
level-triggered line, a PHY that keeps a link interrupt asserted, such
as the one from the speed change above, aborts every suspend with
Wake-on-LAN enabled: its driver has to restrict the interrupt to
Wake-on-LAN in its suspend callback, as rtl8211f_suspend() does.

Tested on Helios64 with 7.3.5 and this series, 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 7 of 8 times, no suspend
  aborted; in the eighth the PHY raised no interrupt, cause not found;
- the same under a flood of magic packets: a packet after
  rtl8211f_suspend() was postponed and disabled the line, the noirq
  callback enabled it, and the wakeup aborted the suspend 5 of 5 times
  (traced); a single magic packet then woke the board.

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        | 13 ++++++++---
 drivers/net/phy/phy_device.c | 54 ++++++++++++++++++++++++++++++++++++++++----
 include/linux/phy.h          |  7 ++++--
 3 files changed, 64 insertions(+), 10 deletions(-)

diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 6cc94b525e09..45891570a56e 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)
@@ -1486,8 +1489,10 @@ void phy_request_interrupt(struct phy_device *phydev)
 
 	/* A line without users restarts at depth 0, so our disable is gone */
 	mutex_lock(&phydev->irq_pm_lock);
-	if (phydev->irq_rerun && !irq_has_action(phydev->irq_rerun_line))
+	if (phydev->irq_rerun && !irq_has_action(phydev->irq_rerun_line)) {
 		phydev->irq_rerun = false;
+		phydev->irq_wake_enabled = false;
+	}
 	mutex_unlock(&phydev->irq_pm_lock);
 
 	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
@@ -1522,8 +1527,10 @@ void phy_free_interrupt(struct phy_device *phydev)
 	/* free_irq() shuts down a line left without users and the next request
 	 * resets its depth; mdio_bus_phy_resume_irq() enables a shared line.
 	 */
-	if (!irq_has_action(phydev->irq))
+	if (!irq_has_action(phydev->irq)) {
 		phydev->irq_rerun = false;
+		phydev->irq_wake_enabled = false;
+	}
 	mutex_unlock(&phydev->irq_pm_lock);
 }
 EXPORT_SYMBOL(phy_free_interrupt);
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 8bc394ff7d2d..c700b34e40a2 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -18,6 +18,7 @@
 #include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
+#include <linux/irq.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/mdio.h>
@@ -377,11 +378,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
@@ -414,8 +418,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;
@@ -446,8 +452,46 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
 	return 0;
 }
 
-static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
-			 mdio_bus_phy_resume);
+static __maybe_unused int mdio_bus_phy_suspend_noirq(struct device *dev)
+{
+	struct phy_device *phydev = to_phy_device(dev);
+	struct irq_data *d;
+
+	mutex_lock(&phydev->irq_pm_lock);
+	/* We enable the line a postponed interrupt disabled if the irq core
+	 * armed it for wakeup, so that it wakes the system.
+	 */
+	if (phydev->irq_rerun) {
+		d = irq_get_irq_data(phydev->irq_rerun_line);
+		if (d && irqd_irq_disabled(d) && irqd_is_wakeup_armed(d)) {
+			enable_irq(phydev->irq_rerun_line);
+			phydev->irq_wake_enabled = true;
+		}
+	}
+	mutex_unlock(&phydev->irq_pm_lock);
+
+	return 0;
+}
+
+static __maybe_unused int mdio_bus_phy_resume_noirq(struct device *dev)
+{
+	struct phy_device *phydev = to_phy_device(dev);
+
+	mutex_lock(&phydev->irq_pm_lock);
+	if (phydev->irq_wake_enabled) {
+		phydev->irq_wake_enabled = false;
+		disable_irq_nosync(phydev->irq_rerun_line);
+	}
+	mutex_unlock(&phydev->irq_pm_lock);
+
+	return 0;
+}
+
+static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(mdio_bus_phy_suspend, mdio_bus_phy_resume)
+	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mdio_bus_phy_suspend_noirq,
+				      mdio_bus_phy_resume_noirq)
+};
 
 /**
  * genphy_match_phy_device - match a PHY device with a PHY driver
diff --git a/include/linux/phy.h b/include/linux/phy.h
index f370690d62a7..74acbb15ef7b 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -649,10 +649,12 @@ 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
  * @irq_rerun_line: The line the postponed interrupt disabled
+ * @irq_wake_enabled: Set while @irq_rerun_line is enabled for the noirq
+ *                    phase, as it wakes the system
  * @default_timestamp: Flag indicating whether we are using the phy
  *		       timestamp as the default one
  * @interface: enum phy_interface_t value
@@ -734,6 +736,7 @@ struct phy_device {
 	bool irq_suspended;
 	bool irq_rerun;
 	int irq_rerun_line;
+	bool irq_wake_enabled;
 
 	unsigned default_timestamp:1;
 

-- 
2.43.0



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

* Re: [PATCH net v7 2/5] net: phy: fix three paths that unbalance a postponed PHY interrupt line
  2026-11-19  7:23 ` [PATCH net v7 2/5] net: phy: fix three paths that unbalance a postponed PHY interrupt line Igor Velkov via B4 Relay
@ 2026-11-22 22:03   ` netdev-bot+sashiko
  0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 22:03 UTC (permalink / raw)
  To: iav
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, tglx,
	linux-pm, maxime.chevallier, lukas, rafael.j.wysocki, wei.fang,
	niklas.soderlund, paul, florian.fainelli, opendmb, Thangaraj.S,
	shenwei.wang, Haoning.CHENG, netdev, linux-arm-kernel,
	linux-rockchip, linux-kernel

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · 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).

- [Medium] mdio_bus_phy_resume() (drivers/net/phy/phy_device.c) can still
  leave a postponed PHY interrupt line disabled for good.
- [Medium] Whether irq_rerun is still valid depends on whether the shared
  line was shut down and started again since this PHY disabled it.
- [Low] The new kernel-doc for irq_pm_lock (include/linux/phy.h:678) and
  the in-struct comment (include/linux/phy.h:814) say it 'Serializes…

--

net: phy: fix three paths that unbalance a postponed PHY interrupt line

With this patch, phy_interrupt() disables a postponed PHY interrupt line
only once and records which line it disabled. phy_request_interrupt() and
phy_free_interrupt() now clear irq_rerun when the line has no users. The
postpone and rerun logic moves into mdio_bus_phy_suspend_irq() and
mdio_bus_phy_resume_irq(), and a new per-PHY irq_pm_lock mutex guards it.

> diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
> index 9af303d60bd6b..92ee4651bdbc6 100644
> --- a/drivers/net/phy/phy.c
> +++ b/drivers/net/phy/phy.c

[ ... ]

> @@ -1480,6 +1484,12 @@ void phy_request_interrupt(struct phy_device *phydev)
>  {
>  	int err;
>  
> +	/* A line without users restarts at depth 0, so our disable is gone */
> +	mutex_lock(&phydev->irq_pm_lock);
> +	if (phydev->irq_rerun && !irq_has_action(phydev->irq_rerun_line))
> +		phydev->irq_rerun = false;
> +	mutex_unlock(&phydev->irq_pm_lock);
> +
>  	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
>  				   IRQF_ONESHOT | IRQF_SHARED,
>  				   phydev_name(phydev), phydev);

[Severity: Medium]
Can irq_rerun go stale on a shared line in ways the commit message does
not list?

irq_pm_lock belongs to one phydev. It does not order this check against
free_irq() or request_threaded_irq() from the other users of the line,
and a second PHY on the line has its own irq_pm_lock.

First window: PHY A postpones on line L, so irq_rerun is set and the
depth is at least 1. A then frees L while user B still holds it, so A
keeps irq_rerun. Later:

PHY A                                  user B
phy_request_interrupt()
  irq_has_action(L) == true
  mutex_unlock(&irq_pm_lock)
                                       free_irq(L)  /* last action */
                                         irq_shutdown()  /* depth++ */
  request_threaded_irq(L)
    __setup_irq()
      irq_startup()  /* depth = 0 */

A's disable is gone, but A's irq_rerun is still true.

Second window: A frees L while B is present, so A keeps irq_rerun. Then
B's phy_free_interrupt() empties the line, but it clears only B's own
flag:

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

If B requests the line again first, or A does not request it again
before resume, nothing clears A's flag.

In both cases, mdio_bus_phy_resume_irq() for A calls
enable_irq(irq_rerun_line) based on the stale flag.

If the depth is 0, this triggers the "Unbalanced enable for IRQ"
warning.

If B was postponed in the meantime, A's enable cancels B's disable and
unmasks L while B is still postponed. B's thread sees its irq_rerun is
already set and does not disable the line again. On a level-triggered
line the IRQF_ONESHOT thread then keeps rerunning until B resumes, and
B's own enable_irq() then triggers the same warning.

If nobody has requested L again, the enable starts a line that has no
action.

The commit message names two cases that stay unbalanced. It also says
irq_rerun is cleared:

    in phy_free_interrupt() when free_irq() leaves the line without
    users

Does that hold for the dra72-evm-revc two-PHY setup if the other PHY is
the one whose free empties the line?

[ ... ]

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 1791d578f4dde..f2e2370b4be7c 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c

[ ... ]

> @@ -393,19 +419,7 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
>  	if (ret < 0)
>  		return ret;
>  no_resume:
> -	if (phy_interrupt_is_valid(phydev)) {
> -		phydev->irq_suspended = false;
> -		synchronize_irq(phydev->irq);
> -
> -		/* Rerun interrupts which were postponed by phy_interrupt()
> -		 * because they occurred during the system sleep transition.
> -		 */
> -		if (phydev->irq_rerun) {
> -			phydev->irq_rerun = false;
> -			enable_irq(phydev->irq);
> -			irq_wake_thread(phydev->irq, phydev);
> -		}
> -	}
> +	mdio_bus_phy_resume_irq(phydev);

[Severity: Medium]
What happens to a postponed line if phy_init_hw() or phy_resume() fails
just above this label?

drivers/net/phy/phy_device.c:mdio_bus_phy_resume() {
	...
	ret = phy_init_hw(phydev);
	if (ret < 0)
		return ret;

	ret = phy_resume(phydev);
	if (ret < 0)
		return ret;
no_resume:
	mdio_bus_phy_resume_irq(phydev);
	...
}

Both early returns skip mdio_bus_phy_resume_irq(), so irq_suspended
stays true. If a postponed interrupt already called disable_irq_nosync(),
the line stays disabled. If not, the next phy_interrupt() takes the
postpone branch and disables it.

Nothing calls enable_irq(irq_rerun_line) until a later sleep cycle
resumes successfully. Until then, this PHY loses its interrupts, and so
does every other user of a shared line, such as the second PHY on
dra72-evm-revc.

The early returns themselves are not new. The result, though, matches
the third bullet of the commit message, reached through a different
return:

    mdio_bus_phy_resume() returns before enable_irq(), and the other
    users of a shared line stay disabled.

The new comment in phy_free_interrupt() ("mdio_bus_phy_resume_irq()
enables a shared line") also assumes the helper always runs.

A later patch in the series, "net: phy: stop postponing PHY interrupts
when phy_suspend() fails", fixes the suspend-side version by calling
mdio_bus_phy_resume_irq() on that error path. The resume-side error
returns are still unchanged at the end of the series.

Should these error paths also call mdio_bus_phy_resume_irq()?

[ ... ]

> diff --git a/include/linux/phy.h b/include/linux/phy.h
> index 59c75296d5355..f370690d62a73 100644
> --- a/include/linux/phy.h
> +++ b/include/linux/phy.h

[ ... ]

> @@ -675,6 +675,8 @@ struct phy_oatc14_sqi_capability {
>   * @n_ports: Number of ports currently attached to the PHY
>   * @max_n_ports: Max number of ports this PHY can expose
>   * @lock:  Mutex for serialization access to PHY
> + * @irq_pm_lock: Serializes requesting and freeing the interrupt against the
> + *               PM callbacks; phy_interrupt() never takes it

[Severity: Low]
Is this description accurate for phy_request_interrupt()?

The mutex is held only around the irq_rerun and irq_has_action() check.
request_threaded_irq(), phy_enable_interrupts() and the two
"phydev->irq = PHY_POLL" fallbacks all run after mutex_unlock().

mdio_bus_phy_resume_irq() reads phydev->irq under the lock, both for
phy_interrupt_is_valid() and for the irq_rerun_line comparison. Those
reads are not serialized against the request.

The commit message states the narrower guarantee: request and free do
not change irq_rerun between the check and the enable_irq() after
resume.

Could this kernel-doc, and the matching comment above the irq_pm_lock
field in struct phy_device, describe that narrower guarantee instead?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119-b4-phylib-mac-pm-irq-v3-v7-0-d8b8f43b5a62%40iav.lv

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

* Re: [PATCH net v7 4/5] net: phy: run a postponed PHY interrupt handler directly on resume
  2026-11-19  7:24 ` [PATCH net v7 4/5] net: phy: run a postponed PHY interrupt handler directly on resume Igor Velkov via B4 Relay
@ 2026-11-22 22:03   ` netdev-bot+sashiko
  0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 22:03 UTC (permalink / raw)
  To: iav
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, tglx,
	linux-pm, maxime.chevallier, lukas, rafael.j.wysocki, wei.fang,
	niklas.soderlund, paul, florian.fainelli, opendmb, Thangaraj.S,
	shenwei.wang, Haoning.CHENG, netdev, linux-arm-kernel,
	linux-rockchip, linux-kernel

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message claims that for a PHY without MAC-managed PM,
  mdio_bus_phy_resume() calls phy_init_hw() before the rerun, so…
- [Low] In mdio_bus_phy_resume_irq()
  (drivers/net/phy/phy_device.c:365-366), the new check `phydev->irq ==…
- [Low] The kernel-doc for @irq_pm_lock (include/linux/phy.h:678-679) and
  the comment on the member (line 814-817) say the lock 'Serializes…

--

net: phy: run a postponed PHY interrupt handler directly on resume

This commit replaces irq_wake_thread() in mdio_bus_phy_resume_irq() with
a direct call to the PHY driver's handle_interrupt() under phydev->lock.
This keeps a postponed interrupt on a nested threaded line from being
lost. The call is skipped when the PHY has freed its interrupt, and
phy_free_interrupt() now calls phy_disable_interrupts() under
irq_pm_lock.

> For a PHY without MAC-managed PM, mdio_bus_phy_resume() calls
> phy_init_hw() before the rerun, and config_intr() there acknowledges
> the latched status in drivers such as realtek. A later patch brings
> PHYs with MAC-managed PM under postponing, and stmmac, for one, does
> not call phy_init_hw() on resume.

[Severity: Low]
Is this true for every PHY without MAC-managed PM?

When mdio_bus_phy_may_suspend() returns false, mdio_bus_phy_suspend()
returns early. That happens with WoL enabled, device_may_wakeup() set on
the netdev or its parent, a PHY that is already suspended, or a driver
with no .suspend. In that case suspended_by_mdio_bus is never set:

drivers/net/phy/phy_device.c:mdio_bus_phy_suspend() {
    ...
	if (!mdio_bus_phy_may_suspend(phydev))
		return 0;

	ret = phy_suspend(phydev);
    ...
	phydev->suspended_by_mdio_bus = 1;
    ...
}

mdio_bus_phy_resume() then skips phy_init_hw() completely:

drivers/net/phy/phy_device.c:mdio_bus_phy_resume() {
    ...
	if (!phydev->suspended_by_mdio_bus)
		goto no_resume;
    ...
	ret = phy_init_hw(phydev);
    ...
no_resume:
	mdio_bus_phy_resume_irq(phydev);
    ...
}

So in the current tree, without the later MAC-managed PM patch, a
WoL-enabled PHY on a nested threaded line already loses its postponed
interrupt to irq_wake_thread(). The code change fixes this case too.

Could the commit message say that the problem exists even without the
later patch?

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 2d2058c29f300..8bc394ff7d2d2 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -355,12 +355,19 @@ static __maybe_unused void mdio_bus_phy_resume_irq(struct phy_device *phydev)
>  	mutex_lock(&phydev->irq_pm_lock);
>  	if (phy_interrupt_is_valid(phydev))
>  		synchronize_irq(phydev->irq);
> -	/* The PHY may have fallen back to polling since it disabled the line */
> +	/* The PHY may have fallen back to polling since it disabled the line,
> +	 * or freed its interrupt while another user keeps the line.
> +	 */
>  	if (phydev->irq_rerun) {
>  		phydev->irq_rerun = false;
>  		enable_irq(phydev->irq_rerun_line);
> -		if (phydev->irq == phydev->irq_rerun_line)
> -			irq_wake_thread(phydev->irq, phydev);
> +		/* A nested threaded line has no thread; we run the handler */
> +		if (phydev->irq == phydev->irq_rerun_line &&
> +		    phydev->interrupts) {

[Severity: Low]
This check treats phydev->interrupts as meaning that the PHY has not
freed its interrupt. Can a driver's .resume callback set that bit again
after phy_free_interrupt()?

mdio_bus_phy_resume() calls phy_init_hw() and phy_resume() ->
drv->resume() before mdio_bus_phy_resume_irq(). Several resume
callbacks set the bit again based only on the IRQ number:

drivers/net/phy/micrel.c:kszphy_resume() {
    ...
	/* Enable PHY Interrupts */
	if (phy_interrupt_is_valid(phydev)) {
		phydev->interrupts = PHY_INTERRUPT_ENABLED;
		if (phydev->drv->config_intr)
			phydev->drv->config_intr(phydev);
	}
    ...
}

ksz9477_resume(), ksz8061_resume(), dp83867_resume() and
mv88q2xxx_resume() do the same.

phy_free_interrupt() clears phydev->interrupts through
phy_disable_interrupts(), but it leaves phydev->irq valid. On a shared
line that still has another action, it also leaves irq_rerun set.

Consider this sequence:

  PHY suspended by the MDIO bus, postponed interrupt sets irq_rerun
  phy_disconnect() -> phy_free_interrupt(), another user keeps the line
  mdio_bus_phy_resume()
    phy_resume() -> kszphy_resume()     /* interrupts set again */
    mdio_bus_phy_resume_irq()
      phydev->drv->handle_interrupt(phydev)

Wouldn't this run the handler on a detached PHY that has no irqaction,
which is the case the new check is meant to exclude? Before this patch,
irq_wake_thread() found no action with this dev_id after free_irq() and
did nothing.

The visible effect looks small. These handlers read the status register
and call phy_trigger_machine() on a PHY in PHY_HALTED, and these
drivers' resume callbacks have just re-enabled the PHY's interrupts.

> +			mutex_lock(&phydev->lock);
> +			phydev->drv->handle_interrupt(phydev);
> +			mutex_unlock(&phydev->lock);

[Severity: Low]
Does the irq_pm_lock documentation still match the code? The kernel-doc
in include/linux/phy.h says:

 * @irq_pm_lock: Serializes requesting and freeing the interrupt against the
 *               PM callbacks; phy_interrupt() never takes it

After this patch, the free side runs entirely under irq_pm_lock, and
this new call reads phydev->interrupts under it. The request side holds
the lock only around the irq_rerun check:

drivers/net/phy/phy.c:phy_request_interrupt() {
	mutex_lock(&phydev->irq_pm_lock);
	if (phydev->irq_rerun && !irq_has_action(phydev->irq_rerun_line))
		phydev->irq_rerun = false;
	mutex_unlock(&phydev->irq_pm_lock);

	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
    ...
		if (phy_enable_interrupts(phydev)) {
    ...
}

So request_threaded_irq() and phy_enable_interrupts() run outside the
lock, and phy_enable_interrupts() is what sets phydev->interrupts and
calls config_intr().

I could not find a concrete failure from this. A race with dev_open()
from an unfrozen workqueue either skips the handler or overlaps with
config_intr(), and phy_interrupt() can already overlap with it the same
way.

Could the comment be updated to say that only freeing is serialized?
Could it also mention the new irq_pm_lock -> phydev->lock nesting
introduced here?

>  		}
>  	}
>  	mutex_unlock(&phydev->irq_pm_lock);
>  }

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119-b4-phylib-mac-pm-irq-v3-v7-0-d8b8f43b5a62%40iav.lv

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

* Re: [PATCH net v7 5/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM
  2026-11-19  7:24 ` [PATCH net v7 5/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
@ 2026-11-22 22:03   ` netdev-bot+sashiko
  0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 22:03 UTC (permalink / raw)
  To: iav
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, tglx,
	linux-pm, maxime.chevallier, lukas, rafael.j.wysocki, wei.fang,
	niklas.soderlund, paul, florian.fainelli, opendmb, Thangaraj.S,
	shenwei.wang, Haoning.CHENG, netdev, linux-arm-kernel,
	linux-rockchip, linux-kernel

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 (1) · 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).

- [Medium] Two phylib changes interact badly.

Pre-existing issues:
- [Medium] This problem wasn't introduced by this patch, but
  mdio_bus_phy_resume() (drivers/net/phy/phy_device.c:439-445) returns…

--

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

With this commit, mdio_bus_phy_suspend() also sets irq_suspended for PHYs
with MAC-managed PM, so their interrupts are postponed across system
sleep, and phy_interrupt() no longer reports a wakeup for them. It also
adds noirq callbacks. The suspend one re-enables a line that a postponed
interrupt disabled if the line is armed for wakeup. The resume one
disables it again before resume_device_irqs().

> diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
> index 6cc94b525e093..45891570a56ee 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 8bc394ff7d2d2..c700b34e40a2c 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c

[ ... ]

> @@ -377,11 +378,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
> @@ -414,8 +418,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;

[Severity: Medium]
This is a pre-existing issue, not one introduced by this patch. Further
down in mdio_bus_phy_resume(), the error returns skip
mdio_bus_phy_resume_irq():

drivers/net/phy/phy_device.c:mdio_bus_phy_resume() {
    ...
	ret = phy_init_hw(phydev);
	if (ret < 0)
		return ret;

	ret = phy_resume(phydev);
	if (ret < 0)
		return ret;
no_resume:
	mdio_bus_phy_resume_irq(phydev);
    ...
}

On a PHY without MAC-managed PM, what happens if phy_init_hw() or
phy_resume() fails? irq_suspended would stay true, so every later PHY
interrupt is postponed. If a postponed interrupt had set irq_rerun, its
disable_irq_nosync() would never be undone, which leaves the PHY line
disabled after resume, along with any other users of a shared line. The
state machine is not restarted either.

This lasts until a later suspend/resume cycle completes cleanly. The new
noirq enable/disable pair runs before mdio_bus_phy_resume(), so it stays
balanced and does not make this worse. Should these error paths still go
through mdio_bus_phy_resume_irq()?

> @@ -446,8 +452,46 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
>  	return 0;
>  }
>  
> -static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
> -			 mdio_bus_phy_resume);
> +static __maybe_unused int mdio_bus_phy_suspend_noirq(struct device *dev)
> +{
> +	struct phy_device *phydev = to_phy_device(dev);
> +	struct irq_data *d;
> +
> +	mutex_lock(&phydev->irq_pm_lock);
> +	/* We enable the line a postponed interrupt disabled if the irq core
> +	 * armed it for wakeup, so that it wakes the system.
> +	 */
> +	if (phydev->irq_rerun) {
> +		d = irq_get_irq_data(phydev->irq_rerun_line);
> +		if (d && irqd_irq_disabled(d) && irqd_is_wakeup_armed(d)) {
> +			enable_irq(phydev->irq_rerun_line);
> +			phydev->irq_wake_enabled = true;
> +		}
> +	}

[Severity: Medium]
Can this enable_irq() unmask a line whose PHY interrupt source was never
acked?

The postponed branch of phy_interrupt() does no MDIO access. The PHY's
latched status (BCM54xx ISR, RTL8211F INSR) stays set, and the PHY keeps
INT asserted. With the new !phydev->mac_managed_pm check, no wakeup is
reported either.

Before this patch, MAC-managed PHYs were not postponed. Their
handle_interrupt() ran and acked the status.

On a level-triggered line, would enable_irq() here go straight to
irq_pm_check_wakeup() -> pm_system_irq_wakeup() and abort the suspend?

With stmmac this could happen on every attempt. stmmac sets
wol_phy_speed_ctrl, and phylink_suspend() renegotiates the link after
phylink_stop(). The PHY is a child of the MAC's MDIO bus, so it is
already postponing by then:

drivers/net/phy/phylink.c:phylink_suspend() {
    ...
	} else {
		phylink_stop(pl);
	}

	if (phylink_phy_pm_speed_ctrl(pl))
		phylink_speed_down(pl, false);
}

rtl8211f_suspend() masks link interrupts and reads INSR before this
point, but bcm54xx_suspend() does not when WoL is enabled:

drivers/net/phy/broadcom.c:bcm54xx_suspend() {
    ...
	ret = bcm_phy_read_exp(phydev, BCM54XX_WOL_INT_STATUS);
	if (ret < 0)
		return ret;

	if (phydev->wol_enabled)
		return bcm54xx_set_wakeup_irq(phydev, true);
    ...
}

Meanwhile bcm_phy_set_wol() wake-arms the shared link/WoL line:

	if (phy_interrupt_is_valid(phydev))
		enable_irq_wake(phydev->irq);

On bcmgenet, bcmasp and bcmsysport, which set mac_managed_pm and use
these PHYs, a remote link change inside this window would now abort the
suspend once. Previously the interrupt was simply acked. The commit
message says PHY drivers have to restrict the interrupt to Wake-on-LAN in
their suspend callback. Should the drivers that don't do this be updated
in the series?

On an edge-triggered line, the commit message says:

    An edge the postponed interrupt consumed does not repeat, so
    on an edge-triggered line a magic packet that arrives during the
    transition does not abort it; the next one wakes the system.

Does this hold for PHYs that keep INT asserted until the status register
is read, such as RTL8211F INTB/INSR? The postponed interrupt never reads
the status. After enable_irq() there would be no new edge and nothing
pending to resend, and no wakeup was reported for a MAC-managed PHY. Could
Wake-on-LAN then stay lost for the whole sleep?

These callbacks are also installed as freeze_noirq/thaw_noirq through
SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(), so hibernation image creation looks
exposed to the same aborts.

> +	mutex_unlock(&phydev->irq_pm_lock);
> +
> +	return 0;
> +}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119-b4-phylib-mac-pm-irq-v3-v7-0-d8b8f43b5a62%40iav.lv

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

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

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  7:23 [PATCH net v7 0/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
2026-11-19  7:23 ` [PATCH net v7 1/5] net: phy: move irq_suspended and irq_rerun out of the bitfield Igor Velkov via B4 Relay
2026-11-19  7:23 ` [PATCH net v7 2/5] net: phy: fix three paths that unbalance a postponed PHY interrupt line Igor Velkov via B4 Relay
2026-11-22 22:03   ` netdev-bot+sashiko
2026-11-19  7:23 ` [PATCH net v7 3/5] net: phy: stop postponing PHY interrupts when phy_suspend() fails Igor Velkov via B4 Relay
2026-11-19  7:24 ` [PATCH net v7 4/5] net: phy: run a postponed PHY interrupt handler directly on resume Igor Velkov via B4 Relay
2026-11-22 22:03   ` netdev-bot+sashiko
2026-11-19  7:24 ` [PATCH net v7 5/5] net: phy: postpone PHY interrupts during sleep with MAC-managed PM Igor Velkov via B4 Relay
2026-11-22 22:03   ` 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