checking_for_null_instead_of_err_ptr: silence debugfs warnings
[smatch.git] / check_locking.c
blob41823855846218c1d248e4bfd1288d9bcc8cb0ee
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
3 * Copyright (C) 2019 Oracle.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 #include <ctype.h>
20 #include "parse.h"
21 #include "smatch.h"
22 #include "smatch_extra.h"
23 #include "smatch_slist.h"
25 static int my_id;
27 STATE(locked);
28 STATE(half_locked);
29 STATE(start_state);
30 STATE(unlocked);
31 STATE(impossible);
32 STATE(restore);
33 STATE(ignore);
35 enum lock_type {
36 spin_lock,
37 read_lock,
38 write_lock,
39 mutex,
40 bottom_half,
41 irq,
42 sem,
43 prepare_lock,
44 enable_lock,
45 rcu,
46 rcu_read,
49 const char *get_lock_name(enum lock_type type)
51 static const char *names[] = {
52 [spin_lock] = "spin_lock",
53 [read_lock] = "read_lock",
54 [write_lock] = "write_lock",
55 [mutex] = "mutex",
56 [bottom_half] = "bottom_half",
57 [irq] = "irq",
58 [sem] = "sem",
59 [prepare_lock] = "prepare_lock",
60 [enable_lock] = "enable_lock",
61 [rcu] = "rcu",
62 [rcu_read] = "rcu_read",
65 return names[type];
68 #define RETURN_VAL -1
69 #define NO_ARG -2
71 struct lock_info {
72 const char *function;
73 int action;
74 enum lock_type type;
75 int arg;
76 const char *key;
77 const sval_t *implies_start, *implies_end;
78 func_hook *call_back;
81 static struct lock_info lock_table[] = {
82 {"spin_lock", LOCK, spin_lock, 0, "$"},
83 {"spin_unlock", UNLOCK, spin_lock, 0, "$"},
84 {"spin_lock_nested", LOCK, spin_lock, 0, "$"},
85 {"_spin_lock", LOCK, spin_lock, 0, "$"},
86 {"_spin_unlock", UNLOCK, spin_lock, 0, "$"},
87 {"_spin_lock_nested", LOCK, spin_lock, 0, "$"},
88 {"__spin_lock", LOCK, spin_lock, 0, "$"},
89 {"__spin_unlock", UNLOCK, spin_lock, 0, "$"},
90 {"__spin_lock_nested", LOCK, spin_lock, 0, "$"},
91 {"raw_spin_lock", LOCK, spin_lock, 0, "$"},
92 {"raw_spin_unlock", UNLOCK, spin_lock, 0, "$"},
93 {"_raw_spin_lock", LOCK, spin_lock, 0, "$"},
94 {"_raw_spin_lock_nested", LOCK, spin_lock, 0, "$"},
95 {"_raw_spin_unlock", UNLOCK, spin_lock, 0, "$"},
96 {"__raw_spin_lock", LOCK, spin_lock, 0, "$"},
97 {"__raw_spin_unlock", UNLOCK, spin_lock, 0, "$"},
99 {"spin_lock_irq", LOCK, spin_lock, 0, "$"},
100 {"spin_unlock_irq", UNLOCK, spin_lock, 0, "$"},
101 {"_spin_lock_irq", LOCK, spin_lock, 0, "$"},
102 {"_spin_unlock_irq", UNLOCK, spin_lock, 0, "$"},
103 {"__spin_lock_irq", LOCK, spin_lock, 0, "$"},
104 {"__spin_unlock_irq", UNLOCK, spin_lock, 0, "$"},
105 {"_raw_spin_lock_irq", LOCK, spin_lock, 0, "$"},
106 {"_raw_spin_unlock_irq", UNLOCK, spin_lock, 0, "$"},
107 {"__raw_spin_unlock_irq", UNLOCK, spin_lock, 0, "$"},
108 {"spin_lock_irqsave", LOCK, spin_lock, 0, "$"},
109 {"spin_unlock_irqrestore", UNLOCK, spin_lock, 0, "$"},
110 {"_spin_lock_irqsave", LOCK, spin_lock, 0, "$"},
111 {"_spin_unlock_irqrestore", UNLOCK, spin_lock, 0, "$"},
112 {"__spin_lock_irqsave", LOCK, spin_lock, 0, "$"},
113 {"__spin_unlock_irqrestore", UNLOCK, spin_lock, 0, "$"},
114 {"_raw_spin_lock_irqsave", LOCK, spin_lock, 0, "$"},
115 {"_raw_spin_unlock_irqrestore", UNLOCK, spin_lock, 0, "$"},
116 {"__raw_spin_lock_irqsave", LOCK, spin_lock, 0, "$"},
117 {"__raw_spin_unlock_irqrestore", UNLOCK, spin_lock, 0, "$"},
118 {"spin_lock_irqsave_nested", LOCK, spin_lock, 0, "$"},
119 {"_spin_lock_irqsave_nested", LOCK, spin_lock, 0, "$"},
120 {"__spin_lock_irqsave_nested", LOCK, spin_lock, 0, "$"},
121 {"_raw_spin_lock_irqsave_nested", LOCK, spin_lock, 0, "$"},
122 {"spin_lock_bh", LOCK, spin_lock, 0, "$"},
123 {"spin_unlock_bh", UNLOCK, spin_lock, 0, "$"},
124 {"_spin_lock_bh", LOCK, spin_lock, 0, "$"},
125 {"_spin_unlock_bh", UNLOCK, spin_lock, 0, "$"},
126 {"__spin_lock_bh", LOCK, spin_lock, 0, "$"},
127 {"__spin_unlock_bh", UNLOCK, spin_lock, 0, "$"},
129 {"spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
130 {"_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
131 {"__spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
132 {"raw_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
133 {"_raw_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
134 {"spin_trylock_irq", LOCK, spin_lock, 0, "$", &int_one, &int_one},
135 {"spin_trylock_irqsave", LOCK, spin_lock, 0, "$", &int_one, &int_one},
136 {"spin_trylock_bh", LOCK, spin_lock, 0, "$", &int_one, &int_one},
137 {"_spin_trylock_bh", LOCK, spin_lock, 0, "$", &int_one, &int_one},
138 {"__spin_trylock_bh", LOCK, spin_lock, 0, "$", &int_one, &int_one},
139 {"__raw_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
140 {"_atomic_dec_and_lock", LOCK, spin_lock, 1, "$", &int_one, &int_one},
142 {"read_lock", LOCK, read_lock, 0, "$"},
143 {"down_read", LOCK, read_lock, 0, "$"},
144 {"down_read_nested", LOCK, read_lock, 0, "$"},
145 {"down_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
146 {"down_read_killable", LOCK, read_lock, 0, "$", &int_zero, &int_zero},
147 {"up_read", UNLOCK, read_lock, 0, "$"},
148 {"read_unlock", UNLOCK, read_lock, 0, "$"},
149 {"_read_lock", LOCK, read_lock, 0, "$"},
150 {"_read_unlock", UNLOCK, read_lock, 0, "$"},
151 {"__read_lock", LOCK, read_lock, 0, "$"},
152 {"__read_unlock", UNLOCK, read_lock, 0, "$"},
153 {"_raw_read_lock", LOCK, read_lock, 0, "$"},
154 {"_raw_read_unlock", UNLOCK, read_lock, 0, "$"},
155 {"__raw_read_lock", LOCK, read_lock, 0, "$"},
156 {"__raw_read_unlock", UNLOCK, read_lock, 0, "$"},
157 {"read_lock_irq", LOCK, read_lock, 0, "$"},
158 {"read_unlock_irq" , UNLOCK, read_lock, 0, "$"},
159 {"_read_lock_irq", LOCK, read_lock, 0, "$"},
160 {"_read_unlock_irq", UNLOCK, read_lock, 0, "$"},
161 {"__read_lock_irq", LOCK, read_lock, 0, "$"},
162 {"__read_unlock_irq", UNLOCK, read_lock, 0, "$"},
163 {"_raw_read_unlock_irq", UNLOCK, read_lock, 0, "$"},
164 {"_raw_read_lock_irq", LOCK, read_lock, 0, "$"},
165 {"_raw_read_lock_bh", LOCK, read_lock, 0, "$"},
166 {"_raw_read_unlock_bh", UNLOCK, read_lock, 0, "$"},
167 {"read_lock_irqsave", LOCK, read_lock, 0, "$"},
168 {"read_unlock_irqrestore", UNLOCK, read_lock, 0, "$"},
169 {"_read_lock_irqsave", LOCK, read_lock, 0, "$"},
170 {"_read_unlock_irqrestore", UNLOCK, read_lock, 0, "$"},
171 {"__read_lock_irqsave", LOCK, read_lock, 0, "$"},
172 {"__read_unlock_irqrestore", UNLOCK, read_lock, 0, "$"},
173 {"read_lock_bh", LOCK, read_lock, 0, "$"},
174 {"read_unlock_bh", UNLOCK, read_lock, 0, "$"},
175 {"_read_lock_bh", LOCK, read_lock, 0, "$"},
176 {"_read_unlock_bh", UNLOCK, read_lock, 0, "$"},
177 {"__read_lock_bh", LOCK, read_lock, 0, "$"},
178 {"__read_unlock_bh", UNLOCK, read_lock, 0, "$"},
179 {"__raw_read_lock_bh", LOCK, read_lock, 0, "$"},
180 {"__raw_read_unlock_bh", UNLOCK, read_lock, 0, "$"},
182 {"_raw_read_lock_irqsave", LOCK, read_lock, 0, "$"},
183 {"_raw_read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
184 {"_raw_read_unlock_irqrestore", UNLOCK, read_lock, 0, "$"},
185 {"_raw_read_unlock_irqrestore", RESTORE, irq, 1, "$"},
186 {"_raw_spin_lock_bh", LOCK, read_lock, 0, "$"},
187 {"_raw_spin_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
188 {"_raw_spin_lock_nest_lock", LOCK, read_lock, 0, "$"},
189 {"_raw_spin_unlock_bh", UNLOCK, read_lock, 0, "$"},
190 {"_raw_spin_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
191 {"_raw_write_lock_irqsave", LOCK, write_lock, 0, "$"},
192 {"_raw_write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
193 {"_raw_write_unlock_irqrestore", UNLOCK, write_lock, 0, "$"},
194 {"_raw_write_unlock_irqrestore", RESTORE, irq, 1, "$"},
195 {"__raw_write_unlock_irq", UNLOCK, write_lock, 0, "$"},
196 {"__raw_write_unlock_irq", UNLOCK, irq, 0, "irq"},
197 {"__raw_write_unlock_irqrestore", UNLOCK, write_lock, 0, "$"},
198 {"__raw_write_unlock_irqrestore", RESTORE, irq, 1, "$"},
200 {"generic__raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
201 {"read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
202 {"_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
203 {"raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
204 {"_raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
205 {"__raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
206 {"__read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
208 {"write_lock", LOCK, write_lock, 0, "$"},
209 {"down_write", LOCK, write_lock, 0, "$"},
210 {"down_write_nested", LOCK, write_lock, 0, "$"},
211 {"up_write", UNLOCK, write_lock, 0, "$"},
212 {"write_unlock", UNLOCK, write_lock, 0, "$"},
213 {"_write_lock", LOCK, write_lock, 0, "$"},
214 {"_write_unlock", UNLOCK, write_lock, 0, "$"},
215 {"__write_lock", LOCK, write_lock, 0, "$"},
216 {"__write_unlock", UNLOCK, write_lock, 0, "$"},
217 {"write_lock_irq", LOCK, write_lock, 0, "$"},
218 {"write_unlock_irq", UNLOCK, write_lock, 0, "$"},
219 {"_write_lock_irq", LOCK, write_lock, 0, "$"},
220 {"_write_unlock_irq", UNLOCK, write_lock, 0, "$"},
221 {"__write_lock_irq", LOCK, write_lock, 0, "$"},
222 {"__write_unlock_irq", UNLOCK, write_lock, 0, "$"},
223 {"_raw_write_unlock_irq", UNLOCK, write_lock, 0, "$"},
224 {"write_lock_irqsave", LOCK, write_lock, 0, "$"},
225 {"write_unlock_irqrestore", UNLOCK, write_lock, 0, "$"},
226 {"_write_lock_irqsave", LOCK, write_lock, 0, "$"},
227 {"_write_unlock_irqrestore", UNLOCK, write_lock, 0, "$"},
228 {"__write_lock_irqsave", LOCK, write_lock, 0, "$"},
229 {"__write_unlock_irqrestore", UNLOCK, write_lock, 0, "$"},
230 {"write_lock_bh", LOCK, write_lock, 0, "$"},
231 {"write_unlock_bh", UNLOCK, write_lock, 0, "$"},
232 {"_write_lock_bh", LOCK, write_lock, 0, "$"},
233 {"_write_unlock_bh", UNLOCK, write_lock, 0, "$"},
234 {"__write_lock_bh", LOCK, write_lock, 0, "$"},
235 {"__write_unlock_bh", UNLOCK, write_lock, 0, "$"},
236 {"_raw_write_lock", LOCK, write_lock, 0, "$"},
237 {"__raw_write_lock", LOCK, write_lock, 0, "$"},
238 {"_raw_write_unlock", UNLOCK, write_lock, 0, "$"},
239 {"__raw_write_unlock", UNLOCK, write_lock, 0, "$"},
240 {"_raw_write_lock_bh", LOCK, write_lock, 0, "$"},
241 {"_raw_write_unlock_bh", UNLOCK, write_lock, 0, "$"},
242 {"_raw_write_lock_irq", LOCK, write_lock, 0, "$"},
244 {"write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
245 {"_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
246 {"raw_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
247 {"_raw_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
248 {"__write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
249 {"__raw_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
250 {"down_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
251 {"down_write_killable", LOCK, write_lock, 0, "$", &int_zero, &int_zero},
253 {"down", LOCK, sem, 0, "$"},
254 {"up", UNLOCK, sem, 0, "$"},
255 {"down_trylock", LOCK, sem, 0, "$", &int_zero, &int_zero},
256 {"down_timeout", LOCK, sem, 0, "$", &int_zero, &int_zero},
257 {"down_interruptible", LOCK, sem, 0, "$", &int_zero, &int_zero},
258 {"down_killable", LOCK, sem, 0, "$", &int_zero, &int_zero},
261 {"mutex_lock", LOCK, mutex, 0, "$"},
262 {"mutex_unlock", UNLOCK, mutex, 0, "$"},
263 {"mutex_destroy", RESTORE, mutex, 0, "$"},
264 {"mutex_lock_nested", LOCK, mutex, 0, "$"},
265 {"mutex_lock_io", LOCK, mutex, 0, "$"},
266 {"mutex_lock_io_nested", LOCK, mutex, 0, "$"},
268 {"mutex_lock_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
269 {"mutex_lock_interruptible_nested", LOCK, mutex, 0, "$", &int_zero, &int_zero},
270 {"mutex_lock_killable", LOCK, mutex, 0, "$", &int_zero, &int_zero},
271 {"mutex_lock_killable_nested", LOCK, mutex, 0, "$", &int_zero, &int_zero},
273 {"mutex_trylock", LOCK, mutex, 0, "$", &int_one, &int_one},
275 {"ww_mutex_lock", LOCK, mutex, 0, "$"},
276 {"__ww_mutex_lock", LOCK, mutex, 0, "$"},
277 {"ww_mutex_lock_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
278 {"ww_mutex_unlock", UNLOCK, mutex, 0, "$"},
280 {"raw_local_irq_disable", LOCK, irq, NO_ARG, "irq"},
281 {"raw_local_irq_enable", UNLOCK, irq, NO_ARG, "irq"},
282 {"spin_lock_irq", LOCK, irq, NO_ARG, "irq"},
283 {"spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
284 {"_spin_lock_irq", LOCK, irq, NO_ARG, "irq"},
285 {"_spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
286 {"__spin_lock_irq", LOCK, irq, NO_ARG, "irq"},
287 {"__spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
288 {"_raw_spin_lock_irq", LOCK, irq, NO_ARG, "irq"},
289 {"_raw_spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
290 {"__raw_spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
291 {"spin_trylock_irq", LOCK, irq, NO_ARG, "irq", &int_one, &int_one},
292 {"read_lock_irq", LOCK, irq, NO_ARG, "irq"},
293 {"read_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
294 {"_read_lock_irq", LOCK, irq, NO_ARG, "irq"},
295 {"_read_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
296 {"__read_lock_irq", LOCK, irq, NO_ARG, "irq"},
297 {"_raw_read_lock_irq", LOCK, irq, NO_ARG, "irq"},
298 {"__read_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
299 {"_raw_read_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
300 {"write_lock_irq", LOCK, irq, NO_ARG, "irq"},
301 {"write_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
302 {"_write_lock_irq", LOCK, irq, NO_ARG, "irq"},
303 {"_write_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
304 {"__write_lock_irq", LOCK, irq, NO_ARG, "irq"},
305 {"__write_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
306 {"_raw_write_lock_irq", LOCK, irq, NO_ARG, "irq"},
307 {"_raw_write_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
309 {"arch_local_irq_save", LOCK, irq, RETURN_VAL, "$"},
310 {"arch_local_irq_restore", RESTORE, irq, 0, "$"},
311 {"__raw_local_irq_save", LOCK, irq, RETURN_VAL, "$"},
312 {"raw_local_irq_restore", RESTORE, irq, 0, "$"},
313 {"spin_lock_irqsave_nested", LOCK, irq, RETURN_VAL, "$"},
314 {"spin_lock_irqsave", LOCK, irq, 1, "$"},
315 {"spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
316 {"_spin_lock_irqsave_nested", LOCK, irq, RETURN_VAL, "$"},
317 {"_spin_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
318 {"_spin_lock_irqsave", LOCK, irq, 1, "$"},
319 {"_spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
320 {"__spin_lock_irqsave_nested", LOCK, irq, 1, "$"},
321 {"__spin_lock_irqsave", LOCK, irq, 1, "$"},
322 {"__spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
323 {"_raw_spin_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
324 {"_raw_spin_lock_irqsave", LOCK, irq, 1, "$"},
325 {"_raw_spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
326 {"__raw_spin_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
327 {"__raw_spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
328 {"_raw_spin_lock_irqsave_nested", LOCK, irq, RETURN_VAL, "$"},
329 {"spin_trylock_irqsave", LOCK, irq, 1, "$", &int_one, &int_one},
330 {"read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
331 {"read_lock_irqsave", LOCK, irq, 1, "$"},
332 {"read_unlock_irqrestore", RESTORE, irq, 1, "$"},
333 {"_read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
334 {"_read_lock_irqsave", LOCK, irq, 1, "$"},
335 {"_read_unlock_irqrestore", RESTORE, irq, 1, "$"},
336 {"__read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
337 {"__read_unlock_irqrestore", RESTORE, irq, 1, "$"},
338 {"write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
339 {"write_lock_irqsave", LOCK, irq, 1, "$"},
340 {"write_unlock_irqrestore", RESTORE, irq, 1, "$"},
341 {"_write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
342 {"_write_lock_irqsave", LOCK, irq, 1, "$"},
343 {"_write_unlock_irqrestore", RESTORE, irq, 1, "$"},
344 {"__write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
345 {"__write_unlock_irqrestore", RESTORE, irq, 1, "$"},
347 {"local_bh_disable", LOCK, bottom_half, NO_ARG, "bh"},
348 {"_local_bh_disable", LOCK, bottom_half, NO_ARG, "bh"},
349 {"__local_bh_disable", LOCK, bottom_half, NO_ARG, "bh"},
350 {"local_bh_enable", UNLOCK, bottom_half, NO_ARG, "bh"},
351 {"_local_bh_enable", UNLOCK, bottom_half, NO_ARG, "bh"},
352 {"__local_bh_enable", UNLOCK, bottom_half, NO_ARG, "bh"},
353 {"spin_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
354 {"spin_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
355 {"_spin_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
356 {"_spin_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
357 {"__spin_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
358 {"__spin_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
359 {"read_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
360 {"read_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
361 {"_read_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
362 {"_read_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
363 {"__read_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
364 {"__read_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
365 {"_raw_read_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
366 {"_raw_read_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
367 {"write_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
368 {"write_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
369 {"_write_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
370 {"_write_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
371 {"__write_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
372 {"__write_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
373 {"_raw_write_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
374 {"_raw_write_unlock_bh",UNLOCK, bottom_half, NO_ARG, "bh"},
375 {"spin_trylock_bh", LOCK, bottom_half, NO_ARG, "bh", &int_one, &int_one},
376 {"_spin_trylock_bh", LOCK, bottom_half, NO_ARG, "bh", &int_one, &int_one},
377 {"__spin_trylock_bh", LOCK, bottom_half, NO_ARG, "bh", &int_one, &int_one},
379 {"ffs_mutex_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
381 {"clk_prepare_lock", LOCK, prepare_lock, NO_ARG, "clk"},
382 {"clk_prepare_unlock", UNLOCK, prepare_lock, NO_ARG, "clk"},
383 {"clk_enable_lock", LOCK, enable_lock, -1, "$"},
384 {"clk_enable_unlock", UNLOCK, enable_lock, 0, "$"},
386 {"dma_resv_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
387 {"dma_resv_trylock", LOCK, mutex, 0, "$", &int_one, &int_one},
388 {"dma_resv_lock_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
389 {"dma_resv_unlock", UNLOCK, mutex, 0, "$"},
391 {"modeset_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
392 {"drm_ modeset_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
393 {"drm_modeset_lock_single_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
394 {"modeset_unlock", UNLOCK, mutex, 0, "$"},
395 // {"nvkm_i2c_aux_acquire", LOCK, mutex,
396 {"i915_gem_object_lock_interruptible", LOCK, mutex, 0, "$->base.resv", &int_zero, &int_zero},
397 {"i915_gem_object_lock", LOCK, mutex, 0, "$->base.resv"},
398 {"msm_gem_lock", LOCK, mutex, 0, "$->resv"},
400 {"reiserfs_write_lock_nested", LOCK, mutex, 0, "$"},
401 {"reiserfs_write_unlock_nested", UNLOCK, mutex, 0, "$"},
403 {"rw_lock", LOCK, write_lock, 1, "$"},
404 {"rw_unlock", UNLOCK, write_lock, 1, "$"},
406 {"sem_lock", LOCK, mutex, 0, "$"},
407 {"sem_unlock", UNLOCK, mutex, 0, "$"},
409 {"rcu_lock_acquire", LOCK, rcu, NO_ARG, "rcu"},
410 {"rcu_lock_release", UNLOCK, rcu, NO_ARG, "rcu"},
412 {"rcu_read_lock", LOCK, rcu_read, NO_ARG, "rcu_read"},
413 {"rcu_read_unlock", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
414 {"rcu_read_lock_bh", LOCK, rcu_read, NO_ARG, "rcu_read"},
415 {"rcu_read_unlock_bh", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
417 {"rcu_read_lock_sched", LOCK, rcu_read, NO_ARG, "rcu_read"},
418 {"rcu_read_lock_sched_notrace", LOCK, rcu_read, NO_ARG, "rcu_read"},
419 {"rcu_read_unlock_sched", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
420 {"rcu_read_unlock_sched_notrace", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
422 {"gfs2_trans_begin", LOCK, sem, 0, "&$->sd_log_flush_lock", &int_zero, &int_zero},
424 {"lock_sock", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
425 {"lock_sock_nested", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
426 {"lock_sock_fast", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
427 {"__lock_sock", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
428 {"release_sock", UNLOCK, spin_lock, 0, "&$->sk_lock.slock"},
429 {"__release_sock", UNLOCK, spin_lock, 0, "&$->sk_lock.slock"},
431 {"lock_task_sighand", LOCK, spin_lock, 0, "&$->sighand->siglock", &valid_ptr_min_sval, &valid_ptr_max_sval},
433 {"rcu_nocb_unlock_irqrestore", RESTORE, spin_lock, 0, "&$->nocb_lock"},
434 {"rcu_nocb_unlock_irqrestore", RESTORE, irq, 1, "$" },
436 {"bch_write_bdev_super", IGNORE_LOCK, sem, 0, "&$->sb_write_mutex"},
437 {"dlfb_set_video_mode", IGNORE_LOCK, sem, 0, "&$->urbs.limit_sem"},
439 {"efx_rwsem_assert_write_locked", IGNORE_LOCK, sem, 0, "&"},
441 // The i915_gem_ww_ctx_unlock_all() is too complicated
442 {"i915_gem_object_pin_pages_unlocked", IGNORE_LOCK, mutex, 0, "$->base.resv"},
443 {"i915_gem_object_pin_map_unlocked", IGNORE_LOCK, mutex, 0, "$->base.resv"},
444 {"i915_gem_object_fill_blt", IGNORE_LOCK, mutex, 0, "$->base.resv"},
445 {"i915_vma_pin", IGNORE_LOCK, mutex, 0, "$->base.resv"},
447 { "perf_event_period", IGNORE_LOCK, mutex, 0, "&$->ctx->mutex"},
448 { "perf_event_enable", IGNORE_LOCK, mutex, 0, "&$->ctx->mutex"},
450 { "deactivate_locked_super", UNLOCK, spin_lock, 0, "&$->s_umount"},
451 { "ext4_lock_group", LOCK, spin_lock, 0, "$"},
452 { "ext4_unlock_group", UNLOCK, spin_lock, 0, "$"},
457 struct macro_info {
458 const char *macro;
459 int action;
460 int param;
463 static struct macro_info macro_table[] = {
464 {"genpd_lock", LOCK, 0},
465 {"genpd_lock_nested", LOCK, 0},
466 {"genpd_lock_interruptible", LOCK, 0},
467 {"genpd_unlock", UNLOCK, 0},
470 static const char *false_positives[][2] = {
471 {"fs/jffs2/", "->alloc_sem"},
472 {"fs/xfs/", "->b_sema"},
473 {"mm/", "pvmw->ptl"},
474 {"drivers/gpu/drm/i915/", "->base.resv"},
477 static struct stree *start_states;
479 static struct tracker_list *locks;
481 static struct expression *ignored_reset;
482 static void reset(struct sm_state *sm, struct expression *mod_expr)
484 struct expression *faked;
486 if (mod_expr && mod_expr->type == EXPR_ASSIGNMENT &&
487 mod_expr->left == ignored_reset)
488 return;
489 faked = get_faked_expression();
490 if (faked && faked->type == EXPR_ASSIGNMENT &&
491 faked->left == ignored_reset)
492 return;
494 set_state(my_id, sm->name, sm->sym, &start_state);
497 static struct smatch_state *get_start_state(struct sm_state *sm)
499 struct smatch_state *orig;
501 if (!sm)
502 return NULL;
504 orig = get_state_stree(start_states, my_id, sm->name, sm->sym);
505 if (orig)
506 return orig;
507 return NULL;
510 static struct expression *remove_spinlock_check(struct expression *expr)
512 if (expr->type != EXPR_CALL)
513 return expr;
514 if (expr->fn->type != EXPR_SYMBOL)
515 return expr;
516 if (strcmp(expr->fn->symbol_name->name, "spinlock_check"))
517 return expr;
518 expr = get_argument_from_call_expr(expr->args, 0);
519 return expr;
522 static struct expression *filter_kernel_args(struct expression *arg)
524 if (arg->type == EXPR_PREOP && arg->op == '&')
525 return strip_expr(arg->unop);
526 if (!is_pointer(arg))
527 return arg;
528 return deref_expression(strip_expr(arg));
531 static char *lock_to_name_sym(struct expression *expr, struct symbol **sym)
533 expr = remove_spinlock_check(expr);
534 expr = filter_kernel_args(expr);
535 return expr_to_str_sym(expr, sym);
538 static struct smatch_state *unmatched_state(struct sm_state *sm)
540 return &start_state;
543 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
545 if (is_impossible_path())
546 set_state(my_id, cur->name, cur->sym, &impossible);
549 static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
551 if (s1 == &impossible)
552 return s2;
553 if (s2 == &impossible)
554 return s1;
555 return &merged;
558 static struct sm_state *get_best_match(const char *key, int lock_unlock)
560 struct sm_state *sm;
561 struct sm_state *match;
562 int cnt = 0;
563 int start_pos, state_len, key_len, chunks, i;
565 if (strncmp(key, "$->", 3) == 0)
566 key += 3;
568 key_len = strlen(key);
569 chunks = 0;
570 for (i = key_len - 1; i > 0; i--) {
571 if (key[i] == '>' || key[i] == '.')
572 chunks++;
573 if (chunks == 2) {
574 key += (i + 1);
575 key_len = strlen(key);
576 break;
580 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
581 if (((lock_unlock == UNLOCK || lock_unlock == RESTORE) &&
582 sm->state != &locked) ||
583 (lock_unlock == LOCK && sm->state != &unlocked))
584 continue;
585 state_len = strlen(sm->name);
586 if (state_len < key_len)
587 continue;
588 start_pos = state_len - key_len;
589 if ((start_pos == 0 || !isalnum(sm->name[start_pos - 1])) &&
590 strcmp(sm->name + start_pos, key) == 0) {
591 cnt++;
592 match = sm;
594 } END_FOR_EACH_SM(sm);
596 if (cnt == 1)
597 return match;
598 return NULL;
601 static char *use_best_match(const char *key, int lock_unlock, struct symbol **sym)
603 struct sm_state *match;
605 match = get_best_match(key, lock_unlock);
606 if (!match) {
607 *sym = NULL;
608 return alloc_string(key);
610 *sym = match->sym;
611 return alloc_string(match->name);
614 static void set_start_state(const char *name, struct symbol *sym, struct smatch_state *start)
616 struct smatch_state *orig;
618 orig = get_state_stree(start_states, my_id, name, sym);
619 if (!orig)
620 set_state_stree(&start_states, my_id, name, sym, start);
621 else if (orig != start)
622 set_state_stree(&start_states, my_id, name, sym, &undefined);
625 static bool common_false_positive(const char *name)
627 const char *path, *lname;
628 int i, len_total, len_path, len_name, skip;
630 if (!get_filename())
631 return false;
633 len_total = strlen(name);
634 for (i = 0; i < ARRAY_SIZE(false_positives); i++) {
635 path = false_positives[i][0];
636 lname = false_positives[i][1];
638 len_path = strlen(path);
639 len_name = strlen(lname);
641 if (len_name > len_total)
642 continue;
643 skip = len_total - len_name;
645 if (strncmp(get_filename(), path, len_path) == 0 &&
646 strcmp(name + skip, lname) == 0)
647 return true;
650 return false;
653 static bool sm_in_start_states(struct sm_state *sm)
655 if (!sm || !cur_func_sym)
656 return false;
657 if (sm->line == cur_func_sym->pos.line)
658 return true;
659 return false;
662 static void warn_on_double(struct sm_state *sm, struct smatch_state *state)
664 struct sm_state *tmp;
666 if (!sm)
667 return;
669 FOR_EACH_PTR(sm->possible, tmp) {
670 if (tmp->state == state)
671 goto found;
672 } END_FOR_EACH_PTR(tmp);
674 return;
675 found:
676 // FIXME: called with read_lock held
677 // drivers/scsi/aic7xxx/aic7xxx_osm.c:1591 ahc_linux_isr() error: double locked 'flags' (orig line 1584)
678 if (strcmp(sm->name, "bottom_half") == 0)
679 return;
680 if (strstr(sm->name, "rcu"))
681 return;
682 if (strstr(sm->name, "timeline->mutex"))
683 return;
684 if (common_false_positive(sm->name))
685 return;
687 if (state == &locked && sm_in_start_states(tmp)) {
688 // sm_warning("called with lock held. '%s'", sm->name);
689 } else {
690 // sm_msg("error: double %s '%s' (orig line %u)",
691 // state->name, sm->name, tmp->line);
695 static bool handle_macro_lock_unlock(void)
697 struct expression *expr, *arg;
698 struct macro_info *info;
699 struct sm_state *sm;
700 struct symbol *sym;
701 const char *macro;
702 char *name;
703 bool ret = false;
704 int i;
706 expr = last_ptr_list((struct ptr_list *)big_expression_stack);
707 while (expr && expr->type == EXPR_ASSIGNMENT)
708 expr = strip_expr(expr->right);
709 if (!expr || expr->type != EXPR_CALL)
710 return false;
712 macro = get_macro_name(expr->pos);
713 if (!macro)
714 return false;
716 for (i = 0; i < ARRAY_SIZE(macro_table); i++) {
717 info = &macro_table[i];
719 if (strcmp(macro, info->macro) != 0)
720 continue;
721 arg = get_argument_from_call_expr(expr->args, info->param);
722 name = expr_to_str_sym(arg, &sym);
723 if (!name || !sym)
724 goto free;
725 sm = get_sm_state(my_id, name, sym);
727 if (info->action == LOCK) {
728 if (!get_start_state(sm))
729 set_start_state(name, sym, &unlocked);
730 if (sm && sm->line != expr->pos.line)
731 warn_on_double(sm, &locked);
732 set_state(my_id, name, sym, &locked);
733 } else {
734 if (!get_start_state(sm))
735 set_start_state(name, sym, &locked);
736 if (sm && sm->line != expr->pos.line)
737 warn_on_double(sm, &unlocked);
738 set_state(my_id, name, sym, &unlocked);
740 ret = true;
741 free:
742 free_string(name);
743 return ret;
745 return false;
748 static bool is_local_IRQ_save(const char *name, struct symbol *sym, struct lock_info *info)
750 if (name && strcmp(name, "flags") == 0)
751 return true;
752 if (!sym)
753 return false;
754 if (!sym->ident || strcmp(sym->ident->name, name) != 0)
755 return false;
756 if (!info)
757 return false;
758 return strstr(info->function, "irq") && strstr(info->function, "save");
761 static void do_lock(const char *name, struct symbol *sym, struct lock_info *info)
763 struct sm_state *sm;
764 bool delete_null = false;
766 if (!info && handle_macro_lock_unlock())
767 return;
769 add_tracker(&locks, my_id, name, sym);
771 sm = get_sm_state(my_id, name, sym);
772 if (!get_start_state(sm))
773 set_start_state(name, sym, &unlocked);
774 if (!sm && !is_local_IRQ_save(name, sym, info) && !sym) {
775 sm = get_best_match(name, LOCK);
776 if (sm) {
777 name = sm->name;
778 if (sm->sym)
779 sym = sm->sym;
780 else
781 delete_null = true;
784 warn_on_double(sm, &locked);
785 if (delete_null)
786 set_state(my_id, name, NULL, &ignore);
788 set_state(my_id, name, sym, &locked);
791 static void do_unlock(const char *name, struct symbol *sym, struct lock_info *info)
793 struct sm_state *sm;
794 bool delete_null = false;
796 if (__path_is_null())
797 return;
799 if (!info && handle_macro_lock_unlock())
800 return;
802 add_tracker(&locks, my_id, name, sym);
803 sm = get_sm_state(my_id, name, sym);
804 if (!sm && !info && !is_local_IRQ_save(name, sym, info)) {
805 sm = get_best_match(name, UNLOCK);
806 if (sm) {
807 name = sm->name;
808 if (sm->sym)
809 sym = sm->sym;
810 else
811 delete_null = true;
814 if (!get_start_state(sm))
815 set_start_state(name, sym, &locked);
816 warn_on_double(sm, &unlocked);
817 if (delete_null)
818 set_state(my_id, name, NULL, &ignore);
819 set_state(my_id, name, sym, &unlocked);
822 static void do_restore(const char *name, struct symbol *sym, struct lock_info *info)
824 struct sm_state *sm;
826 if (__path_is_null())
827 return;
829 sm = get_sm_state(my_id, name, sym);
830 if (!get_start_state(sm))
831 set_start_state(name, sym, &locked);
833 add_tracker(&locks, my_id, name, sym);
834 set_state(my_id, name, sym, &restore);
837 static int get_db_type(struct sm_state *sm)
840 * Bottom half is complicated because it's nestable.
841 * Say it's merged at the start and we lock and unlock then
842 * it should go back to merged.
844 if (sm->state == get_start_state(sm)) {
845 if (sm->state == &locked)
846 return KNOWN_LOCKED;
847 if (sm->state == &unlocked)
848 return KNOWN_UNLOCKED;
851 if (sm->state == &locked)
852 return LOCK;
853 if (sm->state == &unlocked)
854 return UNLOCK;
855 if (sm->state == &restore)
856 return RESTORE;
857 return LOCK;
860 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
862 struct smatch_state *start;
863 struct sm_state *sm;
864 const char *param_name;
865 int param;
867 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
868 if (sm->state != &locked &&
869 sm->state != &unlocked &&
870 sm->state != &restore)
871 continue;
872 if (sm->name[0] == '$')
873 continue;
876 * If the state is locked at the end, that doesn't mean
877 * anything. It could be been locked at the start. Or
878 * it could be &merged at the start but its locked now
879 * because of implications and not because we set the
880 * state.
882 * This is slightly a hack, but when we change the state, we
883 * call set_start_state() so if get_start_state() returns NULL
884 * that means we haven't manually the locked state.
886 start = get_start_state(sm);
887 if (sm->state == &restore) {
888 if (start != &locked)
889 continue;
890 } else if (!start || sm->state == start)
891 continue; /* !start means it was passed in */
893 param = get_param_key_from_sm(sm, expr, &param_name);
894 sql_insert_return_states(return_id, return_ranges,
895 get_db_type(sm),
896 param, param_name, "");
897 } END_FOR_EACH_SM(sm);
900 static int sm_both_locked_and_unlocked(struct sm_state *sm)
902 int is_locked = 0;
903 int is_unlocked = 0;
904 struct sm_state *tmp;
906 if (sm->state != &merged)
907 return 0;
909 FOR_EACH_PTR(sm->possible, tmp) {
910 if (tmp->state == &locked)
911 is_locked = 1;
912 if (tmp->state == &unlocked)
913 is_unlocked = 1;
914 } END_FOR_EACH_PTR(tmp);
916 return is_locked && is_unlocked;
920 static bool is_EINTR(struct range_list *rl)
922 sval_t sval;
924 if (!rl_to_sval(rl, &sval))
925 return false;
926 return sval.value == -4;
929 static bool sym_in_lock_table(struct symbol *sym)
931 int i;
933 if (!sym || !sym->ident)
934 return false;
936 for (i = 0; lock_table[i].function != NULL; i++) {
937 if (strcmp(lock_table[i].function, sym->ident->name) == 0)
938 return true;
940 return false;
943 static bool func_in_lock_table(struct expression *expr)
945 if (expr->type != EXPR_SYMBOL)
946 return false;
947 return sym_in_lock_table(expr->symbol);
950 #define NUM_BUCKETS (RET_UNKNOWN + 1)
952 static void check_lock(char *name, struct symbol *sym)
954 struct range_list *locked_lines = NULL;
955 struct range_list *unlocked_lines = NULL;
956 int locked_buckets[NUM_BUCKETS] = {};
957 int unlocked_buckets[NUM_BUCKETS] = {};
958 struct stree *stree, *orig;
959 struct sm_state *return_sm;
960 struct sm_state *sm;
961 sval_t line = sval_type_val(&int_ctype, 0);
962 int bucket;
963 int i;
965 if (sym_in_lock_table(cur_func_sym))
966 return;
968 FOR_EACH_PTR(get_all_return_strees(), stree) {
969 orig = __swap_cur_stree(stree);
971 if (is_impossible_path())
972 goto swap_stree;
974 return_sm = get_sm_state(RETURN_ID, "return_ranges", NULL);
975 if (!return_sm)
976 goto swap_stree;
977 line.value = return_sm->line;
979 sm = get_sm_state(my_id, name, sym);
980 if (!sm)
981 goto swap_stree;
983 if (parent_is_gone_var_sym(sm->name, sm->sym))
984 goto swap_stree;
986 if (sm->state != &locked &&
987 sm->state != &unlocked &&
988 sm->state != &restore)
989 goto swap_stree;
991 if ((sm->state == &unlocked || sm->state == &restore) &&
992 is_EINTR(estate_rl(return_sm->state)))
993 goto swap_stree;
995 bucket = success_fail_return(estate_rl(return_sm->state));
996 if (sm->state == &locked) {
997 add_range(&locked_lines, line, line);
998 locked_buckets[bucket] = true;
1000 if (sm->state == &unlocked || sm->state == &restore) {
1001 add_range(&unlocked_lines, line, line);
1002 unlocked_buckets[bucket] = true;
1004 swap_stree:
1005 __swap_cur_stree(orig);
1006 } END_FOR_EACH_PTR(stree);
1009 if (!locked_lines || !unlocked_lines)
1010 return;
1012 for (i = 0; i < NUM_BUCKETS; i++) {
1013 if (locked_buckets[i] && unlocked_buckets[i])
1014 goto complain;
1017 if (locked_buckets[RET_FAIL])
1018 goto complain;
1020 return;
1022 complain:
1023 if (common_false_positive(name))
1024 return;
1026 sm_msg("warn: inconsistent returns '%s'.", name);
1027 sm_printf(" Locked on : %s\n", show_rl(locked_lines));
1028 sm_printf(" Unlocked on: %s\n", show_rl(unlocked_lines));
1031 static void match_func_end(struct symbol *sym)
1033 struct tracker *tracker;
1035 FOR_EACH_PTR(locks, tracker) {
1036 check_lock(tracker->name, tracker->sym);
1037 } END_FOR_EACH_PTR(tracker);
1040 static void db_param_locked_unlocked(struct expression *expr, int param, const char *key, int lock_unlock, struct lock_info *info)
1042 struct expression *call, *arg;
1043 char *name;
1044 struct symbol *sym;
1046 if (info && info->action == IGNORE_LOCK)
1047 return;
1049 call = expr;
1050 while (call->type == EXPR_ASSIGNMENT)
1051 call = strip_expr(call->right);
1052 if (call->type != EXPR_CALL)
1053 return;
1055 if (!info && func_in_lock_table(call->fn))
1056 return;
1058 if (param == -2) {
1059 if (!info)
1060 name = use_best_match(key, lock_unlock, &sym);
1061 else {
1062 name = alloc_string(info->key);
1063 sym = NULL;
1065 } else if (param == -1) {
1066 if (expr->type != EXPR_ASSIGNMENT)
1067 return;
1068 ignored_reset = expr->left;
1070 name = get_variable_from_key(expr->left, key, &sym);
1071 } else {
1072 arg = get_argument_from_call_expr(call->args, param);
1073 if (!arg)
1074 return;
1076 arg = remove_spinlock_check(arg);
1077 name = get_variable_from_key(arg, key, &sym);
1080 if (!name || !sym)
1081 goto free;
1083 if (lock_unlock == LOCK)
1084 do_lock(name, sym, info);
1085 else if (lock_unlock == UNLOCK)
1086 do_unlock(name, sym, info);
1087 else if (lock_unlock == RESTORE)
1088 do_restore(name, sym, info);
1090 free:
1091 free_string(name);
1094 static void db_param_locked(struct expression *expr, int param, char *key, char *value)
1096 db_param_locked_unlocked(expr, param, key, LOCK, NULL);
1099 static void db_param_unlocked(struct expression *expr, int param, char *key, char *value)
1101 db_param_locked_unlocked(expr, param, key, UNLOCK, NULL);
1104 static void db_param_restore(struct expression *expr, int param, char *key, char *value)
1106 db_param_locked_unlocked(expr, param, key, RESTORE, NULL);
1109 static void match_lock_unlock(const char *fn, struct expression *expr, void *data)
1111 struct lock_info *info = data;
1112 struct expression *parent;
1114 if (info->arg == -1) {
1115 parent = expr_get_parent_expr(expr);
1116 while (parent && parent->type != EXPR_ASSIGNMENT)
1117 parent = expr_get_parent_expr(parent);
1118 if (!parent || parent->type != EXPR_ASSIGNMENT)
1119 return;
1120 expr = parent;
1123 db_param_locked_unlocked(expr, info->arg, info->key, info->action, info);
1126 static void match_lock_held(const char *fn, struct expression *call_expr,
1127 struct expression *assign_expr, void *data)
1129 struct lock_info *info = data;
1131 db_param_locked_unlocked(assign_expr ?: call_expr, info->arg, info->key, info->action, info);
1134 static void match_assign(struct expression *expr)
1136 struct smatch_state *state;
1138 /* This is only for the DB */
1139 if (is_fake_var_assign(expr))
1140 return;
1141 state = get_state_expr(my_id, expr->right);
1142 if (!state)
1143 return;
1144 set_state_expr(my_id, expr->left, state);
1147 static struct stree *printed;
1148 static void call_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1150 int locked_type = 0;
1152 if (sm->state == &locked)
1153 locked_type = LOCK;
1154 else if (sm->state == &unlocked)
1155 locked_type = UNLOCK;
1156 else if (slist_has_state(sm->possible, &locked) ||
1157 slist_has_state(sm->possible, &half_locked))
1158 locked_type = HALF_LOCKED;
1159 else
1160 return;
1162 avl_insert(&printed, sm);
1163 sql_insert_caller_info(call, locked_type, param, printed_name, "");
1166 static void match_call_info(struct expression *expr)
1168 struct sm_state *sm;
1169 const char *name;
1170 int locked_type;
1172 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1173 if (sm->state == &locked)
1174 locked_type = LOCK;
1175 else if (sm->state == &half_locked ||
1176 slist_has_state(sm->possible, &locked))
1177 locked_type = HALF_LOCKED;
1178 else
1179 continue;
1181 if (avl_lookup(printed, sm))
1182 continue;
1184 if (strcmp(sm->name, "bottom_half") == 0)
1185 name = "bh";
1186 else if (strcmp(sm->name, "rcu_read") == 0)
1187 name = "rcu_read_lock";
1188 else
1189 name = sm->name;
1191 if (strncmp(name, "__fake_param", 12) == 0 ||
1192 strchr(name, '$'))
1193 continue;
1195 sql_insert_caller_info(expr, locked_type, -2, name, "");
1196 } END_FOR_EACH_SM(sm);
1197 free_stree(&printed);
1200 static void set_locked(const char *name, struct symbol *sym, char *value)
1202 set_state(my_id, name, sym, &locked);
1205 static void set_half_locked(const char *name, struct symbol *sym, char *value)
1207 set_state(my_id, name, sym, &half_locked);
1210 static void set_unlocked(const char *name, struct symbol *sym, char *value)
1212 set_state(my_id, name, sym, &unlocked);
1215 static void match_after_func(struct symbol *sym)
1217 free_stree(&start_states);
1220 static void match_dma_resv_lock_NULL(const char *fn, struct expression *call_expr,
1221 struct expression *assign_expr, void *_index)
1223 struct expression *lock, *ctx;
1224 char *lock_name;
1225 struct symbol *sym;
1227 lock = get_argument_from_call_expr(call_expr->args, 0);
1228 ctx = get_argument_from_call_expr(call_expr->args, 1);
1229 if (!expr_is_zero(ctx))
1230 return;
1232 lock_name = lock_to_name_sym(lock, &sym);
1233 if (!lock_name || !sym)
1234 goto free;
1235 do_lock(lock_name, sym, NULL);
1236 free:
1237 free_string(lock_name);
1240 /* print_held_locks() is used in check_call_tree.c */
1241 void print_held_locks(void)
1243 struct stree *stree;
1244 struct sm_state *sm;
1245 int i = 0;
1247 stree = __get_cur_stree();
1248 FOR_EACH_MY_SM(my_id, stree, sm) {
1249 if (sm->state != &locked)
1250 continue;
1251 if (i++)
1252 sm_printf(" ");
1253 sm_printf("'%s'", sm->name);
1254 } END_FOR_EACH_SM(sm);
1257 static void load_table(struct lock_info *lock_table)
1259 int i;
1261 for (i = 0; lock_table[i].function != NULL; i++) {
1262 struct lock_info *lock = &lock_table[i];
1264 if (lock->call_back) {
1265 add_function_hook(lock->function, lock->call_back, lock);
1266 } else if (lock->implies_start) {
1267 return_implies_state_sval(lock->function,
1268 *lock->implies_start,
1269 *lock->implies_end,
1270 &match_lock_held, lock);
1271 } else {
1272 add_function_hook(lock->function, &match_lock_unlock, lock);
1277 static bool is_smp_config(void)
1279 struct ident *id;
1281 id = built_in_ident("CONFIG_SMP");
1282 return !!lookup_symbol(id, NS_MACRO);
1285 void check_locking(int id)
1287 my_id = id;
1289 if (option_project != PROJ_KERNEL)
1290 return;
1292 if (!is_smp_config())
1293 return;
1295 load_table(lock_table);
1297 set_dynamic_states(my_id);
1298 add_unmatched_state_hook(my_id, &unmatched_state);
1299 add_pre_merge_hook(my_id, &pre_merge_hook);
1300 add_merge_hook(my_id, &merge_func);
1301 add_modification_hook(my_id, &reset);
1303 add_hook(&match_assign, ASSIGNMENT_HOOK);
1304 add_hook(&match_func_end, END_FUNC_HOOK);
1306 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1307 add_function_data((unsigned long *)&start_states);
1309 add_caller_info_callback(my_id, call_info_callback);
1310 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1312 add_split_return_callback(match_return_info);
1313 select_return_states_hook(LOCK, &db_param_locked);
1314 select_return_states_hook(UNLOCK, &db_param_unlocked);
1315 select_return_states_hook(RESTORE, &db_param_restore);
1317 select_caller_name_sym(set_locked, LOCK);
1318 select_caller_name_sym(set_half_locked, HALF_LOCKED);
1319 select_caller_name_sym(set_unlocked, UNLOCK);
1321 return_implies_state("dma_resv_lock", -4095, -1, &match_dma_resv_lock_NULL, 0);