David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 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 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
| 21 | virtual context |
| 22 | virtual patch |
| 23 | virtual org |
| 24 | virtual report |
| 25 | |
| 26 | //---------------------------------------------------------- |
| 27 | // For context mode |
| 28 | //---------------------------------------------------------- |
| 29 | |
| 30 | @depends on context@ |
| 31 | type T, T2; |
| 32 | expression x; |
| 33 | expression E1; |
| 34 | statement 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@ |
| 48 | type T, T2; |
| 49 | expression x; |
| 50 | expression E1,E2,E3,E4; |
| 51 | statement 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 | | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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@ |
| 135 | type T, T2; |
| 136 | expression x; |
| 137 | expression E1,E2; |
| 138 | statement S; |
| 139 | position 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@ |
| 147 | p << r.p; |
| 148 | x << r.x; |
| 149 | @@ |
| 150 | |
| 151 | msg="%s" % (x) |
| 152 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 153 | coccilib.org.print_todo(p[0], msg_safe) |
| 154 | |
| 155 | @script:python depends on report@ |
| 156 | p << r.p; |
| 157 | x << r.x; |
| 158 | @@ |
| 159 | |
| 160 | msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x) |
| 161 | coccilib.report.print_report(p[0], msg) |
| 162 | |
| 163 | //----------------------------------------------------------------- |
| 164 | @r1 depends on org || report@ |
| 165 | type T, T2; |
| 166 | expression x; |
| 167 | expression E1; |
| 168 | statement S; |
| 169 | position 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@ |
| 177 | p << r1.p; |
| 178 | x << r1.x; |
| 179 | @@ |
| 180 | |
| 181 | msg="%s" % (x) |
| 182 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 183 | coccilib.org.print_todo(p[0], msg_safe) |
| 184 | |
| 185 | @script:python depends on report@ |
| 186 | p << r1.p; |
| 187 | x << r1.x; |
| 188 | @@ |
| 189 | |
| 190 | msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x) |
| 191 | coccilib.report.print_report(p[0], msg) |
| 192 | |
| 193 | //----------------------------------------------------------------- |
| 194 | @r2 depends on org || report@ |
| 195 | type T, T2; |
| 196 | expression x; |
| 197 | expression E1,E2,E3,E4; |
| 198 | statement S; |
| 199 | position 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@ |
| 207 | p << r2.p; |
| 208 | x << r2.x; |
| 209 | @@ |
| 210 | |
| 211 | msg="%s" % (x) |
| 212 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 213 | coccilib.org.print_todo(p[0], msg_safe) |
| 214 | |
| 215 | @script:python depends on report@ |
| 216 | p << r2.p; |
| 217 | x << r2.x; |
| 218 | @@ |
| 219 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 220 | msg="WARNING: dma_alloc_coherent use in %s already zeroes out memory, so memset is not needed" % (x) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 221 | coccilib.report.print_report(p[0], msg) |
| 222 | |
| 223 | //----------------------------------------------------------------- |
| 224 | @r3 depends on org || report@ |
| 225 | type T, T2; |
| 226 | expression x; |
| 227 | expression E1,E2,E3; |
| 228 | statement S; |
| 229 | position 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@ |
| 237 | p << r3.p; |
| 238 | x << r3.x; |
| 239 | @@ |
| 240 | |
| 241 | msg="%s" % (x) |
| 242 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 243 | coccilib.org.print_todo(p[0], msg_safe) |
| 244 | |
| 245 | @script:python depends on report@ |
| 246 | p << r3.p; |
| 247 | x << r3.x; |
| 248 | @@ |
| 249 | |
| 250 | msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x) |
| 251 | coccilib.report.print_report(p[0], msg) |
| 252 | |
| 253 | //----------------------------------------------------------------- |
| 254 | @r4 depends on org || report@ |
| 255 | type T, T2; |
| 256 | expression x; |
| 257 | expression E1,E2,E3; |
| 258 | statement S; |
| 259 | position 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@ |
| 267 | p << r4.p; |
| 268 | x << r4.x; |
| 269 | @@ |
| 270 | |
| 271 | msg="%s" % (x) |
| 272 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 273 | coccilib.org.print_todo(p[0], msg_safe) |
| 274 | |
| 275 | @script:python depends on report@ |
| 276 | p << r4.p; |
| 277 | x << r4.x; |
| 278 | @@ |
| 279 | |
| 280 | msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x) |
| 281 | coccilib.report.print_report(p[0], msg) |
| 282 | |
| 283 | //----------------------------------------------------------------- |
| 284 | @r5 depends on org || report@ |
| 285 | type T, T2; |
| 286 | expression x; |
| 287 | expression E1,E2; |
| 288 | statement S; |
| 289 | position 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@ |
| 297 | p << r5.p; |
| 298 | x << r5.x; |
| 299 | @@ |
| 300 | |
| 301 | msg="%s" % (x) |
| 302 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 303 | coccilib.org.print_todo(p[0], msg_safe) |
| 304 | |
| 305 | @script:python depends on report@ |
| 306 | p << r5.p; |
| 307 | x << r5.x; |
| 308 | @@ |
| 309 | |
| 310 | msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x) |
| 311 | coccilib.report.print_report(p[0], msg) |
| 312 | |
| 313 | //----------------------------------------------------------------- |
| 314 | @r6 depends on org || report@ |
| 315 | type T, T2; |
| 316 | expression x; |
| 317 | expression E1,E2,E3; |
| 318 | statement S; |
| 319 | position 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@ |
| 327 | p << r6.p; |
| 328 | x << r6.x; |
| 329 | @@ |
| 330 | |
| 331 | msg="%s" % (x) |
| 332 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 333 | coccilib.org.print_todo(p[0], msg_safe) |
| 334 | |
| 335 | @script:python depends on report@ |
| 336 | p << r6.p; |
| 337 | x << r6.x; |
| 338 | @@ |
| 339 | |
| 340 | msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x) |
| 341 | coccilib.report.print_report(p[0], msg) |
| 342 | |
| 343 | //----------------------------------------------------------------- |
| 344 | @r7 depends on org || report@ |
| 345 | type T, T2; |
| 346 | expression x; |
| 347 | expression E1,E2; |
| 348 | statement S; |
| 349 | position 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@ |
| 357 | p << r7.p; |
| 358 | x << r7.x; |
| 359 | @@ |
| 360 | |
| 361 | msg="%s" % (x) |
| 362 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 363 | coccilib.org.print_todo(p[0], msg_safe) |
| 364 | |
| 365 | @script:python depends on report@ |
| 366 | p << r7.p; |
| 367 | x << r7.x; |
| 368 | @@ |
| 369 | |
| 370 | msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x) |
| 371 | coccilib.report.print_report(p[0], msg) |
| 372 | |
| 373 | //----------------------------------------------------------------- |
| 374 | @r9 depends on org || report@ |
| 375 | type T, T2; |
| 376 | expression x; |
| 377 | expression E1,E2,E3; |
| 378 | statement S; |
| 379 | position 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@ |
| 387 | p << r9.p; |
| 388 | x << r9.x; |
| 389 | @@ |
| 390 | |
| 391 | msg="%s" % (x) |
| 392 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 393 | coccilib.org.print_todo(p[0], msg_safe) |
| 394 | |
| 395 | @script:python depends on report@ |
| 396 | p << r9.p; |
| 397 | x << r9.x; |
| 398 | @@ |
| 399 | |
| 400 | msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x) |
| 401 | coccilib.report.print_report(p[0], msg) |