[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250919163147.4743-2-tiwai@suse.de>
Date: Fri, 19 Sep 2025 18:31:42 +0200
From: Takashi Iwai <tiwai@...e.de>
To: "Rafael J . Wysocki" <rafael@...nel.org>
Cc: linux-pm@...r.kernel.org,
Bjorn Helgaas <bhelgaas@...gle.com>,
linux-pci@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup
From: "Rafael J. Wysocki" <rafael@...nel.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@...e.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
Powered by blists - more mailing lists