Attachment 'kj.pl'
Download
Toggle line numbers
1 #!/usr/bin/perl -w
2 #
3 # An automated kernel janitor.
4 # (c) 2001, Dave Jones. <davej@suse.de>, with invaluable
5 # perl assistance from Rob Andrews <nine@impure.org.uk>
6 # Various code also inspired from hundreds of other perl scripts.
7 #
8 # Additional functionality added by The kerneljanitors.org team :)
9 #
10 # Licensed under the terms of the GNU GPL License version 2
11 #
12 #
13 # TODO:
14 # Should reduce some false positives.
15 # - Spinlock balancing routine needs to check all return paths
16 # of current subroutine
17 # - Spinlock balancing routine can be made more generic to a
18 # "Check that all return paths call $1"
19
20 use strict;
21
22 my @lines;
23
24 foreach my $file (@ARGV) {
25 process ($file)
26 or warn "Couldn't check file $file: $!";
27 }
28
29
30 sub check_pci_enable_device {
31 my $used = shift;
32 my $filename = shift;
33 my $currentlinenr;
34 foreach my $current (@lines) {
35 $currentlinenr++;
36 if ($current=~/pci_enable_device/) {
37 if ($currentlinenr < $used) {
38 print "Should be ok, called on line $currentlinenr\n";
39 print "$filename:$currentlinenr:$current\n";
40 }
41 }
42 }
43 }
44
45
46 sub check_spinlock {
47 my $lock = shift;
48 my $filename = shift;
49 my $locklinenr = shift;
50 my $foundunlock = 0;
51 my $currentlinenr;
52
53 # TODO: Start at $locklinenr
54
55 foreach my $current (@lines) {
56 $currentlinenr++;
57 if ($current=~/spin_unlock\($lock\)/) {
58 # print ("Lock closed on line $currentlinenr\n");
59 $foundunlock = 1;
60 }
61 }
62
63 return $foundunlock;
64 }
65
66
67 sub process {
68 my $filename=shift;
69
70 return undef unless $filename;
71
72 open INPUT, $filename
73 or return undef;
74
75 while(!eof INPUT) {
76 my $input=<INPUT>;
77 chomp $input;
78 push @lines, $input;
79 }
80 close INPUT;
81
82 # s!/\*(.*?)\*/!"\n" x (@lines =~ tr/\n//)!esg;
83
84 my $spinlockwarn=0;
85 my $lookspci=0;
86 my $linenr=0;
87
88 foreach my $line (@lines) {
89
90 $linenr++;
91
92 # For later, we may need to preprocess include files.
93 # read as: Include them into the array before the current file.
94 # if ($line=~/^#include [<"].*?[>"]/i) {
95
96 if ($line=~/static const char.*__initdata/) {
97 print "const & __initdata in string def. Remove const.\n";
98 print "$filename:$linenr:$line\n\n";
99 }
100
101 if ($line=~/\(struct netdev_private \*\)/) {
102 print "Unnecessary cast.\n";
103 print "$filename:$linenr:$line\n\n";
104 }
105
106 if ($line=~/netif_rx/) {
107 print "Net drivers should set dev->last_rx immediately after netif_rx\n";
108 print "Also make sure the skb isn't referenced after giving it to netif_rx\n";
109 print "$filename:$linenr:$line\n\n";
110 }
111
112 if ($line=~/MOD_(INC|DEC)_USE_COUNT/) {
113 print "MOD_{INC,DEC}_USE_COUNT are deprecated for 2.6: see the module-init-tools FAQ\n";
114 print "$filename:$linenr:$line\n\n";
115 }
116
117 if ($line=~/SET_MODULE_OWNER/) {
118 print "SET_MODULE_OWNER is useless for 2.6: see the module-init-tools FAQ\n";
119 print "$filename:$linenr:$line\n\n";
120 }
121
122 if ($line=~/sleep_on/) {
123 print "Using sleep_on derivative, is racy. consider using wait_event instead\n";
124 print "$filename:$linenr:$line\n\n";
125 }
126
127 if ($line=~/check_region/) {
128 print "Using check_region, is racy, use just request_region and check for its return.\n";
129 print "$filename:$linenr:$line\n\n";
130 }
131
132 if ($line=~/check_mem_region/) {
133 print "Using check_mem_region, is racy, use just request_mem_region and check for its return.\n";
134 print "$filename:$linenr:$line\n\n";
135 }
136
137 if ($line=~/pdev-\>irq/) {
138 print "Make sure pci_enable_device before reading irq\n";
139 check_pci_enable_device($linenr, $filename);
140 print "$filename:$linenr:$line\n\n";
141 }
142
143 if (($lookspci==0) and ($line=~/pci_/)) {
144 $lookspci=1;
145 print "Looks like a PCI driver. Make sure it uses pci_enable_device.\n";
146 check_pci_enable_device($linenr, $filename);
147 print "$filename:$linenr:$line\n\n";
148 }
149
150 if ($line=~/pdev-\>resource/) {
151 print "Make sure pci_enable_device --before-- reading resource\n";
152 check_pci_enable_device($linenr, $filename);
153 print "$filename:$linenr:$line\n\n";
154 }
155
156 if ($line=~/pcibios_/) {
157 print "Uses obsolete pcibios_xxx functions.\n";
158 print "$filename:$linenr:$line\n\n";
159 }
160
161 if ($line=~/save_flags_cli/) {
162 print "Use local_irq_save instead.\n";
163 print "$filename:$linenr:$line\n\n";
164 }
165
166 if ($line=~/isa_{read,write}{b,w,l}/) {
167 print "Use ioremap instead of isa_read/write functions.\n";
168 print "$filename:$linenr:$line\n\n";
169 }
170
171 if ($line=~/current-\>state/) {
172 print "Bad. Should use set_current_state.\n";
173 print "$filename:$linenr:$line\n\n";
174 }
175
176
177 if ($line=~/loops_per_sec/) {
178 print "Warning: loops_per_sec may change..\n";
179 print "$filename:$linenr:$line\n\n";
180 }
181
182 if ($line=~/if.*dev-\>mem_start/) {
183 if (($line!~/0xffffffff/) or ($line!~/-1/)) {
184 print "Should check for 0xffffffff too.\n";
185 print "$filename:$linenr:$line\n\n";
186 }
187 }
188
189 if (($spinlockwarn==0) and
190 (($line=~/cli\(\)/) or
191 ($line=~/sti\(\)/) or
192 ($line=~/lock_kernel/))){
193 $spinlockwarn=1;
194 print "Consider using spinlocks.\n";
195 print "$filename:$linenr:$line\n\n";
196 }
197
198 if ($line=~/return E/) {
199 print "Should be return -E ?";
200 print "$filename:$linenr:$line\n\n";
201 }
202
203 if ($line=~/spin_lock\((.*)\)/) {
204 if (check_spinlock($1, $filename, $line) == 0) {
205 print ("Obtained spinlock on line $linenr, but never unlocked.\n\n");
206 }
207 }
208 }
209 return 1;
210 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.