2 /// Use zeroing allocator rather than allocator followed by memset with 0
4 /// This considers some simple cases that are common and easy to validate
5 /// Note in particular that there are no ...s in the rule, so all of the
6 /// matched code has to be contiguous
9 // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU. GPLv2.
10 // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6. GPLv2.
11 // Copyright: (C) 2017 Himanshu Jha GPLv2.
12 // URL: http://coccinelle.lip6.fr/rules/kzalloc.html
13 // Options: --no-includes --include-headers
15 // Keywords: kmalloc, kzalloc
16 // Version min: < 2.6.12 kmalloc
17 // Version min: 2.6.14 kzalloc
25 //----------------------------------------------------------
27 //----------------------------------------------------------
36 * x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
37 kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
38 devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|kvmalloc_node(E1,...)\);
39 if ((x==NULL) || ...) S
42 //----------------------------------------------------------
44 //----------------------------------------------------------
49 expression E1,E2,E3,E4;
57 - x = (T *)kmalloc(E1,E2);
60 - x = (T)kmalloc(E1,E2);
61 + x = (T)kzalloc(E1,E2);
66 - x = (T *)vmalloc(E1);
72 - x = kmalloc_node(E1,E2,E3);
73 + x = kzalloc_node(E1,E2,E3);
75 - x = (T *)kmalloc_node(E1,E2,E3);
76 + x = kzalloc_node(E1,E2,E3);
78 - x = (T)kmalloc_node(E1,E2,E3);
79 + x = (T)kzalloc_node(E1,E2,E3);
81 - x = kmem_cache_alloc(E3,E4);
82 + x = kmem_cache_zalloc(E3,E4);
84 - x = (T *)kmem_cache_alloc(E3,E4);
85 + x = kmem_cache_zalloc(E3,E4);
87 - x = (T)kmem_cache_alloc(E3,E4);
88 + x = (T)kmem_cache_zalloc(E3,E4);
90 - x = kmem_alloc(E1,E2);
91 + x = kmem_zalloc(E1,E2);
93 - x = (T *)kmem_alloc(E1,E2);
94 + x = kmem_zalloc(E1,E2);
96 - x = (T)kmem_alloc(E1,E2);
97 + x = (T)kmem_zalloc(E1,E2);
99 - x = devm_kmalloc(E2,E1,E3);
100 + x = devm_kzalloc(E2,E1,E3);
102 - x = (T *)devm_kmalloc(E2,E1,E3);
103 + x = devm_kzalloc(E2,E1,E3);
105 - x = (T)devm_kmalloc(E2,E1,E3);
106 + x = (T)devm_kzalloc(E2,E1,E3);
108 - x = kvmalloc(E1,E2);
109 + x = kvzalloc(E1,E2);
111 - x = (T *)kvmalloc(E1,E2);
112 + x = kvzalloc(E1,E2);
114 - x = (T)kvmalloc(E1,E2);
115 + x = (T)kvzalloc(E1,E2);
117 - x = kvmalloc_node(E1,E2,E3);
118 + x = kvzalloc_node(E1,E2,E3);
120 - x = (T *)kvmalloc_node(E1,E2,E3);
121 + x = kvzalloc_node(E1,E2,E3);
123 - x = (T)kvmalloc_node(E1,E2,E3);
124 + x = (T)kvzalloc_node(E1,E2,E3);
126 if ((x==NULL) || ...) S
127 - memset((T2)x,0,E1);
129 //----------------------------------------------------------
131 //----------------------------------------------------------
133 @r depends on org || report@
141 x = (T)kmalloc@p(E1,E2);
142 if ((x==NULL) || ...) S
145 @script:python depends on org@
151 msg_safe=msg.replace("[","@(").replace("]",")")
152 coccilib.org.print_todo(p[0], msg_safe)
154 @script:python depends on report@
159 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
160 coccilib.report.print_report(p[0], msg)
162 //-----------------------------------------------------------------
163 @r1 depends on org || report@
171 x = (T)vmalloc@p(E1);
172 if ((x==NULL) || ...) S
175 @script:python depends on org@
181 msg_safe=msg.replace("[","@(").replace("]",")")
182 coccilib.org.print_todo(p[0], msg_safe)
184 @script:python depends on report@
189 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
190 coccilib.report.print_report(p[0], msg)
192 //-----------------------------------------------------------------
193 @r2 depends on org || report@
196 expression E1,E2,E3,E4;
201 x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
202 if ((x==NULL) || ...) S
205 @script:python depends on org@
211 msg_safe=msg.replace("[","@(").replace("]",")")
212 coccilib.org.print_todo(p[0], msg_safe)
214 @script:python depends on report@
219 msg="WARNING: dma_alloc_coherent use in %s already zeroes out memory, so memset is not needed" % (x)
220 coccilib.report.print_report(p[0], msg)
222 //-----------------------------------------------------------------
223 @r3 depends on org || report@
231 x = (T)kmalloc_node@p(E1,E2,E3);
232 if ((x==NULL) || ...) S
235 @script:python depends on org@
241 msg_safe=msg.replace("[","@(").replace("]",")")
242 coccilib.org.print_todo(p[0], msg_safe)
244 @script:python depends on report@
249 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
250 coccilib.report.print_report(p[0], msg)
252 //-----------------------------------------------------------------
253 @r4 depends on org || report@
261 x = (T)kmem_cache_alloc@p(E2,E3);
262 if ((x==NULL) || ...) S
265 @script:python depends on org@
271 msg_safe=msg.replace("[","@(").replace("]",")")
272 coccilib.org.print_todo(p[0], msg_safe)
274 @script:python depends on report@
279 msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
280 coccilib.report.print_report(p[0], msg)
282 //-----------------------------------------------------------------
283 @r5 depends on org || report@
291 x = (T)kmem_alloc@p(E1,E2);
292 if ((x==NULL) || ...) S
295 @script:python depends on org@
301 msg_safe=msg.replace("[","@(").replace("]",")")
302 coccilib.org.print_todo(p[0], msg_safe)
304 @script:python depends on report@
309 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
310 coccilib.report.print_report(p[0], msg)
312 //-----------------------------------------------------------------
313 @r6 depends on org || report@
321 x = (T)devm_kmalloc@p(E2,E1,E3);
322 if ((x==NULL) || ...) S
325 @script:python depends on org@
331 msg_safe=msg.replace("[","@(").replace("]",")")
332 coccilib.org.print_todo(p[0], msg_safe)
334 @script:python depends on report@
339 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
340 coccilib.report.print_report(p[0], msg)
342 //-----------------------------------------------------------------
343 @r7 depends on org || report@
351 x = (T)kvmalloc@p(E1,E2);
352 if ((x==NULL) || ...) S
355 @script:python depends on org@
361 msg_safe=msg.replace("[","@(").replace("]",")")
362 coccilib.org.print_todo(p[0], msg_safe)
364 @script:python depends on report@
369 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
370 coccilib.report.print_report(p[0], msg)
372 //-----------------------------------------------------------------
373 @r9 depends on org || report@
381 x = (T)kvmalloc_node@p(E1,E2,E3);
382 if ((x==NULL) || ...) S
385 @script:python depends on org@
391 msg_safe=msg.replace("[","@(").replace("]",")")
392 coccilib.org.print_todo(p[0], msg_safe)
394 @script:python depends on report@
399 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
400 coccilib.report.print_report(p[0], msg)