net: sysfs: Implement is_visible for phys_(port_id, port_name, switch_id)
Subsystems (2)
PATCH 1/1 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.
--- include/linux/netdevice.h | 2 +- net/core/net-sysfs.c | 78 +++++++++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 28 deletions(-)
include/linux/netdevice.h +1 -1
@@ -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;
net/core/net-sysfs.c +51 -27
126 lines not on this page · raw message
Signature · 1 line
2.25.1
Jakub Kicinski
maintainerQuoted 1 line · 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.
Quoted 23 lines
net/core/net-sysfs.c +23
-0+ &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 &&
net/core/net-sysfs.c static ssize_t phys_switch_id_show(struct device *dev, · line 736
Quoted 10 lines
net/core/net-sysfs.c +10
-0+ !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.
Signature · 1 line
pw-bot: cr
Quoted 11 lines · Jakub Kicinski
Quoted 3 lines · On Thu, 15 May 2025 21:02:05 +0800 Yajun Deng wrote:
+static struct attribute *netdev_phys_attrs[]
__ro_after_init= {Why
__ro_after_initand not const? I can't find the reason witha quick grep. This is just an array of pointers, not objects.
These attributes in net_class_attrs had __ro_after_init before this patch.
Quoted 65 lines
Quoted 65 lines
+ &dev_attr_phys_port_id.attr,
+ &dev_attr_phys_port_name.attr,
+ &dev_attr_phys_switch_id.attr,
+ NULL,
+};
+
+static
umode_tnetdev_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)
Quoted 9 lines
Quoted 2 lines
+ 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.
Quoted 4 lines
Signature · 1 line
pw-bot: cr