]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: check global runq during "delicate dance"
authorMichael Pratt <mpratt@google.com>
Wed, 7 Jun 2023 15:38:26 +0000 (11:38 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 20 Jul 2023 21:39:53 +0000 (21:39 +0000)
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 <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/runtime/proc.go

index f4af26f172dd27bf344fd298c67628f418e0d3ad..68d20edf41203a328dca0e4caec09ab9a9bfa7b9 100644 (file)
@@ -84,7 +84,7 @@ var modinfo string
 // semi-persistent CPU underutilization.
 //
 // The general pattern for submission is:
-// 1. Submit work to the local run queue, timer heap, or GC state.
+// 1. Submit work to the local or global run queue, timer heap, or GC state.
 // 2. #StoreLoad-style memory barrier.
 // 3. Check sched.nmspinning.
 //
@@ -3093,7 +3093,7 @@ top:
        //
        // This applies to the following sources of work:
        //
-       // * Goroutines added to a per-P run queue.
+       // * Goroutines added to the global or a per-P run queue.
        // * New/modified-earlier timers on a per-P timer heap.
        // * Idle-priority GC work (barring golang.org/issue/19112).
        //
@@ -3135,7 +3135,24 @@ top:
                //
                // See https://go.dev/issue/43997.
 
-               // Check all runqueues once again.
+               // Check global and P runqueues again.
+
+               lock(&sched.lock)
+               if sched.runqsize != 0 {
+                       pp, _ := pidlegetSpinning(0)
+                       if pp != nil {
+                               gp := globrunqget(pp, 0)
+                               if gp == nil {
+                                       throw("global runq empty with non-zero runqsize")
+                               }
+                               unlock(&sched.lock)
+                               acquirep(pp)
+                               mp.becomeSpinning()
+                               return gp, false, false
+                       }
+               }
+               unlock(&sched.lock)
+
                pp := checkRunqsNoP(allpSnapshot, idlepMaskSnapshot)
                if pp != nil {
                        acquirep(pp)