From: Takashi Iwai <tiwai@suse.de>
To: "Rafael J . Wysocki" <rafael@kernel.org>
Cc: linux-pm@vger.kernel.org, Bjorn Helgaas <bhelgaas@google.com>,
linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup
Date: Fri, 19 Sep 2025 18:31:42 +0200 [thread overview]
Message-ID: <20250919163147.4743-2-tiwai@suse.de> (raw)
In-Reply-To: <20250919163147.4743-1-tiwai@suse.de>
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
next prev parent reply other threads:[~2025-09-19 16:32 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2025-09-19 17:04 ` [PATCH 1/3] PM: runtime: Define class helpers for automatic PM runtime cleanup 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250919163147.4743-2-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=bhelgaas@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=rafael@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox