Skip to contentSkip to conversation

PM: runtime: New class macros for auto-cleanup

PATCH In review Takashi Iwai 2 participants 9 messages Sep 19, 2025 – Sep 20, 2025

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

cover PM: runtime: New class macros for auto-cleanup

Hi,

this is a patch series to introduce the new class macros for easier usage of PM runtime auto-cleanup features.

There is only one existing user of __free(pm_runtime_put) in PCI core, and this is converted with CLASS() macro, too.
Then the pm_runtime_put __free definition is dropped.

The first patch was from Rafael (as found in the thread below), and I left no sign-off as I expect he'll get and sign later again.

thanks,

Takashi

===

Rafael J. Wysocki (1):
PM: runtime: Define class helpers for automatic PM runtime cleanup

Takashi Iwai (2):
PCI: Use PM runtime class macro for the auto cleanup
PM: runtime: Drop unused pm_runtime_free __free() definition

 drivers/pci/pci-sysfs.c    |  5 +++--
 include/linux/pm_runtime.h | 45 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 46 insertions(+), 4 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/3 PM: runtime: Define class helpers for automatic PM runtime cleanup

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

This patch adds two CLASS macros for the easier use of the auto-cleanup feature to manage the runtime PM get/put pairs. With the CLASS macro, pm_runtime_put() (or *_autosuspend) is called automatically at its scope exit, so you can remove the explicit pm_runtime_put() call.

Simply put, a code like below

	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_resume_and_get, pm)(dev);
	if (IS_ERR(pm))
		return PTR_ERR(pm);
	.....
	return 0;

(see pm_runtime_put() call is gone).

When the original code calls pm_runtime_put_autosuspend(), use CLASS(pm_runtime_resume_and_get_auto) variant, instead. e.g. a code like:

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

will be like:

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

Note that there is no CLASS macro defined for pm_runtime_get_sync(). With the auto-cleanup, we can unify both usages.
You can use CLASS(pm_runtime_resume_and_get) but simply skip the error check; so a code like below:

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

will become like:

	CLASS(pm_runtime_resume_and_get, pm)(dev);
	.....
	return 0;
---
 include/linux/pm_runtime.h | 43 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
include/linux/pm_runtime.h +43 -0
index d88d6b6ccf5b..637bfda9c2cd 100644
@@ -532,6 +532,30 @@ static inline int pm_runtime_resume_and_get(struct device *dev)
 	return 0;
 }
 
