Article View: linux.kernel.netdev
Article #21411Re: [PATCH net] net: bgmac: unregister the fixed PHY on probe failure and removal
From: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 00:40
Date: Tue, 24 Nov 2026 00:40
197 lines
14993 bytes
14993 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 (1) · Low: 0
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] The new bgmac_phy_disconnect() reads phydev after
phy_disconnect() may have freed it, which is a use-after-free on the…
- [Medium] bgmac_phy_disconnect() picks the release method from the
device tree, not from how the connected PHY was registered.
Pre-existing issues:
- [Medium] This problem wasn't introduced by this patch, but
of_phy_get_and_connect() in drivers/net/mdio/of_mdio.c can leak a…
--
net: bgmac: unregister the fixed PHY on probe failure and removal
This patch adds bgmac_phy_disconnect(). It disconnects the PHY and then
releases any fixed PHY, using of_phy_deregister_fixed_link() for DT
fixed-link nodes and fixed_phy_unregister() otherwise. The helper is used
on the bgmac_enet_probe() error path and in bgmac_enet_remove(). Also,
bgmac_phy_connect_direct() now unregisters its fixed PHY when
phy_connect_direct() fails.
> diff --git a/drivers/net/ethernet/broadcom/bgmac.c b/drivers/net/ethernet/broadcom/bgmac.c
> index 9ef7307edd41e..54092bfacd189 100644
> --- a/drivers/net/ethernet/broadcom/bgmac.c
> +++ b/drivers/net/ethernet/broadcom/bgmac.c
[ ... ]
> @@ -1482,6 +1484,22 @@ int bgmac_phy_connect_direct(struct bgmac *bgmac)
> }
> EXPORT_SYMBOL_GPL(bgmac_phy_connect_direct);
>
> +/* Disconnect the PHY and release a fixed PHY registered for the connection. */
> +static void bgmac_phy_disconnect(struct bgmac *bgmac)
> +{
> + struct device_node *np = bgmac->dev->of_node;
> + struct phy_device *phydev = bgmac->net_dev->phydev;
> +
> + if (!phydev)
> + return;
> +
> + phy_disconnect(phydev);
> + if (np && of_phy_is_fixed_link(np))
> + of_phy_deregister_fixed_link(np);
[Severity: Medium]
Does this leak the fixed PHY created by bgmac_phy_connect_direct() when
the DT node also satisfies of_phy_is_fixed_link()?
The branch is picked from the DT node, not from how the connected PHY was
registered. bgmac_phy_connect_direct() registers its PHY with no of_node:
phy_dev = fixed_phy_register(&fphy_status, NULL);
So of_phy_deregister_fixed_link(np) can't find it through
of_phy_find_device(np) and returns without a warning. Because the fallback
is an else-if, fixed_phy_unregister() never runs.
There seem to be two ways to end up here. In bgmac-platform.c,
bgmac_probe() picks bgmac_phy_connect_direct() whenever phy-handle is
missing:
phy_node = of_parse_phandle(np, "phy-handle", 0);
if (phy_node) {
of_node_put(phy_node);
bgmac->phy_connect = platform_phy_connect;
} else {
bgmac->phy_connect = bgmac_phy_connect_direct;
brcm,amac.yaml references ethernet-controller.yaml, so a node with a
fixed-link child or a managed property and no phy-handle passes the
schema.
In bgmac-bcma.c, bcma_phy_connect() falls back to
bgmac_phy_connect_direct() when of_phy_get_and_connect() returns NULL. One
case is a fixed-link node without phy-mode. Another is of_phy_connect()
failing after of_phy_register_fixed_link() has already succeeded. In that
second case, teardown would remove the stale, unconnected DT fixed PHY and
leak the connected direct one.
None of the in-tree DTs seem to hit this today. The gmac nodes in
bcm-ns.dtsi and bcm53573.dtsi have both phy-mode and fixed-link. Still,
this is the same leak the commit message describes.
Could the release method be based on the phydev itself? For example,
call of_phy_deregister_fixed_link() only when the phydev's fwnode matches
np, and otherwise call fixed_phy_unregister() for any pseudo fixed link.
> + else if (phy_is_pseudo_fixed_link(phydev))
> + fixed_phy_unregister(phydev);
[Severity: High]
Can phy_is_pseudo_fixed_link(phydev) read freed memory here?
On the bcma remove path, bgmac_remove() unregisters the MDIO bus before
it calls bgmac_enet_remove():
drivers/net/ethernet/broadcom/bgmac-bcma.c:bgmac_remove() {
...
bcma_mdio_mii_unregister(bgmac->mii_bus);
bgmac_enet_remove(bgmac);
...
}
mdiobus_unregister() calls device_remove() and then device_free() on the
PHY at bgmac->phyaddr, which drops the registration reference. After that,
the only reference left is the get_device() taken in phy_attach_direct().
The call chain is then:
bgmac_enet_remove()
bgmac_phy_disconnect()
phy_disconnect()
phy_detach()
phy_detach_internal()
put_device(&phydev->mdio.dev) <- can free phydev
phy_detach_internal() itself has a comment warning that "The phydev might
go away on the put_device() below".
The first condition is false when np is NULL (BCM47xx without DT). It is
also false on DT boards whose gmac uses phy-handle and has no fixed-link.
In both cases the else-if reads phydev->is_pseudo_fixed_link from the
freed object. If that stale bit reads as set, fixed_phy_unregister() runs
on freed memory.
Before this patch, bgmac_enet_remove() did not touch phydev after
phy_disconnect().
Would it work to evaluate phy_is_pseudo_fixed_link(phydev) and the DT
check before calling phy_disconnect()? Separately, could bgmac_remove() be
reordered so that bgmac_enet_remove() runs before
bcma_mdio_mii_unregister()?
> +}
> +
> struct bgmac *bgmac_alloc(struct device *dev)
> {
> struct net_device *net_dev;
> @@ -1575,7 +1593,7 @@ int bgmac_enet_probe(struct bgmac *bgmac)
> return 0;
>
> err_phy_disconnect:
> - phy_disconnect(net_dev->phydev);
> + bgmac_phy_disconnect(bgmac);
> err_dma_free:
> bgmac_dma_free(bgmac);
[Severity: Medium]
This is a pre-existing issue, not one introduced by this patch. Is the DT
fixed PHY leaked when bgmac_phy_connect() fails and probe jumps to
err_dma_free?
If of_phy_connect() fails, of_phy_get_and_connect() returns NULL without
deregistering the fixed link it just registered:
drivers/net/mdio/of_mdio.c:of_phy_get_and_connect() {
...
if (of_phy_is_fixed_link(np)) {
ret = of_phy_register_fixed_link(np);
...
phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
of_node_put(phy_np);
return phy;
}
In bgmac-platform.c, platform_phy_connect() turns that into -ENODEV.
bgmac_enet_probe() then takes the err_dma_free path and skips
bgmac_phy_disconnect(). Calling it wouldn't help anyway, because
net_dev->phydev is NULL at that point.
In bgmac-bcma.c, bcma_phy_connect() then falls back to phy_connect() or
bgmac_phy_connect_direct(). That can leave two fixed PHYs registered.
This should be rare: of_phy_connect() on a freshly registered fixed PHY
mostly fails only on allocation errors. Other users of this helper, such
as ftgmac100 and gemini, don't clean up on this failure either.
> err_out:
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120003044.2366432-1-florian.fainelli%40broadcom.com
Technical Details
Message-ID:
<179548085846.3135.4498796165187686052@kernel.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-369888-linux-kernel-netdev=dewy.ruReferences:
<20261120003044.2366432-1-florian.fainelli@broadcom.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-369888-linux-kernel-netdev=dewy.ru From: netdev-bot+sashiko@kernel.org Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net] net: bgmac: unregister the fixed PHY on probe failure and removal Date: Tue, 24 Nov 2026 00:40:58 +0000 Organization: Usenet Network Message-ID: <179548085846.3135.4498796165187686052@kernel.org> References: <20261120003044.2366432-1-florian.fainelli@broadcom.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="4017939"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:JV38dkTKrUBhuz7SMk0bbDWAUzA= sha256:I1R3yaQyPNXZdgv0rCd5qTrchxoNx0V94feKqwVk3YA= sha1:tuROHg9nv/Eefb77wt87qsBSQsg= sha256:t9TsJ9dWOQtMCxMBSMD5NyTz2BxEVlfS9nNUZkoshSw= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 6D9A7125DC8; Tue, 24 Nov 2026 03:41:36 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6igHwdkEnR4n; Tue, 24 Nov 2026 03:41:32 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-369888-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id 4162F125DCA; Tue, 24 Nov 2026 03:41:23 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sin.lore.kernel.org (Postfix) with ESMTP id 42E0829191; Tue, 24 Nov 2026 00:41:21 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795480860; b=oghgQ6NO+nGj06AoKxw6Ytz5f7SkgbnBq6fWp6dld7Cxy4g05STs0530y8gVxhNHWs48 nKUVgcXYhLzE6/6Z3qnzhEPfuLlGtXd4FFXen+QWRwl1d+9/7485ZjzOf8c7NrGGSAm4u 9l6eaUiBEe495X5hfRolg5NomRuHQwsY7Wui4JtYKYGWhPZ5adPB7b0wgFAt7qawcESRi 8DLFSAVhjqJTUhY71nqGo14HCkmVRQ8oF+T9PpLdRYMHWRCmdi4N0mq+1n6TioLzjXaGW cHJryl+7LElgHPlaiMR+WV0GU/4VZDUZr54huwthL6DZSwhWTwNQ2XwpEJ+gD8yPJ6g== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795480860; h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc: Date:Message-ID:In-Reply-To:References:X-sashiko-severity: Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version; bh=GQ2l0lAcStEFkwyCF6a+mLahg3rjU7KyaIT0/szFxi0=; b=ANQPYZWeKQx51XBJJvvtM0vehgFbdmQ7OjNrW4M/bidz3b8Y1B93rmhMx/ZBjCOqSJJH k9x+ilvj0iaAU+aoceq8otMrVQjm3oNYX3fszbY3w+Wq1PJ9uAnHGWTCz5cHIpSZHK72V ltfMdOYCLY3IPnViNMcm7ohfcyd403LOJocJllufB3see34pO0uWbcKzPxqkMp7aGAy3T kvbR6jugzdbUKfF4KmDh17hHnKMKvrWWqeiYqLYGvW3VIFVo9XglPaEmoldBC/S7dRKd3 qSVb3YQkvg8T9A1nlZHYOenfEbB21Mx9vchdzP3ON8ijWI2TIgjSxn87vbTIkFocVag== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZXi0AxoE; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D09C260A3830; Tue, 24 Nov 2026 00:41:00 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZXi0AxoE X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795480860; b=clkUP98GJfUkg2et0KL76//YFEzYSgDnzcG5uyZyIsCMrTw8IH3tLNhChvTzxXWl7sRT aoVXHEJB4AwuCZA6GjyoNn2fKBKvEF8w/zRTa6JA/Ck01eXQ8Ybd8pPmlI7Oh+rg0pDV1 nL9+xdKfkB46cnc7Ah3JENHt+Qq8mpZzTsi6WaUQBFdmc7sYh0+xx5gkLcDbam5XMkD1u D8TyMUz53OyUP48fngtIfD/cCyhT44g2fq55Tw31jbAya91uiHhZDB0LruyGh08CYNBYe gfsLdqR5rkloIfh9zPKcTh6mRtmPFeiJ/ifD6qPVWJBHmPrnUSHehtMJbpwYnI6ar4g== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795480860; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=GQ2l0lAcStEFkwyCF6a+mLahg3rjU7KyaIT0/szFxi0=; b=fiPEwZBJL0OetLp5Yt8wu9m8ogB2TEOLqyFpqXHQrQWT3STm18S7YOzyrTsUv+B3Dfs3 H5dRS5rrRIXIa6n1geGKN5kN7F7e/t3388hUFDPsS/Raq+DC3T9UlcY4tnzpyvA5jLbwg 5WTCks0Q1gx0Engu8bNg9s1T8cQaxg+VSl5kDPDLh/DWBsl3iICt8vV9qYM+O89OOPR3E 8XF23NCOpsiX0iLMvAeidfnJi1kV3edQ7qyQNoKghQRMswlVyMlVylRZn5FFsu4Ao3xI8 RfWLfQO6YsCXy6pS26wIA1AcSoqcgXbW9ydx2YsQjr+82Wofw99KyufjBhYC+jYJcDw== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZXi0AxoE; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1850606A6BC; Tue, 24 Nov 2026 00:40:59 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0A801F01574; Tue, 24 Nov 2026 00:40:58 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795480859; bh=GQ2l0lAcStEFkwyCF6a+mLahg3rjU7KyaIT0/szFxi0=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=ZXi0AxoEBJEMvlvpV4hETWSX5Vess1KdAnxGknUeOjimlPqoM7zVfhxEIzkKO/U+4 4JvoN0lXvsth24i/VMQ8jjVhazjSK7c7gAaISm2DMTiGqakw5OW490bCwrhl9FgiyY 3fojlsUu/F9bu+lirmWL4ioBr06rmc6DCJgYaXnHngGLdqXjZT7V1DLhbuNQ6Kpjc9 pDqGdoG0NNolQWy2hwvlCdBV981iTGQJveywLiEMIElxtSJPQqOVtPWhTXYLKsOcmd grkZHDpVtUDA9jQ879yZLucgFuTTtBf+QSFm0kubS8swZjQurD4GsxUeG6pYCNA0Pr hdXH71h+59j1g== X-Cc: netdev@vger.kernel.org, rosenp@gmail.com, rafal@milecki.pl, bcm-kernel-feedback-list@broadcom.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, linux-kernel@vger.kernel.org X-In-Reply-To: <20261120003044.2366432-1-florian.fainelli@broadcom.com> X-sashiko-severity: High X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list