From: sashiko-bot@kernel.org
To: "James Hilliard" <james.hilliard1@gmail.com>
Cc: conor+dt@kernel.org, devicetree@vger.kernel.org, robh@kernel.org
Subject: Re: [PATCH net-next v3 3/4] net: mdio: make device map changes hotplug-safe
Date: Thu, 06 Aug 2026 17:49:30 +0000 [thread overview]
Message-ID: <20260806174931.51BAB1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260805-submit-mdio-of-dynamic-v2-v3-3-16f3cc893b0e@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [High] Race condition in `phy_claim()` causes concurrent MAC driver probes to fail with `-ENODEV` when attaching to a PHY.
- [Medium] Unbounded memory leak of dynamically removed PHY devices due to intentional pinning in `mdio_map_retired_phys`.
Pre-existing issues:
- [High] Missing NULL pointer check for `mdiobus_get_phy()` in `vsc8584_base_priv()` leads to kernel panic.
--
--- Patch [3]: [PATCH net-next v3 3/4] net: mdio: make device map changes hotplug-safe ---
commit d8ecb0a89add88ea9e28273a5be54ac5e3624bc8
Author: James Hilliard <james.hilliard1@gmail.com>
net: mdio: make device map changes hotplug-safe
MDIO device registration currently publishes directly into mdio_map
without serializing address ownership, and removal frees the object
immediately. That is sufficient while bus population is static, but
dynamic firmware changes can race registration, PHY attachment and bus
teardown. It also cannot safely preserve mdiobus_get_phy()'s
borrowed-pointer convention.
> diff --git a/drivers/net/phy/mdio_device.c b/drivers/net/phy/mdio_device.c
> --- a/drivers/net/phy/mdio_device.c
> +++ b/drivers/net/phy/mdio_device.c
[ ... ]
> +int mdiobus_begin_remove(struct mdio_device *mdiodev, bool dynamic)
> +{
> + struct mii_bus *bus = mdiodev->bus;
> + struct phy_device *phydev = NULL;
> + int err = 0;
[ ... ]
> + if (dynamic && mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
> + mdio_device_get(mdiodev);
> + list_add_tail(&phydev->retired_node,
> + &bus->mdio_map_retired_phys);
> + }
> +
> +out_unlock:
> + mutex_unlock(&bus->mdio_map_lock);
> + return err;
> +}
[Severity: Medium]
Will this cause an unbounded memory leak if PHYs are dynamically added and
removed continuously?
When a PHY is dynamically removed, mdiobus_begin_remove() takes a reference
and adds the device to the mdio_map_retired_phys list. These pinned devices
are intentionally never freed until mdiobus_remove_devices() runs during the
teardown of the entire parent MDIO bus. Continuous application and removal of
device tree overlays could exhaust memory over time.
> diff --git a/drivers/net/phy/mscc/mscc_ptp.c b/drivers/net/phy/mscc/mscc_ptp.c
> --- a/drivers/net/phy/mscc/mscc_ptp.c
> +++ b/drivers/net/phy/mscc/mscc_ptp.c
> @@ -1279,10 +1279,8 @@ static struct vsc8531_private *vsc8584_base_priv(struct phy_device *phydev)
> struct vsc8531_private *vsc8531 = phydev->priv;
>
> if (vsc8531->ts_base_addr != phydev->mdio.addr) {
> - struct mdio_device *dev;
> -
> - dev = phydev->mdio.bus->mdio_map[vsc8531->ts_base_addr];
> - phydev = container_of(dev, struct phy_device, mdio);
> + phydev = mdiobus_get_phy(phydev->mdio.bus,
> + vsc8531->ts_base_addr);
>
> return phydev->priv;
[Severity: High]
This isn't a bug introduced by this patch, but could this result in a kernel
panic if the base PHY fails to initialize?
If the base PHY does not successfully initialize during MDIO bus scanning
(e.g., due to a hardware fault), mdiobus_get_phy() will return NULL. The
subsequent direct dereference of phydev->priv will crash.
> }
>
> return vsc8531;
> }
> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
[ ... ]
> @@ -1121,25 +1124,40 @@ int phy_device_register(struct phy_device *phydev)
> err = phy_scan_fixups(phydev);
> if (err) {
> phydev_err(phydev, "failed to initialize\n");
> - goto out;
> + return mdiobus_registration_done(&phydev->mdio, err);
> }
>
> err = device_add(&phydev->mdio.dev);
> - if (err) {
> + if (err)
> phydev_err(phydev, "failed to add\n");
> - goto out;
> - }
>
> - return 0;
> + return mdiobus_registration_done(&phydev->mdio, err);
> }
[ ... ]
> +static int phy_claim(struct phy_device *phydev)
> +{
> + struct mdio_device *mdiodev = &phydev->mdio;
> + struct mii_bus *bus = mdiodev->bus;
> + int err = 0;
> +
> + mutex_lock(&bus->mdio_map_lock);
> + if (bus->state != MDIOBUS_REGISTERED ||
> + bus->mdio_map_removing ||
> + bus->mdio_map[mdiodev->addr] != mdiodev ||
> + (bus->mdio_map_pending & BIT(mdiodev->addr)))
> + err = -ENODEV;
[Severity: High]
Is it possible for a MAC driver probe to race with
mdiobus_registration_done() and fail permanently with -ENODEV?
In phy_device_register(), device_add() is called before
mdiobus_registration_done() completes the publication. Once device_add() is
called, the PHY becomes visible in the driver core's bus lists.
If a MAC driver concurrently probes the bus and finds the PHY, it will
attempt to attach. However, since mdiobus_registration_done() hasn't
executed yet, mdio_map_pending is still set for this address. The phy_claim()
check above will reject the valid attachment attempt with -ENODEV, causing
the MAC driver probe to permanently fail rather than deferring.
> + else if (phydev->attached)
> + err = -EBUSY;
> + else
> + phydev->attached = true;
> + mutex_unlock(&bus->mdio_map_lock);
> +
> + return err;
> +}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260805-submit-mdio-of-dynamic-v2-v3-0-16f3cc893b0e@gmail.com?part=3
next prev parent reply other threads:[~2026-08-06 17:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-05 17:49 [PATCH net-next v3 0/4] net: mdio: support dynamic OF device changes James Hilliard
2026-08-05 17:49 ` [PATCH net-next v3 1/4] net: phy: cache MDIO bus owner before dropping PHY reference James Hilliard
2026-08-05 17:49 ` [PATCH net-next v3 2/4] net: mdio: factor out OF child registration helpers James Hilliard
2026-08-05 17:49 ` [PATCH net-next v3 3/4] net: mdio: make device map changes hotplug-safe James Hilliard
2026-08-06 17:49 ` sashiko-bot [this message]
2026-08-05 17:49 ` [PATCH net-next v3 4/4] net: mdio: support dynamic OF device changes James Hilliard
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=20260806174931.51BAB1F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=james.hilliard1@gmail.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.