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