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

driver core: add helper to reuse a device-tree node

Add a helper function to be used when reusing the device-tree node of
another device.

It is fairly common for drivers to reuse the device-tree node of a
parent (or other ancestor) device when creating class or bus devices
(e.g. gpio chips, i2c adapters, iio chips, spi masters, serdev, phys,
usb root hubs). But reusing a device-tree node may cause problems if the
new device is later probed as for example driver core would currently
attempt to reinitialise an already active associated pinmux
configuration.

Other potential issues include the platform-bus code unconditionally
dropping the device-tree node reference in its device destructor,
reinitialisation of other bus-managed resources such as clocks, and the
recently added DMA-setup in driver core.

Note that for most examples above this is currently not an issue as the
devices are never probed, but this is a problem for the USB bus which
has recently gained device-tree support. This was discovered and
worked-around in a rather ad-hoc fashion by commit dc5878abf49c ("usb:
core: move root hub's device node assignment after it is added to bus")
by not setting the of_node pointer until after the root-hub device has
been registered.

Instead we can allow devices to reuse a device-tree node by setting a
flag in their struct device that can be used by core, bus and driver
code to avoid resources from being over-allocated.

Note that the helper also grabs an extra reference to the device node,
which specifically balances the unconditional put in the platform-device
destructor.

Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Johan Hovold and committed by
Greg Kroah-Hartman
4e75e1d7 60a93cff

+20
+16
drivers/base/core.c
··· 2884 2884 else 2885 2885 dev->fwnode = fwnode; 2886 2886 } 2887 + 2888 + /** 2889 + * device_set_of_node_from_dev - reuse device-tree node of another device 2890 + * @dev: device whose device-tree node is being set 2891 + * @dev2: device whose device-tree node is being reused 2892 + * 2893 + * Takes another reference to the new device-tree node after first dropping 2894 + * any reference held to the old node. 2895 + */ 2896 + void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) 2897 + { 2898 + of_node_put(dev->of_node); 2899 + dev->of_node = of_node_get(dev2->of_node); 2900 + dev->of_node_reused = true; 2901 + } 2902 + EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
+4
include/linux/device.h
··· 879 879 * 880 880 * @offline_disabled: If set, the device is permanently online. 881 881 * @offline: Set after successful invocation of bus type's .offline(). 882 + * @of_node_reused: Set if the device-tree node is shared with an ancestor 883 + * device. 882 884 * 883 885 * At the lowest level, every device in a Linux system is represented by an 884 886 * instance of struct device. The device structure contains the information ··· 968 966 969 967 bool offline_disabled:1; 970 968 bool offline:1; 969 + bool of_node_reused:1; 971 970 }; 972 971 973 972 static inline struct device *kobj_to_dev(struct kobject *kobj) ··· 1147 1144 extern int device_online(struct device *dev); 1148 1145 extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 1149 1146 extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 1147 + void device_set_of_node_from_dev(struct device *dev, const struct device *dev2); 1150 1148 1151 1149 static inline int dev_num_vf(struct device *dev) 1152 1150 {