* [PATCH net-next] net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)
@ 2025-05-15 13:02 Yajun Deng
2025-05-16 22:26 ` Jakub Kicinski
0 siblings, 1 reply; 3+ messages in thread
From: Yajun Deng @ 2025-05-15 13:02 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni, horms, andrew+netdev; +Cc: netdev, Yajun Deng
phys_port_id_show, phys_port_name_show and phys_switch_id_show would
return -EOPNOTSUPP if the netdev didn't implement the corresponding
method.
There is no point in creating these files if they are unsupported.
Put these attributes in netdev_phys_group and implement the is_visible
method. make phys_(port_id, port_name, switch_id) invisible if the netdev
dosen't implement the corresponding method.
Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
include/linux/netdevice.h | 2 +-
net/core/net-sysfs.c | 78 +++++++++++++++++++++++++--------------
2 files changed, 52 insertions(+), 28 deletions(-)
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 32a1e41636a9..efbcc4836498 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2384,7 +2384,7 @@ struct net_device {
struct dm_hw_stat_delta __rcu *dm_private;
#endif
struct device dev;
- const struct attribute_group *sysfs_groups[4];
+ const struct attribute_group *sysfs_groups[5];
const struct attribute_group *sysfs_rx_queue_group;
const struct rtnl_link_ops *rtnl_link_ops;
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 1ace0cd01adc..f176b7808abe 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -641,12 +641,6 @@ static ssize_t phys_port_id_show(struct device *dev,
struct netdev_phys_item_id ppid;
ssize_t ret;
- /* The check is also done in dev_get_phys_port_id; this helps returning
- * early without hitting the locking section below.
- */
- if (!netdev->netdev_ops->ndo_get_phys_port_id)
- return -EOPNOTSUPP;
-
ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
if (ret)
return ret;
@@ -659,7 +653,8 @@ static ssize_t phys_port_id_show(struct device *dev,
return ret;
}
-static DEVICE_ATTR_RO(phys_port_id);
+static struct device_attribute dev_attr_phys_port_id =
+ __ATTR(phys_port_id, 0444, phys_port_id_show, NULL);
static ssize_t phys_port_name_show(struct device *dev,
struct device_attribute *attr, char *buf)
@@ -668,13 +663,6 @@ static ssize_t phys_port_name_show(struct device *dev,
char name[IFNAMSIZ];
ssize_t ret;
- /* The checks are also done in dev_get_phys_port_name; this helps
- * returning early without hitting the locking section below.
- */
- if (!netdev->netdev_ops->ndo_get_phys_port_name &&
- !netdev->devlink_port)
- return -EOPNOTSUPP;
-
ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
if (ret)
return ret;
@@ -687,7 +675,8 @@ static ssize_t phys_port_name_show(struct device *dev,
return ret;
}
-static DEVICE_ATTR_RO(phys_port_name);
+static struct device_attribute dev_attr_phys_port_name =
+ __ATTR(phys_port_name, 0444, phys_port_name_show, NULL);
static ssize_t phys_switch_id_show(struct device *dev,
struct device_attribute *attr, char *buf)
@@ -696,14 +685,6 @@ static ssize_t phys_switch_id_show(struct device *dev,
struct netdev_phys_item_id ppid = { };
ssize_t ret;
- /* The checks are also done in dev_get_phys_port_name; this helps
- * returning early without hitting the locking section below. This works
- * because recurse is false when calling dev_get_port_parent_id.
- */
- if (!netdev->netdev_ops->ndo_get_port_parent_id &&
- !netdev->devlink_port)
- return -EOPNOTSUPP;
-
ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
if (ret)
return ret;
@@ -716,7 +697,52 @@ static ssize_t phys_switch_id_show(struct device *dev,
return ret;
}
-static DEVICE_ATTR_RO(phys_switch_id);
+static struct device_attribute dev_attr_phys_switch_id =
+ __ATTR(phys_switch_id, 0444, phys_switch_id_show, NULL);
+
+static struct attribute *netdev_phys_attrs[] __ro_after_init = {
+ &dev_attr_phys_port_id.attr,
+ &dev_attr_phys_port_name.attr,
+ &dev_attr_phys_switch_id.attr,
+ NULL,
+};
+
+static umode_t netdev_phys_is_visible(struct kobject *kobj,
+ struct attribute *attr, int index)
+{
+ struct device *dev = kobj_to_dev(kobj);
+ struct net_device *netdev = to_net_dev(dev);
+
+ if (attr == &dev_attr_phys_port_id.attr) {
+ /* The check is also done in dev_get_phys_port_id; this helps returning
+ * early without hitting the locking section below.
+ */
+ if (!netdev->netdev_ops->ndo_get_phys_port_id)
+ return 0;
+ } else if (attr == &dev_attr_phys_port_name.attr) {
+ /* The checks are also done in dev_get_phys_port_name; this helps
+ * returning early without hitting the locking section below.
+ */
+ if (!netdev->netdev_ops->ndo_get_phys_port_name &&
+ !netdev->devlink_port)
+ return 0;
+ } else if (attr == &dev_attr_phys_switch_id.attr) {
+ /* The checks are also done in dev_get_phys_port_name; this helps
+ * returning early without hitting the locking section below. This works
+ * because recurse is false when calling dev_get_port_parent_id.
+ */
+ if (!netdev->netdev_ops->ndo_get_port_parent_id &&
+ !netdev->devlink_port)
+ return 0;
+ }
+
+ return attr->mode;
+}
+
+static struct attribute_group netdev_phys_group = {
+ .attrs = netdev_phys_attrs,
+ .is_visible = netdev_phys_is_visible,
+};
static ssize_t threaded_show(struct device *dev,
struct device_attribute *attr, char *buf)
@@ -783,9 +809,6 @@ static struct attribute *net_class_attrs[] __ro_after_init = {
&dev_attr_tx_queue_len.attr,
&dev_attr_gro_flush_timeout.attr,
&dev_attr_napi_defer_hard_irqs.attr,
- &dev_attr_phys_port_id.attr,
- &dev_attr_phys_port_name.attr,
- &dev_attr_phys_switch_id.attr,
&dev_attr_proto_down.attr,
&dev_attr_carrier_up_count.attr,
&dev_attr_carrier_down_count.attr,
@@ -2328,6 +2351,7 @@ int netdev_register_kobject(struct net_device *ndev)
groups++;
*groups++ = &netstat_group;
+ *groups++ = &netdev_phys_group;
if (wireless_group_needed(ndev))
*groups++ = &wireless_group;
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net-next] net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)
2025-05-15 13:02 [PATCH net-next] net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id) Yajun Deng
@ 2025-05-16 22:26 ` Jakub Kicinski
2025-05-17 2:24 ` Yajun Deng
0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2025-05-16 22:26 UTC (permalink / raw)
To: Yajun Deng; +Cc: davem, edumazet, pabeni, horms, andrew+netdev, netdev
On Thu, 15 May 2025 21:02:05 +0800 Yajun Deng wrote:
> +static struct attribute *netdev_phys_attrs[] __ro_after_init = {
Why __ro_after_init and not const? I can't find the reason with
a quick grep. This is just an array of pointers, not objects.
> + &dev_attr_phys_port_id.attr,
> + &dev_attr_phys_port_name.attr,
> + &dev_attr_phys_switch_id.attr,
> + NULL,
> +};
> +
> +static umode_t netdev_phys_is_visible(struct kobject *kobj,
> + struct attribute *attr, int index)
> +{
> + struct device *dev = kobj_to_dev(kobj);
> + struct net_device *netdev = to_net_dev(dev);
> +
> + if (attr == &dev_attr_phys_port_id.attr) {
> + /* The check is also done in dev_get_phys_port_id; this helps returning
> + * early without hitting the locking section below.
> + */
> + if (!netdev->netdev_ops->ndo_get_phys_port_id)
> + return 0;
> + } else if (attr == &dev_attr_phys_port_name.attr) {
> + /* The checks are also done in dev_get_phys_port_name; this helps
> + * returning early without hitting the locking section below.
> + */
> + if (!netdev->netdev_ops->ndo_get_phys_port_name &&
> + !netdev->devlink_port)
> + return 0;
> + } else if (attr == &dev_attr_phys_switch_id.attr) {
> + /* The checks are also done in dev_get_phys_port_name; this helps
> + * returning early without hitting the locking section below. This works
> + * because recurse is false when calling dev_get_port_parent_id.
> + */
> + if (!netdev->netdev_ops->ndo_get_port_parent_id &&
> + !netdev->devlink_port)
> + return 0;
I'm slightly worried some user space depends on the files existing,
but maybe ENOENT vs EOPNOTSUPP doesn't make a big difference.
Can you remove the comments, tho? I don't think they add much value.
--
pw-bot: cr
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net-next] net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)
2025-05-16 22:26 ` Jakub Kicinski
@ 2025-05-17 2:24 ` Yajun Deng
0 siblings, 0 replies; 3+ messages in thread
From: Yajun Deng @ 2025-05-17 2:24 UTC (permalink / raw)
To: Jakub Kicinski; +Cc: davem, edumazet, pabeni, horms, andrew+netdev, netdev
May 17, 2025 at 6:26 AM, "Jakub Kicinski" <kuba@kernel.org> wrote:
>
> On Thu, 15 May 2025 21:02:05 +0800 Yajun Deng wrote:
>
> >
> > +static struct attribute *netdev_phys_attrs[] __ro_after_init = {
> >
>
> Why __ro_after_init and not const? I can't find the reason with
>
> a quick grep. This is just an array of pointers, not objects.
>
These attributes in net_class_attrs had __ro_after_init before this patch.
> >
> > + &dev_attr_phys_port_id.attr,
> >
> > + &dev_attr_phys_port_name.attr,
> >
> > + &dev_attr_phys_switch_id.attr,
> >
> > + NULL,
> >
> > +};
> >
> > +
> >
> > +static umode_t netdev_phys_is_visible(struct kobject *kobj,
> >
> > + struct attribute *attr, int index)
> >
> > +{
> >
> > + struct device *dev = kobj_to_dev(kobj);
> >
> > + struct net_device *netdev = to_net_dev(dev);
> >
> > +
> >
> > + if (attr == &dev_attr_phys_port_id.attr) {
> >
> > + /* The check is also done in dev_get_phys_port_id; this helps returning
> >
> > + * early without hitting the locking section below.
> >
> > + */
> >
> > + if (!netdev->netdev_ops->ndo_get_phys_port_id)
> >
> > + return 0;
> >
> > + } else if (attr == &dev_attr_phys_port_name.attr) {
> >
> > + /* The checks are also done in dev_get_phys_port_name; this helps
> >
> > + * returning early without hitting the locking section below.
> >
> > + */
> >
> > + if (!netdev->netdev_ops->ndo_get_phys_port_name &&
> >
> > + !netdev->devlink_port)
> >
> > + return 0;
> >
> > + } else if (attr == &dev_attr_phys_switch_id.attr) {
> >
> > + /* The checks are also done in dev_get_phys_port_name; this helps
> >
> > + * returning early without hitting the locking section below. This works
> >
> > + * because recurse is false when calling dev_get_port_parent_id.
> >
> > + */
> >
> > + if (!netdev->netdev_ops->ndo_get_port_parent_id &&
> >
> > + !netdev->devlink_port)
> >
> > + return 0;
> >
>
> I'm slightly worried some user space depends on the files existing,
>
> but maybe ENOENT vs EOPNOTSUPP doesn't make a big difference.
>
> Can you remove the comments, tho? I don't think they add much value.
>
Okay.
> --
>
> pw-bot: cr
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-05-17 2:24 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-15 13:02 [PATCH net-next] net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id) Yajun Deng
2025-05-16 22:26 ` Jakub Kicinski
2025-05-17 2:24 ` Yajun Deng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).