From: Takashi Iwai <tiwai@suse.de>
To: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Takashi Iwai <tiwai@suse.de>,
linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: PM runtime auto-cleanup macros
Date: Thu, 18 Sep 2025 09:10:41 +0200 [thread overview]
Message-ID: <87tt10b5hq.wl-tiwai@suse.de> (raw)
In-Reply-To: <CAJZ5v0htMKOcCoKts-B9BaE0VpS2oc9-cp=5VnNwS2Qe2iB+Kg@mail.gmail.com>
On Wed, 17 Sep 2025 20:58:36 +0200,
Rafael J. Wysocki wrote:
>
> Hi,
>
> Sorry for the delay.
>
> On Thu, Sep 11, 2025 at 9:31 AM Takashi Iwai <tiwai@suse.de> wrote:
> >
> > On Wed, 10 Sep 2025 16:00:17 +0200,
> > Takashi Iwai wrote:
> > >
> > > Hi,
> > >
> > > while I worked on the code cleanups in the drivers with the recent
> > > auto-cleanup macros, I noticed that pm_runtime_get*() and _put*() can
> > > be also managed with the auto-cleanup gracefully, too. Actually we
> > > already defined the __free(pm_runtime_put) in commit bfa4477751e9, and
> > > there is a (single) user of it in pci-sysfs.c.
> > >
> > > Now I wanted to extend it to pm_runtime_put_autosuspend() as:
> > >
> > > DEFINE_FREE(pm_runtime_put_autosuspend, struct device *,
> > > if (_T) pm_runtime_put_autosuspend(_T))
> > >
> > > Then one can use it like
> > >
> > > ret = pm_runtime_resume_and_get(dev);
> > > if (ret < 0)
> > > return ret;
> > > struct device *pmdev __free(pm_runtime_put_autosuspend) = dev;
> > >
> > > that is similar as done in pci-sysfs.c. So far, so good.
> > >
> > > But, I find putting the line like above at each place a bit ugly.
> > > So I'm wondering whether it'd be better to introduce some helper
> > > macros, e.g.
> > >
> > > #define pm_runtime_auto_clean(dev, var) \
> > > struct device *var __free(pm_runtime_put) = (dev)
> >
> > It can be even simpler by assigning a temporary variable such as:
> >
> > #define pm_runtime_auto_clean(dev) \
> > struct device *__pm_runtime_var ## __LINE__ __free(pm_runtime_put) = (dev)
>
> Well, if there's something like
>
> struct device *pm_runtime_resume_and_get_dev(struct device *dev)
> {
> int ret = pm_runtime_resume_and_get(dev);
> if (ret < 0)
> return ERR_PTR(ret);
>
> return dev;
> }
>
> It would be a matter of redefining the FREE to also take error
> pointers into account and you could do
>
> struct device *__dev __free(pm_runtim_put) = pm_runtime_resume_and_get_dev(dev);
> if (IS_ERR(__dev))
> return PTR_ERR(__dev);
That'll work, too. Though, I find the notion of __free() and a
temporary variable __dev a bit too cumbersome; it's used only for
auto-clean stuff, so it could be somewhat anonymous.
But it's all about a matter of taste, and I'd follow what you and
other guys suggest.
FWIW, there are lots of code doing like
pm_runtime_get_sync(dev);
mutex_lock(&foo);
....
mutex_unlock(&foo);
pm_runtime_put(dev);
return;
or
ret = pm_runtime_resume_and_get(dev);
if (ret)
return ret;
mutex_lock(&foo);
....
mutex_unlock(&foo);
pm_runtime_put_autosuspend(dev);
return 0;
and they can be converted nicely with guard() once when PM runtime can
be automatically unreferenced. With my proposed change, it would
become like:
pm_runtime_get_sync(dev);
pm_runtime_auto_clean(dev);
guard(mutex)(&foo);
....
return;
or
ret = pm_runtime_resume_and_get(dev);
if (ret)
return ret;
pm_runtime_auto_clean_autosuspend(dev);
guard(mutex)(&foo);
....
return 0;
thanks,
Takashi
next prev parent reply other threads:[~2025-09-18 7:10 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-10 14:00 PM runtime auto-cleanup macros Takashi Iwai
2025-09-11 7:31 ` Takashi Iwai
2025-09-17 18:58 ` Rafael J. Wysocki
2025-09-18 7:10 ` Takashi Iwai [this message]
2025-09-18 11:28 ` Rafael J. Wysocki
2025-09-18 20:19 ` Rafael J. Wysocki
2025-09-18 20:41 ` Rafael J. Wysocki
2025-09-19 7:37 ` Takashi Iwai
2025-09-19 13:05 ` Rafael J. Wysocki
2025-09-19 13:41 ` Takashi Iwai
2025-09-19 13:43 ` Takashi Iwai
2025-09-19 15:49 ` Rafael J. Wysocki
2025-09-19 16:04 ` Takashi Iwai
2025-09-19 15:52 ` Rafael J. Wysocki
2025-09-19 16:06 ` 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=87tt10b5hq.wl-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=linux-kernel@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