check_kernel: fix the build
[smatch.git] / check_locking.c
blob576c813bff6a44a7ae1ef1b9a0e883c961e8a7ab
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 void match_class_mutex_destructor(const char *fn, struct expression *expr, void *data);
83 static struct lock_info lock_table[] = {
84 {"spin_lock", LOCK, spin_lock, 0, "$"},
85 {"spin_unlock", UNLOCK, spin_lock, 0, "$"},
86 {"spin_lock_nested", LOCK, spin_lock, 0, "$"},
87 {"_spin_lock", LOCK, spin_lock, 0, "$"},
88 {"_spin_unlock", UNLOCK, spin_lock, 0, "$"},
89 {"_spin_lock_nested", LOCK, spin_lock, 0, "$"},
90 {"__spin_lock", LOCK, spin_lock, 0, "$"},
91 {"__spin_unlock", UNLOCK, spin_lock, 0, "$"},
92 {"__spin_lock_nested", LOCK, spin_lock, 0, "$"},
93 {"raw_spin_lock", LOCK, spin_lock, 0, "$"},
94 {"raw_spin_unlock", UNLOCK, spin_lock, 0, "$"},
95 {"_raw_spin_lock", LOCK, spin_lock, 0, "$"},
96 {"_raw_spin_lock_nested", LOCK, spin_lock, 0, "$"},
97 {"_raw_spin_unlock", UNLOCK, spin_lock, 0, "$"},
98 {"__raw_spin_lock", LOCK, spin_lock, 0, "$"},
99 {"__raw_spin_unlock", 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 {"__spin_lock_irq", LOCK, spin_lock, 0, "$"},
106 {"__spin_unlock_irq", UNLOCK, spin_lock, 0, "$"},
107 {"_raw_spin_lock_irq", LOCK, spin_lock, 0, "$"},
108 {"_raw_spin_unlock_irq", UNLOCK, spin_lock, 0, "$"},
109 {"__raw_spin_unlock_irq", 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 {"__spin_lock_irqsave", LOCK, spin_lock, 0, "$"},
115 {"__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 {"__raw_spin_lock_irqsave", LOCK, spin_lock, 0, "$"},
119 {"__raw_spin_unlock_irqrestore", UNLOCK, spin_lock, 0, "$"},
120 {"spin_lock_irqsave_nested", LOCK, spin_lock, 0, "$"},
121 {"_spin_lock_irqsave_nested", LOCK, spin_lock, 0, "$"},
122 {"__spin_lock_irqsave_nested", LOCK, spin_lock, 0, "$"},
123 {"_raw_spin_lock_irqsave_nested", LOCK, 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, "$"},
128 {"__spin_lock_bh", LOCK, spin_lock, 0, "$"},
129 {"__spin_unlock_bh", UNLOCK, spin_lock, 0, "$"},
131 {"spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
132 {"_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
133 {"__spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
134 {"raw_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
135 {"_raw_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
136 {"spin_trylock_irq", LOCK, spin_lock, 0, "$", &int_one, &int_one},
137 {"spin_trylock_irqsave", LOCK, spin_lock, 0, "$", &int_one, &int_one},
138 {"spin_trylock_bh", LOCK, spin_lock, 0, "$", &int_one, &int_one},
139 {"_spin_trylock_bh", LOCK, spin_lock, 0, "$", &int_one, &int_one},
140 {"__spin_trylock_bh", LOCK, spin_lock, 0, "$", &int_one, &int_one},
141 {"__raw_spin_trylock", LOCK, spin_lock, 0, "$", &int_one, &int_one},
142 {"_atomic_dec_and_lock", LOCK, spin_lock, 1, "$", &int_one, &int_one},
144 {"read_lock", LOCK, read_lock, 0, "$"},
145 {"down_read", LOCK, read_lock, 0, "$"},
146 {"down_read_nested", LOCK, read_lock, 0, "$"},
147 {"down_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
148 {"down_read_killable", LOCK, read_lock, 0, "$", &int_zero, &int_zero},
149 {"up_read", UNLOCK, 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 {"__read_lock", LOCK, read_lock, 0, "$"},
154 {"__read_unlock", UNLOCK, read_lock, 0, "$"},
155 {"_raw_read_lock", LOCK, read_lock, 0, "$"},
156 {"_raw_read_unlock", UNLOCK, read_lock, 0, "$"},
157 {"__raw_read_lock", LOCK, read_lock, 0, "$"},
158 {"__raw_read_unlock", 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 {"__read_lock_irq", LOCK, read_lock, 0, "$"},
164 {"__read_unlock_irq", UNLOCK, read_lock, 0, "$"},
165 {"_raw_read_unlock_irq", UNLOCK, read_lock, 0, "$"},
166 {"_raw_read_lock_irq", LOCK, read_lock, 0, "$"},
167 {"_raw_read_lock_bh", LOCK, read_lock, 0, "$"},
168 {"_raw_read_unlock_bh", 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_irqsave", LOCK, read_lock, 0, "$"},
174 {"__read_unlock_irqrestore", 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 {"__read_lock_bh", LOCK, read_lock, 0, "$"},
180 {"__read_unlock_bh", UNLOCK, read_lock, 0, "$"},
181 {"__raw_read_lock_bh", LOCK, read_lock, 0, "$"},
182 {"__raw_read_unlock_bh", UNLOCK, read_lock, 0, "$"},
184 {"_raw_read_lock_irqsave", LOCK, read_lock, 0, "$"},
185 {"_raw_read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
186 {"_raw_read_unlock_irqrestore", UNLOCK, read_lock, 0, "$"},
187 {"_raw_read_unlock_irqrestore", RESTORE, irq, 1, "$"},
188 {"_raw_spin_lock_bh", LOCK, read_lock, 0, "$"},
189 {"_raw_spin_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
190 {"_raw_spin_lock_nest_lock", LOCK, read_lock, 0, "$"},
191 {"_raw_spin_unlock_bh", UNLOCK, read_lock, 0, "$"},
192 {"_raw_spin_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
193 {"_raw_write_lock_irqsave", LOCK, write_lock, 0, "$"},
194 {"_raw_write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
195 {"_raw_write_unlock_irqrestore", UNLOCK, write_lock, 0, "$"},
196 {"_raw_write_unlock_irqrestore", RESTORE, irq, 1, "$"},
197 {"__raw_write_unlock_irq", UNLOCK, write_lock, 0, "$"},
198 {"__raw_write_unlock_irq", UNLOCK, irq, 0, "irq"},
199 {"__raw_write_unlock_irqrestore", UNLOCK, write_lock, 0, "$"},
200 {"__raw_write_unlock_irqrestore", RESTORE, irq, 1, "$"},
202 {"generic__raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
203 {"read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
204 {"_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
205 {"raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
206 {"_raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
207 {"__raw_read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
208 {"__read_trylock", LOCK, read_lock, 0, "$", &int_one, &int_one},
210 {"write_lock", LOCK, write_lock, 0, "$"},
211 {"down_write", LOCK, write_lock, 0, "$"},
212 {"down_write_nested", LOCK, write_lock, 0, "$"},
213 {"up_write", UNLOCK, 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", LOCK, write_lock, 0, "$"},
218 {"__write_unlock", 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 {"__write_lock_irq", LOCK, write_lock, 0, "$"},
224 {"__write_unlock_irq", UNLOCK, write_lock, 0, "$"},
225 {"_raw_write_unlock_irq", 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_irqsave", LOCK, write_lock, 0, "$"},
231 {"__write_unlock_irqrestore", 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 {"__write_lock_bh", LOCK, write_lock, 0, "$"},
237 {"__write_unlock_bh", UNLOCK, write_lock, 0, "$"},
238 {"_raw_write_lock", LOCK, write_lock, 0, "$"},
239 {"__raw_write_lock", LOCK, write_lock, 0, "$"},
240 {"_raw_write_unlock", UNLOCK, write_lock, 0, "$"},
241 {"__raw_write_unlock", UNLOCK, write_lock, 0, "$"},
242 {"_raw_write_lock_bh", LOCK, write_lock, 0, "$"},
243 {"_raw_write_unlock_bh", UNLOCK, write_lock, 0, "$"},
244 {"_raw_write_lock_irq", LOCK, write_lock, 0, "$"},
246 {"write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
247 {"_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
248 {"raw_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
249 {"_raw_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
250 {"__write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
251 {"__raw_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
252 {"down_write_trylock", LOCK, write_lock, 0, "$", &int_one, &int_one},
253 {"down_write_killable", LOCK, write_lock, 0, "$", &int_zero, &int_zero},
255 {"down", LOCK, sem, 0, "$"},
256 {"up", UNLOCK, sem, 0, "$"},
257 {"down_trylock", LOCK, sem, 0, "$", &int_zero, &int_zero},
258 {"down_timeout", LOCK, sem, 0, "$", &int_zero, &int_zero},
259 {"down_interruptible", LOCK, sem, 0, "$", &int_zero, &int_zero},
260 {"down_killable", LOCK, sem, 0, "$", &int_zero, &int_zero},
263 {"mutex_lock", LOCK, mutex, 0, "$"},
264 {"mutex_unlock", UNLOCK, mutex, 0, "$"},
265 {"mutex_destroy", RESTORE, mutex, 0, "$"},
266 {"mutex_lock_nested", LOCK, mutex, 0, "$"},
267 {"mutex_lock_io", LOCK, mutex, 0, "$"},
268 {"mutex_lock_io_nested", LOCK, mutex, 0, "$"},
270 {"mutex_lock_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
271 {"mutex_lock_interruptible_nested", LOCK, mutex, 0, "$", &int_zero, &int_zero},
272 {"mutex_lock_killable", LOCK, mutex, 0, "$", &int_zero, &int_zero},
273 {"mutex_lock_killable_nested", LOCK, mutex, 0, "$", &int_zero, &int_zero},
275 {"mutex_trylock", LOCK, mutex, 0, "$", &int_one, &int_one},
277 {"ww_mutex_lock", LOCK, mutex, 0, "$"},
278 {"__ww_mutex_lock", LOCK, mutex, 0, "$"},
279 {"ww_mutex_lock_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
280 {"ww_mutex_unlock", UNLOCK, mutex, 0, "$"},
282 {"raw_local_irq_disable", LOCK, irq, NO_ARG, "irq"},
283 {"raw_local_irq_enable", 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 {"__spin_lock_irq", LOCK, irq, NO_ARG, "irq"},
289 {"__spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
290 {"_raw_spin_lock_irq", LOCK, irq, NO_ARG, "irq"},
291 {"_raw_spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
292 {"__raw_spin_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
293 {"spin_trylock_irq", LOCK, irq, NO_ARG, "irq", &int_one, &int_one},
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 {"_read_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
298 {"__read_lock_irq", LOCK, irq, NO_ARG, "irq"},
299 {"_raw_read_lock_irq", LOCK, irq, NO_ARG, "irq"},
300 {"__read_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
301 {"_raw_read_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 {"__write_lock_irq", LOCK, irq, NO_ARG, "irq"},
307 {"__write_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
308 {"_raw_write_lock_irq", LOCK, irq, NO_ARG, "irq"},
309 {"_raw_write_unlock_irq", UNLOCK, irq, NO_ARG, "irq"},
311 {"arch_local_irq_save", LOCK, irq, RETURN_VAL, "$"},
312 {"arch_local_irq_restore", RESTORE, irq, 0, "$"},
313 {"__raw_local_irq_save", LOCK, irq, RETURN_VAL, "$"},
314 {"raw_local_irq_restore", RESTORE, irq, 0, "$"},
315 {"spin_lock_irqsave_nested", LOCK, irq, RETURN_VAL, "$"},
316 {"spin_lock_irqsave", LOCK, irq, 1, "$"},
317 {"spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
318 {"_spin_lock_irqsave_nested", LOCK, irq, RETURN_VAL, "$"},
319 {"_spin_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
320 {"_spin_lock_irqsave", LOCK, irq, 1, "$"},
321 {"_spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
322 {"__spin_lock_irqsave_nested", LOCK, irq, 1, "$"},
323 {"__spin_lock_irqsave", LOCK, irq, 1, "$"},
324 {"__spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
325 {"_raw_spin_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
326 {"_raw_spin_lock_irqsave", LOCK, irq, 1, "$"},
327 {"_raw_spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
328 {"__raw_spin_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
329 {"__raw_spin_unlock_irqrestore", RESTORE, irq, 1, "$"},
330 {"_raw_spin_lock_irqsave_nested", LOCK, irq, RETURN_VAL, "$"},
331 {"spin_trylock_irqsave", LOCK, irq, 1, "$", &int_one, &int_one},
332 {"read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
333 {"read_lock_irqsave", LOCK, irq, 1, "$"},
334 {"read_unlock_irqrestore", RESTORE, irq, 1, "$"},
335 {"_read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
336 {"_read_lock_irqsave", LOCK, irq, 1, "$"},
337 {"_read_unlock_irqrestore", RESTORE, irq, 1, "$"},
338 {"__read_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
339 {"__read_unlock_irqrestore", RESTORE, irq, 1, "$"},
340 {"write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
341 {"write_lock_irqsave", LOCK, irq, 1, "$"},
342 {"write_unlock_irqrestore", RESTORE, irq, 1, "$"},
343 {"_write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
344 {"_write_lock_irqsave", LOCK, irq, 1, "$"},
345 {"_write_unlock_irqrestore", RESTORE, irq, 1, "$"},
346 {"__write_lock_irqsave", LOCK, irq, RETURN_VAL, "$"},
347 {"__write_unlock_irqrestore", RESTORE, irq, 1, "$"},
349 {"local_bh_disable", LOCK, bottom_half, NO_ARG, "bh"},
350 {"_local_bh_disable", LOCK, bottom_half, NO_ARG, "bh"},
351 {"__local_bh_disable", LOCK, bottom_half, NO_ARG, "bh"},
352 {"local_bh_enable", UNLOCK, bottom_half, NO_ARG, "bh"},
353 {"_local_bh_enable", UNLOCK, bottom_half, NO_ARG, "bh"},
354 {"__local_bh_enable", 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 {"__spin_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
360 {"__spin_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 {"__read_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
366 {"__read_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
367 {"_raw_read_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
368 {"_raw_read_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 {"__write_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
374 {"__write_unlock_bh", UNLOCK, bottom_half, NO_ARG, "bh"},
375 {"_raw_write_lock_bh", LOCK, bottom_half, NO_ARG, "bh"},
376 {"_raw_write_unlock_bh",UNLOCK, bottom_half, NO_ARG, "bh"},
377 {"spin_trylock_bh", LOCK, bottom_half, NO_ARG, "bh", &int_one, &int_one},
378 {"_spin_trylock_bh", LOCK, bottom_half, NO_ARG, "bh", &int_one, &int_one},
379 {"__spin_trylock_bh", LOCK, bottom_half, NO_ARG, "bh", &int_one, &int_one},
381 {"ffs_mutex_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
383 {"clk_prepare_lock", LOCK, prepare_lock, NO_ARG, "clk"},
384 {"clk_prepare_unlock", UNLOCK, prepare_lock, NO_ARG, "clk"},
385 {"clk_enable_lock", LOCK, enable_lock, -1, "$"},
386 {"clk_enable_unlock", UNLOCK, enable_lock, 0, "$"},
388 {"dma_resv_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
389 {"dma_resv_trylock", LOCK, mutex, 0, "$", &int_one, &int_one},
390 {"dma_resv_lock_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
391 {"dma_resv_unlock", UNLOCK, mutex, 0, "$"},
393 {"modeset_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
394 {"drm_ modeset_lock", LOCK, mutex, 0, "$", &int_zero, &int_zero},
395 {"drm_modeset_lock_single_interruptible", LOCK, mutex, 0, "$", &int_zero, &int_zero},
396 {"drm_exec_unlock_obj", UNLOCK, mutex, 1, "$->resv" },
397 {"modeset_unlock", UNLOCK, mutex, 0, "$"},
398 // {"nvkm_i2c_aux_acquire", LOCK, mutex,
399 {"i915_gem_object_lock_interruptible", LOCK, mutex, 0, "$->base.resv", &int_zero, &int_zero},
400 {"i915_gem_object_lock", LOCK, mutex, 0, "$->base.resv"},
401 {"msm_gem_lock", LOCK, mutex, 0, "$->resv"},
403 {"reiserfs_write_lock_nested", LOCK, mutex, 0, "$"},
404 {"reiserfs_write_unlock_nested", UNLOCK, mutex, 0, "$"},
406 {"rw_lock", LOCK, write_lock, 1, "$"},
407 {"rw_unlock", UNLOCK, write_lock, 1, "$"},
409 {"sem_lock", LOCK, mutex, 0, "$"},
410 {"sem_unlock", UNLOCK, mutex, 0, "$"},
412 {"rcu_lock_acquire", LOCK, rcu, NO_ARG, "rcu"},
413 {"rcu_lock_release", UNLOCK, rcu, NO_ARG, "rcu"},
415 {"rcu_read_lock", LOCK, rcu_read, NO_ARG, "rcu_read"},
416 {"rcu_read_unlock", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
417 {"rcu_read_lock_bh", LOCK, rcu_read, NO_ARG, "rcu_read"},
418 {"rcu_read_unlock_bh", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
420 {"rcu_read_lock_sched", LOCK, rcu_read, NO_ARG, "rcu_read"},
421 {"rcu_read_lock_sched_notrace", LOCK, rcu_read, NO_ARG, "rcu_read"},
422 {"rcu_read_unlock_sched", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
423 {"rcu_read_unlock_sched_notrace", UNLOCK, rcu_read, NO_ARG, "rcu_read"},
425 {"gfs2_trans_begin", LOCK, sem, 0, "&$->sd_log_flush_lock", &int_zero, &int_zero},
427 {"lock_sock", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
428 {"lock_sock_nested", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
429 {"lock_sock_fast", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
430 {"__lock_sock", LOCK, spin_lock, 0, "&$->sk_lock.slock"},
431 {"release_sock", UNLOCK, spin_lock, 0, "&$->sk_lock.slock"},
432 {"__release_sock", UNLOCK, spin_lock, 0, "&$->sk_lock.slock"},
434 {"lock_task_sighand", LOCK, spin_lock, 0, "&$->sighand->siglock", &valid_ptr_min_sval, &valid_ptr_max_sval},
436 {"rcu_nocb_unlock_irqrestore", RESTORE, spin_lock, 0, "&$->nocb_lock"},
437 {"rcu_nocb_unlock_irqrestore", RESTORE, irq, 1, "$" },
439 {"bch_write_bdev_super", IGNORE_LOCK, sem, 0, "&$->sb_write_mutex"},
440 {"dlfb_set_video_mode", IGNORE_LOCK, sem, 0, "&$->urbs.limit_sem"},
442 {"efx_rwsem_assert_write_locked", IGNORE_LOCK, sem, 0, "&"},
444 // The i915_gem_ww_ctx_unlock_all() is too complicated
445 {"i915_gem_object_pin_pages_unlocked", IGNORE_LOCK, mutex, 0, "$->base.resv"},
446 {"i915_gem_object_pin_map_unlocked", IGNORE_LOCK, mutex, 0, "$->base.resv"},
447 {"i915_gem_object_fill_blt", IGNORE_LOCK, mutex, 0, "$->base.resv"},
448 {"i915_vma_pin", IGNORE_LOCK, mutex, 0, "$->base.resv"},
450 { "perf_event_period", IGNORE_LOCK, mutex, 0, "&$->ctx->mutex"},
451 { "perf_event_enable", IGNORE_LOCK, mutex, 0, "&$->ctx->mutex"},
453 { "deactivate_locked_super", UNLOCK, spin_lock, 0, "&$->s_umount"},
454 { "ext4_lock_group", LOCK, spin_lock, 0, "$"},
455 { "ext4_unlock_group", UNLOCK, spin_lock, 0, "$"},
457 {"__pte_offset_map_lock", LOCK, spin_lock, 3, "*$", &valid_ptr_min_sval, &valid_ptr_max_sval},
458 {"pte_offset_map_lock", LOCK, spin_lock, 3, "*$", &valid_ptr_min_sval, &valid_ptr_max_sval},
460 {"uart_unlock_and_check_sysrq_irqrestore", UNLOCK, spin_lock, 0, "&$->lock"},
462 {"class_mutex_destructor", UNLOCK, mutex, 0, "*$", NULL, NULL, &match_class_mutex_destructor},
467 struct macro_info {
468 const char *macro;
469 int action;
470 int param;
473 static struct macro_info macro_table[] = {
474 {"genpd_lock", LOCK, 0},
475 {"genpd_lock_nested", LOCK, 0},
476 {"genpd_lock_interruptible", LOCK, 0},
477 {"genpd_unlock", UNLOCK, 0},
480 static const char *false_positives[][2] = {
481 {"fs/jffs2/", "->alloc_sem"},
482 {"fs/xfs/", "->b_sema"},
483 {"mm/", "pvmw->ptl"},
484 {"drivers/gpu/drm/i915/", "->base.resv"},
487 static struct stree *start_states;
489 static struct tracker_list *locks;
491 static struct expression *ignored_reset;
492 static void reset(struct sm_state *sm, struct expression *mod_expr)
494 struct expression *faked;
496 if (mod_expr && mod_expr->type == EXPR_ASSIGNMENT &&
497 mod_expr->left == ignored_reset)
498 return;
499 faked = get_faked_expression();
500 if (faked && faked->type == EXPR_ASSIGNMENT &&
501 faked->left == ignored_reset)
502 return;
504 set_state(my_id, sm->name, sm->sym, &start_state);
507 void match_class_mutex_destructor(const char *fn, struct expression *expr, void *data)
509 struct expression *arg, *lock;
511 if (!expr || expr->type != EXPR_CALL)
512 return;
513 arg = get_argument_from_call_expr(expr->args, 0);
514 arg = strip_expr(arg);
515 if (!arg || arg->type != EXPR_PREOP || arg->op != '&')
516 return;
517 arg = strip_expr(arg->unop);
518 lock = get_assigned_expr(arg);
519 if (!lock)
520 return;
521 set_state_expr(my_id, lock, &unlocked);
524 static struct smatch_state *get_start_state(struct sm_state *sm)
526 struct smatch_state *orig;
528 if (!sm)
529 return NULL;
531 orig = get_state_stree(start_states, my_id, sm->name, sm->sym);
532 if (orig)
533 return orig;
534 return NULL;
537 static struct expression *remove_spinlock_check(struct expression *expr)
539 if (expr->type != EXPR_CALL)
540 return expr;
541 if (expr->fn->type != EXPR_SYMBOL)
542 return expr;
543 if (strcmp(expr->fn->symbol_name->name, "spinlock_check"))
544 return expr;
545 expr = get_argument_from_call_expr(expr->args, 0);
546 return expr;
549 static struct expression *filter_kernel_args(struct expression *arg)
551 if (arg->type == EXPR_PREOP && arg->op == '&')
552 return strip_expr(arg->unop);
553 if (!is_pointer(arg))
554 return arg;
555 return deref_expression(strip_expr(arg));
558 static char *lock_to_name_sym(struct expression *expr, struct symbol **sym)
560 expr = remove_spinlock_check(expr);
561 expr = filter_kernel_args(expr);
562 return expr_to_str_sym(expr, sym);
565 static struct smatch_state *unmatched_state(struct sm_state *sm)
567 return &start_state;
570 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
572 if (is_impossible_path())
573 set_state(my_id, cur->name, cur->sym, &impossible);
576 static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
578 if (s1 == &impossible)
579 return s2;
580 if (s2 == &impossible)
581 return s1;
582 return &merged;
585 static struct sm_state *get_best_match(const char *key, int lock_unlock)
587 struct sm_state *sm;
588 struct sm_state *match;
589 int cnt = 0;
590 int start_pos, state_len, key_len, chunks, i;
592 if (strncmp(key, "$->", 3) == 0)
593 key += 3;
595 key_len = strlen(key);
596 chunks = 0;
597 for (i = key_len - 1; i > 0; i--) {
598 if (key[i] == '>' || key[i] == '.')
599 chunks++;
600 if (chunks == 2) {
601 key += (i + 1);
602 key_len = strlen(key);
603 break;
607 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
608 if (((lock_unlock == UNLOCK || lock_unlock == RESTORE) &&
609 sm->state != &locked) ||
610 (lock_unlock == LOCK && sm->state != &unlocked))
611 continue;
612 state_len = strlen(sm->name);
613 if (state_len < key_len)
614 continue;
615 start_pos = state_len - key_len;
616 if ((start_pos == 0 || !isalnum(sm->name[start_pos - 1])) &&
617 strcmp(sm->name + start_pos, key) == 0) {
618 cnt++;
619 match = sm;
621 } END_FOR_EACH_SM(sm);
623 if (cnt == 1)
624 return match;
625 return NULL;
628 static char *use_best_match(const char *key, int lock_unlock, struct symbol **sym)
630 struct sm_state *match;
632 match = get_best_match(key, lock_unlock);
633 if (!match) {
634 *sym = NULL;
635 return alloc_string(key);
637 *sym = match->sym;
638 return alloc_string(match->name);
641 static void set_start_state(const char *name, struct symbol *sym, struct smatch_state *start)
643 struct smatch_state *orig;
645 orig = get_state_stree(start_states, my_id, name, sym);
646 if (!orig)
647 set_state_stree(&start_states, my_id, name, sym, start);
648 else if (orig != start)
649 set_state_stree(&start_states, my_id, name, sym, &undefined);
652 static bool common_false_positive(const char *name)
654 const char *path, *lname;
655 int i, len_total, len_path, len_name, skip;
657 if (!get_filename())
658 return false;
660 len_total = strlen(name);
661 for (i = 0; i < ARRAY_SIZE(false_positives); i++) {
662 path = false_positives[i][0];
663 lname = false_positives[i][1];
665 len_path = strlen(path);
666 len_name = strlen(lname);
668 if (len_name > len_total)
669 continue;
670 skip = len_total - len_name;
672 if (strncmp(get_filename(), path, len_path) == 0 &&
673 strcmp(name + skip, lname) == 0)
674 return true;
677 return false;
680 static bool sm_in_start_states(struct sm_state *sm)
682 if (!sm || !cur_func_sym)
683 return false;
684 if (sm->line == cur_func_sym->pos.line)
685 return true;
686 return false;
689 static void warn_on_double(struct sm_state *sm, struct smatch_state *state)
691 struct sm_state *tmp;
693 if (!sm)
694 return;
696 FOR_EACH_PTR(sm->possible, tmp) {
697 if (tmp->state == state)
698 goto found;
699 } END_FOR_EACH_PTR(tmp);
701 return;
702 found:
703 // FIXME: called with read_lock held
704 // drivers/scsi/aic7xxx/aic7xxx_osm.c:1591 ahc_linux_isr() error: double locked 'flags' (orig line 1584)
705 if (strcmp(sm->name, "bottom_half") == 0)
706 return;
707 if (strstr(sm->name, "rcu"))
708 return;
709 if (strstr(sm->name, "timeline->mutex"))
710 return;
711 if (common_false_positive(sm->name))
712 return;
714 if (state == &locked && sm_in_start_states(tmp)) {
715 // sm_warning("called with lock held. '%s'", sm->name);
716 } else {
717 // sm_msg("error: double %s '%s' (orig line %u)",
718 // state->name, sm->name, tmp->line);
722 static bool handle_macro_lock_unlock(void)
724 struct expression *expr, *arg;
725 struct macro_info *info;
726 struct sm_state *sm;
727 struct symbol *sym;
728 const char *macro;
729 char *name;
730 bool ret = false;
731 int i;
733 expr = last_ptr_list((struct ptr_list *)big_expression_stack);
734 while (expr && expr->type == EXPR_ASSIGNMENT)
735 expr = strip_expr(expr->right);
736 if (!expr || expr->type != EXPR_CALL)
737 return false;
739 macro = get_macro_name(expr->pos);
740 if (!macro)
741 return false;
743 for (i = 0; i < ARRAY_SIZE(macro_table); i++) {
744 info = &macro_table[i];
746 if (strcmp(macro, info->macro) != 0)
747 continue;
748 arg = get_argument_from_call_expr(expr->args, info->param);
749 name = expr_to_str_sym(arg, &sym);
750 if (!name || !sym)
751 goto free;
752 sm = get_sm_state(my_id, name, sym);
754 if (info->action == LOCK) {
755 if (!get_start_state(sm))
756 set_start_state(name, sym, &unlocked);
757 if (sm && sm->line != expr->pos.line)
758 warn_on_double(sm, &locked);
759 set_state(my_id, name, sym, &locked);
760 } else {
761 if (!get_start_state(sm))
762 set_start_state(name, sym, &locked);
763 if (sm && sm->line != expr->pos.line)
764 warn_on_double(sm, &unlocked);
765 set_state(my_id, name, sym, &unlocked);
767 ret = true;
768 free:
769 free_string(name);
770 return ret;
772 return false;
775 static bool is_local_IRQ_save(const char *name, struct symbol *sym, struct lock_info *info)
777 if (name && strcmp(name, "flags") == 0)
778 return true;
779 if (!sym)
780 return false;
781 if (!sym->ident || strcmp(sym->ident->name, name) != 0)
782 return false;
783 if (!info)
784 return false;
785 return strstr(info->function, "irq") && strstr(info->function, "save");
788 static void do_lock(const char *name, struct symbol *sym, struct lock_info *info)
790 struct sm_state *sm;
791 bool delete_null = false;
793 if (!info && handle_macro_lock_unlock())
794 return;
796 add_tracker(&locks, my_id, name, sym);
798 sm = get_sm_state(my_id, name, sym);
799 if (!get_start_state(sm))
800 set_start_state(name, sym, &unlocked);
801 if (!sm && !is_local_IRQ_save(name, sym, info) && !sym) {
802 sm = get_best_match(name, LOCK);
803 if (sm) {
804 name = sm->name;
805 if (sm->sym)
806 sym = sm->sym;
807 else
808 delete_null = true;
811 warn_on_double(sm, &locked);
812 if (delete_null)
813 set_state(my_id, name, NULL, &ignore);
815 set_state(my_id, name, sym, &locked);
818 static void do_unlock(const char *name, struct symbol *sym, struct lock_info *info)
820 struct sm_state *sm;
821 bool delete_null = false;
823 if (__path_is_null())
824 return;
826 if (!info && handle_macro_lock_unlock())
827 return;
829 add_tracker(&locks, my_id, name, sym);
830 sm = get_sm_state(my_id, name, sym);
831 if (!sm && !info && !is_local_IRQ_save(name, sym, info)) {
832 sm = get_best_match(name, UNLOCK);
833 if (sm) {
834 name = sm->name;
835 if (sm->sym)
836 sym = sm->sym;
837 else
838 delete_null = true;
841 if (!get_start_state(sm))
842 set_start_state(name, sym, &locked);
843 warn_on_double(sm, &unlocked);
844 if (delete_null)
845 set_state(my_id, name, NULL, &ignore);
846 set_state(my_id, name, sym, &unlocked);
849 static void do_restore(const char *name, struct symbol *sym, struct lock_info *info)
851 struct sm_state *sm;
853 if (__path_is_null())
854 return;
856 sm = get_sm_state(my_id, name, sym);
857 if (!get_start_state(sm))
858 set_start_state(name, sym, &locked);
860 add_tracker(&locks, my_id, name, sym);
861 set_state(my_id, name, sym, &restore);
864 static int get_db_type(struct sm_state *sm)
867 * Bottom half is complicated because it's nestable.
868 * Say it's merged at the start and we lock and unlock then
869 * it should go back to merged.
871 if (sm->state == get_start_state(sm)) {
872 if (sm->state == &locked)
873 return KNOWN_LOCKED;
874 if (sm->state == &unlocked)
875 return KNOWN_UNLOCKED;
878 if (sm->state == &locked)
879 return LOCK;
880 if (sm->state == &unlocked)
881 return UNLOCK;
882 if (sm->state == &restore)
883 return RESTORE;
884 return LOCK;
887 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
889 struct smatch_state *start;
890 struct sm_state *sm;
891 const char *param_name;
892 int param;
894 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
895 if (sm->state != &locked &&
896 sm->state != &unlocked &&
897 sm->state != &restore)
898 continue;
899 if (sm->name[0] == '$')
900 continue;
903 * If the state is locked at the end, that doesn't mean
904 * anything. It could be been locked at the start. Or
905 * it could be &merged at the start but its locked now
906 * because of implications and not because we set the
907 * state.
909 * This is slightly a hack, but when we change the state, we
910 * call set_start_state() so if get_start_state() returns NULL
911 * that means we haven't manually the locked state.
913 start = get_start_state(sm);
914 if (sm->state == &restore) {
915 if (start != &locked)
916 continue;
917 } else if (!start || sm->state == start)
918 continue; /* !start means it was passed in */
920 param = get_param_key_from_sm(sm, expr, &param_name);
921 sql_insert_return_states(return_id, return_ranges,
922 get_db_type(sm),
923 param, param_name, "");
924 } END_FOR_EACH_SM(sm);
927 static int sm_both_locked_and_unlocked(struct sm_state *sm)
929 int is_locked = 0;
930 int is_unlocked = 0;
931 struct sm_state *tmp;
933 if (sm->state != &merged)
934 return 0;
936 FOR_EACH_PTR(sm->possible, tmp) {
937 if (tmp->state == &locked)
938 is_locked = 1;
939 if (tmp->state == &unlocked)
940 is_unlocked = 1;
941 } END_FOR_EACH_PTR(tmp);
943 return is_locked && is_unlocked;
947 static bool is_EINTR(struct range_list *rl)
949 sval_t sval;
951 if (!rl_to_sval(rl, &sval))
952 return false;
953 return sval.value == -4;
956 static bool sym_in_lock_table(struct symbol *sym)
958 int i;
960 if (!sym || !sym->ident)
961 return false;
963 for (i = 0; lock_table[i].function != NULL; i++) {
964 if (strcmp(lock_table[i].function, sym->ident->name) == 0)
965 return true;
967 return false;
970 static bool func_in_lock_table(struct expression *expr)
972 if (expr->type != EXPR_SYMBOL)
973 return false;
974 return sym_in_lock_table(expr->symbol);
977 #define NUM_BUCKETS (RET_UNKNOWN + 1)
979 static void check_lock(char *name, struct symbol *sym)
981 struct range_list *locked_lines = NULL;
982 struct range_list *unlocked_lines = NULL;
983 int locked_buckets[NUM_BUCKETS] = {};
984 int unlocked_buckets[NUM_BUCKETS] = {};
985 struct stree *stree, *orig;
986 struct sm_state *return_sm;
987 struct sm_state *sm;
988 sval_t line = sval_type_val(&int_ctype, 0);
989 int bucket;
990 int i;
992 if (sym_in_lock_table(cur_func_sym))
993 return;
995 FOR_EACH_PTR(get_all_return_strees(), stree) {
996 orig = __swap_cur_stree(stree);
998 if (is_impossible_path())
999 goto swap_stree;
1001 return_sm = get_sm_state(RETURN_ID, "return_ranges", NULL);
1002 if (!return_sm)
1003 goto swap_stree;
1004 line.value = return_sm->line;
1006 sm = get_sm_state(my_id, name, sym);
1007 if (!sm)
1008 goto swap_stree;
1010 if (parent_is_gone_var_sym(sm->name, sm->sym))
1011 goto swap_stree;
1013 if (sm->state != &locked &&
1014 sm->state != &unlocked &&
1015 sm->state != &restore)
1016 goto swap_stree;
1018 if ((sm->state == &unlocked || sm->state == &restore) &&
1019 is_EINTR(estate_rl(return_sm->state)))
1020 goto swap_stree;
1022 bucket = success_fail_return(estate_rl(return_sm->state));
1023 if (sm->state == &locked) {
1024 add_range(&locked_lines, line, line);
1025 locked_buckets[bucket] = true;
1027 if (sm->state == &unlocked || sm->state == &restore) {
1028 add_range(&unlocked_lines, line, line);
1029 unlocked_buckets[bucket] = true;
1031 swap_stree:
1032 __swap_cur_stree(orig);
1033 } END_FOR_EACH_PTR(stree);
1036 if (!locked_lines || !unlocked_lines)
1037 return;
1039 for (i = 0; i < NUM_BUCKETS; i++) {
1040 if (locked_buckets[i] && unlocked_buckets[i])
1041 goto complain;
1044 if (locked_buckets[RET_FAIL])
1045 goto complain;
1047 return;
1049 complain:
1050 if (common_false_positive(name))
1051 return;
1053 sm_msg("warn: inconsistent returns '%s'.", name);
1054 sm_printf(" Locked on : %s\n", show_rl(locked_lines));
1055 sm_printf(" Unlocked on: %s\n", show_rl(unlocked_lines));
1058 static void match_func_end(struct symbol *sym)
1060 struct tracker *tracker;
1062 FOR_EACH_PTR(locks, tracker) {
1063 check_lock(tracker->name, tracker->sym);
1064 } END_FOR_EACH_PTR(tracker);
1067 static void db_param_locked_unlocked(struct expression *expr, int param, const char *key, int lock_unlock, struct lock_info *info)
1069 struct expression *call, *arg;
1070 char *name;
1071 struct symbol *sym;
1073 if (info && info->action == IGNORE_LOCK)
1074 return;
1076 call = expr;
1077 while (call->type == EXPR_ASSIGNMENT)
1078 call = strip_expr(call->right);
1079 if (call->type != EXPR_CALL)
1080 return;
1082 if (!info && func_in_lock_table(call->fn))
1083 return;
1085 if (param == -2) {
1086 if (!info)
1087 name = use_best_match(key, lock_unlock, &sym);
1088 else {
1089 name = alloc_string(info->key);
1090 sym = NULL;
1092 } else if (param == -1) {
1093 if (expr->type != EXPR_ASSIGNMENT)
1094 return;
1095 ignored_reset = expr->left;
1097 name = get_variable_from_key(expr->left, key, &sym);
1098 } else {
1099 arg = get_argument_from_call_expr(call->args, param);
1100 if (!arg)
1101 return;
1103 arg = remove_spinlock_check(arg);
1104 name = get_variable_from_key(arg, key, &sym);
1107 if (!name || !sym)
1108 goto free;
1110 if (lock_unlock == LOCK)
1111 do_lock(name, sym, info);
1112 else if (lock_unlock == UNLOCK)
1113 do_unlock(name, sym, info);
1114 else if (lock_unlock == RESTORE)
1115 do_restore(name, sym, info);
1117 free:
1118 free_string(name);
1121 static void db_param_locked(struct expression *expr, int param, char *key, char *value)
1123 db_param_locked_unlocked(expr, param, key, LOCK, NULL);
1126 static void db_param_unlocked(struct expression *expr, int param, char *key, char *value)
1128 db_param_locked_unlocked(expr, param, key, UNLOCK, NULL);
1131 static void db_param_restore(struct expression *expr, int param, char *key, char *value)
1133 db_param_locked_unlocked(expr, param, key, RESTORE, NULL);
1136 static void match_lock_unlock(const char *fn, struct expression *expr, void *data)
1138 struct lock_info *info = data;
1139 struct expression *parent;
1141 if (info->arg == -1) {
1142 parent = expr_get_parent_expr(expr);
1143 while (parent && parent->type != EXPR_ASSIGNMENT)
1144 parent = expr_get_parent_expr(parent);
1145 if (!parent || parent->type != EXPR_ASSIGNMENT)
1146 return;
1147 expr = parent;
1150 db_param_locked_unlocked(expr, info->arg, info->key, info->action, info);
1153 static void match_lock_held(const char *fn, struct expression *call_expr,
1154 struct expression *assign_expr, void *data)
1156 struct lock_info *info = data;
1158 db_param_locked_unlocked(assign_expr ?: call_expr, info->arg, info->key, info->action, info);
1161 static void match_assign(struct expression *expr)
1163 struct smatch_state *state;
1165 /* This is only for the DB */
1166 if (is_fake_var_assign(expr))
1167 return;
1168 state = get_state_expr(my_id, expr->right);
1169 if (!state)
1170 return;
1171 set_state_expr(my_id, expr->left, state);
1174 static struct stree *printed;
1175 static void call_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1177 int locked_type = 0;
1179 if (sm->state == &locked)
1180 locked_type = LOCK;
1181 else if (sm->state == &unlocked)
1182 locked_type = UNLOCK;
1183 else if (slist_has_state(sm->possible, &locked) ||
1184 slist_has_state(sm->possible, &half_locked))
1185 locked_type = HALF_LOCKED;
1186 else
1187 return;
1189 avl_insert(&printed, sm);
1190 sql_insert_caller_info(call, locked_type, param, printed_name, "");
1193 static void match_call_info(struct expression *expr)
1195 struct sm_state *sm;
1196 const char *name;
1197 int locked_type;
1199 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1200 if (sm->state == &locked)
1201 locked_type = LOCK;
1202 else if (sm->state == &half_locked ||
1203 slist_has_state(sm->possible, &locked))
1204 locked_type = HALF_LOCKED;
1205 else
1206 continue;
1208 if (avl_lookup(printed, sm))
1209 continue;
1211 if (strcmp(sm->name, "bottom_half") == 0)
1212 name = "bh";
1213 else if (strcmp(sm->name, "rcu_read") == 0)
1214 name = "rcu_read_lock";
1215 else
1216 name = sm->name;
1218 if (strncmp(name, "__fake_param", 12) == 0 ||
1219 strchr(name, '$'))
1220 continue;
1222 sql_insert_caller_info(expr, locked_type, -2, name, "");
1223 } END_FOR_EACH_SM(sm);
1224 free_stree(&printed);
1227 static void set_locked(const char *name, struct symbol *sym, char *value)
1229 set_state(my_id, name, sym, &locked);
1232 static void set_half_locked(const char *name, struct symbol *sym, char *value)
1234 set_state(my_id, name, sym, &half_locked);
1237 static void set_unlocked(const char *name, struct symbol *sym, char *value)
1239 set_state(my_id, name, sym, &unlocked);
1242 static void match_after_func(struct symbol *sym)
1244 free_stree(&start_states);
1247 static void match_dma_resv_lock_NULL(const char *fn, struct expression *call_expr,
1248 struct expression *assign_expr, void *_index)
1250 struct expression *lock, *ctx;
1251 char *lock_name;
1252 struct symbol *sym;
1254 lock = get_argument_from_call_expr(call_expr->args, 0);
1255 ctx = get_argument_from_call_expr(call_expr->args, 1);
1256 if (!expr_is_zero(ctx))
1257 return;
1259 lock_name = lock_to_name_sym(lock, &sym);
1260 if (!lock_name || !sym)
1261 goto free;
1262 do_lock(lock_name, sym, NULL);
1263 free:
1264 free_string(lock_name);
1267 /* print_held_locks() is used in check_call_tree.c */
1268 void print_held_locks(void)
1270 struct stree *stree;
1271 struct sm_state *sm;
1272 int i = 0;
1274 stree = __get_cur_stree();
1275 FOR_EACH_MY_SM(my_id, stree, sm) {
1276 if (sm->state != &locked)
1277 continue;
1278 if (i++)
1279 sm_printf(" ");
1280 sm_printf("'%s'", sm->name);
1281 } END_FOR_EACH_SM(sm);
1284 static void load_table(struct lock_info *lock_table)
1286 int i;
1288 for (i = 0; lock_table[i].function != NULL; i++) {
1289 struct lock_info *lock = &lock_table[i];
1291 if (lock->call_back) {
1292 add_function_hook(lock->function, lock->call_back, lock);
1293 } else if (lock->implies_start) {
1294 return_implies_state_sval(lock->function,
1295 *lock->implies_start,
1296 *lock->implies_end,
1297 &match_lock_held, lock);
1298 } else {
1299 add_function_hook(lock->function, &match_lock_unlock, lock);
1304 static bool is_smp_config(void)
1306 struct ident *id;
1308 id = built_in_ident("CONFIG_SMP");
1309 return !!lookup_symbol(id, NS_MACRO);
1312 void check_locking(int id)
1314 my_id = id;
1316 if (option_project != PROJ_KERNEL)
1317 return;
1319 if (!is_smp_config())
1320 return;
1322 load_table(lock_table);
1324 set_dynamic_states(my_id);
1325 add_unmatched_state_hook(my_id, &unmatched_state);
1326 add_pre_merge_hook(my_id, &pre_merge_hook);
1327 add_merge_hook(my_id, &merge_func);
1328 add_modification_hook(my_id, &reset);
1330 add_hook(&match_assign, ASSIGNMENT_HOOK);
1331 add_hook(&match_func_end, END_FUNC_HOOK);
1333 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1334 add_function_data((unsigned long *)&start_states);
1336 add_caller_info_callback(my_id, call_info_callback);
1337 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1339 add_split_return_callback(match_return_info);
1340 select_return_states_hook(LOCK, &db_param_locked);
1341 select_return_states_hook(UNLOCK, &db_param_unlocked);
1342 select_return_states_hook(RESTORE, &db_param_restore);
1344 select_caller_name_sym(set_locked, LOCK);
1345 select_caller_name_sym(set_half_locked, HALF_LOCKED);
1346 select_caller_name_sym(set_unlocked, UNLOCK);
1348 return_implies_state("dma_resv_lock", -4095, -1, &match_dma_resv_lock_NULL, 0);