Merge tag 'drm-msm-fixes-2019-01-24' of git://people.freedesktop.org/~robclark/linux...
[sfrench/cifs-2.6.git] / scripts / coccinelle / api / alloc / zalloc-simple.cocci
1 ///
2 /// Use zeroing allocator rather than allocator followed by memset with 0
3 ///
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
7 ///
8 // Confidence: High
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
14 //
15 // Keywords: kmalloc, kzalloc
16 // Version min: < 2.6.12 kmalloc
17 // Version min:   2.6.14 kzalloc
18 //
19
20 virtual context
21 virtual patch
22 virtual org
23 virtual report
24
25 //----------------------------------------------------------
26 //  For context mode
27 //----------------------------------------------------------
28
29 @depends on context@
30 type T, T2;
31 expression x;
32 expression E1;
33 statement S;
34 @@
35
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
40 * memset((T2)x,0,E1);
41
42 //----------------------------------------------------------
43 //  For patch mode
44 //----------------------------------------------------------
45
46 @depends on patch@
47 type T, T2;
48 expression x;
49 expression E1,E2,E3,E4;
50 statement S;
51 @@
52
53 (
54 - x = kmalloc(E1,E2);
55 + x = kzalloc(E1,E2);
56 |
57 - x = (T *)kmalloc(E1,E2);
58 + x = kzalloc(E1,E2);
59 |
60 - x = (T)kmalloc(E1,E2);
61 + x = (T)kzalloc(E1,E2);
62 |
63 - x = vmalloc(E1);
64 + x = vzalloc(E1);
65 |
66 - x = (T *)vmalloc(E1);
67 + x = vzalloc(E1);
68 |
69 - x = (T)vmalloc(E1);
70 + x = (T)vzalloc(E1);
71 |
72 - x = kmalloc_node(E1,E2,E3);
73 + x = kzalloc_node(E1,E2,E3);
74 |
75 - x = (T *)kmalloc_node(E1,E2,E3);
76 + x = kzalloc_node(E1,E2,E3);
77 |
78 - x = (T)kmalloc_node(E1,E2,E3);
79 + x = (T)kzalloc_node(E1,E2,E3);
80 |
81 - x = kmem_cache_alloc(E3,E4);
82 + x = kmem_cache_zalloc(E3,E4);
83 |
84 - x = (T *)kmem_cache_alloc(E3,E4);
85 + x = kmem_cache_zalloc(E3,E4);
86 |
87 - x = (T)kmem_cache_alloc(E3,E4);
88 + x = (T)kmem_cache_zalloc(E3,E4);
89 |
90 - x = kmem_alloc(E1,E2);
91 + x = kmem_zalloc(E1,E2);
92 |
93 - x = (T *)kmem_alloc(E1,E2);
94 + x = kmem_zalloc(E1,E2);
95 |
96 - x = (T)kmem_alloc(E1,E2);
97 + x = (T)kmem_zalloc(E1,E2);
98 |
99 - x = devm_kmalloc(E2,E1,E3);
100 + x = devm_kzalloc(E2,E1,E3);
101 |
102 - x = (T *)devm_kmalloc(E2,E1,E3);
103 + x = devm_kzalloc(E2,E1,E3);
104 |
105 - x = (T)devm_kmalloc(E2,E1,E3);
106 + x = (T)devm_kzalloc(E2,E1,E3);
107 |
108 - x = kvmalloc(E1,E2);
109 + x = kvzalloc(E1,E2);
110 |
111 - x = (T *)kvmalloc(E1,E2);
112 + x = kvzalloc(E1,E2);
113 |
114 - x = (T)kvmalloc(E1,E2);
115 + x = (T)kvzalloc(E1,E2);
116 |
117 - x = kvmalloc_node(E1,E2,E3);
118 + x = kvzalloc_node(E1,E2,E3);
119 |
120 - x = (T *)kvmalloc_node(E1,E2,E3);
121 + x = kvzalloc_node(E1,E2,E3);
122 |
123 - x = (T)kvmalloc_node(E1,E2,E3);
124 + x = (T)kvzalloc_node(E1,E2,E3);
125 )
126   if ((x==NULL) || ...) S
127 - memset((T2)x,0,E1);
128
129 //----------------------------------------------------------
130 //  For org mode
131 //----------------------------------------------------------
132
133 @r depends on org || report@
134 type T, T2;
135 expression x;
136 expression E1,E2;
137 statement S;
138 position p;
139 @@
140
141  x = (T)kmalloc@p(E1,E2);
142  if ((x==NULL) || ...) S
143  memset((T2)x,0,E1);
144
145 @script:python depends on org@
146 p << r.p;
147 x << r.x;
148 @@
149
150 msg="%s" % (x)
151 msg_safe=msg.replace("[","@(").replace("]",")")
152 coccilib.org.print_todo(p[0], msg_safe)
153
154 @script:python depends on report@
155 p << r.p;
156 x << r.x;
157 @@
158
159 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
160 coccilib.report.print_report(p[0], msg)
161
162 //-----------------------------------------------------------------
163 @r1 depends on org || report@
164 type T, T2;
165 expression x;
166 expression E1;
167 statement S;
168 position p;
169 @@
170
171  x = (T)vmalloc@p(E1);
172  if ((x==NULL) || ...) S
173  memset((T2)x,0,E1);
174
175 @script:python depends on org@
176 p << r1.p;
177 x << r1.x;
178 @@
179
180 msg="%s" % (x)
181 msg_safe=msg.replace("[","@(").replace("]",")")
182 coccilib.org.print_todo(p[0], msg_safe)
183
184 @script:python depends on report@
185 p << r1.p;
186 x << r1.x;
187 @@
188
189 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
190 coccilib.report.print_report(p[0], msg)
191
192 //-----------------------------------------------------------------
193 @r2 depends on org || report@
194 type T, T2;
195 expression x;
196 expression E1,E2,E3,E4;
197 statement S;
198 position p;
199 @@
200
201  x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
202  if ((x==NULL) || ...) S
203  memset((T2)x,0,E1);
204
205 @script:python depends on org@
206 p << r2.p;
207 x << r2.x;
208 @@
209
210 msg="%s" % (x)
211 msg_safe=msg.replace("[","@(").replace("]",")")
212 coccilib.org.print_todo(p[0], msg_safe)
213
214 @script:python depends on report@
215 p << r2.p;
216 x << r2.x;
217 @@
218
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)
221
222 //-----------------------------------------------------------------
223 @r3 depends on org || report@
224 type T, T2;
225 expression x;
226 expression E1,E2,E3;
227 statement S;
228 position p;
229 @@
230
231  x = (T)kmalloc_node@p(E1,E2,E3);
232  if ((x==NULL) || ...) S
233  memset((T2)x,0,E1);
234
235 @script:python depends on org@
236 p << r3.p;
237 x << r3.x;
238 @@
239
240 msg="%s" % (x)
241 msg_safe=msg.replace("[","@(").replace("]",")")
242 coccilib.org.print_todo(p[0], msg_safe)
243
244 @script:python depends on report@
245 p << r3.p;
246 x << r3.x;
247 @@
248
249 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
250 coccilib.report.print_report(p[0], msg)
251
252 //-----------------------------------------------------------------
253 @r4 depends on org || report@
254 type T, T2;
255 expression x;
256 expression E1,E2,E3;
257 statement S;
258 position p;
259 @@
260
261  x = (T)kmem_cache_alloc@p(E2,E3);
262  if ((x==NULL) || ...) S
263  memset((T2)x,0,E1);
264
265 @script:python depends on org@
266 p << r4.p;
267 x << r4.x;
268 @@
269
270 msg="%s" % (x)
271 msg_safe=msg.replace("[","@(").replace("]",")")
272 coccilib.org.print_todo(p[0], msg_safe)
273
274 @script:python depends on report@
275 p << r4.p;
276 x << r4.x;
277 @@
278
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)
281
282 //-----------------------------------------------------------------
283 @r5 depends on org || report@
284 type T, T2;
285 expression x;
286 expression E1,E2;
287 statement S;
288 position p;
289 @@
290
291  x = (T)kmem_alloc@p(E1,E2);
292  if ((x==NULL) || ...) S
293  memset((T2)x,0,E1);
294
295 @script:python depends on org@
296 p << r5.p;
297 x << r5.x;
298 @@
299
300 msg="%s" % (x)
301 msg_safe=msg.replace("[","@(").replace("]",")")
302 coccilib.org.print_todo(p[0], msg_safe)
303
304 @script:python depends on report@
305 p << r5.p;
306 x << r5.x;
307 @@
308
309 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
310 coccilib.report.print_report(p[0], msg)
311
312 //-----------------------------------------------------------------
313 @r6 depends on org || report@
314 type T, T2;
315 expression x;
316 expression E1,E2,E3;
317 statement S;
318 position p;
319 @@
320
321  x = (T)devm_kmalloc@p(E2,E1,E3);
322  if ((x==NULL) || ...) S
323  memset((T2)x,0,E1);
324
325 @script:python depends on org@
326 p << r6.p;
327 x << r6.x;
328 @@
329
330 msg="%s" % (x)
331 msg_safe=msg.replace("[","@(").replace("]",")")
332 coccilib.org.print_todo(p[0], msg_safe)
333
334 @script:python depends on report@
335 p << r6.p;
336 x << r6.x;
337 @@
338
339 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
340 coccilib.report.print_report(p[0], msg)
341
342 //-----------------------------------------------------------------
343 @r7 depends on org || report@
344 type T, T2;
345 expression x;
346 expression E1,E2;
347 statement S;
348 position p;
349 @@
350
351  x = (T)kvmalloc@p(E1,E2);
352  if ((x==NULL) || ...) S
353  memset((T2)x,0,E1);
354
355 @script:python depends on org@
356 p << r7.p;
357 x << r7.x;
358 @@
359
360 msg="%s" % (x)
361 msg_safe=msg.replace("[","@(").replace("]",")")
362 coccilib.org.print_todo(p[0], msg_safe)
363
364 @script:python depends on report@
365 p << r7.p;
366 x << r7.x;
367 @@
368
369 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
370 coccilib.report.print_report(p[0], msg)
371
372 //-----------------------------------------------------------------
373 @r9 depends on org || report@
374 type T, T2;
375 expression x;
376 expression E1,E2,E3;
377 statement S;
378 position p;
379 @@
380
381  x = (T)kvmalloc_node@p(E1,E2,E3);
382  if ((x==NULL) || ...) S
383  memset((T2)x,0,E1);
384
385 @script:python depends on org@
386 p << r9.p;
387 x << r9.x;
388 @@
389
390 msg="%s" % (x)
391 msg_safe=msg.replace("[","@(").replace("]",")")
392 coccilib.org.print_todo(p[0], msg_safe)
393
394 @script:python depends on report@
395 p << r9.p;
396 x << r9.x;
397 @@
398
399 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
400 coccilib.report.print_report(p[0], msg)