[PATCH] tsan: do not deadlock after fork

Dmitry Vyukov dvyukov at google.com
Tue Dec 24 08:48:25 PST 2013


Hi kcc, samsonov, eugenis, glider, earthdok, timurrrr,

This is not a finished version, but more for a discussion.

The problem this tries to solve is as follows:
1. There are programs that carefully fork-exec in multithreaded programs, but this carefully-ness does not work under tsan because there are lots of internal mutexes.
2. There are system libraries that create background threads (libglib), they usually do not interfere with user program so it can fork (thinking that it is single-threaded process); this also does not work under tsan because it intercepts some functions in the background thread.

The public interface is as follows:
// MutexForkLocker allows to effectively lock all mutexes in process at once.
// This is useful before fork.
class MutexForkLocker {
 public:
  // Must be called before/after fork.
  static void BeforeFork();
  static void AfterFork();

  // Must be called when thread starts/finishes (before/after it takes any mutexes).
  static void RegisterThread();
  static void UnregisterThread();
};

This allows to synchronize with all internal mutexes in the process w/o the need to enumerate all them.

This introduces small overhead onto mutex fast-paths:
1. Lock needs to increment TLS counter, load TLS counter, load read-mostly global, branch.
2. Unlock needs to decrement TLS counter.

I would like to have a simpler solution to the problem, but I do not see any other simple and reliable solution.

Thoughts?

http://llvm-reviews.chandlerc.com/D2470

Files:
  sanitizer_common/sanitizer_mutex.h
  sanitizer_common/sanitizer_linux.cc
  sanitizer_common/tests/sanitizer_thread_registry_test.cc
  sanitizer_common/tests/sanitizer_mutex_test.cc
  tsan/rtl/tsan_mutex.h
  tsan/rtl/tsan_mutex.cc
  tsan/rtl/tsan_interceptors.cc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2470.1.patch
Type: text/x-patch
Size: 8913 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131224/30123552/attachment.bin>


More information about the llvm-commits mailing list