Skip to content

Commit c856a10

Browse files
jfeng18lihuiba
andauthored
Fix thread_migrate TOCTOU race and work-stealing sleepq dangling ref (#1494)
* fix: correct lock order in thread_migrate * fix: skip sleepq threads in work-stealing * Update thread.cpp --------- Co-authored-by: Huiba Li <lihuiba@gmail.com>
1 parent 3b388a1 commit c856a10

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

thread/thread.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,12 @@ R"(
19961996
if ((possibly_running && th->state == states::RUNNING) ||
19971997
!th->allow_work_stealing()) {
19981998
th = th->next();
1999+
} else if (th->idx != -1) {
2000+
// sleeping threads interrupted by another vCPU are inserted to
2001+
// standby q without poping from sleeping q, they can not be stolen.
2002+
th = th->next();
2003+
// note that migrated threads are also inserted to standby q, but
2004+
// they are not in a sleeping q, so they are able to be stolen.
19992005
} else {
20002006
auto next = th->remove_from_list();
20012007
stolen.push_back(th); count++;
@@ -2145,7 +2151,13 @@ R"(
21452151
}
21462152
static int do_thread_migrate(thread* th, vcpu_base* vb) {
21472153
assert(vb != th->vcpu);
2148-
AtomicRunQ().remove_from_list(th);
2154+
AtomicRunQ arq;
2155+
SCOPED_LOCK(th->lock);
2156+
if (th->state != READY || th->vcpu != CURRENT->vcpu) {
2157+
LOG_ERROR_RETURN(EINVAL, -1,
2158+
"thread ` state changed during migrate", th)
2159+
}
2160+
arq.remove_from_list(th);
21492161
th->get_vcpu()->nthreads--;
21502162
th->state = STANDBY;
21512163
auto vcpu = (vcpu_t*)vb;

0 commit comments

Comments
 (0)