From 03ba6b070d625bf00eac5350c4f363e5e87828b2 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Wed, 15 Apr 2020 18:01:00 +0000 Subject: [PATCH] runtime: prevent preemption while releasing worldsema in gcStart Currently, as a result of us releasing worldsema now to allow STW events during a mark phase, we release worldsema between starting the world and having the goroutine block in STW mode. This inserts preemption points which, if followed through, could lead to a deadlock. Specifically, because user goroutine scheduling is disabled in STW mode, the goroutine will block before properly releasing worldsema. The fix here is to prevent preemption while releasing the worldsema. Fixes #38404. Updates #19812. Change-Id: I8ed5b3aa108ab2e4680c38e77b0584fb75690e3d Reviewed-on: https://go-review.googlesource.com/c/go/+/228337 Run-TryBot: Michael Knyszek TryBot-Result: Gobot Gobot Reviewed-by: Cherry Zhang Reviewed-by: Austin Clements --- src/runtime/mgc.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 08159e219a..58b76bca70 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -1373,6 +1373,10 @@ func gcStart(trigger gcTrigger) { // the world. gcController.markStartTime = now + // In STW mode, we could block the instant systemstack + // returns, so make sure we're not preemptible. + mp = acquirem() + // Concurrent mark. systemstack(func() { now = startTheWorldWithSema(trace.enabled) @@ -1385,10 +1389,10 @@ func gcStart(trigger gcTrigger) { // this goroutine becomes runnable again, and we could // self-deadlock otherwise. semrelease(&worldsema) + releasem(mp) - // In STW mode, we could block the instant systemstack - // returns, so don't do anything important here. Make sure we - // block rather than returning to user code. + // Make sure we block instead of returning to user code + // in STW mode. if mode != gcBackgroundMode { Gosched() } -- 2.50.0