Skip to content

Commit 0c17270

Browse files
Yajun Dengkuba-moo
authored andcommitted
net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)
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>
1 parent ffe8a49 commit 0c17270

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

include/linux/netdevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2388,7 +2388,7 @@ struct net_device {
23882388
struct dm_hw_stat_delta __rcu *dm_private;
23892389
#endif
23902390
struct device dev;
2391-
const struct attribute_group *sysfs_groups[4];
2391+
const struct attribute_group *sysfs_groups[5];
23922392
const struct attribute_group *sysfs_rx_queue_group;
23932393

23942394
const struct rtnl_link_ops *rtnl_link_ops;

net/core/net-sysfs.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,6 @@ static ssize_t phys_port_id_show(struct device *dev,
641641
struct netdev_phys_item_id ppid;
642642
ssize_t ret;
643643

644-
/* The check is also done in dev_get_phys_port_id; this helps returning
645-
* early without hitting the locking section below.
646-
*/
647-
if (!netdev->netdev_ops->ndo_get_phys_port_id)
648-
return -EOPNOTSUPP;
649-
650644
ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
651645
if (ret)
652646
return ret;
@@ -668,13 +662,6 @@ static ssize_t phys_port_name_show(struct device *dev,
668662
char name[IFNAMSIZ];
669663
ssize_t ret;
670664

671-
/* The checks are also done in dev_get_phys_port_name; this helps
672-
* returning early without hitting the locking section below.
673-
*/
674-
if (!netdev->netdev_ops->ndo_get_phys_port_name &&
675-
!netdev->devlink_port)
676-
return -EOPNOTSUPP;
677-
678665
ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
679666
if (ret)
680667
return ret;
@@ -696,14 +683,6 @@ static ssize_t phys_switch_id_show(struct device *dev,
696683
struct netdev_phys_item_id ppid = { };
697684
ssize_t ret;
698685

699-
/* The checks are also done in dev_get_phys_port_name; this helps
700-
* returning early without hitting the locking section below. This works
701-
* because recurse is false when calling dev_get_port_parent_id.
702-
*/
703-
if (!netdev->netdev_ops->ndo_get_port_parent_id &&
704-
!netdev->devlink_port)
705-
return -EOPNOTSUPP;
706-
707686
ret = sysfs_rtnl_lock(&dev->kobj, &attr->attr, netdev);
708687
if (ret)
709688
return ret;
@@ -718,6 +697,40 @@ static ssize_t phys_switch_id_show(struct device *dev,
718697
}
719698
static DEVICE_ATTR_RO(phys_switch_id);
720699

700+
static struct attribute *netdev_phys_attrs[] __ro_after_init = {
701+
&dev_attr_phys_port_id.attr,
702+
&dev_attr_phys_port_name.attr,
703+
&dev_attr_phys_switch_id.attr,
704+
NULL,
705+
};
706+
707+
static umode_t netdev_phys_is_visible(struct kobject *kobj,
708+
struct attribute *attr, int index)
709+
{
710+
struct device *dev = kobj_to_dev(kobj);
711+
struct net_device *netdev = to_net_dev(dev);
712+
713+
if (attr == &dev_attr_phys_port_id.attr) {
714+
if (!netdev->netdev_ops->ndo_get_phys_port_id)
715+
return 0;
716+
} else if (attr == &dev_attr_phys_port_name.attr) {
717+
if (!netdev->netdev_ops->ndo_get_phys_port_name &&
718+
!netdev->devlink_port)
719+
return 0;
720+
} else if (attr == &dev_attr_phys_switch_id.attr) {
721+
if (!netdev->netdev_ops->ndo_get_port_parent_id &&
722+
!netdev->devlink_port)
723+
return 0;
724+
}
725+
726+
return attr->mode;
727+
}
728+
729+
static const struct attribute_group netdev_phys_group = {
730+
.attrs = netdev_phys_attrs,
731+
.is_visible = netdev_phys_is_visible,
732+
};
733+
721734
static ssize_t threaded_show(struct device *dev,
722735
struct device_attribute *attr, char *buf)
723736
{
@@ -783,9 +796,6 @@ static struct attribute *net_class_attrs[] __ro_after_init = {
783796
&dev_attr_tx_queue_len.attr,
784797
&dev_attr_gro_flush_timeout.attr,
785798
&dev_attr_napi_defer_hard_irqs.attr,
786-
&dev_attr_phys_port_id.attr,
787-
&dev_attr_phys_port_name.attr,
788-
&dev_attr_phys_switch_id.attr,
789799
&dev_attr_proto_down.attr,
790800
&dev_attr_carrier_up_count.attr,
791801
&dev_attr_carrier_down_count.attr,
@@ -2328,6 +2338,7 @@ int netdev_register_kobject(struct net_device *ndev)
23282338
groups++;
23292339

23302340
*groups++ = &netstat_group;
2341+
*groups++ = &netdev_phys_group;
23312342

23322343
if (wireless_group_needed(ndev))
23332344
*groups++ = &wireless_group;

0 commit comments

Comments
 (0)