Skip to contentSkip to conversation

driver core: Fixes related to device links

PATCH In review Rafael J. Wysocki 5 participants 26 messages Feb 12, 2019 – Feb 18, 2019

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover driver core: Fixes related to device links

Hi Greg at al,

These fix two issues on top of the recent device links material in driver-core/driver-core-next.

The first one fixes a race condition that may trigger when __pm_runtime_set_status() is used incorrectly (that is, when it is called with PM-runtime enabled for the target device and working).

The second one fixes a supplier PM-runtime usage counter imbalance resulting from adding and removing (e.g. in the error code path) a stateless device link to it from within the consumer driver's probe callback.

Please refer to the patch changelogs for details.

Cheers,
Rafael

#1 lore raw

PATCH 1/2 PM-runtime: Fix __pm_runtime_set_status() race with runtime resume

From: Rafael J. Wysocki <[email protected]>

Commit 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") introduced a race condition that may trigger if __pm_runtime_set_status() is used incorrectly (that is, if it is called when PM-runtime is enabled for the target device and working).

In that case, if the original PM-runtime status of the device is RPM_SUSPENDED, a runtime resume of the device may occur after __pm_runtime_set_status() has dropped its power.lock spinlock and before deactivating its suppliers, so the suppliers may be deactivated while the device is PM-runtime-active which may lead to functional issues.

To avoid that, modify __pm_runtime_set_status() to check whether or not PM-runtime is enabled for the device before activating its suppliers (if the new status is RPM_ACTIVE) and either return an error if that's the case or increment the device's disable_depth counter to prevent PM-runtime from being enabled for it while the remaining part of the function is running (disable_depth is then decremented on the way out).

Fixes 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()")
---
 drivers/base/power/runtime.c |   24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/base/power/runtime.c +18 -6
Index: linux-pm/drivers/base/power/runtime.c
===================================================================
@@ -1129,6 +1129,22 @@ int __pm_runtime_set_status(struct devic
 	if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
 		return -EINVAL;
 
+	spin_lock_irq(&dev->power.lock);
+
+	/*
+	 * Prevent PM-runtime from being enabled for the device or return an
+	 * error if it is enabled already and working.
+	 */
+	if (dev->power.runtime_error || dev->power.disable_depth)
+		dev->power.disable_depth++;
+	else
+		error = -EAGAIN;
+
+	spin_unlock_irq(&dev->power.lock);
+
+	if (error)
+		return error;
+
 	/*
 	 * If the new status is RPM_ACTIVE, the suppliers can be activated
 	 * upfront regardless of the current status, because next time
@@ -1147,12 +1163,6 @@ int __pm_runtime_set_status(struct devic
 
 	spin_lock_irq(&dev->power.lock);
 
-	if (!dev->power.runtime_error && !dev->power.disable_depth) {
-		status = dev->power.runtime_status;
-		error = -EAGAIN;
-		goto out;
-	}
-
 	if (dev->power.runtime_status == status || !parent)
 		goto out_set;
 
@@ -1205,6 +1215,8 @@ int __pm_runtime_set_status(struct devic
 		device_links_read_unlock(idx);
 	}
 
+	pm_runtime_enable(dev);
+
 	return error;
 }
 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
#2 lore raw

Re: [PATCH 1/2] PM-runtime: Fix __pm_runtime_set_status() race with runtime resume

Quoted 71 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") introduced a race condition that may trigger if __pm_runtime_set_status() is used incorrectly (that is, if it is called when PM-runtime is enabled for the target device and working).

In that case, if the original PM-runtime status of the device is RPM_SUSPENDED, a runtime resume of the device may occur after __pm_runtime_set_status() has dropped its power.lock spinlock and before deactivating its suppliers, so the suppliers may be deactivated while the device is PM-runtime-active which may lead to functional issues.

To avoid that, modify __pm_runtime_set_status() to check whether or not PM-runtime is enabled for the device before activating its suppliers (if the new status is RPM_ACTIVE) and either return an error if that's the case or increment the device's disable_depth counter to prevent PM-runtime from being enabled for it while the remaining part of the function is running (disable_depth is then decremented on the way out).

Fixes 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()")
---
 drivers/base/power/runtime.c |   24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +16 -6
@@ -1129,6 +1129,22 @@ int __pm_runtime_set_status(struct devic
        if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
                return -EINVAL;
 
+       spin_lock_irq(&dev->power.lock);
+
+       /*
+        * Prevent PM-runtime from being enabled for the device or return an
+        * error if it is enabled already and working.
+        */
+       if (dev->power.runtime_error || dev->power.disable_depth)
+               dev->power.disable_depth++;
+       else
+               error = -EAGAIN;
+
+       spin_unlock_irq(&dev->power.lock);
+
+       if (error)
+               return error;
+
        /*
         * If the new status is RPM_ACTIVE, the suppliers can be activated
         * upfront regardless of the current status, because next time
@@ -1147,12 +1163,6 @@ int __pm_runtime_set_status(struct devic
 
        spin_lock_irq(&dev->power.lock);
 
-       if (!dev->power.runtime_error && !dev->power.disable_depth) {
-               status = dev->power.runtime_status;
-               error = -EAGAIN;
-               goto out;
-       }
-
        if (dev->power.runtime_status == status || !parent)
                goto out_set;
 

drivers/base/power/runtime.c int __pm_runtime_set_status(struct devic · line 1218

Quoted 4 lines
drivers/base/power/runtime.c +1 -0
@@ -1205,6 +1215,8 @@ int __pm_runtime_set_status(struct devic
                device_links_read_unlock(idx);
        }
 
+       pm_runtime_enable(dev);

pm_runtime_enable() uses spin_lock_irqsave(), rather than spin_lock_irq() - is there a reason to why you want to allow that here, but not earlier in the function?

drivers/base/power/runtime.c int __pm_runtime_set_status(struct devic · line 1222

Quoted 5 lines
drivers/base/power/runtime.c +1 -0
+
        return error;
 }
 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);

Other than the above comment, this looks good to me.

Kind regards
Uffe

#3 lore raw

Re: [PATCH 1/2] PM-runtime: Fix __pm_runtime_set_status() race with runtime resume

Quoted 76 lines · Ulf Hansson
Quoted 74 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") introduced a race condition that may trigger if __pm_runtime_set_status() is used incorrectly (that is, if it is called when PM-runtime is enabled for the target device and working).

In that case, if the original PM-runtime status of the device is RPM_SUSPENDED, a runtime resume of the device may occur after __pm_runtime_set_status() has dropped its power.lock spinlock and before deactivating its suppliers, so the suppliers may be deactivated while the device is PM-runtime-active which may lead to functional issues.

To avoid that, modify __pm_runtime_set_status() to check whether or not PM-runtime is enabled for the device before activating its suppliers (if the new status is RPM_ACTIVE) and either return an error if that's the case or increment the device's disable_depth counter to prevent PM-runtime from being enabled for it while the remaining part of the function is running (disable_depth is then decremented on the way out).

Fixes 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()")
---
 drivers/base/power/runtime.c |   24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +16 -6
@@ -1129,6 +1129,22 @@ int __pm_runtime_set_status(struct devic
        if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
                return -EINVAL;
 
+       spin_lock_irq(&dev->power.lock);
+
+       /*
+        * Prevent PM-runtime from being enabled for the device or return an
+        * error if it is enabled already and working.
+        */
+       if (dev->power.runtime_error || dev->power.disable_depth)
+               dev->power.disable_depth++;
+       else
+               error = -EAGAIN;
+
+       spin_unlock_irq(&dev->power.lock);
+
+       if (error)
+               return error;
+
        /*
         * If the new status is RPM_ACTIVE, the suppliers can be activated
         * upfront regardless of the current status, because next time
@@ -1147,12 +1163,6 @@ int __pm_runtime_set_status(struct devic
 
        spin_lock_irq(&dev->power.lock);
 
-       if (!dev->power.runtime_error && !dev->power.disable_depth) {
-               status = dev->power.runtime_status;
-               error = -EAGAIN;
-               goto out;
-       }
-
        if (dev->power.runtime_status == status || !parent)
                goto out_set;
 
@@ -1205,6 +1215,8 @@ int __pm_runtime_set_status(struct devic
                device_links_read_unlock(idx);
        }
 
Quoted 5 lines

drivers/base/power/runtime.c int __pm_runtime_set_status(struct devic · line 1218

Quoted 1 line
drivers/base/power/runtime.c +1 -0
+       pm_runtime_enable(dev);

pm_runtime_enable() uses spin_lock_irqsave(), rather than spin_lock_irq() - is there a reason to why you want to allow that here, but not earlier in the function?

Device links locking cannot be used in interrupt context.

#4 lore raw

Re: [PATCH 1/2] PM-runtime: Fix __pm_runtime_set_status() race with runtime resume

Quoted 78 lines · Rafael J. Wysocki
Quoted 76 lines · Ulf Hansson
Quoted 74 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") introduced a race condition that may trigger if __pm_runtime_set_status() is used incorrectly (that is, if it is called when PM-runtime is enabled for the target device and working).

In that case, if the original PM-runtime status of the device is RPM_SUSPENDED, a runtime resume of the device may occur after __pm_runtime_set_status() has dropped its power.lock spinlock and before deactivating its suppliers, so the suppliers may be deactivated while the device is PM-runtime-active which may lead to functional issues.

To avoid that, modify __pm_runtime_set_status() to check whether or not PM-runtime is enabled for the device before activating its suppliers (if the new status is RPM_ACTIVE) and either return an error if that's the case or increment the device's disable_depth counter to prevent PM-runtime from being enabled for it while the remaining part of the function is running (disable_depth is then decremented on the way out).

Fixes 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()")
---
 drivers/base/power/runtime.c |   24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +16 -6
@@ -1129,6 +1129,22 @@ int __pm_runtime_set_status(struct devic
        if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
                return -EINVAL;
 
+       spin_lock_irq(&dev->power.lock);
+
+       /*
+        * Prevent PM-runtime from being enabled for the device or return an
+        * error if it is enabled already and working.
+        */
+       if (dev->power.runtime_error || dev->power.disable_depth)
+               dev->power.disable_depth++;
+       else
+               error = -EAGAIN;
+
+       spin_unlock_irq(&dev->power.lock);
+
+       if (error)
+               return error;
+
        /*
         * If the new status is RPM_ACTIVE, the suppliers can be activated
         * upfront regardless of the current status, because next time
@@ -1147,12 +1163,6 @@ int __pm_runtime_set_status(struct devic
 
        spin_lock_irq(&dev->power.lock);
 
-       if (!dev->power.runtime_error && !dev->power.disable_depth) {
-               status = dev->power.runtime_status;
-               error = -EAGAIN;
-               goto out;
-       }
-
        if (dev->power.runtime_status == status || !parent)
                goto out_set;
 
@@ -1205,6 +1215,8 @@ int __pm_runtime_set_status(struct devic
                device_links_read_unlock(idx);
        }
 
Quoted 7 lines
Quoted 5 lines

drivers/base/power/runtime.c int __pm_runtime_set_status(struct devic · line 1218

Quoted 1 line
drivers/base/power/runtime.c +1 -0
+       pm_runtime_enable(dev);

pm_runtime_enable() uses spin_lock_irqsave(), rather than spin_lock_irq() - is there a reason to why you want to allow that here, but not earlier in the function?

Device links locking cannot be used in interrupt context.

I get that, but thanks for clarifying.

However, then why did you convert __pm_runtime_set_status() from using spin_lock_irqsave() to spin_lock_irq() in commit "PM-runtime: Take suppliers into account in __pm_runtime_set_status()"?

As far as I can tell, the spin_lock is not being held while we operate on the device links anyway. Or is this just to give the caller an indication what kind of context the function can be called from?

Kind regards
Uffe

#5 lore raw

Re: [PATCH 1/2] PM-runtime: Fix __pm_runtime_set_status() race with runtime resume

Quoted 90 lines · Ulf Hansson
Quoted 78 lines · Rafael J. Wysocki
Quoted 76 lines · Ulf Hansson
Quoted 74 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") introduced a race condition that may trigger if __pm_runtime_set_status() is used incorrectly (that is, if it is called when PM-runtime is enabled for the target device and working).

In that case, if the original PM-runtime status of the device is RPM_SUSPENDED, a runtime resume of the device may occur after __pm_runtime_set_status() has dropped its power.lock spinlock and before deactivating its suppliers, so the suppliers may be deactivated while the device is PM-runtime-active which may lead to functional issues.

To avoid that, modify __pm_runtime_set_status() to check whether or not PM-runtime is enabled for the device before activating its suppliers (if the new status is RPM_ACTIVE) and either return an error if that's the case or increment the device's disable_depth counter to prevent PM-runtime from being enabled for it while the remaining part of the function is running (disable_depth is then decremented on the way out).

Fixes 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()")
---
 drivers/base/power/runtime.c |   24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +16 -6
@@ -1129,6 +1129,22 @@ int __pm_runtime_set_status(struct devic
        if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
                return -EINVAL;
 
+       spin_lock_irq(&dev->power.lock);
+
+       /*
+        * Prevent PM-runtime from being enabled for the device or return an
+        * error if it is enabled already and working.
+        */
+       if (dev->power.runtime_error || dev->power.disable_depth)
+               dev->power.disable_depth++;
+       else
+               error = -EAGAIN;
+
+       spin_unlock_irq(&dev->power.lock);
+
+       if (error)
+               return error;
+
        /*
         * If the new status is RPM_ACTIVE, the suppliers can be activated
         * upfront regardless of the current status, because next time
@@ -1147,12 +1163,6 @@ int __pm_runtime_set_status(struct devic
 
        spin_lock_irq(&dev->power.lock);
 
-       if (!dev->power.runtime_error && !dev->power.disable_depth) {
-               status = dev->power.runtime_status;
-               error = -EAGAIN;
-               goto out;
-       }
-
        if (dev->power.runtime_status == status || !parent)
                goto out_set;
 
@@ -1205,6 +1215,8 @@ int __pm_runtime_set_status(struct devic
                device_links_read_unlock(idx);
        }
 
Quoted 7 lines
Quoted 5 lines

drivers/base/power/runtime.c int __pm_runtime_set_status(struct devic · line 1218

Quoted 1 line
drivers/base/power/runtime.c +1 -0
+       pm_runtime_enable(dev);

pm_runtime_enable() uses spin_lock_irqsave(), rather than spin_lock_irq() - is there a reason to why you want to allow that here, but not earlier in the function?

Device links locking cannot be used in interrupt context.

I get that, but thanks for clarifying.

Quoted 7 lines

However, then why did you convert __pm_runtime_set_status() from using spin_lock_irqsave() to spin_lock_irq() in commit "PM-runtime: Take suppliers into account in __pm_runtime_set_status()"?

As far as I can tell, the spin_lock is not being held while we operate on the device links anyway. Or is this just to give the caller an indication what kind of context the function can be called from?

The latter plus getting rid of saving/restoring the flags which isn't necessary.

Reviewed-by on v1 1/2
#6 lore raw

Re: [PATCH 1/2] PM-runtime: Fix __pm_runtime_set_status() race with runtime resume

In reply to Rafael J. Wysocki
Quoted 24 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") introduced a race condition that may trigger if __pm_runtime_set_status() is used incorrectly (that is, if it is called when PM-runtime is enabled for the target device and working).

In that case, if the original PM-runtime status of the device is RPM_SUSPENDED, a runtime resume of the device may occur after __pm_runtime_set_status() has dropped its power.lock spinlock and before deactivating its suppliers, so the suppliers may be deactivated while the device is PM-runtime-active which may lead to functional issues.

To avoid that, modify __pm_runtime_set_status() to check whether or not PM-runtime is enabled for the device before activating its suppliers (if the new status is RPM_ACTIVE) and either return an error if that's the case or increment the device's disable_depth counter to prevent PM-runtime from being enabled for it while the remaining part of the function is running (disable_depth is then decremented on the way out).

Quoted 2 lines
Fixes 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()")

Kind regards
Uffe

Quoted 54 lines
---
 drivers/base/power/runtime.c |   24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +18 -6
@@ -1129,6 +1129,22 @@ int __pm_runtime_set_status(struct devic
        if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
                return -EINVAL;
 
+       spin_lock_irq(&dev->power.lock);
+
+       /*
+        * Prevent PM-runtime from being enabled for the device or return an
+        * error if it is enabled already and working.
+        */
+       if (dev->power.runtime_error || dev->power.disable_depth)
+               dev->power.disable_depth++;
+       else
+               error = -EAGAIN;
+
+       spin_unlock_irq(&dev->power.lock);
+
+       if (error)
+               return error;
+
        /*
         * If the new status is RPM_ACTIVE, the suppliers can be activated
         * upfront regardless of the current status, because next time
@@ -1147,12 +1163,6 @@ int __pm_runtime_set_status(struct devic
 
        spin_lock_irq(&dev->power.lock);
 
-       if (!dev->power.runtime_error && !dev->power.disable_depth) {
-               status = dev->power.runtime_status;
-               error = -EAGAIN;
-               goto out;
-       }
-
        if (dev->power.runtime_status == status || !parent)
                goto out_set;
 
@@ -1205,6 +1215,8 @@ int __pm_runtime_set_status(struct devic
                device_links_read_unlock(idx);
        }
 
+       pm_runtime_enable(dev);
+
        return error;
 }
 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
#7 lore raw

PATCH 2/2 driver core: Fix possible supplier PM-usage counter imbalance

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

---
 drivers/base/core.c          |   21 ++++-----------------
 drivers/base/power/runtime.c |   27 +++++++++++++++++++++++++--
 include/linux/pm_runtime.h   |    4 ++++
 3 files changed, 33 insertions(+), 19 deletions(-)
drivers/base/power/runtime.c +25 -2
Index: linux-pm/drivers/base/power/runtime.c
===================================================================
@@ -1655,8 +1655,10 @@ void pm_runtime_get_suppliers(struct dev
 	idx = device_links_read_lock();
 
 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
-		if (link->flags & DL_FLAG_PM_RUNTIME)
+		if (link->flags & DL_FLAG_PM_RUNTIME) {
+			refcount_inc(&link->rpm_active);
 			pm_runtime_get_sync(link->supplier);
+		}
 
 	device_links_read_unlock(idx);
 }
@@ -1673,7 +1675,8 @@ void pm_runtime_put_suppliers(struct dev
 	idx = device_links_read_lock();
 
 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
-		if (link->flags & DL_FLAG_PM_RUNTIME)
+		if (link->flags & DL_FLAG_PM_RUNTIME &&
+		    refcount_dec_not_one(&link->rpm_active))
 			pm_runtime_put(link->supplier);
 
 	device_links_read_unlock(idx);
@@ -1686,6 +1689,26 @@ void pm_runtime_new_link(struct device *
 	spin_unlock_irq(&dev->power.lock);
 }
 
+/**
+ * pm_runtime_active_link - Set up new device link as active for PM-runtime.
+ * @link: Device link to be set up as active.
+ * @supplier: Supplier end of the link.
+ *
+ * Add 2 to the rpm_active refcount of @link and increment the PM-runtime
+ * usage counter of @supplier once more in case the link is being added while
+ * the consumer driver is probing and pm_runtime_put_suppliers() will be called
+ * subsequently.
+ *
+ * Note that this doesn't prevent rpm_put_suppliers() from decreasing the link's
+ * rpm_active refcount down to one, so runtime suspend of the consumer end of
+ * @link is not affected.
+ */
+void pm_runtime_active_link(struct device_link *link, struct device *supplier)
+{
+	refcount_add(2, &link->rpm_active);
+	pm_runtime_get_noresume(supplier);
+}
+
 void pm_runtime_drop_link(struct device *dev)
 {
 	spin_lock_irq(&dev->power.lock);
drivers/base/core.c +4 -17
Index: linux-pm/drivers/base/core.c
===================================================================
@@ -165,19 +165,6 @@ void device_pm_move_to_tail(struct devic
 	device_links_read_unlock(idx);
 }
 
-static void device_link_rpm_prepare(struct device *consumer,
-				    struct device *supplier)
-{
-	pm_runtime_new_link(consumer);
-	/*
-	 * If the link is being added by the consumer driver at probe time,
-	 * balance the decrementation of the supplier's runtime PM usage counter
-	 * after consumer probe in driver_probe_device().
-	 */
-	if (consumer->links.status == DL_DEV_PROBING)
-		pm_runtime_get_noresume(supplier);
-}
-
 /**
  * device_link_add - Create a link between two devices.
  * @consumer: Consumer end of the link.
@@ -286,11 +273,11 @@ struct device_link *device_link_add(stru
 
 		if (flags & DL_FLAG_PM_RUNTIME) {
 			if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
-				device_link_rpm_prepare(consumer, supplier);
+				pm_runtime_new_link(consumer);
 				link->flags |= DL_FLAG_PM_RUNTIME;
 			}
 			if (flags & DL_FLAG_RPM_ACTIVE)
-				refcount_inc(&link->rpm_active);
+				pm_runtime_active_link(link, supplier);
 		}
 
 		if (flags & DL_FLAG_STATELESS) {
@@ -323,9 +310,9 @@ struct device_link *device_link_add(stru
 
 	if (flags & DL_FLAG_PM_RUNTIME) {
 		if (flags & DL_FLAG_RPM_ACTIVE)
-			refcount_inc(&link->rpm_active);
+			pm_runtime_active_link(link, supplier);
 
-		device_link_rpm_prepare(consumer, supplier);
+		pm_runtime_new_link(consumer);
 	}
 
 	get_device(supplier);
include/linux/pm_runtime.h +4 -0
Index: linux-pm/include/linux/pm_runtime.h
===================================================================
@@ -59,6 +59,8 @@ extern void pm_runtime_clean_up_links(st
 extern void pm_runtime_get_suppliers(struct device *dev);
 extern void pm_runtime_put_suppliers(struct device *dev);
 extern void pm_runtime_new_link(struct device *dev);
+extern void pm_runtime_active_link(struct device_link *link,
+				   struct device *supplier);
 extern void pm_runtime_drop_link(struct device *dev);
 
 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
@@ -178,6 +180,8 @@ static inline void pm_runtime_clean_up_l
 static inline void pm_runtime_get_suppliers(struct device *dev) {}
 static inline void pm_runtime_put_suppliers(struct device *dev) {}
 static inline void pm_runtime_new_link(struct device *dev) {}
+static inline void pm_runtime_active_link(struct device_link *link,
+					  struct device *supplier) {}
 static inline void pm_runtime_drop_link(struct device *dev) {}
 
 #endif /* !CONFIG_PM */
Reviewed-by Tested-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 27 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Quoted 8 lines

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")

I have tested a several various common probe sequences (including error paths) by using my RPM test driver, no issues found. Of course it also fixes the problem that occurred while deleting the device link during ->probe(). Thanks a lot!

Kind regards
Uffe

Quoted 139 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

---
 drivers/base/core.c          |   21 ++++-----------------
 drivers/base/power/runtime.c |   27 +++++++++++++++++++++++++--
 include/linux/pm_runtime.h   |    4 ++++
 3 files changed, 33 insertions(+), 19 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +25 -2
@@ -1655,8 +1655,10 @@ void pm_runtime_get_suppliers(struct dev
        idx = device_links_read_lock();
 
        list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
-               if (link->flags & DL_FLAG_PM_RUNTIME)
+               if (link->flags & DL_FLAG_PM_RUNTIME) {
+                       refcount_inc(&link->rpm_active);
                        pm_runtime_get_sync(link->supplier);
+               }
 
        device_links_read_unlock(idx);
 }
@@ -1673,7 +1675,8 @@ void pm_runtime_put_suppliers(struct dev
        idx = device_links_read_lock();
 
        list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
-               if (link->flags & DL_FLAG_PM_RUNTIME)
+               if (link->flags & DL_FLAG_PM_RUNTIME &&
+                   refcount_dec_not_one(&link->rpm_active))
                        pm_runtime_put(link->supplier);
 
        device_links_read_unlock(idx);
@@ -1686,6 +1689,26 @@ void pm_runtime_new_link(struct device *
        spin_unlock_irq(&dev->power.lock);
 }
 
+/**
+ * pm_runtime_active_link - Set up new device link as active for PM-runtime.
+ * @link: Device link to be set up as active.
+ * @supplier: Supplier end of the link.
+ *
+ * Add 2 to the rpm_active refcount of @link and increment the PM-runtime
+ * usage counter of @supplier once more in case the link is being added while
+ * the consumer driver is probing and pm_runtime_put_suppliers() will be called
+ * subsequently.
+ *
+ * Note that this doesn't prevent rpm_put_suppliers() from decreasing the link's
+ * rpm_active refcount down to one, so runtime suspend of the consumer end of
+ * @link is not affected.
+ */
+void pm_runtime_active_link(struct device_link *link, struct device *supplier)
+{
+       refcount_add(2, &link->rpm_active);
+       pm_runtime_get_noresume(supplier);
+}
+
 void pm_runtime_drop_link(struct device *dev)
 {
        spin_lock_irq(&dev->power.lock);

Index: linux-pm/drivers/base/core.c
===================================================================

drivers/base/core.c +4 -17
@@ -165,19 +165,6 @@ void device_pm_move_to_tail(struct devic
        device_links_read_unlock(idx);
 }
 
-static void device_link_rpm_prepare(struct device *consumer,
-                                   struct device *supplier)
-{
-       pm_runtime_new_link(consumer);
-       /*
-        * If the link is being added by the consumer driver at probe time,
-        * balance the decrementation of the supplier's runtime PM usage counter
-        * after consumer probe in driver_probe_device().
-        */
-       if (consumer->links.status == DL_DEV_PROBING)
-               pm_runtime_get_noresume(supplier);
-}
-
 /**
  * device_link_add - Create a link between two devices.
  * @consumer: Consumer end of the link.
@@ -286,11 +273,11 @@ struct device_link *device_link_add(stru
 
                if (flags & DL_FLAG_PM_RUNTIME) {
                        if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
-                               device_link_rpm_prepare(consumer, supplier);
+                               pm_runtime_new_link(consumer);
                                link->flags |= DL_FLAG_PM_RUNTIME;
                        }
                        if (flags & DL_FLAG_RPM_ACTIVE)
-                               refcount_inc(&link->rpm_active);
+                               pm_runtime_active_link(link, supplier);
                }
 
                if (flags & DL_FLAG_STATELESS) {
@@ -323,9 +310,9 @@ struct device_link *device_link_add(stru
 
        if (flags & DL_FLAG_PM_RUNTIME) {
                if (flags & DL_FLAG_RPM_ACTIVE)
-                       refcount_inc(&link->rpm_active);
+                       pm_runtime_active_link(link, supplier);
 
-               device_link_rpm_prepare(consumer, supplier);
+               pm_runtime_new_link(consumer);
        }
 
        get_device(supplier);

Index: linux-pm/include/linux/pm_runtime.h
===================================================================

include/linux/pm_runtime.h +4 -0
@@ -59,6 +59,8 @@ extern void pm_runtime_clean_up_links(st
 extern void pm_runtime_get_suppliers(struct device *dev);
 extern void pm_runtime_put_suppliers(struct device *dev);
 extern void pm_runtime_new_link(struct device *dev);
+extern void pm_runtime_active_link(struct device_link *link,
+                                  struct device *supplier);
 extern void pm_runtime_drop_link(struct device *dev);
 
 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
@@ -178,6 +180,8 @@ static inline void pm_runtime_clean_up_l
 static inline void pm_runtime_get_suppliers(struct device *dev) {}
 static inline void pm_runtime_put_suppliers(struct device *dev) {}
 static inline void pm_runtime_new_link(struct device *dev) {}
+static inline void pm_runtime_active_link(struct device_link *link,
+                                         struct device *supplier) {}
 static inline void pm_runtime_drop_link(struct device *dev) {}
 
 #endif /* !CONFIG_PM */
#9 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 38 lines · Ulf Hansson
Quoted 27 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Quoted 8 lines

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 7 lines

I have tested a several various common probe sequences (including error paths) by using my RPM test driver, no issues found. Of course it also fixes the problem that occurred while deleting the device link during ->probe(). Thanks a lot!

Thank you!

maintainer
#10 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

In reply to Rafael J. Wysocki

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Cheers
Jon

Signature · 1 line
nvpublic
#11 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 45 lines · Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

Quoted 4 lines

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

Is it failing because of this particular commit? That is, does reverting the entire commit help?

Quoted 7 lines

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

It won't be, because you use stateless links.

Quoted 3 lines

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Well, if it breaks, then there is a bug somewhere. I'm not seeing it now, but let's dig into this.

Since you don't pass DL_FLAG_RPM_ACTIVE to device_link_add(), the changes related to that don't matter.

The links are not there before your probe function runs. It adds the links and then pm_runtime_put_suppliers() sees them, but since link->rpm_active is one for the new links, it won't do anything with them.

Well, there is a difference, but if it matters, then something fishy is going on IMO. Before this change pm_runtime_put_suppliers() would do pm_runtime_put() on the new links' suppliers and (because their PM-runtime usage counters are both one at that point) it will actually try to suspend the suppliers. It should be easy enough to verify if this really matters, stay tuned.

#12 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

In reply to Jon Hunter
Quoted 51 lines · CET Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

Quoted 8 lines

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Please try the appended patch on top of the $subject one (provided that reverting the $subject patch makes the problem go away).

---
 drivers/base/power/runtime.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/base/power/runtime.c +6 -3
Index: linux-pm/drivers/base/power/runtime.c
===================================================================
@@ -1675,9 +1675,12 @@ void pm_runtime_put_suppliers(struct dev
 	idx = device_links_read_lock();
 
 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
-		if (link->flags & DL_FLAG_PM_RUNTIME &&
-		    refcount_dec_not_one(&link->rpm_active))
-			pm_runtime_put(link->supplier);
+		if (link->flags & DL_FLAG_PM_RUNTIME) {
+			if (refcount_dec_not_one(&link->rpm_active))
+				pm_runtime_put(link->supplier);
+			else
+				pm_request_idle(link->supplier);
+		}
 
 	device_links_read_unlock(idx);
 }
maintainer
#13 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 57 lines · Rafael J. Wysocki
Quoted 56 lines · CET Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

Quoted 6 lines
Quoted 3 lines

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Please try the appended patch on top of the $subject one (provided that reverting the $subject patch makes the problem go away).

Thanks and yes to confirm, reverting the $subject patch on top of next does make the issue go away.

Quoted 14 lines
---
 drivers/base/power/runtime.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +0 -2
@@ -1675,9 +1675,12 @@ void pm_runtime_put_suppliers(struct dev
 	idx = device_links_read_lock();
 
 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
-		if (link->flags & DL_FLAG_PM_RUNTIME &&
-		    refcount_dec_not_one(&link->rpm_active))

drivers/base/power/runtime.c void pm_runtime_put_suppliers(struct dev · line 1686

Quoted 10 lines
drivers/base/power/runtime.c +6 -1
-			pm_runtime_put(link->supplier);
+		if (link->flags & DL_FLAG_PM_RUNTIME) {
+			if (refcount_dec_not_one(&link->rpm_active))
+				pm_runtime_put(link->supplier);
+			else
+				pm_request_idle(link->supplier);
+		}
 
 	device_links_read_unlock(idx);
 }

I will try this now and report back in a bit.

Cheers
Jon

Signature · 1 line
nvpublic
maintainer
#14 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 91 lines · Jon Hunter
Quoted 57 lines · Rafael J. Wysocki
Quoted 56 lines · CET Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

Quoted 6 lines
Quoted 3 lines

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Please try the appended patch on top of the $subject one (provided that reverting the $subject patch makes the problem go away).

Thanks and yes to confirm, reverting the $subject patch on top of next does make the issue go away.

Quoted 22 lines
---
 drivers/base/power/runtime.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Index: linux-pm/drivers/base/power/runtime.c
===================================================================

drivers/base/power/runtime.c +6 -3
@@ -1675,9 +1675,12 @@ void pm_runtime_put_suppliers(struct dev
 	idx = device_links_read_lock();
 
 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
-		if (link->flags & DL_FLAG_PM_RUNTIME &&
-		    refcount_dec_not_one(&link->rpm_active))
-			pm_runtime_put(link->supplier);
+		if (link->flags & DL_FLAG_PM_RUNTIME) {
+			if (refcount_dec_not_one(&link->rpm_active))
+				pm_runtime_put(link->supplier);
+			else
+				pm_request_idle(link->supplier);
+		}
 
Quoted 4 lines

drivers/base/power/runtime.c void pm_runtime_put_suppliers(struct dev · line 1686

Quoted 2 lines
drivers/base/power/runtime.c +0 -0
 	device_links_read_unlock(idx);
 }

I will try this now and report back in a bit.

I tried this on top of next, but unfortunately the same issue still persists and so this did not fix it. Let me know if there is any debug I can add/enable.

Cheers
Jon

Signature · 1 line
nvpublic
#15 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

In reply to Jon Hunter
Quoted 52 lines · Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

Quoted 8 lines

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

If you are willing to help debugging then I am offering my assistance.

I would start by enabling CONFIG_PM_ADVANCED_DEBUG, which gives you some more information about the runtime PM state of the device, like the usage count for example.
I would also add a couple of prints in tegra_xusb_runtime_suspend|resume() and in the ->power_on|off() callbacks for the corresponding genpds, to see when those gets called.

While I was testing $subject patch I also used a local debug patch, which adds a sysfs node that can be used to get the state of linked suppliers for a consumer device. Feel free to use it, attached below.

Of course, the interesting part is the comparison of what happens with and without $subject patch.

From: Ulf Hansson <[email protected]>
Date: Mon, 11 Feb 2019 15:37:44 +0100
Subject: [PATCH] PM / Runtime: Add sysfs for runtime counting of suppliers

---
 drivers/base/power/sysfs.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/base/power/sysfs.c +20 -0
index d713738ce796..ce5c188cdf54 100644
@@ -537,6 +537,25 @@ static ssize_t runtime_enabled_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(runtime_enabled);
 
+static ssize_t runtime_suppliers_show(struct device *dev,
+                                 struct device_attribute *attr, char *buf)
+{
+       struct device_link *link;
+       int chars = 0;
+
+       list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
+
+               if (!(link->flags & DL_FLAG_PM_RUNTIME))
+                       continue;
+
+               chars += sprintf(buf + chars, "%s %d\n",
+                               dev_name(link->supplier),
+                               refcount_read(&link->rpm_active));
+       }
+       return chars;
+}
+static DEVICE_ATTR_RO(runtime_suppliers);
+
 #ifdef CONFIG_PM_SLEEP
 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
                          char *buf)
@@ -572,6 +591,7 @@ static struct attribute *power_attrs[] = {
        &dev_attr_runtime_usage.attr,
        &dev_attr_runtime_active_kids.attr,
        &dev_attr_runtime_enabled.attr,
+       &dev_attr_runtime_suppliers.attr,
 #endif /* CONFIG_PM_ADVANCED_DEBUG */
        NULL,
 };
Signature · 4 lines
2.17.1

Kind regards
Uffe
maintainer
#16 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 64 lines · Ulf Hansson
Quoted 52 lines · Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

Quoted 8 lines

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

If you are willing to help debugging then I am offering my assistance.

Quoted 8 lines

I would start by enabling CONFIG_PM_ADVANCED_DEBUG, which gives you some more information about the runtime PM state of the device, like the usage count for example.
I would also add a couple of prints in tegra_xusb_runtime_suspend|resume() and in the ->power_on|off() callbacks for the corresponding genpds, to see when those gets called.

From the bootlog I see ...

[    4.445827] tegra_xusb_runtime_resume-788
[    4.508799] tegra-xusb 70090000.usb: Firmware timestamp: 2015-08-10 09:47:54 UTC
[    4.516223] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.521622] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 1
[    4.530087] tegra-xusb 70090000.usb: hcc params 0x0184f525 hci version 0x100 quirks 0x0000000000010010
[    4.539398] tegra-xusb 70090000.usb: irq 69, io mem 0x70090000
[    4.553671] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.559064] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 2
[    4.566622] tegra-xusb 70090000.usb: Host supports USB 3.0  SuperSpeed
[    4.595393] tegra-pmc: tegra_genpd_power_off-673: xusbc
[    4.600672] tegra-pmc: tegra_genpd_power_off-673: xusba
[    4.657346] tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead
[    4.665157] tegra-xusb 70090000.usb: HC died; cleaning up

This shows the xusb controller is runtime resumed during probe but then after probe the pm-domains, xusba and xusbc, are turned off without it being runtime suspended. We never suspend it until it is removed currently.

Following boot the xusb device appears to still be active ...

$ cat /sys/devices/platform/70090000.usb/power/runtime_status 
active

... but the status reported by the pm_genpd summary disagrees ...

$ sudo cat /sys/kernel/debug/pm_genpd/pm_genpd_summary
domain                          status          slaves
    /device                                             runtime status
----------------------------------------------------------------------
vic                             off-0           
    /devices/platform/50000000.host1x/54340000.vic      suspended
xusbc                           off-0           
    /devices/genpd:0:70090000.usb                       suspended
xusbb                           off-0           
xusba                           off-0           
    /devices/genpd:1:70090000.usb                       suspended
sor                             on              
    /devices/platform/700e3000.mipi                     unsupported
    /devices/platform/50000000.host1x/54300000.dsi      active
    /devices/platform/50000000.host1x/54040000.dpaux    active
    /devices/platform/50000000.host1x/54580000.sor      suspended
aud                             off-0           

... and ...

$ cat /sys/devices/platform/70090000.usb/power/runtime_suppliers 
genpd:0:70090000.usb 1
genpd:1:70090000.usb 1

Let me know if you have any thoughts.

Cheers
Jon

Signature · 1 line
nvpublic
#17 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 90 lines · Jon Hunter
Quoted 62 lines · Ulf Hansson
Quoted 52 lines · Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

Quoted 8 lines

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Quoted 8 lines

If you are willing to help debugging then I am offering my assistance.

I would start by enabling CONFIG_PM_ADVANCED_DEBUG, which gives you some more information about the runtime PM state of the device, like the usage count for example.
I would also add a couple of prints in tegra_xusb_runtime_suspend|resume() and in the ->power_on|off() callbacks for the corresponding genpds, to see when those gets called.

From the bootlog I see ...

[    4.445827] tegra_xusb_runtime_resume-788
[    4.508799] tegra-xusb 70090000.usb: Firmware timestamp: 2015-08-10 09:47:54 UTC
[    4.516223] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.521622] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 1
[    4.530087] tegra-xusb 70090000.usb: hcc params 0x0184f525 hci version 0x100 quirks 0x0000000000010010
[    4.539398] tegra-xusb 70090000.usb: irq 69, io mem 0x70090000
[    4.553671] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.559064] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 2
[    4.566622] tegra-xusb 70090000.usb: Host supports USB 3.0  SuperSpeed
[    4.595393] tegra-pmc: tegra_genpd_power_off-673: xusbc
[    4.600672] tegra-pmc: tegra_genpd_power_off-673: xusba
[    4.657346] tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead
[    4.665157] tegra-xusb 70090000.usb: HC died; cleaning up
Quoted 4 lines

