aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/core.c
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 /drivers/base/core.c
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>
Diffstat (limited to 'drivers/base/core.c')
-rw-r--r--drivers/base/core.c540
1 files changed, 540 insertions, 0 deletions
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);