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: https://coccinelle.gitlabpages.inria.fr/website
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);
133 expression E1,E2,E3,E4;
136 x = (T)dma_alloc_coherent(E1, E2, E3, E4);
137 if ((x==NULL) || ...) S
138 - memset((T2)x, 0, E2);
140 //----------------------------------------------------------
142 //----------------------------------------------------------
144 @r depends on org || report@
152 x = (T)kmalloc@p(E1,E2);
153 if ((x==NULL) || ...) S
156 @script:python depends on org@
162 msg_safe=msg.replace("[","@(").replace("]",")")
163 coccilib.org.print_todo(p[0], msg_safe)
165 @script:python depends on report@
170 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
171 coccilib.report.print_report(p[0], msg)
173 //-----------------------------------------------------------------
174 @r1 depends on org || report@
182 x = (T)vmalloc@p(E1);
183 if ((x==NULL) || ...) S
186 @script:python depends on org@
192 msg_safe=msg.replace("[","@(").replace("]",")")
193 coccilib.org.print_todo(p[0], msg_safe)
195 @script:python depends on report@
200 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
201 coccilib.report.print_report(p[0], msg)
203 //-----------------------------------------------------------------
204 @r2 depends on org || report@
207 expression E1,E2,E3,E4;
212 x = (T)dma_alloc_coherent@p(E1,E2,E3,E4);
213 if ((x==NULL) || ...) S
216 @script:python depends on org@
222 msg_safe=msg.replace("[","@(").replace("]",")")
223 coccilib.org.print_todo(p[0], msg_safe)
225 @script:python depends on report@
230 msg="WARNING: dma_alloc_coherent used in %s already zeroes out memory, so memset is not needed" % (x)
231 coccilib.report.print_report(p[0], msg)
233 //-----------------------------------------------------------------
234 @r3 depends on org || report@
242 x = (T)kmalloc_node@p(E1,E2,E3);
243 if ((x==NULL) || ...) S
246 @script:python depends on org@
252 msg_safe=msg.replace("[","@(").replace("]",")")
253 coccilib.org.print_todo(p[0], msg_safe)
255 @script:python depends on report@
260 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
261 coccilib.report.print_report(p[0], msg)
263 //-----------------------------------------------------------------
264 @r4 depends on org || report@
272 x = (T)kmem_cache_alloc@p(E2,E3);
273 if ((x==NULL) || ...) S
276 @script:python depends on org@
282 msg_safe=msg.replace("[","@(").replace("]",")")
283 coccilib.org.print_todo(p[0], msg_safe)
285 @script:python depends on report@
290 msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
291 coccilib.report.print_report(p[0], msg)
293 //-----------------------------------------------------------------
294 @r5 depends on org || report@
302 x = (T)kmem_alloc@p(E1,E2);
303 if ((x==NULL) || ...) S
306 @script:python depends on org@
312 msg_safe=msg.replace("[","@(").replace("]",")")
313 coccilib.org.print_todo(p[0], msg_safe)
315 @script:python depends on report@
320 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
321 coccilib.report.print_report(p[0], msg)
323 //-----------------------------------------------------------------
324 @r6 depends on org || report@
332 x = (T)devm_kmalloc@p(E2,E1,E3);
333 if ((x==NULL) || ...) S
336 @script:python depends on org@
342 msg_safe=msg.replace("[","@(").replace("]",")")
343 coccilib.org.print_todo(p[0], msg_safe)
345 @script:python depends on report@
350 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
351 coccilib.report.print_report(p[0], msg)
353 //-----------------------------------------------------------------
354 @r7 depends on org || report@
362 x = (T)kvmalloc@p(E1,E2);
363 if ((x==NULL) || ...) S
366 @script:python depends on org@
372 msg_safe=msg.replace("[","@(").replace("]",")")
373 coccilib.org.print_todo(p[0], msg_safe)
375 @script:python depends on report@
380 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
381 coccilib.report.print_report(p[0], msg)
383 //-----------------------------------------------------------------
384 @r9 depends on org || report@
392 x = (T)kvmalloc_node@p(E1,E2,E3);
393 if ((x==NULL) || ...) S
396 @script:python depends on org@
402 msg_safe=msg.replace("[","@(").replace("]",")")
403 coccilib.org.print_todo(p[0], msg_safe)
405 @script:python depends on report@
410 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
411 coccilib.report.print_report(p[0], msg)