PM: Fix potential issue with failing asynchronous suspend

There is a potential issue with the asynchronous suspend code that
a device driver suspending asynchronously may not notice that it
should back off. There are two failing scenarions, (1) when the
driver is waiting for a driver suspending synchronously to complete
and that second driver returns error code, in which case async_error
won't be set and the waiting driver will continue suspending and (2)
after the driver has called device_pm_wait_for_dev() and the waited
for driver returns error code, in which case the caller of
device_pm_wait_for_dev() will not know that there was an error and
will continue suspending.

To fix this issue make __device_suspend() set async_error, so
async_suspend() doesn't need to set it any more, and make
device_pm_wait_for_dev() return async_error, so that its callers
can check whether or not they should continue suspending.

No more changes are necessary, since device_pm_wait_for_dev() is
not used by any drivers' suspend routines.

Reported-by: Colin Cross <ccross@android.com>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Acked-by: Greg Kroah-Hartman <gregkh@suse.de>

+14 -8
+9 -6
drivers/base/power/main.c
··· 51 51 */ 52 52 static bool transition_started; 53 53 54 + static int async_error; 55 + 54 56 /** 55 57 * device_pm_init - Initialize the PM-related part of a device object. 56 58 * @dev: Device object being initialized. ··· 604 602 INIT_LIST_HEAD(&list); 605 603 mutex_lock(&dpm_list_mtx); 606 604 pm_transition = state; 605 + async_error = 0; 607 606 608 607 list_for_each_entry(dev, &dpm_list, power.entry) { 609 608 if (dev->power.status < DPM_OFF) ··· 834 831 return error; 835 832 } 836 833 837 - static int async_error; 838 - 839 834 /** 840 835 * device_suspend - Execute "suspend" callbacks for given device. 841 836 * @dev: Device to handle. ··· 888 887 device_unlock(dev); 889 888 complete_all(&dev->power.completion); 890 889 890 + if (error) 891 + async_error = error; 892 + 891 893 return error; 892 894 } 893 895 ··· 900 896 int error; 901 897 902 898 error = __device_suspend(dev, pm_transition, true); 903 - if (error) { 899 + if (error) 904 900 pm_dev_err(dev, pm_transition, " async", error); 905 - async_error = error; 906 - } 907 901 908 902 put_device(dev); 909 903 } ··· 1089 1087 * @dev: Device to wait for. 1090 1088 * @subordinate: Device that needs to wait for @dev. 1091 1089 */ 1092 - void device_pm_wait_for_dev(struct device *subordinate, struct device *dev) 1090 + int device_pm_wait_for_dev(struct device *subordinate, struct device *dev) 1093 1091 { 1094 1092 dpm_wait(dev, subordinate->power.async_suspend); 1093 + return async_error; 1095 1094 } 1096 1095 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
+5 -2
include/linux/pm.h
··· 559 559 __suspend_report_result(__func__, fn, ret); \ 560 560 } while (0) 561 561 562 - extern void device_pm_wait_for_dev(struct device *sub, struct device *dev); 562 + extern int device_pm_wait_for_dev(struct device *sub, struct device *dev); 563 563 #else /* !CONFIG_PM_SLEEP */ 564 564 565 565 #define device_pm_lock() do {} while (0) ··· 572 572 573 573 #define suspend_report_result(fn, ret) do {} while (0) 574 574 575 - static inline void device_pm_wait_for_dev(struct device *a, struct device *b) {} 575 + static inline int device_pm_wait_for_dev(struct device *a, struct device *b) 576 + { 577 + return 0; 578 + } 576 579 #endif /* !CONFIG_PM_SLEEP */ 577 580 578 581 /* How to reorder dpm_list after device_move() */