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
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.
© 2016 - 2025 Red Hat, Inc.