This shows the xusb controller is runtime resumed during probe but then after probe the pm-domains, xusba and xusbc, are turned off without it being runtime suspended. We never suspend it until it is removed currently.

As I said offline on Sat, I suspected that the suppliers (that would be xusba and xusbc if my understanding of the system is correct) were suspended prematurely during or after the consumer (xusb) probe, which previously had been prevented from occurring by the pm_runtime_get_noresume(supplier) removed by the $subject patch.

What you said above seems to be in agreement with that theory, so it looks like you simply need to pass DL_FLAG_RPM_ACTICE to device_link_add() in both cases, as that would prevent the suppliers from being suspended until the consumer suspends (or its PM-runtime status is changed to "suspended"). And if the consumer never suspends, the suppliers will never suspend too then.

#18 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

In reply to Jon Hunter
Quoted 74 lines · Jon Hunter
Quoted 62 lines · Ulf Hansson
Quoted 52 lines · Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

Quoted 8 lines

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Quoted 8 lines

If you are willing to help debugging then I am offering my assistance.

I would start by enabling CONFIG_PM_ADVANCED_DEBUG, which gives you some more information about the runtime PM state of the device, like the usage count for example.
I would also add a couple of prints in tegra_xusb_runtime_suspend|resume() and in the ->power_on|off() callbacks for the corresponding genpds, to see when those gets called.

Quoted 4 lines

From the bootlog I see ...

[    4.445827] tegra_xusb_runtime_resume-788
[    4.508799] tegra-xusb 70090000.usb: Firmware timestamp: 2015-08-10 09:47:54 UTC

This message comes from tegra_xusb_load_firmware() in tegra_xusb_probe() which is after the pm_runtime_get_sync().

If the device was PM-runtime-suspended before, the pm_runtime_get_sync() will runtime-resume and reference-count the suppliers in addition to resuming the device. In that case pm_runtime_put_suppliers() will suspend the suppliers, so there is a bug in there.

What happens is that the links are new when pm_runtime_get_sync() runs and so their rpm_active refcounts are one. After the pm_runtime_get_sync() they are two and pm_runtime_put_suppliers() will drop them by one and drop the PM-runtime usage counter of each of them by one, so they will become zero and the suppliers will suspend.

Passing DL_FLAG_RPM_ACTIVE to device_link_add() should help, but IMO things should also work without that.

Quoted 2 lines
[    4.516223] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.521622] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 1

This comes from usb_add_hcd()

Quoted 4 lines
[    4.530087] tegra-xusb 70090000.usb: hcc params 0x0184f525 hci version 0x100 quirks 0x0000000000010010
[    4.539398] tegra-xusb 70090000.usb: irq 69, io mem 0x70090000
[    4.553671] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.559064] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 2

Like this.

Quoted 1 line
[    4.566622] tegra-xusb 70090000.usb: Host supports USB 3.0  SuperSpeed

And this if from xhci_gen_setup(), so probe returns around this point.

Quoted 2 lines
[    4.595393] tegra-pmc: tegra_genpd_power_off-673: xusbc
[    4.600672] tegra-pmc: tegra_genpd_power_off-673: xusba

And this appears to be done by pm_runtime_put_suppliers().

Hmm, I need to think how to fix this. Maybe we'll need to revert $subject patch and do something else, we'll see (later today).

maintainer
#19 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 89 lines · Rafael J. Wysocki
Quoted 74 lines · Jon Hunter
Quoted 62 lines · Ulf Hansson
Quoted 52 lines · Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

Quoted 8 lines

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Quoted 8 lines

If you are willing to help debugging then I am offering my assistance.

I would start by enabling CONFIG_PM_ADVANCED_DEBUG, which gives you some more information about the runtime PM state of the device, like the usage count for example.
I would also add a couple of prints in tegra_xusb_runtime_suspend|resume() and in the ->power_on|off() callbacks for the corresponding genpds, to see when those gets called.

Quoted 4 lines

From the bootlog I see ...

[    4.445827] tegra_xusb_runtime_resume-788
[    4.508799] tegra-xusb 70090000.usb: Firmware timestamp: 2015-08-10 09:47:54 UTC

This message comes from tegra_xusb_load_firmware() in tegra_xusb_probe() which is after the pm_runtime_get_sync().

If the device was PM-runtime-suspended before, the pm_runtime_get_sync() will runtime-resume and reference-count the suppliers in addition to resuming the device. In that case pm_runtime_put_suppliers() will suspend the suppliers, so there is a bug in there.

Quoted 8 lines

What happens is that the links are new when pm_runtime_get_sync() runs and so their rpm_active refcounts are one. After the pm_runtime_get_sync() they are two and pm_runtime_put_suppliers() will drop them by one and drop the PM-runtime usage counter of each of them by one, so they will become zero and the suppliers will suspend.

Passing DL_FLAG_RPM_ACTIVE to device_link_add() should help, but IMO things should also work without that.

I can confirm that DL_FLAG_RPM_ACTIVE does indeed work. I assume though this would prevent the suppliers from ever being suspended, which maybe we will want to do eventually.

Quoted 16 lines
Quoted 2 lines
[    4.516223] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.521622] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 1

This comes from usb_add_hcd()

Quoted 4 lines
[    4.530087] tegra-xusb 70090000.usb: hcc params 0x0184f525 hci version 0x100 quirks 0x0000000000010010
[    4.539398] tegra-xusb 70090000.usb: irq 69, io mem 0x70090000
[    4.553671] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.559064] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 2

Like this.

Quoted 1 line
[    4.566622] tegra-xusb 70090000.usb: Host supports USB 3.0  SuperSpeed

And this if from xhci_gen_setup(), so probe returns around this point.

Quoted 7 lines
Quoted 2 lines
[    4.595393] tegra-pmc: tegra_genpd_power_off-673: xusbc
[    4.600672] tegra-pmc: tegra_genpd_power_off-673: xusba

And this appears to be done by pm_runtime_put_suppliers().

Hmm, I need to think how to fix this. Maybe we'll need to revert $subject patch and do something else, we'll see (later today).

OK, thanks. Let me know if there is anything else I can test.

Cheers
Jon

Signature · 1 line
nvpublic
#20 lore raw

Re: [PATCH 2/2] driver core: Fix possible supplier PM-usage counter imbalance

Quoted 97 lines · CET Jon Hunter
Quoted 95 lines · Rafael J. Wysocki
Quoted 74 lines · Jon Hunter
Quoted 62 lines · Ulf Hansson
Quoted 52 lines · Jon Hunter

Hi Rafael,

Quoted 34 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If a stateless device link to a certain supplier with DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the consumer driver's probe callback, the supplier's PM-runtime usage counter will be nonzero after that which effectively causes the supplier to remain "always on" going forward.

Namely, device_link_add() called to add the link invokes device_link_rpm_prepare() which notices that the consumer driver is probing, so it increments the supplier's PM-runtime usage counter with the assumption that the link will stay around until pm_runtime_put_suppliers() is called by driver_probe_device(), but if the link goes away before that point, the supplier's PM-runtime usage counter will remain nonzero.

To prevent that from happening, first rework pm_runtime_get_suppliers() and pm_runtime_put_suppliers() to use the rpm_active refounts of device links and make the latter only drop rpm_active and the supplier's PM-runtime usage counter for each link by one, unless rpm_active is one already for it. Next, modify device_link_add() to bump up the new link's rpm_active refcount and the suppliers PM-runtime usage counter by two, to prevent pm_runtime_put_suppliers(), if it is called subsequently, from suspending the supplier prematurely (in case its PM-runtime usage counter goes down to 0 in there).

Due to the way rpm_put_suppliers() works, this change does not affect runtime suspend of the consumer ends of new device links (or, generally, device links for which DL_FLAG_PM_RUNTIME has just been set).

Fixes e2f3cd831a28 ("driver core: Fix handling of runtime PM flags in device_link_add()")
Quoted 6 lines

---

Note that the issue had been there before commit e2f3cd831a28, but it was overlooked by that commit and this change is a fix on top of it, so make the Fixes: tag point to commit e2f3cd831a28 (instead of an earlier one that the patch will not be applicable to).

I noticed that yesterday's and today's -next were no longer booting on one of our Tegra boards (Tegra210 Jetson TX2) because networking is failing. The ethernet chip is a USB device and looking at the bootlogs I can see that the Tegra XHCI driver is failing ...

tegra-xusb 70090000.usb: xHCI host controller not responding, assume dead tegra-xusb 70090000.usb: HC died; cleaning up

Quoted 8 lines

The Tegra XHCI driver uses multiple power-domains and uses device_link_add() to attach them. So now I am wondering if there is something that we have got wrong in our implementation. However, I don't see the device being probed deferred on boot or anything like that.

The driver in question is drivers/usb/host/xhci-tegra.c and we add the links in the function tegra_xusb_powerdomain_init() which is before RPM is enabled. Let me know if you have any thoughts.

Quoted 8 lines

If you are willing to help debugging then I am offering my assistance.

I would start by enabling CONFIG_PM_ADVANCED_DEBUG, which gives you some more information about the runtime PM state of the device, like the usage count for example.
I would also add a couple of prints in tegra_xusb_runtime_suspend|resume() and in the ->power_on|off() callbacks for the corresponding genpds, to see when those gets called.

Quoted 4 lines

From the bootlog I see ...

[    4.445827] tegra_xusb_runtime_resume-788
[    4.508799] tegra-xusb 70090000.usb: Firmware timestamp: 2015-08-10 09:47:54 UTC

This message comes from tegra_xusb_load_firmware() in tegra_xusb_probe() which is after the pm_runtime_get_sync().

If the device was PM-runtime-suspended before, the pm_runtime_get_sync() will runtime-resume and reference-count the suppliers in addition to resuming the device. In that case pm_runtime_put_suppliers() will suspend the suppliers, so there is a bug in there.

What happens is that the links are new when pm_runtime_get_sync() runs and so their rpm_active refcounts are one. After the pm_runtime_get_sync() they are two and pm_runtime_put_suppliers() will drop them by one and drop the PM-runtime usage counter of each of them by one, so they will become zero and the suppliers will suspend.

Quoted 5 lines
Quoted 2 lines

Passing DL_FLAG_RPM_ACTIVE to device_link_add() should help, but IMO things should also work without that.

I can confirm that DL_FLAG_RPM_ACTIVE does indeed work. I assume though this would prevent the suppliers from ever being suspended,

No, it wouldn't. It only prevents the supplier from suspending as long as the consumer remains active.

In your case it shouldn't matter (it only matters because of the bug in the $subject patch), because the consumer is suspended to start with.

Had it been active initially, though, passing DL_FLAG_RPM_ACTIVE to device_link_add() would have been the only way to to force the supplier to remain active accross the return from consumer probe until the consumer is suspended.

Quoted 22 lines

which maybe
we will want to do eventually.

Quoted 19 lines
Quoted 2 lines
[    4.516223] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.521622] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 1

This comes from usb_add_hcd()

Quoted 4 lines
[    4.530087] tegra-xusb 70090000.usb: hcc params 0x0184f525 hci version 0x100 quirks 0x0000000000010010
[    4.539398] tegra-xusb 70090000.usb: irq 69, io mem 0x70090000
[    4.553671] tegra-xusb 70090000.usb: xHCI Host Controller
[    4.559064] tegra-xusb 70090000.usb: new USB bus registered, assigned bus number 2

Like this.

Quoted 1 line
[    4.566622] tegra-xusb 70090000.usb: Host supports USB 3.0  SuperSpeed

And this if from xhci_gen_setup(), so probe returns around this point.

Quoted 2 lines
[    4.595393] tegra-pmc: tegra_genpd_power_off-673: xusbc
[    4.600672] tegra-pmc: tegra_genpd_power_off-673: xusba
Quoted 6 lines
Quoted 4 lines

And this appears to be done by pm_runtime_put_suppliers().

Hmm, I need to think how to fix this. Maybe we'll need to revert $subject patch and do something else, we'll see (later today).

OK, thanks. Let me know if there is anything else I can test.

Please test https://patchwork.kernel.org/patch/10818923/

Cheers,
Rafael

#21 lore raw
In reply to Rafael J. Wysocki
Quoted 9 lines · Rafael J. Wysocki

Hi Greg at al,

These fix two issues on top of the recent device links material in driver-core/driver-core-next.

The first one fixes a race condition that may trigger when __pm_runtime_set_status() is used incorrectly (that is, when it is called with PM-runtime enabled for the target device and working).

Quoted 6 lines

The second one fixes a supplier PM-runtime usage counter imbalance resulting from adding and removing (e.g. in the error code path) a stateless device link to it from within the consumer driver's probe callback.

Please refer to the patch changelogs for details.

Looks good, all now queued up, thanks.

greg k-h

#22 lore raw
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Rafael J. Wysocki

Hi Greg at al,

These fix two issues on top of the recent device links material in driver-core/driver-core-next.

The first one fixes a race condition that may trigger when __pm_runtime_set_status() is used incorrectly (that is, when it is called with PM-runtime enabled for the target device and working).

Quoted 8 lines
Quoted 6 lines

The second one fixes a supplier PM-runtime usage counter imbalance resulting from adding and removing (e.g. in the error code path) a stateless device link to it from within the consumer driver's probe callback.

Please refer to the patch changelogs for details.

Looks good, all now queued up, thanks.

Greg, please don't get me wrong, but ~1.5 hours isn't sufficient for me to review/test submitted patches.

I have been trying to collaborate (review/test) device links related code with Rafael, but what's the point if you queue up the patches, before I even got the change to look at them. Shall I interpret it as you don't care about me reviewing this, then just tell me so I don't have to waste my time.

Kind regards
Uffe

#23 lore raw
Quoted 23 lines · Ulf Hansson
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Rafael J. Wysocki

Hi Greg at al,

These fix two issues on top of the recent device links material in driver-core/driver-core-next.

The first one fixes a race condition that may trigger when __pm_runtime_set_status() is used incorrectly (that is, when it is called with PM-runtime enabled for the target device and working).

Quoted 8 lines
Quoted 6 lines

The second one fixes a supplier PM-runtime usage counter imbalance resulting from adding and removing (e.g. in the error code path) a stateless device link to it from within the consumer driver's probe callback.

Please refer to the patch changelogs for details.

Looks good, all now queued up, thanks.

Quoted 8 lines

Greg, please don't get me wrong, but ~1.5 hours isn't sufficient for me to review/test submitted patches.

I have been trying to collaborate (review/test) device links related code with Rafael, but what's the point if you queue up the patches, before I even got the change to look at them. Shall I interpret it as you don't care about me reviewing this, then just tell me so I don't have to waste my time.

I certainly do care about that.

Moreover, if you find any issues in the patches, they still can be dropped or incremental fixes on top of them can be done.

Your work and feedback here is much appreciated, please don't drop the ball. :-)

#24 lore raw
In reply to Ulf Hansson
Quoted 22 lines · Ulf Hansson
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Rafael J. Wysocki

Hi Greg at al,

These fix two issues on top of the recent device links material in driver-core/driver-core-next.

The first one fixes a race condition that may trigger when __pm_runtime_set_status() is used incorrectly (that is, when it is called with PM-runtime enabled for the target device and working).

Quoted 8 lines
Quoted 6 lines

The second one fixes a supplier PM-runtime usage counter imbalance resulting from adding and removing (e.g. in the error code path) a stateless device link to it from within the consumer driver's probe callback.

Please refer to the patch changelogs for details.

Looks good, all now queued up, thanks.

Quoted 8 lines

Greg, please don't get me wrong, but ~1.5 hours isn't sufficient for me to review/test submitted patches.

I have been trying to collaborate (review/test) device links related code with Rafael, but what's the point if you queue up the patches, before I even got the change to look at them. Shall I interpret it as you don't care about me reviewing this, then just tell me so I don't have to waste my time.

As they are just in my -testing branch, I can easily drop them now if you find problems. I didn't realize that Rafael was wanting you to review this as they were marked as "fixes:" for previous patches.

thanks,

greg k-h

#25 lore raw
Quoted 33 lines · Greg Kroah-Hartman
Quoted 22 lines · Ulf Hansson
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Rafael J. Wysocki

Hi Greg at al,

These fix two issues on top of the recent device links material in driver-core/driver-core-next.

The first one fixes a race condition that may trigger when __pm_runtime_set_status() is used incorrectly (that is, when it is called with PM-runtime enabled for the target device and working).

Quoted 8 lines
Quoted 6 lines

The second one fixes a supplier PM-runtime usage counter imbalance resulting from adding and removing (e.g. in the error code path) a stateless device link to it from within the consumer driver's probe callback.

Please refer to the patch changelogs for details.

Looks good, all now queued up, thanks.

Quoted 8 lines

Greg, please don't get me wrong, but ~1.5 hours isn't sufficient for me to review/test submitted patches.

I have been trying to collaborate (review/test) device links related code with Rafael, but what's the point if you queue up the patches, before I even got the change to look at them. Shall I interpret it as you don't care about me reviewing this, then just tell me so I don't have to waste my time.

Quoted 3 lines

As they are just in my -testing branch, I can easily drop them now if you find problems. I didn't realize that Rafael was wanting you to review this as they were marked as "fixes:" for previous patches.

Sorry for the confusion.

Quoted 1 line

From my perspective they fix issues on top of the previous commits as

indicated by the Fixes: tags and they work on my systems, but even though *I* think that they are all good, there still may be problems with them that I don't see. As usual. :-)

I guess I should have sent them as RFC this time.

Cheers,
Rafael