public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] PM: runtime: New class macros for auto-cleanup
@ 2025-09-19 16:31 Takashi Iwai
  2025-09-19 16:31 ` [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup Takashi Iwai
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Takashi Iwai @ 2025-09-19 16:31 UTC (permalink / raw)
  To: Rafael J . Wysocki; +Cc: linux-pm, Bjorn Helgaas, linux-pci, linux-kernel

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.


Link: https://lore.kernel.org/878qimv24u.wl-tiwai@suse.de

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(-)

-- 
2.50.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup
  2025-09-19 16:31 [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Takashi Iwai
@ 2025-09-19 16:31 ` Takashi Iwai
  2025-09-19 17:04   ` Rafael J. Wysocki
  2025-09-19 16:31 ` [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup Takashi Iwai
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Takashi Iwai @ 2025-09-19 16:31 UTC (permalink / raw)
  To: Rafael J . Wysocki; +Cc: linux-pm, Bjorn Helgaas, linux-pci, linux-kernel

From: "Rafael J. Wysocki" <rafael@kernel.org>

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;

Link: https://lore.kernel.org/878qimv24u.wl-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 include/linux/pm_runtime.h | 43 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/include/linux/pm_runtime.h b/include/linux/pm_runtime.h
index d88d6b6ccf5b..637bfda9c2cd 100644
--- a/include/linux/pm_runtime.h
+++ b/include/linux/pm_runtime.h
@@ -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.
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup
  2025-09-19 16:31 [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Takashi Iwai
  2025-09-19 16:31 ` [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup Takashi Iwai
@ 2025-09-19 16:31 ` Takashi Iwai
  2025-09-19 18:39   ` Rafael J. Wysocki
  2025-09-19 16:31 ` [PATCH 3/3] PM: runtime: Drop unused pm_runtime_free __free() definition Takashi Iwai
  2025-09-19 16:44 ` [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Rafael J. Wysocki
  3 siblings, 1 reply; 9+ messages in thread
From: Takashi Iwai @ 2025-09-19 16:31 UTC (permalink / raw)
  To: Rafael J . Wysocki; +Cc: linux-pm, Bjorn Helgaas, linux-pci, linux-kernel

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

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 drivers/pci/pci-sysfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 5eea14c1f7f5..08e5cf2ba73e 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -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);
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/3] PM: runtime: Drop unused pm_runtime_free __free() definition
  2025-09-19 16:31 [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Takashi Iwai
  2025-09-19 16:31 ` [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup Takashi Iwai
  2025-09-19 16:31 ` [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup Takashi Iwai
@ 2025-09-19 16:31 ` Takashi Iwai
  2025-09-19 16:44 ` [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Rafael J. Wysocki
  3 siblings, 0 replies; 9+ messages in thread
From: Takashi Iwai @ 2025-09-19 16:31 UTC (permalink / raw)
  To: Rafael J . Wysocki; +Cc: linux-pm, Bjorn Helgaas, linux-pci, linux-kernel

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.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 include/linux/pm_runtime.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/include/linux/pm_runtime.h b/include/linux/pm_runtime.h
index 637bfda9c2cd..29d746f57f98 100644
--- a/include/linux/pm_runtime.h
+++ b/include/linux/pm_runtime.h
@@ -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.
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/3] PM: runtime: New class macros for auto-cleanup
  2025-09-19 16:31 [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Takashi Iwai
                   ` (2 preceding siblings ...)
  2025-09-19 16:31 ` [PATCH 3/3] PM: runtime: Drop unused pm_runtime_free __free() definition Takashi Iwai
@ 2025-09-19 16:44 ` Rafael J. Wysocki
  2025-09-20  6:36   ` Takashi Iwai
  3 siblings, 1 reply; 9+ messages in thread
From: Rafael J. Wysocki @ 2025-09-19 16:44 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Rafael J . Wysocki, linux-pm, Bjorn Helgaas, linux-pci,
	linux-kernel

Hi,

On Fri, Sep 19, 2025 at 6:32 PM Takashi Iwai <tiwai@suse.de> wrote:
>
> 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.
>
>
> Link: https://lore.kernel.org/878qimv24u.wl-tiwai@suse.de

I've just done the same thing:

https://lore.kernel.org/linux-pm/5049058.31r3eYUQgx@rafael.j.wysocki/

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?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup
  2025-09-19 16:31 ` [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup Takashi Iwai
@ 2025-09-19 17:04   ` Rafael J. Wysocki
  0 siblings, 0 replies; 9+ messages in thread
From: Rafael J. Wysocki @ 2025-09-19 17:04 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Rafael J . Wysocki, linux-pm, Bjorn Helgaas, linux-pci,
	linux-kernel

On Fri, Sep 19, 2025 at 6:32 PM Takashi Iwai <tiwai@suse.de> wrote:
>
> From: "Rafael J. Wysocki" <rafael@kernel.org>
>
> 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.

> 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;
>
> Link: https://lore.kernel.org/878qimv24u.wl-tiwai@suse.de
> Signed-off-by: Takashi Iwai <tiwai@suse.de>

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup
  2025-09-19 16:31 ` [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup Takashi Iwai
@ 2025-09-19 18:39   ` Rafael J. Wysocki
  2025-09-20  6:33     ` Takashi Iwai
  0 siblings, 1 reply; 9+ messages in thread
From: Rafael J. Wysocki @ 2025-09-19 18:39 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Rafael J . Wysocki, linux-pm, Bjorn Helgaas, linux-pci,
	linux-kernel

On Fri, Sep 19, 2025 at 6:32 PM Takashi Iwai <tiwai@suse.de> wrote:
>
> Use the newly introduced class macro to simplify the code.
> Also, add the proper error handling for the PM runtime get errors,
> too.
>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
>  drivers/pci/pci-sysfs.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index 5eea14c1f7f5..08e5cf2ba73e 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -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);
> --

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup
  2025-09-19 18:39   ` Rafael J. Wysocki
@ 2025-09-20  6:33     ` Takashi Iwai
  0 siblings, 0 replies; 9+ messages in thread
From: Takashi Iwai @ 2025-09-20  6:33 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Takashi Iwai, linux-pm, Bjorn Helgaas, linux-pci, linux-kernel

On Fri, 19 Sep 2025 20:39:04 +0200,
Rafael J. Wysocki wrote:
> 
> On Fri, Sep 19, 2025 at 6:32 PM Takashi Iwai <tiwai@suse.de> wrote:
> >
> > Use the newly introduced class macro to simplify the code.
> > Also, add the proper error handling for the PM runtime get errors,
> > too.
> >
> > Signed-off-by: Takashi Iwai <tiwai@suse.de>
> > ---
> >  drivers/pci/pci-sysfs.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> > index 5eea14c1f7f5..08e5cf2ba73e 100644
> > --- a/drivers/pci/pci-sysfs.c
> > +++ b/drivers/pci/pci-sysfs.c
> > @@ -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);
> > --
> 
> 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...

> 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/3] PM: runtime: New class macros for auto-cleanup
  2025-09-19 16:44 ` [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Rafael J. Wysocki
@ 2025-09-20  6:36   ` Takashi Iwai
  0 siblings, 0 replies; 9+ messages in thread
From: Takashi Iwai @ 2025-09-20  6:36 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Takashi Iwai, linux-pm, Bjorn Helgaas, linux-pci, linux-kernel

On Fri, 19 Sep 2025 18:44:12 +0200,
Rafael J. Wysocki wrote:
> 
> Hi,
> 
> On Fri, Sep 19, 2025 at 6:32 PM Takashi Iwai <tiwai@suse.de> wrote:
> >
> > 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.
> >
> >
> > Link: https://lore.kernel.org/878qimv24u.wl-tiwai@suse.de
> 
> I've just done the same thing:
> 
> https://lore.kernel.org/linux-pm/5049058.31r3eYUQgx@rafael.j.wysocki/
> 
> 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?

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


Takashi

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-09-20  6:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-19 16:31 [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Takashi Iwai
2025-09-19 16:31 ` [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup Takashi Iwai
2025-09-19 17:04   ` Rafael J. Wysocki
2025-09-19 16:31 ` [PATCH 2/3] PCI: Use PM runtime class macro for the auto cleanup Takashi Iwai
2025-09-19 18:39   ` Rafael J. Wysocki
2025-09-20  6:33     ` Takashi Iwai
2025-09-19 16:31 ` [PATCH 3/3] PM: runtime: Drop unused pm_runtime_free __free() definition Takashi Iwai
2025-09-19 16:44 ` [PATCH 0/3] PM: runtime: New class macros for auto-cleanup Rafael J. Wysocki
2025-09-20  6:36   ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox