Skip to contentSkip to conversation

PM: runtime: Auto-cleanup macros for runtime PM

PATCH Superseded Rafael J. Wysocki 2 participants 10 messages Sep 20, 2025 – Sep 22, 2025

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

v2 cover PM: runtime: Auto-cleanup macros for runtime PM

Hi All,

This supersedes both

https://lore.kernel.org/linux-pm/[email protected]/

and

https://lore.kernel.org/linux-pm/[email protected]/

that were sent simultaneously by mistake and both made a mistake of forgetting that __pm_runtime_suspend() returns an error code for devices with runtime PM disabled.

The first patch in this series has been modified to provide additional two macros for the cases in which runtime PM is expected to work transparently if runtime PM has been disabled for the given device. Also the names of the new classes defined in it and the new helper static inline function have been changed.

The second patch has been updated to reflect the changes in the first patch.

The last patch is basically the same as before.

Thanks!

author maintainer
#1 lore raw

PATCH v2 1/3 PM: runtime: Add auto-cleanup macros for "resume and get" operations

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

It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1].

To that end, add DEFINE_CLASS() macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases.

Simply put, a piece of code like below:

	pm_runtime_get_sync(dev);
	.....
	pm_runtime_put(dev);
	return 0;

can be transformed with CLASS() like:

	CLASS(pm_runtime_get_active, pm)(dev);
	if (IS_ERR(pm))
		return PTR_ERR(pm);
	.....
	return 0;

(note the new resume error handling).

In all of the cases in which runtime PM is known to be enabled for the given device or it can be regarded as operational (and so it can be accessed) with runtime PM disabled, a piece of code like:

	ret = pm_runtime_resume_and_get(dev);
	if (ret < 0)
		return ret;
	.....
	pm_runtime_put(dev);
	return 0;

can be simplified with CLASS() like:

	CLASS(pm_runtime_get_active, pm)(dev);
	if (IS_ERR(pm))
		return PTR_ERR(pm);
	.....
	return 0;

(see pm_runtime_put() call is gone).

However, if the device cannot be accessed unless runtime PM has been enabled for it, the CLASS(pm_runtime_get_active_enabled) variant needs to be used, that is (in the context of the example above):

	CLASS(pm_runtime_get_active_enabled, pm)(dev);
	if (IS_ERR(pm))
		return PTR_ERR(pm);
	.....
	return 0;

When the original code calls pm_runtime_put_autosuspend(), use CLASS(pm_runtime_get_active_enabled_auto) variant, instead. For example, a piece of code like:

	ret = pm_runtime_resume_and_get(dev);
	if (ret < 0)
		return ret;
	.....
	pm_runtime_put_autosuspend(dev);
	return 0;

will become:

	CLASS(pm_runtime_get_active_enabled_auto, pm)(dev);
	if (IS_ERR(pm))
		return PTR_ERR(pm);
	.....
	return 0;

Note that the cases in which the return value of pm_runtime_get_sync() is checked can also be handled with the help of the new class macros. For example, a piece of code like:

	ret = pm_runtime_get_sync(dev);
	if (ret < 0) {
		pm_runtime_put(dev);
		return ret;
	}
	.....
	pm_runtime_put(dev);
	return 0;

can be rewritten as:

	CLASS(pm_runtime_get_active_enabled, pm)(dev);
	if (IS_ERR(pm))
		return PTR_ERR(pm);
	.....
	return 0;

---

v1 -> v2:
* Rename the new classes and the new static inline helper.
* Add two classes for handling disabled runtime PM.
* Expand the changelog.
* Adjust the subject.

---
 drivers/base/power/runtime.c |    2 +
 include/linux/pm_runtime.h   |   70 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
drivers/base/power/runtime.c +2 -0
@@ -796,6 +796,8 @@ static int rpm_resume(struct device *dev
 		if (dev->power.runtime_status == RPM_ACTIVE &&
 		    dev->power.last_status == RPM_ACTIVE)
 			retval = 1;
+		else if (rpmflags & RPM_TRANSPARENT)
+			goto out;
 		else
 			retval = -EACCES;
 	}
include/linux/pm_runtime.h +70 -0

88 lines not on this page · raw message

#2 lore raw

Re: [PATCH v2 1/3] PM: runtime: Add auto-cleanup macros for "resume and get" operations

Quoted 20 lines · Rafael J. Wysocki

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

It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1].

To that end, add DEFINE_CLASS() macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases.

Simply put, a piece of code like below:

	pm_runtime_get_sync(dev);
	.....
	pm_runtime_put(dev);
	return 0;

can be transformed with CLASS() like:

Quoted 7 lines
	CLASS(pm_runtime_get_active, pm)(dev);
	if (IS_ERR(pm))
		return PTR_ERR(pm);
	.....
	return 0;

(note the new resume error handling).

Do we still allow the code without the error check even using CLASS()? Although the error check should be handled, it's not mandatory for now. That said, the above example could be still in a form like:

	CLASS(pm_runtime_get_active, pm)(dev);
	.....
	return 0;

while adding the proper error check is recommended?

thanks,

Takashi

author maintainer
#3 lore raw

Re: [PATCH v2 1/3] PM: runtime: Add auto-cleanup macros for "resume and get" operations

Quoted 35 lines · Takashi Iwai
Quoted 20 lines · Rafael J. Wysocki

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

It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1].

To that end, add DEFINE_CLASS() macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases.

Simply put, a piece of code like below:

      pm_runtime_get_sync(dev);
      .....
      pm_runtime_put(dev);
      return 0;

can be transformed with CLASS() like:

Quoted 7 lines
      CLASS(pm_runtime_get_active, pm)(dev);
      if (IS_ERR(pm))
              return PTR_ERR(pm);
      .....
      return 0;

(note the new resume error handling).

Do we still allow the code without the error check even using CLASS()? Although the error check should be handled, it's not mandatory for now. That said, the above example could be still in a form like:

Quoted 5 lines
        CLASS(pm_runtime_get_active, pm)(dev);
        .....
        return 0;

while adding the proper error check is recommended?

I'd rather not encourage doing this.

While it may still produce working code in some cases, one needs to remember that in case of a runtime resume error it will be running without a runtime PM reference it has attempted to acquire.

#4 lore raw

Re: [PATCH v2 1/3] PM: runtime: Add auto-cleanup macros for "resume and get" operations

Quoted 41 lines · Rafael J. Wysocki
Quoted 39 lines · Takashi Iwai
Quoted 20 lines · Rafael J. Wysocki

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

It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1].

To that end, add DEFINE_CLASS() macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases.

Simply put, a piece of code like below:

      pm_runtime_get_sync(dev);
      .....
      pm_runtime_put(dev);
      return 0;

can be transformed with CLASS() like:

Quoted 7 lines
      CLASS(pm_runtime_get_active, pm)(dev);
      if (IS_ERR(pm))
              return PTR_ERR(pm);
      .....
      return 0;

(note the new resume error handling).

Do we still allow the code without the error check even using CLASS()? Although the error check should be handled, it's not mandatory for now. That said, the above example could be still in a form like:

        CLASS(pm_runtime_get_active, pm)(dev);
        .....
        return 0;
Quoted 7 lines
Quoted 1 line

while adding the proper error check is recommended?

I'd rather not encourage doing this.

While it may still produce working code in some cases, one needs to remember that in case of a runtime resume error it will be running without a runtime PM reference it has attempted to acquire.

Fair enough. Then it'd be also good to mention that in the description, too.

thanks,

Takashi

author maintainer
#5 lore raw

Re: [PATCH v2 1/3] PM: runtime: Add auto-cleanup macros for "resume and get" operations

Quoted 46 lines · Takashi Iwai
Quoted 43 lines · Rafael J. Wysocki
Quoted 40 lines · Takashi Iwai
Quoted 20 lines · Rafael J. Wysocki

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

It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1].

To that end, add DEFINE_CLASS() macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases.

Simply put, a piece of code like below:

      pm_runtime_get_sync(dev);
      .....
      pm_runtime_put(dev);
      return 0;

can be transformed with CLASS() like:

Quoted 7 lines
      CLASS(pm_runtime_get_active, pm)(dev);
      if (IS_ERR(pm))
              return PTR_ERR(pm);
      .....
      return 0;

(note the new resume error handling).

Do we still allow the code without the error check even using CLASS()? Although the error check should be handled, it's not mandatory for now. That said, the above example could be still in a form like:

        CLASS(pm_runtime_get_active, pm)(dev);
        .....
        return 0;

while adding the proper error check is recommended?

Quoted 8 lines
Quoted 5 lines

I'd rather not encourage doing this.

While it may still produce working code in some cases, one needs to remember that in case of a runtime resume error it will be running without a runtime PM reference it has attempted to acquire.

Fair enough. Then it'd be also good to mention that in the description, too.

I can also add classes for the cases in which resume errors can be neglected, like these:

DEFINE_CLASS(pm_runtime_get_sync, struct device *,
if (_T) pm_runtime_put(_T),
({ pm_runtime_get_sync(dev); dev; }), struct device *dev)

DEFINE_CLASS(pm_runtime_get_sync_auto, struct device *,
if (_T) pm_runtime_put_autosuspend(_T),
({ pm_runtime_get_sync(dev); dev; }), struct device *dev)

with a comment explaining what they are for.

#6 lore raw

Re: [PATCH v2 1/3] PM: runtime: Add auto-cleanup macros for "resume and get" operations

Quoted 64 lines · Rafael J. Wysocki
Quoted 46 lines · Takashi Iwai
Quoted 43 lines · Rafael J. Wysocki
Quoted 40 lines · Takashi Iwai
Quoted 20 lines · Rafael J. Wysocki

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

It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1].

To that end, add DEFINE_CLASS() macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases.

Simply put, a piece of code like below:

      pm_runtime_get_sync(dev);
      .....
      pm_runtime_put(dev);
      return 0;

can be transformed with CLASS() like:

Quoted 7 lines
      CLASS(pm_runtime_get_active, pm)(dev);
      if (IS_ERR(pm))
              return PTR_ERR(pm);
      .....
      return 0;

(note the new resume error handling).

Do we still allow the code without the error check even using CLASS()? Although the error check should be handled, it's not mandatory for now. That said, the above example could be still in a form like:

        CLASS(pm_runtime_get_active, pm)(dev);
        .....
        return 0;

while adding the proper error check is recommended?

Quoted 8 lines
Quoted 5 lines

I'd rather not encourage doing this.

While it may still produce working code in some cases, one needs to remember that in case of a runtime resume error it will be running without a runtime PM reference it has attempted to acquire.

Fair enough. Then it'd be also good to mention that in the description, too.

I can also add classes for the cases in which resume errors can be neglected, like these:

DEFINE_CLASS(pm_runtime_get_sync, struct device *,
if (_T) pm_runtime_put(_T),
({ pm_runtime_get_sync(dev); dev; }), struct device *dev)

Quoted 5 lines

DEFINE_CLASS(pm_runtime_get_sync_auto, struct device *,
if (_T) pm_runtime_put_autosuspend(_T),
({ pm_runtime_get_sync(dev); dev; }), struct device *dev)

with a comment explaining what they are for.

It might be helpful, indeed, since the error handling isn't always straightforward, and this still allows us to convert to the auto-cleanup safely. It's still worth to mention that those aren't recommended options, though.

thanks,

Takashi

author maintainer
#7 lore raw

Re: [PATCH v2 1/3] PM: runtime: Add auto-cleanup macros for "resume and get" operations

Quoted 71 lines · Takashi Iwai
Quoted 68 lines · Rafael J. Wysocki
Quoted 46 lines · Takashi Iwai
Quoted 43 lines · Rafael J. Wysocki
Quoted 40 lines · Takashi Iwai
Quoted 20 lines · Rafael J. Wysocki

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

It is generally useful to be able to automatically drop a device's runtime PM usage counter incremented by runtime PM operations that resume a device and bump up its usage counter [1].

To that end, add DEFINE_CLASS() macros allowing pm_runtime_put() and pm_runtime_put_autosuspend() to be used for the auto-cleanup in those cases.

Simply put, a piece of code like below:

      pm_runtime_get_sync(dev);
      .....
      pm_runtime_put(dev);
      return 0;

can be transformed with CLASS() like:

Quoted 7 lines
      CLASS(pm_runtime_get_active, pm)(dev);
      if (IS_ERR(pm))
              return PTR_ERR(pm);
      .....
      return 0;

(note the new resume error handling).

Do we still allow the code without the error check even using CLASS()? Although the error check should be handled, it's not mandatory for now. That said, the above example could be still in a form like:

        CLASS(pm_runtime_get_active, pm)(dev);
        .....
        return 0;

while adding the proper error check is recommended?

Quoted 8 lines
Quoted 5 lines

I'd rather not encourage doing this.

While it may still produce working code in some cases, one needs to remember that in case of a runtime resume error it will be running without a runtime PM reference it has attempted to acquire.

Fair enough. Then it'd be also good to mention that in the description, too.

I can also add classes for the cases in which resume errors can be neglected, like these:

DEFINE_CLASS(pm_runtime_get_sync, struct device *,
if (_T) pm_runtime_put(_T),
({ pm_runtime_get_sync(dev); dev; }), struct device *dev)

DEFINE_CLASS(pm_runtime_get_sync_auto, struct device *,
if (_T) pm_runtime_put_autosuspend(_T),
({ pm_runtime_get_sync(dev); dev; }), struct device *dev)

Quoted 6 lines
Quoted 1 line

with a comment explaining what they are for.

It might be helpful, indeed, since the error handling isn't always straightforward, and this still allows us to convert to the auto-cleanup safely. It's still worth to mention that those aren't recommended options, though.

Agreed.

I'll send a v3 including these changes.

author maintainer
#8 lore raw

PATCH v2 2/3 PCI/sysfs: Use runtime PM class macro for auto-cleanup

In reply to Rafael J. Wysocki

From: Takashi Iwai <[email protected]>

Use the newly introduced class macro to simplify the code.

Also, add the proper error handling for the PM runtime get errors.

[ rjw: Adjusted the subject and the name of the class ]

---

v1 -> v2:
* Adjust the name of the class to handle the disabled runtime PM case transparently (like the original code).

---
 drivers/pci/pci-sysfs.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/pci/pci-sysfs.c +3 -2
@@ -1475,8 +1475,9 @@ static ssize_t reset_method_store(struct
 		return count;
 	}
 
-	pm_runtime_get_sync(dev);
-	struct device *pmdev __free(pm_runtime_put) = dev;
+	CLASS(pm_runtime_get_active, pmdev)(dev);
+	if (IS_ERR(pmdev))
+		return -ENXIO;
 
 	if (sysfs_streq(buf, "default")) {
 		pci_init_reset_methods(pdev);
author maintainer
#9 lore raw

PATCH v2 3/3 PM: runtime: Drop DEFINE_FREE() for pm_runtime_put

In reply to Rafael J. Wysocki

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

The DEFINE_FREE() for pm_runtime_put has been superseded by recently introduced runtime PM auto-cleanup macros and its only user has been converted to using one of the new macros, so drop it.

---

v1 -> v2: No changes.

---
 include/linux/pm_runtime.h |    2 --
 1 file changed, 2 deletions(-)
include/linux/pm_runtime.h +0 -2
@@ -582,8 +582,6 @@ static inline int pm_runtime_put(struct
 	return __pm_runtime_idle(dev, RPM_GET_PUT | RPM_ASYNC);
 }
 
-DEFINE_FREE(pm_runtime_put, struct device *, if (_T) pm_runtime_put(_T))
-
 /**
  * __pm_runtime_put_autosuspend - Drop device usage counter and queue autosuspend if 0.
  * @dev: Target device.