[PATCH] coccinelle: api: add a checker for pm_runtime_use_autosuspend() usage

Joshua Crofts posted 1 patch 1 week, 3 days ago
There is a newer version of this series
scripts/coccinelle/api/pm_autosuspend.cocci | 62 +++++++++++++++++++++
1 file changed, 62 insertions(+)
create mode 100644 scripts/coccinelle/api/pm_autosuspend.cocci
[PATCH] coccinelle: api: add a checker for pm_runtime_use_autosuspend() usage
Posted by Joshua Crofts 1 week, 3 days ago
Add a checker for ensuring that pm_runtime_dont_use_autosuspend() is
called in a file if pm_runtime_use_autosuspend() is called and its
devm_* counterpart isn't present.

Without _dont_use_autosuspend(), removing devices causes resource leaks.

Assisted-by: LLM
Signed-off-by: Joshua Crofts <joshua.crofts1@gmail.com>
---
 scripts/coccinelle/api/pm_autosuspend.cocci | 62 +++++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 scripts/coccinelle/api/pm_autosuspend.cocci

diff --git a/scripts/coccinelle/api/pm_autosuspend.cocci b/scripts/coccinelle/api/pm_autosuspend.cocci
new file mode 100644
index 000000000000..2354396d9344
--- /dev/null
+++ b/scripts/coccinelle/api/pm_autosuspend.cocci
@@ -0,0 +1,62 @@
+//SPDX-License-Identifier: GPL-2.0-only
+//
+// Confidence: High
+// Copyright: (C) 2026 Joshua Crofts
+// URL: https://coccinelle.gitlabpages.inria.fr/website
+// Options: --no-includes
+
+virtual context
+virtual org
+virtual report
+
+//----------------------------------------------------------
+// Detection
+//----------------------------------------------------------
+
+@has_use@
+expression dev;
+position p;
+@@
+
+pm_runtime_use_autosuspend@p(dev);
+
+@has_dont@
+expression dev;
+@@
+
+pm_runtime_dont_use_autosuspend(dev)
+
+@has_devm@
+expression dev;
+@@
+
+devm_pm_runtime_enable(dev)
+
+//----------------------------------------------------------
+// Context mode
+//----------------------------------------------------------
+
+@depends on context && has_use && !has_dont && !has_devm@
+expression dev;
+position has_use.p;
+@@
+
+* pm_runtime_use_autosuspend@p(dev)
+
+//----------------------------------------------------------
+// Org and report mode
+//----------------------------------------------------------
+
+@script:python depends on org && has_use && !has_dont && !has_devm@
+p << has_use.p;
+@@
+
+msg = "WARNING: pm_runtime_use_autosuspend() called without matching pm_runtime_dont_use_autosuspend or devm_pm_runtime_enable()"
+cocci.print_main(msg, p)
+
+@script:python depends on report && has_use && !has_dont && !has_devm@
+p << has_use.p;
+@@
+
+msg = "WARNING: pm_runtime_use_autosuspend() called without matching pm_runtime_dont_use_autosuspend or devm_pm_runtime_enable()"
+coccilib.report.print_report(p[0], msg)
-- 
2.47.3
Re: [PATCH] coccinelle: api: add a checker for pm_runtime_use_autosuspend() usage
Posted by Julia Lawall 1 week, 3 days ago

On Mon, 14 Sep 2026, Joshua Crofts wrote:

> Add a checker for ensuring that pm_runtime_dont_use_autosuspend() is
> called in a file if pm_runtime_use_autosuspend() is called and its
> devm_* counterpart isn't present.
>
> Without _dont_use_autosuspend(), removing devices causes resource leaks.
>
> Assisted-by: LLM
> Signed-off-by: Joshua Crofts <joshua.crofts1@gmail.com>
> ---
>  scripts/coccinelle/api/pm_autosuspend.cocci | 62 +++++++++++++++++++++
>  1 file changed, 62 insertions(+)
>  create mode 100644 scripts/coccinelle/api/pm_autosuspend.cocci
>
> diff --git a/scripts/coccinelle/api/pm_autosuspend.cocci b/scripts/coccinelle/api/pm_autosuspend.cocci
> new file mode 100644
> index 000000000000..2354396d9344
> --- /dev/null
> +++ b/scripts/coccinelle/api/pm_autosuspend.cocci
> @@ -0,0 +1,62 @@
> +//SPDX-License-Identifier: GPL-2.0-only
> +//
> +// Confidence: High
> +// Copyright: (C) 2026 Joshua Crofts
> +// URL: https://coccinelle.gitlabpages.inria.fr/website
> +// Options: --no-includes
> +
> +virtual context
> +virtual org
> +virtual report
> +
> +//----------------------------------------------------------
> +// Detection
> +//----------------------------------------------------------
> +
> +@has_use@
> +expression dev;
> +position p;
> +@@
> +
> +pm_runtime_use_autosuspend@p(dev);
> +
> +@has_dont@
> +expression dev;
> +@@
> +
> +pm_runtime_dont_use_autosuspend(dev)
> +
> +@has_devm@
> +expression dev;
> +@@
> +
> +devm_pm_runtime_enable(dev)
> +
> +//----------------------------------------------------------
> +// Context mode
> +//----------------------------------------------------------
> +
> +@depends on context && has_use && !has_dont && !has_devm@
> +expression dev;
> +position has_use.p;
> +@@
> +
> +* pm_runtime_use_autosuspend@p(dev)

