From: Julia Lawall <julia.lawall@lip6.fr>
To: Himanshu Jha <himanshujha199640@gmail.com>
Cc: yamada.masahiro@socionext.com,
Julia Lawall <Julia.Lawall@lip6.fr>,
Gilles Muller <Gilles.Muller@lip6.fr>,
nicolas.palix@imag.fr, michal.lkml@markovi.net,
cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] Coccinelle: kzalloc-simple: Add all zero allocating functions
Date: Tue, 26 Dec 2017 22:52:48 +0100 (CET) [thread overview]
Message-ID: <alpine.DEB.2.20.1712262252360.5715@hadrien> (raw)
In-Reply-To: <1514324410-14561-1-git-send-email-himanshujha199640@gmail.com>
On Wed, 27 Dec 2017, Himanshu Jha wrote:
> There are many instances where memory is allocated using regular
> allocator
> functions immediately followed by setting the allocated memory
> to 0 value using memset.
>
> We already have zero memory allocator functions to set the memory to
> 0 value instead of manually setting it using memset.
>
> Therefore, use zero memory allocating functions instead of regular
> memory allocators followed by memset 0 to remove redundant memset and
> make the code more cleaner and also reduce the code size.
>
> Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
> ---
>
> v2:
> -fix typo in copyright.
> -move all the (T *) disjunction cases before (T) as (T) matches any cast
> at all including (T *) ones which is not desirable.
>
> scripts/coccinelle/api/alloc/kzalloc-simple.cocci | 373 +++++++++++++++++++++-
> 1 file changed, 368 insertions(+), 5 deletions(-)
>
> diff --git a/scripts/coccinelle/api/alloc/kzalloc-simple.cocci b/scripts/coccinelle/api/alloc/kzalloc-simple.cocci
> index 52c55e4..d08d526 100644
> --- a/scripts/coccinelle/api/alloc/kzalloc-simple.cocci
> +++ b/scripts/coccinelle/api/alloc/kzalloc-simple.cocci
> @@ -1,5 +1,5 @@
> ///
> -/// Use kzalloc rather than kmalloc followed by memset with 0
> +/// Use zeroing allocator rather than allocator followed by memset with 0
> ///
> /// This considers some simple cases that are common and easy to validate
> /// Note in particular that there are no ...s in the rule, so all of the
> @@ -8,6 +8,7 @@
> // Confidence: High
> // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU. GPLv2.
> // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6. GPLv2.
> +// Copyright: (C) 2017 Himanshu Jha GPLv2.
> // URL: http://coccinelle.lip6.fr/rules/kzalloc.html
> // Options: --no-includes --include-headers
> //
> @@ -28,11 +29,14 @@ virtual report
> @depends on context@
> type T, T2;
> expression x;
> -expression E1,E2;
> +expression E1;
> statement S;
> @@
>
> -* x = (T)kmalloc(E1,E2);
> +* x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
> + kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
> + devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|pci_alloc_consistent(...,E1,...)\|
> + kvmalloc_node(E1,...)\);
> if ((x==NULL) || ...) S
> * memset((T2)x,0,E1);
>
> @@ -43,12 +47,101 @@ statement S;
> @depends on patch@
> type T, T2;
> expression x;
> -expression E1,E2;
> +expression E1,E2,E3,E4;
> statement S;
> @@
>
> -- x = (T)kmalloc(E1,E2);
> +(
> +- x = kmalloc(E1,E2);
> ++ x = kzalloc(E1,E2);
> +|
> +- x = (T *)kmalloc(E1,E2);
> + x = kzalloc(E1,E2);
> +|
> +- x = (T)kmalloc(E1,E2);
> ++ x = (T)kzalloc(E1,E2);
> +|
> +- x = vmalloc(E1);
> ++ x = vzalloc(E1);
> +|
> +- x = (T *)vmalloc(E1);
> ++ x = vzalloc(E1);
> +|
> +- x = (T)vmalloc(E1);
> ++ x = (T)vzalloc(E1);
> +|
> +- x = dma_alloc_coherent(E2,E1,E3,E4);
> ++ x = dma_zalloc_coherent(E2,E1,E3,E4);
> +|
> +- x = (T *)dma_alloc_coherent(E2,E1,E3,E4);
> ++ x = dma_zalloc_coherent(E2,E1,E3,E4);
> +|
> +- x = (T)dma_alloc_coherent(E2,E1,E3,E4);
> ++ x = (T)dma_zalloc_coherent(E2,E1,E3,E4);
> +|
> +- x = kmalloc_node(E1,E2,E3);
> ++ x = kzalloc_node(E1,E2,E3);
> +|
> +- x = (T *)kmalloc_node(E1,E2,E3);
> ++ x = kzalloc_node(E1,E2,E3);
> +|
> +- x = (T)kmalloc_node(E1,E2,E3);
> ++ x = (T)kzalloc_node(E1,E2,E3);
> +|
> +- x = kmem_cache_alloc(E3,E4);
> ++ x = kmem_cache_zalloc(E3,E4);
> +|
> +- x = (T *)kmem_cache_alloc(E3,E4);
> ++ x = kmem_cache_zalloc(E3,E4);
> +|
> +- x = (T)kmem_cache_alloc(E3,E4);
> ++ x = (T)kmem_cache_zalloc(E3,E4);
> +|
> +- x = kmem_alloc(E1,E2);
> ++ x = kmem_zalloc(E1,E2);
> +|
> +- x = (T *)kmem_alloc(E1,E2);
> ++ x = kmem_zalloc(E1,E2);
> +|
> +- x = (T)kmem_alloc(E1,E2);
> ++ x = (T)kmem_zalloc(E1,E2);
> +|
> +- x = devm_kmalloc(E2,E1,E3);
> ++ x = devm_kzalloc(E2,E1,E3);
> +|
> +- x = (T *)devm_kmalloc(E2,E1,E3);
> ++ x = devm_kzalloc(E2,E1,E3);
> +|
> +- x = (T)devm_kmalloc(E2,E1,E3);
> ++ x = (T)devm_kzalloc(E2,E1,E3);
> +|
> +- x = kvmalloc(E1,E2);
> ++ x = kvzalloc(E1,E2);
> +|
> +- x = (T *)kvmalloc(E1,E2);
> ++ x = kvzalloc(E1,E2);
> +|
> +- x = (T)kvmalloc(E1,E2);
> ++ x = (T)kvzalloc(E1,E2);
> +|
> +- x = pci_alloc_consistent(E2,E1,E3);
> ++ x = pci_zalloc_consistent(E2,E1,E3);
> +|
> +- x = (T *)pci_alloc_consistent(E2,E1,E3);
> ++ x = pci_zalloc_consistent(E2,E1,E3);
> +|
> +- x = (T)pci_alloc_consistent(E2,E1,E3);
> ++ x = (T)pci_zalloc_consistent(E2,E1,E3);
> +|
> +- x = kvmalloc_node(E1,E2,E3);
> ++ x = kvzalloc_node(E1,E2,E3);
> +|
> +- x = (T *)kvmalloc_node(E1,E2,E3);
> ++ x = kvzalloc_node(E1,E2,E3);
> +|
> +- x = (T)kvmalloc_node(E1,E2,E3);
> ++ x = (T)kvzalloc_node(E1,E2,E3);
> +)
> if ((x==NULL) || ...) S
> - memset((T2)x,0,E1);
>
> @@ -84,3 +177,273 @@ x << r.x;
>
> msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
> coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r1 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)vmalloc@p(E1);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r1.p;
> +x << r1.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r1.p;
> +x << r1.x;
> +@@
> +
> +msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r2 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2,E3,E4;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r2.p;
> +x << r2.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r2.p;
> +x << r2.x;
> +@@
> +
> +msg="WARNING: dma_zalloc_coherent should be used for %s, instead of dma_alloc_coherent/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r3 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2,E3;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)kmalloc_node@p(E1,E2,E3);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r3.p;
> +x << r3.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r3.p;
> +x << r3.x;
> +@@
> +
> +msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r4 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2,E3;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)kmem_cache_alloc@p(E2,E3);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r4.p;
> +x << r4.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r4.p;
> +x << r4.x;
> +@@
> +
> +msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r5 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)kmem_alloc@p(E1,E2);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r5.p;
> +x << r5.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r5.p;
> +x << r5.x;
> +@@
> +
> +msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r6 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2,E3;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)devm_kmalloc@p(E2,E1,E3);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r6.p;
> +x << r6.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r6.p;
> +x << r6.x;
> +@@
> +
> +msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r7 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)kvmalloc@p(E1,E2);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r7.p;
> +x << r7.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r7.p;
> +x << r7.x;
> +@@
> +
> +msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> +//-----------------------------------------------------------------
> +@r8 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2,E3;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)pci_alloc_consistent@p(E2,E1,E3);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r8.p;
> +x << r8.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r8.p;
> +x << r8.x;
> +@@
> +
> +msg="WARNING: pci_zalloc_consistent should be used for %s, instead of pci_alloc_consistent/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +//-----------------------------------------------------------------
> +@r9 depends on org || report@
> +type T, T2;
> +expression x;
> +expression E1,E2,E3;
> +statement S;
> +position p;
> +@@
> +
> + x = (T)kvmalloc_node@p(E1,E2,E3);
> + if ((x==NULL) || ...) S
> + memset((T2)x,0,E1);
> +
> +@script:python depends on org@
> +p << r9.p;
> +x << r9.x;
> +@@
> +
> +msg="%s" % (x)
> +msg_safe=msg.replace("[","@(").replace("]",")")
> +coccilib.org.print_todo(p[0], msg_safe)
> +
> +@script:python depends on report@
> +p << r9.p;
> +x << r9.x;
> +@@
> +
> +msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
> +coccilib.report.print_report(p[0], msg)
> +
> --
> 2.7.4
>
>
next prev parent reply other threads:[~2017-12-26 21:52 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-26 21:40 Himanshu Jha
2017-12-26 21:52 ` Julia Lawall [this message]
2017-12-29 17:22 ` Masahiro Yamada
2017-12-29 17:49 ` Julia Lawall
2018-01-02 14:25 ` Rename the SmPL script “kzalloc-….cocci”? SF Markus Elfring
2018-01-02 14:28 ` Julia Lawall
2018-01-02 14:38 ` SF Markus Elfring
2018-01-02 14:43 ` Julia Lawall
2018-01-02 15:00 ` SF Markus Elfring
2018-01-08 9:55 ` SF Markus Elfring
2018-01-03 11:55 ` [PATCH] Coccinelle: Rename the script for a transformation of memory allocations SF Markus Elfring
2018-01-03 12:02 ` Julia Lawall
2018-01-03 12:13 ` SF Markus Elfring
2018-01-03 12:17 ` Julia Lawall
2018-01-03 12:31 ` SF Markus Elfring
2018-01-03 12:40 ` Julia Lawall
2018-01-03 12:45 ` SF Markus Elfring
2018-01-04 8:36 ` SF Markus Elfring
2018-01-04 8:54 ` Julia Lawall
2018-01-04 9:43 ` SF Markus Elfring
2018-01-17 16:47 ` [v2] Coccinelle: zalloc-simple: Safer transformations with SmPL SF Markus Elfring
2018-01-19 16:14 ` Coccinelle: zalloc-simple: Checking consequences from the usage of at signs in Python strings SF Markus Elfring
2018-01-19 16:18 ` Julia Lawall
2018-01-19 16:43 ` SF Markus Elfring
2018-01-24 8:41 ` SF Markus Elfring
2018-01-31 17:28 ` [v2] Coccinelle: zalloc-simple: Delete function “kmem_cache_alloc” from SmPL rules SF Markus Elfring
2018-01-31 17:38 ` Julia Lawall
2018-01-31 17:53 ` SF Markus Elfring
2018-02-01 9:35 ` [PATCH] Coccinelle: zalloc-simple: Delete function "kmem_cache_alloc" " SF Markus Elfring
2018-02-01 9:40 ` Julia Lawall
2018-02-01 10:17 ` SF Markus Elfring
2018-02-01 10:27 ` Julia Lawall
2018-02-01 11:00 ` SF Markus Elfring
2018-02-03 7:22 ` Coccinelle: zalloc-simple: Checking consistency for " SF Markus Elfring
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=alpine.DEB.2.20.1712262252360.5715@hadrien \
--to=julia.lawall@lip6.fr \
--cc=Gilles.Muller@lip6.fr \
--cc=cocci@systeme.lip6.fr \
--cc=himanshujha199640@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=michal.lkml@markovi.net \
--cc=nicolas.palix@imag.fr \
--cc=yamada.masahiro@socionext.com \
/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
all inboxes | Powered by JetHome®