Skip to content

Commit dd5db4d

Browse files
prattmicgopherbot
authored andcommitted
runtime: check global runq during "delicate dance"
When a thread transitions to spinning to non-spinning it must recheck all sources of work because other threads may submit new work but skip wakep because they see a spinning thread. However, since the beginning of time (CL 7314062) we do not check the global run queue, only the local per-P run queues. The global run queue is checked just above the spinning checks while dropping the P. I am unsure what the purpose of this check is. It appears to simply be opportunistic since sched.lock is already held there in order to drop the P. It is not sufficient to synchronize with threads adding work because it occurs before decrementing sched.nmspinning, which is what threads us to decide to wake a thread. Resolve this by adding an explicit global run queue check alongside the local per-P run queue checks. Almost nothing happens between dropped sched.lock after dropping the P and relocking sched.lock: just clearing mp.spinning and decrementing sched.nmspinning. Thus it may be better to just hold sched.lock for this entire period, but this is a larger change that I would prefer to avoid in the freeze and backports. For #55160. Change-Id: Ifd88b5a4c561c063cedcfcfe1dd8ae04202d9666 Reviewed-on: https://go-review.googlesource.com/c/go/+/501975 Run-TryBot: Michael Pratt <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Auto-Submit: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 884aa71 commit dd5db4d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/runtime/proc.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var modinfo string
8484
// semi-persistent CPU underutilization.
8585
//
8686
// The general pattern for submission is:
87-
// 1. Submit work to the local run queue, timer heap, or GC state.
87+
// 1. Submit work to the local or global run queue, timer heap, or GC state.
8888
// 2. #StoreLoad-style memory barrier.
8989
// 3. Check sched.nmspinning.
9090
//
@@ -3093,7 +3093,7 @@ top:
30933093
//
30943094
// This applies to the following sources of work:
30953095
//
3096-
// * Goroutines added to a per-P run queue.
3096+
// * Goroutines added to the global or a per-P run queue.
30973097
// * New/modified-earlier timers on a per-P timer heap.
30983098
// * Idle-priority GC work (barring golang.org/issue/19112).
30993099
//
@@ -3135,7 +3135,24 @@ top:
31353135
//
31363136
// See https://go.dev/issue/43997.
31373137

3138-
// Check all runqueues once again.
3138+
// Check global and P runqueues again.
3139+
3140+
lock(&sched.lock)
3141+
if sched.runqsize != 0 {
3142+
pp, _ := pidlegetSpinning(0)
3143+
if pp != nil {
3144+
gp := globrunqget(pp, 0)
3145+
if gp == nil {
3146+
throw("global runq empty with non-zero runqsize")
3147+
}
3148+
unlock(&sched.lock)
3149+
acquirep(pp)
3150+
mp.becomeSpinning()
3151+
return gp, false, false
3152+
}
3153+
}
3154+
unlock(&sched.lock)
3155+
31393156
pp := checkRunqsNoP(allpSnapshot, idlepMaskSnapshot)
31403157
if pp != nil {
31413158
acquirep(pp)

0 commit comments

Comments
 (0)