net: phy: Check against net_device being NULL [Linux 4.9.187]

This Linux kernel change "net: phy: Check against net_device being NULL" is included in the Linux 4.9.187 release. This change is authored by Ioana Ciornei <ioana.ciornei [at]> on Tue May 28 20:38:09 2019 +0300. The commit for this change in Linux stable tree is 6e6872b (patch) which is from upstream commit 82c76ac. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 82c76ac.

net: phy: Check against net_device being NULL

[ Upstream commit 82c76aca81187b3d28a6fb3062f6916450ce955e ]

In general, we don't want MAC drivers calling phy_attach_direct with the
net_device being NULL. Add checks against this in all the functions
calling it: phy_attach() and phy_connect_direct().

Signed-off-by: Ioana Ciornei <>
Suggested-by: Andrew Lunn <>
Reviewed-by: Andrew Lunn <>
Reviewed-by: Florian Fainelli <>
Signed-off-by: David S. Miller <>
Signed-off-by: Sasha Levin <>

There are 6 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/net/phy/phy_device.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 5048a6d..5c2c72b 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -673,6 +673,9 @@ int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
    int rc;

+   if (!dev)
+       return -EINVAL;
    rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
    if (rc)
        return rc;
@@ -965,6 +968,9 @@ struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
    struct device *d;
    int rc;

+   if (!dev)
+       return ERR_PTR(-EINVAL);
    /* Search the list of PHY devices on the mdio bus for the
     * PHY with the requested name

Leave a Reply

Your email address will not be published. Required fields are marked *