1 // SPDX-License-Identifier: GPL-2.0-only
2 /// Remove casting the values returned by memory allocation functions
3 /// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
5 //# This makes an effort to find cases of casting of values returned by
6 //# kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc,
7 //# kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes
8 //# the casting as it is not required. The result in the patch case may
9 //# need some reformatting.
12 // Copyright: (C) 2014 Himangi Saraogi
13 // Copyright: (C) 2017 Himanshu Jha
15 // Options: --no-includes --include-headers
27 m = re.compile(pattern)
29 @r1 depends on context || patch@
34 \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
35 kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
36 dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
37 kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
38 pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
39 kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
41 //----------------------------------------------------------
43 //----------------------------------------------------------
45 @script:python depends on context@
49 if m.search(t) != None:
50 cocci.include_match(False)
52 @depends on context && r1@
57 \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
58 kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
59 dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
60 kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
61 pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
62 kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
64 //----------------------------------------------------------
66 //----------------------------------------------------------
68 @script:python depends on patch@
72 if m.search(t) != None:
73 cocci.include_match(False)
75 @depends on patch && r1@
80 \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
81 kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
82 dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
83 kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
84 pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
85 kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
87 //----------------------------------------------------------
88 // For org and report mode
89 //----------------------------------------------------------
91 @r2 depends on org || report@
97 \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
98 kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
99 dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
100 kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
101 pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
102 kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)
104 @script:python depends on org@
109 if m.search(t) != None:
110 cocci.include_match(False)
112 coccilib.org.print_safe_todo(p[0], t)
114 @script:python depends on report@
119 if m.search(t) != None:
120 cocci.include_match(False)
122 msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
123 coccilib.report.print_report(p[0], msg)