1 // SPDX-License-Identifier: GPL-2.0-only
3 /// Find if/else condition with kmalloc/vmalloc calls.
4 /// Suggest to use kvmalloc instead. Same for kvfree.
7 // Copyright: (C) 2020 Denis Efremov ISPRAS
8 // Options: --no-includes --include-headers
18 filter = frozenset(['kvfree'])
21 return not (filter & {el.current_element for el in p})
23 @kvmalloc depends on !patch@
24 expression E, E1, size;
26 binary operator cmp = {<=, <, ==, >, >=};
33 * if (size cmp E1 || ...)@p {
35 * E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
36 * kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
37 * (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...)
41 * E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
45 * E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
46 * kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
47 * (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...)
53 * E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
57 * T x = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
58 * kmalloc_array\|kmalloc_array_node\|kcalloc_node\)
59 * (..., size, \(flags\|GFP_KERNEL\|\(GFP_KERNEL\|flags\)|__GFP_NOWARN\), ...);
65 * x = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
70 @kvfree depends on !patch@
72 position p : script:python() { relevant(p) };
75 * if (is_vmalloc_addr(E))@p {
80 ... when != krealloc(E, ...)
82 * \(kfree\|kzfree\)(E)
87 expression E, E1, size, node;
88 binary operator cmp = {<=, <, ==, >, >=};
95 - E = kmalloc(size, flags);
98 + E = kvmalloc(size, flags);
101 - E = kmalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
104 + E = kvmalloc(size, GFP_KERNEL);
106 - E = kmalloc(size, flags | __GFP_NOWARN);
109 + E = kvmalloc(size, flags);
111 - E = kmalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
114 + E = kvmalloc(size, GFP_KERNEL);
116 - T x = kmalloc(size, flags | __GFP_NOWARN);
119 + T x = kvmalloc(size, flags);
121 - T x = kmalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
124 + T x = kvmalloc(size, GFP_KERNEL);
127 - E = kzalloc(size, flags);
130 + E = kvzalloc(size, flags);
133 - E = kzalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
136 + E = kvzalloc(size, GFP_KERNEL);
138 - E = kzalloc(size, flags | __GFP_NOWARN);
141 + E = kvzalloc(size, flags);
143 - E = kzalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
146 + E = kvzalloc(size, GFP_KERNEL);
148 - T x = kzalloc(size, flags | __GFP_NOWARN);
151 + T x = kvzalloc(size, flags);
153 - T x = kzalloc(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\));
156 + T x = kvzalloc(size, GFP_KERNEL);
159 - E = kmalloc_node(size, flags, node);
161 - E = vmalloc_node(size, node);
162 + E = kvmalloc_node(size, flags, node);
165 - E = kmalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
167 - E = vmalloc_node(size, node);
168 + E = kvmalloc_node(size, GFP_KERNEL, node);
170 - E = kmalloc_node(size, flags | __GFP_NOWARN, node);
172 - E = vmalloc_node(size, node);
173 + E = kvmalloc_node(size, flags, node);
175 - E = kmalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
177 - E = vmalloc_node(size, node);
178 + E = kvmalloc_node(size, GFP_KERNEL, node);
180 - T x = kmalloc_node(size, flags | __GFP_NOWARN, node);
182 - x = vmalloc_node(size, node);
183 + T x = kvmalloc_node(size, flags, node);
185 - T x = kmalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
187 - x = vmalloc_node(size, node);
188 + T x = kvmalloc_node(size, GFP_KERNEL, node);
191 - E = kvzalloc_node(size, flags, node);
193 - E = vzalloc_node(size, node);
194 + E = kvzalloc_node(size, flags, node);
197 - E = kvzalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
199 - E = vzalloc_node(size, node);
200 + E = kvzalloc_node(size, GFP_KERNEL, node);
202 - E = kvzalloc_node(size, flags | __GFP_NOWARN, node);
204 - E = vzalloc_node(size, node);
205 + E = kvzalloc_node(size, flags, node);
207 - E = kvzalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
209 - E = vzalloc_node(size, node);
210 + E = kvzalloc_node(size, GFP_KERNEL, node);
212 - T x = kvzalloc_node(size, flags | __GFP_NOWARN, node);
214 - x = vzalloc_node(size, node);
215 + T x = kvzalloc_node(size, flags, node);
217 - T x = kvzalloc_node(size, \(GFP_KERNEL\|GFP_KERNEL|__GFP_NOWARN\), node);
219 - x = vzalloc_node(size, node);
220 + T x = kvzalloc_node(size, GFP_KERNEL, node);
225 position p : script:python() { relevant(p) };
228 - if (is_vmalloc_addr(E))@p
234 @script: python depends on report@
238 coccilib.report.print_report(p[0], "WARNING opportunity for kvmalloc")
240 @script: python depends on org@
244 coccilib.org.print_todo(p[0], "WARNING opportunity for kvmalloc")
246 @script: python depends on report@
250 coccilib.report.print_report(p[0], "WARNING opportunity for kvfree")
252 @script: python depends on org@
256 coccilib.org.print_todo(p[0], "WARNING opportunity for kvfree")