Skip to content

Commit

Permalink
Sched: fix race start_early_threads()
Browse files Browse the repository at this point in the history
In issue #145 I reported a crash during boot in start_early_threads().
I wasn't actually able to replicate this bug on master, but it happens
quite frequently (e.g., on virtually every "make check" run) with some
patches of mine that seem unrelated to this bug.

The problem is that start_early_threads() (added in 63216e8)
iterates on the threads in the thread list, and uses
t->remote_thread_local_var() for each thread. This can only work if
the thread has its TLS initialized, but unfortunately in thread's
constructor we first added the new thread to the list, and only later
called setup_tcb() (which allocates and initializes the TLS). If we're
unlucky, start_early_threads() can find a thread on the list which still
doesn't have its TLS allocated, so remote_thread_local_var() will crash.

The simple fix is to switch the order of the construction: First
set up the new thread's TLS, and only then add it to the list of
threads.

Fixes #145.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
  • Loading branch information
nyh authored and Pekka Enberg committed Jan 2, 2014
1 parent 2ebfd91 commit 4bed7ed
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion core/sched.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ thread::thread(std::function<void ()> func, attr attr, bool main)
, _cleanup([this] { delete this; })
, _joiner(nullptr)
{
setup_tcb();
WITH_LOCK(thread_map_mutex) {
if (!main) {
auto ttid = _s_idgen;
Expand All @@ -591,7 +592,6 @@ thread::thread(std::function<void ()> func, attr attr, bool main)
}
}
}
setup_tcb();
// setup s_current before switching to the thread, so interrupts
// can call thread::current()
// remote_thread_local_var() doesn't work when there is no current
Expand Down

0 comments on commit 4bed7ed

Please sign in to comment.