1 // SPDX-License-Identifier: GPL-2.0-only
3 /// Use zeroing allocator rather than allocator followed by memset with 0
5 /// This considers some simple cases that are common and easy to validate
6 /// Note in particular that there are no ...s in the rule, so all of the
7 /// matched code has to be contiguous
10 // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU.
11 // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6.
12 // Copyright: (C) 2017 Himanshu Jha
13 // URL: http://coccinelle.lip6.fr/rules/kzalloc.html
14 // Options: --no-includes --include-headers
16 // Keywords: kmalloc, kzalloc
17 // Version min: < 2.6.12 kmalloc
18 // Version min: 2.6.14 kzalloc
26 //----------------------------------------------------------
28 //----------------------------------------------------------
37 * x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
38 kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
39 devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|kvmalloc_node(E1,...)\);
40 if ((x==NULL) || ...) S
43 //----------------------------------------------------------
45 //----------------------------------------------------------
50 expression E1,E2,E3,E4;
58 - x = (T *)kmalloc(E1,E2);
61 - x = (T)kmalloc(E1,E2);
62 + x = (T)kzalloc(E1,E2);
67 - x = (T *)vmalloc(E1);
73 - x = kmalloc_node(E1,E2,E3);
74 + x = kzalloc_node(E1,E2,E3);
76 - x = (T *)kmalloc_node(E1,E2,E3);
77 + x = kzalloc_node(E1,E2,E3);
79 - x = (T)kmalloc_node(E1,E2,E3);
80 + x = (T)kzalloc_node(E1,E2,E3);
82 - x = kmem_cache_alloc(E3,E4);
83 + x = kmem_cache_zalloc(E3,E4);
85 - x = (T *)kmem_cache_alloc(E3,E4);
86 + x = kmem_cache_zalloc(E3,E4);
88 - x = (T)kmem_cache_alloc(E3,E4);
89 + x = (T)kmem_cache_zalloc(E3,E4);
91 - x = kmem_alloc(E1,E2);
92 + x = kmem_zalloc(E1,E2);
94 - x = (T *)kmem_alloc(E1,E2);
95 + x = kmem_zalloc(E1,E2);
97 - x = (T)kmem_alloc(E1,E2);
98 + x = (T)kmem_zalloc(E1,E2);
100 - x = devm_kmalloc(E2,E1,E3);
101 + x = devm_kzalloc(E2,E1,E3);
103 - x = (T *)devm_kmalloc(E2,E1,E3);
104 + x = devm_kzalloc(E2,E1,E3);
106 - x = (T)devm_kmalloc(E2,E1,E3);
107 + x = (T)devm_kzalloc(E2,E1,E3);
109 - x = kvmalloc(E1,E2);
110 + x = kvzalloc(E1,E2);
112 - x = (T *)kvmalloc(E1,E2);
113 + x = kvzalloc(E1,E2);
115 - x = (T)kvmalloc(E1,E2);
116 + x = (T)kvzalloc(E1,E2);
118 - x = kvmalloc_node(E1,E2,E3);
119 + x = kvzalloc_node(E1,E2,E3);
121 - x = (T *)kvmalloc_node(E1,E2,E3);
122 + x = kvzalloc_node(E1,E2,E3);
124 - x = (T)kvmalloc_node(E1,E2,E3);
125 + x = (T)kvzalloc_node(E1,E2,E3);
127 if ((x==NULL) || ...) S
128 - memset((T2)x,0,E1);
130 //----------------------------------------------------------
132 //----------------------------------------------------------
134 @r depends on org || report@
142 x = (T)kmalloc@p(E1,E2);
143 if ((x==NULL) || ...) S
146 @script:python depends on org@
152 msg_safe=msg.replace("[","@(").replace("]",")")
153 coccilib.org.print_todo(p[0], msg_safe)
155 @script:python depends on report@
160 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
161 coccilib.report.print_report(p[0], msg)
163 //-----------------------------------------------------------------
164 @r1 depends on org || report@
172 x = (T)vmalloc@p(E1);
173 if ((x==NULL) || ...) S
176 @script:python depends on org@
182 msg_safe=msg.replace("[","@(").replace("]",")")
183 coccilib.org.print_todo(p[0], msg_safe)
185 @script:python depends on report@
190 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
191 coccilib.report.print_report(p[0], msg)
193 //-----------------------------------------------------------------
194 @r2 depends on org || report@
197 expression E1,E2,E3,E4;
202 x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
203 if ((x==NULL) || ...) S
206 @script:python depends on org@
212 msg_safe=msg.replace("[","@(").replace("]",")")
213 coccilib.org.print_todo(p[0], msg_safe)
215 @script:python depends on report@
220 msg="WARNING: dma_alloc_coherent use in %s already zeroes out memory, so memset is not needed" % (x)
221 coccilib.report.print_report(p[0], msg)
223 //-----------------------------------------------------------------
224 @r3 depends on org || report@
232 x = (T)kmalloc_node@p(E1,E2,E3);
233 if ((x==NULL) || ...) S
236 @script:python depends on org@
242 msg_safe=msg.replace("[","@(").replace("]",")")
243 coccilib.org.print_todo(p[0], msg_safe)
245 @script:python depends on report@
250 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
251 coccilib.report.print_report(p[0], msg)
253 //-----------------------------------------------------------------
254 @r4 depends on org || report@
262 x = (T)kmem_cache_alloc@p(E2,E3);
263 if ((x==NULL) || ...) S
266 @script:python depends on org@
272 msg_safe=msg.replace("[","@(").replace("]",")")
273 coccilib.org.print_todo(p[0], msg_safe)
275 @script:python depends on report@
280 msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
281 coccilib.report.print_report(p[0], msg)
283 //-----------------------------------------------------------------
284 @r5 depends on org || report@
292 x = (T)kmem_alloc@p(E1,E2);
293 if ((x==NULL) || ...) S
296 @script:python depends on org@
302 msg_safe=msg.replace("[","@(").replace("]",")")
303 coccilib.org.print_todo(p[0], msg_safe)
305 @script:python depends on report@
310 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
311 coccilib.report.print_report(p[0], msg)
313 //-----------------------------------------------------------------
314 @r6 depends on org || report@
322 x = (T)devm_kmalloc@p(E2,E1,E3);
323 if ((x==NULL) || ...) S
326 @script:python depends on org@
332 msg_safe=msg.replace("[","@(").replace("]",")")
333 coccilib.org.print_todo(p[0], msg_safe)
335 @script:python depends on report@
340 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
341 coccilib.report.print_report(p[0], msg)
343 //-----------------------------------------------------------------
344 @r7 depends on org || report@
352 x = (T)kvmalloc@p(E1,E2);
353 if ((x==NULL) || ...) S
356 @script:python depends on org@
362 msg_safe=msg.replace("[","@(").replace("]",")")
363 coccilib.org.print_todo(p[0], msg_safe)
365 @script:python depends on report@
370 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
371 coccilib.report.print_report(p[0], msg)
373 //-----------------------------------------------------------------
374 @r9 depends on org || report@
382 x = (T)kvmalloc_node@p(E1,E2,E3);
383 if ((x==NULL) || ...) S
386 @script:python depends on org@
392 msg_safe=msg.replace("[","@(").replace("]",")")
393 coccilib.org.print_todo(p[0], msg_safe)
395 @script:python depends on report@
400 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
401 coccilib.report.print_report(p[0], msg)