The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: ansuelsmth@gmail.com
Cc: Jakub Kicinski <kuba@kernel.org>,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	pabeni@redhat.com, robh@kernel.org, krzk+dt@kernel.org,
	conor+dt@kernel.org, horms@kernel.org, corbet@lwn.net,
	skhan@linuxfoundation.org, rdunlap@infradead.org,
	lorenzo@kernel.org, hkallweit1@gmail.com, linux@armlinux.org.uk,
	maxime.chevallier@bootlin.com, p.zabel@pengutronix.de,
	nathan@kernel.org, ndesaulniers@google.com, morbo@google.com,
	justinstitt@google.com, rmk+kernel@armlinux.org.uk,
	netdev@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org, llvm@lists.linux.dev,
	zhengping.zhang@airoha.com
Subject: Re: [PATCH net-next v22 08/15] net: phylink: support PCS provider release
Date: Sat, 21 Nov 2026 15:26:49 -0800	[thread overview]
Message-ID: <20261121232649.1365412-1-kuba@kernel.org> (raw)
In-Reply-To: <20261117123956.11981-9-ansuelsmth@gmail.com>

This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
---
net: phylink: support PCS provider release

This adds a blocking notifier chain to the fwnode PCS layer.
fwnode_pcs_del_provider() uses it to emit FWNODE_PCS_PROVIDER_DEL. Each
phylink instance with pcs_interfaces subscribes, drops the matching PCS from
pl->pcs_list on that event, recalculates its capabilities, and accesses
pcs_list under state_mutex.

> diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c
> index b1d48f6eee7f1..f234a46354176 100644
> --- a/drivers/net/pcs/pcs.c
> +++ b/drivers/net/pcs/pcs.c

[ ... ]

> @@ -74,6 +87,11 @@ void fwnode_pcs_del_provider(struct fwnode_pcs_provider *pp)
>  
>  	mutex_unlock(&fwnode_pcs_mutex);
>  
> +	/* Signal phylink to release any PCS from this provider */
> +	blocking_notifier_call_chain(&fwnode_pcs_notify_list,
> +				     FWNODE_PCS_PROVIDER_DEL,
> +				     pp);
> +

[Severity: Medium]
Can this notifier call into PCS hardware that the provider has already
powered down?

pcs_provider_notify()->phylink_del_pcs() calls phylink_pcs_disable(), which
calls pcs->ops->pcs_disable(). If the link is up it also calls
phylink_link_down(), and later in the series that calls
pcs->ops->pcs_link_down() too.

With devm_fwnode_pcs_add_provider(), devm_fwnode_pcs_del() runs during devres
release. That happens after the driver's .remove(), and in reverse order of
registration.

A provider might gate clocks, drop runtime PM or assert reset in .remove().
It might also get devm clocks or resets after it registers the provider.
Either way, these PCS register accesses would hit unpowered hardware.

For example, pcs-rzn1-miic calls pm_runtime_put() in .remove(). If it were
converted to a devm fwnode provider, the del would run after that.

The airoha PCS in this series registers its devm provider last and has no
.remove(), so it is not affected.

Neither pcs-provider.h nor pcs.rst says the hardware must stay accessible
until fwnode_pcs_del_provider() returns. Should that be documented, or
should the notifier avoid touching the departing PCS hardware?

>  	fwnode_handle_put(pp->fwnode);
>  	kfree(pp);
>  }

[ ... ]

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 921c227a5b3c0..094a15f3dbcd0 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -2425,6 +2451,133 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac
>  }
>  EXPORT_SYMBOL_GPL(phylink_update_mac_pause_capabilities);
>  
> +static void phylink_del_pcs(struct phylink *pl, struct phylink_pcs *pcs)
> +{
> +	ASSERT_RTNL();
> +
> +	lockdep_assert_held(&pl->state_mutex);
> +
> +	rcu_assign_pointer(pcs->phylink, NULL);
> +	synchronize_rcu();
> +	list_del(&pcs->list);

[Severity: Low]
Is a full grace period needed for every PCS while RTNL is held?

pcs_provider_notify() calls phylink_del_pcs() in a loop. It holds
rtnl_lock(), pl->phydev_mutex, phydev->lock and pl->state_mutex the whole
time, inside the notifier rwsem read section.

So each phylink subscribed to the departing provider holds RTNL for one
normal grace period per removed PCS. The instances run one after another.

phylink_detach_pcss() in this same patch clears all the pointers first and
then calls synchronize_rcu() once. Could pcs_provider_notify() do the same,
or use synchronize_net()?

> +
> +	/*
> +	 * Check if we are removing the PCS currently

[ ... ]

> +	if (pl->pcs == pcs) {
> +		if (pl->old_link_state) {
> +			phylink_link_down(pl);
> +			pl->old_link_state = false;
> +		}
> +		if (pl->cfg_link_an_mode == MLO_AN_INBAND)
> +			timer_delete_sync(&pl->link_poll);
> +
> +		if (pl->pcs_state != PCS_STATE_DOWN)
> +			phylink_pcs_disable(pl->pcs);
> +
> +		if (!pl->phylink_disable_state)
> +			pl->force_major_config = true;
> +
> +		WRITE_ONCE(pl->pcs, NULL);
> +	}
> +}

[Severity: Medium]
Can the link stay down for good if the active PCS is removed while
PHYLINK_DISABLE_LINK is set?

On SFP loss of signal, phylink_sfp_link_down() sets PHYLINK_DISABLE_LINK
and pcs_state stays PCS_STATE_STARTED.

Suppose the active PCS's provider goes away during LOS.
phylink_pcs_disable() powers the PCS down and pl->pcs becomes NULL.
force_major_config is not set, because phylink_disable_state is non-zero.

When LOS clears:

phylink_sfp_link_up()
  phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK)
    phylink_resolve()
        if ((mac_config && link_state.interface != pl->link_config.interface) ||
            pl->force_major_config) {

Neither condition holds, so phylink_major_config() is skipped and no PCS is
picked from pl->pcs_list again.

In in-band mode, phylink_mac_pcs_get_state() then sees pl->pcs == NULL and
reports link = 0.

That stays true even if another PCS in pcs_list supports the interface. It
also stays true after the provider is re-added, because the late attach
path only forces a major config when major_config_failed is set.

In MLO_AN_PHY mode, phylink_link_up() would call mac_link_up() and
netif_carrier_on() while the PCS is disabled.

The STOPPED, MAC_WOL and REPLAY bits each recover through
phylink_mac_initial_config() or force_major_config, but DISABLE_LINK does
not. Should force_major_config be set here whatever phylink_disable_state
is?

> +
> +static int pcs_provider_notify(struct notifier_block *self,
> +			       unsigned long val, void *data)
> +{
> +	struct phylink *pl = container_of(self, struct phylink, fwnode_pcs_nb);
> +	struct fwnode_pcs_provider *pp = data;
> +	struct phylink_pcs *pcs, *tmp;
> +	struct phy_device *phydev;
> +	bool resolve = false;
> +
> +	rtnl_lock();
> +	mutex_lock(&pl->phydev_mutex);
> +	phydev = pl->phydev;
> +	if (phydev)
> +		mutex_lock(&phydev->lock);
> +	mutex_lock(&pl->state_mutex);

[Severity: Medium]
Does this give fwnode_pcs_del_provider() a hidden "must not hold RTNL"
requirement?

fwnode_pcs_del_provider() used to take only fwnode_pcs_mutex. It now calls
blocking_notifier_call_chain(), which holds the chain rwsem for read. While
that is held, this callback takes rtnl_lock(), pl->phydev_mutex,
phydev->lock and pl->state_mutex.

A provider removed from a path that already holds RTNL, or any of those
locks, would deadlock here.

The rwsem-read -> rtnl ordering could also cause an ABBA deadlock with
register_fwnode_pcs_notifier() and unregister_fwnode_pcs_notifier(), which
take the same rwsem with down_write. That would happen if a MAC with
non-empty pcs_interfaces called phylink_create() or phylink_destroy() under
RTNL while another provider is being removed.

No current in-tree path does this. The airoha PCS deletes through devres at
unbind without RTNL, and airoha_eth creates and destroys phylink outside
RTNL.

But pcs-provider.h and pcs.rst do not mention this rule, and nothing
asserts it. Would a lockdep_assert_not_held() or a note in the API
documentation help?

> +
> +	/* phylink instance is being destroyed, ignore any event */
> +	if (pl->pcs_state == PCS_STATE_DETACHING)
> +		goto exit;
> +
> +	/*
> +	 * Loop all the PCS for phylink instance and check if
> +	 * this notification is relevant for some of them.
> +	 */
> +	list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) {
> +		if (!fwnode_pcs_matches_provider(pp, pl->fwnode, pcs))
> +			continue;
> +
> +		phylink_del_pcs(pl, pcs);
> +		resolve = true;
> +	}

[Severity: Medium]
What happens when a PCS in pl->pcs_list did not come from the pcs-handle
property of pl->fwnode?

fwnode_pcs_matches_provider() works out ownership again at teardown. It
re-parses "pcs-handle" from the fwnode it is given and calls
provider->fwnode_xlate() again.

phylink does not record where each PCS came from when fill_available_pcs()
hands it over. The fill_available_pcs() contract in
include/linux/phylink.h does not require the PCS to come from the fwnode
passed to phylink_create().

The earlier patch "net: phylink: save phylink instance fwnode on
phylink_create" notes that pl->fwnode can differ from the fwnode of
config->dev. The pcs.rst example in "net: Document PCS subsystem" calls
fwnode_phylink_pcs_parse(dev_fwnode(config->dev), ...) from
fill_available_pcs.

For such a MAC, every check here returns false and phylink_del_pcs() is
never called. The PCS stays on pl->pcs_list, pcs->phylink stays set, and
the PCS may still be pl->pcs.

fwnode_pcs_del_provider() then returns, and the provider frees its
phylink_pcs (for example, a devm-allocated priv). After that,
phylink_resolve(), phylink_major_config() and phylink_mac_pcs_get_state()
would call pcs->ops through freed memory, and phylink_detach_pcss() would
write to it.

airoha_eth passes the same node to both, so it is not affected today. Would
it be more robust to record the provider, or the source fwnode, for each
PCS when it is added?

[ ... ]

> @@ -2515,10 +2668,18 @@ struct phylink *phylink_create(struct phylink_config *config,
>  	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
>  	timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
>  
> +	/* First register notifier for hotplug PCS events */
> +	if (!phy_interface_empty(config->pcs_interfaces)) {
> +		pl->fwnode_pcs_nb.notifier_call = pcs_provider_notify;
> +		register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb);
> +	}

[Severity: Medium]
Can pcs_provider_notify() run against a partly initialized phylink here?

The notifier goes live, and pl->pcs_list is filled, before
phylink_parse_mode(), phylink_parse_fixedlink() and phylink_register_sfp()
run.

If a provider for one of the filled PCS is unbound at the same moment (for
example via sysfs), pcs_provider_notify() takes state_mutex and runs
phylink_update_capabilities(). That reads and writes pl->cfg_link_an_mode,
pl->sfp_bus, pl->link_config, pl->supported and pl->advertising.

Meanwhile phylink_create() writes the same fields without state_mutex.
phylink_parse_mode() sets pl->cfg_link_an_mode. phylink_parse_fixedlink()
sets link_config.speed and duplex, and does this before it takes
state_mutex:

	__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
		  pl->link_config.lp_advertising);

pl->req_link_an_mode and pl->sfp_bus are also written without the lock.

On a fixed-link port, once cfg_link_an_mode == MLO_AN_FIXED, the notifier
goes through phylink_parse_fixedlink_capabilities(). That function does a
read-modify-write linkmode_or() on config->lp_advertising, which could
overwrite the Pause bit phylink_parse_fixedlink() just set.

The later locked recalculation only ORs in match, so the link partner pause
ability from DT would be lost for good.

This also seems to conflict with the commit message:

  Protect every access of phylink pcs_list with state_mutex to handle async
  PCS provider release and capabilities recalculation.

Could the registration move to the end of phylink_create(), or could the
notifier ignore events until initialization is complete?

>  
>  	/* Fill the PCS list with available PCS from phylink config */
>  	ret = phylink_fill_available_pcs(pl, config);

[ ... ]

  reply	other threads:[~2026-11-21 23:26 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 12:39 [PATCH net-next v22 00/15] net: pcs: Introduce support for fwnode PCS Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 01/15] net: phylink: make PCS phylink instance access RCU protected Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski
2026-11-17 12:39 ` [PATCH net-next v22 02/15] net: phylink: keep and use MAC supported_interfaces in phylink struct Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 03/15] net: phylink: introduce internal phylink PCS handling Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski
2026-11-17 12:39 ` [PATCH net-next v22 04/15] net: pcs: implement Firmware node support for PCS driver Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 05/15] net: phylink: save phylink instance fwnode on phylink_create Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 06/15] net: phylink: make phylink_validate state locked Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 07/15] net: phylink: add capabilities helpers and track modified advertising Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski
2026-11-17 12:39 ` [PATCH net-next v22 08/15] net: phylink: support PCS provider release Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski [this message]
2026-11-17 12:39 ` [PATCH net-next v22 09/15] net: phylink: support late PCS provider attach Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski
2026-11-17 12:39 ` [PATCH net-next v22 10/15] net: Document PCS subsystem Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski
2026-11-17 12:39 ` [PATCH net-next v22 11/15] MAINTAINERS: add myself as PCS subsystem maintainer Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 12/15] net: phylink: add .pcs_link_down PCS OP Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 13/15] dt-bindings: net: pcs: Document support for Airoha Ethernet PCS Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski
2026-11-17 12:39 ` [PATCH net-next v22 14/15] net: pcs: airoha: add PCS driver for Airoha AN7581 SoC Christian Marangi
2026-11-17 12:39 ` [PATCH net-next v22 15/15] net: airoha: add phylink support Christian Marangi
2026-11-21 23:26   ` Jakub Kicinski
2026-11-17 12:43 ` [PATCH net-next v22 00/15] net: pcs: Introduce support for fwnode PCS netdev-bot+sinfo
2026-11-17 12:47   ` Christian Marangi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261121232649.1365412-1-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=ansuelsmth@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=edumazet@kernel.org \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=justinstitt@google.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux@armlinux.org.uk \
    --cc=llvm@lists.linux.dev \
    --cc=lorenzo@kernel.org \
    --cc=maxime.chevallier@bootlin.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=p.zabel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=rmk+kernel@armlinux.org.uk \
    --cc=robh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=zhengping.zhang@airoha.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox