* [PATCH v1] PM: runtime: Check refcount value in pm_runtime_release_supplier() @ 2026-12-04 16:05 Rafael J. Wysocki 2026-12-04 19:38 ` Brian Norris 0 siblings, 1 reply; 4+ messages in thread From: Rafael J. Wysocki @ 2026-12-04 16:05 UTC (permalink / raw) To: Linux PM; +Cc: Linux Driver Core Development, Brian Norris, Ulf Hansson From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com> If the device link's rpm_active refcount becomes saturated or underflows, the loop in pm_runtime_release_supplier() will continue until the supplier's usage_count goes down to 0, but that may allow the supplier to suspend when it is in use for reasons other than the consumer dependency. To avoid that, check the refcount value, expected to always be greater than 0 when read as an int, and break out of the loop immediately when it becomes invalid, instead of checking the supplier's usage counter. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> --- drivers/base/power/runtime.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -334,13 +334,13 @@ void pm_runtime_release_supplier(struct pm_runtime_mark_last_busy(supplier); /* - * The additional power.usage_count check is a safety net in case - * the rpm_active refcount becomes saturated, in which case - * refcount_dec_not_one() would return true forever, but it is not - * strictly necessary. + * The rpm_active refcount value check is a safety net in + * case it becomes saturated or underflows, which would cause + * refcount_dec_not_one() to return true forever. A valid + * rpm_active value cast to an int is always greater than 0. */ while (refcount_dec_not_one(&link->rpm_active) && - atomic_read(&supplier->power.usage_count) > 0) + (int)refcount_read(&link->rpm_active) > 0) pm_runtime_put_noidle(supplier); } ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] PM: runtime: Check refcount value in pm_runtime_release_supplier() 2026-12-04 16:05 [PATCH v1] PM: runtime: Check refcount value in pm_runtime_release_supplier() Rafael J. Wysocki @ 2026-12-04 19:38 ` Brian Norris 2026-12-04 20:19 ` Rafael J. Wysocki (Intel) 0 siblings, 1 reply; 4+ messages in thread From: Brian Norris @ 2026-12-04 19:38 UTC (permalink / raw) To: Rafael J. Wysocki; +Cc: Linux PM, Linux Driver Core Development, Ulf Hansson On Fri, Dec 04, 2026 at 05:05:44PM +0100, Rafael J. Wysocki wrote: > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com> > > If the device link's rpm_active refcount becomes saturated or > underflows, the loop in pm_runtime_release_supplier() will continue > until the supplier's usage_count goes down to 0, but that may allow > the supplier to suspend when it is in use for reasons other than the > consumer dependency. > > To avoid that, check the refcount value, expected to always be greater > than 0 when read as an int, and break out of the loop immediately when > it becomes invalid, instead of checking the supplier's usage counter. So, erring on the side of "hold reference forever", instead of premature drop? Seems OK, especially for something that should be extremely difficult to hit. Reviewed-by: Brian Norris <briannorris@chromium.org> BTW, I still think all this rpm_active logic is very strange. We shouldn't ever need to increase the rpm_active refcount larger than a small handful (approximately on the scale of how many DL_FLAG_RPM_ACTIVE links are created with the same consumer/supplier pair), but... ...we actually can dynamically increase rpm_active forever simply by calling pm_runtime_set_active() repeatedly. This feels less like a refcount and more like a bandaid over poor code structure. > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > --- > drivers/base/power/runtime.c | 10 +++++----- > 1 file changed, 5 insertions(+), 5 deletions(-) > > --- a/drivers/base/power/runtime.c > +++ b/drivers/base/power/runtime.c > @@ -334,13 +334,13 @@ void pm_runtime_release_supplier(struct > pm_runtime_mark_last_busy(supplier); > > /* > - * The additional power.usage_count check is a safety net in case > - * the rpm_active refcount becomes saturated, in which case > - * refcount_dec_not_one() would return true forever, but it is not > - * strictly necessary. > + * The rpm_active refcount value check is a safety net in > + * case it becomes saturated or underflows, which would cause > + * refcount_dec_not_one() to return true forever. A valid > + * rpm_active value cast to an int is always greater than 0. > */ > while (refcount_dec_not_one(&link->rpm_active) && > - atomic_read(&supplier->power.usage_count) > 0) > + (int)refcount_read(&link->rpm_active) > 0) > pm_runtime_put_noidle(supplier); > } > > > > ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] PM: runtime: Check refcount value in pm_runtime_release_supplier() 2026-12-04 19:38 ` Brian Norris @ 2026-12-04 20:19 ` Rafael J. Wysocki (Intel) 2026-12-04 20:51 ` Brian Norris 0 siblings, 1 reply; 4+ messages in thread From: Rafael J. Wysocki (Intel) @ 2026-12-04 20:19 UTC (permalink / raw) To: Brian Norris Cc: Rafael J. Wysocki, Linux PM, Linux Driver Core Development, Ulf Hansson On Fri, Dec 4, 2026 at 8:38 PM Brian Norris <briannorris@chromium.org> wrote: > > On Fri, Dec 04, 2026 at 05:05:44PM +0100, Rafael J. Wysocki wrote: > > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com> > > > > If the device link's rpm_active refcount becomes saturated or > > underflows, the loop in pm_runtime_release_supplier() will continue > > until the supplier's usage_count goes down to 0, but that may allow > > the supplier to suspend when it is in use for reasons other than the > > consumer dependency. > > > > To avoid that, check the refcount value, expected to always be greater > > than 0 when read as an int, and break out of the loop immediately when > > it becomes invalid, instead of checking the supplier's usage counter. > > So, erring on the side of "hold reference forever", instead of premature > drop? Seems OK, especially for something that should be extremely > difficult to hit. > > Reviewed-by: Brian Norris <briannorris@chromium.org> Thanks! > BTW, I still think all this rpm_active logic is very strange. We > shouldn't ever need to increase the rpm_active refcount larger than a > small handful (approximately on the scale of how many DL_FLAG_RPM_ACTIVE > links are created with the same consumer/supplier pair), but... True (I actually had to recall what the original problem addressed by it was). > ...we actually can dynamically increase rpm_active forever simply by > calling pm_runtime_set_active() repeatedly. If anyone does that, they will get what they deserve. > This feels less like a refcount and more like a bandaid over poor code > structure. So in theory, __pm_runtime_set_status() could somehow avoid calling rpm_get_suppliers() if the current status of the device is RPM_ACTIVE, but I had not found a good way to ensure that it would not be racy, so I gave up. OTOH, I'm now thinking that it might be better to combine __pm_runtime_set_status() with pm_runtime_enable(), so there would be two "stateful" variants of the latter, pm_runtime_enable_active() and pm_runtime_enable_suspended(), but then there needs to be a separate way to clear runtime_error (if set) and system suspend/resume enable and disable runtime PM. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] PM: runtime: Check refcount value in pm_runtime_release_supplier() 2026-12-04 20:19 ` Rafael J. Wysocki (Intel) @ 2026-12-04 20:51 ` Brian Norris 0 siblings, 0 replies; 4+ messages in thread From: Brian Norris @ 2026-12-04 20:51 UTC (permalink / raw) To: Rafael J. Wysocki (Intel) Cc: Linux PM, Linux Driver Core Development, Ulf Hansson On Fri, Dec 04, 2026 at 09:19:25PM +0100, Rafael J. Wysocki (Intel) wrote: > On Fri, Dec 4, 2026 at 8:38 PM Brian Norris <briannorris@chromium.org> wrote: > > > > On Fri, Dec 04, 2026 at 05:05:44PM +0100, Rafael J. Wysocki wrote: > > > From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com> > > > > > > If the device link's rpm_active refcount becomes saturated or > > > underflows, the loop in pm_runtime_release_supplier() will continue > > > until the supplier's usage_count goes down to 0, but that may allow > > > the supplier to suspend when it is in use for reasons other than the > > > consumer dependency. > > > > > > To avoid that, check the refcount value, expected to always be greater > > > than 0 when read as an int, and break out of the loop immediately when > > > it becomes invalid, instead of checking the supplier's usage counter. > > > > So, erring on the side of "hold reference forever", instead of premature > > drop? Seems OK, especially for something that should be extremely > > difficult to hit. > > > > Reviewed-by: Brian Norris <briannorris@chromium.org> > > Thanks! > > > BTW, I still think all this rpm_active logic is very strange. We > > shouldn't ever need to increase the rpm_active refcount larger than a > > small handful (approximately on the scale of how many DL_FLAG_RPM_ACTIVE > > links are created with the same consumer/supplier pair), but... > > True (I actually had to recall what the original problem addressed by it was). > > > ...we actually can dynamically increase rpm_active forever simply by > > calling pm_runtime_set_active() repeatedly. > > If anyone does that, they will get what they deserve. Haha, maybe. Although it doesn't seem all that impossible today; consider system suspend/resume for devices that never go through runtime suspend (e.g., .../power/control == on) in drivers like dwc3_pm_resume(). It forces a pm_runtime_set_active() on the resume path. Now, suspend your system INT_MAX times (lol) and... That pattern is even sort of in the docs: https://docs.kernel.org/power/runtime_pm.html#runtime-pm-and-system-sleep I think there may be some other similar paths in smart-suspend, which could potentially apply to lots of devices. > > This feels less like a refcount and more like a bandaid over poor code > > structure. > > So in theory, __pm_runtime_set_status() could somehow avoid calling > rpm_get_suppliers() if the current status of the device is RPM_ACTIVE, > but I had not found a good way to ensure that it would not be racy, so > I gave up. I didn't think too hard about that one, but I gotta believe it's possible. Maybe I'll spend time looking at it and come to the same conclusion :) > OTOH, I'm now thinking that it might be better to combine > __pm_runtime_set_status() with pm_runtime_enable(), so there would be > two "stateful" variants of the latter, pm_runtime_enable_active() and > pm_runtime_enable_suspended(), but then there needs to be a separate > way to clear runtime_error (if set) and system suspend/resume enable > and disable runtime PM. I'll admit, I'm not immediately seeing the value in this. Brian ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-04 20:51 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-04 16:05 [PATCH v1] PM: runtime: Check refcount value in pm_runtime_release_supplier() Rafael J. Wysocki 2026-12-04 19:38 ` Brian Norris 2026-12-04 20:19 ` Rafael J. Wysocki (Intel) 2026-12-04 20:51 ` Brian Norris
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox