Skip to content

Commit 44cc89f

Browse files
committed
PM: runtime: Update device status before letting suppliers suspend
Because the PM-runtime status of the device is not updated in __rpm_callback(), attempts to suspend the suppliers of the given device triggered by rpm_put_suppliers() called by it may fail. Fix this by making __rpm_callback() update the device's status to RPM_SUSPENDED before calling rpm_put_suppliers() if the current status of the device is RPM_SUSPENDING and the callback just invoked by it has returned 0 (success). While at it, modify the code in __rpm_callback() to always check the device's PM-runtime status under its PM lock. Link: https://lore.kernel.org/linux-pm/CAPDyKFqm06KDw_p8WXsM4dijDbho4bb6T4k50UqqvR1_COsp8g@mail.gmail.com/ Fixes: 21d5c57 ("PM / runtime: Use device links") Reported-by: Elaine Zhang <[email protected]> Diagnosed-by: Ulf Hansson <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]> Tested-by: Elaine Zhang <[email protected]> Reviewed-by: Ulf Hansson <[email protected]> Cc: 4.10+ <[email protected]> # 4.10+ Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent fe07bfd commit 44cc89f

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

drivers/base/power/runtime.c

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -325,22 +325,22 @@ static void rpm_put_suppliers(struct device *dev)
325325
static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
326326
__releases(&dev->power.lock) __acquires(&dev->power.lock)
327327
{
328-
int retval, idx;
329328
bool use_links = dev->power.links_count > 0;
329+
bool get = false;
330+
int retval, idx;
331+
bool put;
330332

331333
if (dev->power.irq_safe) {
332334
spin_unlock(&dev->power.lock);
335+
} else if (!use_links) {
336+
spin_unlock_irq(&dev->power.lock);
333337
} else {
338+
get = dev->power.runtime_status == RPM_RESUMING;
339+
334340
spin_unlock_irq(&dev->power.lock);
335341

336-
/*
337-
* Resume suppliers if necessary.
338-
*
339-
* The device's runtime PM status cannot change until this
340-
* routine returns, so it is safe to read the status outside of
341-
* the lock.
342-
*/
343-
if (use_links && dev->power.runtime_status == RPM_RESUMING) {
342+
/* Resume suppliers if necessary. */
343+
if (get) {
344344
idx = device_links_read_lock();
345345

346346
retval = rpm_get_suppliers(dev);
@@ -355,24 +355,36 @@ static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
355355

356356
if (dev->power.irq_safe) {
357357
spin_lock(&dev->power.lock);
358-
} else {
359-
/*
360-
* If the device is suspending and the callback has returned
361-
* success, drop the usage counters of the suppliers that have
362-
* been reference counted on its resume.
363-
*
364-
* Do that if resume fails too.
365-
*/
366-
if (use_links
367-
&& ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
368-
|| (dev->power.runtime_status == RPM_RESUMING && retval))) {
369-
idx = device_links_read_lock();
358+
return retval;
359+
}
370360

371-
fail:
372-
rpm_put_suppliers(dev);
361+
spin_lock_irq(&dev->power.lock);
373362

374-
device_links_read_unlock(idx);
375-
}
363+
if (!use_links)
364+
return retval;
365+
366+
/*
367+
* If the device is suspending and the callback has returned success,
368+
* drop the usage counters of the suppliers that have been reference
369+
* counted on its resume.
370+
*
371+
* Do that if the resume fails too.
372+
*/
373+
put = dev->power.runtime_status == RPM_SUSPENDING && !retval;
374+
if (put)
375+
__update_runtime_status(dev, RPM_SUSPENDED);
376+
else
377+
put = get && retval;
378+
379+
if (put) {
380+
spin_unlock_irq(&dev->power.lock);
381+
382+
idx = device_links_read_lock();
383+
384+
fail:
385+
rpm_put_suppliers(dev);
386+
387+
device_links_read_unlock(idx);
376388

377389
spin_lock_irq(&dev->power.lock);
378390
}

0 commit comments

Comments
 (0)