Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Coccinelle: kzalloc-simple: Add all zero allocating functions

There are many instances where memory is allocated using regular
allocator functions immediately followed by setting the allocated
memory to 0 value using memset.

We already have zero memory allocator functions to set the memory to
0 value instead of manually setting it using memset.

Therefore, use zero memory allocating functions instead of regular
memory allocators followed by memset 0 to remove redundant memset and
make the code more cleaner and also reduce the code size.

Signed-off-by: Himanshu Jha <himanshujha199640@gmail.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

authored by

Himanshu Jha and committed by
Masahiro Yamada
5e2d9da5 262dad68

+367 -5
+367 -5
scripts/coccinelle/api/alloc/kzalloc-simple.cocci
··· 1 1 /// 2 - /// Use kzalloc rather than kmalloc followed by memset with 0 2 + /// Use zeroing allocator rather than allocator followed by memset with 0 3 3 /// 4 4 /// This considers some simple cases that are common and easy to validate 5 5 /// Note in particular that there are no ...s in the rule, so all of the ··· 8 8 // Confidence: High 9 9 // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU. GPLv2. 10 10 // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6. GPLv2. 11 + // Copyright: (C) 2017 Himanshu Jha GPLv2. 11 12 // URL: http://coccinelle.lip6.fr/rules/kzalloc.html 12 13 // Options: --no-includes --include-headers 13 14 // ··· 29 28 @depends on context@ 30 29 type T, T2; 31 30 expression x; 32 - expression E1,E2; 31 + expression E1; 33 32 statement S; 34 33 @@ 35 34 36 - * x = (T)kmalloc(E1,E2); 35 + * x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\| 36 + kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\| 37 + devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|pci_alloc_consistent(...,E1,...)\| 38 + kvmalloc_node(E1,...)\); 37 39 if ((x==NULL) || ...) S 38 40 * memset((T2)x,0,E1); 39 41 ··· 47 43 @depends on patch@ 48 44 type T, T2; 49 45 expression x; 50 - expression E1,E2; 46 + expression E1,E2,E3,E4; 51 47 statement S; 52 48 @@ 53 49 54 - - x = (T)kmalloc(E1,E2); 50 + ( 51 + - x = kmalloc(E1,E2); 55 52 + x = kzalloc(E1,E2); 53 + | 54 + - x = (T *)kmalloc(E1,E2); 55 + + x = kzalloc(E1,E2); 56 + | 57 + - x = (T)kmalloc(E1,E2); 58 + + x = (T)kzalloc(E1,E2); 59 + | 60 + - x = vmalloc(E1); 61 + + x = vzalloc(E1); 62 + | 63 + - x = (T *)vmalloc(E1); 64 + + x = vzalloc(E1); 65 + | 66 + - x = (T)vmalloc(E1); 67 + + x = (T)vzalloc(E1); 68 + | 69 + - x = dma_alloc_coherent(E2,E1,E3,E4); 70 + + x = dma_zalloc_coherent(E2,E1,E3,E4); 71 + | 72 + - x = (T *)dma_alloc_coherent(E2,E1,E3,E4); 73 + + x = dma_zalloc_coherent(E2,E1,E3,E4); 74 + | 75 + - x = (T)dma_alloc_coherent(E2,E1,E3,E4); 76 + + x = (T)dma_zalloc_coherent(E2,E1,E3,E4); 77 + | 78 + - x = kmalloc_node(E1,E2,E3); 79 + + x = kzalloc_node(E1,E2,E3); 80 + | 81 + - x = (T *)kmalloc_node(E1,E2,E3); 82 + + x = kzalloc_node(E1,E2,E3); 83 + | 84 + - x = (T)kmalloc_node(E1,E2,E3); 85 + + x = (T)kzalloc_node(E1,E2,E3); 86 + | 87 + - x = kmem_cache_alloc(E3,E4); 88 + + x = kmem_cache_zalloc(E3,E4); 89 + | 90 + - x = (T *)kmem_cache_alloc(E3,E4); 91 + + x = kmem_cache_zalloc(E3,E4); 92 + | 93 + - x = (T)kmem_cache_alloc(E3,E4); 94 + + x = (T)kmem_cache_zalloc(E3,E4); 95 + | 96 + - x = kmem_alloc(E1,E2); 97 + + x = kmem_zalloc(E1,E2); 98 + | 99 + - x = (T *)kmem_alloc(E1,E2); 100 + + x = kmem_zalloc(E1,E2); 101 + | 102 + - x = (T)kmem_alloc(E1,E2); 103 + + x = (T)kmem_zalloc(E1,E2); 104 + | 105 + - x = devm_kmalloc(E2,E1,E3); 106 + + x = devm_kzalloc(E2,E1,E3); 107 + | 108 + - x = (T *)devm_kmalloc(E2,E1,E3); 109 + + x = devm_kzalloc(E2,E1,E3); 110 + | 111 + - x = (T)devm_kmalloc(E2,E1,E3); 112 + + x = (T)devm_kzalloc(E2,E1,E3); 113 + | 114 + - x = kvmalloc(E1,E2); 115 + + x = kvzalloc(E1,E2); 116 + | 117 + - x = (T *)kvmalloc(E1,E2); 118 + + x = kvzalloc(E1,E2); 119 + | 120 + - x = (T)kvmalloc(E1,E2); 121 + + x = (T)kvzalloc(E1,E2); 122 + | 123 + - x = pci_alloc_consistent(E2,E1,E3); 124 + + x = pci_zalloc_consistent(E2,E1,E3); 125 + | 126 + - x = (T *)pci_alloc_consistent(E2,E1,E3); 127 + + x = pci_zalloc_consistent(E2,E1,E3); 128 + | 129 + - x = (T)pci_alloc_consistent(E2,E1,E3); 130 + + x = (T)pci_zalloc_consistent(E2,E1,E3); 131 + | 132 + - x = kvmalloc_node(E1,E2,E3); 133 + + x = kvzalloc_node(E1,E2,E3); 134 + | 135 + - x = (T *)kvmalloc_node(E1,E2,E3); 136 + + x = kvzalloc_node(E1,E2,E3); 137 + | 138 + - x = (T)kvmalloc_node(E1,E2,E3); 139 + + x = (T)kvzalloc_node(E1,E2,E3); 140 + ) 56 141 if ((x==NULL) || ...) S 57 142 - memset((T2)x,0,E1); 58 143 ··· 176 83 @@ 177 84 178 85 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x) 86 + coccilib.report.print_report(p[0], msg) 87 + 88 + //----------------------------------------------------------------- 89 + @r1 depends on org || report@ 90 + type T, T2; 91 + expression x; 92 + expression E1; 93 + statement S; 94 + position p; 95 + @@ 96 + 97 + x = (T)vmalloc@p(E1); 98 + if ((x==NULL) || ...) S 99 + memset((T2)x,0,E1); 100 + 101 + @script:python depends on org@ 102 + p << r1.p; 103 + x << r1.x; 104 + @@ 105 + 106 + msg="%s" % (x) 107 + msg_safe=msg.replace("[","@(").replace("]",")") 108 + coccilib.org.print_todo(p[0], msg_safe) 109 + 110 + @script:python depends on report@ 111 + p << r1.p; 112 + x << r1.x; 113 + @@ 114 + 115 + msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x) 116 + coccilib.report.print_report(p[0], msg) 117 + 118 + //----------------------------------------------------------------- 119 + @r2 depends on org || report@ 120 + type T, T2; 121 + expression x; 122 + expression E1,E2,E3,E4; 123 + statement S; 124 + position p; 125 + @@ 126 + 127 + x = (T)dma_alloc_coherent@p(E2,E1,E3,E4); 128 + if ((x==NULL) || ...) S 129 + memset((T2)x,0,E1); 130 + 131 + @script:python depends on org@ 132 + p << r2.p; 133 + x << r2.x; 134 + @@ 135 + 136 + msg="%s" % (x) 137 + msg_safe=msg.replace("[","@(").replace("]",")") 138 + coccilib.org.print_todo(p[0], msg_safe) 139 + 140 + @script:python depends on report@ 141 + p << r2.p; 142 + x << r2.x; 143 + @@ 144 + 145 + msg="WARNING: dma_zalloc_coherent should be used for %s, instead of dma_alloc_coherent/memset" % (x) 146 + coccilib.report.print_report(p[0], msg) 147 + 148 + //----------------------------------------------------------------- 149 + @r3 depends on org || report@ 150 + type T, T2; 151 + expression x; 152 + expression E1,E2,E3; 153 + statement S; 154 + position p; 155 + @@ 156 + 157 + x = (T)kmalloc_node@p(E1,E2,E3); 158 + if ((x==NULL) || ...) S 159 + memset((T2)x,0,E1); 160 + 161 + @script:python depends on org@ 162 + p << r3.p; 163 + x << r3.x; 164 + @@ 165 + 166 + msg="%s" % (x) 167 + msg_safe=msg.replace("[","@(").replace("]",")") 168 + coccilib.org.print_todo(p[0], msg_safe) 169 + 170 + @script:python depends on report@ 171 + p << r3.p; 172 + x << r3.x; 173 + @@ 174 + 175 + msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x) 176 + coccilib.report.print_report(p[0], msg) 177 + 178 + //----------------------------------------------------------------- 179 + @r4 depends on org || report@ 180 + type T, T2; 181 + expression x; 182 + expression E1,E2,E3; 183 + statement S; 184 + position p; 185 + @@ 186 + 187 + x = (T)kmem_cache_alloc@p(E2,E3); 188 + if ((x==NULL) || ...) S 189 + memset((T2)x,0,E1); 190 + 191 + @script:python depends on org@ 192 + p << r4.p; 193 + x << r4.x; 194 + @@ 195 + 196 + msg="%s" % (x) 197 + msg_safe=msg.replace("[","@(").replace("]",")") 198 + coccilib.org.print_todo(p[0], msg_safe) 199 + 200 + @script:python depends on report@ 201 + p << r4.p; 202 + x << r4.x; 203 + @@ 204 + 205 + msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x) 206 + coccilib.report.print_report(p[0], msg) 207 + 208 + //----------------------------------------------------------------- 209 + @r5 depends on org || report@ 210 + type T, T2; 211 + expression x; 212 + expression E1,E2; 213 + statement S; 214 + position p; 215 + @@ 216 + 217 + x = (T)kmem_alloc@p(E1,E2); 218 + if ((x==NULL) || ...) S 219 + memset((T2)x,0,E1); 220 + 221 + @script:python depends on org@ 222 + p << r5.p; 223 + x << r5.x; 224 + @@ 225 + 226 + msg="%s" % (x) 227 + msg_safe=msg.replace("[","@(").replace("]",")") 228 + coccilib.org.print_todo(p[0], msg_safe) 229 + 230 + @script:python depends on report@ 231 + p << r5.p; 232 + x << r5.x; 233 + @@ 234 + 235 + msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x) 236 + coccilib.report.print_report(p[0], msg) 237 + 238 + //----------------------------------------------------------------- 239 + @r6 depends on org || report@ 240 + type T, T2; 241 + expression x; 242 + expression E1,E2,E3; 243 + statement S; 244 + position p; 245 + @@ 246 + 247 + x = (T)devm_kmalloc@p(E2,E1,E3); 248 + if ((x==NULL) || ...) S 249 + memset((T2)x,0,E1); 250 + 251 + @script:python depends on org@ 252 + p << r6.p; 253 + x << r6.x; 254 + @@ 255 + 256 + msg="%s" % (x) 257 + msg_safe=msg.replace("[","@(").replace("]",")") 258 + coccilib.org.print_todo(p[0], msg_safe) 259 + 260 + @script:python depends on report@ 261 + p << r6.p; 262 + x << r6.x; 263 + @@ 264 + 265 + msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x) 266 + coccilib.report.print_report(p[0], msg) 267 + 268 + //----------------------------------------------------------------- 269 + @r7 depends on org || report@ 270 + type T, T2; 271 + expression x; 272 + expression E1,E2; 273 + statement S; 274 + position p; 275 + @@ 276 + 277 + x = (T)kvmalloc@p(E1,E2); 278 + if ((x==NULL) || ...) S 279 + memset((T2)x,0,E1); 280 + 281 + @script:python depends on org@ 282 + p << r7.p; 283 + x << r7.x; 284 + @@ 285 + 286 + msg="%s" % (x) 287 + msg_safe=msg.replace("[","@(").replace("]",")") 288 + coccilib.org.print_todo(p[0], msg_safe) 289 + 290 + @script:python depends on report@ 291 + p << r7.p; 292 + x << r7.x; 293 + @@ 294 + 295 + msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x) 296 + coccilib.report.print_report(p[0], msg) 297 + 298 + //----------------------------------------------------------------- 299 + @r8 depends on org || report@ 300 + type T, T2; 301 + expression x; 302 + expression E1,E2,E3; 303 + statement S; 304 + position p; 305 + @@ 306 + 307 + x = (T)pci_alloc_consistent@p(E2,E1,E3); 308 + if ((x==NULL) || ...) S 309 + memset((T2)x,0,E1); 310 + 311 + @script:python depends on org@ 312 + p << r8.p; 313 + x << r8.x; 314 + @@ 315 + 316 + msg="%s" % (x) 317 + msg_safe=msg.replace("[","@(").replace("]",")") 318 + coccilib.org.print_todo(p[0], msg_safe) 319 + 320 + @script:python depends on report@ 321 + p << r8.p; 322 + x << r8.x; 323 + @@ 324 + 325 + msg="WARNING: pci_zalloc_consistent should be used for %s, instead of pci_alloc_consistent/memset" % (x) 326 + coccilib.report.print_report(p[0], msg) 327 + //----------------------------------------------------------------- 328 + @r9 depends on org || report@ 329 + type T, T2; 330 + expression x; 331 + expression E1,E2,E3; 332 + statement S; 333 + position p; 334 + @@ 335 + 336 + x = (T)kvmalloc_node@p(E1,E2,E3); 337 + if ((x==NULL) || ...) S 338 + memset((T2)x,0,E1); 339 + 340 + @script:python depends on org@ 341 + p << r9.p; 342 + x << r9.x; 343 + @@ 344 + 345 + msg="%s" % (x) 346 + msg_safe=msg.replace("[","@(").replace("]",")") 347 + coccilib.org.print_todo(p[0], msg_safe) 348 + 349 + @script:python depends on report@ 350 + p << r9.p; 351 + x << r9.x; 352 + @@ 353 + 354 + msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x) 179 355 coccilib.report.print_report(p[0], msg)