From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DF7CC43460 for ; Tue, 27 Apr 2021 13:51:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 286E560FEE for ; Tue, 27 Apr 2021 13:51:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236556AbhD0NwS (ORCPT ); Tue, 27 Apr 2021 09:52:18 -0400 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:59636 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234429AbhD0NwM (ORCPT ); Tue, 27 Apr 2021 09:52:12 -0400 IronPort-HdrOrdr: =?us-ascii?q?A9a23=3A7gB5OK+jPivzqTsXsfBuk+ATI+orLtY04lQ7?= =?us-ascii?q?vn1ZYxY9SKGlvuqpm+kW0gKxtS0YX2sulcvFFK6LR37d8pAd2/hzAZ6OVBTr0V?= =?us-ascii?q?HGEKhM9o3nqgeKJwTb1spwkZhtaLJ/DtqYNzhHpOL3+hOxHdpl4NTvys6VrNzT?= =?us-ascii?q?xXtsUg1mApsIhztRMBqREUF9WWB9aaYRKZz03KB6jgvlXXwWa8ihb0NlY9T+?= X-IronPort-AV: E=Sophos;i="5.82,254,1613430000"; d="scan'208";a="379816306" Received: from palace.rsr.lip6.fr (HELO palace.lip6.fr) ([132.227.105.202]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 27 Apr 2021 15:51:26 +0200 From: Julia Lawall To: Julia Lawall , Krzysztof Kozlowski , Mauro Carvalho Chehab Cc: kernel-janitors@vger.kernel.org, Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org, "Rafael J . Wysocki" , Johan Hovold , Zhang Qilong , Jakub Kicinski Subject: [PATCH v3] coccinelle: api: semantic patch to use pm_runtime_resume_and_get Date: Tue, 27 Apr 2021 14:58:34 +0200 Message-Id: <20210427125834.2477467-1-Julia.Lawall@inria.fr> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pm_runtime_get_sync keeps a reference count on failure, which can lead to leaks. pm_runtime_resume_and_get drops the reference count in the failure case. This rule very conservatively follows the definition of pm_runtime_resume_and_get to address the cases where the reference count is unlikely to be needed in the failure case. Specifically, the change is only done when pm_runtime_get_sync is followed immediately by an if and when the branch of the if is immediately a call to pm_runtime_put_noidle (like in the definition of pm_runtime_resume_and_get) or something that is likely a print statement followed by a pm_runtime_resume_and_get call. The patch case appears somewhat more complicated, because it also deals with the cases where {}s need to be removed. pm_runtime_resume_and_get was introduced in commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter") Signed-off-by: Julia Lawall --- v3: add the people who signed off on commit dd8088d5a896, expand the log message v2: better keyword scripts/coccinelle/api/pm_runtime_resume_and_get.cocci | 153 +++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/scripts/coccinelle/api/pm_runtime_resume_and_get.cocci b/scripts/coccinelle/api/pm_runtime_resume_and_get.cocci new file mode 100644 index 000000000000..3387cb606f9b --- /dev/null +++ b/scripts/coccinelle/api/pm_runtime_resume_and_get.cocci @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Use pm_runtime_resume_and_get. +/// pm_runtime_get_sync keeps a reference count on failure, +/// which can lead to leaks. pm_runtime_resume_and_get +/// drops the reference count in the failure case. +/// This rule addresses the cases where the reference count +/// is unlikely to be needed in the failure case. +/// +// Confidence: High +// Copyright: (C) 2021 Julia Lawall, Inria +// URL: https://coccinelle.gitlabpages.inria.fr/website +// Options: --include-headers --no-includes +// Keywords: pm_runtime_get_sync + +virtual patch +virtual context +virtual org +virtual report + +@r0 depends on patch && !context && !org && !report@ +expression ret,e; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); +- if (ret < 0) +- pm_runtime_put_noidle(e); + +@r1 depends on patch && !context && !org && !report@ +expression ret,e; +statement S1,S2; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) +- { +- pm_runtime_put_noidle(e); + S1 +- } + else S2 + +@r2 depends on patch && !context && !org && !report@ +expression ret,e; +statement S; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) { +- pm_runtime_put_noidle(e); + ... + } else S + +@r3 depends on patch && !context && !org && !report@ +expression ret,e; +identifier f; +constant char[] c; +statement S; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) +- { + f(...,c,...); +- pm_runtime_put_noidle(e); +- } + else S + +@r4 depends on patch && !context && !org && !report@ +expression ret,e; +identifier f; +constant char[] c; +statement S; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) { + f(...,c,...); +- pm_runtime_put_noidle(e); + ... + } else S + +// ---------------------------------------------------------------------------- + +@r2_context depends on !patch && (context || org || report)@ +statement S; +expression e, ret; +position j0, j1; +@@ + +* ret@j0 = pm_runtime_get_sync(e); + if (ret < 0) { +* pm_runtime_put_noidle@j1(e); + ... + } else S + +@r3_context depends on !patch && (context || org || report)@ +identifier f; +statement S; +constant char []c; +expression e, ret; +position j0, j1; +@@ + +* ret@j0 = pm_runtime_get_sync(e); + if (ret < 0) { + f(...,c,...); +* pm_runtime_put_noidle@j1(e); + ... + } else S + +// ---------------------------------------------------------------------------- + +@script:python r2_org depends on org@ +j0 << r2_context.j0; +j1 << r2_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync" +coccilib.org.print_todo(j0[0], msg) +coccilib.org.print_link(j1[0], "") + +@script:python r3_org depends on org@ +j0 << r3_context.j0; +j1 << r3_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync" +coccilib.org.print_todo(j0[0], msg) +coccilib.org.print_link(j1[0], "") + +// ---------------------------------------------------------------------------- + +@script:python r2_report depends on report@ +j0 << r2_context.j0; +j1 << r2_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync on line %s." % (j0[0].line) +coccilib.report.print_report(j0[0], msg) + +@script:python r3_report depends on report@ +j0 << r3_context.j0; +j1 << r3_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync on %s." % (j0[0].line) +coccilib.report.print_report(j0[0], msg) + From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19643C433ED for ; Tue, 27 Apr 2021 13:51:35 +0000 (UTC) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5BD86613DA for ; Tue, 27 Apr 2021 13:51:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5BD86613DA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=inria.fr Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=cocci-bounces@systeme.lip6.fr Received: from systeme.lip6.fr (systeme.lip6.fr [132.227.104.7]) by isis.lip6.fr (8.15.2/8.15.2) with ESMTP id 13RDpUV6000080; Tue, 27 Apr 2021 15:51:30 +0200 (CEST) Received: from systeme.lip6.fr (systeme.lip6.fr [127.0.0.1]) by systeme.lip6.fr (Postfix) with ESMTP id F421F7634; Tue, 27 Apr 2021 15:51:29 +0200 (CEST) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by systeme.lip6.fr (Postfix) with ESMTPS id 384833DD8 for ; Tue, 27 Apr 2021 15:51:28 +0200 (CEST) Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by isis.lip6.fr (8.15.2/8.15.2) with ESMTPS id 13RDpRcX020417 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 27 Apr 2021 15:51:27 +0200 (CEST) IronPort-HdrOrdr: =?us-ascii?q?A9a23=3A7gB5OK+jPivzqTsXsfBuk+ATI+orLtY04lQ7?= =?us-ascii?q?vn1ZYxY9SKGlvuqpm+kW0gKxtS0YX2sulcvFFK6LR37d8pAd2/hzAZ6OVBTr0V?= =?us-ascii?q?HGEKhM9o3nqgeKJwTb1spwkZhtaLJ/DtqYNzhHpOL3+hOxHdpl4NTvys6VrNzT?= =?us-ascii?q?xXtsUg1mApsIhztRMBqREUF9WWB9aaYRKZz03KB6jgvlXXwWa8ihb0NlY9T+?= X-IronPort-AV: E=Sophos;i="5.82,254,1613430000"; d="scan'208";a="379816306" Received: from palace.rsr.lip6.fr (HELO palace.lip6.fr) ([132.227.105.202]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 27 Apr 2021 15:51:26 +0200 From: Julia Lawall To: Julia Lawall , Krzysztof Kozlowski , Mauro Carvalho Chehab Date: Tue, 27 Apr 2021 14:58:34 +0200 Message-Id: <20210427125834.2477467-1-Julia.Lawall@inria.fr> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Greylist: Sender IP whitelisted, Sender e-mail whitelisted, not delayed by milter-greylist-4.4.3 (isis.lip6.fr [132.227.60.2]); Tue, 27 Apr 2021 15:51:30 +0200 (CEST) X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.4.3 (isis.lip6.fr [132.227.60.2]); Tue, 27 Apr 2021 15:51:27 +0200 (CEST) X-Scanned-By: MIMEDefang 2.78 on 132.227.60.2 X-Scanned-By: MIMEDefang 2.78 on 132.227.60.2 Cc: Michal Marek , "Rafael J . Wysocki" , kernel-janitors@vger.kernel.org, Zhang Qilong , Nicolas Palix , linux-kernel@vger.kernel.org, Johan Hovold , Jakub Kicinski , cocci@systeme.lip6.fr Subject: [Cocci] [PATCH v3] coccinelle: api: semantic patch to use pm_runtime_resume_and_get X-BeenThere: cocci@systeme.lip6.fr X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: cocci-bounces@systeme.lip6.fr Errors-To: cocci-bounces@systeme.lip6.fr pm_runtime_get_sync keeps a reference count on failure, which can lead to leaks. pm_runtime_resume_and_get drops the reference count in the failure case. This rule very conservatively follows the definition of pm_runtime_resume_and_get to address the cases where the reference count is unlikely to be needed in the failure case. Specifically, the change is only done when pm_runtime_get_sync is followed immediately by an if and when the branch of the if is immediately a call to pm_runtime_put_noidle (like in the definition of pm_runtime_resume_and_get) or something that is likely a print statement followed by a pm_runtime_resume_and_get call. The patch case appears somewhat more complicated, because it also deals with the cases where {}s need to be removed. pm_runtime_resume_and_get was introduced in commit dd8088d5a896 ("PM: runtime: Add pm_runtime_resume_and_get to deal with usage counter") Signed-off-by: Julia Lawall --- v3: add the people who signed off on commit dd8088d5a896, expand the log message v2: better keyword scripts/coccinelle/api/pm_runtime_resume_and_get.cocci | 153 +++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/scripts/coccinelle/api/pm_runtime_resume_and_get.cocci b/scripts/coccinelle/api/pm_runtime_resume_and_get.cocci new file mode 100644 index 000000000000..3387cb606f9b --- /dev/null +++ b/scripts/coccinelle/api/pm_runtime_resume_and_get.cocci @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Use pm_runtime_resume_and_get. +/// pm_runtime_get_sync keeps a reference count on failure, +/// which can lead to leaks. pm_runtime_resume_and_get +/// drops the reference count in the failure case. +/// This rule addresses the cases where the reference count +/// is unlikely to be needed in the failure case. +/// +// Confidence: High +// Copyright: (C) 2021 Julia Lawall, Inria +// URL: https://coccinelle.gitlabpages.inria.fr/website +// Options: --include-headers --no-includes +// Keywords: pm_runtime_get_sync + +virtual patch +virtual context +virtual org +virtual report + +@r0 depends on patch && !context && !org && !report@ +expression ret,e; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); +- if (ret < 0) +- pm_runtime_put_noidle(e); + +@r1 depends on patch && !context && !org && !report@ +expression ret,e; +statement S1,S2; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) +- { +- pm_runtime_put_noidle(e); + S1 +- } + else S2 + +@r2 depends on patch && !context && !org && !report@ +expression ret,e; +statement S; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) { +- pm_runtime_put_noidle(e); + ... + } else S + +@r3 depends on patch && !context && !org && !report@ +expression ret,e; +identifier f; +constant char[] c; +statement S; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) +- { + f(...,c,...); +- pm_runtime_put_noidle(e); +- } + else S + +@r4 depends on patch && !context && !org && !report@ +expression ret,e; +identifier f; +constant char[] c; +statement S; +@@ + +- ret = pm_runtime_get_sync(e); ++ ret = pm_runtime_resume_and_get(e); + if (ret < 0) { + f(...,c,...); +- pm_runtime_put_noidle(e); + ... + } else S + +// ---------------------------------------------------------------------------- + +@r2_context depends on !patch && (context || org || report)@ +statement S; +expression e, ret; +position j0, j1; +@@ + +* ret@j0 = pm_runtime_get_sync(e); + if (ret < 0) { +* pm_runtime_put_noidle@j1(e); + ... + } else S + +@r3_context depends on !patch && (context || org || report)@ +identifier f; +statement S; +constant char []c; +expression e, ret; +position j0, j1; +@@ + +* ret@j0 = pm_runtime_get_sync(e); + if (ret < 0) { + f(...,c,...); +* pm_runtime_put_noidle@j1(e); + ... + } else S + +// ---------------------------------------------------------------------------- + +@script:python r2_org depends on org@ +j0 << r2_context.j0; +j1 << r2_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync" +coccilib.org.print_todo(j0[0], msg) +coccilib.org.print_link(j1[0], "") + +@script:python r3_org depends on org@ +j0 << r3_context.j0; +j1 << r3_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync" +coccilib.org.print_todo(j0[0], msg) +coccilib.org.print_link(j1[0], "") + +// ---------------------------------------------------------------------------- + +@script:python r2_report depends on report@ +j0 << r2_context.j0; +j1 << r2_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync on line %s." % (j0[0].line) +coccilib.report.print_report(j0[0], msg) + +@script:python r3_report depends on report@ +j0 << r3_context.j0; +j1 << r3_context.j1; +@@ + +msg = "WARNING: opportunity for pm_runtime_get_sync on %s." % (j0[0].line) +coccilib.report.print_report(j0[0], msg) + _______________________________________________ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci