]> Cypherpunks repositories - gostls13.git/commit
runtime: ensure startm new M is consistently visible to checkdead
authorMichael Pratt <mpratt@google.com>
Mon, 27 Jul 2020 19:04:17 +0000 (15:04 -0400)
committerMichael Pratt <mpratt@google.com>
Tue, 28 Jul 2020 16:59:04 +0000 (16:59 +0000)
commit85afa2eb190d5d1a06584803bde4b4ee9b0e79b0
tree1ed8494c2f4d84deba043e9da13bf611bc417579
parentc4fed25553ee266ed9cb3a98e7a33a82af110ed4
runtime: ensure startm new M is consistently visible to checkdead

If no M is available, startm first grabs an idle P, then drops
sched.lock and calls newm to start a new M to run than P.

Unfortunately, that leaves a window in which a G (e.g., returning from a
syscall) may find no idle P, add to the global runq, and then in stopm
discover that there are no running M's, a condition that should be
impossible with runnable G's.

To avoid this condition, we pre-allocate the new M ID in startm before
dropping sched.lock. This ensures that checkdead will see the M as
running, and since that new M must eventually run the scheduler, it will
handle any pending work as necessary.

Outside of startm, most other calls to newm/allocm don't have a P at
all. The only exception is startTheWorldWithSema, which always has an M
if there is 1 P (i.e., the currently running M), and if there is >1 P
the findrunnable spinning dance ensures the problem never occurs.

This has been tested with strategically placed sleeps in the runtime to
help induce the correct race ordering, but the timing on this is too
narrow for a test that can be checked in.

Fixes #40368

Change-Id: If5e0293a430cc85154b7ed55bc6dadf9b340abe2
Reviewed-on: https://go-review.googlesource.com/c/go/+/245018
Run-TryBot: Michael Pratt <mpratt@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
src/runtime/proc.go