From 632e270e01d8a1ee9e8ea56c83028727f17b1d17 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Fri, 1 Jul 2011 22:29:15 +0200 Subject: PM / Runtime: Return special error code if runtime PM is disabled Some callers of pm_runtime_get_sync() and other runtime PM helper functions, scsi_autopm_get_host() and scsi_autopm_get_device() in particular, need to distinguish error codes returned when runtime PM is disabled (i.e. power.disable_depth is nonzero for the given device) from error codes returned in other situations. For this reason, make the runtime PM helper functions return -EACCES when power.disable_depth is nonzero and ensure that this error code won't be returned by them in any other circumstances. Modify scsi_autopm_get_host() and scsi_autopm_get_device() to check the error code returned by pm_runtime_get_sync() and ignore -EACCES. Signed-off-by: Rafael J. Wysocki --- drivers/base/power/runtime.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index 5f5c4236f00..ee99025be6b 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -135,8 +135,9 @@ static int rpm_check_suspend_allowed(struct device *dev) if (dev->power.runtime_error) retval = -EINVAL; - else if (atomic_read(&dev->power.usage_count) > 0 - || dev->power.disable_depth > 0) + else if (dev->power.disable_depth > 0) + retval = -EACCES; + else if (atomic_read(&dev->power.usage_count) > 0) retval = -EAGAIN; else if (!pm_children_suspended(dev)) retval = -EBUSY; @@ -262,7 +263,7 @@ static int rpm_callback(int (*cb)(struct device *), struct device *dev) spin_lock_irq(&dev->power.lock); } dev->power.runtime_error = retval; - return retval; + return retval != -EACCES ? retval : -EIO; } /** @@ -458,7 +459,7 @@ static int rpm_resume(struct device *dev, int rpmflags) if (dev->power.runtime_error) retval = -EINVAL; else if (dev->power.disable_depth > 0) - retval = -EAGAIN; + retval = -EACCES; if (retval) goto out; -- cgit v1.2.3-18-g5258 From 1e2ef05bb8cf851a694d38e9170c89e7ff052741 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Wed, 6 Jul 2011 10:51:58 +0200 Subject: PM: Limit race conditions between runtime PM and system sleep (v2) One of the roles of the PM core is to prevent different PM callbacks executed for the same device object from racing with each other. Unfortunately, after commit e8665002477f0278f84f898145b1f141ba26ee26 (PM: Allow pm_runtime_suspend() to succeed during system suspend) runtime PM callbacks may be executed concurrently with system suspend/resume callbacks for the same device. The main reason for commit e8665002477f0278f84f898145b1f141ba26ee26 was that some subsystems and device drivers wanted to use runtime PM helpers, pm_runtime_suspend() and pm_runtime_put_sync() in particular, for carrying out the suspend of devices in their .suspend() callbacks. However, as it's been determined recently, there are multiple reasons not to do so, inlcuding: * The caller really doesn't control the runtime PM usage counters, because user space can access them through sysfs and effectively block runtime PM. That means using pm_runtime_suspend() or pm_runtime_get_sync() to suspend devices during system suspend may or may not work. * If a driver calls pm_runtime_suspend() from its .suspend() callback, it causes the subsystem's .runtime_suspend() callback to be executed, which leads to the call sequence: subsys->suspend(dev) driver->suspend(dev) pm_runtime_suspend(dev) subsys->runtime_suspend(dev) recursive from the subsystem's point of view. For some subsystems that may actually work (e.g. the platform bus type), but for some it will fail in a rather spectacular fashion (e.g. PCI). In each case it means a layering violation. * Both the subsystem and the driver can provide .suspend_noirq() callbacks for system suspend that can do whatever the .runtime_suspend() callbacks do just fine, so it really isn't necessary to call pm_runtime_suspend() during system suspend. * The runtime PM's handling of wakeup devices is usually different from the system suspend's one, so .runtime_suspend() may simply be inappropriate for system suspend. * System suspend is supposed to work even if CONFIG_PM_RUNTIME is unset. * The runtime PM workqueue is frozen before system suspend, so if whatever the driver is going to do during system suspend depends on it, that simply won't work. Still, there is a good reason to allow pm_runtime_resume() to succeed during system suspend and resume (for instance, some subsystems and device drivers may legitimately use it to ensure that their devices are in full-power states before suspending them). Moreover, there is no reason to prevent runtime PM callbacks from being executed in parallel with the system suspend/resume .prepare() and .complete() callbacks and the code removed by commit e8665002477f0278f84f898145b1f141ba26ee26 went too far in this respect. On the other hand, runtime PM callbacks, including .runtime_resume(), must not be executed during system suspend's "late" stage of suspending devices and during system resume's "early" device resume stage. Taking all of the above into consideration, make the PM core acquire a runtime PM reference to every device and resume it if there's a runtime PM resume request pending right before executing the subsystem-level .suspend() callback for it. Make the PM core drop references to all devices right after executing the subsystem-level .resume() callbacks for them. Additionally, make the PM core disable the runtime PM framework for all devices during system suspend, after executing the subsystem-level .suspend() callbacks for them, and enable the runtime PM framework for all devices during system resume, right before executing the subsystem-level .resume() callbacks for them. Signed-off-by: Rafael J. Wysocki Acked-by: Kevin Hilman --- drivers/base/power/main.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c index 85b591a5429..a85459126bc 100644 --- a/drivers/base/power/main.c +++ b/drivers/base/power/main.c @@ -505,6 +505,7 @@ static int legacy_resume(struct device *dev, int (*cb)(struct device *dev)) static int device_resume(struct device *dev, pm_message_t state, bool async) { int error = 0; + bool put = false; TRACE_DEVICE(dev); TRACE_RESUME(0); @@ -521,6 +522,9 @@ static int device_resume(struct device *dev, pm_message_t state, bool async) if (!dev->power.is_suspended) goto Unlock; + pm_runtime_enable(dev); + put = true; + if (dev->pm_domain) { pm_dev_dbg(dev, state, "power domain "); error = pm_op(dev, &dev->pm_domain->ops, state); @@ -563,6 +567,10 @@ static int device_resume(struct device *dev, pm_message_t state, bool async) complete_all(&dev->power.completion); TRACE_RESUME(error); + + if (put) + pm_runtime_put_sync(dev); + return error; } @@ -843,16 +851,22 @@ static int __device_suspend(struct device *dev, pm_message_t state, bool async) int error = 0; dpm_wait_for_children(dev, async); - device_lock(dev); if (async_error) - goto Unlock; + return 0; + + pm_runtime_get_noresume(dev); + if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) + pm_wakeup_event(dev, 0); if (pm_wakeup_pending()) { + pm_runtime_put_sync(dev); async_error = -EBUSY; - goto Unlock; + return 0; } + device_lock(dev); + if (dev->pm_domain) { pm_dev_dbg(dev, state, "power domain "); error = pm_op(dev, &dev->pm_domain->ops, state); @@ -890,12 +904,15 @@ static int __device_suspend(struct device *dev, pm_message_t state, bool async) End: dev->power.is_suspended = !error; - Unlock: device_unlock(dev); complete_all(&dev->power.completion); - if (error) + if (error) { + pm_runtime_put_sync(dev); async_error = error; + } else if (dev->power.is_suspended) { + __pm_runtime_disable(dev, false); + } return error; } @@ -1035,13 +1052,7 @@ int dpm_prepare(pm_message_t state) get_device(dev); mutex_unlock(&dpm_list_mtx); - pm_runtime_get_noresume(dev); - if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) - pm_wakeup_event(dev, 0); - - pm_runtime_put_sync(dev); - error = pm_wakeup_pending() ? - -EBUSY : device_prepare(dev, state); + error = device_prepare(dev, state); mutex_lock(&dpm_list_mtx); if (error) { -- cgit v1.2.3-18-g5258 From 62052ab1d1a456f5f62f8b753e12d10ca1a83604 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Wed, 6 Jul 2011 10:52:13 +0200 Subject: PM / Runtime: Replace "run-time" with "runtime" in documentation The runtime PM documentation and kerneldoc comments sometimes spell "runtime" with a dash (i.e. "run-time"). Replace all of those instances with "runtime" to make the naming consistent. Signed-off-by: Rafael J. Wysocki --- drivers/base/power/runtime.c | 66 ++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'drivers/base') diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index ee99025be6b..be7b982d252 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -1,5 +1,5 @@ /* - * drivers/base/power/runtime.c - Helper functions for device run-time PM + * drivers/base/power/runtime.c - Helper functions for device runtime PM * * Copyright (c) 2009 Rafael J. Wysocki , Novell Inc. * Copyright (C) 2010 Alan Stern @@ -159,7 +159,7 @@ static int rpm_check_suspend_allowed(struct device *dev) * @dev: Device to notify the bus type about. * @rpmflags: Flag bits. * - * Check if the device's run-time PM status allows it to be suspended. If + * Check if the device's runtime PM status allows it to be suspended. If * another idle notification has been started earlier, return immediately. If * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise * run the ->runtime_idle() callback directly. @@ -267,11 +267,11 @@ static int rpm_callback(int (*cb)(struct device *), struct device *dev) } /** - * rpm_suspend - Carry out run-time suspend of given device. + * rpm_suspend - Carry out runtime suspend of given device. * @dev: Device to suspend. * @rpmflags: Flag bits. * - * Check if the device's run-time PM status allows it to be suspended. If + * Check if the device's runtime PM status allows it to be suspended. If * another suspend has been started earlier, either return immediately or wait * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a * pending idle notification. If the RPM_ASYNC flag is set then queue a @@ -430,11 +430,11 @@ static int rpm_suspend(struct device *dev, int rpmflags) } /** - * rpm_resume - Carry out run-time resume of given device. + * rpm_resume - Carry out runtime resume of given device. * @dev: Device to resume. * @rpmflags: Flag bits. * - * Check if the device's run-time PM status allows it to be resumed. Cancel + * Check if the device's runtime PM status allows it to be resumed. Cancel * any scheduled or pending requests. If another resume has been started * earlier, either return immediately or wait for it to finish, depending on the * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in @@ -551,7 +551,7 @@ static int rpm_resume(struct device *dev, int rpmflags) spin_lock(&parent->power.lock); /* - * We can resume if the parent's run-time PM is disabled or it + * We can resume if the parent's runtime PM is disabled or it * is set to ignore children. */ if (!parent->power.disable_depth @@ -615,11 +615,11 @@ static int rpm_resume(struct device *dev, int rpmflags) } /** - * pm_runtime_work - Universal run-time PM work function. + * pm_runtime_work - Universal runtime PM work function. * @work: Work structure used for scheduling the execution of this function. * * Use @work to get the device object the work is to be done for, determine what - * is to be done and execute the appropriate run-time PM function. + * is to be done and execute the appropriate runtime PM function. */ static void pm_runtime_work(struct work_struct *work) { @@ -718,7 +718,7 @@ int pm_schedule_suspend(struct device *dev, unsigned int delay) EXPORT_SYMBOL_GPL(pm_schedule_suspend); /** - * __pm_runtime_idle - Entry point for run-time idle operations. + * __pm_runtime_idle - Entry point for runtime idle operations. * @dev: Device to send idle notification for. * @rpmflags: Flag bits. * @@ -747,7 +747,7 @@ int __pm_runtime_idle(struct device *dev, int rpmflags) EXPORT_SYMBOL_GPL(__pm_runtime_idle); /** - * __pm_runtime_suspend - Entry point for run-time put/suspend operations. + * __pm_runtime_suspend - Entry point for runtime put/suspend operations. * @dev: Device to suspend. * @rpmflags: Flag bits. * @@ -776,7 +776,7 @@ int __pm_runtime_suspend(struct device *dev, int rpmflags) EXPORT_SYMBOL_GPL(__pm_runtime_suspend); /** - * __pm_runtime_resume - Entry point for run-time resume operations. + * __pm_runtime_resume - Entry point for runtime resume operations. * @dev: Device to resume. * @rpmflags: Flag bits. * @@ -802,11 +802,11 @@ int __pm_runtime_resume(struct device *dev, int rpmflags) EXPORT_SYMBOL_GPL(__pm_runtime_resume); /** - * __pm_runtime_set_status - Set run-time PM status of a device. + * __pm_runtime_set_status - Set runtime PM status of a device. * @dev: Device to handle. - * @status: New run-time PM status of the device. + * @status: New runtime PM status of the device. * - * If run-time PM of the device is disabled or its power.runtime_error field is + * If runtime PM of the device is disabled or its power.runtime_error field is * different from zero, the status may be changed either to RPM_ACTIVE, or to * RPM_SUSPENDED, as long as that reflects the actual state of the device. * However, if the device has a parent and the parent is not active, and the @@ -852,7 +852,7 @@ int __pm_runtime_set_status(struct device *dev, unsigned int status) /* * It is invalid to put an active child under a parent that is - * not active, has run-time PM enabled and the + * not active, has runtime PM enabled and the * 'power.ignore_children' flag unset. */ if (!parent->power.disable_depth @@ -886,7 +886,7 @@ EXPORT_SYMBOL_GPL(__pm_runtime_set_status); * @dev: Device to handle. * * Flush all pending requests for the device from pm_wq and wait for all - * run-time PM operations involving the device in progress to complete. + * runtime PM operations involving the device in progress to complete. * * Should be called under dev->power.lock with interrupts disabled. */ @@ -934,7 +934,7 @@ static void __pm_runtime_barrier(struct device *dev) * Prevent the device from being suspended by incrementing its usage counter and * if there's a pending resume request for the device, wake the device up. * Next, make sure that all pending requests for the device have been flushed - * from pm_wq and wait for all run-time PM operations involving the device in + * from pm_wq and wait for all runtime PM operations involving the device in * progress to complete. * * Return value: @@ -964,18 +964,18 @@ int pm_runtime_barrier(struct device *dev) EXPORT_SYMBOL_GPL(pm_runtime_barrier); /** - * __pm_runtime_disable - Disable run-time PM of a device. + * __pm_runtime_disable - Disable runtime PM of a device. * @dev: Device to handle. * @check_resume: If set, check if there's a resume request for the device. * * Increment power.disable_depth for the device and if was zero previously, - * cancel all pending run-time PM requests for the device and wait for all + * cancel all pending runtime PM requests for the device and wait for all * operations in progress to complete. The device can be either active or - * suspended after its run-time PM has been disabled. + * suspended after its runtime PM has been disabled. * * If @check_resume is set and there's a resume request pending when * __pm_runtime_disable() is called and power.disable_depth is zero, the - * function will wake up the device before disabling its run-time PM. + * function will wake up the device before disabling its runtime PM. */ void __pm_runtime_disable(struct device *dev, bool check_resume) { @@ -988,7 +988,7 @@ void __pm_runtime_disable(struct device *dev, bool check_resume) /* * Wake up the device if there's a resume request pending, because that - * means there probably is some I/O to process and disabling run-time PM + * means there probably is some I/O to process and disabling runtime PM * shouldn't prevent the device from processing the I/O. */ if (check_resume && dev->power.request_pending @@ -1013,7 +1013,7 @@ void __pm_runtime_disable(struct device *dev, bool check_resume) EXPORT_SYMBOL_GPL(__pm_runtime_disable); /** - * pm_runtime_enable - Enable run-time PM of a device. + * pm_runtime_enable - Enable runtime PM of a device. * @dev: Device to handle. */ void pm_runtime_enable(struct device *dev) @@ -1032,7 +1032,7 @@ void pm_runtime_enable(struct device *dev) EXPORT_SYMBOL_GPL(pm_runtime_enable); /** - * pm_runtime_forbid - Block run-time PM of a device. + * pm_runtime_forbid - Block runtime PM of a device. * @dev: Device to handle. * * Increase the device's usage count and clear its power.runtime_auto flag, @@ -1055,7 +1055,7 @@ void pm_runtime_forbid(struct device *dev) EXPORT_SYMBOL_GPL(pm_runtime_forbid); /** - * pm_runtime_allow - Unblock run-time PM of a device. + * pm_runtime_allow - Unblock runtime PM of a device. * @dev: Device to handle. * * Decrease the device's usage count and set its power.runtime_auto flag. @@ -1076,12 +1076,12 @@ void pm_runtime_allow(struct device *dev) EXPORT_SYMBOL_GPL(pm_runtime_allow); /** - * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device. + * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device. * @dev: Device to handle. * * Set the power.no_callbacks flag, which tells the PM core that this - * device is power-managed through its parent and has no run-time PM - * callbacks of its own. The run-time sysfs attributes will be removed. + * device is power-managed through its parent and has no runtime PM + * callbacks of its own. The runtime sysfs attributes will be removed. */ void pm_runtime_no_callbacks(struct device *dev) { @@ -1157,8 +1157,8 @@ static void update_autosuspend(struct device *dev, int old_delay, int old_use) * @delay: Value of the new delay in milliseconds. * * Set the device's power.autosuspend_delay value. If it changes to negative - * and the power.use_autosuspend flag is set, prevent run-time suspends. If it - * changes the other way, allow run-time suspends. + * and the power.use_autosuspend flag is set, prevent runtime suspends. If it + * changes the other way, allow runtime suspends. */ void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) { @@ -1178,7 +1178,7 @@ EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay); * @dev: Device to handle. * @use: New value for use_autosuspend. * - * Set the device's power.use_autosuspend flag, and allow or prevent run-time + * Set the device's power.use_autosuspend flag, and allow or prevent runtime * suspends as needed. */ void __pm_runtime_use_autosuspend(struct device *dev, bool use) @@ -1195,7 +1195,7 @@ void __pm_runtime_use_autosuspend(struct device *dev, bool use) EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); /** - * pm_runtime_init - Initialize run-time PM fields in given device object. + * pm_runtime_init - Initialize runtime PM fields in given device object. * @dev: Device object to initialize. */ void pm_runtime_init(struct device *dev) -- cgit v1.2.3-18-g5258 From 69c843b45eb3b8f267019e6a05860c9c48337419 Mon Sep 17 00:00:00 2001 From: Alan Stern Date: Wed, 6 Jul 2011 10:52:23 +0200 Subject: PM / Runtime: Prevent runtime_resume from racing with probe This patch (as1475) adds device_lock() and device_unlock() calls to the store methods for the power/control and power/autosuspend_delay_ms sysfs attribute files. We don't want badly timed writes to these files to cause runtime_resume callbacks to occur while a driver is being probed for a device. Signed-off-by: Alan Stern Signed-off-by: Rafael J. Wysocki --- drivers/base/power/sysfs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'drivers/base') diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c index a9f5b897961..942d6a7c9ae 100644 --- a/drivers/base/power/sysfs.c +++ b/drivers/base/power/sysfs.c @@ -116,12 +116,14 @@ static ssize_t control_store(struct device * dev, struct device_attribute *attr, cp = memchr(buf, '\n', n); if (cp) len = cp - buf; + device_lock(dev); if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0) pm_runtime_allow(dev); else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0) pm_runtime_forbid(dev); else - return -EINVAL; + n = -EINVAL; + device_unlock(dev); return n; } @@ -205,7 +207,9 @@ static ssize_t autosuspend_delay_ms_store(struct device *dev, if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay) return -EINVAL; + device_lock(dev); pm_runtime_set_autosuspend_delay(dev, delay); + device_unlock(dev); return n; } -- cgit v1.2.3-18-g5258 From 2cffff1281a74714c9e035322077ec52ffb1f838 Mon Sep 17 00:00:00 2001 From: ShuoX Liu Date: Fri, 8 Jul 2011 20:53:55 +0200 Subject: PM / Runtime: Consistent utilization of deferred_resume dev->power.deferred_resume is used as a bool typically, so change one assignment to false from 0, like other places. Signed-off-by: ShuoX Liu --- drivers/base/power/runtime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/base') diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index be7b982d252..8dc247c974a 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -389,7 +389,7 @@ static int rpm_suspend(struct device *dev, int rpmflags) retval = rpm_callback(callback, dev); if (retval) { __update_runtime_status(dev, RPM_ACTIVE); - dev->power.deferred_resume = 0; + dev->power.deferred_resume = false; if (retval == -EAGAIN || retval == -EBUSY) dev->power.runtime_error = 0; else -- cgit v1.2.3-18-g5258