| 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: https://coccinelle.gitlabpages.inria.fr/website |
| 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(...)\| |
| 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 | | |
| 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 = devm_kmalloc(E2,E1,E3); |
| 92 | + x = devm_kzalloc(E2,E1,E3); |
| 93 | | |
| 94 | - x = (T *)devm_kmalloc(E2,E1,E3); |
| 95 | + x = devm_kzalloc(E2,E1,E3); |
| 96 | | |
| 97 | - x = (T)devm_kmalloc(E2,E1,E3); |
| 98 | + x = (T)devm_kzalloc(E2,E1,E3); |
| 99 | | |
| 100 | - x = kvmalloc(E1,E2); |
| 101 | + x = kvzalloc(E1,E2); |
| 102 | | |
| 103 | - x = (T *)kvmalloc(E1,E2); |
| 104 | + x = kvzalloc(E1,E2); |
| 105 | | |
| 106 | - x = (T)kvmalloc(E1,E2); |
| 107 | + x = (T)kvzalloc(E1,E2); |
| 108 | | |
| 109 | - x = kvmalloc_node(E1,E2,E3); |
| 110 | + x = kvzalloc_node(E1,E2,E3); |
| 111 | | |
| 112 | - x = (T *)kvmalloc_node(E1,E2,E3); |
| 113 | + x = kvzalloc_node(E1,E2,E3); |
| 114 | | |
| 115 | - x = (T)kvmalloc_node(E1,E2,E3); |
| 116 | + x = (T)kvzalloc_node(E1,E2,E3); |
| 117 | ) |
| 118 | if ((x==NULL) || ...) S |
| 119 | - memset((T2)x,0,E1); |
| 120 | |
| 121 | @depends on patch@ |
| 122 | type T, T2; |
| 123 | expression x; |
| 124 | expression E1,E2,E3,E4; |
| 125 | statement S; |
| 126 | @@ |
| 127 | x = (T)dma_alloc_coherent(E1, E2, E3, E4); |
| 128 | if ((x==NULL) || ...) S |
| 129 | - memset((T2)x, 0, E2); |
| 130 | |
| 131 | //---------------------------------------------------------- |
| 132 | // For org mode |
| 133 | //---------------------------------------------------------- |
| 134 | |
| 135 | @r depends on org || report@ |
| 136 | type T, T2; |
| 137 | expression x; |
| 138 | expression E1,E2; |
| 139 | statement S; |
| 140 | position p; |
| 141 | @@ |
| 142 | |
| 143 | x = (T)kmalloc@p(E1,E2); |
| 144 | if ((x==NULL) || ...) S |
| 145 | memset((T2)x,0,E1); |
| 146 | |
| 147 | @script:python depends on org@ |
| 148 | p << r.p; |
| 149 | x << r.x; |
| 150 | @@ |
| 151 | |
| 152 | msg="%s" % (x) |
| 153 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 154 | coccilib.org.print_todo(p[0], msg_safe) |
| 155 | |
| 156 | @script:python depends on report@ |
| 157 | p << r.p; |
| 158 | x << r.x; |
| 159 | @@ |
| 160 | |
| 161 | msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x) |
| 162 | coccilib.report.print_report(p[0], msg) |
| 163 | |
| 164 | //----------------------------------------------------------------- |
| 165 | @r1 depends on org || report@ |
| 166 | type T, T2; |
| 167 | expression x; |
| 168 | expression E1; |
| 169 | statement S; |
| 170 | position p; |
| 171 | @@ |
| 172 | |
| 173 | x = (T)vmalloc@p(E1); |
| 174 | if ((x==NULL) || ...) S |
| 175 | memset((T2)x,0,E1); |
| 176 | |
| 177 | @script:python depends on org@ |
| 178 | p << r1.p; |
| 179 | x << r1.x; |
| 180 | @@ |
| 181 | |
| 182 | msg="%s" % (x) |
| 183 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 184 | coccilib.org.print_todo(p[0], msg_safe) |
| 185 | |
| 186 | @script:python depends on report@ |
| 187 | p << r1.p; |
| 188 | x << r1.x; |
| 189 | @@ |
| 190 | |
| 191 | msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x) |
| 192 | coccilib.report.print_report(p[0], msg) |
| 193 | |
| 194 | //----------------------------------------------------------------- |
| 195 | @r2 depends on org || report@ |
| 196 | type T, T2; |
| 197 | expression x; |
| 198 | expression E1,E2,E3,E4; |
| 199 | statement S; |
| 200 | position p; |
| 201 | @@ |
| 202 | |
| 203 | x = (T)dma_alloc_coherent@p(E1,E2,E3,E4); |
| 204 | if ((x==NULL) || ...) S |
| 205 | memset((T2)x,0,E2); |
| 206 | |
| 207 | @script:python depends on org@ |
| 208 | p << r2.p; |
| 209 | x << r2.x; |
| 210 | @@ |
| 211 | |
| 212 | msg="%s" % (x) |
| 213 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 214 | coccilib.org.print_todo(p[0], msg_safe) |
| 215 | |
| 216 | @script:python depends on report@ |
| 217 | p << r2.p; |
| 218 | x << r2.x; |
| 219 | @@ |
| 220 | |
| 221 | msg="WARNING: dma_alloc_coherent used in %s already zeroes out memory, so memset is not needed" % (x) |
| 222 | coccilib.report.print_report(p[0], msg) |
| 223 | |
| 224 | //----------------------------------------------------------------- |
| 225 | @r3 depends on org || report@ |
| 226 | type T, T2; |
| 227 | expression x; |
| 228 | expression E1,E2,E3; |
| 229 | statement S; |
| 230 | position p; |
| 231 | @@ |
| 232 | |
| 233 | x = (T)kmalloc_node@p(E1,E2,E3); |
| 234 | if ((x==NULL) || ...) S |
| 235 | memset((T2)x,0,E1); |
| 236 | |
| 237 | @script:python depends on org@ |
| 238 | p << r3.p; |
| 239 | x << r3.x; |
| 240 | @@ |
| 241 | |
| 242 | msg="%s" % (x) |
| 243 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 244 | coccilib.org.print_todo(p[0], msg_safe) |
| 245 | |
| 246 | @script:python depends on report@ |
| 247 | p << r3.p; |
| 248 | x << r3.x; |
| 249 | @@ |
| 250 | |
| 251 | msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x) |
| 252 | coccilib.report.print_report(p[0], msg) |
| 253 | |
| 254 | //----------------------------------------------------------------- |
| 255 | @r4 depends on org || report@ |
| 256 | type T, T2; |
| 257 | expression x; |
| 258 | expression E1,E2,E3; |
| 259 | statement S; |
| 260 | position p; |
| 261 | @@ |
| 262 | |
| 263 | x = (T)kmem_cache_alloc@p(E2,E3); |
| 264 | if ((x==NULL) || ...) S |
| 265 | memset((T2)x,0,E1); |
| 266 | |
| 267 | @script:python depends on org@ |
| 268 | p << r4.p; |
| 269 | x << r4.x; |
| 270 | @@ |
| 271 | |
| 272 | msg="%s" % (x) |
| 273 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 274 | coccilib.org.print_todo(p[0], msg_safe) |
| 275 | |
| 276 | @script:python depends on report@ |
| 277 | p << r4.p; |
| 278 | x << r4.x; |
| 279 | @@ |
| 280 | |
| 281 | msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x) |
| 282 | coccilib.report.print_report(p[0], msg) |
| 283 | |
| 284 | //----------------------------------------------------------------- |
| 285 | @r6 depends on org || report@ |
| 286 | type T, T2; |
| 287 | expression x; |
| 288 | expression E1,E2,E3; |
| 289 | statement S; |
| 290 | position p; |
| 291 | @@ |
| 292 | |
| 293 | x = (T)devm_kmalloc@p(E2,E1,E3); |
| 294 | if ((x==NULL) || ...) S |
| 295 | memset((T2)x,0,E1); |
| 296 | |
| 297 | @script:python depends on org@ |
| 298 | p << r6.p; |
| 299 | x << r6.x; |
| 300 | @@ |
| 301 | |
| 302 | msg="%s" % (x) |
| 303 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 304 | coccilib.org.print_todo(p[0], msg_safe) |
| 305 | |
| 306 | @script:python depends on report@ |
| 307 | p << r6.p; |
| 308 | x << r6.x; |
| 309 | @@ |
| 310 | |
| 311 | msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x) |
| 312 | coccilib.report.print_report(p[0], msg) |
| 313 | |
| 314 | //----------------------------------------------------------------- |
| 315 | @r7 depends on org || report@ |
| 316 | type T, T2; |
| 317 | expression x; |
| 318 | expression E1,E2; |
| 319 | statement S; |
| 320 | position p; |
| 321 | @@ |
| 322 | |
| 323 | x = (T)kvmalloc@p(E1,E2); |
| 324 | if ((x==NULL) || ...) S |
| 325 | memset((T2)x,0,E1); |
| 326 | |
| 327 | @script:python depends on org@ |
| 328 | p << r7.p; |
| 329 | x << r7.x; |
| 330 | @@ |
| 331 | |
| 332 | msg="%s" % (x) |
| 333 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 334 | coccilib.org.print_todo(p[0], msg_safe) |
| 335 | |
| 336 | @script:python depends on report@ |
| 337 | p << r7.p; |
| 338 | x << r7.x; |
| 339 | @@ |
| 340 | |
| 341 | msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x) |
| 342 | coccilib.report.print_report(p[0], msg) |
| 343 | |
| 344 | //----------------------------------------------------------------- |
| 345 | @r9 depends on org || report@ |
| 346 | type T, T2; |
| 347 | expression x; |
| 348 | expression E1,E2,E3; |
| 349 | statement S; |
| 350 | position p; |
| 351 | @@ |
| 352 | |
| 353 | x = (T)kvmalloc_node@p(E1,E2,E3); |
| 354 | if ((x==NULL) || ...) S |
| 355 | memset((T2)x,0,E1); |
| 356 | |
| 357 | @script:python depends on org@ |
| 358 | p << r9.p; |
| 359 | x << r9.x; |
| 360 | @@ |
| 361 | |
| 362 | msg="%s" % (x) |
| 363 | msg_safe=msg.replace("[","@(").replace("]",")") |
| 364 | coccilib.org.print_todo(p[0], msg_safe) |
| 365 | |
| 366 | @script:python depends on report@ |
| 367 | p << r9.p; |
| 368 | x << r9.x; |
| 369 | @@ |
| 370 | |
| 371 | msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x) |
| 372 | coccilib.report.print_report(p[0], msg) |