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

of: Ensure unique names without sacrificing determinism

The way the driver core is implemented, every device using the same bus
type is required to have a unique name because a symlink to each device
is created in the appropriate /sys/bus/*/devices directory, and two
identical names causes a collision.

The current code handles the requirement by using an globally
incremented counter that is appended to the device name. It works, but
it means any change to device registration will change the assigned
numbers. Instead, if we build up the name by using information from the
parent nodes, then it can be guaranteed to be unique without adding a
random number to the end of it.

Signed-off-by: Grant Likely <grant.likely@linaro.org>
Cc: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
Cc: Rob Herring <robh@kernel.org>

+19 -21
+19 -21
drivers/of/platform.c
··· 68 68 * of_device_make_bus_id - Use the device node data to assign a unique name 69 69 * @dev: pointer to device structure that is linked to a device tree node 70 70 * 71 - * This routine will first try using either the dcr-reg or the reg property 72 - * value to derive a unique name. As a last resort it will use the node 73 - * name followed by a unique number. 71 + * This routine will first try using the translated bus address to 72 + * derive a unique name. If it cannot, then it will prepend names from 73 + * parent nodes until a unique name can be derived. 74 74 */ 75 75 void of_device_make_bus_id(struct device *dev) 76 76 { 77 - static atomic_t bus_no_reg_magic; 78 77 struct device_node *node = dev->of_node; 79 78 const __be32 *reg; 80 79 u64 addr; 81 - int magic; 82 80 83 81 #ifdef CONFIG_PPC_DCR 84 82 /* ··· 98 100 } 99 101 #endif /* CONFIG_PPC_DCR */ 100 102 101 - /* 102 - * For MMIO, get the physical address 103 - */ 104 - reg = of_get_property(node, "reg", NULL); 105 - if (reg) { 106 - addr = of_translate_address(node, reg); 107 - if (addr != OF_BAD_ADDR) { 108 - dev_set_name(dev, "%llx.%s", 109 - (unsigned long long)addr, node->name); 103 + /* Construct the name, using parent nodes if necessary to ensure uniqueness */ 104 + while (node->parent) { 105 + /* 106 + * If the address can be translated, then that is as much 107 + * uniqueness as we need. Make it the first component and return 108 + */ 109 + reg = of_get_property(node, "reg", NULL); 110 + if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) { 111 + dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s", 112 + (unsigned long long)addr, node->name, 113 + dev_name(dev)); 110 114 return; 111 115 } 112 - } 113 116 114 - /* 115 - * No BusID, use the node name and add a globally incremented 116 - * counter (and pray...) 117 - */ 118 - magic = atomic_add_return(1, &bus_no_reg_magic); 119 - dev_set_name(dev, "%s.%d", node->name, magic - 1); 117 + /* format arguments only used if dev_name() resolves to NULL */ 118 + dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", 119 + strrchr(node->full_name, '/') + 1, dev_name(dev)); 120 + node = node->parent; 121 + } 120 122 } 121 123 122 124 /**