Skip to content

Commit e0d029f

Browse files
committed
runtime: avoid gp.lockedm race in exitsyscall0
Following https://golang.org/cl/291329, exitsyscall0 accesses gp.lockedm after releasing gp to the global runq. This creates a race window where another M may schedule the (unlocked) G, which subsequently calls LockOSThread, setting gp.lockedm and thus causing exitsyscall0 to think it should call stoplockedm. Avoid this race by checking if gp is locked before releasing it to the global runq. Fixes #46524 Change-Id: I3acdaf09e7a2178725adbe61e985130e9ebd0680 Reviewed-on: https://go-review.googlesource.com/c/go/+/324350 Trust: Michael Pratt <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent dd7ba3b commit e0d029f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/runtime/proc.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4083,8 +4083,16 @@ func exitsyscall0(gp *g) {
40834083
if schedEnabled(gp) {
40844084
_p_ = pidleget()
40854085
}
4086+
var locked bool
40864087
if _p_ == nil {
40874088
globrunqput(gp)
4089+
4090+
// Below, we stoplockedm if gp is locked. globrunqput releases
4091+
// ownership of gp, so we must check if gp is locked prior to
4092+
// committing the release by unlocking sched.lock, otherwise we
4093+
// could race with another M transitioning gp from unlocked to
4094+
// locked.
4095+
locked = gp.lockedm != 0
40884096
} else if atomic.Load(&sched.sysmonwait) != 0 {
40894097
atomic.Store(&sched.sysmonwait, 0)
40904098
notewakeup(&sched.sysmonnote)
@@ -4094,7 +4102,7 @@ func exitsyscall0(gp *g) {
40944102
acquirep(_p_)
40954103
execute(gp, false) // Never returns.
40964104
}
4097-
if gp.lockedm != 0 {
4105+
if locked {
40984106
// Wait until another thread schedules gp and so m again.
40994107
//
41004108
// N.B. lockedm must be this M, as this g was running on this M

0 commit comments

Comments
 (0)