comparison: select the caller_info
[smatch.git] / check_locking.c
blob05cd4b553f72ddc4652bfd4efa1ed6a2842f5508
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 (common_false_positive(sm->name))
661 return;
663 if (state == &locked && sm_in_start_states(tmp)) {
664 // sm_warning("called with lock held. '%s'", sm->name);
665 } else {
666 // sm_msg("error: double %s '%s' (orig line %u)",
667 // state->name, sm->name, tmp->line);
671 static bool handle_macro_lock_unlock(void)
673 struct expression *expr, *arg;
674 struct macro_info *info;
675 struct sm_state *sm;
676 struct symbol *sym;
677 const char *macro;
678 char *name;
679 bool ret = false;
680 int i;
682 expr = last_ptr_list((struct ptr_list *)big_expression_stack);
683 while (expr && expr->type == EXPR_ASSIGNMENT)
684 expr = strip_expr(expr->right);
685 if (!expr || expr->type != EXPR_CALL)
686 return false;
688 macro = get_macro_name(expr->pos);
689 if (!macro)
690 return false;
692 for (i = 0; i < ARRAY_SIZE(macro_table); i++) {
693 info = &macro_table[i];
695 if (strcmp(macro, info->macro) != 0)
696 continue;
697 arg = get_argument_from_call_expr(expr->args, info->param);
698 name = expr_to_str_sym(arg, &sym);
699 if (!name || !sym)
700 goto free;
701 sm = get_sm_state(my_id, name, sym);
703 if (info->action == LOCK) {
704 if (!get_start_state(sm))
705 set_start_state(name, sym, &unlocked);
706 if (sm && sm->line != expr->pos.line)
707 warn_on_double(sm, &locked);
708 set_state(my_id, name, sym, &locked);
709 } else {
710 if (!get_start_state(sm))
711 set_start_state(name, sym, &locked);
712 if (sm && sm->line != expr->pos.line)
713 warn_on_double(sm, &unlocked);
714 set_state(my_id, name, sym, &unlocked);
716 ret = true;
717 free:
718 free_string(name);
719 return ret;
721 return false;
724 static bool is_local_IRQ_save(const char *name, struct symbol *sym, struct lock_info *info)
726 if (name && strcmp(name, "flags") == 0)
727 return true;
728 if (!sym)
729 return false;
730 if (!sym->ident || strcmp(sym->ident->name, name) != 0)
731 return false;
732 if (!info)
733 return false;
734 return strstr(info->function, "irq") && strstr(info->function, "save");
737 static void do_lock(const char *name, struct symbol *sym, struct lock_info *info)
739 struct sm_state *sm;
740 bool delete_null = false;
742 if (!info && handle_macro_lock_unlock())
743 return;
745 add_tracker(&locks, my_id, name, sym);
747 sm = get_sm_state(my_id, name, sym);
748 if (!get_start_state(sm))
749 set_start_state(name, sym, &unlocked);
750 if (!sm && !is_local_IRQ_save(name, sym, info) && !sym) {
751 sm = get_best_match(name, LOCK);
752 if (sm) {
753 name = sm->name;
754 if (sm->sym)
755 sym = sm->sym;
756 else
757 delete_null = true;
760 warn_on_double(sm, &locked);
761 if (delete_null)
762 set_state(my_id, name, NULL, &ignore);
764 set_state(my_id, name, sym, &locked);
767 static void do_unlock(const char *name, struct symbol *sym, struct lock_info *info)
769 struct sm_state *sm;
770 bool delete_null = false;
772 if (__path_is_null())
773 return;
775 if (!info && handle_macro_lock_unlock())
776 return;
778 add_tracker(&locks, my_id, name, sym);
779 sm = get_sm_state(my_id, name, sym);
780 if (!sm && !info && !is_local_IRQ_save(name, sym, info)) {
781 sm = get_best_match(name, UNLOCK);
782 if (sm) {
783 name = sm->name;
784 if (sm->sym)
785 sym = sm->sym;
786 else
787 delete_null = true;
790 if (!get_start_state(sm))
791 set_start_state(name, sym, &locked);
792 warn_on_double(sm, &unlocked);
793 if (delete_null)
794 set_state(my_id, name, NULL, &ignore);
795 set_state(my_id, name, sym, &unlocked);
798 static void do_restore(const char *name, struct symbol *sym, struct lock_info *info)
800 struct sm_state *sm;
802 if (__path_is_null())
803 return;
805 sm = get_sm_state(my_id, name, sym);
806 if (!get_start_state(sm))
807 set_start_state(name, sym, &locked);
809 add_tracker(&locks, my_id, name, sym);
810 set_state(my_id, name, sym, &restore);
813 static int get_db_type(struct sm_state *sm)
816 * Bottom half is complicated because it's nestable.
817 * Say it's merged at the start and we lock and unlock then
818 * it should go back to merged.
820 if (sm->state == get_start_state(sm)) {
821 if (sm->state == &locked)
822 return KNOWN_LOCKED;
823 if (sm->state == &unlocked)
824 return KNOWN_UNLOCKED;
827 if (sm->state == &locked)
828 return LOCK;
829 if (sm->state == &unlocked)
830 return UNLOCK;
831 if (sm->state == &restore)
832 return RESTORE;
833 return LOCK;
836 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
838 struct smatch_state *start;
839 struct sm_state *sm;
840 const char *param_name;
841 int param;
843 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
844 if (sm->state != &locked &&
845 sm->state != &unlocked &&
846 sm->state != &restore)
847 continue;
848 if (sm->name[0] == '$')
849 continue;
852 * If the state is locked at the end, that doesn't mean
853 * anything. It could be been locked at the start. Or
854 * it could be &merged at the start but its locked now
855 * because of implications and not because we set the
856 * state.
858 * This is slightly a hack, but when we change the state, we
859 * call set_start_state() so if get_start_state() returns NULL
860 * that means we haven't manually the locked state.
862 start = get_start_state(sm);
863 if (sm->state == &restore) {
864 if (start != &locked)
865 continue;
866 } else if (!start || sm->state == start)
867 continue; /* !start means it was passed in */
869 param = get_param_key_from_sm(sm, expr, &param_name);
870 sql_insert_return_states(return_id, return_ranges,
871 get_db_type(sm),
872 param, param_name, "");
873 } END_FOR_EACH_SM(sm);
876 static int sm_both_locked_and_unlocked(struct sm_state *sm)
878 int is_locked = 0;
879 int is_unlocked = 0;
880 struct sm_state *tmp;
882 if (sm->state != &merged)
883 return 0;
885 FOR_EACH_PTR(sm->possible, tmp) {
886 if (tmp->state == &locked)
887 is_locked = 1;
888 if (tmp->state == &unlocked)
889 is_unlocked = 1;
890 } END_FOR_EACH_PTR(tmp);
892 return is_locked && is_unlocked;
895 enum {
896 ERR_PTR, VALID_PTR, NEGATIVE, ZERO, POSITIVE, NUM_BUCKETS,
899 static bool is_EINTR(struct range_list *rl)
901 sval_t sval;
903 if (!rl_to_sval(rl, &sval))
904 return false;
905 return sval.value == -4;
908 static int success_fail_positive(struct range_list *rl)
910 /* void returns are the same as success (zero in the kernel) */
911 if (!rl)
912 return ZERO;
914 if (rl_type(rl)->type != SYM_PTR &&
915 !is_whole_rl(rl) &&
916 sval_is_negative(rl_min(rl)))
917 return NEGATIVE;
919 if (rl_min(rl).value == 0 && rl_max(rl).value == 0)
920 return ZERO;
922 if (is_err_ptr(rl_min(rl)) &&
923 is_err_ptr(rl_max(rl)))
924 return ERR_PTR;
927 * Trying to match ERR_PTR(ret) but without the expression struct.
928 * Ugly...
930 if (type_bits(&long_ctype) == 64 &&
931 rl_type(rl)->type == SYM_PTR &&
932 rl_min(rl).value == INT_MIN)
933 return ERR_PTR;
935 return POSITIVE;
938 static bool sym_in_lock_table(struct symbol *sym)
940 int i;
942 if (!sym || !sym->ident)
943 return false;
945 for (i = 0; lock_table[i].function != NULL; i++) {
946 if (strcmp(lock_table[i].function, sym->ident->name) == 0)
947 return true;
949 return false;
952 static bool func_in_lock_table(struct expression *expr)
954 if (expr->type != EXPR_SYMBOL)
955 return false;
956 return sym_in_lock_table(expr->symbol);
959 static void check_lock(char *name, struct symbol *sym)
961 struct range_list *locked_lines = NULL;
962 struct range_list *unlocked_lines = NULL;
963 int locked_buckets[NUM_BUCKETS] = {};
964 int unlocked_buckets[NUM_BUCKETS] = {};
965 struct stree *stree, *orig;
966 struct sm_state *return_sm;
967 struct sm_state *sm;
968 sval_t line = sval_type_val(&int_ctype, 0);
969 int bucket;
970 int i;
972 if (sym_in_lock_table(cur_func_sym))
973 return;
975 FOR_EACH_PTR(get_all_return_strees(), stree) {
976 orig = __swap_cur_stree(stree);
978 if (is_impossible_path())
979 goto swap_stree;
981 return_sm = get_sm_state(RETURN_ID, "return_ranges", NULL);
982 if (!return_sm)
983 goto swap_stree;
984 line.value = return_sm->line;
986 sm = get_sm_state(my_id, name, sym);
987 if (!sm)
988 goto swap_stree;
990 if (parent_is_gone_var_sym(sm->name, sm->sym))
991 goto swap_stree;
993 if (sm->state != &locked &&
994 sm->state != &unlocked &&
995 sm->state != &restore)
996 goto swap_stree;
998 if ((sm->state == &unlocked || sm->state == &restore) &&
999 is_EINTR(estate_rl(return_sm->state)))
1000 goto swap_stree;
1002 bucket = success_fail_positive(estate_rl(return_sm->state));
1003 if (sm->state == &locked) {
1004 add_range(&locked_lines, line, line);
1005 locked_buckets[bucket] = true;
1007 if (sm->state == &unlocked || sm->state == &restore) {
1008 add_range(&unlocked_lines, line, line);
1009 unlocked_buckets[bucket] = true;
1011 swap_stree:
1012 __swap_cur_stree(orig);
1013 } END_FOR_EACH_PTR(stree);
1016 if (!locked_lines || !unlocked_lines)
1017 return;
1019 for (i = 0; i < NUM_BUCKETS; i++) {
1020 if (locked_buckets[i] && unlocked_buckets[i])
1021 goto complain;
1023 if (locked_buckets[NEGATIVE] &&
1024 (unlocked_buckets[ZERO] || unlocked_buckets[POSITIVE]))
1025 goto complain;
1027 if (locked_buckets[ERR_PTR])
1028 goto complain;
1030 return;
1032 complain:
1033 if (common_false_positive(name))
1034 return;
1036 sm_msg("warn: inconsistent returns '%s'.", name);
1037 sm_printf(" Locked on : %s\n", show_rl(locked_lines));
1038 sm_printf(" Unlocked on: %s\n", show_rl(unlocked_lines));
1041 static void match_func_end(struct symbol *sym)
1043 struct tracker *tracker;
1045 FOR_EACH_PTR(locks, tracker) {
1046 check_lock(tracker->name, tracker->sym);
1047 } END_FOR_EACH_PTR(tracker);
1050 static void db_param_locked_unlocked(struct expression *expr, int param, const char *key, int lock_unlock, struct lock_info *info)
1052 struct expression *call, *arg;
1053 char *name;
1054 struct symbol *sym;
1056 if (info && info->action == IGNORE_LOCK)
1057 return;
1059 call = expr;
1060 while (call->type == EXPR_ASSIGNMENT)
1061 call = strip_expr(call->right);
1062 if (call->type != EXPR_CALL)
1063 return;
1065 if (!info && func_in_lock_table(call->fn))
1066 return;
1068 if (param == -2) {
1069 if (!info)
1070 name = use_best_match(key, lock_unlock, &sym);
1071 else {
1072 name = alloc_string(info->key);
1073 sym = NULL;
1075 } else if (param == -1) {
1076 if (expr->type != EXPR_ASSIGNMENT)
1077 return;
1078 ignored_reset = expr->left;
1080 name = get_variable_from_key(expr->left, key, &sym);
1081 if (!name || !sym)
1082 goto free;
1083 } else {
1084 arg = get_argument_from_call_expr(call->args, param);
1085 if (!arg)
1086 return;
1088 arg = remove_spinlock_check(arg);
1089 name = get_variable_from_key(arg, key, &sym);
1090 if (!name || !sym)
1091 goto free;
1094 if (!name || !sym)
1095 goto free;
1097 if (lock_unlock == LOCK)
1098 do_lock(name, sym, info);
1099 else if (lock_unlock == UNLOCK)
1100 do_unlock(name, sym, info);
1101 else if (lock_unlock == RESTORE)
1102 do_restore(name, sym, info);
1104 free:
1105 free_string(name);
1108 static void db_param_locked(struct expression *expr, int param, char *key, char *value)
1110 db_param_locked_unlocked(expr, param, key, LOCK, NULL);
1113 static void db_param_unlocked(struct expression *expr, int param, char *key, char *value)
1115 db_param_locked_unlocked(expr, param, key, UNLOCK, NULL);
1118 static void db_param_restore(struct expression *expr, int param, char *key, char *value)
1120 db_param_locked_unlocked(expr, param, key, RESTORE, NULL);
1123 static void match_lock_unlock(const char *fn, struct expression *expr, void *data)
1125 struct lock_info *info = data;
1126 struct expression *parent;
1128 if (info->arg == -1) {
1129 parent = expr_get_parent_expr(expr);
1130 while (parent && parent->type != EXPR_ASSIGNMENT)
1131 parent = expr_get_parent_expr(parent);
1132 if (!parent || parent->type != EXPR_ASSIGNMENT)
1133 return;
1134 expr = parent;
1137 db_param_locked_unlocked(expr, info->arg, info->key, info->action, info);
1140 static void match_lock_held(const char *fn, struct expression *call_expr,
1141 struct expression *assign_expr, void *data)
1143 struct lock_info *info = data;
1145 db_param_locked_unlocked(assign_expr ?: call_expr, info->arg, info->key, info->action, info);
1148 static void match_assign(struct expression *expr)
1150 struct smatch_state *state;
1152 /* This is only for the DB */
1153 if (!__in_fake_var_assign)
1154 return;
1155 state = get_state_expr(my_id, expr->right);
1156 if (!state)
1157 return;
1158 set_state_expr(my_id, expr->left, state);
1161 static struct stree *printed;
1162 static void call_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1164 int locked_type = 0;
1166 if (sm->state == &locked)
1167 locked_type = LOCK;
1168 else if (sm->state == &unlocked)
1169 locked_type = UNLOCK;
1170 else if (slist_has_state(sm->possible, &locked) ||
1171 slist_has_state(sm->possible, &half_locked))
1172 locked_type = HALF_LOCKED;
1173 else
1174 return;
1176 avl_insert(&printed, sm);
1177 sql_insert_caller_info(call, locked_type, param, printed_name, "");
1180 static void match_call_info(struct expression *expr)
1182 struct sm_state *sm;
1183 const char *name;
1184 int locked_type;
1186 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1187 if (sm->state == &locked)
1188 locked_type = LOCK;
1189 else if (sm->state == &half_locked ||
1190 slist_has_state(sm->possible, &locked))
1191 locked_type = HALF_LOCKED;
1192 else
1193 continue;
1195 if (avl_lookup(printed, sm))
1196 continue;
1198 if (strcmp(sm->name, "bottom_half") == 0)
1199 name = "bh";
1200 else if (strcmp(sm->name, "rcu_read") == 0)
1201 name = "rcu_read_lock";
1202 else
1203 name = sm->name;
1205 if (strncmp(name, "__fake_param", 12) == 0 ||
1206 strchr(name, '$'))
1207 continue;
1209 sql_insert_caller_info(expr, locked_type, -2, name, "");
1210 } END_FOR_EACH_SM(sm);
1211 free_stree(&printed);
1214 static void set_locked(const char *name, struct symbol *sym, char *value)
1216 set_state(my_id, name, sym, &locked);
1219 static void set_half_locked(const char *name, struct symbol *sym, char *value)
1221 set_state(my_id, name, sym, &half_locked);
1224 static void set_unlocked(const char *name, struct symbol *sym, char *value)
1226 set_state(my_id, name, sym, &unlocked);
1229 static void match_after_func(struct symbol *sym)
1231 free_stree(&start_states);
1234 static void match_dma_resv_lock_NULL(const char *fn, struct expression *call_expr,
1235 struct expression *assign_expr, void *_index)
1237 struct expression *lock, *ctx;
1238 char *lock_name;
1239 struct symbol *sym;
1241 lock = get_argument_from_call_expr(call_expr->args, 0);
1242 ctx = get_argument_from_call_expr(call_expr->args, 1);
1243 if (!expr_is_zero(ctx))
1244 return;
1246 lock_name = lock_to_name_sym(lock, &sym);
1247 if (!lock_name || !sym)
1248 goto free;
1249 do_lock(lock_name, sym, NULL);
1250 free:
1251 free_string(lock_name);
1254 /* print_held_locks() is used in check_call_tree.c */
1255 void print_held_locks(void)
1257 struct stree *stree;
1258 struct sm_state *sm;
1259 int i = 0;
1261 stree = __get_cur_stree();
1262 FOR_EACH_MY_SM(my_id, stree, sm) {
1263 if (sm->state != &locked)
1264 continue;
1265 if (i++)
1266 sm_printf(" ");
1267 sm_printf("'%s'", sm->name);
1268 } END_FOR_EACH_SM(sm);
1271 static void load_table(struct lock_info *lock_table)
1273 int i;
1275 for (i = 0; lock_table[i].function != NULL; i++) {
1276 struct lock_info *lock = &lock_table[i];
1278 if (lock->call_back) {
1279 add_function_hook(lock->function, lock->call_back, lock);
1280 } else if (lock->implies_start) {
1281 return_implies_state_sval(lock->function,
1282 *lock->implies_start,
1283 *lock->implies_end,
1284 &match_lock_held, lock);
1285 } else {
1286 add_function_hook(lock->function, &match_lock_unlock, lock);
1291 static bool is_smp_config(void)
1293 struct ident *id;
1295 id = built_in_ident("CONFIG_SMP");
1296 return !!lookup_symbol(id, NS_MACRO);
1299 void check_locking(int id)
1301 my_id = id;
1303 if (option_project != PROJ_KERNEL)
1304 return;
1306 if (!is_smp_config())
1307 return;
1309 load_table(lock_table);
1311 set_dynamic_states(my_id);
1312 add_unmatched_state_hook(my_id, &unmatched_state);
1313 add_pre_merge_hook(my_id, &pre_merge_hook);
1314 add_merge_hook(my_id, &merge_func);
1315 add_modification_hook(my_id, &reset);
1317 add_hook(&match_assign, ASSIGNMENT_HOOK);
1318 add_hook(&match_func_end, END_FUNC_HOOK);
1320 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1321 add_function_data((unsigned long *)&start_states);
1323 add_caller_info_callback(my_id, call_info_callback);
1324 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1326 add_split_return_callback(match_return_info);
1327 select_return_states_hook(LOCK, &db_param_locked);
1328 select_return_states_hook(UNLOCK, &db_param_unlocked);
1329 select_return_states_hook(RESTORE, &db_param_restore);
1331 select_caller_name_sym(set_locked, LOCK);
1332 select_caller_name_sym(set_half_locked, HALF_LOCKED);
1333 select_caller_name_sym(set_unlocked, UNLOCK);
1335 return_implies_state("dma_resv_lock", -4095, -1, &match_dma_resv_lock_NULL, 0);