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