In some cases (for example, during system-wide suspend and resume of devices) it is useful to know whether or not runtime PM has ever been enabled for a given device and, if so, what the runtime PM status of it had been right before runtime PM was disabled for it last time. For this reason, introduce a new struct dev_pm_info field called last_status that will be used for capturing the runtime PM status of the device when its power.disable_depth counter changes from 0 to 1. The new field will be set to RPM_INVALID to start with and whenever power.disable_depth changes from 1 to 0, so it will be valid only when runtime PM of the device is currently disabled, but it has been enabled at least once. Immediately use power.last_status in rpm_resume() to make it handle the case when PM runtime is disabled for the device, but its runtime PM status is RPM_ACTIVE more consistently. Namely, make it return 1 if power.last_status is also equal to RPM_ACTIVE in that case (the idea being that if the status was RPM_ACTIVE last time when power.disable_depth was changing from 0 to 1 and it is still RPM_ACTIVE, it can be assumed to reflect what happened to the device last time when it was using runtime PM) and -EACCES otherwise. Update the documentation to provide a description of last_status and change the description of pm_runtime_resume() in it to reflect the new behavior of rpm_active(). While at it, rearrange the code in pm_runtime_enable() to be more straightforward and replace the WARN() macro in it with a pr_warn() invocation which is less disruptive.
Unified · Split
| @@ -265,6 +265,10 @@ defined in include/linux/pm.h: · 264 unmodified lines | ||
| 265 | 265 | RPM_SUSPENDED, which means that each device is initially regarded by the |
| 266 | 266 | PM core as 'suspended', regardless of its real hardware status |
| 267 | 267 | |
| 268 | + `enum rpm_status last_status;` | |
| 269 | + - the last runtime PM status of the device captured before disabling runtime | |
| 270 | + PM for it (invalid initially and when disable_depth is 0) | |
| 271 | + | |
| 268 | 272 | `unsigned int runtime_auto;` |
| 269 | 273 | - if set, indicates that the user space has allowed the device driver to |
| 270 | 274 | power manage the device at run time via the /sys/devices/.../power/control |
| @@ -333,10 +337,12 @@ drivers/base/power/runtime.c and include/linux/pm_runtime.h: · 62 unmodified lines | ||
| 333 | 337 | |
| 334 | 338 | `int pm_runtime_resume(struct device *dev);` |
| 335 | 339 | - execute the subsystem-level resume callback for the device; returns 0 on |
| 336 | − success, 1 if the device's runtime PM status was already 'active' or | |
| 337 | − error code on failure, where -EAGAIN means it may be safe to attempt to | |
| 338 | − resume the device again in future, but 'power.runtime_error' should be | |
| 339 | − checked additionally, and -EACCES means that 'power.disable_depth' is | |
| 340 | + success, 1 if the device's runtime PM status is already 'active' (also if | |
| 341 | + 'power.disable_depth' is nonzero, but the status was 'active' when it was | |
| 342 | + changing from 0 to 1) or error code on failure, where -EAGAIN means it may | |
| 343 | + be safe to attempt to resume the device again in future, but | |
| 344 | + 'power.runtime_error' should be checked additionally, and -EACCES means | |
| 345 | + that the callback could not be run, because 'power.disable_depth' was | |
| 340 | 346 | different from 0 |
| 341 | 347 | |
| 342 | 348 | `int pm_runtime_resume_and_get(struct device *dev);` |
| @@ -742,13 +742,15 @@ static int rpm_resume(struct device *dev, int rpmflags) · 741 unmodified lines | ||
| 742 | 742 | trace_rpm_resume_rcuidle(dev, rpmflags); |
| 743 | 743 | |
| 744 | 744 | repeat: |
| 745 | − if (dev->power.runtime_error) | |
| 745 | + if (dev->power.runtime_error) { | |
| 746 | 746 | retval = -EINVAL; |
| 747 | − else if (dev->power.disable_depth == 1 && dev->power.is_suspended | |
| 748 | − && dev->power.runtime_status == RPM_ACTIVE) | |
| 749 | − retval = 1; | |
| 750 | − else if (dev->power.disable_depth > 0) | |
| 751 | − retval = -EACCES; | |
| 747 | + } else if (dev->power.disable_depth > 0) { | |
| 748 | + if (dev->power.runtime_status == RPM_ACTIVE && | |
| 749 | + dev->power.last_status == RPM_ACTIVE) | |
| 750 | + retval = 1; | |
| 751 | + else | |
| 752 | + retval = -EACCES; | |
| 753 | + } | |
| 752 | 754 | if (retval) |
| 753 | 755 | goto out; |
| 754 | 756 | |
| @@ -1410,8 +1412,10 @@ void __pm_runtime_disable(struct device *dev, bool check_resume) · 655 unmodified lines | ||
| 1410 | 1412 | /* Update time accounting before disabling PM-runtime. */ |
| 1411 | 1413 | update_pm_runtime_accounting(dev); |
| 1412 | 1414 | |
| 1413 | − if (!dev->power.disable_depth++) | |
| 1415 | + if (!dev->power.disable_depth++) { | |
| 1414 | 1416 | __pm_runtime_barrier(dev); |
| 1417 | + dev->power.last_status = dev->power.runtime_status; | |
| 1418 | + } | |
| 1415 | 1419 | |
| 1416 | 1420 | out: |
| 1417 | 1421 | spin_unlock_irq(&dev->power.lock); |
| @@ -1428,23 +1432,23 @@ void pm_runtime_enable(struct device *dev) · 10 unmodified lines | ||
| 1428 | 1432 | |
| 1429 | 1433 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1430 | 1434 | |
| 1431 | − if (dev->power.disable_depth > 0) { | |
| 1432 | − dev->power.disable_depth--; | |
| 1433 | − | |
| 1434 | − /* About to enable runtime pm, set accounting_timestamp to now */ | |
| 1435 | − if (!dev->power.disable_depth) | |
| 1436 | − dev->power.accounting_timestamp = ktime_get_mono_fast_ns(); | |
| 1437 | − } else { | |
| 1435 | + if (!dev->power.disable_depth) { | |
| 1438 | 1436 | dev_warn(dev, "Unbalanced %s!\n", __func__); |
| 1437 | + goto out; | |
| 1439 | 1438 | } |
| 1440 | 1439 | |
| 1441 | − WARN(!dev->power.disable_depth && | |
| 1442 | − dev->power.runtime_status == RPM_SUSPENDED && | |
| 1443 | − !dev->power.ignore_children && | |
| 1444 | − atomic_read(&dev->power.child_count) > 0, | |
| 1445 | − "Enabling runtime PM for inactive device (%s) with active children\n", | |
| 1446 | − dev_name(dev)); | |
| 1440 | + if (--dev->power.disable_depth > 0) | |
| 1441 | + goto out; | |
| 1447 | 1442 | |
| 1443 | + dev->power.last_status = RPM_INVALID; | |
| 1444 | + dev->power.accounting_timestamp = ktime_get_mono_fast_ns(); | |
| 1445 | + | |
| 1446 | + if (dev->power.runtime_status == RPM_SUSPENDED && | |
| 1447 | + !dev->power.ignore_children && | |
| 1448 | + atomic_read(&dev->power.child_count) > 0) | |
| 1449 | + dev_warn(dev, "Enabling runtime PM for inactive device with active children\n"); | |
| 1450 | + | |
| 1451 | +out: | |
| 1448 | 1452 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1449 | 1453 | } |
| 1450 | 1454 | EXPORT_SYMBOL_GPL(pm_runtime_enable); |
| @@ -1640,6 +1644,7 @@ EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); · 189 unmodified lines | ||
| 1640 | 1644 | void pm_runtime_init(struct device *dev) |
| 1641 | 1645 | { |
| 1642 | 1646 | dev->power.runtime_status = RPM_SUSPENDED; |
| 1647 | + dev->power.last_status = RPM_INVALID; | |
| 1643 | 1648 | dev->power.idle_notification = false; |
| 1644 | 1649 | |
| 1645 | 1650 | dev->power.disable_depth = 1; |
| @@ -521,6 +521,7 @@ const struct dev_pm_ops __maybe_unused name = { \ · 520 unmodified lines | ||
| 521 | 521 | */ |
| 522 | 522 | |
| 523 | 523 | enum rpm_status { |
| 524 | + RPM_INVALID = -1, | |
| 524 | 525 | RPM_ACTIVE = 0, |
| 525 | 526 | RPM_RESUMING, |
| 526 | 527 | RPM_SUSPENDED, |
| @@ -634,6 +635,7 @@ struct dev_pm_info { · 107 unmodified lines | ||
| 634 | 635 | unsigned int links_count; |
| 635 | 636 | enum rpm_request request; |
| 636 | 637 | enum rpm_status runtime_status; |
| 638 | + enum rpm_status last_status; | |
| 637 | 639 | int runtime_error; |
| 638 | 640 | int autosuspend_delay; |
| 639 | 641 | u64 last_busy; |