The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: github@szelinsky.de
Cc: o.rempel@pengutronix.de, kory.maincent@bootlin.com,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	broonie@kernel.org, lgirdwood@gmail.com,
	maxime.chevallier@bootlin.com, kyle.swenson@est.tech,
	ross@r-sc.ca, netdev-bot+sashiko@kernel.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker
Date: Fri, 20 Nov 2026 06:41:48 +0000	[thread overview]
Message-ID: <179515690895.3136.4077280921885475671@kernel.org> (raw)
In-Reply-To: <20261118183957.2031311-6-github@szelinsky.de>

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 new kernel-doc implies that a NULL attached_phydev is
  temporary, but the code makes it permanent.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
  pse_send_ntf_worker() can still use a freed net_device.

--

net: pse-pd: do not reach a freed PHY from the notification worker

The notification worker used to keep a reference on the pse_control and
then dereference psec->attached_phydev, which could already be freed. With
this patch the worker looks up the pse_control and reads attached_phydev
under pse_list_mutex, nested inside rtnl. When a second PHY takes a shared
PI, pse_control_get_internal() now clears attached_phydev and warns.

> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 0894613515e0d..b8cba6f86a7fd 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
> @@ -31,7 +31,9 @@ static DEFINE_MUTEX(pse_pw_d_mutex);
>   * @list: list entry for the pcdev's PSE controller list
>   * @id: ID of the PSE line in the PSE controller device
>   * @refcnt: Number of gets of this pse_control
> - * @attached_phydev: PHY device pointer attached by the PSE control
> + * @attached_phydev: PHY device pointer attached by the PSE control, or NULL
> + *                   when several PHYs share the PI. Read it only under
> + *                   pse_list_mutex.

[Severity: Low]
This isn't a bug, but "NULL when several PHYs share the PI" reads as if
the field is only NULL while the PI is shared. Is that what is intended?

The only writes to attached_phydev are the assignment when the pse_control
is created and the new clear in pse_control_get_internal(). Neither
pse_control_put() nor __pse_control_put_internal() sets it back.

So if PHY A and PHY B share a PI and B is removed, only A holds the
pse_control, but attached_phydev stays NULL. The commit message describes
this:

  Events for that PI are then no longer reported, even after one PHY goes
  away.

The new pse_control_get_netdev() kernel-doc has the same problem:

  Return: netdev pointer, or NULL if no single PHY is attached

It also leaves out the NULL return when the single PHY has no
attached_dev.

Would wording like "NULL once the PI has been shared by several PHYs"
match the code better?

[ ... ]

> @@ -1014,13 +1017,18 @@ static void pse_send_ntf_worker(struct work_struct *work)
>  		struct net_device *netdev;
>  		struct pse_control *psec;
>  
> -		psec = pse_control_find_by_id(pcdev, ntf.id);
> +		/* pse_control_put() frees the pse_control under
> +		 * pse_list_mutex before its PHY is freed, so the mutex
> +		 * keeps attached_phydev valid.
> +		 */
>  		rtnl_lock();
> +		mutex_lock(&pse_list_mutex);
> +		psec = pse_control_find_by_id(pcdev, ntf.id);
>  		netdev = pse_control_get_netdev(psec);
>  		if (netdev)
>  			ethnl_pse_send_ntf(netdev, ntf.notifs);

[Severity: High]
This is a pre-existing issue, not one introduced by this patch, but can
the worker still use a freed net_device here?

pse_control_get_netdev() returns psec->attached_phydev->attached_dev
without taking a netdev reference. ethnl_pse_send_ntf() then dereferences
it in ethnl_fill_reply_header() and ethnl_multicast().

Neither rtnl nor pse_list_mutex excludes the paths that clear attached_dev
or free the netdev. phy_detach_internal() clears it without needing rtnl:

drivers/net/phy/phy_device.c:phy_detach_internal() {
    ...
    phydev->attached_dev->phydev = NULL;
    phydev->attached_dev = NULL;
    ...
}

Its own comment says "Drivers may detach without RTNL while dev is
unregistered". free_netdev() on a NETREG_UNREGISTERED device does not need
rtnl either.

usbnet_disconnect() is one example of a remove path that does all of this
outside rtnl:

CPU0 (usbnet_disconnect)            CPU1 (pse_send_ntf_worker)
unregister_netdev(net);
                                    rtnl_lock();
                                    mutex_lock(&pse_list_mutex);
                                    netdev = pse_control_get_netdev(psec);
                                      /* attached_dev still set */
->unbind()
  smsc95xx_unbind()
    phy_disconnect()
free_netdev(net);
                                    ethnl_pse_send_ntf(netdev, ...);

The PHY stays on the MDIO bus in this sequence, so psec and attached_phydev
remain valid, and the new pse_list_mutex scheme does not cover the netdev.

Does the worker also need a lifetime guarantee for the netdev? For
example, a netdev reference taken when the PHY is attached, or a reg_state
check together with serialisation against phy_detach.

> +		mutex_unlock(&pse_list_mutex);
>  		rtnl_unlock();
> -		pse_control_put(psec);
>  	}
>  }

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118183957.2031311-1-github%40szelinsky.de

  reply	other threads:[~2026-11-20  6:41 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 01/14] net: pse-pd: size the notification kfifo after defaulting nr_lines Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 02/14] net: pse-pd: remove dead pi_get_admin_state() checks Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 03/14] net: pse-pd: fix module refcount leak in pse_control_get_internal() Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-20  8:32     ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko [this message]
2026-11-20  8:33     ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-20  8:33     ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-20  8:33     ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-20  8:33     ` Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 09/14] net: pse-pd: unregister from the controller list before freeing PI data Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 10/14] net: pse-pd: keep the PI data until the PI regulators are gone Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 11/14] net: pse-pd: free the notification kfifo when registration fails Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 12/14] net: pse-pd: use each PI's own supply handle for the power budget Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 13/14] net: pse-pd: pd692x0: release the manager power budget through devres Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 14/14] net: pse-pd: pd692x0: defer probe while a manager supply is unresolved Carlo Szelinsky

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=179515690895.3136.4077280921885475671@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=broonie@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=github@szelinsky.de \
    --cc=horms@kernel.org \
    --cc=kory.maincent@bootlin.com \
    --cc=kuba@kernel.org \
    --cc=kyle.swenson@est.tech \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=ross@r-sc.ca \
    /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