[PATCH v1] driver core: Fix device link device name collision

From: Saravana Kannan
Date: Wed Jan 06 2021 - 18:27:35 EST


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@xxxxxxxx/
Reported-by: Michael Walle <michael@xxxxxxxx>
Signed-off-by: Saravana Kannan <saravanak@xxxxxxxxxx>
---

Michael,

Can you please test this? This should fix your issue.

Having said that, do you have some local DT changes when you are testing
this? Because it's not obvious from the DT in upstream what dependency
is even being derived from the firmware. I don't see any dependency in
upstream DT files between mdio_bus/0000:00:00.1 and
pci0000:00/0000:00:00.1

Thanks,
Saravana

Documentation/ABI/testing/sysfs-class-devlink | 4 ++--
drivers/base/core.c | 9 ++++-----
include/linux/device.h | 12 ++++++++++++
3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-class-devlink b/Documentation/ABI/testing/sysfs-class-devlink
index b662f747c83e..8a21ce515f61 100644
--- a/Documentation/ABI/testing/sysfs-class-devlink
+++ b/Documentation/ABI/testing/sysfs-class-devlink
@@ -5,8 +5,8 @@ Description:
Provide a place in sysfs for the device link objects in the
kernel at any given time. The name of a device link directory,
denoted as ... above, is of the form <supplier>--<consumer>
- where <supplier> is the supplier device name and <consumer> is
- the consumer device name.
+ where <supplier> is the supplier bus:device name and <consumer>
+ is the consumer bus:device name.

What: /sys/class/devlink/.../auto_remove_on
Date: May 2020
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 25e08e5f40bd..e54c51926250 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -737,8 +737,9 @@ struct device_link *device_link_add(struct device *consumer,

link->link_dev.class = &devlink_class;
device_set_pm_not_required(&link->link_dev);
- dev_set_name(&link->link_dev, "%s--%s",
- dev_name(supplier), dev_name(consumer));
+ dev_set_name(&link->link_dev, "%s:%s--%s:%s",
+ dev_bus_name(supplier), dev_name(supplier),
+ dev_bus_name(consumer), dev_name(consumer));
if (device_register(&link->link_dev)) {
put_device(consumer);
put_device(supplier);
@@ -1808,9 +1809,7 @@ const char *dev_driver_string(const struct device *dev)
* never change once they are set, so they don't need special care.
*/
drv = READ_ONCE(dev->driver);
- return drv ? drv->name :
- (dev->bus ? dev->bus->name :
- (dev->class ? dev->class->name : ""));
+ return drv ? drv->name : dev_bus_name(dev);
}
EXPORT_SYMBOL(dev_driver_string);

diff --git a/include/linux/device.h b/include/linux/device.h
index 89bb8b84173e..1779f90eeb4c 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -609,6 +609,18 @@ static inline const char *dev_name(const struct device *dev)
return kobject_name(&dev->kobj);
}

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

#ifdef CONFIG_NUMA
--
2.29.2.729.g45daf8777d-goog