From: netdev-bot+sashiko@kernel.org
To: xiaolinkui@126.com
Cc: andrew+netdev@lunn.ch, netdev@vger.kernel.org, andrew@lunn.ch,
davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, rosenp@gmail.com, chunkeey@gmail.com,
jacob.e.keller@intel.com, kees+treewide@kernel.org,
daniel.machon@microchip.com, hkallweit1@gmail.com,
linux-kernel@vger.kernel.org, xiaolinkui@kylinos.cn
Subject: Re: [PATCH net v4] net: ibm: emac: attach and start the fixed-link PHY
Date: Sat, 28 Nov 2026 02:39:17 +0000 [thread overview]
Message-ID: <179583355780.3135.1650054860252955858@kernel.org> (raw)
In-Reply-To: <20261127014748.2908781-1-xiaolinkui@126.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] Use-after-free on the probe error path.
- [Medium] On the fixed-link path, the MAC link parameters are now filled
in asynchronously and without a lock shared with the readers.
- [Low] Since the fixed-link PHY is now attached and started at probe,
two writers manage the carrier on the fixed-link path and nothing…
- [Low] emac_remove() now calls phy_stop() before unregister_netdev().
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the PHY-
less/fixed-link branch of emac_init_phy() returns before it sets…
--
net: ibm: emac: attach and start the fixed-link PHY
This patch attaches the fixed-link PHY in emac_init_phy() with
of_phy_connect() and starts it through emac_mdio_init_phy(), so phylib
fills in dev->phy via emac_adjust_link(). It also restores phy_disconnect()
and of_phy_deregister_fixed_link() in emac_remove() and on the probe error
path, and stops the PHY before unregister_netdev().
> diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
> index 1b95b4d5deff5..27b79f897e36f 100644
> --- a/drivers/net/ethernet/ibm/emac/core.c
> +++ b/drivers/net/ethernet/ibm/emac/core.c
> @@ -2673,11 +2673,17 @@ static int emac_init_phy(struct emac_instance *dev)
> return res;
>
> res = of_phy_register_fixed_link(np);
> - ndev->phydev = of_phy_find_device(np);
> - if (res || !ndev->phydev)
> - return res ? res : -EINVAL;
> - emac_adjust_link(dev->ndev);
> - put_device(&ndev->phydev->mdio.dev);
> + if (res)
> + return res;
> +
> + if (!of_phy_connect(ndev, np, &emac_adjust_link, 0,
> + dev->phy_mode))
> + return -ENODEV;
> +
> + /* Start it the same way as a real PHY. */
> + res = emac_mdio_init_phy(&dev->phy);
[Severity: Medium]
Before this patch, emac_adjust_link() was called synchronously here, so
dev->phy was filled in by the time probe returned. Now phy_start() only
queues phy_state_machine(). The first emac_adjust_link() runs later from
the workqueue, holding only phydev->lock:
emac_adjust_link() {
dev->phy.autoneg = phy->autoneg;
dev->phy.speed = phy->speed;
dev->phy.duplex = phy->duplex;
dev->phy.pause = phy->pause;
dev->phy.asym_pause = phy->asym_pause;
...
}
On the fixed-link path, emac_open() holds RTNL and dev->link_lock but never
refreshes these fields before it builds MR1:
emac_open() {
...
if (dev->phy.address >= 0) {
...
} else
netif_carrier_on(dev->ndev);
...
emac_configure(dev);
...
}
emac_configure() reads dev->phy.speed, duplex, pause and asym_pause, and
the two lock sets do not overlap. If emac_open() runs before the first
state machine pass, MR1 gets the probe defaults (SPEED_100, DUPLEX_FULL).
If it runs during that pass, it can see a mix of old and new fields.
emac_adjust_link() never reprograms MR1, and a fixed link without
link-gpios never changes state again. Can the wrong MR1 then stay in place
until the next ifdown/ifup?
The commit message says:
That window is the one the phy-handle path has always had,
this driver starts its PHY at probe time rather than in ndo_open, and the
next ifdown/ifup closes it.
Is that accurate? On the phy-handle path (dev->phy.address >= 0),
emac_open() reads the link synchronously through poll_link()/read_link(),
which ends up in phy_read_status(). emac_link_timer() also reprograms the
MAC on link changes. A fixed link has neither, so a fixed link can still
be programmed with the wrong speed, duplex or pause, which is the bug this
patch is meant to fix.
[Severity: Low]
Now that the fixed-link PHY is started here, two writers set the carrier
on this path. phylib's phy_link_change() calls netif_carrier_on()/off()
from the state machine work. The driver still calls netif_carrier_off() in
emac_probe() before register_netdev(), netif_carrier_on() in emac_open()
for dev->phy.address < 0, and netif_carrier_off() in emac_close().
Nothing orders these writers. phy_start() here runs before the
netif_carrier_off() in emac_probe(). If the work runs after that call but
while reg_state is still NETREG_UNINITIALIZED, netif_carrier_on() clears
__LINK_STATE_NOCARRIER without a linkwatch event. The netdev then
registers with the carrier on while it is administratively down. The later
netif_carrier_on() in emac_open() is a no-op, so no linkwatch event fires
on open.
Could operstate then stay at UNKNOWN instead of moving to UP?
If the work runs first instead, the netif_carrier_off() in emac_probe()
overrides phylib, and phylib then thinks the link is up while the carrier
is off. With link-gpios, phylib could also turn the carrier back on for a
closed interface.
Should the driver leave the carrier to phylib in the fixed-link case?
> + if (res)
> + return res;
> }
> return 0;
> }
[Severity: High]
This isn't a bug introduced by this patch, but the PHY-less/fixed-link
branch of emac_init_phy() returns before these assignments further down:
dev->phy.mdio_read = emac_mdio_read;
dev->phy.mdio_write = emac_mdio_write;
...
dev->phy.gpcs_address = dev->gpcs_address;
The private area is zeroed, so for a fixed link dev->phy.mdio_read is NULL
and dev->phy.gpcs_address is 0. With phy-mode sgmii, tbi or rtbi,
emac_open()->emac_configure() reaches:
if (emac_phy_gpcs(dev->phy.mode)) {
if (dev->phy.gpcs_address != 0xffffffff)
emac_mii_reset_gpcs(&dev->phy);
Then emac_mii_reset_gpcs()->gpcs_phy_read() does:
return phy->mdio_read(phy->dev, phy->gpcs_address, reg);
Does this call a NULL function pointer with RTNL and dev->link_lock held?
emac_reset_work() and the reinit path in emac_link_timer() reach
emac_configure() the same way.
There is a second effect now that the real fixed-link speed reaches
dev->phy. For a 1000 Mbit/s GPCS fixed link, MR1 is programmed with
EMAC_MR1_MF_IPPA(0) instead of the gpcs-address from DT. The oops above
would happen first, though.
> @@ -3190,6 +3196,10 @@ static int emac_probe(struct platform_device *ofdev)
> /* I have a bad feeling about this ... */
>
> err_detach_tah:
> + if (ndev->phydev)
> + phy_disconnect(ndev->phydev);
> + if (of_phy_is_fixed_link(np))
> + of_phy_deregister_fixed_link(np);
[Severity: High]
Can this unwind leave a linkwatch event queued on a netdev that devres
then frees?
The fixed-link PHY is started in emac_init_phy() before register_netdev(),
so the state machine can run during registration:
emac_init_phy()
emac_mdio_init_phy()
phy_start()
phy_state_machine()
phy_link_up()->phy_link_change(true)
netif_carrier_on() <- clears NOCARRIER, no event yet
register_netdev()
register_netdevice()
netdev_register_kobject() fails <- reg_state = NETREG_UNREGISTERED
goto err_detach_tah
phy_disconnect()
phy_stop()
phy_link_down()->phy_link_change(false)
netif_carrier_off()
linkwatch_fire_event() <- netdev_hold(), added to lweventlist
emac_probe() returns the error
devm action: free_netdev()
put_device() <- net_device freed
netif_carrier_off() only suppresses the event for NETREG_UNINITIALIZED:
if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
if (dev->reg_state == NETREG_UNINITIALIZED)
return;
atomic_inc(&dev->carrier_down_count);
linkwatch_fire_event(dev);
free_netdev() does not wait for the linkwatch reference, so
linkwatch_event() would later walk lweventlist and dereference the freed
net_device. A NETDEV_REGISTER notifier veto ends in the same state. If the
state machine first runs after the failure, its own netif_carrier_on()
queues the event in the same way.
The commit message describes this hazard for emac_remove(), but the probe
unwind does not seem to be covered. Would starting the PHY only after
register_netdev() succeeds, or from ndo_open/ndo_stop, avoid this?
> @@ -3214,6 +3224,14 @@ static void emac_remove(struct platform_device *ofdev)
>
> DBG(dev, "remove" NL);
>
> + /* Stop the PHY first: phy_stop() turns the carrier off and queues a
> + * linkwatch event, which unregister_netdev() then drains.
> + * phy_disconnect() has to stay below, detaching a PHY from a
> + * registered netdev wants RTNL and we do not hold it here.
> + */
> + if (dev->ndev->phydev)
> + phy_stop(dev->ndev->phydev);
[Severity: Low]
For a phy-handle PHY, phy_stop() also powers the PHY down. PHY_HALTED
returns PHY_STATE_WORK_SUSPEND, _phy_state_machine_post_work() calls
phy_suspend(), and genphy_suspend() sets BMCR_PDOWN. Here that happens
while the MAC is still running, since emac_close() only runs later inside
unregister_netdev().
emac_close() then calls emac_rx_disable()/emac_tx_disable(), which busy
wait for the idle bits:
while (!(in_be32(&p->mr0) & EMAC_MR0_RXI) && n) {
udelay(1);
--n;
}
if (unlikely(!n))
emac_report_timeout_error(dev, "RX disable timeout");
If the powered-down PHY stops supplying RX_CLK/TX_CLK, could these waits
run to the full timeout and log "RX/TX disable timeout" on unbind?
The link-loss handling in emac_link_timer() (emac_rx_clk_tx() and
emac_reinitialize()) is also skipped here, because phylib has already
cleared the carrier.
Before commit baab9de385a8, the PHY was disconnected only after
unregister_netdev(), so the MAC was stopped while the PHY still had power.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127014748.2908781-1-xiaolinkui%40126.com
prev parent reply other threads:[~2026-11-28 2:39 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 1:47 Linkui Xiao
2026-11-28 2:39 ` netdev-bot+sashiko [this message]
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=179583355780.3135.1650054860252955858@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=andrew@lunn.ch \
--cc=chunkeey@gmail.com \
--cc=daniel.machon@microchip.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=hkallweit1@gmail.com \
--cc=jacob.e.keller@intel.com \
--cc=kees+treewide@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rosenp@gmail.com \
--cc=xiaolinkui@126.com \
--cc=xiaolinkui@kylinos.cn \
/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
all inboxes | Powered by JetHome®