Received: by 10.223.176.5 with SMTP id f5csp3169524wra; Thu, 1 Feb 2018 11:55:26 -0800 (PST) X-Google-Smtp-Source: AH8x227/RdLTrq0riZdREY/KRGS28ca27TH/v+tnhgA1JkPE1MLHjHfxJkUe1GZupe3Z8pUAfGfl X-Received: by 10.98.19.137 with SMTP id 9mr37610855pft.5.1517514926423; Thu, 01 Feb 2018 11:55:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517514926; cv=none; d=google.com; s=arc-20160816; b=IQC4N2xZxF01qsU0eJLfcS+S6BXdMnQXqFZZXp7SRaJONkiIztYGVWJJzUw2Cj6UAb 6lrb7WLoKBmOvyKd+SOkg0ljOCAJsAxDBGXsUGGgHyJ51YcBrlZJ4lh3lFTNA9lrbDTH KLw/67758lMlHjHmzBedAy98ua8ZZ8uDXf9cP53AiGjd4Li0vWXvaIdZgLmfOyXvorIu Xf/7qOyImL9AZX64vnSwsBnnVcxwfNyE8+XoeDyEoyc3t/Ws7WzNDUSmngmvDxXHeQrO f2b7wWC6G3p2hr8sMCQkWM1PIf033Fv5xQSrNs/F+ZIq+NeTvrjZRzddlqJU7OUocLAn 2Eog== 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 :arc-authentication-results; bh=CCBqc9/Zi7a1U4i/jMhUWVFIKIJhXj0oaGBolxrAmjU=; b=EBjP7X2L+cgbEdDQ4GxCCy+ikFOoru8h5/ydo+Bbg9jLpkqKuvkbj58O9nlZ+gEjNu zznKfQXNIUKObDUI15OveANrXPFI6semAZK1nRO5HNtAZUAs0rOLxxkJG5QO7o41flXn CJe2V/Lx5FmSUtYjK8toGAsuzebyOECQbaShnk039jgMSvew3FHP1FCHE2F9Fdr+iXUz qa/BOTcOFgkiuk8h2DfuCgQ3WPR6jlCC0DzX7dlPfoTogQLWixTgLekTsWAaxwnohX+v ZP9xeMteXffCSrZ7SnurkRcLDE4Ot36MFmfw3nU28IUjD1650VWtBCX/p81ZDufWMkWY gOIw== 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 s8si180853pgv.654.2018.02.01.11.55.08; Thu, 01 Feb 2018 11:55:26 -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 S1754850AbeBATyb (ORCPT + 99 others); Thu, 1 Feb 2018 14:54:31 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:63001 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754594AbeBATy1 (ORCPT ); Thu, 1 Feb 2018 14:54:27 -0500 X-IronPort-AV: E=Sophos;i="5.46,444,1511823600"; d="scan'208";a="253322749" Received: from abo-202-111-68.mrs.modulonet.fr (HELO [192.168.0.15]) ([85.68.111.202]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Feb 2018 20:54:25 +0100 Date: Thu, 1 Feb 2018 20:54:25 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Mathieu Malaterre cc: Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: Re: cocci script for detecting alloc_apertures mem leak In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) 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 Thu, 1 Feb 2018, Mathieu Malaterre wrote: > Hi cocci gurus, > > I am wondering if coccinelle can handle detection of kzalloc mem leak > (within alloc_apertures call) ? Typically: > > $ cat drivers/video/fbdev/vesafb.c > static int vesafb_probe(struct platform_device *dev) > [...] > info->apertures = alloc_apertures(1); > > but then: > > static void vesafb_destroy(struct fb_info *info) > { > struct vesafb_par *par = info->par; > > fb_dealloc_cmap(&info->cmap); > arch_phys_wc_del(par->wc_cookie); > if (info->screen_base) > iounmap(info->screen_base); > release_mem_region(info->apertures->ranges[0].base, > info->apertures->ranges[0].size); > } > > For reference: > > $ cat include/linux/fb.h > static inline struct apertures_struct *alloc_apertures(unsigned int max_num) { > struct apertures_struct *a = kzalloc(sizeof(struct apertures_struct) > + max_num * sizeof(struct aperture), GFP_KERNEL); You could do something like this: @nm@ identifier i,j,prb,rem; @@ struct i j = { .prob = prb, .rem = remove, }; @a exists@ identifier nm.prb; expression e; @@ prb(...) { <+... e = alloc_apertures(...) ...+> } @@ identifier nm.rem; expression a.e; @@ *rem(...) { ... when != kfree(e) } This is assuming that the reference to the alloc_apertures value is made in the same way in the probe and remove function. If this is not the case, you have to figure out how to express some relation between them. This is also assuming that the kfree is directly in the remove function, not in some function called by it. If that hypothesis does not hold, it might be better to just report any cases where ther is no call to kfree(e) in the whole file. For this you could replace the last rule with: @ok@ expression a.e; @@ kfree(e); @depends on !ok@ expression a.e; @@ * e = alloc_apertures(...) That would be checking for files that don't free the result os alloc_apertures anywhere. If you want to do a full interprocedural analysis, it is possible, but more complicated. You could look at coccinelle/demos/iteration.cocci. julia