On Sun, 7 Aug 2016, Lukas Wunner wrote:
Normally the device hierarchy is traversed bottom-up during suspend
and top-down during resume. However ->prepare and ->complete do it
the other way round. In the case of ->prepare, this is even documented
in Documentation/power/devices.txt but the reason thereof is not.
Could you explain this please?
The purpose of ->prepare is to tell drivers that a system sleep is
beginning and accordingly they should stop registering new children.
This is necessary for the PM core to be able to traverse the entire
device tree safely; we want to avoid races where a new child is added
below a device concurrently with that device being suspended. (Or if
you want to be more precise, races in which a new child is added below
a device while the PM core is acquiring the device's lock just prior to
invoking its ->suspend callback.)
Telling drivers to stop registering new children below a device has to
be done top-down, because if it were done bottom-up then it would be
subject to the same race described above. Doing it top-down avoids
problems; if a device registers new children while the PM core is
acquiring its lock prior to invoking ->prepare, it doesn't matter. The
new children will be handled later, right along with the existing ones.
Alan Stern