Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Christian Marangi" <ansuelsmth@gmail.com>, devicetree@vger.kernel.org
Cc: robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH net-next v22 08/15] net: phylink: support PCS provider release
Date: Wed, 18 Nov 2026 12:42:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-220928@kernel.org> (raw)
In-Reply-To: <20261117123956.11981-9-ansuelsmth@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] net: phylink: broken capability recalculation in
  phylink_update_capabilities()
- [Medium] net: phylink: mishandled force_major_config with
  phylink_disable_state
- [Medium] net: phylink: race between pcs_provider_notify() and
  phylink_create()
- [Medium] net: phylink: missing hardware advertisement update in
  pcs_provider_notify()
--

commit 43bb91371f247055f063dc4ec828173d12f85adf
Author: Christian Marangi <ansuelsmth@gmail.com>

net: phylink: support PCS provider release

This commit adds a blocking notifier chain for fwnode PCS provider removal
and updates phylink to detach removed PCS instances, recalculate supported
interfaces and capabilities, and trigger link resolution.

> 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

[ ... ]

> @@ -2350,6 +2371,11 @@ static void phylink_update_capabilities(struct phylink *pl,
>  							config->interface);
>  			linkmode_copy(pl->supported, supported);
>  			linkmode_copy(pl->advertising, config->advertising);
> +		} else {
> +			/* No SFP modes remain usable with the current interfaces. */
> +			linkmode_zero(pl->supported);
> +			linkmode_zero(pl->advertising);
> +			linkmode_zero(config->advertising);
>  		}
>  
>  		goto exit;

[Severity: Medium]
Can this else branch ever be reached when a PCS is removed, and what
happens to the non-SFP branches in phylink_update_capabilities() when
validation fails?

First, phylink_update_capabilities() passes unmasked pl->sfp_interfaces to
__phylink_validate_mask(), and in phylink_validate_mac_and_pcs() a missing
PCS in pl->pcs_list leaves pcs as NULL without returning -EINVAL:

drivers/net/phy/phylink.c:phylink_validate_mac_and_pcs() {
    ...
	} else if (test_bit(state->interface, pl->config->pcs_interfaces)) {
		struct phylink_pcs *tmp;

		list_for_each_entry(tmp, &pl->pcs_list, list) {
			if (!phylink_validate_pcs_interface(tmp, state->interface)) {
				pcs = tmp;
				break;
			}
		}
	}

	if (pcs) {
    ...
}

Because pcs is NULL, phylink_validate_mac_and_pcs() skips PCS validation
and succeeds against the MAC capabilities alone, so
__phylink_validate_mask() still returns 0 after the PCS is removed.

Second, when validation does return -EINVAL (for example, when
state->interface is no longer in pl->supported_interfaces), none of the
other branches in phylink_update_capabilities() clear the capability masks:
the generic fallback presets pl->supported and config->advertising with
linkmode_fill(), ignores the -EINVAL return from __phylink_validate(), and
leaves all linkmode bits set in pl->supported and pl->advertising;
similarly, the pl->phydev, MLO_AN_FIXED, and MLO_AN_INBAND paths leave
stale or unvalidated linkmodes in pl->supported and pl->advertising.

> @@ -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);
> +
> +	/*
> +	 * Check if we are removing the PCS currently
> +	 * in use by this phylink instance. If this is the case,
> +	 * tear down the link, force phylink resolve to reconfigure the
> +	 * interface mode, disable the current PCS and set the
> +	 * phylink PCS to NULL.
> +	 */
> +	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]
Does setting pl->force_major_config based on !pl->phylink_disable_state
interact safely with phylink_stop() and PHYLINK_DISABLE_LINK?

If pl->phylink_disable_state is 0 when phylink_del_pcs() sets
pl->force_major_config = true, and phylink_stop() runs after
pcs_provider_notify() drops rtnl_lock() but before pl->resolve executes,
phylink_stop() calls timer_delete_sync(&pl->link_poll) and then flushes
pl->resolve via phylink_run_resolve_and_disable().  In phylink_resolve(),
pl->force_major_config triggers phylink_major_config() even when
pl->phylink_disable_state is non-zero, reconfiguring stopped hardware and
potentially re-arming pl->link_poll via phylink_pcs_poll_start() after
phylink_stop() already deleted it.

Conversely, if the active PCS is removed while the instance is started
(pl->pcs_state == PCS_STATE_STARTED) but PHYLINK_DISABLE_LINK is set (such
as when SFP LOS is asserted), !pl->phylink_disable_state is false, so
pl->pcs is set to NULL without setting pl->force_major_config.  When
PHYLINK_DISABLE_LINK is later cleared on the same interface,
phylink_resolve() skips phylink_major_config() and calls phylink_link_up()
with pl->pcs == NULL.

