0001 // SPDX-License-Identifier: GPL-2.0-only
0002 ///
0003 /// Use zeroing allocator rather than allocator followed by memset with 0
0004 ///
0005 /// This considers some simple cases that are common and easy to validate
0006 /// Note in particular that there are no ...s in the rule, so all of the
0007 /// matched code has to be contiguous
0008 ///
0009 // Confidence: High
0010 // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU.
0011 // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6.
0012 // Copyright: (C) 2017 Himanshu Jha
0013 // URL: https://coccinelle.gitlabpages.inria.fr/website
0014 // Options: --no-includes --include-headers
0015 //
0016 // Keywords: kmalloc, kzalloc
0017 // Version min: < 2.6.12 kmalloc
0018 // Version min: 2.6.14 kzalloc
0019 //
0020
0021 virtual context
0022 virtual patch
0023 virtual org
0024 virtual report
0025
0026 //----------------------------------------------------------
0027 // For context mode
0028 //----------------------------------------------------------
0029
0030 @depends on context@
0031 type T, T2;
0032 expression x;
0033 expression E1;
0034 statement S;
0035 @@
0036
0037 * x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
0038 kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
0039 devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|kvmalloc_node(E1,...)\);
0040 if ((x==NULL) || ...) S
0041 * memset((T2)x,0,E1);
0042
0043 //----------------------------------------------------------
0044 // For patch mode
0045 //----------------------------------------------------------
0046
0047 @depends on patch@
0048 type T, T2;
0049 expression x;
0050 expression E1,E2,E3,E4;
0051 statement S;
0052 @@
0053
0054 (
0055 - x = kmalloc(E1,E2);
0056 + x = kzalloc(E1,E2);
0057 |
0058 - x = (T *)kmalloc(E1,E2);
0059 + x = kzalloc(E1,E2);
0060 |
0061 - x = (T)kmalloc(E1,E2);
0062 + x = (T)kzalloc(E1,E2);
0063 |
0064 - x = vmalloc(E1);
0065 + x = vzalloc(E1);
0066 |
0067 - x = (T *)vmalloc(E1);
0068 + x = vzalloc(E1);
0069 |
0070 - x = (T)vmalloc(E1);
0071 + x = (T)vzalloc(E1);
0072 |
0073 - x = kmalloc_node(E1,E2,E3);
0074 + x = kzalloc_node(E1,E2,E3);
0075 |
0076 - x = (T *)kmalloc_node(E1,E2,E3);
0077 + x = kzalloc_node(E1,E2,E3);
0078 |
0079 - x = (T)kmalloc_node(E1,E2,E3);
0080 + x = (T)kzalloc_node(E1,E2,E3);
0081 |
0082 - x = kmem_cache_alloc(E3,E4);
0083 + x = kmem_cache_zalloc(E3,E4);
0084 |
0085 - x = (T *)kmem_cache_alloc(E3,E4);
0086 + x = kmem_cache_zalloc(E3,E4);
0087 |
0088 - x = (T)kmem_cache_alloc(E3,E4);
0089 + x = (T)kmem_cache_zalloc(E3,E4);
0090 |
0091 - x = kmem_alloc(E1,E2);
0092 + x = kmem_zalloc(E1,E2);
0093 |
0094 - x = (T *)kmem_alloc(E1,E2);
0095 + x = kmem_zalloc(E1,E2);
0096 |
0097 - x = (T)kmem_alloc(E1,E2);
0098 + x = (T)kmem_zalloc(E1,E2);
0099 |
0100 - x = devm_kmalloc(E2,E1,E3);
0101 + x = devm_kzalloc(E2,E1,E3);
0102 |
0103 - x = (T *)devm_kmalloc(E2,E1,E3);
0104 + x = devm_kzalloc(E2,E1,E3);
0105 |
0106 - x = (T)devm_kmalloc(E2,E1,E3);
0107 + x = (T)devm_kzalloc(E2,E1,E3);
0108 |
0109 - x = kvmalloc(E1,E2);
0110 + x = kvzalloc(E1,E2);
0111 |
0112 - x = (T *)kvmalloc(E1,E2);
0113 + x = kvzalloc(E1,E2);
0114 |
0115 - x = (T)kvmalloc(E1,E2);
0116 + x = (T)kvzalloc(E1,E2);
0117 |
0118 - x = kvmalloc_node(E1,E2,E3);
0119 + x = kvzalloc_node(E1,E2,E3);
0120 |
0121 - x = (T *)kvmalloc_node(E1,E2,E3);
0122 + x = kvzalloc_node(E1,E2,E3);
0123 |
0124 - x = (T)kvmalloc_node(E1,E2,E3);
0125 + x = (T)kvzalloc_node(E1,E2,E3);
0126 )
0127 if ((x==NULL) || ...) S
0128 - memset((T2)x,0,E1);
0129
0130 @depends on patch@
0131 type T, T2;
0132 expression x;
0133 expression E1,E2,E3,E4;
0134 statement S;
0135 @@
0136 x = (T)dma_alloc_coherent(E1, E2, E3, E4);
0137 if ((x==NULL) || ...) S
0138 - memset((T2)x, 0, E2);
0139
0140 //----------------------------------------------------------
0141 // For org mode
0142 //----------------------------------------------------------
0143
0144 @r depends on org || report@
0145 type T, T2;
0146 expression x;
0147 expression E1,E2;
0148 statement S;
0149 position p;
0150 @@
0151
0152 x = (T)kmalloc@p(E1,E2);
0153 if ((x==NULL) || ...) S
0154 memset((T2)x,0,E1);
0155
0156 @script:python depends on org@
0157 p << r.p;
0158 x << r.x;
0159 @@
0160
0161 msg="%s" % (x)
0162 msg_safe=msg.replace("[","@(").replace("]",")")
0163 coccilib.org.print_todo(p[0], msg_safe)
0164
0165 @script:python depends on report@
0166 p << r.p;
0167 x << r.x;
0168 @@
0169
0170 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
0171 coccilib.report.print_report(p[0], msg)
0172
0173 //-----------------------------------------------------------------
0174 @r1 depends on org || report@
0175 type T, T2;
0176 expression x;
0177 expression E1;
0178 statement S;
0179 position p;
0180 @@
0181
0182 x = (T)vmalloc@p(E1);
0183 if ((x==NULL) || ...) S
0184 memset((T2)x,0,E1);
0185
0186 @script:python depends on org@
0187 p << r1.p;
0188 x << r1.x;
0189 @@
0190
0191 msg="%s" % (x)
0192 msg_safe=msg.replace("[","@(").replace("]",")")
0193 coccilib.org.print_todo(p[0], msg_safe)
0194
0195 @script:python depends on report@
0196 p << r1.p;
0197 x << r1.x;
0198 @@
0199
0200 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
0201 coccilib.report.print_report(p[0], msg)
0202
0203 //-----------------------------------------------------------------
0204 @r2 depends on org || report@
0205 type T, T2;
0206 expression x;
0207 expression E1,E2,E3,E4;
0208 statement S;
0209 position p;
0210 @@
0211
0212 x = (T)dma_alloc_coherent@p(E1,E2,E3,E4);
0213 if ((x==NULL) || ...) S
0214 memset((T2)x,0,E2);
0215
0216 @script:python depends on org@
0217 p << r2.p;
0218 x << r2.x;
0219 @@
0220
0221 msg="%s" % (x)
0222 msg_safe=msg.replace("[","@(").replace("]",")")
0223 coccilib.org.print_todo(p[0], msg_safe)
0224
0225 @script:python depends on report@
0226 p << r2.p;
0227 x << r2.x;
0228 @@
0229
0230 msg="WARNING: dma_alloc_coherent used in %s already zeroes out memory, so memset is not needed" % (x)
0231 coccilib.report.print_report(p[0], msg)
0232
0233 //-----------------------------------------------------------------
0234 @r3 depends on org || report@
0235 type T, T2;
0236 expression x;
0237 expression E1,E2,E3;
0238 statement S;
0239 position p;
0240 @@
0241
0242 x = (T)kmalloc_node@p(E1,E2,E3);
0243 if ((x==NULL) || ...) S
0244 memset((T2)x,0,E1);
0245
0246 @script:python depends on org@
0247 p << r3.p;
0248 x << r3.x;
0249 @@
0250
0251 msg="%s" % (x)
0252 msg_safe=msg.replace("[","@(").replace("]",")")
0253 coccilib.org.print_todo(p[0], msg_safe)
0254
0255 @script:python depends on report@
0256 p << r3.p;
0257 x << r3.x;
0258 @@
0259
0260 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
0261 coccilib.report.print_report(p[0], msg)
0262
0263 //-----------------------------------------------------------------
0264 @r4 depends on org || report@
0265 type T, T2;
0266 expression x;
0267 expression E1,E2,E3;
0268 statement S;
0269 position p;
0270 @@
0271
0272 x = (T)kmem_cache_alloc@p(E2,E3);
0273 if ((x==NULL) || ...) S
0274 memset((T2)x,0,E1);
0275
0276 @script:python depends on org@
0277 p << r4.p;
0278 x << r4.x;
0279 @@
0280
0281 msg="%s" % (x)
0282 msg_safe=msg.replace("[","@(").replace("]",")")
0283 coccilib.org.print_todo(p[0], msg_safe)
0284
0285 @script:python depends on report@
0286 p << r4.p;
0287 x << r4.x;
0288 @@
0289
0290 msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
0291 coccilib.report.print_report(p[0], msg)
0292
0293 //-----------------------------------------------------------------
0294 @r5 depends on org || report@
0295 type T, T2;
0296 expression x;
0297 expression E1,E2;
0298 statement S;
0299 position p;
0300 @@
0301
0302 x = (T)kmem_alloc@p(E1,E2);
0303 if ((x==NULL) || ...) S
0304 memset((T2)x,0,E1);
0305
0306 @script:python depends on org@
0307 p << r5.p;
0308 x << r5.x;
0309 @@
0310
0311 msg="%s" % (x)
0312 msg_safe=msg.replace("[","@(").replace("]",")")
0313 coccilib.org.print_todo(p[0], msg_safe)
0314
0315 @script:python depends on report@
0316 p << r5.p;
0317 x << r5.x;
0318 @@
0319
0320 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
0321 coccilib.report.print_report(p[0], msg)
0322
0323 //-----------------------------------------------------------------
0324 @r6 depends on org || report@
0325 type T, T2;
0326 expression x;
0327 expression E1,E2,E3;
0328 statement S;
0329 position p;
0330 @@
0331
0332 x = (T)devm_kmalloc@p(E2,E1,E3);
0333 if ((x==NULL) || ...) S
0334 memset((T2)x,0,E1);
0335
0336 @script:python depends on org@
0337 p << r6.p;
0338 x << r6.x;
0339 @@
0340
0341 msg="%s" % (x)
0342 msg_safe=msg.replace("[","@(").replace("]",")")
0343 coccilib.org.print_todo(p[0], msg_safe)
0344
0345 @script:python depends on report@
0346 p << r6.p;
0347 x << r6.x;
0348 @@
0349
0350 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
0351 coccilib.report.print_report(p[0], msg)
0352
0353 //-----------------------------------------------------------------
0354 @r7 depends on org || report@
0355 type T, T2;
0356 expression x;
0357 expression E1,E2;
0358 statement S;
0359 position p;
0360 @@
0361
0362 x = (T)kvmalloc@p(E1,E2);
0363 if ((x==NULL) || ...) S
0364 memset((T2)x,0,E1);
0365
0366 @script:python depends on org@
0367 p << r7.p;
0368 x << r7.x;
0369 @@
0370
0371 msg="%s" % (x)
0372 msg_safe=msg.replace("[","@(").replace("]",")")
0373 coccilib.org.print_todo(p[0], msg_safe)
0374
0375 @script:python depends on report@
0376 p << r7.p;
0377 x << r7.x;
0378 @@
0379
0380 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
0381 coccilib.report.print_report(p[0], msg)
0382
0383 //-----------------------------------------------------------------
0384 @r9 depends on org || report@
0385 type T, T2;
0386 expression x;
0387 expression E1,E2,E3;
0388 statement S;
0389 position p;
0390 @@
0391
0392 x = (T)kvmalloc_node@p(E1,E2,E3);
0393 if ((x==NULL) || ...) S
0394 memset((T2)x,0,E1);
0395
0396 @script:python depends on org@
0397 p << r9.p;
0398 x << r9.x;
0399 @@
0400
0401 msg="%s" % (x)
0402 msg_safe=msg.replace("[","@(").replace("]",")")
0403 coccilib.org.print_todo(p[0], msg_safe)
0404
0405 @script:python depends on report@
0406 p << r9.p;
0407 x << r9.x;
0408 @@
0409
0410 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
0411 coccilib.report.print_report(p[0], msg)