aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>2016-10-30 12:32:16 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-10-31 13:36:20 -0400
commit9ed9895370aedd6032af2a9181c62c394d08223b (patch)
tree9cb133714d76646996248082a6231ab97103bf3a
parentbb41d2a51f0ed379cbe6b2f73271688784c40a1a (diff)
driver core: Functional dependencies tracking support
Currently, there is a problem with taking functional dependencies between devices into account. What I mean by a "functional dependency" is when the driver of device B needs device A to be functional and (generally) its driver to be present in order to work properly. This has certain consequences for power management (suspend/resume and runtime PM ordering) and shutdown ordering of these devices. In general, it also implies that the driver of A needs to be working for B to be probed successfully and it cannot be unbound from the device before the B's driver. Support for representing those functional dependencies between devices is added here to allow the driver core to track them and act on them in certain cases where applicable. The argument for doing that in the driver core is that there are quite a few distinct use cases involving device dependencies, they are relatively hard to get right in a driver (if one wants to address all of them properly) and it only gets worse if multiplied by the number of drivers potentially needing to do it. Morever, at least one case (asynchronous system suspend/resume) cannot be handled in a single driver at all, because it requires the driver of A to wait for B to suspend (during system suspend) and the driver of B to wait for A to resume (during system resume). For this reason, represent dependencies between devices as "links", with the help of struct device_link objects each containing pointers to the "linked" devices, a list node for each of them, status information, flags, and an RCU head for synchronization. Also add two new list heads, representing the lists of links to the devices that depend on the given one (consumers) and to the devices depended on by it (suppliers), and a "driver presence status" field (needed for figuring out initial states of device links) to struct device. The entire data structure consisting of all of the lists of link objects for all devices is protected by a mutex (for link object addition/removal and for list walks during device driver probing and removal) and by SRCU (for list walking in other case that will be introduced by subsequent change sets). If CONFIG_SRCU is not selected, however, an rwsem is used for protecting the entire data structure. In addition, each link object has an internal status field whose value reflects whether or not drivers are bound to the devices pointed to by the link or probing/removal of their drivers is in progress etc. That field is only modified under the device links mutex, but it may be read outside of it in some cases (introduced by subsequent change sets), so modifications of it are annotated with WRITE_ONCE(). New links are added by calling device_link_add() which takes three arguments: pointers to the devices in question and flags. In particular, if DL_FLAG_STATELESS is set in the flags, the link status is not to be taken into account for this link and the driver core will not manage it. In turn, if DL_FLAG_AUTOREMOVE is set in the flags, the driver core will remove the link automatically when the consumer device driver unbinds from it. One of the actions carried out by device_link_add() is to reorder the lists used for device shutdown and system suspend/resume to put the consumer device along with all of its children and all of its consumers (and so on, recursively) to the ends of those lists in order to ensure the right ordering between all of the supplier and consumer devices. For this reason, it is not possible to create a link between two devices if the would-be supplier device already depends on the would-be consumer device as either a direct descendant of it or a consumer of one of its direct descendants or one of its consumers and so on. There are two types of link objects, persistent and non-persistent. The persistent ones stay around until one of the target devices is deleted, while the non-persistent ones are removed automatically when the consumer driver unbinds from its device (ie. they are assumed to be valid only as long as the consumer device has a driver bound to it). Persistent links are created by default and non-persistent links are created when the DL_FLAG_AUTOREMOVE flag is passed to device_link_add(). Both persistent and non-persistent device links can be deleted with an explicit call to device_link_del(). Links created without the DL_FLAG_STATELESS flag set are managed by the driver core using a simple state machine. There are 5 states each link can be in: DORMANT (unused), AVAILABLE (the supplier driver is present and functional), CONSUMER_PROBE (the consumer driver is probing), ACTIVE (both supplier and consumer drivers are present and functional), and SUPPLIER_UNBIND (the supplier driver is unbinding). The driver core updates the link state automatically depending on what happens to the linked devices and for each link state specific actions are taken in addition to that. For example, if the supplier driver unbinds from its device, the driver core will also unbind the drivers of all of its consumers automatically under the assumption that they cannot function properly without the supplier. Analogously, the driver core will only allow the consumer driver to bind to its device if the supplier driver is present and functional (ie. the link is in the AVAILABLE state). If that's not the case, it will rely on the existing deferred probing mechanism to wait for the supplier driver to become available. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/base/base.h13
-rw-r--r--drivers/base/core.c540
-rw-r--r--drivers/base/dd.c41
-rw-r--r--drivers/base/power/main.c2
-rw-r--r--drivers/base/power/power.h10
-rw-r--r--include/linux/device.h80
-rw-r--r--include/linux/pm.h1
7 files changed, 682 insertions, 5 deletions
diff --git a/drivers/base/base.h b/drivers/base/base.h
index e05db388bd1c..e19b1008e5fb 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -107,6 +107,9 @@ extern void bus_remove_device(struct device *dev);
107 107
108extern int bus_add_driver(struct device_driver *drv); 108extern int bus_add_driver(struct device_driver *drv);
109extern void bus_remove_driver(struct device_driver *drv); 109extern void bus_remove_driver(struct device_driver *drv);
110extern void device_release_driver_internal(struct device *dev,
111 struct device_driver *drv,
112 struct device *parent);
110 113
111extern void driver_detach(struct device_driver *drv); 114extern void driver_detach(struct device_driver *drv);
112extern int driver_probe_device(struct device_driver *drv, struct device *dev); 115extern int driver_probe_device(struct device_driver *drv, struct device *dev);
@@ -152,3 +155,13 @@ extern int devtmpfs_init(void);
152#else 155#else
153static inline int devtmpfs_init(void) { return 0; } 156static inline int devtmpfs_init(void) { return 0; }
154#endif 157#endif
158
159/* Device links support */
160extern int device_links_read_lock(void);
161extern void device_links_read_unlock(int idx);
162extern int device_links_check_suppliers(struct device *dev);
163extern void device_links_driver_bound(struct device *dev);
164extern void device_links_driver_cleanup(struct device *dev);
165extern void device_links_no_driver(struct device *dev);
166extern bool device_links_busy(struct device *dev);
167extern void device_links_unbind_consumers(struct device *dev);
diff --git a/drivers/base/core.c b/drivers/base/core.c
index ce057a568673..3c5ff17f578f 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -44,6 +44,541 @@ static int __init sysfs_deprecated_setup(char *arg)
44early_param("sysfs.deprecated", sysfs_deprecated_setup); 44early_param("sysfs.deprecated", sysfs_deprecated_setup);
45#endif 45#endif
46 46
47/* Device links support. */
48
49#ifdef CONFIG_SRCU
50static DEFINE_MUTEX(device_links_lock);
51DEFINE_STATIC_SRCU(device_links_srcu);
52
53static inline void device_links_write_lock(void)
54{
55 mutex_lock(&device_links_lock);
56}
57
58static inline void device_links_write_unlock(void)
59{
60 mutex_unlock(&device_links_lock);
61}
62
63int device_links_read_lock(void)
64{
65 return srcu_read_lock(&device_links_srcu);
66}
67
68void device_links_read_unlock(int idx)
69{
70 srcu_read_unlock(&device_links_srcu, idx);
71}
72#else /* !CONFIG_SRCU */
73static DECLARE_RWSEM(device_links_lock);
74
75static inline void device_links_write_lock(void)
76{
77 down_write(&device_links_lock);
78}
79
80static inline void device_links_write_unlock(void)
81{
82 up_write(&device_links_lock);
83}
84
85int device_links_read_lock(void)
86{
87 down_read(&device_links_lock);
88 return 0;
89}
90
91void device_links_read_unlock(int not_used)
92{
93 up_read(&device_links_lock);
94}
95#endif /* !CONFIG_SRCU */
96
97/**
98 * device_is_dependent - Check if one device depends on another one
99 * @dev: Device to check dependencies for.
100 * @target: Device to check against.
101 *
102 * Check if @target depends on @dev or any device dependent on it (its child or
103 * its consumer etc). Return 1 if that is the case or 0 otherwise.
104 */
105static int device_is_dependent(struct device *dev, void *target)
106{
107 struct device_link *link;
108 int ret;
109
110 if (WARN_ON(dev == target))
111 return 1;
112
113 ret = device_for_each_child(dev, target, device_is_dependent);
114 if (ret)
115 return ret;
116
117 list_for_each_entry(link, &dev->links.consumers, s_node) {
118 if (WARN_ON(link->consumer == target))
119 return 1;
120
121 ret = device_is_dependent(link->consumer, target);
122 if (ret)
123 break;
124 }
125 return ret;
126}
127
128static int device_reorder_to_tail(struct device *dev, void *not_used)
129{
130 struct device_link *link;
131
132 /*
133 * Devices that have not been registered yet will be put to the ends
134 * of the lists during the registration, so skip them here.
135 */
136 if (device_is_registered(dev))
137 devices_kset_move_last(dev);
138
139 if (device_pm_initialized(dev))
140 device_pm_move_last(dev);
141
142 device_for_each_child(dev, NULL, device_reorder_to_tail);
143 list_for_each_entry(link, &dev->links.consumers, s_node)
144 device_reorder_to_tail(link->consumer, NULL);
145
146 return 0;
147}
148
149/**
150 * device_link_add - Create a link between two devices.
151 * @consumer: Consumer end of the link.
152 * @supplier: Supplier end of the link.
153 * @flags: Link flags.
154 *
155 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
156 * when the consumer device driver unbinds from it. The combination of both
157 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
158 * to be returned.
159 *
160 * A side effect of the link creation is re-ordering of dpm_list and the
161 * devices_kset list by moving the consumer device and all devices depending
162 * on it to the ends of these lists (that does not happen to devices that have
163 * not been registered when this function is called).
164 *
165 * The supplier device is required to be registered when this function is called
166 * and NULL will be returned if that is not the case. The consumer device need
167 * not be registerd, however.
168 */
169struct device_link *device_link_add(struct device *consumer,
170 struct device *supplier, u32 flags)
171{
172 struct device_link *link;
173
174 if (!consumer || !supplier ||
175 ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
176 return NULL;
177
178 device_links_write_lock();
179 device_pm_lock();
180
181 /*
182 * If the supplier has not been fully registered yet or there is a
183 * reverse dependency between the consumer and the supplier already in
184 * the graph, return NULL.
185 */
186 if (!device_pm_initialized(supplier)
187 || device_is_dependent(consumer, supplier)) {
188 link = NULL;
189 goto out;
190 }
191
192 list_for_each_entry(link, &supplier->links.consumers, s_node)
193 if (link->consumer == consumer)
194 goto out;
195
196 link = kmalloc(sizeof(*link), GFP_KERNEL);
197 if (!link)
198 goto out;
199
200 get_device(supplier);
201 link->supplier = supplier;
202 INIT_LIST_HEAD(&link->s_node);
203 get_device(consumer);
204 link->consumer = consumer;
205 INIT_LIST_HEAD(&link->c_node);
206 link->flags = flags;
207
208 /* Deterine the initial link state. */
209 if (flags & DL_FLAG_STATELESS) {
210 link->status = DL_STATE_NONE;
211 } else {
212 switch (supplier->links.status) {
213 case DL_DEV_DRIVER_BOUND:
214 switch (consumer->links.status) {
215 case DL_DEV_PROBING:
216 link->status = DL_STATE_CONSUMER_PROBE;
217 break;
218 case DL_DEV_DRIVER_BOUND:
219 link->status = DL_STATE_ACTIVE;
220 break;
221 default:
222 link->status = DL_STATE_AVAILABLE;
223 break;
224 }
225 break;
226 case DL_DEV_UNBINDING:
227 link->status = DL_STATE_SUPPLIER_UNBIND;
228 break;
229 default:
230 link->status = DL_STATE_DORMANT;
231 break;
232 }
233 }
234
235 /*
236 * Move the consumer and all of the devices depending on it to the end
237 * of dpm_list and the devices_kset list.
238 *
239 * It is necessary to hold dpm_list locked throughout all that or else
240 * we may end up suspending with a wrong ordering of it.
241 */
242 device_reorder_to_tail(consumer, NULL);
243
244 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
245 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
246
247 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
248
249 out:
250 device_pm_unlock();
251 device_links_write_unlock();
252 return link;
253}
254EXPORT_SYMBOL_GPL(device_link_add);
255
256static void device_link_free(struct device_link *link)
257{
258 put_device(link->consumer);
259 put_device(link->supplier);
260 kfree(link);
261}
262
263#ifdef CONFIG_SRCU
264static void __device_link_free_srcu(struct rcu_head *rhead)
265{
266 device_link_free(container_of(rhead, struct device_link, rcu_head));
267}
268
269static void __device_link_del(struct device_link *link)
270{
271 dev_info(link->consumer, "Dropping the link to %s\n",
272 dev_name(link->supplier));
273
274 list_del_rcu(&link->s_node);
275 list_del_rcu(&link->c_node);
276 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
277}
278#else /* !CONFIG_SRCU */
279static void __device_link_del(struct device_link *link)
280{
281 dev_info(link->consumer, "Dropping the link to %s\n",
282 dev_name(link->supplier));
283
284 list_del(&link->s_node);
285 list_del(&link->c_node);
286 device_link_free(link);
287}
288#endif /* !CONFIG_SRCU */
289
290/**
291 * device_link_del - Delete a link between two devices.
292 * @link: Device link to delete.
293 *
294 * The caller must ensure proper synchronization of this function with runtime
295 * PM.
296 */
297void device_link_del(struct device_link *link)
298{
299 device_links_write_lock();
300 device_pm_lock();
301 __device_link_del(link);
302 device_pm_unlock();
303 device_links_write_unlock();
304}
305EXPORT_SYMBOL_GPL(device_link_del);
306
307static void device_links_missing_supplier(struct device *dev)
308{
309 struct device_link *link;
310
311 list_for_each_entry(link, &dev->links.suppliers, c_node)
312 if (link->status == DL_STATE_CONSUMER_PROBE)
313 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
314}
315
316/**
317 * device_links_check_suppliers - Check presence of supplier drivers.
318 * @dev: Consumer device.
319 *
320 * Check links from this device to any suppliers. Walk the list of the device's
321 * links to suppliers and see if all of them are available. If not, simply
322 * return -EPROBE_DEFER.
323 *
324 * We need to guarantee that the supplier will not go away after the check has
325 * been positive here. It only can go away in __device_release_driver() and
326 * that function checks the device's links to consumers. This means we need to
327 * mark the link as "consumer probe in progress" to make the supplier removal
328 * wait for us to complete (or bad things may happen).
329 *
330 * Links with the DL_FLAG_STATELESS flag set are ignored.
331 */
332int device_links_check_suppliers(struct device *dev)
333{
334 struct device_link *link;
335 int ret = 0;
336
337 device_links_write_lock();
338
339 list_for_each_entry(link, &dev->links.suppliers, c_node) {
340 if (link->flags & DL_FLAG_STATELESS)
341 continue;
342
343 if (link->status != DL_STATE_AVAILABLE) {
344 device_links_missing_supplier(dev);
345 ret = -EPROBE_DEFER;
346 break;
347 }
348 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
349 }
350 dev->links.status = DL_DEV_PROBING;
351
352 device_links_write_unlock();
353 return ret;
354}
355
356/**
357 * device_links_driver_bound - Update device links after probing its driver.
358 * @dev: Device to update the links for.
359 *
360 * The probe has been successful, so update links from this device to any
361 * consumers by changing their status to "available".
362 *
363 * Also change the status of @dev's links to suppliers to "active".
364 *
365 * Links with the DL_FLAG_STATELESS flag set are ignored.
366 */
367void device_links_driver_bound(struct device *dev)
368{
369 struct device_link *link;
370
371 device_links_write_lock();
372
373 list_for_each_entry(link, &dev->links.consumers, s_node) {
374 if (link->flags & DL_FLAG_STATELESS)
375 continue;
376
377 WARN_ON(link->status != DL_STATE_DORMANT);
378 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
379 }
380
381 list_for_each_entry(link, &dev->links.suppliers, c_node) {
382 if (link->flags & DL_FLAG_STATELESS)
383 continue;
384
385 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
386 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
387 }
388
389 dev->links.status = DL_DEV_DRIVER_BOUND;
390
391 device_links_write_unlock();
392}
393
394/**
395 * __device_links_no_driver - Update links of a device without a driver.
396 * @dev: Device without a drvier.
397 *
398 * Delete all non-persistent links from this device to any suppliers.
399 *
400 * Persistent links stay around, but their status is changed to "available",
401 * unless they already are in the "supplier unbind in progress" state in which
402 * case they need not be updated.
403 *
404 * Links with the DL_FLAG_STATELESS flag set are ignored.
405 */
406static void __device_links_no_driver(struct device *dev)
407{
408 struct device_link *link, *ln;
409
410 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
411 if (link->flags & DL_FLAG_STATELESS)
412 continue;
413
414 if (link->flags & DL_FLAG_AUTOREMOVE)
415 __device_link_del(link);
416 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
417 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
418 }
419
420 dev->links.status = DL_DEV_NO_DRIVER;
421}
422
423void device_links_no_driver(struct device *dev)
424{
425 device_links_write_lock();
426 __device_links_no_driver(dev);
427 device_links_write_unlock();
428}
429
430/**
431 * device_links_driver_cleanup - Update links after driver removal.
432 * @dev: Device whose driver has just gone away.
433 *
434 * Update links to consumers for @dev by changing their status to "dormant" and
435 * invoke %__device_links_no_driver() to update links to suppliers for it as
436 * appropriate.
437 *
438 * Links with the DL_FLAG_STATELESS flag set are ignored.
439 */
440void device_links_driver_cleanup(struct device *dev)
441{
442 struct device_link *link;
443
444 device_links_write_lock();
445
446 list_for_each_entry(link, &dev->links.consumers, s_node) {
447 if (link->flags & DL_FLAG_STATELESS)
448 continue;
449
450 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
451 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
452 WRITE_ONCE(link->status, DL_STATE_DORMANT);
453 }
454
455 __device_links_no_driver(dev);
456
457 device_links_write_unlock();
458}
459
460/**
461 * device_links_busy - Check if there are any busy links to consumers.
462 * @dev: Device to check.
463 *
464 * Check each consumer of the device and return 'true' if its link's status
465 * is one of "consumer probe" or "active" (meaning that the given consumer is
466 * probing right now or its driver is present). Otherwise, change the link
467 * state to "supplier unbind" to prevent the consumer from being probed
468 * successfully going forward.
469 *
470 * Return 'false' if there are no probing or active consumers.
471 *
472 * Links with the DL_FLAG_STATELESS flag set are ignored.
473 */
474bool device_links_busy(struct device *dev)
475{
476 struct device_link *link;
477 bool ret = false;
478
479 device_links_write_lock();
480
481 list_for_each_entry(link, &dev->links.consumers, s_node) {
482 if (link->flags & DL_FLAG_STATELESS)
483 continue;
484
485 if (link->status == DL_STATE_CONSUMER_PROBE
486 || link->status == DL_STATE_ACTIVE) {
487 ret = true;
488 break;
489 }
490 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
491 }
492
493 dev->links.status = DL_DEV_UNBINDING;
494
495 device_links_write_unlock();
496 return ret;
497}
498
499/**
500 * device_links_unbind_consumers - Force unbind consumers of the given device.
501 * @dev: Device to unbind the consumers of.
502 *
503 * Walk the list of links to consumers for @dev and if any of them is in the
504 * "consumer probe" state, wait for all device probes in progress to complete
505 * and start over.
506 *
507 * If that's not the case, change the status of the link to "supplier unbind"
508 * and check if the link was in the "active" state. If so, force the consumer
509 * driver to unbind and start over (the consumer will not re-probe as we have
510 * changed the state of the link already).
511 *
512 * Links with the DL_FLAG_STATELESS flag set are ignored.
513 */
514void device_links_unbind_consumers(struct device *dev)
515{
516 struct device_link *link;
517
518 start:
519 device_links_write_lock();
520
521 list_for_each_entry(link, &dev->links.consumers, s_node) {
522 enum device_link_state status;
523
524 if (link->flags & DL_FLAG_STATELESS)
525 continue;
526
527 status = link->status;
528 if (status == DL_STATE_CONSUMER_PROBE) {
529 device_links_write_unlock();
530
531 wait_for_device_probe();
532 goto start;
533 }
534 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
535 if (status == DL_STATE_ACTIVE) {
536 struct device *consumer = link->consumer;
537
538 get_device(consumer);
539
540 device_links_write_unlock();
541
542 device_release_driver_internal(consumer, NULL,
543 consumer->parent);
544 put_device(consumer);
545 goto start;
546 }
547 }
548
549 device_links_write_unlock();
550}
551
552/**
553 * device_links_purge - Delete existing links to other devices.
554 * @dev: Target device.
555 */
556static void device_links_purge(struct device *dev)
557{
558 struct device_link *link, *ln;
559
560 /*
561 * Delete all of the remaining links from this device to any other
562 * devices (either consumers or suppliers).
563 */
564 device_links_write_lock();
565
566 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
567 WARN_ON(link->status == DL_STATE_ACTIVE);
568 __device_link_del(link);
569 }
570
571 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
572 WARN_ON(link->status != DL_STATE_DORMANT &&
573 link->status != DL_STATE_NONE);
574 __device_link_del(link);
575 }
576
577 device_links_write_unlock();
578}
579
580/* Device links support end. */
581
47int (*platform_notify)(struct device *dev) = NULL; 582int (*platform_notify)(struct device *dev) = NULL;
48int (*platform_notify_remove)(struct device *dev) = NULL; 583int (*platform_notify_remove)(struct device *dev) = NULL;
49static struct kobject *dev_kobj; 584static struct kobject *dev_kobj;
@@ -711,6 +1246,9 @@ void device_initialize(struct device *dev)
711#ifdef CONFIG_GENERIC_MSI_IRQ 1246#ifdef CONFIG_GENERIC_MSI_IRQ
712 INIT_LIST_HEAD(&dev->msi_list); 1247 INIT_LIST_HEAD(&dev->msi_list);
713#endif 1248#endif
1249 INIT_LIST_HEAD(&dev->links.consumers);
1250 INIT_LIST_HEAD(&dev->links.suppliers);
1251 dev->links.status = DL_DEV_NO_DRIVER;
714} 1252}
715EXPORT_SYMBOL_GPL(device_initialize); 1253EXPORT_SYMBOL_GPL(device_initialize);
716 1254
@@ -1258,6 +1796,8 @@ void device_del(struct device *dev)
1258 if (dev->bus) 1796 if (dev->bus)
1259 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1797 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1260 BUS_NOTIFY_DEL_DEVICE, dev); 1798 BUS_NOTIFY_DEL_DEVICE, dev);
1799
1800 device_links_purge(dev);
1261 dpm_sysfs_remove(dev); 1801 dpm_sysfs_remove(dev);
1262 if (parent) 1802 if (parent)
1263 klist_del(&dev->p->knode_parent); 1803 klist_del(&dev->p->knode_parent);
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index df4ab5509c04..b2bca3cf0dd2 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -244,6 +244,7 @@ static void driver_bound(struct device *dev)
244 __func__, dev_name(dev)); 244 __func__, dev_name(dev));
245 245
246 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices); 246 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
247 device_links_driver_bound(dev);
247 248
248 device_pm_check_callbacks(dev); 249 device_pm_check_callbacks(dev);
249 250
@@ -337,6 +338,10 @@ static int really_probe(struct device *dev, struct device_driver *drv)
337 return ret; 338 return ret;
338 } 339 }
339 340
341 ret = device_links_check_suppliers(dev);
342 if (ret)
343 return ret;
344
340 atomic_inc(&probe_count); 345 atomic_inc(&probe_count);
341 pr_debug("bus: '%s': %s: probing driver %s with device %s\n", 346 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
342 drv->bus->name, __func__, drv->name, dev_name(dev)); 347 drv->bus->name, __func__, drv->name, dev_name(dev));
@@ -415,6 +420,7 @@ probe_failed:
415 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 420 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
416 BUS_NOTIFY_DRIVER_NOT_BOUND, dev); 421 BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
417pinctrl_bind_failed: 422pinctrl_bind_failed:
423 device_links_no_driver(dev);
418 devres_release_all(dev); 424 devres_release_all(dev);
419 driver_sysfs_remove(dev); 425 driver_sysfs_remove(dev);
420 dev->driver = NULL; 426 dev->driver = NULL;
@@ -771,7 +777,7 @@ EXPORT_SYMBOL_GPL(driver_attach);
771 * __device_release_driver() must be called with @dev lock held. 777 * __device_release_driver() must be called with @dev lock held.
772 * When called for a USB interface, @dev->parent lock must be held as well. 778 * When called for a USB interface, @dev->parent lock must be held as well.
773 */ 779 */
774static void __device_release_driver(struct device *dev) 780static void __device_release_driver(struct device *dev, struct device *parent)
775{ 781{
776 struct device_driver *drv; 782 struct device_driver *drv;
777 783
@@ -780,6 +786,25 @@ static void __device_release_driver(struct device *dev)
780 if (driver_allows_async_probing(drv)) 786 if (driver_allows_async_probing(drv))
781 async_synchronize_full(); 787 async_synchronize_full();
782 788
789 while (device_links_busy(dev)) {
790 device_unlock(dev);
791 if (parent)
792 device_unlock(parent);
793
794 device_links_unbind_consumers(dev);
795 if (parent)
796 device_lock(parent);
797
798 device_lock(dev);
799 /*
800 * A concurrent invocation of the same function might
801 * have released the driver successfully while this one
802 * was waiting, so check for that.
803 */
804 if (dev->driver != drv)
805 return;
806 }
807
783 pm_runtime_get_sync(dev); 808 pm_runtime_get_sync(dev);
784 809
785 driver_sysfs_remove(dev); 810 driver_sysfs_remove(dev);
@@ -795,6 +820,8 @@ static void __device_release_driver(struct device *dev)
795 dev->bus->remove(dev); 820 dev->bus->remove(dev);
796 else if (drv->remove) 821 else if (drv->remove)
797 drv->remove(dev); 822 drv->remove(dev);
823
824 device_links_driver_cleanup(dev);
798 devres_release_all(dev); 825 devres_release_all(dev);
799 dev->driver = NULL; 826 dev->driver = NULL;
800 dev_set_drvdata(dev, NULL); 827 dev_set_drvdata(dev, NULL);
@@ -811,16 +838,16 @@ static void __device_release_driver(struct device *dev)
811 } 838 }
812} 839}
813 840
814static void device_release_driver_internal(struct device *dev, 841void device_release_driver_internal(struct device *dev,
815 struct device_driver *drv, 842 struct device_driver *drv,
816 struct device *parent) 843 struct device *parent)
817{ 844{
818 if (parent) 845 if (parent)
819 device_lock(parent); 846 device_lock(parent);
820 847
821 device_lock(dev); 848 device_lock(dev);
822 if (!drv || drv == dev->driver) 849 if (!drv || drv == dev->driver)
823 __device_release_driver(dev); 850 __device_release_driver(dev, parent);
824 851
825 device_unlock(dev); 852 device_unlock(dev);
826 if (parent) 853 if (parent)
@@ -833,6 +860,10 @@ static void device_release_driver_internal(struct device *dev,
833 * 860 *
834 * Manually detach device from driver. 861 * Manually detach device from driver.
835 * When called for a USB interface, @dev->parent lock must be held. 862 * When called for a USB interface, @dev->parent lock must be held.
863 *
864 * If this function is to be called with @dev->parent lock held, ensure that
865 * the device's consumers are unbound in advance or that their locks can be
866 * acquired under the @dev->parent lock.
836 */ 867 */
837void device_release_driver(struct device *dev) 868void device_release_driver(struct device *dev)
838{ 869{
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index e44944f4be77..420914061405 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -131,6 +131,7 @@ void device_pm_add(struct device *dev)
131 dev_warn(dev, "parent %s should not be sleeping\n", 131 dev_warn(dev, "parent %s should not be sleeping\n",
132 dev_name(dev->parent)); 132 dev_name(dev->parent));
133 list_add_tail(&dev->power.entry, &dpm_list); 133 list_add_tail(&dev->power.entry, &dpm_list);
134 dev->power.in_dpm_list = true;
134 mutex_unlock(&dpm_list_mtx); 135 mutex_unlock(&dpm_list_mtx);
135} 136}
136 137
@@ -145,6 +146,7 @@ void device_pm_remove(struct device *dev)
145 complete_all(&dev->power.completion); 146 complete_all(&dev->power.completion);
146 mutex_lock(&dpm_list_mtx); 147 mutex_lock(&dpm_list_mtx);
147 list_del_init(&dev->power.entry); 148 list_del_init(&dev->power.entry);
149 dev->power.in_dpm_list = false;
148 mutex_unlock(&dpm_list_mtx); 150 mutex_unlock(&dpm_list_mtx);
149 device_wakeup_disable(dev); 151 device_wakeup_disable(dev);
150 pm_runtime_remove(dev); 152 pm_runtime_remove(dev);
diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h
index 50e30e7b059d..0ba7842d665b 100644
--- a/drivers/base/power/power.h
+++ b/drivers/base/power/power.h
@@ -127,6 +127,11 @@ extern void device_pm_move_after(struct device *, struct device *);
127extern void device_pm_move_last(struct device *); 127extern void device_pm_move_last(struct device *);
128extern void device_pm_check_callbacks(struct device *dev); 128extern void device_pm_check_callbacks(struct device *dev);
129 129
130static inline bool device_pm_initialized(struct device *dev)
131{
132 return dev->power.in_dpm_list;
133}
134
130#else /* !CONFIG_PM_SLEEP */ 135#else /* !CONFIG_PM_SLEEP */
131 136
132static inline void device_pm_sleep_init(struct device *dev) {} 137static inline void device_pm_sleep_init(struct device *dev) {}
@@ -146,6 +151,11 @@ static inline void device_pm_move_last(struct device *dev) {}
146 151
147static inline void device_pm_check_callbacks(struct device *dev) {} 152static inline void device_pm_check_callbacks(struct device *dev) {}
148 153
154static inline bool device_pm_initialized(struct device *dev)
155{
156 return device_is_registered(dev);
157}
158
149#endif /* !CONFIG_PM_SLEEP */ 159#endif /* !CONFIG_PM_SLEEP */
150 160
151static inline void device_pm_init(struct device *dev) 161static inline void device_pm_init(struct device *dev)
diff --git a/include/linux/device.h b/include/linux/device.h
index bc41e87a969b..9cae2feaf5cb 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -708,6 +708,81 @@ struct device_dma_parameters {
708}; 708};
709 709
710/** 710/**
711 * enum device_link_state - Device link states.
712 * @DL_STATE_NONE: The presence of the drivers is not being tracked.
713 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
714 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
715 * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
716 * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
717 * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
718 */
719enum device_link_state {
720 DL_STATE_NONE = -1,
721 DL_STATE_DORMANT = 0,
722 DL_STATE_AVAILABLE,
723 DL_STATE_CONSUMER_PROBE,
724 DL_STATE_ACTIVE,
725 DL_STATE_SUPPLIER_UNBIND,
726};
727
728/*
729 * Device link flags.
730 *
731 * STATELESS: The core won't track the presence of supplier/consumer drivers.
732 * AUTOREMOVE: Remove this link automatically on consumer driver unbind.
733 */
734#define DL_FLAG_STATELESS BIT(0)
735#define DL_FLAG_AUTOREMOVE BIT(1)
736
737/**
738 * struct device_link - Device link representation.
739 * @supplier: The device on the supplier end of the link.
740 * @s_node: Hook to the supplier device's list of links to consumers.
741 * @consumer: The device on the consumer end of the link.
742 * @c_node: Hook to the consumer device's list of links to suppliers.
743 * @status: The state of the link (with respect to the presence of drivers).
744 * @flags: Link flags.
745 * @rcu_head: An RCU head to use for deferred execution of SRCU callbacks.
746 */
747struct device_link {
748 struct device *supplier;
749 struct list_head s_node;
750 struct device *consumer;
751 struct list_head c_node;
752 enum device_link_state status;
753 u32 flags;
754#ifdef CONFIG_SRCU
755 struct rcu_head rcu_head;
756#endif
757};
758
759/**
760 * enum dl_dev_state - Device driver presence tracking information.
761 * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
762 * @DL_DEV_PROBING: A driver is probing.
763 * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
764 * @DL_DEV_UNBINDING: The driver is unbinding from the device.
765 */
766enum dl_dev_state {
767 DL_DEV_NO_DRIVER = 0,
768 DL_DEV_PROBING,
769 DL_DEV_DRIVER_BOUND,
770 DL_DEV_UNBINDING,
771};
772
773/**
774 * struct dev_links_info - Device data related to device links.
775 * @suppliers: List of links to supplier devices.
776 * @consumers: List of links to consumer devices.
777 * @status: Driver status information.
778 */
779struct dev_links_info {
780 struct list_head suppliers;
781 struct list_head consumers;
782 enum dl_dev_state status;
783};
784
785/**
711 * struct device - The basic device structure 786 * struct device - The basic device structure
712 * @parent: The device's "parent" device, the device to which it is attached. 787 * @parent: The device's "parent" device, the device to which it is attached.
713 * In most cases, a parent device is some sort of bus or host 788 * In most cases, a parent device is some sort of bus or host
@@ -799,6 +874,7 @@ struct device {
799 core doesn't touch it */ 874 core doesn't touch it */
800 void *driver_data; /* Driver data, set and get with 875 void *driver_data; /* Driver data, set and get with
801 dev_set/get_drvdata */ 876 dev_set/get_drvdata */
877 struct dev_links_info links;
802 struct dev_pm_info power; 878 struct dev_pm_info power;
803 struct dev_pm_domain *pm_domain; 879 struct dev_pm_domain *pm_domain;
804 880
@@ -1116,6 +1192,10 @@ extern void device_shutdown(void);
1116/* debugging and troubleshooting/diagnostic helpers. */ 1192/* debugging and troubleshooting/diagnostic helpers. */
1117extern const char *dev_driver_string(const struct device *dev); 1193extern const char *dev_driver_string(const struct device *dev);
1118 1194
1195/* Device links interface. */
1196struct device_link *device_link_add(struct device *consumer,
1197 struct device *supplier, u32 flags);
1198void device_link_del(struct device_link *link);
1119 1199
1120#ifdef CONFIG_PRINTK 1200#ifdef CONFIG_PRINTK
1121 1201
diff --git a/include/linux/pm.h b/include/linux/pm.h
index 06eb353182ab..721a70241fcd 100644
--- a/include/linux/pm.h
+++ b/include/linux/pm.h
@@ -559,6 +559,7 @@ struct dev_pm_info {
559 pm_message_t power_state; 559 pm_message_t power_state;
560 unsigned int can_wakeup:1; 560 unsigned int can_wakeup:1;
561 unsigned int async_suspend:1; 561 unsigned int async_suspend:1;
562 bool in_dpm_list:1; /* Owned by the PM core */
562 bool is_prepared:1; /* Owned by the PM core */ 563 bool is_prepared:1; /* Owned by the PM core */
563 bool is_suspended:1; /* Ditto */ 564 bool is_suspended:1; /* Ditto */
564 bool is_noirq_suspended:1; 565 bool is_noirq_suspended:1;