Skip to content

Commit b3bc56e

Browse files
Saravana Kannangregkh
authored andcommitted
driver core: Fix device link device name collision
commit e020ff6 upstream. The device link device's name was of the form: <supplier-dev-name>--<consumer-dev-name> This can cause name collision as reported here [1] as device names are not globally unique. Since device names have to be unique within the bus/class, add the bus/class name as a prefix to the device names used to construct the device link device name. So the devuce link device's name will be of the form: <supplier-bus-name>:<supplier-dev-name>--<consumer-bus-name>:<consumer-dev-name> [1] - https://lore.kernel.org/lkml/20201229033440.32142-1-michael@walle.cc/ Fixes: 287905e ("driver core: Expose device link details in sysfs") Cc: stable@vger.kernel.org Reported-by: Michael Walle <michael@walle.cc> Tested-by: Michael Walle <michael@walle.cc> Signed-off-by: Saravana Kannan <saravanak@google.com> Link: https://lore.kernel.org/r/20210110175408.1465657-1-saravanak@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8cd3c48 commit b3bc56e

5 files changed

Lines changed: 35 additions & 18 deletions

File tree

‎Documentation/ABI/testing/sysfs-class-devlink‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Description:
55
Provide a place in sysfs for the device link objects in the
66
kernel at any given time. The name of a device link directory,
77
denoted as ... above, is of the form <supplier>--<consumer>
8-
where <supplier> is the supplier device name and <consumer> is
9-
the consumer device name.
8+
where <supplier> is the supplier bus:device name and <consumer>
9+
is the consumer bus:device name.
1010

1111
What: /sys/class/devlink/.../auto_remove_on
1212
Date: May 2020

‎Documentation/ABI/testing/sysfs-devices-consumer‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Contact: Saravana Kannan <saravanak@google.com>
44
Description:
55
The /sys/devices/.../consumer:<consumer> are symlinks to device
66
links where this device is the supplier. <consumer> denotes the
7-
name of the consumer in that device link. There can be zero or
8-
more of these symlinks for a given device.
7+
name of the consumer in that device link and is of the form
8+
bus:device name. There can be zero or more of these symlinks
9+
for a given device.

‎Documentation/ABI/testing/sysfs-devices-supplier‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Contact: Saravana Kannan <saravanak@google.com>
44
Description:
55
The /sys/devices/.../supplier:<supplier> are symlinks to device
66
links where this device is the consumer. <supplier> denotes the
7-
name of the supplier in that device link. There can be zero or
8-
more of these symlinks for a given device.
7+
name of the supplier in that device link and is of the form
8+
bus:device name. There can be zero or more of these symlinks
9+
for a given device.

‎drivers/base/core.c‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ static int devlink_add_symlinks(struct device *dev,
363363
struct device *con = link->consumer;
364364
char *buf;
365365

366-
len = max(strlen(dev_name(sup)), strlen(dev_name(con)));
366+
len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
367+
strlen(dev_bus_name(con)) + strlen(dev_name(con)));
368+
len += strlen(":");
367369
len += strlen("supplier:") + 1;
368370
buf = kzalloc(len, GFP_KERNEL);
369371
if (!buf)
@@ -377,20 +379,20 @@ static int devlink_add_symlinks(struct device *dev,
377379
if (ret)
378380
goto err_con;
379381

380-
snprintf(buf, len, "consumer:%s", dev_name(con));
382+
snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
381383
ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
382384
if (ret)
383385
goto err_con_dev;
384386

385-
snprintf(buf, len, "supplier:%s", dev_name(sup));
387+
snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
386388
ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
387389
if (ret)
388390
goto err_sup_dev;
389391

390392
goto out;
391393

392394
err_sup_dev:
393-
snprintf(buf, len, "consumer:%s", dev_name(con));
395+
snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
394396
sysfs_remove_link(&sup->kobj, buf);
395397
err_con_dev:
396398
sysfs_remove_link(&link->link_dev.kobj, "consumer");
@@ -413,17 +415,19 @@ static void devlink_remove_symlinks(struct device *dev,
413415
sysfs_remove_link(&link->link_dev.kobj, "consumer");
414416
sysfs_remove_link(&link->link_dev.kobj, "supplier");
415417

416-
len = max(strlen(dev_name(sup)), strlen(dev_name(con)));
418+
len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
419+
strlen(dev_bus_name(con)) + strlen(dev_name(con)));
420+
len += strlen(":");
417421
len += strlen("supplier:") + 1;
418422
buf = kzalloc(len, GFP_KERNEL);
419423
if (!buf) {
420424
WARN(1, "Unable to properly free device link symlinks!\n");
421425
return;
422426
}
423427

424-
snprintf(buf, len, "supplier:%s", dev_name(sup));
428+
snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
425429
sysfs_remove_link(&con->kobj, buf);
426-
snprintf(buf, len, "consumer:%s", dev_name(con));
430+
snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
427431
sysfs_remove_link(&sup->kobj, buf);
428432
kfree(buf);
429433
}
@@ -633,8 +637,9 @@ struct device_link *device_link_add(struct device *consumer,
633637

634638
link->link_dev.class = &devlink_class;
635639
device_set_pm_not_required(&link->link_dev);
636-
dev_set_name(&link->link_dev, "%s--%s",
637-
dev_name(supplier), dev_name(consumer));
640+
dev_set_name(&link->link_dev, "%s:%s--%s:%s",
641+
dev_bus_name(supplier), dev_name(supplier),
642+
dev_bus_name(consumer), dev_name(consumer));
638643
if (device_register(&link->link_dev)) {
639644
put_device(consumer);
640645
put_device(supplier);
@@ -1652,9 +1657,7 @@ const char *dev_driver_string(const struct device *dev)
16521657
* never change once they are set, so they don't need special care.
16531658
*/
16541659
drv = READ_ONCE(dev->driver);
1655-
return drv ? drv->name :
1656-
(dev->bus ? dev->bus->name :
1657-
(dev->class ? dev->class->name : ""));
1660+
return drv ? drv->name : dev_bus_name(dev);
16581661
}
16591662
EXPORT_SYMBOL(dev_driver_string);
16601663

‎include/linux/device.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,18 @@ static inline const char *dev_name(const struct device *dev)
615615
return kobject_name(&dev->kobj);
616616
}
617617

618+
/**
619+
* dev_bus_name - Return a device's bus/class name, if at all possible
620+
* @dev: struct device to get the bus/class name of
621+
*
622+
* Will return the name of the bus/class the device is attached to. If it is
623+
* not attached to a bus/class, an empty string will be returned.
624+
*/
625+
static inline const char *dev_bus_name(const struct device *dev)
626+
{
627+
return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
628+
}
629+
618630
__printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
619631

620632
#ifdef CONFIG_NUMA

0 commit comments

Comments
 (0)