Re: [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker
From: netdev-bot+sashiko
Date: Fri Nov 20 2026 - 01:42:00 EST
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