From 515db266a9dace92b0cbaed9a6044dd5304b8ca9 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 16 Jul 2019 17:21:06 +0200 Subject: driver core: Remove device link creation limitation If device_link_add() is called for a consumer/supplier pair with an existing device link between them and the existing link's type is not in agreement with the flags passed to that function by its caller, NULL will be returned. That is seriously inconvenient, because it forces the callers of device_link_add() to worry about what others may or may not do even if that is not relevant to them for any other reasons. It turns out, however, that this limitation can be made go away relatively easily. The underlying observation is that if DL_FLAG_STATELESS has been passed to device_link_add() in flags for the given consumer/supplier pair at least once, calling either device_link_del() or device_link_remove() to release the link returned by it should work, but there are no other requirements associated with that flag. In turn, if at least one of the callers of device_link_add() for the given consumer/supplier pair has not passed DL_FLAG_STATELESS to it in flags, the driver core should track the status of the link and act on it as appropriate (ie. the link should be treated as "managed"). This means that DL_FLAG_STATELESS needs to be set for managed device links and it should be valid to call device_link_del() or device_link_remove() to drop references to them in certain sutiations. To allow that to happen, introduce a new (internal) device link flag called DL_FLAG_MANAGED and make device_link_add() set it automatically whenever DL_FLAG_STATELESS is not passed to it. Also make it take additional references to existing device links that were previously stateless (that is, with DL_FLAG_STATELESS set and DL_FLAG_MANAGED unset) and will need to be managed going forward and initialize their status (which has been DL_STATE_NONE so far). Accordingly, when a managed device link is dropped automatically by the driver core, make it clear DL_FLAG_MANAGED, reset the link's status back to DL_STATE_NONE and drop the reference to it associated with DL_FLAG_MANAGED instead of just deleting it right away (to allow it to stay around in case it still needs to be released explicitly by someone). With that, since setting DL_FLAG_STATELESS doesn't mean that the device link in question is not managed any more, replace all of the status-tracking checks against DL_FLAG_STATELESS with analogous checks against DL_FLAG_MANAGED and update the documentation to reflect these changes. While at it, make device_link_add() reject flags that it does not recognize, including DL_FLAG_MANAGED. Signed-off-by: Rafael J. Wysocki Reviewed-by: Saravana Kannan Tested-by: Marek Szyprowski Review-by: Saravana Kannan Link: https://lore.kernel.org/r/2305283.AStDPdUUnE@kreacher Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/linux/device.h b/include/linux/device.h index c330b75c6c57..c1c489921e4c 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -833,12 +833,13 @@ enum device_link_state { /* * Device link flags. * - * STATELESS: The core won't track the presence of supplier/consumer drivers. + * STATELESS: The core will not remove this link automatically. * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind. * PM_RUNTIME: If set, the runtime PM framework will use this link. * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation. * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind. * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds. + * MANAGED: The core tracks presence of supplier/consumer drivers (internal). */ #define DL_FLAG_STATELESS BIT(0) #define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1) @@ -846,6 +847,7 @@ enum device_link_state { #define DL_FLAG_RPM_ACTIVE BIT(3) #define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4) #define DL_FLAG_AUTOPROBE_CONSUMER BIT(5) +#define DL_FLAG_MANAGED BIT(6) /** * struct device_link - Device link representation. -- cgit v1.2.3 From dc607f6bbafd2d8bf1d518c346560ba9577e402f Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Sat, 6 Jul 2019 18:47:22 +0200 Subject: mfd: aat2870: no need to check return value of debugfs_create functions When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this. Cc: Lee Jones Link: https://lore.kernel.org/r/20190706164722.18766-3-gregkh@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman --- drivers/mfd/aat2870-core.c | 13 ++----------- include/linux/mfd/aat2870.h | 1 - 2 files changed, 2 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/drivers/mfd/aat2870-core.c b/drivers/mfd/aat2870-core.c index 9f58cb2d3789..78ee4b28fca2 100644 --- a/drivers/mfd/aat2870-core.c +++ b/drivers/mfd/aat2870-core.c @@ -321,18 +321,9 @@ static const struct file_operations aat2870_reg_fops = { static void aat2870_init_debugfs(struct aat2870_data *aat2870) { aat2870->dentry_root = debugfs_create_dir("aat2870", NULL); - if (!aat2870->dentry_root) { - dev_warn(aat2870->dev, - "Failed to create debugfs root directory\n"); - return; - } - aat2870->dentry_reg = debugfs_create_file("regs", 0644, - aat2870->dentry_root, - aat2870, &aat2870_reg_fops); - if (!aat2870->dentry_reg) - dev_warn(aat2870->dev, - "Failed to create debugfs register file\n"); + debugfs_create_file("regs", 0644, aat2870->dentry_root, aat2870, + &aat2870_reg_fops); } #else diff --git a/include/linux/mfd/aat2870.h b/include/linux/mfd/aat2870.h index af7267c480ee..2445842d482d 100644 --- a/include/linux/mfd/aat2870.h +++ b/include/linux/mfd/aat2870.h @@ -136,7 +136,6 @@ struct aat2870_data { /* for debugfs */ struct dentry *dentry_root; - struct dentry *dentry_reg; }; struct aat2870_subdev_info { -- cgit v1.2.3 From b9515ecbf6caef2ea911ca59801eff84d034fa48 Mon Sep 17 00:00:00 2001 From: Suzuki K Poulose Date: Thu, 1 Aug 2019 11:20:25 +0100 Subject: drivers: Fix typo in parameter description for driver_find_device_by_acpi_dev Fix a typo in the comment describing the parameters for the new API, which triggers the following warning for htmldocs: include/linux/device.h:479: warning: Function parameter or member 'drv' not described in 'driver_find_device_by_acpi_dev' Reported-by: kbuild test robot Signed-off-by: Suzuki K Poulose Link: https://lore.kernel.org/r/20190801102026.27312-2-suzuki.poulose@arm.com Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/linux/device.h b/include/linux/device.h index 23ef6eba7213..8e1b2ead5d96 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -467,7 +467,7 @@ driver_find_device_by_fwnode(struct device_driver *drv, /** * driver_find_device_by_devt- device iterator for locating a particular device * by devt. - * @driver: the driver we're iterating + * @drv: the driver we're iterating * @devt: devt pointer to match. */ static inline struct device *driver_find_device_by_devt(struct device_driver *drv, -- cgit v1.2.3 From 313b46d831189f593840c625d7972092cb0088fc Mon Sep 17 00:00:00 2001 From: Suzuki K Poulose Date: Thu, 1 Aug 2019 11:20:26 +0100 Subject: drivers: Fix htmldocs warnings with bus_find_next_device() Document the parameters for bus_find_next_device() to avoid htmldocs build warnings as reported below : include/linux/device.h:236: warning: Function parameter or member 'bus' not described in 'bus_find_next_device' include/linux/device.h:236: warning: Function parameter or member 'cur' not described in 'bus_find_next_device' Reported-by: kbuild test robot Signed-off-by: Suzuki K Poulose Link: https://lore.kernel.org/r/20190801102026.27312-3-suzuki.poulose@arm.com Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/linux/device.h b/include/linux/device.h index 8e1b2ead5d96..bff46ce3bc3b 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -229,6 +229,8 @@ static inline struct device *bus_find_device_by_devt(struct bus_type *bus, /** * bus_find_next_device - Find the next device after a given device in a * given bus. + * @bus: bus type + * @cur: device to begin the search with. */ static inline struct device * bus_find_next_device(struct bus_type *bus,struct device *cur) -- cgit v1.2.3 From 5302dd7dd0b6d04c63cdce51d1e9fda9ef0be886 Mon Sep 17 00:00:00 2001 From: Saravana Kannan Date: Wed, 31 Jul 2019 15:17:14 -0700 Subject: driver core: Add support for linking devices during device addition When devices are added, the bus might want to create device links to track functional dependencies between supplier and consumer devices. This tracking of supplier-consumer relationship allows optimizing device probe order and tracking whether all consumers of a supplier are active. The add_links bus callback is added to support this. However, when consumer devices are added, they might not have a supplier device to link to despite needing mandatory resources/functionality from one or more suppliers. A waiting_for_suppliers list is created to track such consumers and retry linking them when new devices get added. Signed-off-by: Saravana Kannan Link: https://lore.kernel.org/r/20190731221721.187713-2-saravanak@google.com Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 14 +++++++++ 2 files changed, 97 insertions(+) (limited to 'include') diff --git a/drivers/base/core.c b/drivers/base/core.c index 90f2dd4661f5..18e04ca1de13 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -44,6 +44,8 @@ early_param("sysfs.deprecated", sysfs_deprecated_setup); #endif /* Device links support. */ +static LIST_HEAD(wait_for_suppliers); +static DEFINE_MUTEX(wfs_lock); #ifdef CONFIG_SRCU static DEFINE_MUTEX(device_links_lock); @@ -418,6 +420,51 @@ struct device_link *device_link_add(struct device *consumer, } EXPORT_SYMBOL_GPL(device_link_add); +/** + * device_link_wait_for_supplier - Mark device as waiting for supplier + * @consumer: Consumer device + * + * Marks the consumer device as waiting for suppliers to become available. The + * consumer device will never be probed until it's unmarked as waiting for + * suppliers. The caller is responsible for adding the link to the supplier + * once the supplier device is present. + * + * This function is NOT meant to be called from the probe function of the + * consumer but rather from code that creates/adds the consumer device. + */ +static void device_link_wait_for_supplier(struct device *consumer) +{ + mutex_lock(&wfs_lock); + list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers); + mutex_unlock(&wfs_lock); +} + +/** + * device_link_check_waiting_consumers - Try to unmark waiting consumers + * + * Loops through all consumers waiting on suppliers and tries to add all their + * supplier links. If that succeeds, the consumer device is unmarked as waiting + * for suppliers. Otherwise, they are left marked as waiting on suppliers, + * + * The add_links bus callback is expected to return 0 if it has found and added + * all the supplier links for the consumer device. It should return an error if + * it isn't able to do so. + * + * The caller of device_link_wait_for_supplier() is expected to call this once + * it's aware of potential suppliers becoming available. + */ +static void device_link_check_waiting_consumers(void) +{ + struct device *dev, *tmp; + + mutex_lock(&wfs_lock); + list_for_each_entry_safe(dev, tmp, &wait_for_suppliers, + links.needs_suppliers) + if (!dev->bus->add_links(dev)) + list_del_init(&dev->links.needs_suppliers); + mutex_unlock(&wfs_lock); +} + static void device_link_free(struct device_link *link) { while (refcount_dec_not_one(&link->rpm_active)) @@ -552,6 +599,19 @@ int device_links_check_suppliers(struct device *dev) struct device_link *link; int ret = 0; + /* + * If a device is waiting for one or more suppliers (in + * wait_for_suppliers list), it is not ready to probe yet. So just + * return -EPROBE_DEFER without having to check the links with existing + * suppliers. + */ + mutex_lock(&wfs_lock); + if (!list_empty(&dev->links.needs_suppliers)) { + mutex_unlock(&wfs_lock); + return -EPROBE_DEFER; + } + mutex_unlock(&wfs_lock); + device_links_write_lock(); list_for_each_entry(link, &dev->links.suppliers, c_node) { @@ -836,6 +896,10 @@ static void device_links_purge(struct device *dev) { struct device_link *link, *ln; + mutex_lock(&wfs_lock); + list_del(&dev->links.needs_suppliers); + mutex_unlock(&wfs_lock); + /* * Delete all of the remaining links from this device to any other * devices (either consumers or suppliers). @@ -1697,6 +1761,7 @@ void device_initialize(struct device *dev) #endif INIT_LIST_HEAD(&dev->links.consumers); INIT_LIST_HEAD(&dev->links.suppliers); + INIT_LIST_HEAD(&dev->links.needs_suppliers); dev->links.status = DL_DEV_NO_DRIVER; } EXPORT_SYMBOL_GPL(device_initialize); @@ -2132,6 +2197,24 @@ int device_add(struct device *dev) BUS_NOTIFY_ADD_DEVICE, dev); kobject_uevent(&dev->kobj, KOBJ_ADD); + + /* + * Check if any of the other devices (consumers) have been waiting for + * this device (supplier) to be added so that they can create a device + * link to it. + * + * This needs to happen after device_pm_add() because device_link_add() + * requires the supplier be registered before it's called. + * + * But this also needs to happe before bus_probe_device() to make sure + * waiting consumers can link to it before the driver is bound to the + * device and the driver sync_state callback is called for this device. + */ + device_link_check_waiting_consumers(); + + if (dev->bus && dev->bus->add_links && dev->bus->add_links(dev)) + device_link_wait_for_supplier(dev); + bus_probe_device(dev); if (parent) klist_add_tail(&dev->p->knode_parent, diff --git a/include/linux/device.h b/include/linux/device.h index bff46ce3bc3b..1e05911325f0 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -78,6 +78,17 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); * -EPROBE_DEFER it will queue the device for deferred probing. * @uevent: Called when a device is added, removed, or a few other things * that generate uevents to add the environment variables. + * @add_links: Called, perhaps multiple times per device, after a device is + * added to this bus. The function is expected to create device + * links to all the suppliers of the input device that are + * available at the time this function is called. As in, the + * function should NOT stop at the first failed device link if + * other unlinked supplier devices are present in the system. + * + * Return 0 if device links have been successfully created to all + * the suppliers of this device. Return an error if some of the + * suppliers are not yet available and this function needs to be + * reattempted in the future. * @probe: Called when a new device or driver add to this bus, and callback * the specific driver's probe to initial the matched device. * @remove: Called when a device removed from this bus. @@ -122,6 +133,7 @@ struct bus_type { int (*match)(struct device *dev, struct device_driver *drv); int (*uevent)(struct device *dev, struct kobj_uevent_env *env); + int (*add_links)(struct device *dev); int (*probe)(struct device *dev); int (*remove)(struct device *dev); void (*shutdown)(struct device *dev); @@ -1128,11 +1140,13 @@ enum dl_dev_state { * struct dev_links_info - Device data related to device links. * @suppliers: List of links to supplier devices. * @consumers: List of links to consumer devices. + * @needs_suppliers: Hook to global list of devices waiting for suppliers. * @status: Driver status information. */ struct dev_links_info { struct list_head suppliers; struct list_head consumers; + struct list_head needs_suppliers; enum dl_dev_state status; }; -- cgit v1.2.3 From 134b23eec9e3a3c795a6ceb0efe2fa63e87983b2 Mon Sep 17 00:00:00 2001 From: Saravana Kannan Date: Wed, 31 Jul 2019 15:17:15 -0700 Subject: driver core: Add edit_links() callback for drivers The driver core/bus adding supplier-consumer dependencies by default enables functional dependencies to be tracked correctly even when the consumer devices haven't had their drivers registered or loaded (if they are modules). However, when the bus incorrectly adds dependencies that it shouldn't have added, the devices might never probe. For example, if device-C is a consumer of device-S and they have phandles to each other in DT, the following could happen: 1. Device-S get added first. 2. The bus add_links() callback will (incorrectly) try to link it as a consumer of device-C. 3. Since device-C isn't present, device-S will be put in "waiting-for-supplier" list. 4. Device-C gets added next. 5. All devices in "waiting-for-supplier" list are retried for linking. 6. Device-S gets linked as consumer to Device-C. 7. The bus add_links() callback will (correctly) try to link it as a consumer of device-S. 8. This isn't allowed because it would create a cyclic device links. Neither devices will get probed since the supplier is marked as dependent on the consumer. And the consumer will never probe because the consumer can't get resources from the supplier. Without this patch, things stay in this broken state. However, with this patch, the execution will continue like this: 9. Device-C's driver is loaded. 10. Device-C's driver removes Device-S as a consumer of Device-C. 11. Device-C's driver adds Device-C as a consumer of Device-S. 12. Device-S probes. 14. Device-C probes. kbuild test robot reported missing documentation for device.has_edit_links Reported-by: kbuild test robot Signed-off-by: Saravana Kannan Link: https://lore.kernel.org/r/20190731221721.187713-3-saravanak@google.com Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 24 ++++++++++++++++++++++-- drivers/base/dd.c | 29 +++++++++++++++++++++++++++++ include/linux/device.h | 20 ++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/drivers/base/core.c b/drivers/base/core.c index 18e04ca1de13..feec8dee1e91 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -439,6 +439,19 @@ static void device_link_wait_for_supplier(struct device *consumer) mutex_unlock(&wfs_lock); } +/** + * device_link_remove_from_wfs - Unmark device as waiting for supplier + * @consumer: Consumer device + * + * Unmark the consumer device as waiting for suppliers to become available. + */ +void device_link_remove_from_wfs(struct device *consumer) +{ + mutex_lock(&wfs_lock); + list_del_init(&consumer->links.needs_suppliers); + mutex_unlock(&wfs_lock); +} + /** * device_link_check_waiting_consumers - Try to unmark waiting consumers * @@ -456,12 +469,19 @@ static void device_link_wait_for_supplier(struct device *consumer) static void device_link_check_waiting_consumers(void) { struct device *dev, *tmp; + int ret; mutex_lock(&wfs_lock); list_for_each_entry_safe(dev, tmp, &wait_for_suppliers, - links.needs_suppliers) - if (!dev->bus->add_links(dev)) + links.needs_suppliers) { + ret = 0; + if (dev->has_edit_links) + ret = driver_edit_links(dev); + else if (dev->bus->add_links) + ret = dev->bus->add_links(dev); + if (!ret) list_del_init(&dev->links.needs_suppliers); + } mutex_unlock(&wfs_lock); } diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 994a90747420..5e7041ede0d7 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -698,6 +698,12 @@ int driver_probe_device(struct device_driver *drv, struct device *dev) pr_debug("bus: '%s': %s: matched device %s with driver %s\n", drv->bus->name, __func__, dev_name(dev), drv->name); + if (drv->edit_links) { + if (drv->edit_links(dev)) + dev->has_edit_links = true; + else + device_link_remove_from_wfs(dev); + } pm_runtime_get_suppliers(dev); if (dev->parent) pm_runtime_get_sync(dev->parent); @@ -786,6 +792,29 @@ struct device_attach_data { bool have_async; }; +static int __driver_edit_links(struct device_driver *drv, void *data) +{ + struct device *dev = data; + + if (!drv->edit_links) + return 0; + + if (driver_match_device(drv, dev) <= 0) + return 0; + + return drv->edit_links(dev); +} + +int driver_edit_links(struct device *dev) +{ + int ret; + + device_lock(dev); + ret = bus_for_each_drv(dev->bus, NULL, dev, __driver_edit_links); + device_unlock(dev); + return ret; +} + static int __device_attach_driver(struct device_driver *drv, void *_data) { struct device_attach_data *data = _data; diff --git a/include/linux/device.h b/include/linux/device.h index 1e05911325f0..d3991810f39d 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -349,6 +349,20 @@ enum probe_type { * @probe_type: Type of the probe (synchronous or asynchronous) to use. * @of_match_table: The open firmware table. * @acpi_match_table: The ACPI match table. + * @edit_links: Called to allow a matched driver to edit the device links the + * bus might have added incorrectly. This will be useful to handle + * cases where the bus incorrectly adds functional dependencies + * that aren't true or tries to create cyclic dependencies. But + * doesn't correctly handle functional dependencies that are + * missed by the bus as the supplier's sync_state might get to + * execute before the driver for a missing consumer is loaded and + * gets to edit the device links for the consumer. + * + * This function might be called multiple times after a new device + * is added. The function is expected to create all the device + * links for the new device and return 0 if it was completed + * successfully or return an error if it needs to be reattempted + * in the future. * @probe: Called to query the existence of a specific device, * whether this driver can work with it, and bind the driver * to a specific device. @@ -388,6 +402,7 @@ struct device_driver { const struct of_device_id *of_match_table; const struct acpi_device_id *acpi_match_table; + int (*edit_links)(struct device *dev); int (*probe) (struct device *dev); int (*remove) (struct device *dev); void (*shutdown) (struct device *dev); @@ -1220,6 +1235,8 @@ struct dev_links_info { * @offline: Set after successful invocation of bus type's .offline(). * @of_node_reused: Set if the device-tree node is shared with an ancestor * device. + * @has_edit_links: This device has a driver than is capable of + * editing the device links created by driver core. * @dma_coherent: this particular device is dma coherent, even if the * architecture supports non-coherent devices. * @@ -1313,6 +1330,7 @@ struct device { bool offline_disabled:1; bool offline:1; bool of_node_reused:1; + bool has_edit_links:1; #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) @@ -1564,6 +1582,7 @@ extern int __must_check device_attach(struct device *dev); extern int __must_check driver_attach(struct device_driver *drv); extern void device_initial_probe(struct device *dev); extern int __must_check device_reprobe(struct device *dev); +extern int driver_edit_links(struct device *dev); extern bool device_is_bound(struct device *dev); @@ -1654,6 +1673,7 @@ struct device_link *device_link_add(struct device *consumer, struct device *supplier, u32 flags); void device_link_del(struct device_link *link); void device_link_remove(void *consumer, struct device *supplier); +void device_link_remove_from_wfs(struct device *consumer); #ifndef dev_fmt #define dev_fmt(fmt) fmt -- cgit v1.2.3 From 8f8184d6bf676a8680d6f441e40317d166b46f73 Mon Sep 17 00:00:00 2001 From: Saravana Kannan Date: Wed, 31 Jul 2019 15:17:17 -0700 Subject: driver core: Add sync_state driver/bus callback This sync_state driver/bus callback is called once all the consumers of a supplier have probed successfully. This allows the supplier device's driver/bus to sync the supplier device's state to the software state with the guarantee that all the consumers are actively managing the resources provided by the supplier device. To maintain backwards compatibility and ease transition from existing frameworks and resource cleanup schemes, late_initcall_sync is the earliest when the sync_state callback might be called. There is no upper bound on the time by which the sync_state callback has to be called. This is because if a consumer device never probes, the supplier has to maintain its resources in the state left by the bootloader. For example, if the bootloader leaves the display backlight at a fixed voltage and the backlight driver is never probed, you don't want the backlight to ever be turned off after boot up. Also, when multiple devices are added after kernel init, some suppliers could be added before their consumer devices get added. In these instances, the supplier devices could get their sync_state callback called right after they probe because the consumers devices haven't had a chance to create device links to the suppliers. To handle this correctly, this change also provides APIs to pause/resume sync state callbacks so that when multiple devices are added, their sync_state callback evaluation can be postponed to happen after all of them are added. kbuild test robot reported missing documentation for device.state_synced Reported-by: kbuild test robot Signed-off-by: Saravana Kannan Link: https://lore.kernel.org/r/20190731221721.187713-5-saravanak@google.com Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 26 ++++++++++++++++++++ 2 files changed, 91 insertions(+) (limited to 'include') diff --git a/drivers/base/core.c b/drivers/base/core.c index feec8dee1e91..5b1795cd116a 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -46,6 +46,8 @@ early_param("sysfs.deprecated", sysfs_deprecated_setup); /* Device links support. */ static LIST_HEAD(wait_for_suppliers); static DEFINE_MUTEX(wfs_lock); +static LIST_HEAD(deferred_sync); +static unsigned int supplier_sync_state_disabled; #ifdef CONFIG_SRCU static DEFINE_MUTEX(device_links_lock); @@ -651,6 +653,62 @@ int device_links_check_suppliers(struct device *dev) return ret; } +static void __device_links_supplier_sync_state(struct device *dev) +{ + struct device_link *link; + + if (dev->state_synced) + return; + + list_for_each_entry(link, &dev->links.consumers, s_node) { + if (!(link->flags & DL_FLAG_MANAGED)) + continue; + if (link->status != DL_STATE_ACTIVE) + return; + } + + if (dev->bus->sync_state) + dev->bus->sync_state(dev); + else if (dev->driver && dev->driver->sync_state) + dev->driver->sync_state(dev); + + dev->state_synced = true; +} + +void device_links_supplier_sync_state_pause(void) +{ + device_links_write_lock(); + supplier_sync_state_disabled++; + device_links_write_unlock(); +} + +void device_links_supplier_sync_state_resume(void) +{ + struct device *dev, *tmp; + + device_links_write_lock(); + if (!supplier_sync_state_disabled) { + WARN(true, "Unmatched sync_state pause/resume!"); + goto out; + } + supplier_sync_state_disabled--; + if (supplier_sync_state_disabled) + goto out; + + list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) { + __device_links_supplier_sync_state(dev); + list_del_init(&dev->links.defer_sync); + } +out: + device_links_write_unlock(); +} + +static void __device_links_supplier_defer_sync(struct device *sup) +{ + if (list_empty(&sup->links.defer_sync)) + list_add_tail(&sup->links.defer_sync, &deferred_sync); +} + /** * device_links_driver_bound - Update device links after probing its driver. * @dev: Device to update the links for. @@ -695,6 +753,11 @@ void device_links_driver_bound(struct device *dev) WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); WRITE_ONCE(link->status, DL_STATE_ACTIVE); + + if (supplier_sync_state_disabled) + __device_links_supplier_defer_sync(link->supplier); + else + __device_links_supplier_sync_state(link->supplier); } dev->links.status = DL_DEV_DRIVER_BOUND; @@ -811,6 +874,7 @@ void device_links_driver_cleanup(struct device *dev) WRITE_ONCE(link->status, DL_STATE_DORMANT); } + list_del_init(&dev->links.defer_sync); __device_links_no_driver(dev); device_links_write_unlock(); @@ -1782,6 +1846,7 @@ void device_initialize(struct device *dev) INIT_LIST_HEAD(&dev->links.consumers); INIT_LIST_HEAD(&dev->links.suppliers); INIT_LIST_HEAD(&dev->links.needs_suppliers); + INIT_LIST_HEAD(&dev->links.defer_sync); dev->links.status = DL_DEV_NO_DRIVER; } EXPORT_SYMBOL_GPL(device_initialize); diff --git a/include/linux/device.h b/include/linux/device.h index d3991810f39d..63a3aafabcd6 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -84,6 +84,8 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); * available at the time this function is called. As in, the * function should NOT stop at the first failed device link if * other unlinked supplier devices are present in the system. + * This is necessary for the sync_state() callback to work + * correctly. * * Return 0 if device links have been successfully created to all * the suppliers of this device. Return an error if some of the @@ -91,6 +93,13 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); * reattempted in the future. * @probe: Called when a new device or driver add to this bus, and callback * the specific driver's probe to initial the matched device. + * @sync_state: Called to sync device state to software state after all the + * state tracking consumers linked to this device (present at + * the time of late_initcall) have successfully bound to a + * driver. If the device has no consumers, this function will + * be called at late_initcall_sync level. If the device has + * consumers that are never bound to a driver, this function + * will never get called until they do. * @remove: Called when a device removed from this bus. * @shutdown: Called at shut-down time to quiesce the device. * @@ -135,6 +144,7 @@ struct bus_type { int (*uevent)(struct device *dev, struct kobj_uevent_env *env); int (*add_links)(struct device *dev); int (*probe)(struct device *dev); + void (*sync_state)(struct device *dev); int (*remove)(struct device *dev); void (*shutdown)(struct device *dev); @@ -366,6 +376,13 @@ enum probe_type { * @probe: Called to query the existence of a specific device, * whether this driver can work with it, and bind the driver * to a specific device. + * @sync_state: Called to sync device state to software state after all the + * state tracking consumers linked to this device (present at + * the time of late_initcall) have successfully bound to a + * driver. If the device has no consumers, this function will + * be called at late_initcall_sync level. If the device has + * consumers that are never bound to a driver, this function + * will never get called until they do. * @remove: Called when the device is removed from the system to * unbind a device from this driver. * @shutdown: Called at shut-down time to quiesce the device. @@ -404,6 +421,7 @@ struct device_driver { int (*edit_links)(struct device *dev); int (*probe) (struct device *dev); + void (*sync_state)(struct device *dev); int (*remove) (struct device *dev); void (*shutdown) (struct device *dev); int (*suspend) (struct device *dev, pm_message_t state); @@ -1156,12 +1174,14 @@ enum dl_dev_state { * @suppliers: List of links to supplier devices. * @consumers: List of links to consumer devices. * @needs_suppliers: Hook to global list of devices waiting for suppliers. + * @defer_sync: Hook to global list of devices that have deferred sync_state. * @status: Driver status information. */ struct dev_links_info { struct list_head suppliers; struct list_head consumers; struct list_head needs_suppliers; + struct list_head defer_sync; enum dl_dev_state status; }; @@ -1237,6 +1257,9 @@ struct dev_links_info { * device. * @has_edit_links: This device has a driver than is capable of * editing the device links created by driver core. + * @state_synced: The hardware state of this device has been synced to match + * the software state of this device by calling the driver/bus + * sync_state() callback. * @dma_coherent: this particular device is dma coherent, even if the * architecture supports non-coherent devices. * @@ -1331,6 +1354,7 @@ struct device { bool offline:1; bool of_node_reused:1; bool has_edit_links:1; + bool state_synced:1; #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) @@ -1674,6 +1698,8 @@ struct device_link *device_link_add(struct device *consumer, void device_link_del(struct device_link *link); void device_link_remove(void *consumer, struct device *supplier); void device_link_remove_from_wfs(struct device *consumer); +void device_links_supplier_sync_state_pause(void); +void device_links_supplier_sync_state_resume(void); #ifndef dev_fmt #define dev_fmt(fmt) fmt -- cgit v1.2.3 From 23b6904442d08b7dbed7622ed33b236d41a3aa8b Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Wed, 31 Jul 2019 14:43:40 +0200 Subject: driver core: add dev_groups to all drivers Add the ability for the driver core to create and remove a list of attribute groups automatically when the device is bound/unbound from a specific driver. Signed-off-by: Dmitry Torokhov Tested-by: Richard Gong Link: https://lore.kernel.org/r/20190731124349.4474-2-gregkh@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman --- drivers/base/dd.c | 14 ++++++++++++++ include/linux/device.h | 3 +++ 2 files changed, 17 insertions(+) (limited to 'include') diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 994a90747420..d811e60610d3 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -554,9 +554,16 @@ re_probe: goto probe_failed; } + if (device_add_groups(dev, drv->dev_groups)) { + dev_err(dev, "device_add_groups() failed\n"); + goto dev_groups_failed; + } + if (test_remove) { test_remove = false; + device_remove_groups(dev, drv->dev_groups); + if (dev->bus->remove) dev->bus->remove(dev); else if (drv->remove) @@ -584,6 +591,11 @@ re_probe: drv->bus->name, __func__, dev_name(dev), drv->name); goto done; +dev_groups_failed: + if (dev->bus->remove) + dev->bus->remove(dev); + else if (drv->remove) + drv->remove(dev); probe_failed: if (dev->bus) blocking_notifier_call_chain(&dev->bus->p->bus_notifier, @@ -1114,6 +1126,8 @@ static void __device_release_driver(struct device *dev, struct device *parent) pm_runtime_put_sync(dev); + device_remove_groups(dev, drv->dev_groups); + if (dev->bus && dev->bus->remove) dev->bus->remove(dev); else if (drv->remove) diff --git a/include/linux/device.h b/include/linux/device.h index c330b75c6c57..98c00b71b598 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -262,6 +262,8 @@ enum probe_type { * @resume: Called to bring a device from sleep mode. * @groups: Default attributes that get created by the driver core * automatically. + * @dev_groups: Additional attributes attached to device instance once the + * it is bound to the driver. * @pm: Power management operations of the device which matched * this driver. * @coredump: Called when sysfs entry is written to. The device driver @@ -296,6 +298,7 @@ struct device_driver { int (*suspend) (struct device *dev, pm_message_t state); int (*resume) (struct device *dev); const struct attribute_group **groups; + const struct attribute_group **dev_groups; const struct dev_pm_ops *pm; void (*coredump) (struct device *dev); -- cgit v1.2.3 From 1f573cce48a2ebb35953034062ef01056b7d6a58 Mon Sep 17 00:00:00 2001 From: Suzuki K Poulose Date: Fri, 9 Aug 2019 11:20:33 +0100 Subject: device.h: Fix warnings for mismatched parameter names in comments Fix the warnings for parameter named as "driver" instead of the actual "drv" in the comments as reported by the kbuild robot. Reported-by: kbuild test robot Cc: Greg Kroah-Hartman Signed-off-by: Suzuki K Poulose Link: https://lore.kernel.org/r/20190809102033.28463-1-suzuki.poulose@arm.com Signed-off-by: Greg Kroah-Hartman --- include/linux/device.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/linux/device.h b/include/linux/device.h index 23efaff5f10c..e32038f4ef56 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -479,7 +479,7 @@ struct device *driver_find_device(struct device_driver *drv, /** * driver_find_device_by_name - device iterator for locating a particular device * of a specific name. - * @driver: the driver we're iterating + * @drv: the driver we're iterating * @name: name of the device to match */ static inline struct device *driver_find_device_by_name(struct device_driver *drv, @@ -491,7 +491,7 @@ static inline struct device *driver_find_device_by_name(struct device_driver *dr /** * driver_find_device_by_of_node- device iterator for locating a particular device * by of_node pointer. - * @driver: the driver we're iterating + * @drv: the driver we're iterating * @np: of_node pointer to match. */ static inline struct device * @@ -504,7 +504,7 @@ driver_find_device_by_of_node(struct device_driver *drv, /** * driver_find_device_by_fwnode- device iterator for locating a particular device * by fwnode pointer. - * @driver: the driver we're iterating + * @drv: the driver we're iterating * @fwnode: fwnode pointer to match. */ static inline struct device * @@ -536,7 +536,7 @@ static inline struct device *driver_find_next_device(struct device_driver *drv, /** * driver_find_device_by_acpi_dev : device iterator for locating a particular * device matching the ACPI_COMPANION device. - * @driver: the driver we're iterating + * @drv: the driver we're iterating * @adev: ACPI_COMPANION device to match. */ static inline struct device * -- cgit v1.2.3 From bcca686c11cdad3b4566ed1818cd2688e7901def Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Tue, 27 Aug 2019 21:40:50 +0200 Subject: Revert "driver core: Add sync_state driver/bus callback" This reverts commit 8f8184d6bf676a8680d6f441e40317d166b46f73. Based on a lot of email and in-person discussions, this patch series is being reworked to address a number of issues that were pointed out that needed to be taken care of before it should be merged. It will be resubmitted with those changes hopefully soon. Cc: Frank Rowand Cc: Saravana Kannan Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 65 -------------------------------------------------- include/linux/device.h | 26 -------------------- 2 files changed, 91 deletions(-) (limited to 'include') diff --git a/drivers/base/core.c b/drivers/base/core.c index 75fd59360940..de775c7a4d7c 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -46,8 +46,6 @@ early_param("sysfs.deprecated", sysfs_deprecated_setup); /* Device links support. */ static LIST_HEAD(wait_for_suppliers); static DEFINE_MUTEX(wfs_lock); -static LIST_HEAD(deferred_sync); -static unsigned int supplier_sync_state_disabled; #ifdef CONFIG_SRCU static DEFINE_MUTEX(device_links_lock); @@ -653,62 +651,6 @@ int device_links_check_suppliers(struct device *dev) return ret; } -static void __device_links_supplier_sync_state(struct device *dev) -{ - struct device_link *link; - - if (dev->state_synced) - return; - - list_for_each_entry(link, &dev->links.consumers, s_node) { - if (!(link->flags & DL_FLAG_MANAGED)) - continue; - if (link->status != DL_STATE_ACTIVE) - return; - } - - if (dev->bus->sync_state) - dev->bus->sync_state(dev); - else if (dev->driver && dev->driver->sync_state) - dev->driver->sync_state(dev); - - dev->state_synced = true; -} - -void device_links_supplier_sync_state_pause(void) -{ - device_links_write_lock(); - supplier_sync_state_disabled++; - device_links_write_unlock(); -} - -void device_links_supplier_sync_state_resume(void) -{ - struct device *dev, *tmp; - - device_links_write_lock(); - if (!supplier_sync_state_disabled) { - WARN(true, "Unmatched sync_state pause/resume!"); - goto out; - } - supplier_sync_state_disabled--; - if (supplier_sync_state_disabled) - goto out; - - list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) { - __device_links_supplier_sync_state(dev); - list_del_init(&dev->links.defer_sync); - } -out: - device_links_write_unlock(); -} - -static void __device_links_supplier_defer_sync(struct device *sup) -{ - if (list_empty(&sup->links.defer_sync)) - list_add_tail(&sup->links.defer_sync, &deferred_sync); -} - /** * device_links_driver_bound - Update device links after probing its driver. * @dev: Device to update the links for. @@ -753,11 +695,6 @@ void device_links_driver_bound(struct device *dev) WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); WRITE_ONCE(link->status, DL_STATE_ACTIVE); - - if (supplier_sync_state_disabled) - __device_links_supplier_defer_sync(link->supplier); - else - __device_links_supplier_sync_state(link->supplier); } dev->links.status = DL_DEV_DRIVER_BOUND; @@ -874,7 +811,6 @@ void device_links_driver_cleanup(struct device *dev) WRITE_ONCE(link->status, DL_STATE_DORMANT); } - list_del_init(&dev->links.defer_sync); __device_links_no_driver(dev); device_links_write_unlock(); @@ -1849,7 +1785,6 @@ void device_initialize(struct device *dev) INIT_LIST_HEAD(&dev->links.consumers); INIT_LIST_HEAD(&dev->links.suppliers); INIT_LIST_HEAD(&dev->links.needs_suppliers); - INIT_LIST_HEAD(&dev->links.defer_sync); dev->links.status = DL_DEV_NO_DRIVER; } EXPORT_SYMBOL_GPL(device_initialize); diff --git a/include/linux/device.h b/include/linux/device.h index 76496497e753..90142ec9ce84 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -84,8 +84,6 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); * available at the time this function is called. As in, the * function should NOT stop at the first failed device link if * other unlinked supplier devices are present in the system. - * This is necessary for the sync_state() callback to work - * correctly. * * Return 0 if device links have been successfully created to all * the suppliers of this device. Return an error if some of the @@ -93,13 +91,6 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); * reattempted in the future. * @probe: Called when a new device or driver add to this bus, and callback * the specific driver's probe to initial the matched device. - * @sync_state: Called to sync device state to software state after all the - * state tracking consumers linked to this device (present at - * the time of late_initcall) have successfully bound to a - * driver. If the device has no consumers, this function will - * be called at late_initcall_sync level. If the device has - * consumers that are never bound to a driver, this function - * will never get called until they do. * @remove: Called when a device removed from this bus. * @shutdown: Called at shut-down time to quiesce the device. * @@ -144,7 +135,6 @@ struct bus_type { int (*uevent)(struct device *dev, struct kobj_uevent_env *env); int (*add_links)(struct device *dev); int (*probe)(struct device *dev); - void (*sync_state)(struct device *dev); int (*remove)(struct device *dev); void (*shutdown)(struct device *dev); @@ -376,13 +366,6 @@ enum probe_type { * @probe: Called to query the existence of a specific device, * whether this driver can work with it, and bind the driver * to a specific device. - * @sync_state: Called to sync device state to software state after all the - * state tracking consumers linked to this device (present at - * the time of late_initcall) have successfully bound to a - * driver. If the device has no consumers, this function will - * be called at late_initcall_sync level. If the device has - * consumers that are never bound to a driver, this function - * will never get called until they do. * @remove: Called when the device is removed from the system to * unbind a device from this driver. * @shutdown: Called at shut-down time to quiesce the device. @@ -423,7 +406,6 @@ struct device_driver { int (*edit_links)(struct device *dev); int (*probe) (struct device *dev); - void (*sync_state)(struct device *dev); int (*remove) (struct device *dev); void (*shutdown) (struct device *dev); int (*suspend) (struct device *dev, pm_message_t state); @@ -1177,14 +1159,12 @@ enum dl_dev_state { * @suppliers: List of links to supplier devices. * @consumers: List of links to consumer devices. * @needs_suppliers: Hook to global list of devices waiting for suppliers. - * @defer_sync: Hook to global list of devices that have deferred sync_state. * @status: Driver status information. */ struct dev_links_info { struct list_head suppliers; struct list_head consumers; struct list_head needs_suppliers; - struct list_head defer_sync; enum dl_dev_state status; }; @@ -1262,9 +1242,6 @@ struct dev_links_info { * device. * @has_edit_links: This device has a driver than is capable of * editing the device links created by driver core. - * @state_synced: The hardware state of this device has been synced to match - * the software state of this device by calling the driver/bus - * sync_state() callback. * @dma_coherent: this particular device is dma coherent, even if the * architecture supports non-coherent devices. * @@ -1362,7 +1339,6 @@ struct device { bool offline:1; bool of_node_reused:1; bool has_edit_links:1; - bool state_synced:1; #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) @@ -1707,8 +1683,6 @@ struct device_link *device_link_add(struct device *consumer, void device_link_del(struct device_link *link); void device_link_remove(void *consumer, struct device *supplier); void device_link_remove_from_wfs(struct device *consumer); -void device_links_supplier_sync_state_pause(void); -void device_links_supplier_sync_state_resume(void); #ifndef dev_fmt #define dev_fmt(fmt) fmt -- cgit v1.2.3 From 33cbfe54499338af08ab906a99afac247ea533f6 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Tue, 27 Aug 2019 21:41:06 +0200 Subject: Revert "driver core: Add edit_links() callback for drivers" This reverts commit 134b23eec9e3a3c795a6ceb0efe2fa63e87983b2. Based on a lot of email and in-person discussions, this patch series is being reworked to address a number of issues that were pointed out that needed to be taken care of before it should be merged. It will be resubmitted with those changes hopefully soon. Cc: Frank Rowand Cc: Saravana Kannan Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 24 ++---------------------- drivers/base/dd.c | 29 ----------------------------- include/linux/device.h | 20 -------------------- 3 files changed, 2 insertions(+), 71 deletions(-) (limited to 'include') diff --git a/drivers/base/core.c b/drivers/base/core.c index de775c7a4d7c..605905de0cca 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -439,19 +439,6 @@ static void device_link_wait_for_supplier(struct device *consumer) mutex_unlock(&wfs_lock); } -/** - * device_link_remove_from_wfs - Unmark device as waiting for supplier - * @consumer: Consumer device - * - * Unmark the consumer device as waiting for suppliers to become available. - */ -void device_link_remove_from_wfs(struct device *consumer) -{ - mutex_lock(&wfs_lock); - list_del_init(&consumer->links.needs_suppliers); - mutex_unlock(&wfs_lock); -} - /** * device_link_check_waiting_consumers - Try to unmark waiting consumers * @@ -469,19 +456,12 @@ void device_link_remove_from_wfs(struct device *consumer) static void device_link_check_waiting_consumers(void) { struct device *dev, *tmp; - int ret; mutex_lock(&wfs_lock); list_for_each_entry_safe(dev, tmp, &wait_for_suppliers, - links.needs_suppliers) { - ret = 0; - if (dev->has_edit_links) - ret = driver_edit_links(dev); - else if (dev->bus->add_links) - ret = dev->bus->add_links(dev); - if (!ret) + links.needs_suppliers) + if (!dev->bus->add_links(dev)) list_del_init(&dev->links.needs_suppliers); - } mutex_unlock(&wfs_lock); } diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 55fbc2467b37..d811e60610d3 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -710,12 +710,6 @@ int driver_probe_device(struct device_driver *drv, struct device *dev) pr_debug("bus: '%s': %s: matched device %s with driver %s\n", drv->bus->name, __func__, dev_name(dev), drv->name); - if (drv->edit_links) { - if (drv->edit_links(dev)) - dev->has_edit_links = true; - else - device_link_remove_from_wfs(dev); - } pm_runtime_get_suppliers(dev); if (dev->parent) pm_runtime_get_sync(dev->parent); @@ -804,29 +798,6 @@ struct device_attach_data { bool have_async; }; -static int __driver_edit_links(struct device_driver *drv, void *data) -{ - struct device *dev = data; - - if (!drv->edit_links) - return 0; - - if (driver_match_device(drv, dev) <= 0) - return 0; - - return drv->edit_links(dev); -} - -int driver_edit_links(struct device *dev) -{ - int ret; - - device_lock(dev); - ret = bus_for_each_drv(dev->bus, NULL, dev, __driver_edit_links); - device_unlock(dev); - return ret; -} - static int __device_attach_driver(struct device_driver *drv, void *_data) { struct device_attach_data *data = _data; diff --git a/include/linux/device.h b/include/linux/device.h index 90142ec9ce84..73210745cc6b 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -349,20 +349,6 @@ enum probe_type { * @probe_type: Type of the probe (synchronous or asynchronous) to use. * @of_match_table: The open firmware table. * @acpi_match_table: The ACPI match table. - * @edit_links: Called to allow a matched driver to edit the device links the - * bus might have added incorrectly. This will be useful to handle - * cases where the bus incorrectly adds functional dependencies - * that aren't true or tries to create cyclic dependencies. But - * doesn't correctly handle functional dependencies that are - * missed by the bus as the supplier's sync_state might get to - * execute before the driver for a missing consumer is loaded and - * gets to edit the device links for the consumer. - * - * This function might be called multiple times after a new device - * is added. The function is expected to create all the device - * links for the new device and return 0 if it was completed - * successfully or return an error if it needs to be reattempted - * in the future. * @probe: Called to query the existence of a specific device, * whether this driver can work with it, and bind the driver * to a specific device. @@ -404,7 +390,6 @@ struct device_driver { const struct of_device_id *of_match_table; const struct acpi_device_id *acpi_match_table; - int (*edit_links)(struct device *dev); int (*probe) (struct device *dev); int (*remove) (struct device *dev); void (*shutdown) (struct device *dev); @@ -1240,8 +1225,6 @@ struct dev_links_info { * @offline: Set after successful invocation of bus type's .offline(). * @of_node_reused: Set if the device-tree node is shared with an ancestor * device. - * @has_edit_links: This device has a driver than is capable of - * editing the device links created by driver core. * @dma_coherent: this particular device is dma coherent, even if the * architecture supports non-coherent devices. * @@ -1338,7 +1321,6 @@ struct device { bool offline_disabled:1; bool offline:1; bool of_node_reused:1; - bool has_edit_links:1; #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) @@ -1590,7 +1572,6 @@ extern int __must_check device_attach(struct device *dev); extern int __must_check driver_attach(struct device_driver *drv); extern void device_initial_probe(struct device *dev); extern int __must_check device_reprobe(struct device *dev); -extern int driver_edit_links(struct device *dev); extern bool device_is_bound(struct device *dev); @@ -1682,7 +1663,6 @@ struct device_link *device_link_add(struct device *consumer, struct device *supplier, u32 flags); void device_link_del(struct device_link *link); void device_link_remove(void *consumer, struct device *supplier); -void device_link_remove_from_wfs(struct device *consumer); #ifndef dev_fmt #define dev_fmt(fmt) fmt -- cgit v1.2.3 From bfb3943bed670a0655aa2f9dcf82222ff09d6dd1 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Tue, 27 Aug 2019 21:41:16 +0200 Subject: Revert "driver core: Add support for linking devices during device addition" This reverts commit 5302dd7dd0b6d04c63cdce51d1e9fda9ef0be886. Based on a lot of email and in-person discussions, this patch series is being reworked to address a number of issues that were pointed out that needed to be taken care of before it should be merged. It will be resubmitted with those changes hopefully soon. Cc: Frank Rowand Cc: Saravana Kannan Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 83 -------------------------------------------------- include/linux/device.h | 14 --------- 2 files changed, 97 deletions(-) (limited to 'include') diff --git a/drivers/base/core.c b/drivers/base/core.c index 605905de0cca..066badad988c 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -44,8 +44,6 @@ early_param("sysfs.deprecated", sysfs_deprecated_setup); #endif /* Device links support. */ -static LIST_HEAD(wait_for_suppliers); -static DEFINE_MUTEX(wfs_lock); #ifdef CONFIG_SRCU static DEFINE_MUTEX(device_links_lock); @@ -420,51 +418,6 @@ struct device_link *device_link_add(struct device *consumer, } EXPORT_SYMBOL_GPL(device_link_add); -/** - * device_link_wait_for_supplier - Mark device as waiting for supplier - * @consumer: Consumer device - * - * Marks the consumer device as waiting for suppliers to become available. The - * consumer device will never be probed until it's unmarked as waiting for - * suppliers. The caller is responsible for adding the link to the supplier - * once the supplier device is present. - * - * This function is NOT meant to be called from the probe function of the - * consumer but rather from code that creates/adds the consumer device. - */ -static void device_link_wait_for_supplier(struct device *consumer) -{ - mutex_lock(&wfs_lock); - list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers); - mutex_unlock(&wfs_lock); -} - -/** - * device_link_check_waiting_consumers - Try to unmark waiting consumers - * - * Loops through all consumers waiting on suppliers and tries to add all their - * supplier links. If that succeeds, the consumer device is unmarked as waiting - * for suppliers. Otherwise, they are left marked as waiting on suppliers, - * - * The add_links bus callback is expected to return 0 if it has found and added - * all the supplier links for the consumer device. It should return an error if - * it isn't able to do so. - * - * The caller of device_link_wait_for_supplier() is expected to call this once - * it's aware of potential suppliers becoming available. - */ -static void device_link_check_waiting_consumers(void) -{ - struct device *dev, *tmp; - - mutex_lock(&wfs_lock); - list_for_each_entry_safe(dev, tmp, &wait_for_suppliers, - links.needs_suppliers) - if (!dev->bus->add_links(dev)) - list_del_init(&dev->links.needs_suppliers); - mutex_unlock(&wfs_lock); -} - static void device_link_free(struct device_link *link) { while (refcount_dec_not_one(&link->rpm_active)) @@ -599,19 +552,6 @@ int device_links_check_suppliers(struct device *dev) struct device_link *link; int ret = 0; - /* - * If a device is waiting for one or more suppliers (in - * wait_for_suppliers list), it is not ready to probe yet. So just - * return -EPROBE_DEFER without having to check the links with existing - * suppliers. - */ - mutex_lock(&wfs_lock); - if (!list_empty(&dev->links.needs_suppliers)) { - mutex_unlock(&wfs_lock); - return -EPROBE_DEFER; - } - mutex_unlock(&wfs_lock); - device_links_write_lock(); list_for_each_entry(link, &dev->links.suppliers, c_node) { @@ -896,10 +836,6 @@ static void device_links_purge(struct device *dev) { struct device_link *link, *ln; - mutex_lock(&wfs_lock); - list_del(&dev->links.needs_suppliers); - mutex_unlock(&wfs_lock); - /* * Delete all of the remaining links from this device to any other * devices (either consumers or suppliers). @@ -1764,7 +1700,6 @@ void device_initialize(struct device *dev) #endif INIT_LIST_HEAD(&dev->links.consumers); INIT_LIST_HEAD(&dev->links.suppliers); - INIT_LIST_HEAD(&dev->links.needs_suppliers); dev->links.status = DL_DEV_NO_DRIVER; } EXPORT_SYMBOL_GPL(device_initialize); @@ -2251,24 +2186,6 @@ int device_add(struct device *dev) BUS_NOTIFY_ADD_DEVICE, dev); kobject_uevent(&dev->kobj, KOBJ_ADD); - - /* - * Check if any of the other devices (consumers) have been waiting for - * this device (supplier) to be added so that they can create a device - * link to it. - * - * This needs to happen after device_pm_add() because device_link_add() - * requires the supplier be registered before it's called. - * - * But this also needs to happe before bus_probe_device() to make sure - * waiting consumers can link to it before the driver is bound to the - * device and the driver sync_state callback is called for this device. - */ - device_link_check_waiting_consumers(); - - if (dev->bus && dev->bus->add_links && dev->bus->add_links(dev)) - device_link_wait_for_supplier(dev); - bus_probe_device(dev); if (parent) klist_add_tail(&dev->p->knode_parent, diff --git a/include/linux/device.h b/include/linux/device.h index 73210745cc6b..ec598ede9455 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -78,17 +78,6 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *); * -EPROBE_DEFER it will queue the device for deferred probing. * @uevent: Called when a device is added, removed, or a few other things * that generate uevents to add the environment variables. - * @add_links: Called, perhaps multiple times per device, after a device is - * added to this bus. The function is expected to create device - * links to all the suppliers of the input device that are - * available at the time this function is called. As in, the - * function should NOT stop at the first failed device link if - * other unlinked supplier devices are present in the system. - * - * Return 0 if device links have been successfully created to all - * the suppliers of this device. Return an error if some of the - * suppliers are not yet available and this function needs to be - * reattempted in the future. * @probe: Called when a new device or driver add to this bus, and callback * the specific driver's probe to initial the matched device. * @remove: Called when a device removed from this bus. @@ -133,7 +122,6 @@ struct bus_type { int (*match)(struct device *dev, struct device_driver *drv); int (*uevent)(struct device *dev, struct kobj_uevent_env *env); - int (*add_links)(struct device *dev); int (*probe)(struct device *dev); int (*remove)(struct device *dev); void (*shutdown)(struct device *dev); @@ -1143,13 +1131,11 @@ enum dl_dev_state { * struct dev_links_info - Device data related to device links. * @suppliers: List of links to supplier devices. * @consumers: List of links to consumer devices. - * @needs_suppliers: Hook to global list of devices waiting for suppliers. * @status: Driver status information. */ struct dev_links_info { struct list_head suppliers; struct list_head consumers; - struct list_head needs_suppliers; enum dl_dev_state status; }; -- cgit v1.2.3 From 8973ea47901c81a1912bd05f1577bed9b5b52506 Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Wed, 28 Aug 2019 10:34:10 +0200 Subject: driver core: platform: Introduce platform_get_irq_optional() In some cases the interrupt line of a device is optional. Introduce a new platform_get_irq_optional() that works much like platform_get_irq() but does not output an error on failure to find the interrupt. Signed-off-by: Thierry Reding Reviewed-by: Stephen Boyd Link: https://lore.kernel.org/r/20190828083411.2496-1-thierry.reding@gmail.com Signed-off-by: Greg Kroah-Hartman --- drivers/base/platform.c | 22 ++++++++++++++++++++++ include/linux/platform_device.h | 1 + 2 files changed, 23 insertions(+) (limited to 'include') diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 8ad701068c11..0dda6ade50fd 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -192,6 +192,28 @@ int platform_get_irq(struct platform_device *dev, unsigned int num) } EXPORT_SYMBOL_GPL(platform_get_irq); +/** + * platform_get_irq_optional - get an optional IRQ for a device + * @dev: platform device + * @num: IRQ number index + * + * Gets an IRQ for a platform device. Device drivers should check the return + * value for errors so as to not pass a negative integer value to the + * request_irq() APIs. This is the same as platform_get_irq(), except that it + * does not print an error message if an IRQ can not be obtained. + * + * Example: + * int irq = platform_get_irq_optional(pdev, 0); + * if (irq < 0) + * return irq; + * + * Return: IRQ number on success, negative error number on failure. + */ +int platform_get_irq_optional(struct platform_device *dev, unsigned int num) +{ + return __platform_get_irq(dev, num); +} + /** * platform_irq_count - Count the number of IRQs a platform device uses * @dev: platform device diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h index 37e15a935a42..35bc4355a9df 100644 --- a/include/linux/platform_device.h +++ b/include/linux/platform_device.h @@ -58,6 +58,7 @@ extern void __iomem * devm_platform_ioremap_resource(struct platform_device *pdev, unsigned int index); extern int platform_get_irq(struct platform_device *, unsigned int); +extern int platform_get_irq_optional(struct platform_device *, unsigned int); extern int platform_irq_count(struct platform_device *); extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, -- cgit v1.2.3 From 7f905761e15a870761a7f9ba14bbb9e132ab8481 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Mon, 26 Aug 2019 17:01:53 +0200 Subject: sysfs: add BIN_ATTR_WO() macro This variant was missing from sysfs.h, I guess no one noticed it before. Turns out the powerpc secure variable code can use it, so add it to the tree for it, and potentially others to take advantage of, instead of open-coding it. Reported-by: Nayna Jain Signed-off-by: Greg Kroah-Hartman Link: https://lore.kernel.org/r/20190826150153.GD18418@kroah.com Signed-off-by: Greg Kroah-Hartman --- include/linux/sysfs.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include') diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 965236795750..5420817ed317 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -196,6 +196,12 @@ struct bin_attribute { .size = _size, \ } +#define __BIN_ATTR_WO(_name) { \ + .attr = { .name = __stringify(_name), .mode = 0200 }, \ + .store = _name##_store, \ + .size = _size, \ +} + #define __BIN_ATTR_RW(_name, _size) \ __BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size) @@ -208,6 +214,9 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \ #define BIN_ATTR_RO(_name, _size) \ struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size) +#define BIN_ATTR_WO(_name, _size) \ +struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size) + #define BIN_ATTR_RW(_name, _size) \ struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size) -- cgit v1.2.3 From 1d5a013f9c0f5b4ef3cf64ac61e9c1f386c7b750 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Mon, 19 Aug 2019 16:16:06 +0900 Subject: driver-core: add include guard to linux/container.h Add a header include guard just in case. Signed-off-by: Masahiro Yamada Reviewed-by: Rafael J. Wysocki Link: https://lore.kernel.org/r/20190819071606.10965-1-yamada.masahiro@socionext.com Signed-off-by: Greg Kroah-Hartman --- include/linux/container.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'include') diff --git a/include/linux/container.h b/include/linux/container.h index 0cc2ee91905c..2566a1baa736 100644 --- a/include/linux/container.h +++ b/include/linux/container.h @@ -6,6 +6,9 @@ * Author: Rafael J. Wysocki */ +#ifndef _LINUX_CONTAINER_H +#define _LINUX_CONTAINER_H + #include /* drivers/base/power/container.c */ @@ -20,3 +23,5 @@ static inline struct container_dev *to_container_dev(struct device *dev) { return container_of(dev, struct container_dev, dev); } + +#endif /* _LINUX_CONTAINER_H */ -- cgit v1.2.3