1 //===-- sanitizer_deadlock_detector2.cc -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Deadlock detector implementation based on adjacency lists.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_deadlock_detector_interface.h"
15 #include "sanitizer_common.h"
16 #include "sanitizer_allocator_internal.h"
17 #include "sanitizer_placement_new.h"
18 #include "sanitizer_mutex.h"
20 #if SANITIZER_DEADLOCK_DETECTOR_VERSION == 2
22 namespace __sanitizer {
24 const int kMaxNesting = 64;
26 const u32 kEndId = -2;
27 const int kMaxLink = 8;
28 const int kL1Size = 1024;
29 const int kL2Size = 1024;
30 const int kMaxMutex = kL1Size * kL2Size;
36 explicit Id(u32 id = 0, u32 seq = 0)
49 explicit Link(u32 id = 0, u32 seq = 0, u32 tid = 0, u32 s0 = 0, u32 s1 = 0)
58 struct DDPhysicalThread {
61 bool visited[kMaxMutex];
62 Link pending[kMaxMutex];
71 struct DDLogicalThread {
73 ThreadMutex locked[kMaxNesting];
84 struct DD : public DDetector {
85 explicit DD(const DDFlags *flags);
87 DDPhysicalThread* CreatePhysicalThread();
88 void DestroyPhysicalThread(DDPhysicalThread *pt);
90 DDLogicalThread* CreateLogicalThread(u64 ctx);
91 void DestroyLogicalThread(DDLogicalThread *lt);
93 void MutexInit(DDCallback *cb, DDMutex *m);
94 void MutexBeforeLock(DDCallback *cb, DDMutex *m, bool wlock);
95 void MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock,
97 void MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock);
98 void MutexDestroy(DDCallback *cb, DDMutex *m);
100 DDReport *GetReport(DDCallback *cb);
102 void CycleCheck(DDPhysicalThread *pt, DDLogicalThread *lt, DDMutex *mtx);
103 void Report(DDPhysicalThread *pt, DDLogicalThread *lt, int npath);
104 u32 allocateId(DDCallback *cb);
105 Mutex *getMutex(u32 id);
106 u32 getMutexId(Mutex *m);
110 Mutex* mutex[kL1Size];
113 InternalMmapVector<u32> free_id;
117 DDetector *DDetector::Create(const DDFlags *flags) {
119 void *mem = MmapOrDie(sizeof(DD), "deadlock detector");
120 return new(mem) DD(flags);
123 DD::DD(const DDFlags *flags) : flags(*flags) { free_id.reserve(1024); }
125 DDPhysicalThread* DD::CreatePhysicalThread() {
126 DDPhysicalThread *pt = (DDPhysicalThread*)MmapOrDie(sizeof(DDPhysicalThread),
127 "deadlock detector (physical thread)");
131 void DD::DestroyPhysicalThread(DDPhysicalThread *pt) {
132 pt->~DDPhysicalThread();
133 UnmapOrDie(pt, sizeof(DDPhysicalThread));
136 DDLogicalThread* DD::CreateLogicalThread(u64 ctx) {
137 DDLogicalThread *lt = (DDLogicalThread*)InternalAlloc(
138 sizeof(DDLogicalThread));
144 void DD::DestroyLogicalThread(DDLogicalThread *lt) {
145 lt->~DDLogicalThread();
149 void DD::MutexInit(DDCallback *cb, DDMutex *m) {
150 VPrintf(2, "#%llu: DD::MutexInit(%p)\n", cb->lt->ctx, m);
153 atomic_store(&m->owner, 0, memory_order_relaxed);
156 Mutex *DD::getMutex(u32 id) {
157 return &mutex[id / kL2Size][id % kL2Size];
160 u32 DD::getMutexId(Mutex *m) {
161 for (int i = 0; i < kL1Size; i++) {
162 Mutex *tab = mutex[i];
165 if (m >= tab && m < tab + kL2Size)
166 return i * kL2Size + (m - tab);
171 u32 DD::allocateId(DDCallback *cb) {
173 SpinMutexLock l(&mtx);
174 if (free_id.size() > 0) {
178 CHECK_LT(id_gen, kMaxMutex);
179 if ((id_gen % kL2Size) == 0) {
180 mutex[id_gen / kL2Size] = (Mutex*)MmapOrDie(kL2Size * sizeof(Mutex),
181 "deadlock detector (mutex table)");
185 CHECK_LE(id, kMaxMutex);
186 VPrintf(3, "#%llu: DD::allocateId assign id %d\n", cb->lt->ctx, id);
190 void DD::MutexBeforeLock(DDCallback *cb, DDMutex *m, bool wlock) {
191 VPrintf(2, "#%llu: DD::MutexBeforeLock(%p, wlock=%d) nlocked=%d\n",
192 cb->lt->ctx, m, wlock, cb->lt->nlocked);
193 DDPhysicalThread *pt = cb->pt;
194 DDLogicalThread *lt = cb->lt;
196 uptr owner = atomic_load(&m->owner, memory_order_relaxed);
197 if (owner == (uptr)cb->lt) {
198 VPrintf(3, "#%llu: DD::MutexBeforeLock recursive\n",
203 CHECK_LE(lt->nlocked, kMaxNesting);
205 // FIXME(dvyukov): don't allocate id if lt->nlocked == 0?
207 m->id = allocateId(cb);
209 ThreadMutex *tm = <->locked[lt->nlocked++];
211 if (flags.second_deadlock_stack)
212 tm->stk = cb->Unwind();
213 if (lt->nlocked == 1) {
214 VPrintf(3, "#%llu: DD::MutexBeforeLock first mutex\n",
220 Mutex *mtx = getMutex(m->id);
221 for (int i = 0; i < lt->nlocked - 1; i++) {
222 u32 id1 = lt->locked[i].id;
223 u32 stk1 = lt->locked[i].stk;
224 Mutex *mtx1 = getMutex(id1);
225 SpinMutexLock l(&mtx1->mtx);
226 if (mtx1->nlink == kMaxLink) {
227 // FIXME(dvyukov): check stale links
231 for (; li < mtx1->nlink; li++) {
232 Link *link = &mtx1->link[li];
233 if (link->id == m->id) {
234 if (link->seq != mtx->seq) {
235 link->seq = mtx->seq;
238 link->stk1 = cb->Unwind();
240 VPrintf(3, "#%llu: DD::MutexBeforeLock added %d->%d link\n",
241 cb->lt->ctx, getMutexId(mtx1), m->id);
246 if (li == mtx1->nlink) {
247 // FIXME(dvyukov): check stale links
248 Link *link = &mtx1->link[mtx1->nlink++];
250 link->seq = mtx->seq;
253 link->stk1 = cb->Unwind();
255 VPrintf(3, "#%llu: DD::MutexBeforeLock added %d->%d link\n",
256 cb->lt->ctx, getMutexId(mtx1), m->id);
260 if (!added || mtx->nlink == 0) {
261 VPrintf(3, "#%llu: DD::MutexBeforeLock don't check\n",
266 CycleCheck(pt, lt, m);
269 void DD::MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock,
271 VPrintf(2, "#%llu: DD::MutexAfterLock(%p, wlock=%d, try=%d) nlocked=%d\n",
272 cb->lt->ctx, m, wlock, trylock, cb->lt->nlocked);
273 DDLogicalThread *lt = cb->lt;
275 uptr owner = atomic_load(&m->owner, memory_order_relaxed);
276 if (owner == (uptr)cb->lt) {
277 VPrintf(3, "#%llu: DD::MutexAfterLock recursive\n", cb->lt->ctx);
284 VPrintf(3, "#%llu: DD::MutexAfterLock set owner\n", cb->lt->ctx);
285 CHECK_EQ(m->recursion, 0);
287 atomic_store(&m->owner, (uptr)cb->lt, memory_order_relaxed);
293 CHECK_LE(lt->nlocked, kMaxNesting);
295 m->id = allocateId(cb);
296 ThreadMutex *tm = <->locked[lt->nlocked++];
298 if (flags.second_deadlock_stack)
299 tm->stk = cb->Unwind();
302 void DD::MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) {
303 VPrintf(2, "#%llu: DD::MutexBeforeUnlock(%p, wlock=%d) nlocked=%d\n",
304 cb->lt->ctx, m, wlock, cb->lt->nlocked);
305 DDLogicalThread *lt = cb->lt;
307 uptr owner = atomic_load(&m->owner, memory_order_relaxed);
308 if (owner == (uptr)cb->lt) {
309 VPrintf(3, "#%llu: DD::MutexBeforeUnlock recursive\n", cb->lt->ctx);
310 if (--m->recursion > 0)
312 VPrintf(3, "#%llu: DD::MutexBeforeUnlock reset owner\n", cb->lt->ctx);
313 atomic_store(&m->owner, 0, memory_order_relaxed);
315 CHECK_NE(m->id, kNoId);
316 int last = lt->nlocked - 1;
317 for (int i = last; i >= 0; i--) {
318 if (cb->lt->locked[i].id == m->id) {
319 lt->locked[i] = lt->locked[last];
326 void DD::MutexDestroy(DDCallback *cb, DDMutex *m) {
327 VPrintf(2, "#%llu: DD::MutexDestroy(%p)\n",
329 DDLogicalThread *lt = cb->lt;
334 // Remove the mutex from lt->locked if there.
335 int last = lt->nlocked - 1;
336 for (int i = last; i >= 0; i--) {
337 if (lt->locked[i].id == m->id) {
338 lt->locked[i] = lt->locked[last];
344 // Clear and invalidate the mutex descriptor.
346 Mutex *mtx = getMutex(m->id);
347 SpinMutexLock l(&mtx->mtx);
352 // Return id to cache.
354 SpinMutexLock l(&mtx);
355 free_id.push_back(m->id);
359 void DD::CycleCheck(DDPhysicalThread *pt, DDLogicalThread *lt,
361 internal_memset(pt->visited, 0, sizeof(pt->visited));
365 Mutex *mtx = getMutex(m->id);
366 SpinMutexLock l(&mtx->mtx);
367 for (int li = 0; li < mtx->nlink; li++)
368 pt->pending[npending++] = mtx->link[li];
370 while (npending > 0) {
371 Link link = pt->pending[--npending];
372 if (link.id == kEndId) {
376 if (pt->visited[link.id])
378 Mutex *mtx1 = getMutex(link.id);
379 SpinMutexLock l(&mtx1->mtx);
380 if (mtx1->seq != link.seq)
382 pt->visited[link.id] = true;
383 if (mtx1->nlink == 0)
385 pt->path[npath++] = link;
386 pt->pending[npending++] = Link(kEndId);
387 if (link.id == m->id)
388 return Report(pt, lt, npath); // Bingo!
389 for (int li = 0; li < mtx1->nlink; li++) {
390 Link *link1 = &mtx1->link[li];
391 // Mutex *mtx2 = getMutex(link->id);
392 // FIXME(dvyukov): fast seq check
393 // FIXME(dvyukov): fast nlink != 0 check
394 // FIXME(dvyukov): fast pending check?
395 // FIXME(dvyukov): npending can be larger than kMaxMutex
396 pt->pending[npending++] = *link1;
401 void DD::Report(DDPhysicalThread *pt, DDLogicalThread *lt, int npath) {
402 DDReport *rep = &pt->rep;
404 for (int i = 0; i < npath; i++) {
405 Link *link = &pt->path[i];
406 Link *link0 = &pt->path[i ? i - 1 : npath - 1];
407 rep->loop[i].thr_ctx = link->tid;
408 rep->loop[i].mtx_ctx0 = link0->id;
409 rep->loop[i].mtx_ctx1 = link->id;
410 rep->loop[i].stk[0] = flags.second_deadlock_stack ? link->stk0 : 0;
411 rep->loop[i].stk[1] = link->stk1;
413 pt->report_pending = true;
416 DDReport *DD::GetReport(DDCallback *cb) {
417 if (!cb->pt->report_pending)
419 cb->pt->report_pending = false;
423 } // namespace __sanitizer
424 #endif // #if SANITIZER_DEADLOCK_DETECTOR_VERSION == 2