mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linkui Xiao <xiaolinkui@126.com>
To: maxime.chevallier@bootlin.com, andrew@lunn.ch, kuba@kernel.org,
	davem@davemloft.net, edumazet@kernel.org, pabeni@redhat.com,
	horms@kernel.org, devnexen@gmail.com, sdf@fomichev.me,
	2022090917019@std.uestc.edu.cn
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Linkui Xiao <xiaolinkui@kylinos.cn>
Subject: [PATCH net v2] net: ethtool: phy: warn about a phydev with no link topology
Date: Thu, 12 Nov 2026 15:55:17 +0800	[thread overview]
Message-ID: <20261112075517.3904496-1-xiaolinkui@126.com> (raw)

From: Linkui Xiao <xiaolinkui@kylinos.cn>

phy_prepare_data() reads reply_data->dev->link_topo and dereferences it
without checking it:

	struct phy_link_topology *topo = reply_data->dev->link_topo;
	...
	pdn = xa_load(&topo->phys, phydev->phyindex);

ethnl_req_get_phydev() returns dev->phydev as-is when the request carries
no phy-index, without consulting the topology, so a driver that populates
net_device->phydev without going through phy_attach_direct() reaches the
xa_load() above with topo == NULL. The IBM EMAC driver did exactly that on
its fixed-link path; it is fixed separately by "net: ibm: emac: attach the
fixed-link PHY with of_phy_connect()".

This is the only place in net/ethtool/ that touches ->link_topo without
coping with a missing topology: phy_link_topo_empty() is "return
!dev->link_topo", phy_link_topo_get_phy() and phy_link_topo_get_port()
both return NULL when it is NULL, and common.c, port.c, tsinfo.c and
ethnl_perphy_dump_one_dev() all test dev->link_topo first. Before
commit 9dd2ad5e92b9 ("net: ethtool: phy: Convert the PHY_GET command to
generic phy dump"), ethnl_phy_parse_request() bailed out with "if (!topo)
return 0;" before reaching the xarray; that check was not carried over
into phy_prepare_data().

A netdev with a phydev but no topology is a driver bug and is worth being
told about, so do not fail silently: warn once and answer -EOPNOTSUPP,
which is what phy_prepare_data() already returns when the phydev cannot be
found in the topology. Compared with the current NULL dereference this
keeps the driver bug in the log without oopsing the box for somebody who
merely ran ethtool on an otherwise working interface, which is a rather
indirect way of pointing at a MAC driver. The dump side already returns
early when dev->link_topo is NULL, so the warning can only fire from the
doit path, at most once per boot.

Verified by compilation; there is no EMAC fixed-link device here to
trigger it.

Fixes: 9dd2ad5e92b9 ("net: ethtool: phy: Convert the PHY_GET command to generic phy dump")
Signed-off-by: Linkui Xiao <xiaolinkui@kylinos.cn>
---
v2: reworked after Andrew Lunn's review.

 - Point the finger at the real bug: the IBM EMAC driver assigning
   net_device->phydev by hand on its fixed-link path. That is now fixed
   separately with "net: ibm: emac: attach the fixed-link PHY with
   of_phy_connect()".
 - Keep the driver bug visible instead of papering over it: v1 silently
   returned -EOPNOTSUPP, v2 warns once and then returns -EOPNOTSUPP.
   Happy to drop this patch entirely if the NULL dereference is preferred
   as the way to catch such drivers.
 - Dropped Cc: stable.

v1: 
 - Link: https://lore.kernel.org/all/20261111072146.3293821-1-xiaolinkui@126.com/
---
 net/ethtool/phy.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/ethtool/phy.c b/net/ethtool/phy.c
index 018b041..7e7e244 100644
--- a/net/ethtool/phy.c
+++ b/net/ethtool/phy.c
@@ -83,6 +83,9 @@ static int phy_prepare_data(const struct ethnl_req_info *req_info,
 	if (IS_ERR_OR_NULL(phydev))
 		return -EOPNOTSUPP;
 
+	if (WARN_ON_ONCE(!topo))
+		return -EOPNOTSUPP;
+
 	pdn = xa_load(&topo->phys, phydev->phyindex);
 	if (!pdn)
 		return -EOPNOTSUPP;
-- 
2.25.1


             reply	other threads:[~2026-11-12  7:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  7:55 Linkui Xiao [this message]
2026-11-12  8:02 ` netdev-bot+sinfo
2026-11-12  8:09   ` Linkui Xiao

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=20261112075517.3904496-1-xiaolinkui@126.com \
    --to=xiaolinkui@126.com \
    --cc=2022090917019@std.uestc.edu.cn \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=devnexen@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --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®