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 21virtual context 22virtual patch 23virtual org 24virtual report 25 26//---------------------------------------------------------- 27// For context mode 28//---------------------------------------------------------- 29 30@depends on context@ 31type T, T2; 32expression x; 33expression E1; 34statement 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@ 48type T, T2; 49expression x; 50expression E1,E2,E3,E4; 51statement 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@ 122type T, T2; 123expression x; 124expression E1,E2,E3,E4; 125statement 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@ 136type T, T2; 137expression x; 138expression E1,E2; 139statement S; 140position 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@ 148p << r.p; 149x << r.x; 150@@ 151 152msg="%s" % (x) 153msg_safe=msg.replace("[","@(").replace("]",")") 154coccilib.org.print_todo(p[0], msg_safe) 155 156@script:python depends on report@ 157p << r.p; 158x << r.x; 159@@ 160 161msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x) 162coccilib.report.print_report(p[0], msg) 163 164//----------------------------------------------------------------- 165@r1 depends on org || report@ 166type T, T2; 167expression x; 168expression E1; 169statement S; 170position 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@ 178p << r1.p; 179x << r1.x; 180@@ 181 182msg="%s" % (x) 183msg_safe=msg.replace("[","@(").replace("]",")") 184coccilib.org.print_todo(p[0], msg_safe) 185 186@script:python depends on report@ 187p << r1.p; 188x << r1.x; 189@@ 190 191msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x) 192coccilib.report.print_report(p[0], msg) 193 194//----------------------------------------------------------------- 195@r2 depends on org || report@ 196type T, T2; 197expression x; 198expression E1,E2,E3,E4; 199statement S; 200position 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@ 208p << r2.p; 209x << r2.x; 210@@ 211 212msg="%s" % (x) 213msg_safe=msg.replace("[","@(").replace("]",")") 214coccilib.org.print_todo(p[0], msg_safe) 215 216@script:python depends on report@ 217p << r2.p; 218x << r2.x; 219@@ 220 221msg="WARNING: dma_alloc_coherent used in %s already zeroes out memory, so memset is not needed" % (x) 222coccilib.report.print_report(p[0], msg) 223 224//----------------------------------------------------------------- 225@r3 depends on org || report@ 226type T, T2; 227expression x; 228expression E1,E2,E3; 229statement S; 230position 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@ 238p << r3.p; 239x << r3.x; 240@@ 241 242msg="%s" % (x) 243msg_safe=msg.replace("[","@(").replace("]",")") 244coccilib.org.print_todo(p[0], msg_safe) 245 246@script:python depends on report@ 247p << r3.p; 248x << r3.x; 249@@ 250 251msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x) 252coccilib.report.print_report(p[0], msg) 253 254//----------------------------------------------------------------- 255@r4 depends on org || report@ 256type T, T2; 257expression x; 258expression E1,E2,E3; 259statement S; 260position 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@ 268p << r4.p; 269x << r4.x; 270@@ 271 272msg="%s" % (x) 273msg_safe=msg.replace("[","@(").replace("]",")") 274coccilib.org.print_todo(p[0], msg_safe) 275 276@script:python depends on report@ 277p << r4.p; 278x << r4.x; 279@@ 280 281msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x) 282coccilib.report.print_report(p[0], msg) 283 284//----------------------------------------------------------------- 285@r6 depends on org || report@ 286type T, T2; 287expression x; 288expression E1,E2,E3; 289statement S; 290position 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@ 298p << r6.p; 299x << r6.x; 300@@ 301 302msg="%s" % (x) 303msg_safe=msg.replace("[","@(").replace("]",")") 304coccilib.org.print_todo(p[0], msg_safe) 305 306@script:python depends on report@ 307p << r6.p; 308x << r6.x; 309@@ 310 311msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x) 312coccilib.report.print_report(p[0], msg) 313 314//----------------------------------------------------------------- 315@r7 depends on org || report@ 316type T, T2; 317expression x; 318expression E1,E2; 319statement S; 320position 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@ 328p << r7.p; 329x << r7.x; 330@@ 331 332msg="%s" % (x) 333msg_safe=msg.replace("[","@(").replace("]",")") 334coccilib.org.print_todo(p[0], msg_safe) 335 336@script:python depends on report@ 337p << r7.p; 338x << r7.x; 339@@ 340 341msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x) 342coccilib.report.print_report(p[0], msg) 343 344//----------------------------------------------------------------- 345@r9 depends on org || report@ 346type T, T2; 347expression x; 348expression E1,E2,E3; 349statement S; 350position 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@ 358p << r9.p; 359x << r9.x; 360@@ 361 362msg="%s" % (x) 363msg_safe=msg.replace("[","@(").replace("]",")") 364coccilib.org.print_todo(p[0], msg_safe) 365 366@script:python depends on report@ 367p << r9.p; 368x << r9.x; 369@@ 370 371msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x) 372coccilib.report.print_report(p[0], msg) 373