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 2/4] net: phy: keep the line of a postponed PHY interrupt balanced
Author: Igor Velkov via B4 Relay
Date: Fri, 13 Nov 2026 21:06
Date: Fri, 13 Nov 2026 21:06
228 lines
20149 bytes
20149 bytes
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. Two 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. If the PHY was alone
on the line, a new request starts the line at depth 0, and the
enable_irq() in mdio_bus_phy_resume() is unbalanced.
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. Let
phy_free_interrupt() clear irq_rerun when free_irq() leaves the line
without users: free_irq() has shut it down, and the next request resets
its depth. A shared line keeps irq_rerun, and mdio_bus_phy_resume()
enables it, as before. Enabling it 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 phy_free_interrupt() does not clear 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 stays a plain bool: one thread runs the
handler, and the other two access it only under the lock, after
free_irq() or synchronize_irq(). Mark the irq_suspended accesses with
READ_ONCE() and WRITE_ONCE(): the handler reads it while the PM
callbacks write it.
This does not keep close and open off the MDIO bus: on close,
phy_disconnect() calls phy_stop(), 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 | 17 +++++++++++---
drivers/net/phy/phy_device.c | 56 +++++++++++++++++++++++++++-----------------
include/linux/phy.h | 7 ++++--
3 files changed, 54 insertions(+), 26 deletions(-)
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 9af303d60bd6..8e03c99fce8f 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,11 @@ 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 = true;
+ disable_irq_nosync(irq);
+ }
return IRQ_HANDLED;
}
@@ -1507,7 +1510,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..573eb234dc79 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -332,6 +332,38 @@ 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);
+ if (!phy_interrupt_is_valid(phydev))
+ return;
+
+ mutex_lock(&phydev->irq_pm_lock);
+ synchronize_irq(phydev->irq);
+ if (phydev->irq_rerun) {
+ phydev->irq_rerun = false;
+ enable_irq(phydev->irq);
+ 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 +371,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 +418,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 +724,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..96d14b6b544f 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -651,8 +651,7 @@ 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
* @default_timestamp: Flag indicating whether we are using the phy
* timestamp as the default one
* @interface: enum phy_interface_t value
@@ -675,6 +674,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 phy_free_interrupt() against the rerun of a
+ * postponed interrupt; 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
@@ -808,6 +809,8 @@ struct phy_device {
struct delayed_work state_queue;
struct mutex lock;
+ /* Serializes phy_free_interrupt() against mdio_bus_phy_resume_irq() */
+ struct mutex irq_pm_lock;
/* This may be modified under the rtnl lock */
bool sfp_bus_attached;
--
2.43.0
Re: [PATCH net v6 2/4] net: phy: keep the line of a postponed PHY interrupt balanced
Author: netdev-bot+sashiko
Date: Tue, 17 Nov 2026 12:08
Date: Tue, 17 Nov 2026 12:08
191 lines
14983 bytes
14983 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 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).
- [High] phy_free_interrupt() decides once, from irq_has_action() right
after this PHY's free_irq(), whether irq_rerun still owns a unit of…
- [Medium] On a shared line, phy_free_interrupt() (drivers/net/phy/phy.c)
keeps irq_rerun set and leaves the rebalance to resume.
--
net: phy: keep the line of a postponed PHY interrupt balanced
With this change, only the first postponed run of phy_interrupt() disables
the PHY interrupt line. phy_free_interrupt() now clears irq_rerun when
free_irq() leaves the line without users. It also adds irq_pm_lock to
serialize phy_free_interrupt() against the rerun after resume, and moves
the postpone/rerun logic into mdio_bus_phy_suspend_irq() and
mdio_bus_phy_resume_irq().
> diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
> index 9af303d60bd6b..8e03c99fce8f5 100644
> --- a/drivers/net/phy/phy.c
> +++ b/drivers/net/phy/phy.c
[ ... ]
> @@ -1430,8 +1430,11 @@ 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 = true;
> + disable_irq_nosync(irq);
> + }
> return IRQ_HANDLED;
> }
>
> @@ -1507,7 +1510,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);
> }
[Severity: High]
Can irq_rerun go stale on a shared line here?
Whether to keep irq_rerun is decided once, by irq_has_action() right after
this PHY's free_irq(). If another user is still on the line, irq_rerun
stays true.
When that remaining user frees the line later, __free_irq() shuts the line
down. The next request then goes through irq_startup(), which resets
desc->depth to 0. Nothing clears the first PHY's irq_rerun at that point.
Take two PHYs sharing one IRQF_SHARED line behind cpsw, which sets
mac_managed_pm. A later patch in this series brings those PHYs under
postponing:
wakeup interrupt
phy_interrupt(PHY0): irq_rerun0 = true, disable_irq_nosync() depth 1
phy_interrupt(PHY1): irq_rerun1 = true, disable_irq_nosync() depth 2
cpsw_suspend()->cpsw_ndo_stop()->phy_disconnect()
phy_free_interrupt(PHY0): PHY1 still on line, irq_rerun0 stays true
phy_free_interrupt(PHY1): line empty, shut down, irq_rerun1 = false
cpsw_resume()->cpsw_ndo_open()->phy_request_interrupt()
irq_startup() resets depth to 0, irq_rerun0 still true
mdio_bus_phy_resume_irq(PHY0)
enable_irq() at depth 0
Wouldn't this hit the "Unbalanced enable for IRQ" warning in
__enable_irq()? That is the symptom the commit message says is fixed.
The new guard in phy_interrupt() also seems to make a stale irq_rerun
worse. Between the line restart and the PHY's resume, a postponed
level-triggered interrupt now returns IRQ_HANDLED without being serviced
or masked:
if (!phydev->irq_rerun) {
phydev->irq_rerun = true;
disable_irq_nosync(irq);
}
return IRQ_HANDLED;
The ONESHOT unmask would then keep firing it until that PHY resumes.
Before this patch, such an interrupt disabled the line again, and resume
balanced it.
There is also a race. irq_has_action() runs after free_irq() has dropped
desc->request_mutex, and irq_pm_lock is per phydev. So a request_irq() or
free_irq() by another driver on the same line can change the answer
between the two calls.
Would it be possible to tie irq_rerun to the lifetime of the line, rather
than to a snapshot taken at free time?
> EXPORT_SYMBOL(phy_free_interrupt);
>
> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 1791d578f4dde..573eb234dc799 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -332,6 +332,38 @@ static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
[ ... ]
> +/* 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);
> + if (!phy_interrupt_is_valid(phydev))
> + return;
[Severity: Medium]
The new comment in phy_free_interrupt() says "mdio_bus_phy_resume_irq()
enables a shared line". Is that always true?
This early return comes before irq_rerun is checked.
phy_request_interrupt() sets phydev->irq to PHY_POLL in two failure cases:
drivers/net/phy/phy.c:phy_request_interrupt() {
...
if (err) {
...
phydev->irq = PHY_POLL;
} else {
if (phy_enable_interrupts(phydev)) {
...
phy_free_interrupt(phydev);
phydev->irq = PHY_POLL;
}
}
}
Suppose a postponed interrupt took a disable on a shared line. Then
phy_free_interrupt() runs during the sleep window while another user stays
on the line, for example from cpsw_suspend() or stmmac_reset_subtask()
through phy_disconnect(). irq_rerun is still true at that point.
If the reopen then fails in one of these two ways, for instance because of
an MDIO error while the bus is down, phydev->irq becomes PHY_POLL. In the
second case, phy_free_interrupt() again keeps irq_rerun, because the other
user is still on the line.
mdio_bus_phy_resume_irq() then returns here without calling enable_irq().
Since phydev->irq is never set back to the old number, can anything ever
balance that disable? It looks like the other users of the shared line
would stay masked for good.
> +
> + mutex_lock(&phydev->irq_pm_lock);
> + synchronize_irq(phydev->irq);
> + if (phydev->irq_rerun) {
> + phydev->irq_rerun = false;
> + enable_irq(phydev->irq);
> + irq_wake_thread(phydev->irq, phydev);
> + }
> + mutex_unlock(&phydev->irq_pm_lock);
> +}
[ ... ]
--
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