๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Birger Koblitz Sat, 14 Nov 2026 10:50
[PATCH net-next v21 01/13] phylink: Add phylink_mac_interrupt
#16231
Author: Birger Koblitz
Date: Sat, 14 Nov 2026 10:50
62 lines
12251 bytes
Add the phylink_mac_interrupt() function to phylink, which
passes a link-change interrupt received by the MAC to phylib.
This function can be used by drivers which want to avoid
polling the PHY and instead rely on an interrupt received by
the MAC when a link-change happens.

Signed-off-by: Birger Koblitz <mail@birger-koblitz.de>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de>
Reviewed-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 drivers/net/phy/phylink.c | 20 ++++++++++++++++++++
 include/linux/phylink.h   |  1 +
 2 files changed, 21 insertions(+)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index e885da1c28311a12a869e29ceb3c96886f31adc6..a87eaa2f3db5bef8fe56e12b90375bdb7f9c52e7 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -1616,6 +1616,26 @@ static bool phylink_link_is_up(struct phylink *pl)
 	return pl->netdev ? netif_carrier_ok(pl->netdev) : pl->old_link_state;
 }
 
+/**
+ * phylink_mac_interrupt() - wrapper for phy_mac_interrupt()
+ * @pl: a pointer to a &struct phylink returned from phylink_create()
+ *
+ * Passes a link-change interrupt received by the MAC to phylib.
+ * This function may be called in interrupt context, and therefore cannot take
+ * a mutex. However, it is safe to assume that the reference to phy does not
+ * disappear during the call, as only an SFP module may be unplugged to make a
+ * PHY disappear. SFP modules however do not support interrupts.
+ */
+void phylink_mac_interrupt(struct phylink *pl)
+{
+	struct phy_device *phy;
+
+	phy = pl->phydev;
+	if (phy)
+		phy_mac_interrupt(phy);
+}
+EXPORT_SYMBOL_GPL(phylink_mac_interrupt);
+
 static void phylink_resolve(struct work_struct *w)
 {
 	struct phylink *pl = container_of(w, struct phylink, resolve);
diff --git a/include/linux/phylink.h b/include/linux/phylink.h
index 3a88a69882a610afb868b55df6f6e77aea918649..a7b6294cc4a075bd2aaa43ed6484de17a6cb88b1 100644
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -768,6 +768,7 @@ int phylink_ethtool_set_eee(struct phylink *link, struct ethtool_keee *eee);
 int phylink_mii_ioctl(struct phylink *, struct ifreq *, int);
 int phylink_speed_down(struct phylink *pl, bool sync);
 int phylink_speed_up(struct phylink *pl);
+void phylink_mac_interrupt(struct phylink *pl);
 
 #define phylink_zero(bm) \
 	bitmap_zero(bm, __ETHTOOL_LINK_MODE_MASK_NBITS)

-- 
2.47.3
Re: [PATCH net-next v21 01/13] phylink: Add phylink_mac_interrupt
#16895
Author: netdev-bot+sashiko
Date: Mon, 16 Nov 2026 02:06
120 lines
12416 bytes
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 ยท High: 0 ยท 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).

- [Medium] phylink_mac_interrupt() (drivers/net/phy/phylink.c:1629-1637)
  is meant to be called from interrupt context.

--

phylink: Add phylink_mac_interrupt

This adds an exported helper, phylink_mac_interrupt(), that MAC drivers
can call from interrupt context. It forwards a link-change interrupt seen
by the MAC to phylib by calling phy_mac_interrupt() on the attached PHY.
Drivers can then skip polling the PHY.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index e885da1c28311..a87eaa2f3db5b 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c
> @@ -1616,6 +1616,26 @@ static bool phylink_link_is_up(struct phylink *pl)
>  	return pl->netdev ? netif_carrier_ok(pl->netdev) : pl->old_link_state;
>  }
>  
> +/**
> + * phylink_mac_interrupt() - wrapper for phy_mac_interrupt()
> + * @pl: a pointer to a &struct phylink returned from phylink_create()
> + *
> + * Passes a link-change interrupt received by the MAC to phylib.
> + * This function may be called in interrupt context, and therefore cannot take
> + * a mutex. However, it is safe to assume that the reference to phy does not
> + * disappear during the call, as only an SFP module may be unplugged to make a
> + * PHY disappear. SFP modules however do not support interrupts.
> + */
> +void phylink_mac_interrupt(struct phylink *pl)
> +{
> +	struct phy_device *phy;
> +
> +	phy = pl->phydev;
> +	if (phy)
> +		phy_mac_interrupt(phy);

[Severity: Medium]

Does the lifetime argument in the kernel-doc above hold?

pl->phydev is read here with a plain load. No lock, RCU or reference is
taken. Every writer, phylink_bringup_phy() and phylink_disconnect_phy(),
changes it while holding phydev_mutex, phy->lock and state_mutex. KCSAN
would likely report this as a data race.

The pointer is then used by phy_mac_interrupt()->phy_trigger_machine(),
which calls mod_delayed_work() on phy->state_queue.

phylink_disconnect_phy() is not specific to SFP. It does not wait for a
phylink_mac_interrupt() call that is already running:

drivers/net/phy/phylink.c:phylink_disconnect_phy() {
    ...
		pl->phydev = NULL;
    ...
	if (phy) {
		flush_work(&pl->resolve);
		phy_disconnect(phy);
	}
}

phy_disconnect() goes through phy_stop_machine(), which calls
cancel_delayed_work_sync(&phydev->state_queue). Then phy_detach() drops
the attach reference.

Suppose an IRQ loaded the old pointer before pl->phydev was cleared. Can
it then queue state_queue again after the cancel, on a PHY that has
already been stopped and detached?

The SFP part of the argument may not hold either. SFP module PHYs are
also stored in pl->phydev, via
phylink_sfp_connect_phy()->phylink_bringup_phy(), and
phylink_mac_interrupt() does not check phy->is_on_sfp_module.

The interrupt comes from the MAC, not from the SFP PHY. Removing a module
is just the kind of event that makes a MAC or PCS report a link change.

When the module is removed, sfp_sm_phy_detach() does:

	sfp_remove_phy(sfp->sfp_bus);
	phy_device_remove(sfp->mod_phy);
	phy_device_free(sfp->mod_phy);

Here sfp_remove_phy() ends up in
phylink_sfp_disconnect_phy()->phylink_disconnect_phy().

If a MAC hardirq has already loaded the old pl->phydev, could
mod_delayed_work() run on a phy_device that has already been freed?

The only caller in this series, ax88179a_status(), looks safe.
usbnet_stop() kills the status URB before ->unbind() calls
phylink_disconnect_phy(). The concern is the contract of the exported
API. The kernel-doc says the call is safe and does not tell callers to
mask and synchronize the MAC interrupt before phylink_disconnect_phy() or
PHY teardown.

Should the kernel-doc state that requirement, or should
phylink_mac_interrupt() protect the pl->phydev pointer itself?

> +}
> +EXPORT_SYMBOL_GPL(phylink_mac_interrupt);

[ ... ]

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261114-ax88179a-v21-0-c2d34410bcbe%40birger-koblitz.de
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