Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp935303imj; Sat, 9 Feb 2019 11:11:19 -0800 (PST) X-Google-Smtp-Source: AHgI3Ibl7y3LOiWgjv2D3T+7KCkUQMVpGfj4xIf8vYIl+/nt1C9fB0+nE5+7frbY8T1VgyzFK7Ga X-Received: by 2002:a63:4e15:: with SMTP id c21mr16901657pgb.50.1549739479788; Sat, 09 Feb 2019 11:11:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549739479; cv=none; d=google.com; s=arc-20160816; b=Mj+wH4hO5hcXOSuifwtrRarrqDhF3J8jY/pU/DjcXyJyw/Obu9yUw2fXHDKJp6nche BxTWD0jPGAoS/JUQdp7cw6LGvgQq/nTKjAMTBrxLDOqVRgUDcNI9+a8J3Aji+8i85AO1 EmjKWZoZ8aCdenWD5KjGVznnxK3EAFJhw3HLYAVB5Ch3s/dklv3iuw5NLcYpg/hoKa+K CCES+dggJ5CG10duv8+jDghmFMErJ3DV4WxbJVMwAkQDB7IpDBoYbcTmM06tyVO3Bjyg lwvIYpcIlETdmzEXVJ57H7i2YOYwDzzYjJlFKKqzghNOfG4FEe/B85bTW2nnBeP49nNG D3oQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=iX47SlPgJyV/X932jytCFTqIe/rUdktrr+xGn5GPpKI=; b=y1UrFpeyJ411HdB0+fbl8gfbGueYN60MlyDi1x+3AcINu6jM7egxFN4TQaXvXcQKSF ufav+GU5NO3g2fs+KDEdLq5HGdhhFk6tmuq6s7px23UhXSzmHd1Jirwl+WeLR6ZXMu3S S2Bz7vvBlUX/IZyr9eZEaF+pGV74WO+u303bizTAA7Hsm/CR5epJgwvoTqGcJBbm/sov zCkTVI1lFH+wsQt3ZwUkjicNp/0dDVcgHmOMiiuocsFkPqJ3tmAjlheJFzVKCM+HEF7g 6wp8F5rd6XAPsUbusxOCuvoAieJxhjf8+bNCHB2+BR6XuaH5WixLWGp59EJizFmhm8PT TO0g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k7si5698979plt.342.2019.02.09.11.11.00; Sat, 09 Feb 2019 11:11:19 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727232AbfBITK5 (ORCPT + 99 others); Sat, 9 Feb 2019 14:10:57 -0500 Received: from mail2-relais-roc.national.inria.fr ([192.134.164.83]:35593 "EHLO mail2-relais-roc.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727013AbfBITK5 (ORCPT ); Sat, 9 Feb 2019 14:10:57 -0500 X-IronPort-AV: E=Sophos;i="5.58,352,1544482800"; d="scan'208";a="368707933" Received: from abo-58-107-68.mrs.modulonet.fr (HELO hadrien) ([85.68.107.58]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Feb 2019 20:10:54 +0100 Date: Sat, 9 Feb 2019 20:10:53 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Wen Yang cc: Gilles Muller , Nicolas Palix , Michal Marek , "cocci@systeme.lip6.fr" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] coccinelle: semantic patch for missing put_device() In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 9 Feb 2019, Wen Yang wrote: > The of_find_device_by_node() takes a reference to the underlying device > structure, we should release that reference. > Use this semantic patch, we've found some object reference leaks, > such as: > 11907e9d3533 ("ASoC: fsl-asoc-card: fix object reference leaks in fsl_asoc_card_probe") > a12085d13997 ("mtd: rawnand: atmel: fix possible object reference leak") > 11493f26856a ("mtd: rawnand: jz4780: fix possible object reference leak") > There are still dozens of reference leaks in the kernel code. > > Signed-off-by: Wen Yang > --- > scripts/coccinelle/free/put_device.cocci | 54 ++++++++++++++++++++++++++++++++ > 1 file changed, 54 insertions(+) > create mode 100644 scripts/coccinelle/free/put_device.cocci > > diff --git a/scripts/coccinelle/free/put_device.cocci b/scripts/coccinelle/free/put_device.cocci > new file mode 100644 > index 0000000..d3e3ba9 > --- /dev/null > +++ b/scripts/coccinelle/free/put_device.cocci > @@ -0,0 +1,54 @@ > +/// Find missing put_device for every of_find_device_by_node. > +/// > +// Confidence: Moderate > +// Copyright: (C) 2018-2019 Wen Yang, ZTE. GPLv2. > +// Comments: > +// Options: --no-includes --include-headers > + > +virtual report > +virtual org > + > +@search@ Just put exists after search, and then drop the when exists below. > +local idexpression id; > +expression x,e; > +position p1,p2; > +type T; > +@@ > + > +id = of_find_device_by_node@p1(x) > +... when != e = id This should not use the same e as in the when's below. Make a new matavariavle e1 and use that here. > +if (id == NULL || ...) { ... return ...; } > +... when != put_device(&id->dev) I have the impression that you should also consider platform_device_put. > + when != of_dev_put(&id) > + when != if (id) { ... put_device(&id->dev) ... } > + when != e = (T)id > + when != e = &id->dev > + when != e = gen_pool_get(&id->dev, ...) > + when != e = platform_get_drvdata(id) > + when exists > +( > +return id; > +| > +return dev_get_drvdata(&id->dev); > +| > +return platform_get_drvdata(id) Make a new type metavariable T1 amd use it to put a cast on the result of platform_get_drvdata. For dev_get_drvdata, I didn't actually find any examples with a cast, but it wouldn't hurt to put a cast there too Thanks, julia > +| > +return@p2 ...; > +) > + > +@script:python depends on report@ > +p1 << search.p1; > +p2 << search.p2; > +@@ > + > +msg = "ERROR: missing put_device; of_find_device_by_node on line %s and return without releasing on line %s" % (p1[0].line,p2[0].line) > +coccilib.report.print_report(p2[0],msg) > + > +@script:python depends on org@ > +p1 << search.p1; > +p2 << search.p2; > +@@ > + > +msg = "ERROR: missing put_device; of_find_device_by_node on line %s and return without releasing on line %s" % (p1[0].line,p2[0].line) > +cocci.print_main(msg,p1) > +cocci.print_secs("",p2) > -- > 2.7.4 > >