I don't think you need the position variable here.


> +
> +//----------------------------------------------------------
> +// Org and report mode
> +//----------------------------------------------------------
> +
> +@script:python depends on org && has_use && !has_dont && !has_devm@

In this rule and the next one, you don't need to depend on has_use.  That
happens already due to the inheritance of the metavariable.

julia

> +p << has_use.p;
> +@@
> +
> +msg = "WARNING: pm_runtime_use_autosuspend() called without matching pm_runtime_dont_use_autosuspend or devm_pm_runtime_enable()"
> +cocci.print_main(msg, p)
> +
> +@script:python depends on report && has_use && !has_dont && !has_devm@
> +p << has_use.p;
> +@@
> +
> +msg = "WARNING: pm_runtime_use_autosuspend() called without matching pm_runtime_dont_use_autosuspend or devm_pm_runtime_enable()"
> +coccilib.report.print_report(p[0], msg)
> --
> 2.47.3
>
>
Re: [PATCH] coccinelle: api: add a checker for pm_runtime_use_autosuspend() usage
Posted by Joshua Crofts 1 week, 3 days ago
On Mon, 14 Sept 2026 at 17:17, Julia Lawall <julia.lawall@inria.fr> wrote:
>
>
>
> On Mon, 14 Sep 2026, Joshua Crofts wrote:
>
> > Add a checker for ensuring that pm_runtime_dont_use_autosuspend() is
> > called in a file if pm_runtime_use_autosuspend() is called and its
> > devm_* counterpart isn't present.
> >
> > Without _dont_use_autosuspend(), removing devices causes resource leaks.
> >
> > Assisted-by: LLM
> > Signed-off-by: Joshua Crofts <joshua.crofts1@gmail.com>
> > ---
> >  scripts/coccinelle/api/pm_autosuspend.cocci | 62 +++++++++++++++++++++
> >  1 file changed, 62 insertions(+)
> >  create mode 100644 scripts/coccinelle/api/pm_autosuspend.cocci
> >
> > diff --git a/scripts/coccinelle/api/pm_autosuspend.cocci b/scripts/coccinelle/api/pm_autosuspend.cocci
> > new file mode 100644
> > index 000000000000..2354396d9344
> > --- /dev/null
> > +++ b/scripts/coccinelle/api/pm_autosuspend.cocci
> > @@ -0,0 +1,62 @@
> > +//SPDX-License-Identifier: GPL-2.0-only
> > +//
> > +// Confidence: High
> > +// Copyright: (C) 2026 Joshua Crofts
> > +// URL: https://coccinelle.gitlabpages.inria.fr/website
> > +// Options: --no-includes
> > +
> > +virtual context
> > +virtual org
> > +virtual report
> > +
> > +//----------------------------------------------------------
> > +// Detection
> > +//----------------------------------------------------------
> > +
> > +@has_use@
> > +expression dev;
> > +position p;
> > +@@
> > +
> > +pm_runtime_use_autosuspend@p(dev);
> > +
> > +@has_dont@
> > +expression dev;
> > +@@
> > +
> > +pm_runtime_dont_use_autosuspend(dev)
> > +
> > +@has_devm@
> > +expression dev;
> > +@@
> > +
> > +devm_pm_runtime_enable(dev)
> > +
> > +//----------------------------------------------------------
> > +// Context mode
> > +//----------------------------------------------------------
> > +
> > +@depends on context && has_use && !has_dont && !has_devm@
> > +expression dev;
> > +position has_use.p;
> > +@@
> > +
> > +* pm_runtime_use_autosuspend@p(dev)
>
> I don't think you need the position variable here.
>
>
> > +
> > +//----------------------------------------------------------
> > +// Org and report mode
> > +//----------------------------------------------------------
> > +
> > +@script:python depends on org && has_use && !has_dont && !has_devm@
>
> In this rule and the next one, you don't need to depend on has_use.  That
> happens already due to the inheritance of the metavariable.
>

Thanks for the review. This is my first time writing a semantic patch,
so it's a bit
rough. Additionally, I just noticed I sent the wrong version...

--
Kind regards,
Joshua Crofts