> +
> +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);
> +
> +	/* 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;
> +	}
> +
> +	/* Exit early if nothing has changed */
> +	if (!resolve)
> +		goto exit;
> +
> +	/* Refresh supported interfaces */
> +	phy_interface_copy(pl->supported_interfaces,
> +			   pl->config->supported_interfaces);
> +	list_for_each_entry(pcs, &pl->pcs_list, list)
> +		phy_interface_or(pl->supported_interfaces,
> +				 pl->supported_interfaces,
> +				 pcs->supported_interfaces);
> +
> +	/* Recalculate capabilities */
> +	phylink_update_capabilities(pl, &pl->link_config);
> +
> +exit:
> +	mutex_unlock(&pl->state_mutex);
> +	if (phydev)
> +		mutex_unlock(&phydev->lock);
> +	mutex_unlock(&pl->phydev_mutex);
> +	rtnl_unlock();
> +
> +	if (resolve)
> +		phylink_run_resolve(pl);
> +
> +	return resolve ? NOTIFY_OK : NOTIFY_DONE;
> +}

[Severity: Medium]
When phylink_update_capabilities() restricts phydev->advertising or
pl->link_config.advertising after a PCS is removed, what updates the
hardware advertisement?

phylink_phy_update_capabilities() copies the restricted mask into
phy->advertising in memory, but once the PHY state machine has moved past
PHY_UP to PHY_NOLINK or PHY_RUNNING, phylib will not reprogram the PHY
autonegotiation registers unless _phy_start_aneg() is called.  Similarly,
if an unused PCS is removed in in-band mode without setting
pl->force_major_config, phylink_resolve() does not call
phylink_change_inband_advert().  An attached PHY can then continue
advertising and negotiating link modes that require the removed PCS,
causing phylink_major_config() to fail when the link comes up.

[ ... ]

> @@ -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);
> +	}
> +
>  	/* Fill the PCS list with available PCS from phylink config */
>  	ret = phylink_fill_available_pcs(pl, config);
>  	if (ret < 0)
> -		goto free_pl;
> +		goto unregister_pcs_notify;
> +
> +	mutex_lock(&pl->state_mutex);
>  
>  	/* Link available PCS to phylink */
>  	list_for_each_entry(pcs, &pl->pcs_list, list)
> @@ -2533,13 +2694,13 @@ struct phylink *phylink_create(struct phylink_config *config,
>  				 pl->supported_interfaces,
>  				 pcs->supported_interfaces);
>  
> -	mutex_lock(&pl->state_mutex);
>  	phylink_update_capabilities(pl, &pl->link_config);
> +
>  	mutex_unlock(&pl->state_mutex);
>  
>  	ret = phylink_parse_mode(pl, fwnode);
>  	if (ret < 0)
> -		goto unlink_pcs_list;
> +		goto unregister_pcs_notify;

[Severity: Medium]
Can pcs_provider_notify() race with phylink_create() before pl
initialization is finished?

phylink_create() is called without rtnl_lock() held and registers
pl->fwnode_pcs_nb before pl->pcs_list, pl->supported_interfaces,
pl->cfg_link_an_mode, and pl->link_config are initialized, dropping
pl->state_mutex between steps:

CPU0                                  CPU1
phylink_create()
  register_fwnode_pcs_notifier()
  phylink_fill_available_pcs()
    mutex_lock(&pl->state_mutex)
    ...
    mutex_unlock(&pl->state_mutex)
  mutex_lock(&pl->state_mutex)
  ...
  mutex_unlock(&pl->state_mutex)
  phylink_parse_mode()
    pl->cfg_link_an_mode = ...
                                      fwnode_pcs_del_provider()
                                        pcs_provider_notify()
                                          mutex_lock(&pl->state_mutex)
                                          phylink_update_capabilities()
  phylink_parse_fixedlink()
    pl->link_config.speed = ...
    __set_bit(..., lp_advertising)        <-- concurrent access to link_config

If pcs_provider_notify() runs after phylink_fill_available_pcs() unlocks
pl->state_mutex, it can also observe entries in pl->pcs_list before their
pcs->phylink pointer or pl->supported_interfaces is initialized.
Registering the notifier after pl is initialized (or keeping initialization
under pl->state_mutex before publishing the notifier) would avoid these
races.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117123956.11981-1-ansuelsmth@gmail.com?part=8

  reply	other threads:[~2026-11-18 12:42 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-18 12:42   ` sashiko-bot
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-18 12:42   ` sashiko-bot
2026-11-17 12:39 ` [PATCH net-next v22 04/15] net: pcs: implement Firmware node support for PCS driver Christian Marangi
2026-11-18 12:42   ` sashiko-bot
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-18 12:42   ` sashiko-bot
2026-11-17 12:39 ` [PATCH net-next v22 08/15] net: phylink: support PCS provider release Christian Marangi
2026-11-18 12:42   ` sashiko-bot [this message]
2026-11-17 12:39 ` [PATCH net-next v22 09/15] net: phylink: support late PCS provider attach Christian Marangi
2026-11-18 12:42   ` sashiko-bot
2026-11-17 12:39 ` [PATCH net-next v22 10/15] net: Document PCS subsystem Christian Marangi
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-18 12:42   ` sashiko-bot
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-18 12:42   ` sashiko-bot
2026-11-17 12:39 ` [PATCH net-next v22 15/15] net: airoha: add phylink support Christian Marangi
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=sashiko-outbox-220928@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ansuelsmth@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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