+/**
+ * pm_runtime_resume_and_get_dev - Resume device and bump up its usage counter.
+ * @dev: Target device.
+ *
+ * Resume @dev synchronously and if that is successful, increment its runtime
+ * PM usage counter.
+ *
+ * Return:
+ * * 0 if the runtime PM usage counter of @dev has been incremented.
+ * * Negative error code otherwise.
+ */
+static inline struct device *pm_runtime_resume_and_get_dev(struct device *dev)
+{
+	int ret;
+
+	ret = __pm_runtime_resume(dev, RPM_GET_PUT);
+	if (ret < 0) {
+		pm_runtime_put_noidle(dev);
+		return ERR_PTR(ret);
+	}
+
+	return dev;
+}
+
 /**
  * pm_runtime_put - Drop device usage counter and queue up "idle check" if 0.
  * @dev: Target device.
@@ -606,6 +630,25 @@ static inline int pm_runtime_put_autosuspend(struct device *dev)
 	return __pm_runtime_put_autosuspend(dev);
 }
 
+/*
+ * The way to use the classes defined below is to define a class variable and
+ * use it going forward for representing the target device until it goes out of
+ * the scope.  For example:
+ *
+ * CLASS(pm_runtime_resume_and_get, active_dev)(dev);
+ * if (IS_ERR(active_dev))
+ *         return PTR_ERR(active_dev);
+ *
+ * ... do something with active_dev (which is guaranteed to never suspend) ...
+ */
+DEFINE_CLASS(pm_runtime_resume_and_get, struct device *,
+	     if (!IS_ERR_OR_NULL(_T)) pm_runtime_put(_T),
+	     pm_runtime_resume_and_get_dev(dev), struct device *dev)
+
+DEFINE_CLASS(pm_runtime_resume_and_get_auto, struct device *,
+	     if (!IS_ERR_OR_NULL(_T)) pm_runtime_put_autosuspend(_T),
+	     pm_runtime_resume_and_get_dev(dev), struct device *dev)
+
 /**
  * pm_runtime_put_sync - Drop device usage counter and run "idle check" if 0.
  * @dev: Target device.
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup

Quoted 8 lines · Takashi Iwai

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

This patch adds two CLASS macros for the easier use of the auto-cleanup feature to manage the runtime PM get/put pairs. With the CLASS macro, pm_runtime_put() (or *_autosuspend) is called automatically at its scope exit, so you can remove the explicit pm_runtime_put() call.

The part of the changelog below is actually really nice.

Quoted 48 lines

Simply put, a code like below

        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_resume_and_get, pm)(dev);
        if (IS_ERR(pm))
                return PTR_ERR(pm);
        .....
        return 0;

(see pm_runtime_put() call is gone).

When the original code calls pm_runtime_put_autosuspend(), use CLASS(pm_runtime_resume_and_get_auto) variant, instead. e.g. a code like:

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

will be like:

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

Note that there is no CLASS macro defined for pm_runtime_get_sync(). With the auto-cleanup, we can unify both usages.
You can use CLASS(pm_runtime_resume_and_get) but simply skip the error check; so a code like below:

        pm_runtime_get_sync(dev);
        .....
        pm_runtime_put(dev);
        return 0;
Quoted 8 lines

will become like:

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

So I'm going to include it into the first patch and add a
Co-developed-by: tag for you to it.

I'll send a v2, but I'll wait some time in case someone else has any comments. Probably tomorrow.

#3 lore raw

PATCH 2/3 PCI: Use PM runtime class macro for the auto cleanup

In reply to Takashi Iwai

Use the newly introduced class macro to simplify the code. Also, add the proper error handling for the PM runtime get errors, too.

---
 drivers/pci/pci-sysfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/pci/pci-sysfs.c +3 -2
index 5eea14c1f7f5..08e5cf2ba73e 100644
@@ -1475,8 +1475,9 @@ static ssize_t reset_method_store(struct device *dev,
 		return count;
 	}
 
-	pm_runtime_get_sync(dev);
-	struct device *pmdev __free(pm_runtime_put) = dev;
+	CLASS(pm_runtime_resume_and_get, pmdev)(dev);
+	if (IS_ERR(pmdev))
+		return -ENXIO;
 
 	if (sysfs_streq(buf, "default")) {
 		pci_init_reset_methods(pdev);
Signature · 1 line
2.50.1
#4 lore raw

Re: [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup

Quoted 17 lines · Takashi Iwai

Use the newly introduced class macro to simplify the code. Also, add the proper error handling for the PM runtime get errors, too.

---
 drivers/pci/pci-sysfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/pci/pci-sysfs.c +0 -0
index 5eea14c1f7f5..08e5cf2ba73e 100644
@@ -1475,8 +1475,9 @@ static ssize_t reset_method_store(struct device *dev,
                return count;
        }

drivers/pci/pci-sysfs.c static ssize_t reset_method_store(struct device *dev,

Quoted 10 lines
drivers/pci/pci-sysfs.c +3 -3
 
-       pm_runtime_get_sync(dev);
-       struct device *pmdev __free(pm_runtime_put) = dev;
+       CLASS(pm_runtime_resume_and_get, pmdev)(dev);
+       if (IS_ERR(pmdev))
+               return -ENXIO;
 
        if (sysfs_streq(buf, "default")) {
                pci_init_reset_methods(pdev);
--

I've just realized that this patch won't work and the reason why is that __pm_runtime_resume() returns an error if runtime PM is disabled for a device, so pmdev above will be an error pointer in that case.

One possible approach may be to make pm_runtime_resume_and_get_dev() return 0 and bump up the usage counter for devices with runtime PM disabled.

#5 lore raw

Re: [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup

Quoted 26 lines · Rafael J. Wysocki
Quoted 24 lines · Takashi Iwai

Use the newly introduced class macro to simplify the code. Also, add the proper error handling for the PM runtime get errors, too.

---
 drivers/pci/pci-sysfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/pci/pci-sysfs.c +3 -2
index 5eea14c1f7f5..08e5cf2ba73e 100644
@@ -1475,8 +1475,9 @@ static ssize_t reset_method_store(struct device *dev,
                return count;
        }
 
-       pm_runtime_get_sync(dev);
-       struct device *pmdev __free(pm_runtime_put) = dev;
+       CLASS(pm_runtime_resume_and_get, pmdev)(dev);
+       if (IS_ERR(pmdev))
+               return -ENXIO;
 
Quoted 7 lines

drivers/pci/pci-sysfs.c static ssize_t reset_method_store(struct device *dev,

Quoted 3 lines
drivers/pci/pci-sysfs.c +0 -1
        if (sysfs_streq(buf, "default")) {
                pci_init_reset_methods(pdev);
--

I've just realized that this patch won't work and the reason why is that __pm_runtime_resume() returns an error if runtime PM is disabled for a device, so pmdev above will be an error pointer in that case.

Good catch, this was completely forgotten...

Quoted 3 lines

One possible approach may be to make pm_runtime_resume_and_get_dev() return 0 and bump up the usage counter for devices with runtime PM disabled.

Sounds reasonable.

thanks,

Takashi

#6 lore raw

PATCH 3/3 PM: runtime: Drop unused pm_runtime_free __free() definition

In reply to Takashi Iwai

Since the introduction of CLASS macros for pm_runtime_get/put and the conversion with it, there is no user of __free(pm_runtime_put) any longer. Let's clean it up.

---
 include/linux/pm_runtime.h | 2 --
 1 file changed, 2 deletions(-)
include/linux/pm_runtime.h +0 -2
index 637bfda9c2cd..29d746f57f98 100644
@@ -579,8 +579,6 @@ static inline int pm_runtime_put(struct device *dev)
 	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.
Signature · 1 line
2.50.1
#7 lore raw
In reply to Takashi Iwai

Hi,

Quoted 10 lines · Takashi Iwai

Hi,

this is a patch series to introduce the new class macros for easier usage of PM runtime auto-cleanup features.

There is only one existing user of __free(pm_runtime_put) in PCI core, and this is converted with CLASS() macro, too.
Then the pm_runtime_put __free definition is dropped.

Quoted 5 lines

The first patch was from Rafael (as found in the thread below), and I left no sign-off as I expect he'll get and sign later again.

I've just done the same thing:

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

which I said I would do:

https://lore.kernel.org/linux-pm/CAJZ5v0jJjYoTceD2_pgvKgKuPypo+8osnAuCefgAjrzY_w2n8A@mail.gmail.com/

:-)

Sorry for the confusion.

Any issues with using my version?

#8 lore raw
Quoted 26 lines · Rafael J. Wysocki

Hi,

Quoted 10 lines · Takashi Iwai

Hi,

this is a patch series to introduce the new class macros for easier usage of PM runtime auto-cleanup features.

There is only one existing user of __free(pm_runtime_put) in PCI core, and this is converted with CLASS() macro, too.
Then the pm_runtime_put __free definition is dropped.

Quoted 5 lines

The first patch was from Rafael (as found in the thread below), and I left no sign-off as I expect he'll get and sign later again.

I've just done the same thing:

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

which I said I would do:

Quoted 7 lines

https://lore.kernel.org/linux-pm/CAJZ5v0jJjYoTceD2_pgvKgKuPypo+8osnAuCefgAjrzY_w2n8A@mail.gmail.com/

:-)

Sorry for the confusion.

Any issues with using my version?

It was my misunderstanding as I read in hurry ;)
The patches look good, thanks!

Takashi