]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
PM: Block enabling of runtime PM during system suspend
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>
Tue, 18 Feb 2025 20:11:42 +0000 (21:11 +0100)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Tue, 18 Feb 2025 20:43:06 +0000 (21:43 +0100)
If device_prepare() runs on a device that has never had runtime
PM enabled so far, it may reasonably assume that runtime PM will
not be enabled for that device during the system suspend-resume
cycle currently in progress, but this has never been guaranteed.

To verify this assumption, make device_prepare() arrange for
triggering a device warning accompanied by a call trace dump if
runtime PM is enabled for such a device after it has returned.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Ulf Hansson <ulf.hansson@linaro.org>
Link: https://patch.msgid.link/6131109.lOV4Wx5bFT@rjwysocki.net
drivers/base/power/main.c
drivers/base/power/runtime.c
include/linux/pm.h
include/linux/pm_runtime.h

index dffa2aa1ba7da0c9499ae924cbf3a869369d8f3f..acabd9f3e60f37bf06f957323ac29bde40e503d6 100644 (file)
@@ -1109,6 +1109,8 @@ static void device_complete(struct device *dev, pm_message_t state)
        device_unlock(dev);
 
 out:
+       /* If enabling runtime PM for the device is blocked, unblock it. */
+       pm_runtime_unblock(dev);
        pm_runtime_put(dev);
 }
 
@@ -1815,6 +1817,13 @@ static int device_prepare(struct device *dev, pm_message_t state)
         * it again during the complete phase.
         */
        pm_runtime_get_noresume(dev);
+       /*
+        * If runtime PM is disabled for the device at this point and it has
+        * never been enabled so far, it should not be enabled until this system
+        * suspend-resume cycle is complete, so prepare to trigger a warning on
+        * subsequent attempts to enable it.
+        */
+       pm_runtime_block_if_disabled(dev);
 
        if (dev->power.syscore)
                return 0;
index a5aed89e1a6b857e3e3bba926640d96eabfca297..797ea38ceba7d2f2c4355a937238c14f51055dab 100644 (file)
@@ -1460,6 +1460,26 @@ int pm_runtime_barrier(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
 
+void pm_runtime_block_if_disabled(struct device *dev)
+{
+       spin_lock_irq(&dev->power.lock);
+
+       if (dev->power.disable_depth && dev->power.last_status == RPM_INVALID)
+               dev->power.last_status = RPM_BLOCKED;
+
+       spin_unlock_irq(&dev->power.lock);
+}
+
+void pm_runtime_unblock(struct device *dev)
+{
+       spin_lock_irq(&dev->power.lock);
+
+       if (dev->power.last_status == RPM_BLOCKED)
+               dev->power.last_status = RPM_INVALID;
+
+       spin_unlock_irq(&dev->power.lock);
+}
+
 void __pm_runtime_disable(struct device *dev, bool check_resume)
 {
        spin_lock_irq(&dev->power.lock);
@@ -1518,6 +1538,10 @@ void pm_runtime_enable(struct device *dev)
        if (--dev->power.disable_depth > 0)
                goto out;
 
+       if (dev->power.last_status == RPM_BLOCKED) {
+               dev_warn(dev, "Attempt to enable runtime PM when it is blocked\n");
+               dump_stack();
+       }
        dev->power.last_status = RPM_INVALID;
        dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
 
index 78855d794342d21bda124eb038c779875ff491b1..6ca6f34c58c36aa06fef296c738268fc32123e2c 100644 (file)
@@ -597,6 +597,7 @@ enum rpm_status {
        RPM_RESUMING,
        RPM_SUSPENDED,
        RPM_SUSPENDING,
+       RPM_BLOCKED,
 };
 
 /*
index 72c62e1171ca7f6587cf51da83065d7c879a9109..10769119867b5b5e8c4a5b2928ed36b831cf9751 100644 (file)
@@ -77,6 +77,8 @@ extern int pm_runtime_get_if_in_use(struct device *dev);
 extern int pm_schedule_suspend(struct device *dev, unsigned int delay);
 extern int __pm_runtime_set_status(struct device *dev, unsigned int status);
 extern int pm_runtime_barrier(struct device *dev);
+extern void pm_runtime_block_if_disabled(struct device *dev);
+extern void pm_runtime_unblock(struct device *dev);
 extern void pm_runtime_enable(struct device *dev);
 extern void __pm_runtime_disable(struct device *dev, bool check_resume);
 extern void pm_runtime_allow(struct device *dev);
@@ -271,6 +273,8 @@ static inline int pm_runtime_get_if_active(struct device *dev)
 static inline int __pm_runtime_set_status(struct device *dev,
                                            unsigned int status) { return 0; }
 static inline int pm_runtime_barrier(struct device *dev) { return 0; }
+static inline void pm_runtime_block_if_disabled(struct device *dev) {}
+static inline void pm_runtime_unblock(struct device *dev) {}
 static inline void pm_runtime_enable(struct device *dev) {}
 static inline void __pm_runtime_disable(struct device *dev, bool c) {}
 static inline void pm_runtime_allow(struct device *dev) {}