From: Wang Zhan <wang.zhan@smartx.com>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com, horms@kernel.org, keyong.sun@smartx.com,
Wang Zhan <wang.zhan@smartx.com>
Subject: [PATCH net-next v2 1/2] net: sysfs: factor out link settings read
Date: Thu, 3 Sep 2026 21:33:13 +0800 [thread overview]
Message-ID: <20260903133314.3703381-2-wang.zhan@smartx.com> (raw)
In-Reply-To: <20260903133314.3703381-1-wang.zhan@smartx.com>
speed_show() and duplex_show() duplicate device validation, RTNL
locking and the link settings query. Move this common work to
sysfs_get_link_ksettings() so later locking changes stay in one place.
RTNL is released before calling sysfs_emit(). The lock only protects the
link settings query; formatting uses the local cmd copy, so the sysfs
output and error handling remain unchanged.
No functional changes.
Assisted-by: LLM
Signed-off-by: Wang Zhan <wang.zhan@smartx.com>
---
net/core/net-sysfs.c | 97 ++++++++++++++++++++++----------------------
1 file changed, 48 insertions(+), 49 deletions(-)
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 352173df75785..7bb8bbc1f71ea 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -118,6 +118,34 @@ static int sysfs_rtnl_lock(struct kobject *kobj, struct attribute *attr,
return ret;
}
+static int sysfs_get_link_ksettings(struct device *dev,
+ struct device_attribute *attr,
+ struct ethtool_link_ksettings *cmd)
+{
+ struct net_device *netdev = to_net_dev(dev);
+ int ret;
+
+ /*
+ * The check is also done in __ethtool_get_link_ksettings; this helps
+ * returning early without hitting the locking section below.
+ */
+ if (!netdev->ethtool_ops->get_link_ksettings)
+ return -EINVAL;
+
+ ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
+ if (ret)
+ return ret;
+
+ ret = -EINVAL;
+ if (netif_running(netdev)) {
+ if (!__ethtool_get_link_ksettings(netdev, cmd))
+ ret = 0;
+ }
+
+ rtnl_unlock();
+ return ret;
+}
+
/* use same locking rules as GIF* ioctl's */
static ssize_t netdev_show(const struct device *dev,
struct device_attribute *attr, char *buf,
@@ -332,70 +360,41 @@ static DEVICE_ATTR_RW(carrier);
static ssize_t speed_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
- struct net_device *netdev = to_net_dev(dev);
- int ret = -EINVAL;
-
- /* The check is also done in __ethtool_get_link_ksettings; this helps
- * returning early without hitting the locking section below.
- */
- if (!netdev->ethtool_ops->get_link_ksettings)
- return ret;
+ struct ethtool_link_ksettings cmd;
+ int ret;
- ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
+ ret = sysfs_get_link_ksettings(dev, attr, &cmd);
if (ret)
return ret;
- ret = -EINVAL;
- if (netif_running(netdev)) {
- struct ethtool_link_ksettings cmd;
-
- if (!__ethtool_get_link_ksettings(netdev, &cmd))
- ret = sysfs_emit(buf, fmt_dec, cmd.base.speed);
- }
- rtnl_unlock();
- return ret;
+ return sysfs_emit(buf, fmt_dec, cmd.base.speed);
}
static DEVICE_ATTR_RO(speed);
static ssize_t duplex_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
- struct net_device *netdev = to_net_dev(dev);
- int ret = -EINVAL;
-
- /* The check is also done in __ethtool_get_link_ksettings; this helps
- * returning early without hitting the locking section below.
- */
- if (!netdev->ethtool_ops->get_link_ksettings)
- return ret;
+ struct ethtool_link_ksettings cmd;
+ const char *duplex;
+ int ret;
- ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
+ ret = sysfs_get_link_ksettings(dev, attr, &cmd);
if (ret)
return ret;
- ret = -EINVAL;
- if (netif_running(netdev)) {
- struct ethtool_link_ksettings cmd;
-
- if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
- const char *duplex;
-
- switch (cmd.base.duplex) {
- case DUPLEX_HALF:
- duplex = "half";
- break;
- case DUPLEX_FULL:
- duplex = "full";
- break;
- default:
- duplex = "unknown";
- break;
- }
- ret = sysfs_emit(buf, "%s\n", duplex);
- }
+ switch (cmd.base.duplex) {
+ case DUPLEX_HALF:
+ duplex = "half";
+ break;
+ case DUPLEX_FULL:
+ duplex = "full";
+ break;
+ default:
+ duplex = "unknown";
+ break;
}
- rtnl_unlock();
- return ret;
+
+ return sysfs_emit(buf, "%s\n", duplex);
}
static DEVICE_ATTR_RO(duplex);
--
2.47.3
next prev parent reply other threads:[~2026-09-03 13:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-03 13:33 [PATCH net-next v2 0/2] net: sysfs: use ops lock for speed and duplex Wang Zhan
2026-09-03 13:33 ` Wang Zhan [this message]
2026-09-03 13:33 ` [PATCH net-next v2 2/2] " Wang Zhan
2026-09-08 13:40 ` [PATCH net-next v2 0/2] " patchwork-bot+netdevbpf
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=20260903133314.3703381-2-wang.zhan@smartx.com \
--to=wang.zhan@smartx.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=keyong.sun@smartx.com \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/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.