]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)
authorYajun Deng <yajun.deng@linux.dev>
Thu, 12 Jun 2025 14:27:07 +0000 (14:27 +0000)
committerJakub Kicinski <kuba@kernel.org>
Sat, 14 Jun 2025 18:26:59 +0000 (11:26 -0700)
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>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/20250612142707.4644-1-yajun.deng@linux.dev
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/linux/netdevice.h
net/core/net-sysfs.c

index adb14db257984e4b704a514e4be674e82b72c1d4..9cbc4e54b7e4a8a431f68032a9eebc82393f59c1 100644 (file)
@@ -2388,7 +2388,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;
index 1ace0cd01adce7efefb6bfe4a9a763a33dff05ba..c9b96938639999ddc1fe52560e0ba2c4f1adff1f 100644 (file)
@@ -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;
@@ -668,13 +662,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;
@@ -696,14 +683,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;
@@ -718,6 +697,40 @@ static ssize_t phys_switch_id_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(phys_switch_id);
 
+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) {
+               if (!netdev->netdev_ops->ndo_get_phys_port_id)
+                       return 0;
+       } else if (attr == &dev_attr_phys_port_name.attr) {
+               if (!netdev->netdev_ops->ndo_get_phys_port_name &&
+                   !netdev->devlink_port)
+                       return 0;
+       } else if (attr == &dev_attr_phys_switch_id.attr) {
+               if (!netdev->netdev_ops->ndo_get_port_parent_id &&
+                   !netdev->devlink_port)
+                       return 0;
+       }
+
+       return attr->mode;
+}
+
+static const 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 +796,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 +2338,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;