Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

leds: trigger: netdev: Display only supported link speed attribute

With the addition of more link speed mode to the netdev trigger, it was
pointed out that there may be a problem with bloating the attribute list
with modes that won't ever be supported by the trigger as the attached
device name doesn't support them.

To clear and address this problem, change the logic where these
additional trigger modes are listed.

Since the netdev trigger REQUIRE a device name to be set, attach to the
device name change function additional logic to parse the supported link
speed modes using ethtool APIs and show only the supported link speed
modes attribute.

Link speed attribute are refreshed on device_name set and on
NETDEV_CHANGE events.

This only apply to the link speed modes and every other mode is still
provided by default.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Reviewed-by: Marek Behún <kabel@kernel.org>
Link: https://lore.kernel.org/r/20240111160501.1774-1-ansuelsmth@gmail.com
Signed-off-by: Lee Jones <lee@kernel.org>

authored by

Christian Marangi and committed by
Lee Jones
06cdca01 66601a29

+84 -6
+84 -6
drivers/leds/trigger/ledtrig-netdev.c
··· 18 18 #include <linux/jiffies.h> 19 19 #include <linux/kernel.h> 20 20 #include <linux/leds.h> 21 + #include <linux/linkmode.h> 21 22 #include <linux/list.h> 22 23 #include <linux/module.h> 23 24 #include <linux/netdevice.h> 24 25 #include <linux/mutex.h> 26 + #include <linux/phy.h> 25 27 #include <linux/rtnetlink.h> 26 28 #include <linux/timer.h> 27 29 #include "../leds.h" ··· 67 65 68 66 unsigned long mode; 69 67 int link_speed; 68 + __ETHTOOL_DECLARE_LINK_MODE_MASK(supported_link_modes); 70 69 u8 duplex; 71 70 72 71 bool carrier_link_up; 73 72 bool hw_control; 74 73 }; 74 + 75 + static const struct attribute_group netdev_trig_link_speed_attrs_group; 75 76 76 77 static void set_baseline_state(struct led_netdev_data *trigger_data) 77 78 { ··· 223 218 struct ethtool_link_ksettings cmd; 224 219 225 220 trigger_data->carrier_link_up = netif_carrier_ok(trigger_data->net_dev); 226 - if (!trigger_data->carrier_link_up) 221 + 222 + if (__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd)) 227 223 return; 228 224 229 - if (!__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd)) { 225 + if (trigger_data->carrier_link_up) { 230 226 trigger_data->link_speed = cmd.base.speed; 231 227 trigger_data->duplex = cmd.base.duplex; 232 228 } 229 + 230 + /* 231 + * Have a local copy of the link speed supported to avoid rtnl lock every time 232 + * modes are refreshed on any change event 233 + */ 234 + linkmode_copy(trigger_data->supported_link_modes, cmd.link_modes.supported); 233 235 } 234 236 235 237 static ssize_t device_name_show(struct device *dev, ··· 310 298 311 299 if (ret < 0) 312 300 return ret; 301 + 302 + /* Refresh link_speed visibility */ 303 + sysfs_update_group(&dev->kobj, &netdev_trig_link_speed_attrs_group); 304 + 313 305 return size; 314 306 } 315 307 ··· 477 461 478 462 static DEVICE_ATTR_RO(offloaded); 479 463 480 - static struct attribute *netdev_trig_attrs[] = { 481 - &dev_attr_device_name.attr, 482 - &dev_attr_link.attr, 464 + #define CHECK_LINK_MODE_ATTR(link_speed) \ 465 + do { \ 466 + if (attr == &dev_attr_link_##link_speed.attr && \ 467 + link_ksettings.base.speed == SPEED_##link_speed) \ 468 + return attr->mode; \ 469 + } while (0) 470 + 471 + static umode_t netdev_trig_link_speed_visible(struct kobject *kobj, 472 + struct attribute *attr, int n) 473 + { 474 + struct device *dev = kobj_to_dev(kobj); 475 + struct led_netdev_data *trigger_data; 476 + unsigned long *supported_link_modes; 477 + u32 mode; 478 + 479 + trigger_data = led_trigger_get_drvdata(dev); 480 + supported_link_modes = trigger_data->supported_link_modes; 481 + 482 + /* 483 + * Search in the supported link mode mask a matching supported mode. 484 + * Stop at the first matching entry as we care only to check if a particular 485 + * speed is supported and not the kind. 486 + */ 487 + for_each_set_bit(mode, supported_link_modes, __ETHTOOL_LINK_MODE_MASK_NBITS) { 488 + struct ethtool_link_ksettings link_ksettings; 489 + 490 + ethtool_params_from_link_mode(&link_ksettings, mode); 491 + 492 + CHECK_LINK_MODE_ATTR(10); 493 + CHECK_LINK_MODE_ATTR(100); 494 + CHECK_LINK_MODE_ATTR(1000); 495 + CHECK_LINK_MODE_ATTR(2500); 496 + CHECK_LINK_MODE_ATTR(5000); 497 + CHECK_LINK_MODE_ATTR(10000); 498 + } 499 + 500 + return 0; 501 + } 502 + 503 + static struct attribute *netdev_trig_link_speed_attrs[] = { 483 504 &dev_attr_link_10.attr, 484 505 &dev_attr_link_100.attr, 485 506 &dev_attr_link_1000.attr, 486 507 &dev_attr_link_2500.attr, 487 508 &dev_attr_link_5000.attr, 488 509 &dev_attr_link_10000.attr, 510 + NULL 511 + }; 512 + 513 + static const struct attribute_group netdev_trig_link_speed_attrs_group = { 514 + .attrs = netdev_trig_link_speed_attrs, 515 + .is_visible = netdev_trig_link_speed_visible, 516 + }; 517 + 518 + static struct attribute *netdev_trig_attrs[] = { 519 + &dev_attr_device_name.attr, 520 + &dev_attr_link.attr, 489 521 &dev_attr_full_duplex.attr, 490 522 &dev_attr_half_duplex.attr, 491 523 &dev_attr_rx.attr, ··· 542 478 &dev_attr_offloaded.attr, 543 479 NULL 544 480 }; 545 - ATTRIBUTE_GROUPS(netdev_trig); 481 + 482 + static const struct attribute_group netdev_trig_attrs_group = { 483 + .attrs = netdev_trig_attrs, 484 + }; 485 + 486 + static const struct attribute_group *netdev_trig_groups[] = { 487 + &netdev_trig_attrs_group, 488 + &netdev_trig_link_speed_attrs_group, 489 + NULL, 490 + }; 546 491 547 492 static int netdev_trig_notify(struct notifier_block *nb, 548 493 unsigned long evt, void *dv) ··· 560 487 netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv); 561 488 struct led_netdev_data *trigger_data = 562 489 container_of(nb, struct led_netdev_data, notifier); 490 + struct led_classdev *led_cdev = trigger_data->led_cdev; 563 491 564 492 if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE 565 493 && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER ··· 595 521 case NETDEV_UP: 596 522 case NETDEV_CHANGE: 597 523 get_device_state(trigger_data); 524 + /* Refresh link_speed visibility */ 525 + if (evt == NETDEV_CHANGE) 526 + sysfs_update_group(&led_cdev->dev->kobj, 527 + &netdev_trig_link_speed_attrs_group); 598 528 break; 599 529 } 600 530