flow: set a flag insidehandle_builtin_overflow_func()
[smatch.git] / check_locking.c
blob728a2db598d0d8af59b8a75cd91dd7db60b7854d
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"},
399 {"reiserfs_write_lock_nested", LOCK, mutex, 0, "$"},
400 {"reiserfs_write_unlock_nested", UNLOCK, mutex, 0, "$"},
402 {"rw_lock", LOCK, write_lock, 1, "$"},
403 {"rw_unlock", UNLOCK, write_lock, 1, "$"},
405 {"sem_lock", LOCK, mutex, 0, "$"},
406 {"sem_unlock", UNLOCK, mutex, 0, "$"},
408 {"rcu_lock_acquire", LOCK, rcu, NO_ARG, "rcu"},
409 {"rcu_lock_release", UNLOCK, rcu, NO_ARG, "rcu"},
411 {"rcu_read_lock", LOCK, rcu_read, NO_ARG, "rcu_read"},
412 {"rcu_read_unlock", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
413 {"rcu_read_lock_bh", LOCK, rcu_read, NO_ARG, "rcu_read"},
414 {"rcu_read_unlock_bh", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
416 {"rcu_read_lock_sched", LOCK, rcu_read, NO_ARG, "rcu_read"},
417 {"rcu_read_lock_sched_notrace", LOCK, rcu_read, NO_ARG, "rcu_read"},
418 {"rcu_read_unlock_sched", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
419 {"rcu_read_unlock_sched_notrace", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
421 {"bch_write_bdev_super", IGNORE_LOCK, sem, 0, "&$->sb_write_mutex"},
422 {"dlfb_set_video_mode", IGNORE_LOCK, sem, 0, "&$->urbs.limit_sem"},
424 {"efx_rwsem_assert_write_locked", IGNORE_LOCK, sem, 0, "&"},
426 // The i915_gem_ww_ctx_unlock_all() is too complicated
427 {"i915_gem_object_pin_pages_unlocked", IGNORE_LOCK, mutex, 0, "$->base.resv"},
428 {"i915_gem_object_pin_map_unlocked", IGNORE_LOCK, mutex, 0, "$->base.resv"},
429 {"i915_gem_object_fill_blt", IGNORE_LOCK, mutex, 0, "$->base.resv"},
430 {"i915_vma_pin", IGNORE_LOCK, mutex, 0, "$->base.resv"},
435 struct macro_info {
436 const char *macro;
437 int action;
438 int param;
441 static struct macro_info macro_table[] = {
442 {"genpd_lock", LOCK, 0},
443 {"genpd_lock_nested", LOCK, 0},
444 {"genpd_lock_interruptible", LOCK, 0},
445 {"genpd_unlock", UNLOCK, 0},
448 static const char *false_positives[][2] = {
449 {"fs/jffs2/", "->alloc_sem"},
450 {"fs/xfs/", "->b_sema"},
451 {"mm/", "pvmw->ptl"},
452 {"drivers/gpu/drm/i915/", "->base.resv"},
455 static struct stree *start_states;
457 static struct tracker_list *locks;
459 static struct expression *ignored_reset;
460 static void reset(struct sm_state *sm, struct expression *mod_expr)
462 struct expression *faked;
464 if (mod_expr && mod_expr->type == EXPR_ASSIGNMENT &&
465 mod_expr->left == ignored_reset)
466 return;
467 faked = get_faked_expression();
468 if (faked && faked->type == EXPR_ASSIGNMENT &&
469 faked->left == ignored_reset)
470 return;
472 set_state(my_id, sm->name, sm->sym, &start_state);
475 static struct smatch_state *get_start_state(struct sm_state *sm)
477 struct smatch_state *orig;
479 if (!sm)
480 return NULL;
482 orig = get_state_stree(start_states, my_id, sm->name, sm->sym);
483 if (orig)
484 return orig;
485 return NULL;
488 static struct expression *remove_spinlock_check(struct expression *expr)
490 if (expr->type != EXPR_CALL)
491 return expr;
492 if (expr->fn->type != EXPR_SYMBOL)
493 return expr;
494 if (strcmp(expr->fn->symbol_name->name, "spinlock_check"))
495 return expr;
496 expr = get_argument_from_call_expr(expr->args, 0);
497 return expr;
500 static struct expression *filter_kernel_args(struct expression *arg)
502 if (arg->type == EXPR_PREOP && arg->op == '&')
503 return strip_expr(arg->unop);
504 if (!is_pointer(arg))
505 return arg;
506 return deref_expression(strip_expr(arg));
509 static char *lock_to_name_sym(struct expression *expr, struct symbol **sym)
511 expr = remove_spinlock_check(expr);
512 expr = filter_kernel_args(expr);
513 return expr_to_str_sym(expr, sym);
516 static struct smatch_state *unmatched_state(struct sm_state *sm)
518 return &start_state;
521 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
523 if (is_impossible_path())
524 set_state(my_id, cur->name, cur->sym, &impossible);
527 static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
529 if (s1 == &impossible)
530 return s2;
531 if (s2 == &impossible)
532 return s1;
533 return &merged;
536 static struct sm_state *get_best_match(const char *key, int lock_unlock)
538 struct sm_state *sm;
539 struct sm_state *match;
540 int cnt = 0;
541 int start_pos, state_len, key_len, chunks, i;
543 if (strncmp(key, "$->", 3) == 0)
544 key += 3;
546 key_len = strlen(key);
547 chunks = 0;
548 for (i = key_len - 1; i > 0; i--) {
549 if (key[i] == '>' || key[i] == '.')
550 chunks++;
551 if (chunks == 2) {
552 key += (i + 1);
553 key_len = strlen(key);
554 break;
558 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
559 if (((lock_unlock == UNLOCK || lock_unlock == RESTORE) &&
560 sm->state != &locked) ||
561 (lock_unlock == LOCK && sm->state != &unlocked))
562 continue;
563 state_len = strlen(sm->name);
564 if (state_len < key_len)
565 continue;
566 start_pos = state_len - key_len;
567 if ((start_pos == 0 || !isalnum(sm->name[start_pos - 1])) &&
568 strcmp(sm->name + start_pos, key) == 0) {
569 cnt++;
570 match = sm;
572 } END_FOR_EACH_SM(sm);
574 if (cnt == 1)
575 return match;
576 return NULL;
579 static char *use_best_match(const char *key, int lock_unlock, struct symbol **sym)
581 struct sm_state *match;
583 match = get_best_match(key, lock_unlock);
584 if (!match) {
585 *sym = NULL;
586 return alloc_string(key);
588 *sym = match->sym;
589 return alloc_string(match->name);
592 static void set_start_state(const char *name, struct symbol *sym, struct smatch_state *start)
594 struct smatch_state *orig;
596 orig = get_state_stree(start_states, my_id, name, sym);
597 if (!orig)
598 set_state_stree(&start_states, my_id, name, sym, start);
599 else if (orig != start)
600 set_state_stree(&start_states, my_id, name, sym, &undefined);
603 static bool common_false_positive(const char *name)
605 const char *path, *lname;
606 int i, len_total, len_path, len_name, skip;
608 if (!get_filename())
609 return false;
611 len_total = strlen(name);
612 for (i = 0; i < ARRAY_SIZE(false_positives); i++) {
613 path = false_positives[i][0];
614 lname = false_positives[i][1];
616 len_path = strlen(path);
617 len_name = strlen(lname);
619 if (len_name > len_total)
620 continue;
621 skip = len_total - len_name;
623 if (strncmp(get_filename(), path, len_path) == 0 &&
624 strcmp(name + skip, lname) == 0)
625 return true;
628 return false;
631 static bool sm_in_start_states(struct sm_state *sm)
633 if (!sm || !cur_func_sym)
634 return false;
635 if (sm->line == cur_func_sym->pos.line)
636 return true;
637 return false;
640 static void warn_on_double(struct sm_state *sm, struct smatch_state *state)
642 struct sm_state *tmp;
644 if (!sm)
645 return;
647 FOR_EACH_PTR(sm->possible, tmp) {
648 if (tmp->state == state)
649 goto found;
650 } END_FOR_EACH_PTR(tmp);
652 return;
653 found:
654 // FIXME: called with read_lock held
655 // drivers/scsi/aic7xxx/aic7xxx_osm.c:1591 ahc_linux_isr() error: double locked 'flags' (orig line 1584)
656 if (strcmp(sm->name, "bottom_half") == 0)
657 return;
658 if (strstr(sm->name, "rcu"))
659 return;
660 if (strstr(sm->name, "timeline->mutex"))
661 return;
662 if (common_false_positive(sm->name))
663 return;
665 if (state == &locked && sm_in_start_states(tmp)) {
666 // sm_warning("called with lock held. '%s'", sm->name);
667 } else {
668 // sm_msg("error: double %s '%s' (orig line %u)",
669 // state->name, sm->name, tmp->line);
673 static bool handle_macro_lock_unlock(void)
675 struct expression *expr, *arg;
676 struct macro_info *info;
677 struct sm_state *sm;
678 struct symbol *sym;
679 const char *macro;
680 char *name;
681 bool ret = false;
682 int i;
684 expr = last_ptr_list((struct ptr_list *)big_expression_stack);
685 while (expr && expr->type == EXPR_ASSIGNMENT)
686 expr = strip_expr(expr->right);
687 if (!expr || expr->type != EXPR_CALL)
688 return false;
690 macro = get_macro_name(expr->pos);
691 if (!macro)
692 return false;
694 for (i = 0; i < ARRAY_SIZE(macro_table); i++) {
695 info = &macro_table[i];
697 if (strcmp(macro, info->macro) != 0)
698 continue;
699 arg = get_argument_from_call_expr(expr->args, info->param);
700 name = expr_to_str_sym(arg, &sym);
701 if (!name || !sym)
702 goto free;
703 sm = get_sm_state(my_id, name, sym);
705 if (info->action == LOCK) {
706 if (!get_start_state(sm))
707 set_start_state(name, sym, &unlocked);
708 if (sm && sm->line != expr->pos.line)
709 warn_on_double(sm, &locked);
710 set_state(my_id, name, sym, &locked);
711 } else {
712 if (!get_start_state(sm))
713 set_start_state(name, sym, &locked);
714 if (sm && sm->line != expr->pos.line)
715 warn_on_double(sm, &unlocked);
716 set_state(my_id, name, sym, &unlocked);
718 ret = true;
719 free:
720 free_string(name);
721 return ret;
723 return false;
726 static bool is_local_IRQ_save(const char *name, struct symbol *sym, struct lock_info *info)
728 if (name && strcmp(name, "flags") == 0)
729 return true;
730 if (!sym)
731 return false;
732 if (!sym->ident || strcmp(sym->ident->name, name) != 0)
733 return false;
734 if (!info)
735 return false;
736 return strstr(info->function, "irq") && strstr(info->function, "save");
739 static void do_lock(const char *name, struct symbol *sym, struct lock_info *info)
741 struct sm_state *sm;
742 bool delete_null = false;
744 if (!info && handle_macro_lock_unlock())
745 return;
747 add_tracker(&locks, my_id, name, sym);
749 sm = get_sm_state(my_id, name, sym);
750 if (!get_start_state(sm))
751 set_start_state(name, sym, &unlocked);
752 if (!sm && !is_local_IRQ_save(name, sym, info) && !sym) {
753 sm = get_best_match(name, LOCK);
754 if (sm) {
755 name = sm->name;
756 if (sm->sym)
757 sym = sm->sym;
758 else
759 delete_null = true;
762 warn_on_double(sm, &locked);
763 if (delete_null)
764 set_state(my_id, name, NULL, &ignore);
766 set_state(my_id, name, sym, &locked);
769 static void do_unlock(const char *name, struct symbol *sym, struct lock_info *info)
771 struct sm_state *sm;
772 bool delete_null = false;
774 if (__path_is_null())
775 return;
777 if (!info && handle_macro_lock_unlock())
778 return;
780 add_tracker(&locks, my_id, name, sym);
781 sm = get_sm_state(my_id, name, sym);
782 if (!sm && !info && !is_local_IRQ_save(name, sym, info)) {
783 sm = get_best_match(name, UNLOCK);
784 if (sm) {
785 name = sm->name;
786 if (sm->sym)
787 sym = sm->sym;
788 else
789 delete_null = true;
792 if (!get_start_state(sm))
793 set_start_state(name, sym, &locked);
794 warn_on_double(sm, &unlocked);
795 if (delete_null)
796 set_state(my_id, name, NULL, &ignore);
797 set_state(my_id, name, sym, &unlocked);
800 static void do_restore(const char *name, struct symbol *sym, struct lock_info *info)
802 struct sm_state *sm;
804 if (__path_is_null())
805 return;
807 sm = get_sm_state(my_id, name, sym);
808 if (!get_start_state(sm))
809 set_start_state(name, sym, &locked);
811 add_tracker(&locks, my_id, name, sym);
812 set_state(my_id, name, sym, &restore);
815 static int get_db_type(struct sm_state *sm)
818 * Bottom half is complicated because it's nestable.
819 * Say it's merged at the start and we lock and unlock then
820 * it should go back to merged.
822 if (sm->state == get_start_state(sm)) {
823 if (sm->state == &locked)
824 return KNOWN_LOCKED;
825 if (sm->state == &unlocked)
826 return KNOWN_UNLOCKED;
829 if (sm->state == &locked)
830 return LOCK;
831 if (sm->state == &unlocked)
832 return UNLOCK;
833 if (sm->state == &restore)
834 return RESTORE;
835 return LOCK;
838 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
840 struct smatch_state *start;
841 struct sm_state *sm;
842 const char *param_name;
843 int param;
845 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
846 if (sm->state != &locked &&
847 sm->state != &unlocked &&
848 sm->state != &restore)
849 continue;
850 if (sm->name[0] == '$')
851 continue;
854 * If the state is locked at the end, that doesn't mean
855 * anything. It could be been locked at the start. Or
856 * it could be &merged at the start but its locked now
857 * because of implications and not because we set the
858 * state.
860 * This is slightly a hack, but when we change the state, we
861 * call set_start_state() so if get_start_state() returns NULL
862 * that means we haven't manually the locked state.
864 start = get_start_state(sm);
865 if (sm->state == &restore) {
866 if (start != &locked)
867 continue;
868 } else if (!start || sm->state == start)
869 continue; /* !start means it was passed in */
871 param = get_param_key_from_sm(sm, expr, &param_name);
872 sql_insert_return_states(return_id, return_ranges,
873 get_db_type(sm),
874 param, param_name, "");
875 } END_FOR_EACH_SM(sm);
878 static int sm_both_locked_and_unlocked(struct sm_state *sm)
880 int is_locked = 0;
881 int is_unlocked = 0;
882 struct sm_state *tmp;
884 if (sm->state != &merged)
885 return 0;
887 FOR_EACH_PTR(sm->possible, tmp) {
888 if (tmp->state == &locked)
889 is_locked = 1;
890 if (tmp->state == &unlocked)
891 is_unlocked = 1;
892 } END_FOR_EACH_PTR(tmp);
894 return is_locked && is_unlocked;
897 enum {
898 ERR_PTR, VALID_PTR, NEGATIVE, ZERO, POSITIVE, NUM_BUCKETS,
901 static bool is_EINTR(struct range_list *rl)
903 sval_t sval;
905 if (!rl_to_sval(rl, &sval))
906 return false;
907 return sval.value == -4;
910 static int success_fail_positive(struct range_list *rl)
912 /* void returns are the same as success (zero in the kernel) */
913 if (!rl)
914 return ZERO;
916 if (rl_type(rl)->type != SYM_PTR &&
917 !is_whole_rl(rl) &&
918 sval_is_negative(rl_min(rl)))
919 return NEGATIVE;
921 if (rl_min(rl).value == 0 && rl_max(rl).value == 0)
922 return ZERO;
924 if (is_err_ptr(rl_min(rl)) &&
925 is_err_ptr(rl_max(rl)))
926 return ERR_PTR;
929 * Trying to match ERR_PTR(ret) but without the expression struct.
930 * Ugly...
932 if (type_bits(&long_ctype) == 64 &&
933 rl_type(rl)->type == SYM_PTR &&
934 rl_min(rl).value == INT_MIN)
935 return ERR_PTR;
937 return POSITIVE;
940 static bool sym_in_lock_table(struct symbol *sym)
942 int i;
944 if (!sym || !sym->ident)
945 return false;
947 for (i = 0; lock_table[i].function != NULL; i++) {
948 if (strcmp(lock_table[i].function, sym->ident->name) == 0)
949 return true;
951 return false;
954 static bool func_in_lock_table(struct expression *expr)
956 if (expr->type != EXPR_SYMBOL)
957 return false;
958 return sym_in_lock_table(expr->symbol);
961 static void check_lock(char *name, struct symbol *sym)
963 struct range_list *locked_lines = NULL;
964 struct range_list *unlocked_lines = NULL;
965 int locked_buckets[NUM_BUCKETS] = {};
966 int unlocked_buckets[NUM_BUCKETS] = {};
967 struct stree *stree, *orig;
968 struct sm_state *return_sm;
969 struct sm_state *sm;
970 sval_t line = sval_type_val(&int_ctype, 0);
971 int bucket;
972 int i;
974 if (sym_in_lock_table(cur_func_sym))
975 return;
977 FOR_EACH_PTR(get_all_return_strees(), stree) {
978 orig = __swap_cur_stree(stree);
980 if (is_impossible_path())
981 goto swap_stree;
983 return_sm = get_sm_state(RETURN_ID, "return_ranges", NULL);
984 if (!return_sm)
985 goto swap_stree;
986 line.value = return_sm->line;
988 sm = get_sm_state(my_id, name, sym);
989 if (!sm)
990 goto swap_stree;
992 if (parent_is_gone_var_sym(sm->name, sm->sym))
993 goto swap_stree;
995 if (sm->state != &locked &&
996 sm->state != &unlocked &&
997 sm->state != &restore)
998 goto swap_stree;
1000 if ((sm->state == &unlocked || sm->state == &restore) &&
1001 is_EINTR(estate_rl(return_sm->state)))
1002 goto swap_stree;
1004 bucket = success_fail_positive(estate_rl(return_sm->state));
1005 if (sm->state == &locked) {
1006 add_range(&locked_lines, line, line);
1007 locked_buckets[bucket] = true;
1009 if (sm->state == &unlocked || sm->state == &restore) {
1010 add_range(&unlocked_lines, line, line);
1011 unlocked_buckets[bucket] = true;
1013 swap_stree:
1014 __swap_cur_stree(orig);
1015 } END_FOR_EACH_PTR(stree);
1018 if (!locked_lines || !unlocked_lines)
1019 return;
1021 for (i = 0; i < NUM_BUCKETS; i++) {
1022 if (locked_buckets[i] && unlocked_buckets[i])
1023 goto complain;
1025 if (locked_buckets[NEGATIVE] &&
1026 (unlocked_buckets[ZERO] || unlocked_buckets[POSITIVE]))
1027 goto complain;
1029 if (locked_buckets[ERR_PTR])
1030 goto complain;
1032 return;
1034 complain:
1035 if (common_false_positive(name))
1036 return;
1038 sm_msg("warn: inconsistent returns '%s'.", name);
1039 sm_printf(" Locked on : %s\n", show_rl(locked_lines));
1040 sm_printf(" Unlocked on: %s\n", show_rl(unlocked_lines));
1043 static void match_func_end(struct symbol *sym)
1045 struct tracker *tracker;
1047 FOR_EACH_PTR(locks, tracker) {
1048 check_lock(tracker->name, tracker->sym);
1049 } END_FOR_EACH_PTR(tracker);
1052 static void db_param_locked_unlocked(struct expression *expr, int param, const char *key, int lock_unlock, struct lock_info *info)
1054 struct expression *call, *arg;
1055 char *name;
1056 struct symbol *sym;
1058 if (info && info->action == IGNORE_LOCK)
1059 return;
1061 call = expr;
1062 while (call->type == EXPR_ASSIGNMENT)
1063 call = strip_expr(call->right);
1064 if (call->type != EXPR_CALL)
1065 return;
1067 if (!info && func_in_lock_table(call->fn))
1068 return;
1070 if (param == -2) {
1071 if (!info)
1072 name = use_best_match(key, lock_unlock, &sym);
1073 else {
1074 name = alloc_string(info->key);
1075 sym = NULL;
1077 } else if (param == -1) {
1078 if (expr->type != EXPR_ASSIGNMENT)
1079 return;
1080 ignored_reset = expr->left;
1082 name = get_variable_from_key(expr->left, key, &sym);
1083 if (!name || !sym)
1084 goto free;
1085 } else {
1086 arg = get_argument_from_call_expr(call->args, param);
1087 if (!arg)
1088 return;
1090 arg = remove_spinlock_check(arg);
1091 name = get_variable_from_key(arg, key, &sym);
1092 if (!name || !sym)
1093 goto free;
1096 if (!name || !sym)
1097 goto free;
1099 if (lock_unlock == LOCK)
1100 do_lock(name, sym, info);
1101 else if (lock_unlock == UNLOCK)
1102 do_unlock(name, sym, info);
1103 else if (lock_unlock == RESTORE)
1104 do_restore(name, sym, info);
1106 free:
1107 free_string(name);
1110 static void db_param_locked(struct expression *expr, int param, char *key, char *value)
1112 db_param_locked_unlocked(expr, param, key, LOCK, NULL);
1115 static void db_param_unlocked(struct expression *expr, int param, char *key, char *value)
1117 db_param_locked_unlocked(expr, param, key, UNLOCK, NULL);
1120 static void db_param_restore(struct expression *expr, int param, char *key, char *value)
1122 db_param_locked_unlocked(expr, param, key, RESTORE, NULL);
1125 static void match_lock_unlock(const char *fn, struct expression *expr, void *data)
1127 struct lock_info *info = data;
1128 struct expression *parent;
1130 if (info->arg == -1) {
1131 parent = expr_get_parent_expr(expr);
1132 while (parent && parent->type != EXPR_ASSIGNMENT)
1133 parent = expr_get_parent_expr(parent);
1134 if (!parent || parent->type != EXPR_ASSIGNMENT)
1135 return;
1136 expr = parent;
1139 db_param_locked_unlocked(expr, info->arg, info->key, info->action, info);
1142 static void match_lock_held(const char *fn, struct expression *call_expr,
1143 struct expression *assign_expr, void *data)
1145 struct lock_info *info = data;
1147 db_param_locked_unlocked(assign_expr ?: call_expr, info->arg, info->key, info->action, info);
1150 static void match_assign(struct expression *expr)
1152 struct smatch_state *state;
1154 /* This is only for the DB */
1155 if (is_fake_var_assign(expr))
1156 return;
1157 state = get_state_expr(my_id, expr->right);
1158 if (!state)
1159 return;
1160 set_state_expr(my_id, expr->left, state);
1163 static struct stree *printed;
1164 static void call_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1166 int locked_type = 0;
1168 if (sm->state == &locked)
1169 locked_type = LOCK;
1170 else if (sm->state == &unlocked)
1171 locked_type = UNLOCK;
1172 else if (slist_has_state(sm->possible, &locked) ||
1173 slist_has_state(sm->possible, &half_locked))
1174 locked_type = HALF_LOCKED;
1175 else
1176 return;
1178 avl_insert(&printed, sm);
1179 sql_insert_caller_info(call, locked_type, param, printed_name, "");
1182 static void match_call_info(struct expression *expr)
1184 struct sm_state *sm;
1185 const char *name;
1186 int locked_type;
1188 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1189 if (sm->state == &locked)
1190 locked_type = LOCK;
1191 else if (sm->state == &half_locked ||
1192 slist_has_state(sm->possible, &locked))
1193 locked_type = HALF_LOCKED;
1194 else
1195 continue;
1197 if (avl_lookup(printed, sm))
1198 continue;
1200 if (strcmp(sm->name, "bottom_half") == 0)
1201 name = "bh";
1202 else if (strcmp(sm->name, "rcu_read") == 0)
1203 name = "rcu_read_lock";
1204 else
1205 name = sm->name;
1207 if (strncmp(name, "__fake_param", 12) == 0 ||
1208 strchr(name, '$'))
1209 continue;
1211 sql_insert_caller_info(expr, locked_type, -2, name, "");
1212 } END_FOR_EACH_SM(sm);
1213 free_stree(&printed);
1216 static void set_locked(const char *name, struct symbol *sym, char *value)
1218 set_state(my_id, name, sym, &locked);
1221 static void set_half_locked(const char *name, struct symbol *sym, char *value)
1223 set_state(my_id, name, sym, &half_locked);
1226 static void set_unlocked(const char *name, struct symbol *sym, char *value)
1228 set_state(my_id, name, sym, &unlocked);
1231 static void match_after_func(struct symbol *sym)
1233 free_stree(&start_states);
1236 static void match_dma_resv_lock_NULL(const char *fn, struct expression *call_expr,
1237 struct expression *assign_expr, void *_index)
1239 struct expression *lock, *ctx;
1240 char *lock_name;
1241 struct symbol *sym;
1243 lock = get_argument_from_call_expr(call_expr->args, 0);
1244 ctx = get_argument_from_call_expr(call_expr->args, 1);
1245 if (!expr_is_zero(ctx))
1246 return;
1248 lock_name = lock_to_name_sym(lock, &sym);
1249 if (!lock_name || !sym)
1250 goto free;
1251 do_lock(lock_name, sym, NULL);
1252 free:
1253 free_string(lock_name);
1256 /* print_held_locks() is used in check_call_tree.c */
1257 void print_held_locks(void)
1259 struct stree *stree;
1260 struct sm_state *sm;
1261 int i = 0;
1263 stree = __get_cur_stree();
1264 FOR_EACH_MY_SM(my_id, stree, sm) {
1265 if (sm->state != &locked)
1266 continue;
1267 if (i++)
1268 sm_printf(" ");
1269 sm_printf("'%s'", sm->name);
1270 } END_FOR_EACH_SM(sm);
1273 static void load_table(struct lock_info *lock_table)
1275 int i;
1277 for (i = 0; lock_table[i].function != NULL; i++) {
1278 struct lock_info *lock = &lock_table[i];
1280 if (lock->call_back) {
1281 add_function_hook(lock->function, lock->call_back, lock);
1282 } else if (lock->implies_start) {
1283 return_implies_state_sval(lock->function,
1284 *lock->implies_start,
1285 *lock->implies_end,
1286 &match_lock_held, lock);
1287 } else {
1288 add_function_hook(lock->function, &match_lock_unlock, lock);
1293 static bool is_smp_config(void)
1295 struct ident *id;
1297 id = built_in_ident("CONFIG_SMP");
1298 return !!lookup_symbol(id, NS_MACRO);
1301 void check_locking(int id)
1303 my_id = id;
1305 if (option_project != PROJ_KERNEL)
1306 return;
1308 if (!is_smp_config())
1309 return;
1311 load_table(lock_table);
1313 set_dynamic_states(my_id);
1314 add_unmatched_state_hook(my_id, &unmatched_state);
1315 add_pre_merge_hook(my_id, &pre_merge_hook);
1316 add_merge_hook(my_id, &merge_func);
1317 add_modification_hook(my_id, &reset);
1319 add_hook(&match_assign, ASSIGNMENT_HOOK);
1320 add_hook(&match_func_end, END_FUNC_HOOK);
1322 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1323 add_function_data((unsigned long *)&start_states);
1325 add_caller_info_callback(my_id, call_info_callback);
1326 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1328 add_split_return_callback(match_return_info);
1329 select_return_states_hook(LOCK, &db_param_locked);
1330 select_return_states_hook(UNLOCK, &db_param_unlocked);
1331 select_return_states_hook(RESTORE, &db_param_restore);
1333 select_caller_name_sym(set_locked, LOCK);
1334 select_caller_name_sym(set_half_locked, HALF_LOCKED);
1335 select_caller_name_sym(set_unlocked, UNLOCK);
1337 return_implies_state("dma_resv_lock", -4095, -1, &match_dma_resv_lock_NULL, 0);