2017-11-07 23:30:07 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 05:20:36 +07:00
|
|
|
/*
|
|
|
|
* drivers/base/core.c - core driver model code (device registration, etc)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-3 Patrick Mochel
|
|
|
|
* Copyright (c) 2002-3 Open Source Development Labs
|
2006-06-29 06:19:58 +07:00
|
|
|
* Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
|
|
|
|
* Copyright (c) 2006 Novell, Inc.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
|
2018-11-09 21:21:35 +07:00
|
|
|
#include <linux/acpi.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/err.h>
|
2015-04-04 04:23:37 +07:00
|
|
|
#include <linux/fwnode.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
2006-06-15 02:14:34 +07:00
|
|
|
#include <linux/kdev_t.h>
|
Driver core: add notification of bus events
I finally did as you suggested and added the notifier to the struct
bus_type itself. There are still problems to be expected is something
attaches to a bus type where the code can hook in different struct
device sub-classes (which is imho a big bogosity but I won't even try to
argue that case now) but it will solve nicely a number of issues I've
had so far.
That also means that clients interested in registering for such
notifications have to do it before devices are added and after bus types
are registered. Fortunately, most bus types that matter for the various
usage scenarios I have in mind are registerd at postcore_initcall time,
which means I have a really nice spot at arch_initcall time to add my
notifiers.
There are 4 notifications provided. Device being added (before hooked to
the bus) and removed (failure of previous case or after being unhooked
from the bus), along with driver being bound to a device and about to be
unbound.
The usage I have for these are:
- The 2 first ones are used to maintain a struct device_ext that is
hooked to struct device.firmware_data. This structure contains for now a
pointer to the Open Firmware node related to the device (if any), the
NUMA node ID (for quick access to it) and the DMA operations pointers &
iommu table instance for DMA to/from this device. For bus types I own
(like IBM VIO or EBUS), I just maintain that structure directly from the
bus code when creating the devices. But for bus types managed by generic
code like PCI or platform (actually, of_platform which is a variation of
platform linked to Open Firmware device-tree), I need this notifier.
- The other two ones have a completely different usage scenario. I have
cases where multiple devices and their drivers depend on each other. For
example, the IBM EMAC network driver needs to attach to a MAL DMA engine
which is a separate device, and a PHY interface which is also a separate
device. They are all of_platform_device's (well, about to be with my
upcoming patches) but there is no say in what precise order the core
will "probe" them and instanciate the various modules. The solution I
found for that is to have the drivers for emac to use multithread_probe,
and wait for a driver to be bound to the target MAL and PHY control
devices (the device-tree contains reference to the MAL and PHY interface
nodes, which I can then match to of_platform_devices). Right now, I've
been polling, but with that notifier, I can more cleanly wait (with a
timeout of course).
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2006-10-25 10:44:59 +07:00
|
|
|
#include <linux/notifier.h>
|
2012-02-02 01:22:22 +07:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2007-11-21 23:29:15 +07:00
|
|
|
#include <linux/genhd.h>
|
2008-05-28 23:28:39 +07:00
|
|
|
#include <linux/mutex.h>
|
PM / driver core: disable device's runtime PM during shutdown
There may be an issue when the user issue "reboot/shutdown" command, then
the device has shut down its hardware, after that, this runtime-pm featured
device's driver will probably be scheduled to do its suspend routine,
and at its suspend routine, it may access hardware, but the device has
already shutdown physically, then the system hang may be occurred.
I ran out this issue using an auto-suspend supported USB devices, like
3G modem, keyboard. The usb runtime suspend routine may be scheduled
after the usb controller has been shut down, and the usb runtime suspend
routine will try to suspend its roothub(controller), it will access
register, then the system hang occurs as the controller is shutdown.
Signed-off-by: Peter Chen <peter.chen@freescale.com>
Acked-by: Ming Lei <tom.leiming@gmail.com>
Acked-by: Greg Kroah-Hartman <gregkh@suse.de>
Cc: stable@kernel.org
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
2011-11-16 03:52:29 +07:00
|
|
|
#include <linux/pm_runtime.h>
|
2012-05-03 07:29:59 +07:00
|
|
|
#include <linux/netdevice.h>
|
2017-02-03 01:15:33 +07:00
|
|
|
#include <linux/sched/signal.h>
|
2013-08-28 00:24:15 +07:00
|
|
|
#include <linux/sysfs.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
#include "base.h"
|
|
|
|
#include "power/power.h"
|
|
|
|
|
2010-09-08 21:54:17 +07:00
|
|
|
#ifdef CONFIG_SYSFS_DEPRECATED
|
|
|
|
#ifdef CONFIG_SYSFS_DEPRECATED_V2
|
|
|
|
long sysfs_deprecated = 1;
|
|
|
|
#else
|
|
|
|
long sysfs_deprecated = 0;
|
|
|
|
#endif
|
2013-08-17 19:42:24 +07:00
|
|
|
static int __init sysfs_deprecated_setup(char *arg)
|
2010-09-08 21:54:17 +07:00
|
|
|
{
|
2013-07-26 11:10:22 +07:00
|
|
|
return kstrtol(arg, 10, &sysfs_deprecated);
|
2010-09-08 21:54:17 +07:00
|
|
|
}
|
|
|
|
early_param("sysfs.deprecated", sysfs_deprecated_setup);
|
|
|
|
#endif
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
/* Device links support. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SRCU
|
|
|
|
static DEFINE_MUTEX(device_links_lock);
|
|
|
|
DEFINE_STATIC_SRCU(device_links_srcu);
|
|
|
|
|
|
|
|
static inline void device_links_write_lock(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&device_links_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void device_links_write_unlock(void)
|
|
|
|
{
|
|
|
|
mutex_unlock(&device_links_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int device_links_read_lock(void)
|
|
|
|
{
|
|
|
|
return srcu_read_lock(&device_links_srcu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void device_links_read_unlock(int idx)
|
|
|
|
{
|
|
|
|
srcu_read_unlock(&device_links_srcu, idx);
|
|
|
|
}
|
|
|
|
#else /* !CONFIG_SRCU */
|
|
|
|
static DECLARE_RWSEM(device_links_lock);
|
|
|
|
|
|
|
|
static inline void device_links_write_lock(void)
|
|
|
|
{
|
|
|
|
down_write(&device_links_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void device_links_write_unlock(void)
|
|
|
|
{
|
|
|
|
up_write(&device_links_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int device_links_read_lock(void)
|
|
|
|
{
|
|
|
|
down_read(&device_links_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void device_links_read_unlock(int not_used)
|
|
|
|
{
|
|
|
|
up_read(&device_links_lock);
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_SRCU */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_is_dependent - Check if one device depends on another one
|
|
|
|
* @dev: Device to check dependencies for.
|
|
|
|
* @target: Device to check against.
|
|
|
|
*
|
|
|
|
* Check if @target depends on @dev or any device dependent on it (its child or
|
|
|
|
* its consumer etc). Return 1 if that is the case or 0 otherwise.
|
|
|
|
*/
|
|
|
|
static int device_is_dependent(struct device *dev, void *target)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
int ret;
|
|
|
|
|
2018-07-16 18:37:44 +07:00
|
|
|
if (dev == target)
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
ret = device_for_each_child(dev, target, device_is_dependent);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.consumers, s_node) {
|
2018-07-16 18:37:44 +07:00
|
|
|
if (link->consumer == target)
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
ret = device_is_dependent(link->consumer, target);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int device_reorder_to_tail(struct device *dev, void *not_used)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Devices that have not been registered yet will be put to the ends
|
|
|
|
* of the lists during the registration, so skip them here.
|
|
|
|
*/
|
|
|
|
if (device_is_registered(dev))
|
|
|
|
devices_kset_move_last(dev);
|
|
|
|
|
|
|
|
if (device_pm_initialized(dev))
|
|
|
|
device_pm_move_last(dev);
|
|
|
|
|
|
|
|
device_for_each_child(dev, NULL, device_reorder_to_tail);
|
|
|
|
list_for_each_entry(link, &dev->links.consumers, s_node)
|
|
|
|
device_reorder_to_tail(link->consumer, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-11 06:57:06 +07:00
|
|
|
/**
|
|
|
|
* device_pm_move_to_tail - Move set of devices to the end of device lists
|
|
|
|
* @dev: Device to move
|
|
|
|
*
|
|
|
|
* This is a device_reorder_to_tail() wrapper taking the requisite locks.
|
|
|
|
*
|
|
|
|
* It moves the @dev along with all of its children and all of its consumers
|
|
|
|
* to the ends of the device_kset and dpm_list, recursively.
|
|
|
|
*/
|
|
|
|
void device_pm_move_to_tail(struct device *dev)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = device_links_read_lock();
|
|
|
|
device_pm_lock();
|
|
|
|
device_reorder_to_tail(dev, NULL);
|
|
|
|
device_pm_unlock();
|
|
|
|
device_links_read_unlock(idx);
|
|
|
|
}
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
/**
|
|
|
|
* device_link_add - Create a link between two devices.
|
|
|
|
* @consumer: Consumer end of the link.
|
|
|
|
* @supplier: Supplier end of the link.
|
|
|
|
* @flags: Link flags.
|
|
|
|
*
|
2016-10-30 23:32:31 +07:00
|
|
|
* The caller is responsible for the proper synchronization of the link creation
|
|
|
|
* with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
|
|
|
|
* runtime PM framework to take the link into account. Second, if the
|
|
|
|
* DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
|
|
|
|
* be forced into the active metastate and reference-counted upon the creation
|
|
|
|
* of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
|
|
|
|
* ignored.
|
|
|
|
*
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
* If DL_FLAG_STATELESS is set in @flags, the link is not going to be managed by
|
|
|
|
* the driver core and, in particular, the caller of this function is expected
|
|
|
|
* to drop the reference to the link acquired by it directly.
|
|
|
|
*
|
|
|
|
* If that flag is not set, however, the caller of this function is handing the
|
|
|
|
* management of the link over to the driver core entirely and its return value
|
|
|
|
* can only be used to check whether or not the link is present. In that case,
|
|
|
|
* the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
|
|
|
|
* flags can be used to indicate to the driver core when the link can be safely
|
|
|
|
* deleted. Namely, setting one of them in @flags indicates to the driver core
|
|
|
|
* that the link is not going to be used (by the given caller of this function)
|
|
|
|
* after unbinding the consumer or supplier driver, respectively, from its
|
|
|
|
* device, so the link can be deleted at that point. If none of them is set,
|
|
|
|
* the link will be maintained until one of the devices pointed to by it (either
|
|
|
|
* the consumer or the supplier) is unregistered.
|
2019-02-01 07:45:55 +07:00
|
|
|
*
|
2019-02-01 07:59:42 +07:00
|
|
|
* Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
|
|
|
|
* DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
|
|
|
|
* managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
|
|
|
|
* be used to request the driver core to automaticall probe for a consmer
|
|
|
|
* driver after successfully binding a driver to the supplier device.
|
|
|
|
*
|
2019-02-01 07:45:55 +07:00
|
|
|
* The combination of DL_FLAG_STATELESS and either DL_FLAG_AUTOREMOVE_CONSUMER
|
|
|
|
* or DL_FLAG_AUTOREMOVE_SUPPLIER set in @flags at the same time is invalid and
|
|
|
|
* will cause NULL to be returned upfront.
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
*
|
|
|
|
* A side effect of the link creation is re-ordering of dpm_list and the
|
|
|
|
* devices_kset list by moving the consumer device and all devices depending
|
|
|
|
* on it to the ends of these lists (that does not happen to devices that have
|
|
|
|
* not been registered when this function is called).
|
|
|
|
*
|
|
|
|
* The supplier device is required to be registered when this function is called
|
|
|
|
* and NULL will be returned if that is not the case. The consumer device need
|
2016-12-04 19:10:04 +07:00
|
|
|
* not be registered, however.
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
*/
|
|
|
|
struct device_link *device_link_add(struct device *consumer,
|
|
|
|
struct device *supplier, u32 flags)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
|
|
|
|
if (!consumer || !supplier ||
|
2019-02-01 07:45:55 +07:00
|
|
|
(flags & DL_FLAG_STATELESS &&
|
2019-02-01 07:59:42 +07:00
|
|
|
flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
|
|
|
|
DL_FLAG_AUTOREMOVE_SUPPLIER |
|
|
|
|
DL_FLAG_AUTOPROBE_CONSUMER)) ||
|
|
|
|
(flags & DL_FLAG_AUTOPROBE_CONSUMER &&
|
|
|
|
flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
|
|
|
|
DL_FLAG_AUTOREMOVE_SUPPLIER)))
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
return NULL;
|
|
|
|
|
2019-02-01 07:47:53 +07:00
|
|
|
if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
|
|
|
|
if (pm_runtime_get_sync(supplier) < 0) {
|
|
|
|
pm_runtime_put_noidle(supplier);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
device_links_write_lock();
|
|
|
|
device_pm_lock();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the supplier has not been fully registered yet or there is a
|
|
|
|
* reverse dependency between the consumer and the supplier already in
|
|
|
|
* the graph, return NULL.
|
|
|
|
*/
|
|
|
|
if (!device_pm_initialized(supplier)
|
|
|
|
|| device_is_dependent(consumer, supplier)) {
|
|
|
|
link = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
/*
|
|
|
|
* DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
|
|
|
|
* longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
|
|
|
|
* together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
|
|
|
|
*/
|
|
|
|
if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
|
|
|
|
flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
|
|
|
|
|
driver core: Avoid careless re-use of existing device links
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally. However, if the flags passed to
it on the second (or any subsequent) attempt to create a device
link between the same consumer-supplier pair are not compatible with
the existing link's flags, that is incorrect.
First off, if the existing link is stateless and the next caller of
device_link_add() for the same consumer-supplier pair wants a
stateful one, or the other way around, the existing link cannot be
returned, because it will not match the expected behavior, so make
device_link_add() dump the stack and return NULL in that case.
Moreover, if the DL_FLAG_AUTOREMOVE_CONSUMER flag is passed to
device_link_add(), its caller will expect its reference to the link
to be dropped automatically on consumer driver removal, which will
not happen if that flag is not set in the link's flags (and
analogously for DL_FLAG_AUTOREMOVE_SUPPLIER). For this reason, make
device_link_add() update the existing link's flags accordingly
before returning it to the caller.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:46:54 +07:00
|
|
|
list_for_each_entry(link, &supplier->links.consumers, s_node) {
|
|
|
|
if (link->consumer != consumer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't return a stateless link if the caller wants a stateful
|
|
|
|
* one and vice versa.
|
|
|
|
*/
|
|
|
|
if (WARN_ON((flags & DL_FLAG_STATELESS) != (link->flags & DL_FLAG_STATELESS))) {
|
|
|
|
link = NULL;
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
goto out;
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
}
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
if (flags & DL_FLAG_PM_RUNTIME) {
|
|
|
|
if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
|
driver core: Fix possible supplier PM-usage counter imbalance
If a stateless device link to a certain supplier with
DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the
consumer driver's probe callback, the supplier's PM-runtime usage
counter will be nonzero after that which effectively causes the
supplier to remain "always on" going forward.
Namely, device_link_add() called to add the link invokes
device_link_rpm_prepare() which notices that the consumer driver is
probing, so it increments the supplier's PM-runtime usage counter
with the assumption that the link will stay around until
pm_runtime_put_suppliers() is called by driver_probe_device(),
but if the link goes away before that point, the supplier's
PM-runtime usage counter will remain nonzero.
To prevent that from happening, first rework pm_runtime_get_suppliers()
and pm_runtime_put_suppliers() to use the rpm_active refounts of device
links and make the latter only drop rpm_active and the supplier's
PM-runtime usage counter for each link by one, unless rpm_active is
one already for it. Next, modify device_link_add() to bump up the
new link's rpm_active refcount and the suppliers PM-runtime usage
counter by two, to prevent pm_runtime_put_suppliers(), if it is
called subsequently, from suspending the supplier prematurely (in
case its PM-runtime usage counter goes down to 0 in there).
Due to the way rpm_put_suppliers() works, this change does not
affect runtime suspend of the consumer ends of new device links (or,
generally, device links for which DL_FLAG_PM_RUNTIME has just been
set).
Fixes: e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Reported-by: Ulf Hansson <ulf.hansson@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Ulf Hansson <ulf.hansson@linaro.org>
Tested-by: Ulf Hansson <ulf.hansson@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-12 19:08:10 +07:00
|
|
|
pm_runtime_new_link(consumer);
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
link->flags |= DL_FLAG_PM_RUNTIME;
|
|
|
|
}
|
|
|
|
if (flags & DL_FLAG_RPM_ACTIVE)
|
2019-02-19 23:53:26 +07:00
|
|
|
refcount_inc(&link->rpm_active);
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
}
|
|
|
|
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
if (flags & DL_FLAG_STATELESS) {
|
|
|
|
kref_get(&link->kref);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the life time of the link following from the new flags is
|
|
|
|
* longer than indicated by the flags of the existing link,
|
|
|
|
* update the existing link to stay around longer.
|
|
|
|
*/
|
|
|
|
if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
|
|
|
|
if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
|
|
|
|
link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
|
|
|
|
link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
|
|
|
|
}
|
|
|
|
} else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
|
|
|
|
link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
|
|
|
|
DL_FLAG_AUTOREMOVE_SUPPLIER);
|
|
|
|
}
|
driver core: Avoid careless re-use of existing device links
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally. However, if the flags passed to
it on the second (or any subsequent) attempt to create a device
link between the same consumer-supplier pair are not compatible with
the existing link's flags, that is incorrect.
First off, if the existing link is stateless and the next caller of
device_link_add() for the same consumer-supplier pair wants a
stateful one, or the other way around, the existing link cannot be
returned, because it will not match the expected behavior, so make
device_link_add() dump the stack and return NULL in that case.
Moreover, if the DL_FLAG_AUTOREMOVE_CONSUMER flag is passed to
device_link_add(), its caller will expect its reference to the link
to be dropped automatically on consumer driver removal, which will
not happen if that flag is not set in the link's flags (and
analogously for DL_FLAG_AUTOREMOVE_SUPPLIER). For this reason, make
device_link_add() update the existing link's flags accordingly
before returning it to the caller.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:46:54 +07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-10-30 23:32:31 +07:00
|
|
|
link = kzalloc(sizeof(*link), GFP_KERNEL);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
if (!link)
|
|
|
|
goto out;
|
|
|
|
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
refcount_set(&link->rpm_active, 1);
|
|
|
|
|
2016-10-30 23:32:43 +07:00
|
|
|
if (flags & DL_FLAG_PM_RUNTIME) {
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
if (flags & DL_FLAG_RPM_ACTIVE)
|
2019-02-19 23:53:26 +07:00
|
|
|
refcount_inc(&link->rpm_active);
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
|
driver core: Fix possible supplier PM-usage counter imbalance
If a stateless device link to a certain supplier with
DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the
consumer driver's probe callback, the supplier's PM-runtime usage
counter will be nonzero after that which effectively causes the
supplier to remain "always on" going forward.
Namely, device_link_add() called to add the link invokes
device_link_rpm_prepare() which notices that the consumer driver is
probing, so it increments the supplier's PM-runtime usage counter
with the assumption that the link will stay around until
pm_runtime_put_suppliers() is called by driver_probe_device(),
but if the link goes away before that point, the supplier's
PM-runtime usage counter will remain nonzero.
To prevent that from happening, first rework pm_runtime_get_suppliers()
and pm_runtime_put_suppliers() to use the rpm_active refounts of device
links and make the latter only drop rpm_active and the supplier's
PM-runtime usage counter for each link by one, unless rpm_active is
one already for it. Next, modify device_link_add() to bump up the
new link's rpm_active refcount and the suppliers PM-runtime usage
counter by two, to prevent pm_runtime_put_suppliers(), if it is
called subsequently, from suspending the supplier prematurely (in
case its PM-runtime usage counter goes down to 0 in there).
Due to the way rpm_put_suppliers() works, this change does not
affect runtime suspend of the consumer ends of new device links (or,
generally, device links for which DL_FLAG_PM_RUNTIME has just been
set).
Fixes: e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Reported-by: Ulf Hansson <ulf.hansson@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Ulf Hansson <ulf.hansson@linaro.org>
Tested-by: Ulf Hansson <ulf.hansson@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-12 19:08:10 +07:00
|
|
|
pm_runtime_new_link(consumer);
|
2016-10-30 23:32:31 +07:00
|
|
|
}
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
get_device(supplier);
|
|
|
|
link->supplier = supplier;
|
|
|
|
INIT_LIST_HEAD(&link->s_node);
|
|
|
|
get_device(consumer);
|
|
|
|
link->consumer = consumer;
|
|
|
|
INIT_LIST_HEAD(&link->c_node);
|
|
|
|
link->flags = flags;
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
kref_init(&link->kref);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
|
2016-12-04 19:10:04 +07:00
|
|
|
/* Determine the initial link state. */
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
if (flags & DL_FLAG_STATELESS) {
|
|
|
|
link->status = DL_STATE_NONE;
|
|
|
|
} else {
|
|
|
|
switch (supplier->links.status) {
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
case DL_DEV_PROBING:
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
switch (consumer->links.status) {
|
|
|
|
case DL_DEV_PROBING:
|
2016-10-30 23:32:31 +07:00
|
|
|
/*
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
* A consumer driver can create a link to a
|
|
|
|
* supplier that has not completed its probing
|
|
|
|
* yet as long as it knows that the supplier is
|
|
|
|
* already functional (for example, it has just
|
|
|
|
* acquired some resources from the supplier).
|
2016-10-30 23:32:31 +07:00
|
|
|
*/
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
link->status = DL_STATE_CONSUMER_PROBE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
link->status = DL_STATE_DORMANT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DL_DEV_DRIVER_BOUND:
|
|
|
|
switch (consumer->links.status) {
|
|
|
|
case DL_DEV_PROBING:
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
link->status = DL_STATE_CONSUMER_PROBE;
|
|
|
|
break;
|
|
|
|
case DL_DEV_DRIVER_BOUND:
|
|
|
|
link->status = DL_STATE_ACTIVE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
link->status = DL_STATE_AVAILABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DL_DEV_UNBINDING:
|
|
|
|
link->status = DL_STATE_SUPPLIER_UNBIND;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
link->status = DL_STATE_DORMANT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
/*
|
|
|
|
* Some callers expect the link creation during consumer driver probe to
|
|
|
|
* resume the supplier even without DL_FLAG_RPM_ACTIVE.
|
|
|
|
*/
|
|
|
|
if (link->status == DL_STATE_CONSUMER_PROBE &&
|
|
|
|
flags & DL_FLAG_PM_RUNTIME)
|
|
|
|
pm_runtime_resume(supplier);
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
/*
|
|
|
|
* Move the consumer and all of the devices depending on it to the end
|
|
|
|
* of dpm_list and the devices_kset list.
|
|
|
|
*
|
|
|
|
* It is necessary to hold dpm_list locked throughout all that or else
|
|
|
|
* we may end up suspending with a wrong ordering of it.
|
|
|
|
*/
|
|
|
|
device_reorder_to_tail(consumer, NULL);
|
|
|
|
|
|
|
|
list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
|
|
|
|
list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
|
|
|
|
|
2018-12-21 23:23:41 +07:00
|
|
|
dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
|
|
|
|
out:
|
|
|
|
device_pm_unlock();
|
|
|
|
device_links_write_unlock();
|
2019-02-01 07:47:53 +07:00
|
|
|
|
driver core: Fix handling of runtime PM flags in device_link_add()
After commit ead18c23c263 ("driver core: Introduce device links
reference counting"), if there is a link between the given supplier
and the given consumer already, device_link_add() will refcount it
and return it unconditionally without updating its flags. It is
possible, however, that the second (or any subsequent) caller of
device_link_add() for the same consumer-supplier pair will pass
DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
to it and the existing link may not behave as expected then.
First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
at all, it needs to be set like during the original initialization of
the link.
Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
(in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
updated to reflect the "active" runtime PM configuration of the
consumer-supplier pair and extra care must be taken here to avoid
possible destructive races with runtime PM of the consumer.
To that end, redefine the rpm_active field in struct device_link
as a refcount, initialize it to 1 and make rpm_resume() (for the
consumer) and device_link_add() increment it whenever they acquire
a runtime PM reference on the supplier device. Accordingly, make
rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
decrement it and drop runtime PM references to the supplier
device in a loop until rpm_active becones 1 again.
Fixes: ead18c23c263 ("driver core: Introduce device links reference counting")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:49:14 +07:00
|
|
|
if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
|
2019-02-01 07:47:53 +07:00
|
|
|
pm_runtime_put(supplier);
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
return link;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_link_add);
|
|
|
|
|
|
|
|
static void device_link_free(struct device_link *link)
|
|
|
|
{
|
2019-02-01 07:52:45 +07:00
|
|
|
while (refcount_dec_not_one(&link->rpm_active))
|
|
|
|
pm_runtime_put(link->supplier);
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
put_device(link->consumer);
|
|
|
|
put_device(link->supplier);
|
|
|
|
kfree(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SRCU
|
|
|
|
static void __device_link_free_srcu(struct rcu_head *rhead)
|
|
|
|
{
|
|
|
|
device_link_free(container_of(rhead, struct device_link, rcu_head));
|
|
|
|
}
|
|
|
|
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
static void __device_link_del(struct kref *kref)
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
{
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
struct device_link *link = container_of(kref, struct device_link, kref);
|
|
|
|
|
2018-12-21 23:23:41 +07:00
|
|
|
dev_dbg(link->consumer, "Dropping the link to %s\n",
|
|
|
|
dev_name(link->supplier));
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
|
2016-10-30 23:32:43 +07:00
|
|
|
if (link->flags & DL_FLAG_PM_RUNTIME)
|
|
|
|
pm_runtime_drop_link(link->consumer);
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
list_del_rcu(&link->s_node);
|
|
|
|
list_del_rcu(&link->c_node);
|
|
|
|
call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
|
|
|
|
}
|
|
|
|
#else /* !CONFIG_SRCU */
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
static void __device_link_del(struct kref *kref)
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
{
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
struct device_link *link = container_of(kref, struct device_link, kref);
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
dev_info(link->consumer, "Dropping the link to %s\n",
|
|
|
|
dev_name(link->supplier));
|
|
|
|
|
2018-02-11 01:13:58 +07:00
|
|
|
if (link->flags & DL_FLAG_PM_RUNTIME)
|
|
|
|
pm_runtime_drop_link(link->consumer);
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
list_del(&link->s_node);
|
|
|
|
list_del(&link->c_node);
|
|
|
|
device_link_free(link);
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_SRCU */
|
|
|
|
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
static void device_link_put_kref(struct device_link *link)
|
|
|
|
{
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
kref_put(&link->kref, __device_link_del);
|
|
|
|
else
|
|
|
|
WARN(1, "Unable to drop a managed device link reference\n");
|
|
|
|
}
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
/**
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
* device_link_del - Delete a stateless link between two devices.
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
* @link: Device link to delete.
|
|
|
|
*
|
|
|
|
* The caller must ensure proper synchronization of this function with runtime
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
* PM. If the link was added multiple times, it needs to be deleted as often.
|
|
|
|
* Care is required for hotplugged devices: Their links are purged on removal
|
|
|
|
* and calling device_link_del() is then no longer allowed.
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
*/
|
|
|
|
void device_link_del(struct device_link *link)
|
|
|
|
{
|
|
|
|
device_links_write_lock();
|
|
|
|
device_pm_lock();
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
device_link_put_kref(link);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
device_pm_unlock();
|
|
|
|
device_links_write_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_link_del);
|
|
|
|
|
2018-07-05 21:25:56 +07:00
|
|
|
/**
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
* device_link_remove - Delete a stateless link between two devices.
|
2018-07-05 21:25:56 +07:00
|
|
|
* @consumer: Consumer end of the link.
|
|
|
|
* @supplier: Supplier end of the link.
|
|
|
|
*
|
|
|
|
* The caller must ensure proper synchronization of this function with runtime
|
|
|
|
* PM.
|
|
|
|
*/
|
|
|
|
void device_link_remove(void *consumer, struct device *supplier)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
|
|
|
|
if (WARN_ON(consumer == supplier))
|
|
|
|
return;
|
|
|
|
|
|
|
|
device_links_write_lock();
|
|
|
|
device_pm_lock();
|
|
|
|
|
|
|
|
list_for_each_entry(link, &supplier->links.consumers, s_node) {
|
|
|
|
if (link->consumer == consumer) {
|
driver core: Make driver core own stateful device links
Even though stateful device links are managed by the driver core in
principle, their creators are allowed and sometimes even expected
to drop references to them via device_link_del() or
device_link_remove(), but that doesn't really play well with the
"persistent" link concept.
If "persistent" managed device links are created from driver
probe callbacks, device_link_add() called to do that will take a
new reference on the link each time the callback runs and those
references will never be dropped, which kind of isn't nice.
This issues arises because of the link reference counting carried
out by device_link_add() for existing links, but that is only done to
avoid deleting device links that may still be necessary, which
shouldn't be a concern for managed (stateful) links. These device
links are managed by the driver core and whoever creates one of them
will need it at least as long as until the consumer driver is detached
from its device and deleting it may be left to the driver core just
fine.
For this reason, rework device_link_add() to apply the reference
counting to stateless links only and make device_link_del() and
device_link_remove() drop references to stateless links only too.
After this change, if called to add a stateful device link for
a consumer-supplier pair for which a stateful device link is
present already, device_link_add() will return the existing link
without incrementing its reference counter. Accordingly,
device_link_del() and device_link_remove() will WARN() and do
nothing when called to drop a reference to a stateful link. Thus,
effectively, all stateful device links will be owned by the driver
core.
In addition, clean up the handling of the link management flags,
DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
(a) they are never set at the same time and (b) if device_link_add()
is called for a consumer-supplier pair with an existing stateful link
between them, the flags of that link will be combined with the flags
passed to device_link_add() to ensure that the life time of the link
is sufficient for all of the callers of device_link_add() for the
same consumer-supplier pair.
Update the device_link_add() kerneldoc comment to reflect the
above changes.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:58:33 +07:00
|
|
|
device_link_put_kref(link);
|
2018-07-05 21:25:56 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device_pm_unlock();
|
|
|
|
device_links_write_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_link_remove);
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
static void device_links_missing_supplier(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.suppliers, c_node)
|
|
|
|
if (link->status == DL_STATE_CONSUMER_PROBE)
|
|
|
|
WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_links_check_suppliers - Check presence of supplier drivers.
|
|
|
|
* @dev: Consumer device.
|
|
|
|
*
|
|
|
|
* Check links from this device to any suppliers. Walk the list of the device's
|
|
|
|
* links to suppliers and see if all of them are available. If not, simply
|
|
|
|
* return -EPROBE_DEFER.
|
|
|
|
*
|
|
|
|
* We need to guarantee that the supplier will not go away after the check has
|
|
|
|
* been positive here. It only can go away in __device_release_driver() and
|
|
|
|
* that function checks the device's links to consumers. This means we need to
|
|
|
|
* mark the link as "consumer probe in progress" to make the supplier removal
|
|
|
|
* wait for us to complete (or bad things may happen).
|
|
|
|
*
|
|
|
|
* Links with the DL_FLAG_STATELESS flag set are ignored.
|
|
|
|
*/
|
|
|
|
int device_links_check_suppliers(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
device_links_write_lock();
|
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.suppliers, c_node) {
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (link->status != DL_STATE_AVAILABLE) {
|
|
|
|
device_links_missing_supplier(dev);
|
|
|
|
ret = -EPROBE_DEFER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
|
|
|
|
}
|
|
|
|
dev->links.status = DL_DEV_PROBING;
|
|
|
|
|
|
|
|
device_links_write_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_links_driver_bound - Update device links after probing its driver.
|
|
|
|
* @dev: Device to update the links for.
|
|
|
|
*
|
|
|
|
* The probe has been successful, so update links from this device to any
|
|
|
|
* consumers by changing their status to "available".
|
|
|
|
*
|
|
|
|
* Also change the status of @dev's links to suppliers to "active".
|
|
|
|
*
|
|
|
|
* Links with the DL_FLAG_STATELESS flag set are ignored.
|
|
|
|
*/
|
|
|
|
void device_links_driver_bound(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
|
|
|
|
device_links_write_lock();
|
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.consumers, s_node) {
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
/*
|
|
|
|
* Links created during consumer probe may be in the "consumer
|
|
|
|
* probe" state to start with if the supplier is still probing
|
|
|
|
* when they are created and they may become "active" if the
|
|
|
|
* consumer probe returns first. Skip them here.
|
|
|
|
*/
|
|
|
|
if (link->status == DL_STATE_CONSUMER_PROBE ||
|
|
|
|
link->status == DL_STATE_ACTIVE)
|
|
|
|
continue;
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
WARN_ON(link->status != DL_STATE_DORMANT);
|
|
|
|
WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
|
2019-02-01 07:59:42 +07:00
|
|
|
|
|
|
|
if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
|
|
|
|
driver_deferred_probe_add(link->consumer);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.suppliers, c_node) {
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
|
|
|
|
WRITE_ONCE(link->status, DL_STATE_ACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->links.status = DL_DEV_DRIVER_BOUND;
|
|
|
|
|
|
|
|
device_links_write_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __device_links_no_driver - Update links of a device without a driver.
|
|
|
|
* @dev: Device without a drvier.
|
|
|
|
*
|
|
|
|
* Delete all non-persistent links from this device to any suppliers.
|
|
|
|
*
|
|
|
|
* Persistent links stay around, but their status is changed to "available",
|
|
|
|
* unless they already are in the "supplier unbind in progress" state in which
|
|
|
|
* case they need not be updated.
|
|
|
|
*
|
|
|
|
* Links with the DL_FLAG_STATELESS flag set are ignored.
|
|
|
|
*/
|
|
|
|
static void __device_links_no_driver(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_link *link, *ln;
|
|
|
|
|
|
|
|
list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
2018-06-27 19:50:55 +07:00
|
|
|
if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
|
2019-01-01 11:51:05 +07:00
|
|
|
__device_link_del(&link->kref);
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
else if (link->status == DL_STATE_CONSUMER_PROBE ||
|
|
|
|
link->status == DL_STATE_ACTIVE)
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->links.status = DL_DEV_NO_DRIVER;
|
|
|
|
}
|
|
|
|
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
/**
|
|
|
|
* device_links_no_driver - Update links after failing driver probe.
|
|
|
|
* @dev: Device whose driver has just failed to probe.
|
|
|
|
*
|
|
|
|
* Clean up leftover links to consumers for @dev and invoke
|
|
|
|
* %__device_links_no_driver() to update links to suppliers for it as
|
|
|
|
* appropriate.
|
|
|
|
*
|
|
|
|
* Links with the DL_FLAG_STATELESS flag set are ignored.
|
|
|
|
*/
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
void device_links_no_driver(struct device *dev)
|
|
|
|
{
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
struct device_link *link;
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
device_links_write_lock();
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.consumers, s_node) {
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The probe has failed, so if the status of the link is
|
|
|
|
* "consumer probe" or "active", it must have been added by
|
|
|
|
* a probing consumer while this device was still probing.
|
|
|
|
* Change its state to "dormant", as it represents a valid
|
|
|
|
* relationship, but it is not functionally meaningful.
|
|
|
|
*/
|
|
|
|
if (link->status == DL_STATE_CONSUMER_PROBE ||
|
|
|
|
link->status == DL_STATE_ACTIVE)
|
|
|
|
WRITE_ONCE(link->status, DL_STATE_DORMANT);
|
|
|
|
}
|
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
__device_links_no_driver(dev);
|
driver core: Fix adding device links to probing suppliers
Currently, it is not valid to add a device link from a consumer
driver ->probe callback to a supplier that is still probing too, but
generally this is a valid use case. For example, if the consumer has
just acquired a resource that can only be available if the supplier
is functional, adding a device link to that supplier right away
should be safe (and even desirable arguably), but device_link_add()
doesn't handle that case correctly and the initial state of the link
created by it is wrong then.
To address this problem, change the initial state of device links
added between a probing supplier and a probing consumer to
DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
skip such links on the supplier side.
With this change, if the supplier probe completes first,
device_links_driver_bound() called for it will skip the link state
update and when it is called for the consumer, the link state will
be updated to "active". In turn, if the consumer probe completes
first, device_links_driver_bound() called for it will change the
state of the link to "active" and when it is called for the
supplier, the link status update will be skipped.
However, in principle the supplier or consumer probe may still fail
after the link has been added, so modify device_links_no_driver() to
change device links in the "active" or "consumer probe" state to
"dormant" on the supplier side and update __device_links_no_driver()
to change the link state to "available" only if it is "consumer
probe" or "active".
Then, if the supplier probe fails first, the leftover link to the
probing consumer will become "dormant" and device_links_no_driver()
called for the consumer (when its probe fails) will clean it up.
In turn, if the consumer probe fails first, it will either drop the
link, or change its state to "available" and, in the latter case,
when device_links_no_driver() is called for the supplier, it will
update the link state to "dormant". [If the supplier probe fails,
but the consumer probe succeeds, which should not happen as long as
the consumer driver is correct, the link still will be around, but
it will be "dormant" until the supplier is probed again.]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-02-01 07:50:39 +07:00
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
device_links_write_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_links_driver_cleanup - Update links after driver removal.
|
|
|
|
* @dev: Device whose driver has just gone away.
|
|
|
|
*
|
|
|
|
* Update links to consumers for @dev by changing their status to "dormant" and
|
|
|
|
* invoke %__device_links_no_driver() to update links to suppliers for it as
|
|
|
|
* appropriate.
|
|
|
|
*
|
|
|
|
* Links with the DL_FLAG_STATELESS flag set are ignored.
|
|
|
|
*/
|
|
|
|
void device_links_driver_cleanup(struct device *dev)
|
|
|
|
{
|
2019-02-01 07:45:55 +07:00
|
|
|
struct device_link *link, *ln;
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
|
|
|
|
device_links_write_lock();
|
|
|
|
|
2019-02-01 07:45:55 +07:00
|
|
|
list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
2018-06-27 19:50:55 +07:00
|
|
|
WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
|
2018-06-27 19:50:56 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* autoremove the links between this @dev and its consumer
|
|
|
|
* devices that are not active, i.e. where the link state
|
|
|
|
* has moved to DL_STATE_SUPPLIER_UNBIND.
|
|
|
|
*/
|
|
|
|
if (link->status == DL_STATE_SUPPLIER_UNBIND &&
|
|
|
|
link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
|
2019-01-01 11:51:05 +07:00
|
|
|
__device_link_del(&link->kref);
|
2018-06-27 19:50:56 +07:00
|
|
|
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
WRITE_ONCE(link->status, DL_STATE_DORMANT);
|
|
|
|
}
|
|
|
|
|
|
|
|
__device_links_no_driver(dev);
|
|
|
|
|
|
|
|
device_links_write_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_links_busy - Check if there are any busy links to consumers.
|
|
|
|
* @dev: Device to check.
|
|
|
|
*
|
|
|
|
* Check each consumer of the device and return 'true' if its link's status
|
|
|
|
* is one of "consumer probe" or "active" (meaning that the given consumer is
|
|
|
|
* probing right now or its driver is present). Otherwise, change the link
|
|
|
|
* state to "supplier unbind" to prevent the consumer from being probed
|
|
|
|
* successfully going forward.
|
|
|
|
*
|
|
|
|
* Return 'false' if there are no probing or active consumers.
|
|
|
|
*
|
|
|
|
* Links with the DL_FLAG_STATELESS flag set are ignored.
|
|
|
|
*/
|
|
|
|
bool device_links_busy(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
device_links_write_lock();
|
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.consumers, s_node) {
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (link->status == DL_STATE_CONSUMER_PROBE
|
|
|
|
|| link->status == DL_STATE_ACTIVE) {
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->links.status = DL_DEV_UNBINDING;
|
|
|
|
|
|
|
|
device_links_write_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_links_unbind_consumers - Force unbind consumers of the given device.
|
|
|
|
* @dev: Device to unbind the consumers of.
|
|
|
|
*
|
|
|
|
* Walk the list of links to consumers for @dev and if any of them is in the
|
|
|
|
* "consumer probe" state, wait for all device probes in progress to complete
|
|
|
|
* and start over.
|
|
|
|
*
|
|
|
|
* If that's not the case, change the status of the link to "supplier unbind"
|
|
|
|
* and check if the link was in the "active" state. If so, force the consumer
|
|
|
|
* driver to unbind and start over (the consumer will not re-probe as we have
|
|
|
|
* changed the state of the link already).
|
|
|
|
*
|
|
|
|
* Links with the DL_FLAG_STATELESS flag set are ignored.
|
|
|
|
*/
|
|
|
|
void device_links_unbind_consumers(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_link *link;
|
|
|
|
|
|
|
|
start:
|
|
|
|
device_links_write_lock();
|
|
|
|
|
|
|
|
list_for_each_entry(link, &dev->links.consumers, s_node) {
|
|
|
|
enum device_link_state status;
|
|
|
|
|
|
|
|
if (link->flags & DL_FLAG_STATELESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
status = link->status;
|
|
|
|
if (status == DL_STATE_CONSUMER_PROBE) {
|
|
|
|
device_links_write_unlock();
|
|
|
|
|
|
|
|
wait_for_device_probe();
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
|
|
|
|
if (status == DL_STATE_ACTIVE) {
|
|
|
|
struct device *consumer = link->consumer;
|
|
|
|
|
|
|
|
get_device(consumer);
|
|
|
|
|
|
|
|
device_links_write_unlock();
|
|
|
|
|
|
|
|
device_release_driver_internal(consumer, NULL,
|
|
|
|
consumer->parent);
|
|
|
|
put_device(consumer);
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device_links_write_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_links_purge - Delete existing links to other devices.
|
|
|
|
* @dev: Target device.
|
|
|
|
*/
|
|
|
|
static void device_links_purge(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_link *link, *ln;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete all of the remaining links from this device to any other
|
|
|
|
* devices (either consumers or suppliers).
|
|
|
|
*/
|
|
|
|
device_links_write_lock();
|
|
|
|
|
|
|
|
list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
|
|
|
|
WARN_ON(link->status == DL_STATE_ACTIVE);
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
__device_link_del(&link->kref);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
|
|
|
|
WARN_ON(link->status != DL_STATE_DORMANT &&
|
|
|
|
link->status != DL_STATE_NONE);
|
driver core: Introduce device links reference counting
If device_link_add() is invoked multiple times with the same supplier
and consumer combo, it will create the link on first addition and
return a pointer to the already existing link on all subsequent
additions.
The semantics for device_link_del() are quite different, it deletes
the link unconditionally, so multiple invocations are not allowed.
In other words, this snippet ...
struct device *dev1, *dev2;
struct device_link *link1, *link2;
link1 = device_link_add(dev1, dev2, 0);
link2 = device_link_add(dev1, dev2, 0);
device_link_del(link1);
device_link_del(link2);
... causes the following crash:
WARNING: CPU: 4 PID: 2686 at drivers/base/power/runtime.c:1611 pm_runtime_drop_link+0x40/0x50
[...]
list_del corruption, 0000000039b800a4->prev is LIST_POISON2 (00000000ecf79852)
kernel BUG at lib/list_debug.c:50!
The issue isn't as arbitrary as it may seem: Imagine a device link
which is added in both the supplier's and the consumer's ->probe hook.
The two drivers can't just call device_link_del() in their ->remove hook
without coordination.
Fix by counting multiple additions and dropping the device link only
when the last addition is unwound.
Signed-off-by: Lukas Wunner <lukas@wunner.de>
[ rjw: Subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2018-02-11 01:27:12 +07:00
|
|
|
__device_link_del(&link->kref);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
device_links_write_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Device links support end. */
|
|
|
|
|
2008-01-25 13:50:12 +07:00
|
|
|
int (*platform_notify)(struct device *dev) = NULL;
|
|
|
|
int (*platform_notify_remove)(struct device *dev) = NULL;
|
2008-04-22 00:51:07 +07:00
|
|
|
static struct kobject *dev_kobj;
|
|
|
|
struct kobject *sysfs_dev_char_kobj;
|
|
|
|
struct kobject *sysfs_dev_block_kobj;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
driver core / ACPI: Avoid device hot remove locking issues
device_hotplug_lock is held around the acpi_bus_trim() call in
acpi_scan_hot_remove() which generally removes devices (it removes
ACPI device objects at least, but it may also remove "physical"
device objects through .detach() callbacks of ACPI scan handlers).
Thus, potentially, device sysfs attributes are removed under that
lock and to remove those attributes it is necessary to hold the
s_active references of their directory entries for writing.
On the other hand, the execution of a .show() or .store() callback
from a sysfs attribute is carried out with that attribute's s_active
reference held for reading. Consequently, if any device sysfs
attribute that may be removed from within acpi_scan_hot_remove()
through acpi_bus_trim() has a .store() or .show() callback which
acquires device_hotplug_lock, the execution of that callback may
deadlock with the removal of the attribute. [Unfortunately, the
"online" device attribute of CPUs and memory blocks is one of them.]
To avoid such deadlocks, make all of the sysfs attribute callbacks
that need to lock device hotplug, for example store_online(), use
a special function, lock_device_hotplug_sysfs(), to lock device
hotplug and return the result of that function immediately if it is
not zero. This will cause the s_active reference of the directory
entry in question to be released and the syscall to be restarted
if device_hotplug_lock cannot be acquired.
[show_online() actually doesn't need to lock device hotplug, but
it is useful to serialize it with respect to device_offline() and
device_online() for the same device (in case user space attempts to
run them concurrently) which can be done with the help of
device_lock().]
Reported-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
Reported-and-tested-by: Gu Zheng <guz.fnst@cn.fujitsu.com>
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Toshi Kani <toshi.kani@hp.com>
2013-08-29 02:41:01 +07:00
|
|
|
static DEFINE_MUTEX(device_hotplug_lock);
|
|
|
|
|
|
|
|
void lock_device_hotplug(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&device_hotplug_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_device_hotplug(void)
|
|
|
|
{
|
|
|
|
mutex_unlock(&device_hotplug_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lock_device_hotplug_sysfs(void)
|
|
|
|
{
|
|
|
|
if (mutex_trylock(&device_hotplug_lock))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Avoid busy looping (5 ms of sleep should do). */
|
|
|
|
msleep(5);
|
|
|
|
return restart_syscall();
|
|
|
|
}
|
|
|
|
|
2008-01-28 03:12:43 +07:00
|
|
|
#ifdef CONFIG_BLOCK
|
|
|
|
static inline int device_is_not_partition(struct device *dev)
|
|
|
|
{
|
|
|
|
return !(dev->type == &part_type);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int device_is_not_partition(struct device *dev)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2018-11-09 21:21:34 +07:00
|
|
|
static int
|
|
|
|
device_platform_notify(struct device *dev, enum kobject_action action)
|
|
|
|
{
|
2018-11-09 21:21:35 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = acpi_platform_notify(dev, action);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-11-09 21:21:36 +07:00
|
|
|
ret = software_node_notify(dev, action);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-11-09 21:21:34 +07:00
|
|
|
if (platform_notify && action == KOBJ_ADD)
|
|
|
|
platform_notify(dev);
|
|
|
|
else if (platform_notify_remove && action == KOBJ_REMOVE)
|
|
|
|
platform_notify_remove(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-17 04:10:48 +07:00
|
|
|
/**
|
|
|
|
* dev_driver_string - Return a device's driver name, if at all possible
|
|
|
|
* @dev: struct device to get the name of
|
|
|
|
*
|
|
|
|
* Will return the device's driver's name if it is bound to a device. If
|
2012-04-20 20:08:45 +07:00
|
|
|
* the device is not bound to a driver, it will return the name of the bus
|
2006-06-17 04:10:48 +07:00
|
|
|
* it is attached to. If it is not attached to a bus either, an empty
|
|
|
|
* string will be returned.
|
|
|
|
*/
|
2008-07-31 02:29:21 +07:00
|
|
|
const char *dev_driver_string(const struct device *dev)
|
2006-06-17 04:10:48 +07:00
|
|
|
{
|
2009-12-04 23:06:57 +07:00
|
|
|
struct device_driver *drv;
|
|
|
|
|
|
|
|
/* dev->driver can change to NULL underneath us because of unbinding,
|
|
|
|
* so be careful about accessing it. dev->bus and dev->class should
|
|
|
|
* never change once they are set, so they don't need special care.
|
|
|
|
*/
|
locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()
Please do not apply this to mainline directly, instead please re-run the
coccinelle script shown below and apply its output.
For several reasons, it is desirable to use {READ,WRITE}_ONCE() in
preference to ACCESS_ONCE(), and new code is expected to use one of the
former. So far, there's been no reason to change most existing uses of
ACCESS_ONCE(), as these aren't harmful, and changing them results in
churn.
However, for some features, the read/write distinction is critical to
correct operation. To distinguish these cases, separate read/write
accessors must be used. This patch migrates (most) remaining
ACCESS_ONCE() instances to {READ,WRITE}_ONCE(), using the following
coccinelle script:
----
// Convert trivial ACCESS_ONCE() uses to equivalent READ_ONCE() and
// WRITE_ONCE()
// $ make coccicheck COCCI=/home/mark/once.cocci SPFLAGS="--include-headers" MODE=patch
virtual patch
@ depends on patch @
expression E1, E2;
@@
- ACCESS_ONCE(E1) = E2
+ WRITE_ONCE(E1, E2)
@ depends on patch @
expression E;
@@
- ACCESS_ONCE(E)
+ READ_ONCE(E)
----
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: davem@davemloft.net
Cc: linux-arch@vger.kernel.org
Cc: mpe@ellerman.id.au
Cc: shuah@kernel.org
Cc: snitzer@redhat.com
Cc: thor.thayer@linux.intel.com
Cc: tj@kernel.org
Cc: viro@zeniv.linux.org.uk
Cc: will.deacon@arm.com
Link: http://lkml.kernel.org/r/1508792849-3115-19-git-send-email-paulmck@linux.vnet.ibm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-10-24 04:07:29 +07:00
|
|
|
drv = READ_ONCE(dev->driver);
|
2009-12-04 23:06:57 +07:00
|
|
|
return drv ? drv->name :
|
2007-03-09 22:33:10 +07:00
|
|
|
(dev->bus ? dev->bus->name :
|
|
|
|
(dev->class ? dev->class->name : ""));
|
2006-06-17 04:10:48 +07:00
|
|
|
}
|
2006-09-24 12:35:04 +07:00
|
|
|
EXPORT_SYMBOL(dev_driver_string);
|
2006-06-17 04:10:48 +07:00
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
|
|
|
|
|
2008-01-25 13:50:12 +07:00
|
|
|
static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
|
|
|
|
char *buf)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-01-25 13:50:12 +07:00
|
|
|
struct device_attribute *dev_attr = to_dev_attr(attr);
|
2012-07-03 23:49:36 +07:00
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
2005-04-29 13:23:47 +07:00
|
|
|
ssize_t ret = -EIO;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (dev_attr->show)
|
2005-05-17 17:39:34 +07:00
|
|
|
ret = dev_attr->show(dev, dev_attr, buf);
|
2008-03-05 06:09:07 +07:00
|
|
|
if (ret >= (ssize_t)PAGE_SIZE) {
|
2017-12-11 19:50:21 +07:00
|
|
|
printk("dev_attr_show: %pS returned bad count\n",
|
|
|
|
dev_attr->show);
|
2008-03-05 06:09:07 +07:00
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-25 13:50:12 +07:00
|
|
|
static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-01-25 13:50:12 +07:00
|
|
|
struct device_attribute *dev_attr = to_dev_attr(attr);
|
2012-07-03 23:49:36 +07:00
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
2005-04-29 13:23:47 +07:00
|
|
|
ssize_t ret = -EIO;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (dev_attr->store)
|
2005-05-17 17:39:34 +07:00
|
|
|
ret = dev_attr->store(dev, dev_attr, buf, count);
|
2005-04-17 05:20:36 +07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-01-19 08:58:23 +07:00
|
|
|
static const struct sysfs_ops dev_sysfs_ops = {
|
2005-04-17 05:20:36 +07:00
|
|
|
.show = dev_attr_show,
|
|
|
|
.store = dev_attr_store,
|
|
|
|
};
|
|
|
|
|
2011-12-15 05:29:38 +07:00
|
|
|
#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
|
|
|
|
|
|
|
|
ssize_t device_store_ulong(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t size)
|
|
|
|
{
|
|
|
|
struct dev_ext_attribute *ea = to_ext_attr(attr);
|
2018-11-06 23:34:54 +07:00
|
|
|
int ret;
|
|
|
|
unsigned long new;
|
|
|
|
|
|
|
|
ret = kstrtoul(buf, 0, &new);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-12-15 05:29:38 +07:00
|
|
|
*(unsigned long *)(ea->var) = new;
|
|
|
|
/* Always return full write size even if we didn't consume all */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_store_ulong);
|
|
|
|
|
|
|
|
ssize_t device_show_ulong(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct dev_ext_attribute *ea = to_ext_attr(attr);
|
|
|
|
return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_show_ulong);
|
|
|
|
|
|
|
|
ssize_t device_store_int(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t size)
|
|
|
|
{
|
|
|
|
struct dev_ext_attribute *ea = to_ext_attr(attr);
|
2018-11-06 23:34:54 +07:00
|
|
|
int ret;
|
|
|
|
long new;
|
|
|
|
|
|
|
|
ret = kstrtol(buf, 0, &new);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (new > INT_MAX || new < INT_MIN)
|
2011-12-15 05:29:38 +07:00
|
|
|
return -EINVAL;
|
|
|
|
*(int *)(ea->var) = new;
|
|
|
|
/* Always return full write size even if we didn't consume all */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_store_int);
|
|
|
|
|
|
|
|
ssize_t device_show_int(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct dev_ext_attribute *ea = to_ext_attr(attr);
|
|
|
|
|
|
|
|
return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_show_int);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2012-10-10 00:52:05 +07:00
|
|
|
ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t size)
|
|
|
|
{
|
|
|
|
struct dev_ext_attribute *ea = to_ext_attr(attr);
|
|
|
|
|
|
|
|
if (strtobool(buf, ea->var) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_store_bool);
|
|
|
|
|
|
|
|
ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct dev_ext_attribute *ea = to_ext_attr(attr);
|
|
|
|
|
|
|
|
return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_show_bool);
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/**
|
2013-06-02 07:17:34 +07:00
|
|
|
* device_release - free device structure.
|
|
|
|
* @kobj: device's kobject.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2013-06-02 07:17:34 +07:00
|
|
|
* This is called once the reference count for the object
|
|
|
|
* reaches 0. We forward the call to the device's release
|
|
|
|
* method, which should handle actually freeing the structure.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2008-01-25 13:50:12 +07:00
|
|
|
static void device_release(struct kobject *kobj)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2012-07-03 23:49:36 +07:00
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
2008-12-17 03:23:36 +07:00
|
|
|
struct device_private *p = dev->p;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2012-07-25 00:42:29 +07:00
|
|
|
/*
|
|
|
|
* Some platform devices are driven without driver attached
|
|
|
|
* and managed resources may have been acquired. Make sure
|
|
|
|
* all resources are released.
|
|
|
|
*
|
|
|
|
* Drivers still can add resources into device after device
|
|
|
|
* is deleted but alive, so release devres here to avoid
|
|
|
|
* possible memory leak.
|
|
|
|
*/
|
|
|
|
devres_release_all(dev);
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
if (dev->release)
|
|
|
|
dev->release(dev);
|
2006-10-08 02:54:55 +07:00
|
|
|
else if (dev->type && dev->type->release)
|
|
|
|
dev->type->release(dev);
|
2006-06-29 06:19:58 +07:00
|
|
|
else if (dev->class && dev->class->dev_release)
|
|
|
|
dev->class->dev_release(dev);
|
2008-07-26 09:45:39 +07:00
|
|
|
else
|
2018-12-03 23:44:35 +07:00
|
|
|
WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
|
2008-10-30 07:36:48 +07:00
|
|
|
dev_name(dev));
|
2008-12-17 03:23:36 +07:00
|
|
|
kfree(p);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2010-03-31 01:31:25 +07:00
|
|
|
static const void *device_namespace(struct kobject *kobj)
|
|
|
|
{
|
2012-07-03 23:49:36 +07:00
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
2010-03-31 01:31:25 +07:00
|
|
|
const void *ns = NULL;
|
|
|
|
|
|
|
|
if (dev->class && dev->class->ns_type)
|
|
|
|
ns = dev->class->namespace(dev);
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2018-07-21 04:56:50 +07:00
|
|
|
static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
|
|
|
|
{
|
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
|
|
|
|
|
|
|
if (dev->class && dev->class->get_ownership)
|
|
|
|
dev->class->get_ownership(dev, uid, gid);
|
|
|
|
}
|
|
|
|
|
2007-10-11 23:47:49 +07:00
|
|
|
static struct kobj_type device_ktype = {
|
2005-04-17 05:20:36 +07:00
|
|
|
.release = device_release,
|
|
|
|
.sysfs_ops = &dev_sysfs_ops,
|
2010-03-31 01:31:25 +07:00
|
|
|
.namespace = device_namespace,
|
2018-07-21 04:56:50 +07:00
|
|
|
.get_ownership = device_get_ownership,
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-11-16 15:00:00 +07:00
|
|
|
static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
struct kobj_type *ktype = get_ktype(kobj);
|
|
|
|
|
2007-10-11 23:47:49 +07:00
|
|
|
if (ktype == &device_ktype) {
|
2012-07-03 23:49:36 +07:00
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (dev->bus)
|
|
|
|
return 1;
|
2006-06-15 02:14:34 +07:00
|
|
|
if (dev->class)
|
|
|
|
return 1;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-16 15:00:00 +07:00
|
|
|
static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2012-07-03 23:49:36 +07:00
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2006-06-15 02:14:34 +07:00
|
|
|
if (dev->bus)
|
|
|
|
return dev->bus->name;
|
|
|
|
if (dev->class)
|
|
|
|
return dev->class->name;
|
|
|
|
return NULL;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2007-08-14 20:15:12 +07:00
|
|
|
static int dev_uevent(struct kset *kset, struct kobject *kobj,
|
|
|
|
struct kobj_uevent_env *env)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2012-07-03 23:49:36 +07:00
|
|
|
struct device *dev = kobj_to_dev(kobj);
|
2005-04-17 05:20:36 +07:00
|
|
|
int retval = 0;
|
|
|
|
|
2009-04-30 20:23:42 +07:00
|
|
|
/* add device node properties if present */
|
2006-06-15 02:14:34 +07:00
|
|
|
if (MAJOR(dev->devt)) {
|
2009-04-30 20:23:42 +07:00
|
|
|
const char *tmp;
|
|
|
|
const char *name;
|
2011-07-24 07:24:48 +07:00
|
|
|
umode_t mode = 0;
|
2013-04-12 01:43:29 +07:00
|
|
|
kuid_t uid = GLOBAL_ROOT_UID;
|
|
|
|
kgid_t gid = GLOBAL_ROOT_GID;
|
2009-04-30 20:23:42 +07:00
|
|
|
|
2007-08-14 20:15:12 +07:00
|
|
|
add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
|
|
|
|
add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
|
2013-04-06 23:56:00 +07:00
|
|
|
name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
|
2009-04-30 20:23:42 +07:00
|
|
|
if (name) {
|
|
|
|
add_uevent_var(env, "DEVNAME=%s", name);
|
2009-09-19 04:01:12 +07:00
|
|
|
if (mode)
|
|
|
|
add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
|
2013-04-12 01:43:29 +07:00
|
|
|
if (!uid_eq(uid, GLOBAL_ROOT_UID))
|
|
|
|
add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
|
|
|
|
if (!gid_eq(gid, GLOBAL_ROOT_GID))
|
|
|
|
add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
|
2013-04-06 23:56:00 +07:00
|
|
|
kfree(tmp);
|
2009-04-30 20:23:42 +07:00
|
|
|
}
|
2006-06-15 02:14:34 +07:00
|
|
|
}
|
|
|
|
|
2007-03-13 03:08:57 +07:00
|
|
|
if (dev->type && dev->type->name)
|
2007-08-14 20:15:12 +07:00
|
|
|
add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
|
2007-03-13 03:08:57 +07:00
|
|
|
|
2006-10-08 02:54:55 +07:00
|
|
|
if (dev->driver)
|
2007-08-14 20:15:12 +07:00
|
|
|
add_uevent_var(env, "DRIVER=%s", dev->driver->name);
|
2006-10-08 02:54:55 +07:00
|
|
|
|
2012-02-02 01:22:22 +07:00
|
|
|
/* Add common DT information about the device */
|
|
|
|
of_device_uevent(dev, env);
|
|
|
|
|
2007-08-14 20:15:12 +07:00
|
|
|
/* have the bus specific function add its stuff */
|
2005-11-16 15:00:00 +07:00
|
|
|
if (dev->bus && dev->bus->uevent) {
|
2007-08-14 20:15:12 +07:00
|
|
|
retval = dev->bus->uevent(dev, env);
|
2006-10-08 02:54:55 +07:00
|
|
|
if (retval)
|
2007-11-29 14:49:41 +07:00
|
|
|
pr_debug("device: '%s': %s: bus uevent() returned %d\n",
|
2008-10-30 07:36:48 +07:00
|
|
|
dev_name(dev), __func__, retval);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2007-08-14 20:15:12 +07:00
|
|
|
/* have the class specific function add its stuff */
|
2006-06-29 06:19:58 +07:00
|
|
|
if (dev->class && dev->class->dev_uevent) {
|
2007-08-14 20:15:12 +07:00
|
|
|
retval = dev->class->dev_uevent(dev, env);
|
2006-10-08 02:54:55 +07:00
|
|
|
if (retval)
|
2007-11-29 14:49:41 +07:00
|
|
|
pr_debug("device: '%s': %s: class uevent() "
|
2008-10-30 07:36:48 +07:00
|
|
|
"returned %d\n", dev_name(dev),
|
2008-03-05 07:41:05 +07:00
|
|
|
__func__, retval);
|
2006-10-08 02:54:55 +07:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:11:15 +07:00
|
|
|
/* have the device type specific function add its stuff */
|
2006-10-08 02:54:55 +07:00
|
|
|
if (dev->type && dev->type->uevent) {
|
2007-08-14 20:15:12 +07:00
|
|
|
retval = dev->type->uevent(dev, env);
|
2006-10-08 02:54:55 +07:00
|
|
|
if (retval)
|
2007-11-29 14:49:41 +07:00
|
|
|
pr_debug("device: '%s': %s: dev_type uevent() "
|
2008-10-30 07:36:48 +07:00
|
|
|
"returned %d\n", dev_name(dev),
|
2008-03-05 07:41:05 +07:00
|
|
|
__func__, retval);
|
2006-06-29 06:19:58 +07:00
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-12-31 20:52:51 +07:00
|
|
|
static const struct kset_uevent_ops device_uevent_ops = {
|
2005-11-16 15:00:00 +07:00
|
|
|
.filter = dev_uevent_filter,
|
|
|
|
.name = dev_uevent_name,
|
|
|
|
.uevent = dev_uevent,
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
|
|
|
|
2013-08-24 07:07:26 +07:00
|
|
|
static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
|
2007-04-06 06:40:38 +07:00
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct kobject *top_kobj;
|
|
|
|
struct kset *kset;
|
2007-08-14 20:15:12 +07:00
|
|
|
struct kobj_uevent_env *env = NULL;
|
2007-04-06 06:40:38 +07:00
|
|
|
int i;
|
|
|
|
size_t count = 0;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* search the kset, the device belongs to */
|
|
|
|
top_kobj = &dev->kobj;
|
2007-08-13 01:43:55 +07:00
|
|
|
while (!top_kobj->kset && top_kobj->parent)
|
|
|
|
top_kobj = top_kobj->parent;
|
2007-04-06 06:40:38 +07:00
|
|
|
if (!top_kobj->kset)
|
|
|
|
goto out;
|
2007-08-13 01:43:55 +07:00
|
|
|
|
2007-04-06 06:40:38 +07:00
|
|
|
kset = top_kobj->kset;
|
|
|
|
if (!kset->uevent_ops || !kset->uevent_ops->uevent)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* respect filter */
|
|
|
|
if (kset->uevent_ops && kset->uevent_ops->filter)
|
|
|
|
if (!kset->uevent_ops->filter(kset, &dev->kobj))
|
|
|
|
goto out;
|
|
|
|
|
2007-08-14 20:15:12 +07:00
|
|
|
env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
|
|
|
|
if (!env)
|
2007-05-02 19:14:11 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-04-06 06:40:38 +07:00
|
|
|
/* let the kset specific function add its keys */
|
2007-08-14 20:15:12 +07:00
|
|
|
retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
|
2007-04-06 06:40:38 +07:00
|
|
|
if (retval)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* copy keys to file */
|
2007-08-14 20:15:12 +07:00
|
|
|
for (i = 0; i < env->envp_idx; i++)
|
|
|
|
count += sprintf(&buf[count], "%s\n", env->envp[i]);
|
2007-04-06 06:40:38 +07:00
|
|
|
out:
|
2007-08-14 20:15:12 +07:00
|
|
|
kfree(env);
|
2007-04-06 06:40:38 +07:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-08-24 07:07:26 +07:00
|
|
|
static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
|
2005-10-01 19:49:43 +07:00
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
2018-12-05 18:27:44 +07:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = kobject_synth_uevent(&dev->kobj, buf, count);
|
|
|
|
|
|
|
|
if (rc) {
|
kobject: support passing in variables for synthetic uevents
This patch makes it possible to pass additional arguments in addition
to uevent action name when writing /sys/.../uevent attribute. These
additional arguments are then inserted into generated synthetic uevent
as additional environment variables.
Before, we were not able to pass any additional uevent environment
variables for synthetic uevents. This made it hard to identify such uevents
properly in userspace to make proper distinction between genuine uevents
originating from kernel and synthetic uevents triggered from userspace.
Also, it was not possible to pass any additional information which would
make it possible to optimize and change the way the synthetic uevents are
processed back in userspace based on the originating environment of the
triggering action in userspace. With the extra additional variables, we are
able to pass through this extra information needed and also it makes it
possible to synchronize with such synthetic uevents as they can be clearly
identified back in userspace.
The format for writing the uevent attribute is following:
ACTION [UUID [KEY=VALUE ...]
There's no change in how "ACTION" is recognized - it stays the same
("add", "change", "remove"). The "ACTION" is the only argument required
to generate synthetic uevent, the rest of arguments, that this patch
adds support for, are optional.
The "UUID" is considered as transaction identifier so it's possible to
use the same UUID value for one or more synthetic uevents in which case
we logically group these uevents together for any userspace listeners.
The "UUID" is expected to be in "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
format where "x" is a hex digit. The value appears in uevent as
"SYNTH_UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" environment variable.
The "KEY=VALUE" pairs can contain alphanumeric characters only. It's
possible to define zero or more more pairs - each pair is then delimited
by a space character " ". Each pair appears in synthetic uevents as
"SYNTH_ARG_KEY=VALUE" environment variable. That means the KEY name gains
"SYNTH_ARG_" prefix to avoid possible collisions with existing variables.
To pass the "KEY=VALUE" pairs, it's also required to pass in the "UUID"
part for the synthetic uevent first.
If "UUID" is not passed in, the generated synthetic uevent gains
"SYNTH_UUID=0" environment variable automatically so it's possible to
identify this situation in userspace when reading generated uevent and so
we can still make a difference between genuine and synthetic uevents.
Signed-off-by: Peter Rajnoha <prajnoha@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-05-09 20:22:30 +07:00
|
|
|
dev_err(dev, "uevent: failed to send synthetic uevent\n");
|
2018-12-05 18:27:44 +07:00
|
|
|
return rc;
|
|
|
|
}
|
2007-07-09 03:29:26 +07:00
|
|
|
|
2005-10-01 19:49:43 +07:00
|
|
|
return count;
|
|
|
|
}
|
2013-08-24 07:07:26 +07:00
|
|
|
static DEVICE_ATTR_RW(uevent);
|
2005-10-01 19:49:43 +07:00
|
|
|
|
2013-08-24 07:07:26 +07:00
|
|
|
static ssize_t online_show(struct device *dev, struct device_attribute *attr,
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
bool val;
|
|
|
|
|
driver core / ACPI: Avoid device hot remove locking issues
device_hotplug_lock is held around the acpi_bus_trim() call in
acpi_scan_hot_remove() which generally removes devices (it removes
ACPI device objects at least, but it may also remove "physical"
device objects through .detach() callbacks of ACPI scan handlers).
Thus, potentially, device sysfs attributes are removed under that
lock and to remove those attributes it is necessary to hold the
s_active references of their directory entries for writing.
On the other hand, the execution of a .show() or .store() callback
from a sysfs attribute is carried out with that attribute's s_active
reference held for reading. Consequently, if any device sysfs
attribute that may be removed from within acpi_scan_hot_remove()
through acpi_bus_trim() has a .store() or .show() callback which
acquires device_hotplug_lock, the execution of that callback may
deadlock with the removal of the attribute. [Unfortunately, the
"online" device attribute of CPUs and memory blocks is one of them.]
To avoid such deadlocks, make all of the sysfs attribute callbacks
that need to lock device hotplug, for example store_online(), use
a special function, lock_device_hotplug_sysfs(), to lock device
hotplug and return the result of that function immediately if it is
not zero. This will cause the s_active reference of the directory
entry in question to be released and the syscall to be restarted
if device_hotplug_lock cannot be acquired.
[show_online() actually doesn't need to lock device hotplug, but
it is useful to serialize it with respect to device_offline() and
device_online() for the same device (in case user space attempts to
run them concurrently) which can be done with the help of
device_lock().]
Reported-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
Reported-and-tested-by: Gu Zheng <guz.fnst@cn.fujitsu.com>
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Toshi Kani <toshi.kani@hp.com>
2013-08-29 02:41:01 +07:00
|
|
|
device_lock(dev);
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
val = !dev->offline;
|
driver core / ACPI: Avoid device hot remove locking issues
device_hotplug_lock is held around the acpi_bus_trim() call in
acpi_scan_hot_remove() which generally removes devices (it removes
ACPI device objects at least, but it may also remove "physical"
device objects through .detach() callbacks of ACPI scan handlers).
Thus, potentially, device sysfs attributes are removed under that
lock and to remove those attributes it is necessary to hold the
s_active references of their directory entries for writing.
On the other hand, the execution of a .show() or .store() callback
from a sysfs attribute is carried out with that attribute's s_active
reference held for reading. Consequently, if any device sysfs
attribute that may be removed from within acpi_scan_hot_remove()
through acpi_bus_trim() has a .store() or .show() callback which
acquires device_hotplug_lock, the execution of that callback may
deadlock with the removal of the attribute. [Unfortunately, the
"online" device attribute of CPUs and memory blocks is one of them.]
To avoid such deadlocks, make all of the sysfs attribute callbacks
that need to lock device hotplug, for example store_online(), use
a special function, lock_device_hotplug_sysfs(), to lock device
hotplug and return the result of that function immediately if it is
not zero. This will cause the s_active reference of the directory
entry in question to be released and the syscall to be restarted
if device_hotplug_lock cannot be acquired.
[show_online() actually doesn't need to lock device hotplug, but
it is useful to serialize it with respect to device_offline() and
device_online() for the same device (in case user space attempts to
run them concurrently) which can be done with the help of
device_lock().]
Reported-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
Reported-and-tested-by: Gu Zheng <guz.fnst@cn.fujitsu.com>
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Toshi Kani <toshi.kani@hp.com>
2013-08-29 02:41:01 +07:00
|
|
|
device_unlock(dev);
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
return sprintf(buf, "%u\n", val);
|
|
|
|
}
|
|
|
|
|
2013-08-24 07:07:26 +07:00
|
|
|
static ssize_t online_store(struct device *dev, struct device_attribute *attr,
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
bool val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = strtobool(buf, &val);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
driver core / ACPI: Avoid device hot remove locking issues
device_hotplug_lock is held around the acpi_bus_trim() call in
acpi_scan_hot_remove() which generally removes devices (it removes
ACPI device objects at least, but it may also remove "physical"
device objects through .detach() callbacks of ACPI scan handlers).
Thus, potentially, device sysfs attributes are removed under that
lock and to remove those attributes it is necessary to hold the
s_active references of their directory entries for writing.
On the other hand, the execution of a .show() or .store() callback
from a sysfs attribute is carried out with that attribute's s_active
reference held for reading. Consequently, if any device sysfs
attribute that may be removed from within acpi_scan_hot_remove()
through acpi_bus_trim() has a .store() or .show() callback which
acquires device_hotplug_lock, the execution of that callback may
deadlock with the removal of the attribute. [Unfortunately, the
"online" device attribute of CPUs and memory blocks is one of them.]
To avoid such deadlocks, make all of the sysfs attribute callbacks
that need to lock device hotplug, for example store_online(), use
a special function, lock_device_hotplug_sysfs(), to lock device
hotplug and return the result of that function immediately if it is
not zero. This will cause the s_active reference of the directory
entry in question to be released and the syscall to be restarted
if device_hotplug_lock cannot be acquired.
[show_online() actually doesn't need to lock device hotplug, but
it is useful to serialize it with respect to device_offline() and
device_online() for the same device (in case user space attempts to
run them concurrently) which can be done with the help of
device_lock().]
Reported-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
Reported-and-tested-by: Gu Zheng <guz.fnst@cn.fujitsu.com>
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Toshi Kani <toshi.kani@hp.com>
2013-08-29 02:41:01 +07:00
|
|
|
ret = lock_device_hotplug_sysfs();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
ret = val ? device_online(dev) : device_offline(dev);
|
|
|
|
unlock_device_hotplug();
|
|
|
|
return ret < 0 ? ret : count;
|
|
|
|
}
|
2013-08-24 07:07:26 +07:00
|
|
|
static DEVICE_ATTR_RW(online);
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
|
2013-08-09 05:22:55 +07:00
|
|
|
int device_add_groups(struct device *dev, const struct attribute_group **groups)
|
2007-03-10 13:37:34 +07:00
|
|
|
{
|
2013-08-22 03:47:50 +07:00
|
|
|
return sysfs_create_groups(&dev->kobj, groups);
|
2006-06-27 14:06:09 +07:00
|
|
|
}
|
2017-07-20 07:24:31 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_add_groups);
|
2006-06-27 14:06:09 +07:00
|
|
|
|
2013-08-09 05:22:55 +07:00
|
|
|
void device_remove_groups(struct device *dev,
|
|
|
|
const struct attribute_group **groups)
|
2006-06-27 14:06:09 +07:00
|
|
|
{
|
2013-08-22 03:47:50 +07:00
|
|
|
sysfs_remove_groups(&dev->kobj, groups);
|
2006-06-27 14:06:09 +07:00
|
|
|
}
|
2017-07-20 07:24:31 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_remove_groups);
|
2006-06-27 14:06:09 +07:00
|
|
|
|
2017-07-20 07:24:33 +07:00
|
|
|
union device_attr_group_devres {
|
|
|
|
const struct attribute_group *group;
|
|
|
|
const struct attribute_group **groups;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int devm_attr_group_match(struct device *dev, void *res, void *data)
|
|
|
|
{
|
|
|
|
return ((union device_attr_group_devres *)res)->group == data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void devm_attr_group_remove(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
union device_attr_group_devres *devres = res;
|
|
|
|
const struct attribute_group *group = devres->group;
|
|
|
|
|
|
|
|
dev_dbg(dev, "%s: removing group %p\n", __func__, group);
|
|
|
|
sysfs_remove_group(&dev->kobj, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void devm_attr_groups_remove(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
union device_attr_group_devres *devres = res;
|
|
|
|
const struct attribute_group **groups = devres->groups;
|
|
|
|
|
|
|
|
dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
|
|
|
|
sysfs_remove_groups(&dev->kobj, groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_device_add_group - given a device, create a managed attribute group
|
|
|
|
* @dev: The device to create the group for
|
|
|
|
* @grp: The attribute group to create
|
|
|
|
*
|
|
|
|
* This function creates a group for the first time. It will explicitly
|
|
|
|
* warn and error if any of the attribute files being created already exist.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or error code on failure.
|
|
|
|
*/
|
|
|
|
int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
|
|
|
|
{
|
|
|
|
union device_attr_group_devres *devres;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
devres = devres_alloc(devm_attr_group_remove,
|
|
|
|
sizeof(*devres), GFP_KERNEL);
|
|
|
|
if (!devres)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
error = sysfs_create_group(&dev->kobj, grp);
|
|
|
|
if (error) {
|
|
|
|
devres_free(devres);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
devres->group = grp;
|
|
|
|
devres_add(dev, devres);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_device_add_group);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_device_remove_group: remove a managed group from a device
|
|
|
|
* @dev: device to remove the group from
|
|
|
|
* @grp: group to remove
|
|
|
|
*
|
|
|
|
* This function removes a group of attributes from a device. The attributes
|
|
|
|
* previously have to have been created for this group, otherwise it will fail.
|
|
|
|
*/
|
|
|
|
void devm_device_remove_group(struct device *dev,
|
|
|
|
const struct attribute_group *grp)
|
|
|
|
{
|
|
|
|
WARN_ON(devres_release(dev, devm_attr_group_remove,
|
|
|
|
devm_attr_group_match,
|
|
|
|
/* cast away const */ (void *)grp));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_device_remove_group);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_device_add_groups - create a bunch of managed attribute groups
|
|
|
|
* @dev: The device to create the group for
|
|
|
|
* @groups: The attribute groups to create, NULL terminated
|
|
|
|
*
|
|
|
|
* This function creates a bunch of managed attribute groups. If an error
|
|
|
|
* occurs when creating a group, all previously created groups will be
|
|
|
|
* removed, unwinding everything back to the original state when this
|
|
|
|
* function was called. It will explicitly warn and error if any of the
|
|
|
|
* attribute files being created already exist.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or error code from sysfs_create_group on failure.
|
|
|
|
*/
|
|
|
|
int devm_device_add_groups(struct device *dev,
|
|
|
|
const struct attribute_group **groups)
|
|
|
|
{
|
|
|
|
union device_attr_group_devres *devres;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
devres = devres_alloc(devm_attr_groups_remove,
|
|
|
|
sizeof(*devres), GFP_KERNEL);
|
|
|
|
if (!devres)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
error = sysfs_create_groups(&dev->kobj, groups);
|
|
|
|
if (error) {
|
|
|
|
devres_free(devres);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
devres->groups = groups;
|
|
|
|
devres_add(dev, devres);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_device_add_groups);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_device_remove_groups - remove a list of managed groups
|
|
|
|
*
|
|
|
|
* @dev: The device for the groups to be removed from
|
|
|
|
* @groups: NULL terminated list of groups to be removed
|
|
|
|
*
|
|
|
|
* If groups is not NULL, remove the specified groups from the device.
|
|
|
|
*/
|
|
|
|
void devm_device_remove_groups(struct device *dev,
|
|
|
|
const struct attribute_group **groups)
|
|
|
|
{
|
|
|
|
WARN_ON(devres_release(dev, devm_attr_groups_remove,
|
|
|
|
devm_attr_group_match,
|
|
|
|
/* cast away const */ (void *)groups));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_device_remove_groups);
|
2006-06-27 14:06:09 +07:00
|
|
|
|
2006-06-29 06:19:58 +07:00
|
|
|
static int device_add_attrs(struct device *dev)
|
|
|
|
{
|
|
|
|
struct class *class = dev->class;
|
2011-03-28 23:12:52 +07:00
|
|
|
const struct device_type *type = dev->type;
|
2007-03-10 13:37:34 +07:00
|
|
|
int error;
|
2006-06-29 06:19:58 +07:00
|
|
|
|
2007-03-10 13:37:34 +07:00
|
|
|
if (class) {
|
2013-07-15 06:05:58 +07:00
|
|
|
error = device_add_groups(dev, class->dev_groups);
|
2006-10-08 02:54:55 +07:00
|
|
|
if (error)
|
2007-03-10 13:37:34 +07:00
|
|
|
return error;
|
2006-06-29 06:19:58 +07:00
|
|
|
}
|
2006-10-08 02:54:55 +07:00
|
|
|
|
2007-03-10 13:37:34 +07:00
|
|
|
if (type) {
|
|
|
|
error = device_add_groups(dev, type->groups);
|
2006-10-08 02:54:55 +07:00
|
|
|
if (error)
|
2013-10-06 08:19:30 +07:00
|
|
|
goto err_remove_class_groups;
|
2006-10-08 02:54:55 +07:00
|
|
|
}
|
|
|
|
|
2007-03-10 13:37:34 +07:00
|
|
|
error = device_add_groups(dev, dev->groups);
|
|
|
|
if (error)
|
|
|
|
goto err_remove_type_groups;
|
|
|
|
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
if (device_supports_offline(dev) && !dev->offline_disabled) {
|
2013-08-24 07:07:26 +07:00
|
|
|
error = device_create_file(dev, &dev_attr_online);
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
if (error)
|
2013-12-12 12:11:02 +07:00
|
|
|
goto err_remove_dev_groups;
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
}
|
|
|
|
|
2007-03-10 13:37:34 +07:00
|
|
|
return 0;
|
|
|
|
|
2013-12-12 12:11:02 +07:00
|
|
|
err_remove_dev_groups:
|
|
|
|
device_remove_groups(dev, dev->groups);
|
2007-03-10 13:37:34 +07:00
|
|
|
err_remove_type_groups:
|
|
|
|
if (type)
|
|
|
|
device_remove_groups(dev, type->groups);
|
2013-07-15 06:05:58 +07:00
|
|
|
err_remove_class_groups:
|
|
|
|
if (class)
|
|
|
|
device_remove_groups(dev, class->dev_groups);
|
2007-03-10 13:37:34 +07:00
|
|
|
|
2006-06-29 06:19:58 +07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_remove_attrs(struct device *dev)
|
|
|
|
{
|
|
|
|
struct class *class = dev->class;
|
2011-03-28 23:12:52 +07:00
|
|
|
const struct device_type *type = dev->type;
|
2006-06-29 06:19:58 +07:00
|
|
|
|
2013-08-24 07:07:26 +07:00
|
|
|
device_remove_file(dev, &dev_attr_online);
|
2007-03-10 13:37:34 +07:00
|
|
|
device_remove_groups(dev, dev->groups);
|
2006-10-08 02:54:55 +07:00
|
|
|
|
2007-03-10 13:37:34 +07:00
|
|
|
if (type)
|
|
|
|
device_remove_groups(dev, type->groups);
|
|
|
|
|
2013-10-06 08:19:30 +07:00
|
|
|
if (class)
|
2013-07-15 06:05:58 +07:00
|
|
|
device_remove_groups(dev, class->dev_groups);
|
2006-06-29 06:19:58 +07:00
|
|
|
}
|
|
|
|
|
2013-08-24 07:07:26 +07:00
|
|
|
static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
|
2006-06-15 02:14:34 +07:00
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
return print_dev_t(buf, dev->devt);
|
|
|
|
}
|
2013-08-24 07:07:26 +07:00
|
|
|
static DEVICE_ATTR_RO(dev);
|
2007-06-14 01:45:17 +07:00
|
|
|
|
2011-12-15 05:29:38 +07:00
|
|
|
/* /sys/devices/ */
|
2007-11-01 22:29:06 +07:00
|
|
|
struct kset *devices_kset;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
driver core: correct device's shutdown order
Now device's shutdown sequence is performed in reverse order of their
registration in devices_kset list and this sequence corresponds to the
reverse device's creation order. So, devices_kset data tracks
"parent<-child" device's dependencies only.
Unfortunately, that's not enough and causes problems in case of
implementing board's specific shutdown procedures. For example [1]:
"DRA7XX_evm uses PCF8575 and one of the PCF output lines feeds to
MMC/SD and this line should be driven high in order for the MMC/SD to
be detected. This line is modelled as regulator and the hsmmc driver
takes care of enabling and disabling it. In the case of 'reboot',
during shutdown path as part of it's cleanup process the hsmmc driver
disables this regulator. This makes MMC boot not functional."
To handle this issue the .shutdown() callback could be implemented
for PCF8575 device where corresponding GPIO pins will be configured to
states, required for correct warm/cold reset. This can be achieved
only when all .shutdown() callbacks have been called already for all
PCF8575's consumers. But devices_kset is not filled correctly now:
devices_kset: Device61 4e000000.dmm
devices_kset: Device62 48070000.i2c
devices_kset: Device63 48072000.i2c
devices_kset: Device64 48060000.i2c
devices_kset: Device65 4809c000.mmc
...
devices_kset: Device102 fixedregulator-sd
...
devices_kset: Device181 0-0020 // PCF8575
devices_kset: Device182 gpiochip496
devices_kset: Device183 0-0021 // PCF8575
devices_kset: Device184 gpiochip480
As can be seen from above .shutdown() callback for PCF8575 will be called
before its consumers, which, in turn means, that any changes of PCF8575
GPIO's pins will be or unsafe or overwritten later by GPIO's consumers.
The problem can be solved if devices_kset list will be filled not only
according device creation order, but also according device's probing
order to track "supplier<-consumer" dependencies also.
Hence, as a fix, lets add devices_kset_move_last(),
devices_kset_move_before(), devices_kset_move_after() and call them
from device_move() and also add call of devices_kset_move_last() in
really_probe(). After this change all entries in devices_kset will
be sorted according to device's creation ("parent<-child") and
probing ("supplier<-consumer") order.
devices_kset after:
devices_kset: Device121 48070000.i2c
devices_kset: Device122 i2c-0
...
devices_kset: Device147 regulator.24
devices_kset: Device148 0-0020
devices_kset: Device149 gpiochip496
devices_kset: Device150 0-0021
devices_kset: Device151 gpiochip480
devices_kset: Device152 0-0019
...
devices_kset: Device372 fixedregulator-sd
devices_kset: Device373 regulator.29
devices_kset: Device374 4809c000.mmc
devices_kset: Device375 mmc0
[1] http://www.spinics.net/lists/linux-mmc/msg29825.html
Cc: Sekhar Nori <nsekhar@ti.com>
Signed-off-by: Grygorii Strashko <grygorii.strashko@ti.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-07-28 00:43:01 +07:00
|
|
|
/**
|
|
|
|
* devices_kset_move_before - Move device in the devices_kset's list.
|
|
|
|
* @deva: Device to move.
|
|
|
|
* @devb: Device @deva should come before.
|
|
|
|
*/
|
|
|
|
static void devices_kset_move_before(struct device *deva, struct device *devb)
|
|
|
|
{
|
|
|
|
if (!devices_kset)
|
|
|
|
return;
|
|
|
|
pr_debug("devices_kset: Moving %s before %s\n",
|
|
|
|
dev_name(deva), dev_name(devb));
|
|
|
|
spin_lock(&devices_kset->list_lock);
|
|
|
|
list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
|
|
|
|
spin_unlock(&devices_kset->list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devices_kset_move_after - Move device in the devices_kset's list.
|
|
|
|
* @deva: Device to move
|
|
|
|
* @devb: Device @deva should come after.
|
|
|
|
*/
|
|
|
|
static void devices_kset_move_after(struct device *deva, struct device *devb)
|
|
|
|
{
|
|
|
|
if (!devices_kset)
|
|
|
|
return;
|
|
|
|
pr_debug("devices_kset: Moving %s after %s\n",
|
|
|
|
dev_name(deva), dev_name(devb));
|
|
|
|
spin_lock(&devices_kset->list_lock);
|
|
|
|
list_move(&deva->kobj.entry, &devb->kobj.entry);
|
|
|
|
spin_unlock(&devices_kset->list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devices_kset_move_last - move the device to the end of devices_kset's list.
|
|
|
|
* @dev: device to move
|
|
|
|
*/
|
|
|
|
void devices_kset_move_last(struct device *dev)
|
|
|
|
{
|
|
|
|
if (!devices_kset)
|
|
|
|
return;
|
|
|
|
pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
|
|
|
|
spin_lock(&devices_kset->list_lock);
|
|
|
|
list_move_tail(&dev->kobj.entry, &devices_kset->list);
|
|
|
|
spin_unlock(&devices_kset->list_lock);
|
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_create_file - create sysfs attribute file for device.
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device attribute descriptor.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2009-12-18 20:34:19 +07:00
|
|
|
int device_create_file(struct device *dev,
|
|
|
|
const struct device_attribute *attr)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
int error = 0;
|
2013-02-20 15:31:42 +07:00
|
|
|
|
|
|
|
if (dev) {
|
|
|
|
WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
|
2013-05-16 13:31:30 +07:00
|
|
|
"Attribute %s: write permission without 'store'\n",
|
|
|
|
attr->attr.name);
|
2013-02-20 15:31:42 +07:00
|
|
|
WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
|
2013-05-16 13:31:30 +07:00
|
|
|
"Attribute %s: read permission without 'show'\n",
|
|
|
|
attr->attr.name);
|
2005-04-17 05:20:36 +07:00
|
|
|
error = sysfs_create_file(&dev->kobj, &attr->attr);
|
2013-02-20 15:31:42 +07:00
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
return error;
|
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_create_file);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_remove_file - remove sysfs attribute file.
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device attribute descriptor.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2009-12-18 20:34:19 +07:00
|
|
|
void device_remove_file(struct device *dev,
|
|
|
|
const struct device_attribute *attr)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-01-31 16:39:38 +07:00
|
|
|
if (dev)
|
2005-04-17 05:20:36 +07:00
|
|
|
sysfs_remove_file(&dev->kobj, &attr->attr);
|
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_remove_file);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
kernfs, sysfs, driver-core: implement kernfs_remove_self() and its wrappers
Sometimes it's necessary to implement a node which wants to delete
nodes including itself. This isn't straightforward because of kernfs
active reference. While a file operation is in progress, an active
reference is held and kernfs_remove() waits for all such references to
drain before completing. For a self-deleting node, this is a deadlock
as kernfs_remove() ends up waiting for an active reference that itself
is sitting on top of.
This currently is worked around in the sysfs layer using
sysfs_schedule_callback() which makes such removals asynchronous.
While it works, it's rather cumbersome and inherently breaks
synchronicity of the operation - the file operation which triggered
the operation may complete before the removal is finished (or even
started) and the removal may fail asynchronously. If a removal
operation is immmediately followed by another operation which expects
the specific name to be available (e.g. removal followed by rename
onto the same name), there's no way to make the latter operation
reliable.
The thing is there's no inherent reason for this to be asynchrnous.
All that's necessary to do this synchronous is a dedicated operation
which drops its own active ref and deactivates self. This patch
implements kernfs_remove_self() and its wrappers in sysfs and driver
core. kernfs_remove_self() is to be called from one of the file
operations, drops the active ref the task is holding, removes the self
node, and restores active ref to the dead node so that the ref is
balanced afterwards. __kernfs_remove() is updated so that it takes an
early exit if the target node is already fully removed so that the
active ref restored by kernfs_remove_self() after removal doesn't
confuse the deactivation path.
This makes implementing self-deleting nodes very easy. The normal
removal path doesn't even need to be changed to use
kernfs_remove_self() for the self-deleting node. The method can
invoke kernfs_remove_self() on itself before proceeding the normal
removal path. kernfs_remove() invoked on the node by the normal
deletion path will simply be ignored.
This will replace sysfs_schedule_callback(). A subtle feature of
sysfs_schedule_callback() is that it collapses multiple invocations -
even if multiple removals are triggered, the removal callback is run
only once. An equivalent effect can be achieved by testing the return
value of kernfs_remove_self() - only the one which gets %true return
value should proceed with actual deletion. All other instances of
kernfs_remove_self() will wait till the enclosing kernfs operation
which invoked the winning instance of kernfs_remove_self() finishes
and then return %false. This trivially makes all users of
kernfs_remove_self() automatically show correct synchronous behavior
even when there are multiple concurrent operations - all "echo 1 >
delete" instances will finish only after the whole operation is
completed by one of the instances.
Note that manipulation of active ref is implemented in separate public
functions - kernfs_[un]break_active_protection().
kernfs_remove_self() is the only user at the moment but this will be
used to cater to more complex cases.
v2: For !CONFIG_SYSFS, dummy version kernfs_remove_self() was missing
and sysfs_remove_file_self() had incorrect return type. Fix it.
Reported by kbuild test bot.
v3: kernfs_[un]break_active_protection() separated out from
kernfs_remove_self() and exposed as public API.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: kbuild test robot <fengguang.wu@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-02-04 02:03:01 +07:00
|
|
|
/**
|
|
|
|
* device_remove_file_self - remove sysfs attribute file from its own method.
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device attribute descriptor.
|
|
|
|
*
|
|
|
|
* See kernfs_remove_self() for details.
|
|
|
|
*/
|
|
|
|
bool device_remove_file_self(struct device *dev,
|
|
|
|
const struct device_attribute *attr)
|
|
|
|
{
|
|
|
|
if (dev)
|
|
|
|
return sysfs_remove_file_self(&dev->kobj, &attr->attr);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_remove_file_self);
|
|
|
|
|
2006-09-19 23:39:19 +07:00
|
|
|
/**
|
|
|
|
* device_create_bin_file - create sysfs binary attribute file for device.
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device binary attribute descriptor.
|
|
|
|
*/
|
2009-12-18 20:34:20 +07:00
|
|
|
int device_create_bin_file(struct device *dev,
|
|
|
|
const struct bin_attribute *attr)
|
2006-09-19 23:39:19 +07:00
|
|
|
{
|
|
|
|
int error = -EINVAL;
|
|
|
|
if (dev)
|
|
|
|
error = sysfs_create_bin_file(&dev->kobj, attr);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_create_bin_file);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_remove_bin_file - remove sysfs binary attribute file
|
|
|
|
* @dev: device.
|
|
|
|
* @attr: device binary attribute descriptor.
|
|
|
|
*/
|
2009-12-18 20:34:20 +07:00
|
|
|
void device_remove_bin_file(struct device *dev,
|
|
|
|
const struct bin_attribute *attr)
|
2006-09-19 23:39:19 +07:00
|
|
|
{
|
|
|
|
if (dev)
|
|
|
|
sysfs_remove_bin_file(&dev->kobj, attr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_remove_bin_file);
|
|
|
|
|
2005-09-07 06:56:51 +07:00
|
|
|
static void klist_children_get(struct klist_node *n)
|
|
|
|
{
|
2008-12-17 03:24:56 +07:00
|
|
|
struct device_private *p = to_device_private_parent(n);
|
|
|
|
struct device *dev = p->device;
|
2005-09-07 06:56:51 +07:00
|
|
|
|
|
|
|
get_device(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void klist_children_put(struct klist_node *n)
|
|
|
|
{
|
2008-12-17 03:24:56 +07:00
|
|
|
struct device_private *p = to_device_private_parent(n);
|
|
|
|
struct device *dev = p->device;
|
2005-09-07 06:56:51 +07:00
|
|
|
|
|
|
|
put_device(dev);
|
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_initialize - init device structure.
|
|
|
|
* @dev: device.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-09-03 23:26:40 +07:00
|
|
|
* This prepares the device for use by other layers by initializing
|
|
|
|
* its fields.
|
2008-01-25 13:50:12 +07:00
|
|
|
* It is the first half of device_register(), if called by
|
2008-09-03 23:26:40 +07:00
|
|
|
* that function, though it can also be called separately, so one
|
|
|
|
* may use @dev's fields. In particular, get_device()/put_device()
|
|
|
|
* may be used for reference counting of @dev after calling this
|
|
|
|
* function.
|
|
|
|
*
|
2012-01-17 23:39:00 +07:00
|
|
|
* All fields in @dev must be initialized by the caller to 0, except
|
|
|
|
* for those explicitly set to some other value. The simplest
|
|
|
|
* approach is to use kzalloc() to allocate the structure containing
|
|
|
|
* @dev.
|
|
|
|
*
|
2008-09-03 23:26:40 +07:00
|
|
|
* NOTE: Use put_device() to give up your reference instead of freeing
|
|
|
|
* @dev directly once you have called this function.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
void device_initialize(struct device *dev)
|
|
|
|
{
|
2007-11-01 22:29:06 +07:00
|
|
|
dev->kobj.kset = devices_kset;
|
2007-12-18 13:05:35 +07:00
|
|
|
kobject_init(&dev->kobj, &device_ktype);
|
2005-04-17 05:20:36 +07:00
|
|
|
INIT_LIST_HEAD(&dev->dma_pools);
|
2010-01-30 03:39:02 +07:00
|
|
|
mutex_init(&dev->mutex);
|
2010-03-19 07:37:42 +07:00
|
|
|
lockdep_set_novalidate_class(&dev->mutex);
|
devres: device resource management
Implement device resource management, in short, devres. A device
driver can allocate arbirary size of devres data which is associated
with a release function. On driver detach, release function is
invoked on the devres data, then, devres data is freed.
devreses are typed by associated release functions. Some devreses are
better represented by single instance of the type while others need
multiple instances sharing the same release function. Both usages are
supported.
devreses can be grouped using devres group such that a device driver
can easily release acquired resources halfway through initialization
or selectively release resources (e.g. resources for port 1 out of 4
ports).
This patch adds devres core including documentation and the following
managed interfaces.
* alloc/free : devm_kzalloc(), devm_kzfree()
* IO region : devm_request_region(), devm_release_region()
* IRQ : devm_request_irq(), devm_free_irq()
* DMA : dmam_alloc_coherent(), dmam_free_coherent(),
dmam_declare_coherent_memory(), dmam_pool_create(),
dmam_pool_destroy()
* PCI : pcim_enable_device(), pcim_pin_device(), pci_is_managed()
* iomap : devm_ioport_map(), devm_ioport_unmap(), devm_ioremap(),
devm_ioremap_nocache(), devm_iounmap(), pcim_iomap_table(),
pcim_iomap(), pcim_iounmap()
Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>
2007-01-20 14:00:26 +07:00
|
|
|
spin_lock_init(&dev->devres_lock);
|
|
|
|
INIT_LIST_HEAD(&dev->devres_head);
|
2008-08-08 00:06:12 +07:00
|
|
|
device_pm_init(dev);
|
2006-12-07 11:32:33 +07:00
|
|
|
set_dev_node(dev, -1);
|
2015-07-09 15:00:44 +07:00
|
|
|
#ifdef CONFIG_GENERIC_MSI_IRQ
|
|
|
|
INIT_LIST_HEAD(&dev->msi_list);
|
|
|
|
#endif
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
INIT_LIST_HEAD(&dev->links.consumers);
|
|
|
|
INIT_LIST_HEAD(&dev->links.suppliers);
|
|
|
|
dev->links.status = DL_DEV_NO_DRIVER;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_initialize);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2013-03-13 01:30:05 +07:00
|
|
|
struct kobject *virtual_device_parent(struct device *dev)
|
2006-10-24 00:40:54 +07:00
|
|
|
{
|
2007-03-14 09:25:56 +07:00
|
|
|
static struct kobject *virtual_dir = NULL;
|
2006-10-24 00:40:54 +07:00
|
|
|
|
2007-03-14 09:25:56 +07:00
|
|
|
if (!virtual_dir)
|
2007-11-06 13:24:43 +07:00
|
|
|
virtual_dir = kobject_create_and_add("virtual",
|
2007-11-01 22:29:06 +07:00
|
|
|
&devices_kset->kobj);
|
2006-10-24 00:40:54 +07:00
|
|
|
|
2007-03-14 09:25:56 +07:00
|
|
|
return virtual_dir;
|
2006-10-24 00:40:54 +07:00
|
|
|
}
|
|
|
|
|
2010-03-31 01:31:25 +07:00
|
|
|
struct class_dir {
|
|
|
|
struct kobject kobj;
|
|
|
|
struct class *class;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
|
|
|
|
|
|
|
|
static void class_dir_release(struct kobject *kobj)
|
|
|
|
{
|
|
|
|
struct class_dir *dir = to_class_dir(kobj);
|
|
|
|
kfree(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const
|
|
|
|
struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
|
2006-10-24 06:37:58 +07:00
|
|
|
{
|
2010-03-31 01:31:25 +07:00
|
|
|
struct class_dir *dir = to_class_dir(kobj);
|
|
|
|
return dir->class->ns_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kobj_type class_dir_ktype = {
|
|
|
|
.release = class_dir_release,
|
|
|
|
.sysfs_ops = &kobj_sysfs_ops,
|
|
|
|
.child_ns_type = class_dir_child_ns_type
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kobject *
|
|
|
|
class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
|
|
|
|
{
|
|
|
|
struct class_dir *dir;
|
2007-11-06 13:24:43 +07:00
|
|
|
int retval;
|
|
|
|
|
2010-03-31 01:31:25 +07:00
|
|
|
dir = kzalloc(sizeof(*dir), GFP_KERNEL);
|
|
|
|
if (!dir)
|
2018-05-07 17:10:31 +07:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2010-03-31 01:31:25 +07:00
|
|
|
|
|
|
|
dir->class = class;
|
|
|
|
kobject_init(&dir->kobj, &class_dir_ktype);
|
|
|
|
|
2010-11-16 05:13:18 +07:00
|
|
|
dir->kobj.kset = &class->p->glue_dirs;
|
2010-03-31 01:31:25 +07:00
|
|
|
|
|
|
|
retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
|
|
|
|
if (retval < 0) {
|
|
|
|
kobject_put(&dir->kobj);
|
2018-05-07 17:10:31 +07:00
|
|
|
return ERR_PTR(retval);
|
2010-03-31 01:31:25 +07:00
|
|
|
}
|
|
|
|
return &dir->kobj;
|
|
|
|
}
|
|
|
|
|
2014-11-07 11:05:49 +07:00
|
|
|
static DEFINE_MUTEX(gdp_mutex);
|
2010-03-31 01:31:25 +07:00
|
|
|
|
|
|
|
static struct kobject *get_device_parent(struct device *dev,
|
|
|
|
struct device *parent)
|
|
|
|
{
|
2007-03-14 09:25:56 +07:00
|
|
|
if (dev->class) {
|
|
|
|
struct kobject *kobj = NULL;
|
|
|
|
struct kobject *parent_kobj;
|
|
|
|
struct kobject *k;
|
|
|
|
|
2010-09-25 04:36:49 +07:00
|
|
|
#ifdef CONFIG_BLOCK
|
2010-09-05 12:33:14 +07:00
|
|
|
/* block disks show up in /sys/block */
|
2010-09-08 21:54:17 +07:00
|
|
|
if (sysfs_deprecated && dev->class == &block_class) {
|
2010-09-05 12:33:14 +07:00
|
|
|
if (parent && parent->class == &block_class)
|
|
|
|
return &parent->kobj;
|
2010-11-16 05:13:18 +07:00
|
|
|
return &block_class.p->subsys.kobj;
|
2010-09-05 12:33:14 +07:00
|
|
|
}
|
2010-09-25 04:36:49 +07:00
|
|
|
#endif
|
2010-09-08 21:54:17 +07:00
|
|
|
|
2007-03-14 09:25:56 +07:00
|
|
|
/*
|
|
|
|
* If we have no parent, we live in "virtual".
|
2007-12-19 07:40:42 +07:00
|
|
|
* Class-devices with a non class-device as parent, live
|
|
|
|
* in a "glue" directory to prevent namespace collisions.
|
2007-03-14 09:25:56 +07:00
|
|
|
*/
|
|
|
|
if (parent == NULL)
|
|
|
|
parent_kobj = virtual_device_parent(dev);
|
2010-07-25 12:43:35 +07:00
|
|
|
else if (parent->class && !dev->class->ns_type)
|
2007-03-14 09:25:56 +07:00
|
|
|
return &parent->kobj;
|
|
|
|
else
|
|
|
|
parent_kobj = &parent->kobj;
|
|
|
|
|
2010-02-05 15:57:02 +07:00
|
|
|
mutex_lock(&gdp_mutex);
|
|
|
|
|
2007-03-14 09:25:56 +07:00
|
|
|
/* find our class-directory at the parent and reference it */
|
2010-11-16 05:13:18 +07:00
|
|
|
spin_lock(&dev->class->p->glue_dirs.list_lock);
|
|
|
|
list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
|
2007-03-14 09:25:56 +07:00
|
|
|
if (k->parent == parent_kobj) {
|
|
|
|
kobj = kobject_get(k);
|
|
|
|
break;
|
|
|
|
}
|
2010-11-16 05:13:18 +07:00
|
|
|
spin_unlock(&dev->class->p->glue_dirs.list_lock);
|
2010-02-05 15:57:02 +07:00
|
|
|
if (kobj) {
|
|
|
|
mutex_unlock(&gdp_mutex);
|
2007-03-14 09:25:56 +07:00
|
|
|
return kobj;
|
2010-02-05 15:57:02 +07:00
|
|
|
}
|
2007-03-14 09:25:56 +07:00
|
|
|
|
|
|
|
/* or create a new class-directory at the parent device */
|
2010-03-31 01:31:25 +07:00
|
|
|
k = class_dir_create_and_add(dev->class, parent_kobj);
|
2007-12-19 07:40:42 +07:00
|
|
|
/* do not emit an uevent for this simple "glue" directory */
|
2010-02-05 15:57:02 +07:00
|
|
|
mutex_unlock(&gdp_mutex);
|
2007-11-06 13:24:43 +07:00
|
|
|
return k;
|
2007-03-14 09:25:56 +07:00
|
|
|
}
|
|
|
|
|
2011-12-15 05:29:38 +07:00
|
|
|
/* subsystems can specify a default root directory for their devices */
|
|
|
|
if (!parent && dev->bus && dev->bus->dev_root)
|
|
|
|
return &dev->bus->dev_root->kobj;
|
|
|
|
|
2007-03-14 09:25:56 +07:00
|
|
|
if (parent)
|
2007-01-09 02:16:44 +07:00
|
|
|
return &parent->kobj;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-21 23:29:15 +07:00
|
|
|
|
2016-07-10 18:27:36 +07:00
|
|
|
static inline bool live_in_glue_dir(struct kobject *kobj,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
if (!kobj || !dev->class ||
|
|
|
|
kobj->kset != &dev->class->p->glue_dirs)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct kobject *get_glue_dir(struct device *dev)
|
|
|
|
{
|
|
|
|
return dev->kobj.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make sure cleaning up dir as the last step, we need to make
|
|
|
|
* sure .release handler of kobject is run with holding the
|
|
|
|
* global lock
|
|
|
|
*/
|
2008-01-21 22:09:44 +07:00
|
|
|
static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
|
2007-11-21 23:29:15 +07:00
|
|
|
{
|
2007-12-19 07:40:42 +07:00
|
|
|
/* see if we live in a "glue" directory */
|
2016-07-10 18:27:36 +07:00
|
|
|
if (!live_in_glue_dir(glue_dir, dev))
|
2007-11-21 23:29:15 +07:00
|
|
|
return;
|
|
|
|
|
2014-11-07 11:05:49 +07:00
|
|
|
mutex_lock(&gdp_mutex);
|
drivers: core: Remove glue dirs from sysfs earlier
For devices with a class, we create a "glue" directory between
the parent device and the new device with the class name.
This directory is never "explicitely" removed when empty however,
this is left to the implicit sysfs removal done by kobject_release()
when the object loses its last reference via kobject_put().
This is problematic because as long as it's not been removed from
sysfs, it is still present in the class kset and in sysfs directory
structure.
The presence in the class kset exposes a use after free bug fixed
by the previous patch, but the presence in sysfs means that until
the kobject is released, which can take a while (especially with
kobject debugging), any attempt at re-creating such as binding a
new device for that class/parent pair, will result in a sysfs
duplicate file name error.
This fixes it by instead doing an explicit kobject_del() when
the glue dir is empty, by keeping track of the number of
child devices of the gluedir.
This is made easy by the fact that all glue dir operations are
done with a global mutex, and there's already a function
(cleanup_glue_dir) called in all the right places taking that
mutex that can be enhanced for this. It appears that this was
in fact the intent of the function, but the implementation was
wrong.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-07-10 07:29:10 +07:00
|
|
|
if (!kobject_has_children(glue_dir))
|
|
|
|
kobject_del(glue_dir);
|
2007-12-19 07:40:42 +07:00
|
|
|
kobject_put(glue_dir);
|
2014-11-07 11:05:49 +07:00
|
|
|
mutex_unlock(&gdp_mutex);
|
2007-11-21 23:29:15 +07:00
|
|
|
}
|
2008-01-21 22:09:44 +07:00
|
|
|
|
2007-07-18 15:43:47 +07:00
|
|
|
static int device_add_class_symlinks(struct device *dev)
|
|
|
|
{
|
drivers/core/of: Add symlink to device-tree from devices with an OF node
So I've been annoyed lately with having a bunch of devices such as i2c
eeproms (for use by VPDs, server world !) and other bits and pieces that
I want to be able to identify from userspace, and possibly provide
additional data about from FW.
Basically, it boils down to correlating the sysfs device with the OF
tree device node, so that user space can use device-tree info such as
additional "location" or "label" (or whatever else we can come up with)
propreties to identify a given device, or get some attributes of use
about it, etc...
Now, so far, we've done that in some subsystem in a fairly ad-hoc basis
using "devspec" properties. For example, PCI creates them if it can
correlate the probed device with a DT node. Some powerpc specific busses
do that too.
However, i2c doesn't and it would be nice to have something more generic
since technically any device can have a corresponding device tree node.
This patch adds an "of_node" symlink to devices that have a non-NULL
dev->of_node pointer, the patch is pretty trivial and seems to work just
fine for me.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-02-18 07:25:18 +07:00
|
|
|
struct device_node *of_node = dev_of_node(dev);
|
2007-07-18 15:43:47 +07:00
|
|
|
int error;
|
|
|
|
|
drivers/core/of: Add symlink to device-tree from devices with an OF node
So I've been annoyed lately with having a bunch of devices such as i2c
eeproms (for use by VPDs, server world !) and other bits and pieces that
I want to be able to identify from userspace, and possibly provide
additional data about from FW.
Basically, it boils down to correlating the sysfs device with the OF
tree device node, so that user space can use device-tree info such as
additional "location" or "label" (or whatever else we can come up with)
propreties to identify a given device, or get some attributes of use
about it, etc...
Now, so far, we've done that in some subsystem in a fairly ad-hoc basis
using "devspec" properties. For example, PCI creates them if it can
correlate the probed device with a DT node. Some powerpc specific busses
do that too.
However, i2c doesn't and it would be nice to have something more generic
since technically any device can have a corresponding device tree node.
This patch adds an "of_node" symlink to devices that have a non-NULL
dev->of_node pointer, the patch is pretty trivial and seems to work just
fine for me.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-02-18 07:25:18 +07:00
|
|
|
if (of_node) {
|
2017-10-05 02:04:01 +07:00
|
|
|
error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
|
drivers/core/of: Add symlink to device-tree from devices with an OF node
So I've been annoyed lately with having a bunch of devices such as i2c
eeproms (for use by VPDs, server world !) and other bits and pieces that
I want to be able to identify from userspace, and possibly provide
additional data about from FW.
Basically, it boils down to correlating the sysfs device with the OF
tree device node, so that user space can use device-tree info such as
additional "location" or "label" (or whatever else we can come up with)
propreties to identify a given device, or get some attributes of use
about it, etc...
Now, so far, we've done that in some subsystem in a fairly ad-hoc basis
using "devspec" properties. For example, PCI creates them if it can
correlate the probed device with a DT node. Some powerpc specific busses
do that too.
However, i2c doesn't and it would be nice to have something more generic
since technically any device can have a corresponding device tree node.
This patch adds an "of_node" symlink to devices that have a non-NULL
dev->of_node pointer, the patch is pretty trivial and seems to work just
fine for me.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-02-18 07:25:18 +07:00
|
|
|
if (error)
|
|
|
|
dev_warn(dev, "Error %d creating of_node link\n",error);
|
|
|
|
/* An error here doesn't warrant bringing down the device */
|
|
|
|
}
|
|
|
|
|
2007-07-18 15:43:47 +07:00
|
|
|
if (!dev->class)
|
|
|
|
return 0;
|
2007-11-21 23:29:15 +07:00
|
|
|
|
2008-05-28 23:28:39 +07:00
|
|
|
error = sysfs_create_link(&dev->kobj,
|
2010-11-16 05:13:18 +07:00
|
|
|
&dev->class->p->subsys.kobj,
|
2007-07-18 15:43:47 +07:00
|
|
|
"subsystem");
|
|
|
|
if (error)
|
drivers/core/of: Add symlink to device-tree from devices with an OF node
So I've been annoyed lately with having a bunch of devices such as i2c
eeproms (for use by VPDs, server world !) and other bits and pieces that
I want to be able to identify from userspace, and possibly provide
additional data about from FW.
Basically, it boils down to correlating the sysfs device with the OF
tree device node, so that user space can use device-tree info such as
additional "location" or "label" (or whatever else we can come up with)
propreties to identify a given device, or get some attributes of use
about it, etc...
Now, so far, we've done that in some subsystem in a fairly ad-hoc basis
using "devspec" properties. For example, PCI creates them if it can
correlate the probed device with a DT node. Some powerpc specific busses
do that too.
However, i2c doesn't and it would be nice to have something more generic
since technically any device can have a corresponding device tree node.
This patch adds an "of_node" symlink to devices that have a non-NULL
dev->of_node pointer, the patch is pretty trivial and seems to work just
fine for me.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-02-18 07:25:18 +07:00
|
|
|
goto out_devnode;
|
2007-11-21 23:29:15 +07:00
|
|
|
|
2008-01-28 03:12:43 +07:00
|
|
|
if (dev->parent && device_is_not_partition(dev)) {
|
2010-09-05 12:33:14 +07:00
|
|
|
error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
|
2007-09-19 12:46:50 +07:00
|
|
|
"device");
|
|
|
|
if (error)
|
2010-09-05 12:33:14 +07:00
|
|
|
goto out_subsys;
|
2007-07-18 15:43:47 +07:00
|
|
|
}
|
|
|
|
|
2010-09-25 04:36:49 +07:00
|
|
|
#ifdef CONFIG_BLOCK
|
2010-09-05 12:33:14 +07:00
|
|
|
/* /sys/block has directories and does not need symlinks */
|
2010-09-08 21:54:17 +07:00
|
|
|
if (sysfs_deprecated && dev->class == &block_class)
|
2010-09-05 12:33:14 +07:00
|
|
|
return 0;
|
2010-09-25 04:36:49 +07:00
|
|
|
#endif
|
2010-09-05 12:33:14 +07:00
|
|
|
|
2007-11-21 23:29:15 +07:00
|
|
|
/* link in the class directory pointing to the device */
|
2010-11-16 05:13:18 +07:00
|
|
|
error = sysfs_create_link(&dev->class->p->subsys.kobj,
|
2008-10-30 07:36:48 +07:00
|
|
|
&dev->kobj, dev_name(dev));
|
2007-11-21 23:29:15 +07:00
|
|
|
if (error)
|
2010-09-05 12:33:14 +07:00
|
|
|
goto out_device;
|
2007-11-21 23:29:15 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2010-09-05 12:33:14 +07:00
|
|
|
out_device:
|
|
|
|
sysfs_remove_link(&dev->kobj, "device");
|
2007-11-21 23:29:15 +07:00
|
|
|
|
2007-07-18 15:43:47 +07:00
|
|
|
out_subsys:
|
|
|
|
sysfs_remove_link(&dev->kobj, "subsystem");
|
drivers/core/of: Add symlink to device-tree from devices with an OF node
So I've been annoyed lately with having a bunch of devices such as i2c
eeproms (for use by VPDs, server world !) and other bits and pieces that
I want to be able to identify from userspace, and possibly provide
additional data about from FW.
Basically, it boils down to correlating the sysfs device with the OF
tree device node, so that user space can use device-tree info such as
additional "location" or "label" (or whatever else we can come up with)
propreties to identify a given device, or get some attributes of use
about it, etc...
Now, so far, we've done that in some subsystem in a fairly ad-hoc basis
using "devspec" properties. For example, PCI creates them if it can
correlate the probed device with a DT node. Some powerpc specific busses
do that too.
However, i2c doesn't and it would be nice to have something more generic
since technically any device can have a corresponding device tree node.
This patch adds an "of_node" symlink to devices that have a non-NULL
dev->of_node pointer, the patch is pretty trivial and seems to work just
fine for me.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-02-18 07:25:18 +07:00
|
|
|
out_devnode:
|
|
|
|
sysfs_remove_link(&dev->kobj, "of_node");
|
2007-07-18 15:43:47 +07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_remove_class_symlinks(struct device *dev)
|
|
|
|
{
|
drivers/core/of: Add symlink to device-tree from devices with an OF node
So I've been annoyed lately with having a bunch of devices such as i2c
eeproms (for use by VPDs, server world !) and other bits and pieces that
I want to be able to identify from userspace, and possibly provide
additional data about from FW.
Basically, it boils down to correlating the sysfs device with the OF
tree device node, so that user space can use device-tree info such as
additional "location" or "label" (or whatever else we can come up with)
propreties to identify a given device, or get some attributes of use
about it, etc...
Now, so far, we've done that in some subsystem in a fairly ad-hoc basis
using "devspec" properties. For example, PCI creates them if it can
correlate the probed device with a DT node. Some powerpc specific busses
do that too.
However, i2c doesn't and it would be nice to have something more generic
since technically any device can have a corresponding device tree node.
This patch adds an "of_node" symlink to devices that have a non-NULL
dev->of_node pointer, the patch is pretty trivial and seems to work just
fine for me.
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-02-18 07:25:18 +07:00
|
|
|
if (dev_of_node(dev))
|
|
|
|
sysfs_remove_link(&dev->kobj, "of_node");
|
|
|
|
|
2007-07-18 15:43:47 +07:00
|
|
|
if (!dev->class)
|
|
|
|
return;
|
2007-11-21 23:29:15 +07:00
|
|
|
|
2008-01-28 03:12:43 +07:00
|
|
|
if (dev->parent && device_is_not_partition(dev))
|
2007-11-21 23:29:15 +07:00
|
|
|
sysfs_remove_link(&dev->kobj, "device");
|
2007-07-18 15:43:47 +07:00
|
|
|
sysfs_remove_link(&dev->kobj, "subsystem");
|
2010-09-25 04:36:49 +07:00
|
|
|
#ifdef CONFIG_BLOCK
|
2010-09-08 21:54:17 +07:00
|
|
|
if (sysfs_deprecated && dev->class == &block_class)
|
2010-09-05 12:33:14 +07:00
|
|
|
return;
|
2010-09-25 04:36:49 +07:00
|
|
|
#endif
|
2010-11-16 05:13:18 +07:00
|
|
|
sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
|
2007-07-18 15:43:47 +07:00
|
|
|
}
|
|
|
|
|
2008-05-30 07:16:40 +07:00
|
|
|
/**
|
|
|
|
* dev_set_name - set a device name
|
|
|
|
* @dev: device
|
2008-06-05 11:40:43 +07:00
|
|
|
* @fmt: format string for the device's name
|
2008-05-30 07:16:40 +07:00
|
|
|
*/
|
|
|
|
int dev_set_name(struct device *dev, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list vargs;
|
2009-01-25 21:17:37 +07:00
|
|
|
int err;
|
2008-05-30 07:16:40 +07:00
|
|
|
|
|
|
|
va_start(vargs, fmt);
|
2009-01-25 21:17:37 +07:00
|
|
|
err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
|
2008-05-30 07:16:40 +07:00
|
|
|
va_end(vargs);
|
2009-01-25 21:17:37 +07:00
|
|
|
return err;
|
2008-05-30 07:16:40 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dev_set_name);
|
|
|
|
|
2008-04-22 00:51:07 +07:00
|
|
|
/**
|
|
|
|
* device_to_dev_kobj - select a /sys/dev/ directory for the device
|
|
|
|
* @dev: device
|
|
|
|
*
|
|
|
|
* By default we select char/ for new entries. Setting class->dev_obj
|
|
|
|
* to NULL prevents an entry from being created. class->dev_kobj must
|
|
|
|
* be set (or cleared) before any devices are registered to the class
|
|
|
|
* otherwise device_create_sys_dev_entry() and
|
2012-04-17 17:12:57 +07:00
|
|
|
* device_remove_sys_dev_entry() will disagree about the presence of
|
|
|
|
* the link.
|
2008-04-22 00:51:07 +07:00
|
|
|
*/
|
|
|
|
static struct kobject *device_to_dev_kobj(struct device *dev)
|
|
|
|
{
|
|
|
|
struct kobject *kobj;
|
|
|
|
|
|
|
|
if (dev->class)
|
|
|
|
kobj = dev->class->dev_kobj;
|
|
|
|
else
|
|
|
|
kobj = sysfs_dev_char_kobj;
|
|
|
|
|
|
|
|
return kobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int device_create_sys_dev_entry(struct device *dev)
|
|
|
|
{
|
|
|
|
struct kobject *kobj = device_to_dev_kobj(dev);
|
|
|
|
int error = 0;
|
|
|
|
char devt_str[15];
|
|
|
|
|
|
|
|
if (kobj) {
|
|
|
|
format_dev_t(devt_str, dev->devt);
|
|
|
|
error = sysfs_create_link(kobj, &dev->kobj, devt_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_remove_sys_dev_entry(struct device *dev)
|
|
|
|
{
|
|
|
|
struct kobject *kobj = device_to_dev_kobj(dev);
|
|
|
|
char devt_str[15];
|
|
|
|
|
|
|
|
if (kobj) {
|
|
|
|
format_dev_t(devt_str, dev->devt);
|
|
|
|
sysfs_remove_link(kobj, devt_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 17:08:56 +07:00
|
|
|
static int device_private_init(struct device *dev)
|
2009-05-12 04:16:57 +07:00
|
|
|
{
|
|
|
|
dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
|
|
|
|
if (!dev->p)
|
|
|
|
return -ENOMEM;
|
|
|
|
dev->p->device = dev;
|
|
|
|
klist_init(&dev->p->klist_children, klist_children_get,
|
|
|
|
klist_children_put);
|
2012-03-09 03:17:22 +07:00
|
|
|
INIT_LIST_HEAD(&dev->p->deferred_probe);
|
2009-05-12 04:16:57 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_add - add device to device hierarchy.
|
|
|
|
* @dev: device.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* This is part 2 of device_register(), though may be called
|
|
|
|
* separately _iff_ device_initialize() has been called separately.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-09-03 23:26:40 +07:00
|
|
|
* This adds @dev to the kobject hierarchy via kobject_add(), adds it
|
2008-01-25 13:50:12 +07:00
|
|
|
* to the global and sibling lists for the device, then
|
|
|
|
* adds it to the other relevant subsystems of the driver model.
|
2008-09-03 23:26:40 +07:00
|
|
|
*
|
2012-01-17 23:39:00 +07:00
|
|
|
* Do not call this routine or device_register() more than once for
|
|
|
|
* any device structure. The driver model core is not designed to work
|
|
|
|
* with devices that get unregistered and then spring back to life.
|
|
|
|
* (Among other things, it's very hard to guarantee that all references
|
|
|
|
* to the previous incarnation of @dev have been dropped.) Allocate
|
|
|
|
* and register a fresh new struct device instead.
|
|
|
|
*
|
2008-09-03 23:26:40 +07:00
|
|
|
* NOTE: _Never_ directly free @dev after calling this function, even
|
|
|
|
* if it returned an error! Always use put_device() to give up your
|
|
|
|
* reference instead.
|
2019-04-19 00:41:56 +07:00
|
|
|
*
|
|
|
|
* Rule of thumb is: if device_add() succeeds, you should call
|
|
|
|
* device_del() when you want to get rid of it. If device_add() has
|
|
|
|
* *not* succeeded, use *only* put_device() to drop the reference
|
|
|
|
* count.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
int device_add(struct device *dev)
|
|
|
|
{
|
2017-03-17 13:54:22 +07:00
|
|
|
struct device *parent;
|
2011-12-15 05:29:38 +07:00
|
|
|
struct kobject *kobj;
|
2006-09-13 20:34:05 +07:00
|
|
|
struct class_interface *class_intf;
|
2008-05-31 00:45:12 +07:00
|
|
|
int error = -EINVAL;
|
2016-07-10 18:27:36 +07:00
|
|
|
struct kobject *glue_dir = NULL;
|
2008-01-13 02:40:46 +07:00
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
dev = get_device(dev);
|
2008-05-31 00:45:12 +07:00
|
|
|
if (!dev)
|
|
|
|
goto done;
|
|
|
|
|
2008-12-17 03:23:36 +07:00
|
|
|
if (!dev->p) {
|
2009-05-12 04:16:57 +07:00
|
|
|
error = device_private_init(dev);
|
|
|
|
if (error)
|
|
|
|
goto done;
|
2008-12-17 03:23:36 +07:00
|
|
|
}
|
|
|
|
|
2009-01-25 21:17:37 +07:00
|
|
|
/*
|
|
|
|
* for statically allocated devices, which should all be converted
|
|
|
|
* some day, we need to initialize the name. We prevent reading back
|
|
|
|
* the name, and force the use of dev_name()
|
|
|
|
*/
|
|
|
|
if (dev->init_name) {
|
2009-06-03 05:39:55 +07:00
|
|
|
dev_set_name(dev, "%s", dev->init_name);
|
2009-01-25 21:17:37 +07:00
|
|
|
dev->init_name = NULL;
|
|
|
|
}
|
2008-05-31 00:45:12 +07:00
|
|
|
|
2011-12-15 05:29:38 +07:00
|
|
|
/* subsystems can specify simple device enumeration */
|
|
|
|
if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
|
|
|
|
dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
|
|
|
|
|
2009-12-11 02:32:49 +07:00
|
|
|
if (!dev_name(dev)) {
|
|
|
|
error = -EINVAL;
|
2009-05-29 04:24:07 +07:00
|
|
|
goto name_error;
|
2009-12-11 02:32:49 +07:00
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-10-30 07:36:48 +07:00
|
|
|
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
|
2006-08-08 12:19:37 +07:00
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
parent = get_device(dev->parent);
|
2011-12-15 05:29:38 +07:00
|
|
|
kobj = get_device_parent(dev, parent);
|
2018-05-07 17:10:31 +07:00
|
|
|
if (IS_ERR(kobj)) {
|
|
|
|
error = PTR_ERR(kobj);
|
|
|
|
goto parent_error;
|
|
|
|
}
|
2011-12-15 05:29:38 +07:00
|
|
|
if (kobj)
|
|
|
|
dev->kobj.parent = kobj;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-02-19 18:20:41 +07:00
|
|
|
/* use parent numa_node */
|
2015-08-25 11:08:22 +07:00
|
|
|
if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
|
2008-02-19 18:20:41 +07:00
|
|
|
set_dev_node(dev, dev_to_node(parent));
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/* first, register with generic layer. */
|
2009-04-19 05:05:45 +07:00
|
|
|
/* we require the name to be set before, and pass NULL */
|
|
|
|
error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
|
2016-07-10 18:27:36 +07:00
|
|
|
if (error) {
|
|
|
|
glue_dir = get_glue_dir(dev);
|
2005-04-17 05:20:36 +07:00
|
|
|
goto Error;
|
2016-07-10 18:27:36 +07:00
|
|
|
}
|
2005-10-01 19:49:43 +07:00
|
|
|
|
2006-08-15 12:43:19 +07:00
|
|
|
/* notify platform of device entry */
|
2018-11-09 21:21:34 +07:00
|
|
|
error = device_platform_notify(dev, KOBJ_ADD);
|
|
|
|
if (error)
|
|
|
|
goto platform_error;
|
2006-08-15 12:43:19 +07:00
|
|
|
|
2013-08-24 07:07:26 +07:00
|
|
|
error = device_create_file(dev, &dev_attr_uevent);
|
2006-09-22 16:37:13 +07:00
|
|
|
if (error)
|
|
|
|
goto attrError;
|
2005-10-01 19:49:43 +07:00
|
|
|
|
2007-07-18 15:43:47 +07:00
|
|
|
error = device_add_class_symlinks(dev);
|
|
|
|
if (error)
|
|
|
|
goto SymlinkError;
|
2007-07-10 01:39:18 +07:00
|
|
|
error = device_add_attrs(dev);
|
|
|
|
if (error)
|
2006-06-29 06:19:58 +07:00
|
|
|
goto AttrsError;
|
2007-07-10 01:39:18 +07:00
|
|
|
error = bus_add_device(dev);
|
|
|
|
if (error)
|
2005-04-17 05:20:36 +07:00
|
|
|
goto BusError;
|
2008-08-08 00:06:12 +07:00
|
|
|
error = dpm_sysfs_add(dev);
|
2008-03-12 06:59:38 +07:00
|
|
|
if (error)
|
2008-08-08 00:06:12 +07:00
|
|
|
goto DPMError;
|
|
|
|
device_pm_add(dev);
|
2008-12-06 02:10:31 +07:00
|
|
|
|
2014-10-08 14:31:54 +07:00
|
|
|
if (MAJOR(dev->devt)) {
|
|
|
|
error = device_create_file(dev, &dev_attr_dev);
|
|
|
|
if (error)
|
|
|
|
goto DevAttrError;
|
|
|
|
|
|
|
|
error = device_create_sys_dev_entry(dev);
|
|
|
|
if (error)
|
|
|
|
goto SysEntryError;
|
|
|
|
|
|
|
|
devtmpfs_create_node(dev);
|
|
|
|
}
|
|
|
|
|
2008-12-06 02:10:31 +07:00
|
|
|
/* Notify clients of device addition. This call must come
|
2012-01-11 22:12:06 +07:00
|
|
|
* after dpm_sysfs_add() and before kobject_uevent().
|
2008-12-06 02:10:31 +07:00
|
|
|
*/
|
|
|
|
if (dev->bus)
|
|
|
|
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
|
|
|
|
BUS_NOTIFY_ADD_DEVICE, dev);
|
|
|
|
|
2007-03-29 16:12:11 +07:00
|
|
|
kobject_uevent(&dev->kobj, KOBJ_ADD);
|
2009-07-31 02:27:18 +07:00
|
|
|
bus_probe_device(dev);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (parent)
|
2008-12-17 03:24:56 +07:00
|
|
|
klist_add_tail(&dev->p->knode_parent,
|
|
|
|
&parent->p->klist_children);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2006-06-23 07:17:32 +07:00
|
|
|
if (dev->class) {
|
2011-12-15 05:29:38 +07:00
|
|
|
mutex_lock(&dev->class->p->mutex);
|
2006-09-13 20:34:05 +07:00
|
|
|
/* tie the class to the device */
|
2019-01-18 09:34:59 +07:00
|
|
|
klist_add_tail(&dev->p->knode_class,
|
2010-11-16 05:13:18 +07:00
|
|
|
&dev->class->p->klist_devices);
|
2006-09-13 20:34:05 +07:00
|
|
|
|
|
|
|
/* notify any interfaces that the device is here */
|
2008-05-28 23:28:39 +07:00
|
|
|
list_for_each_entry(class_intf,
|
2011-12-15 05:29:38 +07:00
|
|
|
&dev->class->p->interfaces, node)
|
2006-09-13 20:34:05 +07:00
|
|
|
if (class_intf->add_dev)
|
|
|
|
class_intf->add_dev(dev, class_intf);
|
2011-12-15 05:29:38 +07:00
|
|
|
mutex_unlock(&dev->class->p->mutex);
|
2006-06-23 07:17:32 +07:00
|
|
|
}
|
2008-05-31 00:45:12 +07:00
|
|
|
done:
|
2005-04-17 05:20:36 +07:00
|
|
|
put_device(dev);
|
|
|
|
return error;
|
2014-10-08 14:31:54 +07:00
|
|
|
SysEntryError:
|
|
|
|
if (MAJOR(dev->devt))
|
|
|
|
device_remove_file(dev, &dev_attr_dev);
|
|
|
|
DevAttrError:
|
|
|
|
device_pm_remove(dev);
|
|
|
|
dpm_sysfs_remove(dev);
|
2008-08-08 00:06:12 +07:00
|
|
|
DPMError:
|
2008-03-12 06:59:38 +07:00
|
|
|
bus_remove_device(dev);
|
|
|
|
BusError:
|
2007-02-22 00:44:51 +07:00
|
|
|
device_remove_attrs(dev);
|
2006-06-29 06:19:58 +07:00
|
|
|
AttrsError:
|
2007-07-18 15:43:47 +07:00
|
|
|
device_remove_class_symlinks(dev);
|
|
|
|
SymlinkError:
|
2013-08-24 07:07:26 +07:00
|
|
|
device_remove_file(dev, &dev_attr_uevent);
|
2006-06-15 02:14:34 +07:00
|
|
|
attrError:
|
2018-11-09 21:21:34 +07:00
|
|
|
device_platform_notify(dev, KOBJ_REMOVE);
|
|
|
|
platform_error:
|
2005-11-16 15:00:00 +07:00
|
|
|
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
|
2016-07-10 18:27:36 +07:00
|
|
|
glue_dir = get_glue_dir(dev);
|
2005-04-17 05:20:36 +07:00
|
|
|
kobject_del(&dev->kobj);
|
|
|
|
Error:
|
2016-07-10 18:27:36 +07:00
|
|
|
cleanup_glue_dir(dev, glue_dir);
|
2018-05-07 17:10:31 +07:00
|
|
|
parent_error:
|
2015-02-05 17:48:26 +07:00
|
|
|
put_device(parent);
|
2009-05-29 04:24:07 +07:00
|
|
|
name_error:
|
|
|
|
kfree(dev->p);
|
|
|
|
dev->p = NULL;
|
2008-05-31 00:45:12 +07:00
|
|
|
goto done;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_add);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_register - register a device with the system.
|
|
|
|
* @dev: pointer to the device structure
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* This happens in two clean steps - initialize the device
|
|
|
|
* and add it to the system. The two steps can be called
|
|
|
|
* separately, but this is the easiest and most common.
|
|
|
|
* I.e. you should only call the two helpers separately if
|
|
|
|
* have a clearly defined need to use and refcount the device
|
|
|
|
* before it is added to the hierarchy.
|
2008-09-03 23:26:40 +07:00
|
|
|
*
|
2012-01-17 23:39:00 +07:00
|
|
|
* For more information, see the kerneldoc for device_initialize()
|
|
|
|
* and device_add().
|
|
|
|
*
|
2008-09-03 23:26:40 +07:00
|
|
|
* NOTE: _Never_ directly free @dev after calling this function, even
|
|
|
|
* if it returned an error! Always use put_device() to give up the
|
|
|
|
* reference initialized in this function instead.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
int device_register(struct device *dev)
|
|
|
|
{
|
|
|
|
device_initialize(dev);
|
|
|
|
return device_add(dev);
|
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_register);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* get_device - increment reference count for device.
|
|
|
|
* @dev: device.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* This simply forwards the call to kobject_get(), though
|
|
|
|
* we do take care to provide for the case that we get a NULL
|
|
|
|
* pointer passed in.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2008-01-25 13:50:12 +07:00
|
|
|
struct device *get_device(struct device *dev)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2012-07-03 23:49:36 +07:00
|
|
|
return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(get_device);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* put_device - decrement reference count.
|
|
|
|
* @dev: device in question.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2008-01-25 13:50:12 +07:00
|
|
|
void put_device(struct device *dev)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2007-05-22 03:08:01 +07:00
|
|
|
/* might_sleep(); */
|
2005-04-17 05:20:36 +07:00
|
|
|
if (dev)
|
|
|
|
kobject_put(&dev->kobj);
|
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(put_device);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_del - delete device from system.
|
|
|
|
* @dev: device.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* This is the first part of the device unregistration
|
|
|
|
* sequence. This removes the device from the lists we control
|
|
|
|
* from here, has it removed from the other driver model
|
|
|
|
* subsystems it was added to in device_add(), and removes it
|
|
|
|
* from the kobject hierarchy.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* NOTE: this should be called manually _iff_ device_add() was
|
|
|
|
* also called manually.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2008-01-25 13:50:12 +07:00
|
|
|
void device_del(struct device *dev)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-01-25 13:50:12 +07:00
|
|
|
struct device *parent = dev->parent;
|
2016-07-10 18:27:36 +07:00
|
|
|
struct kobject *glue_dir = NULL;
|
2006-09-13 20:34:05 +07:00
|
|
|
struct class_interface *class_intf;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2019-01-23 01:39:10 +07:00
|
|
|
/*
|
|
|
|
* Hold the device lock and set the "dead" flag to guarantee that
|
|
|
|
* the update behavior is consistent with the other bitfields near
|
|
|
|
* it and that we cannot have an asynchronous probe routine trying
|
|
|
|
* to run while we are tearing out the bus/class/sysfs from
|
|
|
|
* underneath the device.
|
|
|
|
*/
|
|
|
|
device_lock(dev);
|
|
|
|
dev->p->dead = true;
|
|
|
|
device_unlock(dev);
|
|
|
|
|
2008-12-06 02:10:31 +07:00
|
|
|
/* Notify clients of device removal. This call must come
|
|
|
|
* before dpm_sysfs_remove().
|
|
|
|
*/
|
|
|
|
if (dev->bus)
|
|
|
|
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
|
|
|
|
BUS_NOTIFY_DEL_DEVICE, dev);
|
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>
2016-10-30 23:32:16 +07:00
|
|
|
|
2008-08-08 00:06:12 +07:00
|
|
|
dpm_sysfs_remove(dev);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (parent)
|
2008-12-17 03:24:56 +07:00
|
|
|
klist_del(&dev->p->knode_parent);
|
2008-04-22 00:51:07 +07:00
|
|
|
if (MAJOR(dev->devt)) {
|
Driver Core: devtmpfs - kernel-maintained tmpfs-based /dev
Devtmpfs lets the kernel create a tmpfs instance called devtmpfs
very early at kernel initialization, before any driver-core device
is registered. Every device with a major/minor will provide a
device node in devtmpfs.
Devtmpfs can be changed and altered by userspace at any time,
and in any way needed - just like today's udev-mounted tmpfs.
Unmodified udev versions will run just fine on top of it, and will
recognize an already existing kernel-created device node and use it.
The default node permissions are root:root 0600. Proper permissions
and user/group ownership, meaningful symlinks, all other policy still
needs to be applied by userspace.
If a node is created by devtmps, devtmpfs will remove the device node
when the device goes away. If the device node was created by
userspace, or the devtmpfs created node was replaced by userspace, it
will no longer be removed by devtmpfs.
If it is requested to auto-mount it, it makes init=/bin/sh work
without any further userspace support. /dev will be fully populated
and dynamic, and always reflect the current device state of the kernel.
With the commonly used dynamic device numbers, it solves the problem
where static devices nodes may point to the wrong devices.
It is intended to make the initial bootup logic simpler and more robust,
by de-coupling the creation of the inital environment, to reliably run
userspace processes, from a complex userspace bootstrap logic to provide
a working /dev.
Signed-off-by: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Jan Blunck <jblunck@suse.de>
Tested-By: Harald Hoyer <harald@redhat.com>
Tested-By: Scott James Remnant <scott@ubuntu.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-04-30 20:23:42 +07:00
|
|
|
devtmpfs_delete_node(dev);
|
2008-04-22 00:51:07 +07:00
|
|
|
device_remove_sys_dev_entry(dev);
|
2013-08-24 07:07:26 +07:00
|
|
|
device_remove_file(dev, &dev_attr_dev);
|
2008-04-22 00:51:07 +07:00
|
|
|
}
|
2006-06-15 20:31:56 +07:00
|
|
|
if (dev->class) {
|
2007-11-21 23:29:15 +07:00
|
|
|
device_remove_class_symlinks(dev);
|
2006-09-14 16:23:28 +07:00
|
|
|
|
2011-12-15 05:29:38 +07:00
|
|
|
mutex_lock(&dev->class->p->mutex);
|
2006-09-13 20:34:05 +07:00
|
|
|
/* notify any interfaces that the device is now gone */
|
2008-05-28 23:28:39 +07:00
|
|
|
list_for_each_entry(class_intf,
|
2011-12-15 05:29:38 +07:00
|
|
|
&dev->class->p->interfaces, node)
|
2006-09-13 20:34:05 +07:00
|
|
|
if (class_intf->remove_dev)
|
|
|
|
class_intf->remove_dev(dev, class_intf);
|
|
|
|
/* remove the device from the class list */
|
2019-01-18 09:34:59 +07:00
|
|
|
klist_del(&dev->p->knode_class);
|
2011-12-15 05:29:38 +07:00
|
|
|
mutex_unlock(&dev->class->p->mutex);
|
2006-06-15 20:31:56 +07:00
|
|
|
}
|
2013-08-24 07:07:26 +07:00
|
|
|
device_remove_file(dev, &dev_attr_uevent);
|
2006-06-29 06:19:58 +07:00
|
|
|
device_remove_attrs(dev);
|
2006-11-09 10:46:14 +07:00
|
|
|
bus_remove_device(dev);
|
driver core / PM: move the calling to device_pm_remove behind the calling to bus_remove_device
We hit an hang issue when removing a mmc device on Medfield Android phone by sysfs interface.
device_pm_remove will call pm_runtime_remove which would disable
runtime PM of the device. After that pm_runtime_get* or
pm_runtime_put* will be ignored. So if we disable the runtime PM
before device really be removed, drivers' _remove callback may
access HW even pm_runtime_get* fails. That is bad.
Consider below call sequence when removing a device:
device_del => device_pm_remove
=> class_intf->remove_dev(dev, class_intf) => pm_runtime_get_sync/put_sync
=> bus_remove_device => device_release_driver => pm_runtime_get_sync/put_sync
remove_dev might call pm_runtime_get_sync/put_sync.
Then, generic device_release_driver also calls pm_runtime_get_sync/put_sync.
Since device_del => device_pm_remove firstly, later _get_sync wouldn't really wake up the device.
I git log -p to find the patch which moves the calling to device_pm_remove ahead.
It's below patch:
commit 775b64d2b6ca37697de925f70799c710aab5849a
Author: Rafael J. Wysocki <rjw@sisk.pl>
Date: Sat Jan 12 20:40:46 2008 +0100
PM: Acquire device locks on suspend
This patch reorganizes the way suspend and resume notifications are
sent to drivers. The major changes are that now the PM core acquires
every device semaphore before calling the methods, and calls to
device_add() during suspends will fail, while calls to device_del()
during suspends will block.
It also provides a way to safely remove a suspended device with the
help of the PM core, by using the device_pm_schedule_removal() callback
introduced specifically for this purpose, and updates two drivers (msr
and cpuid) that need to use it.
As device_pm_schedule_removal is deleted by another patch, we need also revert other parts of the patch,
i.e. move the calling of device_pm_remove after the calling to bus_remove_device.
Signed-off-by: LongX Zhang <longx.zhang@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2012-10-25 05:21:28 +07:00
|
|
|
device_pm_remove(dev);
|
2012-03-05 22:47:41 +07:00
|
|
|
driver_deferred_probe_del(dev);
|
2018-11-09 21:21:34 +07:00
|
|
|
device_platform_notify(dev, KOBJ_REMOVE);
|
2016-07-28 07:25:41 +07:00
|
|
|
device_remove_properties(dev);
|
2017-10-20 19:01:01 +07:00
|
|
|
device_links_purge(dev);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2014-09-30 18:02:02 +07:00
|
|
|
if (dev->bus)
|
|
|
|
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
|
|
|
|
BUS_NOTIFY_REMOVED_DEVICE, dev);
|
2005-11-16 15:00:00 +07:00
|
|
|
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
|
2016-07-10 18:27:36 +07:00
|
|
|
glue_dir = get_glue_dir(dev);
|
2005-04-17 05:20:36 +07:00
|
|
|
kobject_del(&dev->kobj);
|
2016-07-10 18:27:36 +07:00
|
|
|
cleanup_glue_dir(dev, glue_dir);
|
2007-11-21 23:29:15 +07:00
|
|
|
put_device(parent);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_del);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_unregister - unregister device from system.
|
|
|
|
* @dev: device going away.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* We do this in two parts, like we do device_register(). First,
|
|
|
|
* we remove it from all the subsystems with device_del(), then
|
|
|
|
* we decrement the reference count via put_device(). If that
|
|
|
|
* is the final reference count, the device will be cleaned up
|
|
|
|
* via device_release() above. Otherwise, the structure will
|
|
|
|
* stick around until the final reference to the device is dropped.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2008-01-25 13:50:12 +07:00
|
|
|
void device_unregister(struct device *dev)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-10-30 07:36:48 +07:00
|
|
|
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
|
2005-04-17 05:20:36 +07:00
|
|
|
device_del(dev);
|
|
|
|
put_device(dev);
|
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_unregister);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2015-07-27 22:04:00 +07:00
|
|
|
static struct device *prev_device(struct klist_iter *i)
|
|
|
|
{
|
|
|
|
struct klist_node *n = klist_prev(i);
|
|
|
|
struct device *dev = NULL;
|
|
|
|
struct device_private *p;
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
p = to_device_private_parent(n);
|
|
|
|
dev = p->device;
|
|
|
|
}
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2008-01-25 13:50:12 +07:00
|
|
|
static struct device *next_device(struct klist_iter *i)
|
2005-03-25 10:08:30 +07:00
|
|
|
{
|
2008-01-25 13:50:12 +07:00
|
|
|
struct klist_node *n = klist_next(i);
|
2008-12-17 03:24:56 +07:00
|
|
|
struct device *dev = NULL;
|
|
|
|
struct device_private *p;
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
p = to_device_private_parent(n);
|
|
|
|
dev = p->device;
|
|
|
|
}
|
|
|
|
return dev;
|
2005-03-25 10:08:30 +07:00
|
|
|
}
|
|
|
|
|
2009-04-30 20:23:42 +07:00
|
|
|
/**
|
2009-09-19 04:01:12 +07:00
|
|
|
* device_get_devnode - path of device node file
|
2009-04-30 20:23:42 +07:00
|
|
|
* @dev: device
|
2009-09-19 04:01:12 +07:00
|
|
|
* @mode: returned file access mode
|
2013-04-06 23:56:00 +07:00
|
|
|
* @uid: returned file owner
|
|
|
|
* @gid: returned file group
|
2009-04-30 20:23:42 +07:00
|
|
|
* @tmp: possibly allocated string
|
|
|
|
*
|
|
|
|
* Return the relative path of a possible device node.
|
|
|
|
* Non-default names may need to allocate a memory to compose
|
|
|
|
* a name. This memory is returned in tmp and needs to be
|
|
|
|
* freed by the caller.
|
|
|
|
*/
|
2009-09-19 04:01:12 +07:00
|
|
|
const char *device_get_devnode(struct device *dev,
|
2013-04-12 01:43:29 +07:00
|
|
|
umode_t *mode, kuid_t *uid, kgid_t *gid,
|
2013-04-06 23:56:00 +07:00
|
|
|
const char **tmp)
|
2009-04-30 20:23:42 +07:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
*tmp = NULL;
|
|
|
|
|
|
|
|
/* the device type may provide a specific name */
|
2009-09-19 04:01:12 +07:00
|
|
|
if (dev->type && dev->type->devnode)
|
2013-04-06 23:56:00 +07:00
|
|
|
*tmp = dev->type->devnode(dev, mode, uid, gid);
|
2009-04-30 20:23:42 +07:00
|
|
|
if (*tmp)
|
|
|
|
return *tmp;
|
|
|
|
|
|
|
|
/* the class may provide a specific name */
|
2009-09-19 04:01:12 +07:00
|
|
|
if (dev->class && dev->class->devnode)
|
|
|
|
*tmp = dev->class->devnode(dev, mode);
|
2009-04-30 20:23:42 +07:00
|
|
|
if (*tmp)
|
|
|
|
return *tmp;
|
|
|
|
|
|
|
|
/* return name without allocation, tmp == NULL */
|
|
|
|
if (strchr(dev_name(dev), '!') == NULL)
|
|
|
|
return dev_name(dev);
|
|
|
|
|
|
|
|
/* replace '!' in the name with '/' */
|
2015-06-26 05:02:33 +07:00
|
|
|
s = kstrdup(dev_name(dev), GFP_KERNEL);
|
|
|
|
if (!s)
|
2009-04-30 20:23:42 +07:00
|
|
|
return NULL;
|
2015-06-26 05:02:33 +07:00
|
|
|
strreplace(s, '!', '/');
|
|
|
|
return *tmp = s;
|
2009-04-30 20:23:42 +07:00
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/**
|
2008-01-25 13:50:12 +07:00
|
|
|
* device_for_each_child - device child iterator.
|
|
|
|
* @parent: parent struct device.
|
|
|
|
* @fn: function to be called for each device.
|
2013-06-02 07:17:34 +07:00
|
|
|
* @data: data for the callback.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* Iterate over @parent's child devices, and call @fn for each,
|
|
|
|
* passing it @data.
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2008-01-25 13:50:12 +07:00
|
|
|
* We check the return of @fn each time. If it returns anything
|
|
|
|
* other than 0, we break out and return that value.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2008-01-25 13:50:12 +07:00
|
|
|
int device_for_each_child(struct device *parent, void *data,
|
|
|
|
int (*fn)(struct device *dev, void *data))
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2005-03-25 10:08:30 +07:00
|
|
|
struct klist_iter i;
|
2008-01-25 13:50:12 +07:00
|
|
|
struct device *child;
|
2005-04-17 05:20:36 +07:00
|
|
|
int error = 0;
|
|
|
|
|
2009-04-16 06:00:12 +07:00
|
|
|
if (!parent->p)
|
|
|
|
return 0;
|
|
|
|
|
2008-12-17 03:24:56 +07:00
|
|
|
klist_iter_init(&parent->p->klist_children, &i);
|
2017-11-11 12:52:54 +07:00
|
|
|
while (!error && (child = next_device(&i)))
|
2005-03-25 10:08:30 +07:00
|
|
|
error = fn(child, data);
|
|
|
|
klist_iter_exit(&i);
|
2005-04-17 05:20:36 +07:00
|
|
|
return error;
|
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_for_each_child);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2015-07-27 22:04:00 +07:00
|
|
|
/**
|
|
|
|
* device_for_each_child_reverse - device child iterator in reversed order.
|
|
|
|
* @parent: parent struct device.
|
|
|
|
* @fn: function to be called for each device.
|
|
|
|
* @data: data for the callback.
|
|
|
|
*
|
|
|
|
* Iterate over @parent's child devices, and call @fn for each,
|
|
|
|
* passing it @data.
|
|
|
|
*
|
|
|
|
* We check the return of @fn each time. If it returns anything
|
|
|
|
* other than 0, we break out and return that value.
|
|
|
|
*/
|
|
|
|
int device_for_each_child_reverse(struct device *parent, void *data,
|
|
|
|
int (*fn)(struct device *dev, void *data))
|
|
|
|
{
|
|
|
|
struct klist_iter i;
|
|
|
|
struct device *child;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (!parent->p)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
klist_iter_init(&parent->p->klist_children, &i);
|
|
|
|
while ((child = prev_device(&i)) && !error)
|
|
|
|
error = fn(child, data);
|
|
|
|
klist_iter_exit(&i);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
|
|
|
|
|
2006-11-16 21:42:07 +07:00
|
|
|
/**
|
|
|
|
* device_find_child - device iterator for locating a particular device.
|
|
|
|
* @parent: parent struct device
|
|
|
|
* @match: Callback function to check device
|
2013-06-02 07:17:34 +07:00
|
|
|
* @data: Data to pass to match function
|
2006-11-16 21:42:07 +07:00
|
|
|
*
|
|
|
|
* This is similar to the device_for_each_child() function above, but it
|
|
|
|
* returns a reference to a device that is 'found' for later use, as
|
|
|
|
* determined by the @match callback.
|
|
|
|
*
|
|
|
|
* The callback should return 0 if the device doesn't match and non-zero
|
|
|
|
* if it does. If the callback returns non-zero and a reference to the
|
|
|
|
* current device can be obtained, this function will return to the caller
|
|
|
|
* and not iterate over any more devices.
|
2013-04-15 16:18:11 +07:00
|
|
|
*
|
|
|
|
* NOTE: you will need to drop the reference with put_device() after use.
|
2006-11-16 21:42:07 +07:00
|
|
|
*/
|
2008-01-25 13:50:12 +07:00
|
|
|
struct device *device_find_child(struct device *parent, void *data,
|
|
|
|
int (*match)(struct device *dev, void *data))
|
2006-11-16 21:42:07 +07:00
|
|
|
{
|
|
|
|
struct klist_iter i;
|
|
|
|
struct device *child;
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
return NULL;
|
|
|
|
|
2008-12-17 03:24:56 +07:00
|
|
|
klist_iter_init(&parent->p->klist_children, &i);
|
2006-11-16 21:42:07 +07:00
|
|
|
while ((child = next_device(&i)))
|
|
|
|
if (match(child, data) && get_device(child))
|
|
|
|
break;
|
|
|
|
klist_iter_exit(&i);
|
|
|
|
return child;
|
|
|
|
}
|
2013-07-22 07:41:14 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_find_child);
|
2006-11-16 21:42:07 +07:00
|
|
|
|
2019-05-31 21:15:37 +07:00
|
|
|
/**
|
|
|
|
* device_find_child_by_name - device iterator for locating a child device.
|
|
|
|
* @parent: parent struct device
|
|
|
|
* @name: name of the child device
|
|
|
|
*
|
|
|
|
* This is similar to the device_find_child() function above, but it
|
|
|
|
* returns a reference to a device that has the name @name.
|
|
|
|
*
|
|
|
|
* NOTE: you will need to drop the reference with put_device() after use.
|
|
|
|
*/
|
|
|
|
struct device *device_find_child_by_name(struct device *parent,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
struct klist_iter i;
|
|
|
|
struct device *child;
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
klist_iter_init(&parent->p->klist_children, &i);
|
|
|
|
while ((child = next_device(&i)))
|
|
|
|
if (!strcmp(dev_name(child), name) && get_device(child))
|
|
|
|
break;
|
|
|
|
klist_iter_exit(&i);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_find_child_by_name);
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
int __init devices_init(void)
|
|
|
|
{
|
2007-11-01 22:29:06 +07:00
|
|
|
devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
|
|
|
|
if (!devices_kset)
|
|
|
|
return -ENOMEM;
|
2008-04-22 00:51:07 +07:00
|
|
|
dev_kobj = kobject_create_and_add("dev", NULL);
|
|
|
|
if (!dev_kobj)
|
|
|
|
goto dev_kobj_err;
|
|
|
|
sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
|
|
|
|
if (!sysfs_dev_block_kobj)
|
|
|
|
goto block_kobj_err;
|
|
|
|
sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
|
|
|
|
if (!sysfs_dev_char_kobj)
|
|
|
|
goto char_kobj_err;
|
|
|
|
|
2007-11-01 22:29:06 +07:00
|
|
|
return 0;
|
2008-04-22 00:51:07 +07:00
|
|
|
|
|
|
|
char_kobj_err:
|
|
|
|
kobject_put(sysfs_dev_block_kobj);
|
|
|
|
block_kobj_err:
|
|
|
|
kobject_put(dev_kobj);
|
|
|
|
dev_kobj_err:
|
|
|
|
kset_unregister(devices_kset);
|
|
|
|
return -ENOMEM;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
Driver core: Add offline/online device operations
In some cases, graceful hot-removal of devices is not possible,
although in principle the devices in question support hotplug.
For example, that may happen for the last CPU in the system or
for memory modules holding kernel memory.
In those cases it is nice to be able to check if the given device
can be gracefully hot-removed before triggering a removal procedure
that cannot be aborted or reversed. Unfortunately, however, the
kernel currently doesn't provide any support for that.
To address that deficiency, introduce support for offline and
online operations that can be performed on devices, respectively,
before a hot-removal and in case when it is necessary (or convenient)
to put a device back online after a successful offline (that has not
been followed by removal). The idea is that the offline will fail
whenever the given device cannot be gracefully removed from the
system and it will not be allowed to use the device after a
successful offline (until a subsequent online) in analogy with the
existing CPU offline/online mechanism.
For now, the offline and online operations are introduced at the
bus type level, as that should be sufficient for the most urgent use
cases (CPUs and memory modules). In the future, however, the
approach may be extended to cover some more complicated device
offline/online scenarios involving device drivers etc.
The lock_device_hotplug() and unlock_device_hotplug() functions are
introduced because subsequent patches need to put larger pieces of
code under device_hotplug_lock to prevent race conditions between
device offline and removal from happening.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Toshi Kani <toshi.kani@hp.com>
2013-05-03 03:15:29 +07:00
|
|
|
static int device_check_offline(struct device *dev, void *not_used)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = device_for_each_child(dev, NULL, device_check_offline);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_offline - Prepare the device for hot-removal.
|
|
|
|
* @dev: Device to be put offline.
|
|
|
|
*
|
|
|
|
* Execute the device bus type's .offline() callback, if present, to prepare
|
|
|
|
* the device for a subsequent hot-removal. If that succeeds, the device must
|
|
|
|
* not be used until either it is removed or its bus type's .online() callback
|
|
|
|
* is executed.
|
|
|
|
*
|
|
|
|
* Call under device_hotplug_lock.
|
|
|
|
*/
|
|
|
|
int device_offline(struct device *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (dev->offline_disabled)
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
ret = device_for_each_child(dev, NULL, device_check_offline);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
device_lock(dev);
|
|
|
|
if (device_supports_offline(dev)) {
|
|
|
|
if (dev->offline) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = dev->bus->offline(dev);
|
|
|
|
if (!ret) {
|
|
|
|
kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
|
|
|
|
dev->offline = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
device_unlock(dev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_online - Put the device back online after successful device_offline().
|
|
|
|
* @dev: Device to be put back online.
|
|
|
|
*
|
|
|
|
* If device_offline() has been successfully executed for @dev, but the device
|
|
|
|
* has not been removed subsequently, execute its bus type's .online() callback
|
|
|
|
* to indicate that the device can be used again.
|
|
|
|
*
|
|
|
|
* Call under device_hotplug_lock.
|
|
|
|
*/
|
|
|
|
int device_online(struct device *dev)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
device_lock(dev);
|
|
|
|
if (device_supports_offline(dev)) {
|
|
|
|
if (dev->offline) {
|
|
|
|
ret = dev->bus->online(dev);
|
|
|
|
if (!ret) {
|
|
|
|
kobject_uevent(&dev->kobj, KOBJ_ONLINE);
|
|
|
|
dev->offline = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
device_unlock(dev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-19 04:16:52 +07:00
|
|
|
struct root_device {
|
2008-12-15 19:58:26 +07:00
|
|
|
struct device dev;
|
|
|
|
struct module *owner;
|
|
|
|
};
|
|
|
|
|
2012-11-19 12:27:55 +07:00
|
|
|
static inline struct root_device *to_root_device(struct device *d)
|
2011-01-07 21:17:47 +07:00
|
|
|
{
|
|
|
|
return container_of(d, struct root_device, dev);
|
|
|
|
}
|
2008-12-15 19:58:26 +07:00
|
|
|
|
|
|
|
static void root_device_release(struct device *dev)
|
|
|
|
{
|
|
|
|
kfree(to_root_device(dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __root_device_register - allocate and register a root device
|
|
|
|
* @name: root device name
|
|
|
|
* @owner: owner module of the root device, usually THIS_MODULE
|
|
|
|
*
|
|
|
|
* This function allocates a root device and registers it
|
|
|
|
* using device_register(). In order to free the returned
|
|
|
|
* device, use root_device_unregister().
|
|
|
|
*
|
|
|
|
* Root devices are dummy devices which allow other devices
|
|
|
|
* to be grouped under /sys/devices. Use this function to
|
|
|
|
* allocate a root device and then use it as the parent of
|
|
|
|
* any device which should appear under /sys/devices/{name}
|
|
|
|
*
|
|
|
|
* The /sys/devices/{name} directory will also contain a
|
|
|
|
* 'module' symlink which points to the @owner directory
|
|
|
|
* in sysfs.
|
|
|
|
*
|
2010-03-11 23:11:45 +07:00
|
|
|
* Returns &struct device pointer on success, or ERR_PTR() on error.
|
|
|
|
*
|
2008-12-15 19:58:26 +07:00
|
|
|
* Note: You probably want to use root_device_register().
|
|
|
|
*/
|
|
|
|
struct device *__root_device_register(const char *name, struct module *owner)
|
|
|
|
{
|
|
|
|
struct root_device *root;
|
|
|
|
int err = -ENOMEM;
|
|
|
|
|
|
|
|
root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
|
|
|
|
if (!root)
|
|
|
|
return ERR_PTR(err);
|
|
|
|
|
2009-06-03 05:39:55 +07:00
|
|
|
err = dev_set_name(&root->dev, "%s", name);
|
2008-12-15 19:58:26 +07:00
|
|
|
if (err) {
|
|
|
|
kfree(root);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
root->dev.release = root_device_release;
|
|
|
|
|
|
|
|
err = device_register(&root->dev);
|
|
|
|
if (err) {
|
|
|
|
put_device(&root->dev);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2010-05-17 21:57:58 +07:00
|
|
|
#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
|
2008-12-15 19:58:26 +07:00
|
|
|
if (owner) {
|
|
|
|
struct module_kobject *mk = &owner->mkobj;
|
|
|
|
|
|
|
|
err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
|
|
|
|
if (err) {
|
|
|
|
device_unregister(&root->dev);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
root->owner = owner;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return &root->dev;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__root_device_register);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* root_device_unregister - unregister and free a root device
|
2009-01-21 07:29:13 +07:00
|
|
|
* @dev: device going away
|
2008-12-15 19:58:26 +07:00
|
|
|
*
|
|
|
|
* This function unregisters and cleans up a device that was created by
|
|
|
|
* root_device_register().
|
|
|
|
*/
|
|
|
|
void root_device_unregister(struct device *dev)
|
|
|
|
{
|
|
|
|
struct root_device *root = to_root_device(dev);
|
|
|
|
|
|
|
|
if (root->owner)
|
|
|
|
sysfs_remove_link(&root->dev.kobj, "module");
|
|
|
|
|
|
|
|
device_unregister(dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(root_device_unregister);
|
|
|
|
|
2006-06-15 02:14:34 +07:00
|
|
|
|
|
|
|
static void device_create_release(struct device *dev)
|
|
|
|
{
|
2008-10-30 07:36:48 +07:00
|
|
|
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
|
2006-06-15 02:14:34 +07:00
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
2018-05-06 02:57:41 +07:00
|
|
|
static __printf(6, 0) struct device *
|
2013-07-15 06:05:57 +07:00
|
|
|
device_create_groups_vargs(struct class *class, struct device *parent,
|
|
|
|
dev_t devt, void *drvdata,
|
|
|
|
const struct attribute_group **groups,
|
|
|
|
const char *fmt, va_list args)
|
2006-06-15 02:14:34 +07:00
|
|
|
{
|
|
|
|
struct device *dev = NULL;
|
|
|
|
int retval = -ENODEV;
|
|
|
|
|
|
|
|
if (class == NULL || IS_ERR(class))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
|
|
|
|
if (!dev) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-11-22 02:15:48 +07:00
|
|
|
device_initialize(dev);
|
2006-06-15 02:14:34 +07:00
|
|
|
dev->devt = devt;
|
|
|
|
dev->class = class;
|
|
|
|
dev->parent = parent;
|
2013-07-15 06:05:57 +07:00
|
|
|
dev->groups = groups;
|
2006-06-15 02:14:34 +07:00
|
|
|
dev->release = device_create_release;
|
2008-05-16 03:44:08 +07:00
|
|
|
dev_set_drvdata(dev, drvdata);
|
2006-06-15 02:14:34 +07:00
|
|
|
|
2009-01-25 21:17:37 +07:00
|
|
|
retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
|
|
|
|
if (retval)
|
|
|
|
goto error;
|
|
|
|
|
2013-11-22 02:15:48 +07:00
|
|
|
retval = device_add(dev);
|
2006-06-15 02:14:34 +07:00
|
|
|
if (retval)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
|
|
|
|
error:
|
2008-09-03 23:26:41 +07:00
|
|
|
put_device(dev);
|
2006-06-15 02:14:34 +07:00
|
|
|
return ERR_PTR(retval);
|
|
|
|
}
|
2013-07-15 06:05:57 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* device_create_vargs - creates a device and registers it with sysfs
|
|
|
|
* @class: pointer to the struct class that this device should be registered to
|
|
|
|
* @parent: pointer to the parent struct device of this new device, if any
|
|
|
|
* @devt: the dev_t for the char device to be added
|
|
|
|
* @drvdata: the data to be added to the device for callbacks
|
|
|
|
* @fmt: string for the device's name
|
|
|
|
* @args: va_list for the device's name
|
|
|
|
*
|
|
|
|
* This function can be used by char device classes. A struct device
|
|
|
|
* will be created in sysfs, registered to the specified class.
|
|
|
|
*
|
|
|
|
* A "dev" file will be created, showing the dev_t for the device, if
|
|
|
|
* the dev_t is not 0,0.
|
|
|
|
* If a pointer to a parent struct device is passed in, the newly created
|
|
|
|
* struct device will be a child of that device in sysfs.
|
|
|
|
* The pointer to the struct device will be returned from the call.
|
|
|
|
* Any further sysfs files that might be required can be created using this
|
|
|
|
* pointer.
|
|
|
|
*
|
|
|
|
* Returns &struct device pointer on success, or ERR_PTR() on error.
|
|
|
|
*
|
|
|
|
* Note: the struct class passed to this function must have previously
|
|
|
|
* been created with a call to class_create().
|
|
|
|
*/
|
|
|
|
struct device *device_create_vargs(struct class *class, struct device *parent,
|
|
|
|
dev_t devt, void *drvdata, const char *fmt,
|
|
|
|
va_list args)
|
|
|
|
{
|
|
|
|
return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
|
|
|
|
fmt, args);
|
|
|
|
}
|
2008-05-16 03:44:08 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_create_vargs);
|
|
|
|
|
|
|
|
/**
|
2008-07-22 10:03:34 +07:00
|
|
|
* device_create - creates a device and registers it with sysfs
|
2008-05-16 03:44:08 +07:00
|
|
|
* @class: pointer to the struct class that this device should be registered to
|
|
|
|
* @parent: pointer to the parent struct device of this new device, if any
|
|
|
|
* @devt: the dev_t for the char device to be added
|
|
|
|
* @drvdata: the data to be added to the device for callbacks
|
|
|
|
* @fmt: string for the device's name
|
|
|
|
*
|
|
|
|
* This function can be used by char device classes. A struct device
|
|
|
|
* will be created in sysfs, registered to the specified class.
|
|
|
|
*
|
|
|
|
* A "dev" file will be created, showing the dev_t for the device, if
|
|
|
|
* the dev_t is not 0,0.
|
|
|
|
* If a pointer to a parent struct device is passed in, the newly created
|
|
|
|
* struct device will be a child of that device in sysfs.
|
|
|
|
* The pointer to the struct device will be returned from the call.
|
|
|
|
* Any further sysfs files that might be required can be created using this
|
|
|
|
* pointer.
|
|
|
|
*
|
2010-03-11 23:11:45 +07:00
|
|
|
* Returns &struct device pointer on success, or ERR_PTR() on error.
|
|
|
|
*
|
2008-05-16 03:44:08 +07:00
|
|
|
* Note: the struct class passed to this function must have previously
|
|
|
|
* been created with a call to class_create().
|
|
|
|
*/
|
2008-07-22 10:03:34 +07:00
|
|
|
struct device *device_create(struct class *class, struct device *parent,
|
|
|
|
dev_t devt, void *drvdata, const char *fmt, ...)
|
2008-05-16 03:44:08 +07:00
|
|
|
{
|
|
|
|
va_list vargs;
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
va_start(vargs, fmt);
|
|
|
|
dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
return dev;
|
|
|
|
}
|
2008-07-22 10:03:34 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_create);
|
2008-05-16 03:44:08 +07:00
|
|
|
|
2013-07-15 06:05:57 +07:00
|
|
|
/**
|
|
|
|
* device_create_with_groups - creates a device and registers it with sysfs
|
|
|
|
* @class: pointer to the struct class that this device should be registered to
|
|
|
|
* @parent: pointer to the parent struct device of this new device, if any
|
|
|
|
* @devt: the dev_t for the char device to be added
|
|
|
|
* @drvdata: the data to be added to the device for callbacks
|
|
|
|
* @groups: NULL-terminated list of attribute groups to be created
|
|
|
|
* @fmt: string for the device's name
|
|
|
|
*
|
|
|
|
* This function can be used by char device classes. A struct device
|
|
|
|
* will be created in sysfs, registered to the specified class.
|
|
|
|
* Additional attributes specified in the groups parameter will also
|
|
|
|
* be created automatically.
|
|
|
|
*
|
|
|
|
* A "dev" file will be created, showing the dev_t for the device, if
|
|
|
|
* the dev_t is not 0,0.
|
|
|
|
* If a pointer to a parent struct device is passed in, the newly created
|
|
|
|
* struct device will be a child of that device in sysfs.
|
|
|
|
* The pointer to the struct device will be returned from the call.
|
|
|
|
* Any further sysfs files that might be required can be created using this
|
|
|
|
* pointer.
|
|
|
|
*
|
|
|
|
* Returns &struct device pointer on success, or ERR_PTR() on error.
|
|
|
|
*
|
|
|
|
* Note: the struct class passed to this function must have previously
|
|
|
|
* been created with a call to class_create().
|
|
|
|
*/
|
|
|
|
struct device *device_create_with_groups(struct class *class,
|
|
|
|
struct device *parent, dev_t devt,
|
|
|
|
void *drvdata,
|
|
|
|
const struct attribute_group **groups,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list vargs;
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
va_start(vargs, fmt);
|
|
|
|
dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
|
|
|
|
fmt, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_create_with_groups);
|
|
|
|
|
2008-01-13 02:40:46 +07:00
|
|
|
/**
|
|
|
|
* device_destroy - removes a device that was created with device_create()
|
|
|
|
* @class: pointer to the struct class that this device was registered with
|
|
|
|
* @devt: the dev_t of the device that was previously registered
|
|
|
|
*
|
|
|
|
* This call unregisters and cleans up a device that was created with a
|
|
|
|
* call to device_create().
|
|
|
|
*/
|
|
|
|
void device_destroy(struct class *class, dev_t devt)
|
|
|
|
{
|
|
|
|
struct device *dev;
|
2006-06-15 02:14:34 +07:00
|
|
|
|
2019-07-24 05:18:35 +07:00
|
|
|
dev = class_find_device_by_devt(class, devt);
|
2008-01-28 15:56:11 +07:00
|
|
|
if (dev) {
|
|
|
|
put_device(dev);
|
2006-06-15 02:14:34 +07:00
|
|
|
device_unregister(dev);
|
2008-01-28 15:56:11 +07:00
|
|
|
}
|
2006-06-15 02:14:34 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_destroy);
|
2006-07-04 04:31:12 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* device_rename - renames a device
|
|
|
|
* @dev: the pointer to the struct device to be renamed
|
|
|
|
* @new_name: the new name of the device
|
2008-05-09 04:41:00 +07:00
|
|
|
*
|
|
|
|
* It is the responsibility of the caller to provide mutual
|
|
|
|
* exclusion between two different calls of device_rename
|
|
|
|
* on the same device to ensure that new_name is valid and
|
|
|
|
* won't conflict with other devices.
|
2010-11-25 05:44:07 +07:00
|
|
|
*
|
2010-12-14 03:08:52 +07:00
|
|
|
* Note: Don't call this function. Currently, the networking layer calls this
|
|
|
|
* function, but that will change. The following text from Kay Sievers offers
|
|
|
|
* some insight:
|
|
|
|
*
|
|
|
|
* Renaming devices is racy at many levels, symlinks and other stuff are not
|
|
|
|
* replaced atomically, and you get a "move" uevent, but it's not easy to
|
|
|
|
* connect the event to the old and new device. Device nodes are not renamed at
|
|
|
|
* all, there isn't even support for that in the kernel now.
|
|
|
|
*
|
|
|
|
* In the meantime, during renaming, your target name might be taken by another
|
|
|
|
* driver, creating conflicts. Or the old name is taken directly after you
|
|
|
|
* renamed it -- then you get events for the same DEVPATH, before you even see
|
|
|
|
* the "move" event. It's just a mess, and nothing new should ever rely on
|
|
|
|
* kernel device renaming. Besides that, it's not even implemented now for
|
|
|
|
* other things than (driver-core wise very simple) network devices.
|
|
|
|
*
|
|
|
|
* We are currently about to change network renaming in udev to completely
|
|
|
|
* disallow renaming of devices in the same namespace as the kernel uses,
|
|
|
|
* because we can't solve the problems properly, that arise with swapping names
|
|
|
|
* of multiple interfaces without races. Means, renaming of eth[0-9]* will only
|
|
|
|
* be allowed to some other name than eth[0-9]*, for the aforementioned
|
|
|
|
* reasons.
|
|
|
|
*
|
|
|
|
* Make up a "real" name in the driver before you register anything, or add
|
|
|
|
* some other attributes for userspace to find the device, or use udev to add
|
|
|
|
* symlinks -- but never rename kernel devices later, it's a complete mess. We
|
|
|
|
* don't even want to get into that and try to implement the missing pieces in
|
|
|
|
* the core. We really have other pieces to fix in the driver core mess. :)
|
2006-07-04 04:31:12 +07:00
|
|
|
*/
|
2010-08-05 22:38:18 +07:00
|
|
|
int device_rename(struct device *dev, const char *new_name)
|
2006-07-04 04:31:12 +07:00
|
|
|
{
|
2013-09-12 09:29:06 +07:00
|
|
|
struct kobject *kobj = &dev->kobj;
|
2007-07-18 15:43:47 +07:00
|
|
|
char *old_device_name = NULL;
|
2006-07-04 04:31:12 +07:00
|
|
|
int error;
|
|
|
|
|
|
|
|
dev = get_device(dev);
|
|
|
|
if (!dev)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2013-10-13 21:12:35 +07:00
|
|
|
dev_dbg(dev, "renaming to %s\n", new_name);
|
2006-07-04 04:31:12 +07:00
|
|
|
|
2009-01-25 21:17:37 +07:00
|
|
|
old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
|
2007-07-18 15:43:47 +07:00
|
|
|
if (!old_device_name) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
2006-07-04 04:31:12 +07:00
|
|
|
}
|
|
|
|
|
2010-03-31 01:31:29 +07:00
|
|
|
if (dev->class) {
|
2013-09-12 09:29:06 +07:00
|
|
|
error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
|
|
|
|
kobj, old_device_name,
|
|
|
|
new_name, kobject_namespace(kobj));
|
2010-03-31 01:31:29 +07:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
2010-09-05 12:33:14 +07:00
|
|
|
|
2013-09-12 09:29:06 +07:00
|
|
|
error = kobject_rename(kobj, new_name);
|
2009-01-25 21:17:37 +07:00
|
|
|
if (error)
|
2007-07-18 15:43:47 +07:00
|
|
|
goto out;
|
2006-07-04 04:31:12 +07:00
|
|
|
|
2007-07-18 15:43:47 +07:00
|
|
|
out:
|
2006-07-04 04:31:12 +07:00
|
|
|
put_device(dev);
|
|
|
|
|
2007-07-18 15:43:47 +07:00
|
|
|
kfree(old_device_name);
|
2006-07-04 04:31:12 +07:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2007-02-28 18:38:31 +07:00
|
|
|
EXPORT_SYMBOL_GPL(device_rename);
|
2006-11-20 23:07:51 +07:00
|
|
|
|
|
|
|
static int device_move_class_links(struct device *dev,
|
|
|
|
struct device *old_parent,
|
|
|
|
struct device *new_parent)
|
|
|
|
{
|
2007-03-07 03:55:53 +07:00
|
|
|
int error = 0;
|
2006-11-20 23:07:51 +07:00
|
|
|
|
2007-03-07 03:55:53 +07:00
|
|
|
if (old_parent)
|
|
|
|
sysfs_remove_link(&dev->kobj, "device");
|
|
|
|
if (new_parent)
|
|
|
|
error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
|
|
|
|
"device");
|
|
|
|
return error;
|
2006-11-20 23:07:51 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_move - moves a device to a new parent
|
|
|
|
* @dev: the pointer to the struct device to be moved
|
2018-05-06 18:23:47 +07:00
|
|
|
* @new_parent: the new parent of the device (can be NULL)
|
2009-03-04 18:44:00 +07:00
|
|
|
* @dpm_order: how to reorder the dpm_list
|
2006-11-20 23:07:51 +07:00
|
|
|
*/
|
2009-03-04 18:44:00 +07:00
|
|
|
int device_move(struct device *dev, struct device *new_parent,
|
|
|
|
enum dpm_order dpm_order)
|
2006-11-20 23:07:51 +07:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct device *old_parent;
|
2007-01-09 02:16:44 +07:00
|
|
|
struct kobject *new_parent_kobj;
|
2006-11-20 23:07:51 +07:00
|
|
|
|
|
|
|
dev = get_device(dev);
|
|
|
|
if (!dev)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-03-04 18:44:00 +07:00
|
|
|
device_pm_lock();
|
2006-11-20 23:07:51 +07:00
|
|
|
new_parent = get_device(new_parent);
|
2008-01-25 13:50:12 +07:00
|
|
|
new_parent_kobj = get_device_parent(dev, new_parent);
|
2018-05-07 17:10:31 +07:00
|
|
|
if (IS_ERR(new_parent_kobj)) {
|
|
|
|
error = PTR_ERR(new_parent_kobj);
|
|
|
|
put_device(new_parent);
|
|
|
|
goto out;
|
|
|
|
}
|
2008-01-21 22:09:44 +07:00
|
|
|
|
2008-10-30 07:36:48 +07:00
|
|
|
pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
|
|
|
|
__func__, new_parent ? dev_name(new_parent) : "<NULL>");
|
2007-01-09 02:16:44 +07:00
|
|
|
error = kobject_move(&dev->kobj, new_parent_kobj);
|
2006-11-20 23:07:51 +07:00
|
|
|
if (error) {
|
2008-01-21 22:09:44 +07:00
|
|
|
cleanup_glue_dir(dev, new_parent_kobj);
|
2006-11-20 23:07:51 +07:00
|
|
|
put_device(new_parent);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
old_parent = dev->parent;
|
|
|
|
dev->parent = new_parent;
|
|
|
|
if (old_parent)
|
2008-12-17 03:24:56 +07:00
|
|
|
klist_remove(&dev->p->knode_parent);
|
2008-02-19 18:20:41 +07:00
|
|
|
if (new_parent) {
|
2008-12-17 03:24:56 +07:00
|
|
|
klist_add_tail(&dev->p->knode_parent,
|
|
|
|
&new_parent->p->klist_children);
|
2008-02-19 18:20:41 +07:00
|
|
|
set_dev_node(dev, dev_to_node(new_parent));
|
|
|
|
}
|
|
|
|
|
2012-04-23 14:16:36 +07:00
|
|
|
if (dev->class) {
|
|
|
|
error = device_move_class_links(dev, old_parent, new_parent);
|
|
|
|
if (error) {
|
|
|
|
/* We ignore errors on cleanup since we're hosed anyway... */
|
|
|
|
device_move_class_links(dev, new_parent, old_parent);
|
|
|
|
if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
|
|
|
|
if (new_parent)
|
|
|
|
klist_remove(&dev->p->knode_parent);
|
|
|
|
dev->parent = old_parent;
|
|
|
|
if (old_parent) {
|
|
|
|
klist_add_tail(&dev->p->knode_parent,
|
|
|
|
&old_parent->p->klist_children);
|
|
|
|
set_dev_node(dev, dev_to_node(old_parent));
|
|
|
|
}
|
2008-02-19 18:20:41 +07:00
|
|
|
}
|
2012-04-23 14:16:36 +07:00
|
|
|
cleanup_glue_dir(dev, new_parent_kobj);
|
|
|
|
put_device(new_parent);
|
|
|
|
goto out;
|
2006-11-20 23:07:51 +07:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 18:44:00 +07:00
|
|
|
switch (dpm_order) {
|
|
|
|
case DPM_ORDER_NONE:
|
|
|
|
break;
|
|
|
|
case DPM_ORDER_DEV_AFTER_PARENT:
|
|
|
|
device_pm_move_after(dev, new_parent);
|
driver core: correct device's shutdown order
Now device's shutdown sequence is performed in reverse order of their
registration in devices_kset list and this sequence corresponds to the
reverse device's creation order. So, devices_kset data tracks
"parent<-child" device's dependencies only.
Unfortunately, that's not enough and causes problems in case of
implementing board's specific shutdown procedures. For example [1]:
"DRA7XX_evm uses PCF8575 and one of the PCF output lines feeds to
MMC/SD and this line should be driven high in order for the MMC/SD to
be detected. This line is modelled as regulator and the hsmmc driver
takes care of enabling and disabling it. In the case of 'reboot',
during shutdown path as part of it's cleanup process the hsmmc driver
disables this regulator. This makes MMC boot not functional."
To handle this issue the .shutdown() callback could be implemented
for PCF8575 device where corresponding GPIO pins will be configured to
states, required for correct warm/cold reset. This can be achieved
only when all .shutdown() callbacks have been called already for all
PCF8575's consumers. But devices_kset is not filled correctly now:
devices_kset: Device61 4e000000.dmm
devices_kset: Device62 48070000.i2c
devices_kset: Device63 48072000.i2c
devices_kset: Device64 48060000.i2c
devices_kset: Device65 4809c000.mmc
...
devices_kset: Device102 fixedregulator-sd
...
devices_kset: Device181 0-0020 // PCF8575
devices_kset: Device182 gpiochip496
devices_kset: Device183 0-0021 // PCF8575
devices_kset: Device184 gpiochip480
As can be seen from above .shutdown() callback for PCF8575 will be called
before its consumers, which, in turn means, that any changes of PCF8575
GPIO's pins will be or unsafe or overwritten later by GPIO's consumers.
The problem can be solved if devices_kset list will be filled not only
according device creation order, but also according device's probing
order to track "supplier<-consumer" dependencies also.
Hence, as a fix, lets add devices_kset_move_last(),
devices_kset_move_before(), devices_kset_move_after() and call them
from device_move() and also add call of devices_kset_move_last() in
really_probe(). After this change all entries in devices_kset will
be sorted according to device's creation ("parent<-child") and
probing ("supplier<-consumer") order.
devices_kset after:
devices_kset: Device121 48070000.i2c
devices_kset: Device122 i2c-0
...
devices_kset: Device147 regulator.24
devices_kset: Device148 0-0020
devices_kset: Device149 gpiochip496
devices_kset: Device150 0-0021
devices_kset: Device151 gpiochip480
devices_kset: Device152 0-0019
...
devices_kset: Device372 fixedregulator-sd
devices_kset: Device373 regulator.29
devices_kset: Device374 4809c000.mmc
devices_kset: Device375 mmc0
[1] http://www.spinics.net/lists/linux-mmc/msg29825.html
Cc: Sekhar Nori <nsekhar@ti.com>
Signed-off-by: Grygorii Strashko <grygorii.strashko@ti.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-07-28 00:43:01 +07:00
|
|
|
devices_kset_move_after(dev, new_parent);
|
2009-03-04 18:44:00 +07:00
|
|
|
break;
|
|
|
|
case DPM_ORDER_PARENT_BEFORE_DEV:
|
|
|
|
device_pm_move_before(new_parent, dev);
|
driver core: correct device's shutdown order
Now device's shutdown sequence is performed in reverse order of their
registration in devices_kset list and this sequence corresponds to the
reverse device's creation order. So, devices_kset data tracks
"parent<-child" device's dependencies only.
Unfortunately, that's not enough and causes problems in case of
implementing board's specific shutdown procedures. For example [1]:
"DRA7XX_evm uses PCF8575 and one of the PCF output lines feeds to
MMC/SD and this line should be driven high in order for the MMC/SD to
be detected. This line is modelled as regulator and the hsmmc driver
takes care of enabling and disabling it. In the case of 'reboot',
during shutdown path as part of it's cleanup process the hsmmc driver
disables this regulator. This makes MMC boot not functional."
To handle this issue the .shutdown() callback could be implemented
for PCF8575 device where corresponding GPIO pins will be configured to
states, required for correct warm/cold reset. This can be achieved
only when all .shutdown() callbacks have been called already for all
PCF8575's consumers. But devices_kset is not filled correctly now:
devices_kset: Device61 4e000000.dmm
devices_kset: Device62 48070000.i2c
devices_kset: Device63 48072000.i2c
devices_kset: Device64 48060000.i2c
devices_kset: Device65 4809c000.mmc
...
devices_kset: Device102 fixedregulator-sd
...
devices_kset: Device181 0-0020 // PCF8575
devices_kset: Device182 gpiochip496
devices_kset: Device183 0-0021 // PCF8575
devices_kset: Device184 gpiochip480
As can be seen from above .shutdown() callback for PCF8575 will be called
before its consumers, which, in turn means, that any changes of PCF8575
GPIO's pins will be or unsafe or overwritten later by GPIO's consumers.
The problem can be solved if devices_kset list will be filled not only
according device creation order, but also according device's probing
order to track "supplier<-consumer" dependencies also.
Hence, as a fix, lets add devices_kset_move_last(),
devices_kset_move_before(), devices_kset_move_after() and call them
from device_move() and also add call of devices_kset_move_last() in
really_probe(). After this change all entries in devices_kset will
be sorted according to device's creation ("parent<-child") and
probing ("supplier<-consumer") order.
devices_kset after:
devices_kset: Device121 48070000.i2c
devices_kset: Device122 i2c-0
...
devices_kset: Device147 regulator.24
devices_kset: Device148 0-0020
devices_kset: Device149 gpiochip496
devices_kset: Device150 0-0021
devices_kset: Device151 gpiochip480
devices_kset: Device152 0-0019
...
devices_kset: Device372 fixedregulator-sd
devices_kset: Device373 regulator.29
devices_kset: Device374 4809c000.mmc
devices_kset: Device375 mmc0
[1] http://www.spinics.net/lists/linux-mmc/msg29825.html
Cc: Sekhar Nori <nsekhar@ti.com>
Signed-off-by: Grygorii Strashko <grygorii.strashko@ti.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-07-28 00:43:01 +07:00
|
|
|
devices_kset_move_before(new_parent, dev);
|
2009-03-04 18:44:00 +07:00
|
|
|
break;
|
|
|
|
case DPM_ORDER_DEV_LAST:
|
|
|
|
device_pm_move_last(dev);
|
driver core: correct device's shutdown order
Now device's shutdown sequence is performed in reverse order of their
registration in devices_kset list and this sequence corresponds to the
reverse device's creation order. So, devices_kset data tracks
"parent<-child" device's dependencies only.
Unfortunately, that's not enough and causes problems in case of
implementing board's specific shutdown procedures. For example [1]:
"DRA7XX_evm uses PCF8575 and one of the PCF output lines feeds to
MMC/SD and this line should be driven high in order for the MMC/SD to
be detected. This line is modelled as regulator and the hsmmc driver
takes care of enabling and disabling it. In the case of 'reboot',
during shutdown path as part of it's cleanup process the hsmmc driver
disables this regulator. This makes MMC boot not functional."
To handle this issue the .shutdown() callback could be implemented
for PCF8575 device where corresponding GPIO pins will be configured to
states, required for correct warm/cold reset. This can be achieved
only when all .shutdown() callbacks have been called already for all
PCF8575's consumers. But devices_kset is not filled correctly now:
devices_kset: Device61 4e000000.dmm
devices_kset: Device62 48070000.i2c
devices_kset: Device63 48072000.i2c
devices_kset: Device64 48060000.i2c
devices_kset: Device65 4809c000.mmc
...
devices_kset: Device102 fixedregulator-sd
...
devices_kset: Device181 0-0020 // PCF8575
devices_kset: Device182 gpiochip496
devices_kset: Device183 0-0021 // PCF8575
devices_kset: Device184 gpiochip480
As can be seen from above .shutdown() callback for PCF8575 will be called
before its consumers, which, in turn means, that any changes of PCF8575
GPIO's pins will be or unsafe or overwritten later by GPIO's consumers.
The problem can be solved if devices_kset list will be filled not only
according device creation order, but also according device's probing
order to track "supplier<-consumer" dependencies also.
Hence, as a fix, lets add devices_kset_move_last(),
devices_kset_move_before(), devices_kset_move_after() and call them
from device_move() and also add call of devices_kset_move_last() in
really_probe(). After this change all entries in devices_kset will
be sorted according to device's creation ("parent<-child") and
probing ("supplier<-consumer") order.
devices_kset after:
devices_kset: Device121 48070000.i2c
devices_kset: Device122 i2c-0
...
devices_kset: Device147 regulator.24
devices_kset: Device148 0-0020
devices_kset: Device149 gpiochip496
devices_kset: Device150 0-0021
devices_kset: Device151 gpiochip480
devices_kset: Device152 0-0019
...
devices_kset: Device372 fixedregulator-sd
devices_kset: Device373 regulator.29
devices_kset: Device374 4809c000.mmc
devices_kset: Device375 mmc0
[1] http://www.spinics.net/lists/linux-mmc/msg29825.html
Cc: Sekhar Nori <nsekhar@ti.com>
Signed-off-by: Grygorii Strashko <grygorii.strashko@ti.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-07-28 00:43:01 +07:00
|
|
|
devices_kset_move_last(dev);
|
2009-03-04 18:44:00 +07:00
|
|
|
break;
|
|
|
|
}
|
2012-04-23 14:16:36 +07:00
|
|
|
|
2006-11-20 23:07:51 +07:00
|
|
|
put_device(old_parent);
|
|
|
|
out:
|
2009-03-04 18:44:00 +07:00
|
|
|
device_pm_unlock();
|
2006-11-20 23:07:51 +07:00
|
|
|
put_device(dev);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_move);
|
2007-11-27 13:11:55 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* device_shutdown - call ->shutdown() on each device to shutdown.
|
|
|
|
*/
|
|
|
|
void device_shutdown(void)
|
|
|
|
{
|
2013-09-25 10:05:11 +07:00
|
|
|
struct device *dev, *parent;
|
2010-03-23 00:36:37 +07:00
|
|
|
|
2018-07-19 12:14:58 +07:00
|
|
|
wait_for_device_probe();
|
|
|
|
device_block_probing();
|
|
|
|
|
2010-03-23 00:36:37 +07:00
|
|
|
spin_lock(&devices_kset->list_lock);
|
|
|
|
/*
|
|
|
|
* Walk the devices list backward, shutting down each in turn.
|
|
|
|
* Beware that device unplug events may also start pulling
|
|
|
|
* devices offline, even as the system is shutting down.
|
|
|
|
*/
|
|
|
|
while (!list_empty(&devices_kset->list)) {
|
|
|
|
dev = list_entry(devices_kset->list.prev, struct device,
|
|
|
|
kobj.entry);
|
2012-06-22 17:01:40 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* hold reference count of device's parent to
|
|
|
|
* prevent it from being freed because parent's
|
|
|
|
* lock is to be held
|
|
|
|
*/
|
2013-09-25 10:05:11 +07:00
|
|
|
parent = get_device(dev->parent);
|
2010-03-23 00:36:37 +07:00
|
|
|
get_device(dev);
|
|
|
|
/*
|
|
|
|
* Make sure the device is off the kset list, in the
|
|
|
|
* event that dev->*->shutdown() doesn't remove it.
|
|
|
|
*/
|
|
|
|
list_del_init(&dev->kobj.entry);
|
|
|
|
spin_unlock(&devices_kset->list_lock);
|
2011-12-07 05:24:52 +07:00
|
|
|
|
2012-06-22 17:01:40 +07:00
|
|
|
/* hold lock to avoid race with probe/release */
|
2013-09-25 10:05:11 +07:00
|
|
|
if (parent)
|
|
|
|
device_lock(parent);
|
2012-06-22 17:01:40 +07:00
|
|
|
device_lock(dev);
|
|
|
|
|
2011-12-07 05:24:52 +07:00
|
|
|
/* Don't allow any more runtime suspends */
|
|
|
|
pm_runtime_get_noresume(dev);
|
|
|
|
pm_runtime_barrier(dev);
|
2007-11-27 13:11:55 +07:00
|
|
|
|
2017-08-11 20:44:43 +07:00
|
|
|
if (dev->class && dev->class->shutdown_pre) {
|
2017-06-26 04:53:23 +07:00
|
|
|
if (initcall_debug)
|
2017-08-11 20:44:43 +07:00
|
|
|
dev_info(dev, "shutdown_pre\n");
|
|
|
|
dev->class->shutdown_pre(dev);
|
|
|
|
}
|
|
|
|
if (dev->bus && dev->bus->shutdown) {
|
2012-11-23 14:14:12 +07:00
|
|
|
if (initcall_debug)
|
|
|
|
dev_info(dev, "shutdown\n");
|
2007-11-27 13:11:55 +07:00
|
|
|
dev->bus->shutdown(dev);
|
|
|
|
} else if (dev->driver && dev->driver->shutdown) {
|
2012-11-23 14:14:12 +07:00
|
|
|
if (initcall_debug)
|
|
|
|
dev_info(dev, "shutdown\n");
|
2007-11-27 13:11:55 +07:00
|
|
|
dev->driver->shutdown(dev);
|
|
|
|
}
|
2012-06-22 17:01:40 +07:00
|
|
|
|
|
|
|
device_unlock(dev);
|
2013-09-25 10:05:11 +07:00
|
|
|
if (parent)
|
|
|
|
device_unlock(parent);
|
2012-06-22 17:01:40 +07:00
|
|
|
|
2010-03-23 00:36:37 +07:00
|
|
|
put_device(dev);
|
2013-09-25 10:05:11 +07:00
|
|
|
put_device(parent);
|
2010-03-23 00:36:37 +07:00
|
|
|
|
|
|
|
spin_lock(&devices_kset->list_lock);
|
2007-11-27 13:11:55 +07:00
|
|
|
}
|
2010-03-23 00:36:37 +07:00
|
|
|
spin_unlock(&devices_kset->list_lock);
|
2007-11-27 13:11:55 +07:00
|
|
|
}
|
2010-06-27 08:02:34 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Device logging functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_PRINTK
|
2012-09-13 10:14:11 +07:00
|
|
|
static int
|
|
|
|
create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
|
2010-06-27 08:02:34 +07:00
|
|
|
{
|
2012-05-03 07:29:59 +07:00
|
|
|
const char *subsys;
|
2012-09-13 10:11:29 +07:00
|
|
|
size_t pos = 0;
|
2010-06-27 08:02:34 +07:00
|
|
|
|
2012-05-03 07:29:59 +07:00
|
|
|
if (dev->class)
|
|
|
|
subsys = dev->class->name;
|
|
|
|
else if (dev->bus)
|
|
|
|
subsys = dev->bus->name;
|
|
|
|
else
|
2012-09-13 10:11:29 +07:00
|
|
|
return 0;
|
2012-05-03 07:29:59 +07:00
|
|
|
|
2012-09-13 10:11:29 +07:00
|
|
|
pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
|
2014-08-26 14:34:44 +07:00
|
|
|
if (pos >= hdrlen)
|
|
|
|
goto overflow;
|
2012-05-03 07:29:59 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add device identifier DEVICE=:
|
|
|
|
* b12:8 block dev_t
|
|
|
|
* c127:3 char dev_t
|
|
|
|
* n8 netdev ifindex
|
|
|
|
* +sound:card0 subsystem:devname
|
|
|
|
*/
|
|
|
|
if (MAJOR(dev->devt)) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (strcmp(subsys, "block") == 0)
|
|
|
|
c = 'b';
|
|
|
|
else
|
|
|
|
c = 'c';
|
2012-09-13 10:11:29 +07:00
|
|
|
pos++;
|
|
|
|
pos += snprintf(hdr + pos, hdrlen - pos,
|
|
|
|
"DEVICE=%c%u:%u",
|
|
|
|
c, MAJOR(dev->devt), MINOR(dev->devt));
|
2012-05-03 07:29:59 +07:00
|
|
|
} else if (strcmp(subsys, "net") == 0) {
|
|
|
|
struct net_device *net = to_net_dev(dev);
|
|
|
|
|
2012-09-13 10:11:29 +07:00
|
|
|
pos++;
|
|
|
|
pos += snprintf(hdr + pos, hdrlen - pos,
|
|
|
|
"DEVICE=n%u", net->ifindex);
|
2012-05-03 07:29:59 +07:00
|
|
|
} else {
|
2012-09-13 10:11:29 +07:00
|
|
|
pos++;
|
|
|
|
pos += snprintf(hdr + pos, hdrlen - pos,
|
|
|
|
"DEVICE=+%s:%s", subsys, dev_name(dev));
|
2012-05-03 07:29:59 +07:00
|
|
|
}
|
drivers-core: make structured logging play nice with dynamic-debug
commit c4e00daaa96d3a0786f1f4fe6456281c60ef9a16 changed __dev_printk
in a way that broke dynamic-debug's ability to control the dynamic
prefix of dev_dbg(dev,..), but not dev_dbg(NULL,..) or pr_debug(..),
which is why it wasnt noticed sooner.
When dev==NULL, __dev_printk() just calls printk(), which just works.
But otherwise, it assumed that level was always a string like "<L>"
and just plucked out the 'L', ignoring the rest. However,
dynamic_emit_prefix() adds "[tid] module:func:line:" to the string,
those additions all got lost.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
Acked-by: Jason Baron <jbaron@redhat.com>
Cc: stable <stable@vger.kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2012-07-20 02:46:21 +07:00
|
|
|
|
2014-08-26 14:34:44 +07:00
|
|
|
if (pos >= hdrlen)
|
|
|
|
goto overflow;
|
|
|
|
|
2012-09-13 10:11:29 +07:00
|
|
|
return pos;
|
2014-08-26 14:34:44 +07:00
|
|
|
|
|
|
|
overflow:
|
|
|
|
dev_WARN(dev, "device/subsystem name too long");
|
|
|
|
return 0;
|
2012-09-13 10:11:29 +07:00
|
|
|
}
|
|
|
|
|
2012-09-13 10:13:37 +07:00
|
|
|
int dev_vprintk_emit(int level, const struct device *dev,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
char hdr[128];
|
|
|
|
size_t hdrlen;
|
|
|
|
|
|
|
|
hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
|
|
|
|
|
|
|
|
return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dev_vprintk_emit);
|
|
|
|
|
|
|
|
int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
r = dev_vprintk_emit(level, dev, fmt, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dev_printk_emit);
|
|
|
|
|
2014-12-26 06:07:04 +07:00
|
|
|
static void __dev_printk(const char *level, const struct device *dev,
|
2012-09-13 10:11:29 +07:00
|
|
|
struct va_format *vaf)
|
|
|
|
{
|
2014-12-26 06:07:04 +07:00
|
|
|
if (dev)
|
|
|
|
dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
|
|
|
|
dev_driver_string(dev), dev_name(dev), vaf);
|
|
|
|
else
|
|
|
|
printk("%s(NULL device *): %pV", level, vaf);
|
2010-06-27 08:02:34 +07:00
|
|
|
}
|
|
|
|
|
2014-12-26 06:07:04 +07:00
|
|
|
void dev_printk(const char *level, const struct device *dev,
|
|
|
|
const char *fmt, ...)
|
2010-06-27 08:02:34 +07:00
|
|
|
{
|
|
|
|
struct va_format vaf;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
vaf.fmt = fmt;
|
|
|
|
vaf.va = &args;
|
|
|
|
|
2014-12-26 06:07:04 +07:00
|
|
|
__dev_printk(level, dev, &vaf);
|
2012-09-13 10:11:29 +07:00
|
|
|
|
2010-06-27 08:02:34 +07:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dev_printk);
|
|
|
|
|
|
|
|
#define define_dev_printk_level(func, kern_level) \
|
2014-12-26 06:07:04 +07:00
|
|
|
void func(const struct device *dev, const char *fmt, ...) \
|
2010-06-27 08:02:34 +07:00
|
|
|
{ \
|
|
|
|
struct va_format vaf; \
|
|
|
|
va_list args; \
|
|
|
|
\
|
|
|
|
va_start(args, fmt); \
|
|
|
|
\
|
|
|
|
vaf.fmt = fmt; \
|
|
|
|
vaf.va = &args; \
|
|
|
|
\
|
2014-12-26 06:07:04 +07:00
|
|
|
__dev_printk(kern_level, dev, &vaf); \
|
2012-09-13 10:11:29 +07:00
|
|
|
\
|
2010-06-27 08:02:34 +07:00
|
|
|
va_end(args); \
|
|
|
|
} \
|
|
|
|
EXPORT_SYMBOL(func);
|
|
|
|
|
2018-05-09 22:15:46 +07:00
|
|
|
define_dev_printk_level(_dev_emerg, KERN_EMERG);
|
|
|
|
define_dev_printk_level(_dev_alert, KERN_ALERT);
|
|
|
|
define_dev_printk_level(_dev_crit, KERN_CRIT);
|
|
|
|
define_dev_printk_level(_dev_err, KERN_ERR);
|
|
|
|
define_dev_printk_level(_dev_warn, KERN_WARNING);
|
|
|
|
define_dev_printk_level(_dev_notice, KERN_NOTICE);
|
2010-06-27 08:02:34 +07:00
|
|
|
define_dev_printk_level(_dev_info, KERN_INFO);
|
|
|
|
|
|
|
|
#endif
|
2015-04-04 04:23:37 +07:00
|
|
|
|
|
|
|
static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
|
|
|
|
{
|
|
|
|
return fwnode && !IS_ERR(fwnode->secondary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_primary_fwnode - Change the primary firmware node of a given device.
|
|
|
|
* @dev: Device to handle.
|
|
|
|
* @fwnode: New primary firmware node of the device.
|
|
|
|
*
|
|
|
|
* Set the device's firmware node pointer to @fwnode, but if a secondary
|
|
|
|
* firmware node of the device is present, preserve it.
|
|
|
|
*/
|
|
|
|
void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
|
|
|
|
{
|
|
|
|
if (fwnode) {
|
|
|
|
struct fwnode_handle *fn = dev->fwnode;
|
|
|
|
|
|
|
|
if (fwnode_is_primary(fn))
|
|
|
|
fn = fn->secondary;
|
|
|
|
|
2015-11-30 22:11:39 +07:00
|
|
|
if (fn) {
|
|
|
|
WARN_ON(fwnode->secondary);
|
|
|
|
fwnode->secondary = fn;
|
|
|
|
}
|
2015-04-04 04:23:37 +07:00
|
|
|
dev->fwnode = fwnode;
|
|
|
|
} else {
|
|
|
|
dev->fwnode = fwnode_is_primary(dev->fwnode) ?
|
|
|
|
dev->fwnode->secondary : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(set_primary_fwnode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_secondary_fwnode - Change the secondary firmware node of a given device.
|
|
|
|
* @dev: Device to handle.
|
|
|
|
* @fwnode: New secondary firmware node of the device.
|
|
|
|
*
|
|
|
|
* If a primary firmware node of the device is present, set its secondary
|
|
|
|
* pointer to @fwnode. Otherwise, set the device's firmware node pointer to
|
|
|
|
* @fwnode.
|
|
|
|
*/
|
|
|
|
void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
|
|
|
|
{
|
|
|
|
if (fwnode)
|
|
|
|
fwnode->secondary = ERR_PTR(-ENODEV);
|
|
|
|
|
|
|
|
if (fwnode_is_primary(dev->fwnode))
|
|
|
|
dev->fwnode->secondary = fwnode;
|
|
|
|
else
|
|
|
|
dev->fwnode = fwnode;
|
|
|
|
}
|
driver core: add helper to reuse a device-tree node
Add a helper function to be used when reusing the device-tree node of
another device.
It is fairly common for drivers to reuse the device-tree node of a
parent (or other ancestor) device when creating class or bus devices
(e.g. gpio chips, i2c adapters, iio chips, spi masters, serdev, phys,
usb root hubs). But reusing a device-tree node may cause problems if the
new device is later probed as for example driver core would currently
attempt to reinitialise an already active associated pinmux
configuration.
Other potential issues include the platform-bus code unconditionally
dropping the device-tree node reference in its device destructor,
reinitialisation of other bus-managed resources such as clocks, and the
recently added DMA-setup in driver core.
Note that for most examples above this is currently not an issue as the
devices are never probed, but this is a problem for the USB bus which
has recently gained device-tree support. This was discovered and
worked-around in a rather ad-hoc fashion by commit dc5878abf49c ("usb:
core: move root hub's device node assignment after it is added to bus")
by not setting the of_node pointer until after the root-hub device has
been registered.
Instead we can allow devices to reuse a device-tree node by setting a
flag in their struct device that can be used by core, bus and driver
code to avoid resources from being over-allocated.
Note that the helper also grabs an extra reference to the device node,
which specifically balances the unconditional put in the platform-device
destructor.
Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-06-06 22:59:00 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* device_set_of_node_from_dev - reuse device-tree node of another device
|
|
|
|
* @dev: device whose device-tree node is being set
|
|
|
|
* @dev2: device whose device-tree node is being reused
|
|
|
|
*
|
|
|
|
* Takes another reference to the new device-tree node after first dropping
|
|
|
|
* any reference held to the old node.
|
|
|
|
*/
|
|
|
|
void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
|
|
|
|
{
|
|
|
|
of_node_put(dev->of_node);
|
|
|
|
dev->of_node = of_node_get(dev2->of_node);
|
|
|
|
dev->of_node_reused = true;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
|
2019-06-15 00:54:01 +07:00
|
|
|
|
2019-07-24 05:18:32 +07:00
|
|
|
int device_match_name(struct device *dev, const void *name)
|
|
|
|
{
|
|
|
|
return sysfs_streq(dev_name(dev), name);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_match_name);
|
|
|
|
|
2019-06-15 00:54:01 +07:00
|
|
|
int device_match_of_node(struct device *dev, const void *np)
|
|
|
|
{
|
|
|
|
return dev->of_node == np;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_match_of_node);
|
2019-07-24 05:18:34 +07:00
|
|
|
|
|
|
|
int device_match_fwnode(struct device *dev, const void *fwnode)
|
|
|
|
{
|
|
|
|
return dev_fwnode(dev) == fwnode;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_match_fwnode);
|
2019-07-24 05:18:35 +07:00
|
|
|
|
|
|
|
int device_match_devt(struct device *dev, const void *pdevt)
|
|
|
|
{
|
|
|
|
return dev->devt == *(dev_t *)pdevt;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(device_match_devt);
|
2019-07-24 05:18:36 +07:00
|
|
|
|
|
|
|
int device_match_acpi_dev(struct device *dev, const void *adev)
|
|
|
|
{
|
|
|
|
return ACPI_COMPANION(dev) == adev;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(device_match_acpi_dev);
|