Skip to content

Commit f9640b8

Browse files
committed
runtime: incorporate Gscan acquire/release into lock ranking order
I added routines that can acquire/release a particular rank without acquiring/releasing an associated lock. I added lockRankGscan as a rank for acquiring/releasing the Gscan bit. castogscanstatus() and casGtoPreemptScan() are acquires of the Gscan bit. casfrom_Gscanstatus() is a release of the Gscan bit. casgstatus() is like an acquire and release of the Gscan bit, since it will wait if Gscan bit is currently set. We have a cycle between hchan and Gscan. The acquisition of Gscan and then hchan only happens in syncadjustsudogs() when the G is suspended, so the main normal ordering (get hchan, then get Gscan) can't be happening. So, I added a new rank lockRankHchanLeaf that is used when acquiring hchan locks in syncadjustsudogs. This ranking is set so no other locks can be acquired except other hchan locks. Fixes #38922 Change-Id: I58ce526a74ba856cb42078f7b9901f2832e1d45c Reviewed-on: https://go-review.googlesource.com/c/go/+/228417 Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 3321303 commit f9640b8

File tree

8 files changed

+129
-41
lines changed

8 files changed

+129
-41
lines changed

src/runtime/lock_futex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func lock2(l *mutex) {
108108
}
109109

110110
func unlock(l *mutex) {
111-
lockRankRelease(l)
111+
unlockWithRank(l)
112112
}
113113

114114
func unlock2(l *mutex) {

src/runtime/lock_js.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func lock2(l *mutex) {
4444
}
4545

4646
func unlock(l *mutex) {
47-
lockRankRelease(l)
47+
unlockWithRank(l)
4848
}
4949

5050
func unlock2(l *mutex) {

src/runtime/lock_sema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Loop:
9494
}
9595

9696
func unlock(l *mutex) {
97-
lockRankRelease(l)
97+
unlockWithRank(l)
9898
}
9999

100100
//go:nowritebarrier

src/runtime/lockrank.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const (
6969
lockRankMcentral // For !go115NewMCentralImpl
7070
lockRankSpine // For !go115NewMCentralImpl
7171
lockRankSpanSetSpine
72+
lockRankGscan
7273
lockRankStackpool
7374
lockRankStackLarge
7475
lockRankDefer
@@ -84,6 +85,14 @@ const (
8485

8586
// Other leaf locks
8687
lockRankGFree
88+
// Generally, hchan must be acquired before gscan. But in one specific
89+
// case (in syncadjustsudogs from markroot after the g has been suspended
90+
// by suspendG), we allow gscan to be acquired, and then an hchan lock. To
91+
// allow this case, we get this lockRankHchanLeaf rank in
92+
// syncadjustsudogs(), rather than lockRankHchan. By using this special
93+
// rank, we don't allow any further locks to be acquired other than more
94+
// hchan locks.
95+
lockRankHchanLeaf
8796

8897
// Leaf locks with no dependencies, so these constants are not actually used anywhere.
8998
// There are other architecture-dependent leaf locks as well.
@@ -141,6 +150,7 @@ var lockNames = []string{
141150
lockRankMcentral: "mcentral",
142151
lockRankSpine: "spine",
143152
lockRankSpanSetSpine: "spanSetSpine",
153+
lockRankGscan: "gscan",
144154
lockRankStackpool: "stackpool",
145155
lockRankStackLarge: "stackLarge",
146156
lockRankDefer: "defer",
@@ -152,7 +162,8 @@ var lockNames = []string{
152162

153163
lockRankGlobalAlloc: "globalAlloc.mutex",
154164

155-
lockRankGFree: "gFree",
165+
lockRankGFree: "gFree",
166+
lockRankHchanLeaf: "hchanLeaf",
156167

157168
lockRankNewmHandoff: "newmHandoff.lock",
158169
lockRankDebugPtrmask: "debugPtrmask.lock",
@@ -217,16 +228,18 @@ var lockPartialOrder [][]lockRank = [][]lockRank{
217228
lockRankMcentral: {lockRankScavenge, lockRankForcegc, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankAllg, lockRankAllp, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankHchan},
218229
lockRankSpine: {lockRankScavenge, lockRankAssistQueue, lockRankCpuprof, lockRankSched, lockRankAllg, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankHchan},
219230
lockRankSpanSetSpine: {lockRankScavenge, lockRankForcegc, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankAllg, lockRankAllp, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankHchan},
220-
lockRankStackpool: {lockRankScavenge, lockRankSweepWaiters, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankPollDesc, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankHchan, lockRankFin, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankProf, lockRankGcBitsArenas, lockRankRoot, lockRankTrace, lockRankTraceStackTab, lockRankNetpollInit, lockRankRwmutexR, lockRankMcentral, lockRankSpine, lockRankSpanSetSpine},
221-
lockRankStackLarge: {lockRankAssistQueue, lockRankSched, lockRankItab, lockRankHchan, lockRankProf, lockRankGcBitsArenas, lockRankRoot, lockRankMcentral, lockRankSpanSetSpine},
231+
lockRankGscan: {lockRankScavenge, lockRankForcegc, lockRankSweepWaiters, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankHchan, lockRankFin, lockRankTraceBuf, lockRankTraceStrings, lockRankRoot, lockRankNotifyList, lockRankProf, lockRankGcBitsArenas, lockRankTrace, lockRankTraceStackTab, lockRankNetpollInit, lockRankMcentral, lockRankSpine, lockRankSpanSetSpine},
232+
lockRankStackpool: {lockRankScavenge, lockRankSweepWaiters, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankPollDesc, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankHchan, lockRankFin, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankProf, lockRankGcBitsArenas, lockRankRoot, lockRankTrace, lockRankTraceStackTab, lockRankNetpollInit, lockRankRwmutexR, lockRankMcentral, lockRankSpine, lockRankSpanSetSpine, lockRankGscan},
233+
lockRankStackLarge: {lockRankAssistQueue, lockRankSched, lockRankItab, lockRankHchan, lockRankProf, lockRankGcBitsArenas, lockRankRoot, lockRankMcentral, lockRankSpanSetSpine, lockRankGscan},
222234
lockRankDefer: {},
223235
lockRankSudog: {lockRankNotifyList, lockRankHchan},
224-
lockRankWbufSpans: {lockRankScavenge, lockRankSweepWaiters, lockRankAssistQueue, lockRankSweep, lockRankSched, lockRankAllg, lockRankPollDesc, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankHchan, lockRankNotifyList, lockRankTraceStrings, lockRankMspanSpecial, lockRankProf, lockRankRoot, lockRankDefer, lockRankSudog},
225-
lockRankMheap: {lockRankScavenge, lockRankSweepWaiters, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankAllg, lockRankAllp, lockRankPollDesc, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankHchan, lockRankMspanSpecial, lockRankProf, lockRankGcBitsArenas, lockRankRoot, lockRankMcentral, lockRankStackpool, lockRankStackLarge, lockRankDefer, lockRankSudog, lockRankWbufSpans, lockRankSpanSetSpine},
236+
lockRankWbufSpans: {lockRankScavenge, lockRankSweepWaiters, lockRankAssistQueue, lockRankSweep, lockRankSched, lockRankAllg, lockRankPollDesc, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankHchan, lockRankNotifyList, lockRankTraceStrings, lockRankMspanSpecial, lockRankProf, lockRankRoot, lockRankGscan, lockRankDefer, lockRankSudog},
237+
lockRankMheap: {lockRankScavenge, lockRankSweepWaiters, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankAllg, lockRankAllp, lockRankPollDesc, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankHchan, lockRankMspanSpecial, lockRankProf, lockRankGcBitsArenas, lockRankRoot, lockRankMcentral, lockRankGscan, lockRankStackpool, lockRankStackLarge, lockRankDefer, lockRankSudog, lockRankWbufSpans, lockRankSpanSetSpine},
226238
lockRankMheapSpecial: {lockRankScavenge, lockRankAssistQueue, lockRankCpuprof, lockRankSweep, lockRankSched, lockRankAllg, lockRankAllp, lockRankTimers, lockRankItab, lockRankReflectOffs, lockRankNotifyList, lockRankTraceBuf, lockRankTraceStrings, lockRankHchan},
227239
lockRankGlobalAlloc: {lockRankProf, lockRankSpine, lockRankSpanSetSpine, lockRankMheap, lockRankMheapSpecial},
228240

229-
lockRankGFree: {lockRankSched},
241+
lockRankGFree: {lockRankSched},
242+
lockRankHchanLeaf: {lockRankGscan, lockRankHchanLeaf},
230243

231244
lockRankNewmHandoff: {},
232245
lockRankDebugPtrmask: {},

src/runtime/lockrank_off.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ func getLockRank(l *mutex) lockRank {
1414
return 0
1515
}
1616

17-
func lockRankRelease(l *mutex) {
17+
func lockWithRank(l *mutex, rank lockRank) {
18+
lock2(l)
19+
}
20+
21+
func acquireLockRank(rank lockRank) {
22+
}
23+
24+
func unlockWithRank(l *mutex) {
1825
unlock2(l)
1926
}
2027

21-
func lockWithRank(l *mutex, rank lockRank) {
22-
lock2(l)
28+
func releaseLockRank(rank lockRank) {
2329
}
2430

2531
func lockWithRankMayAcquire(l *mutex, rank lockRank) {

src/runtime/lockrank_on.go

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ func getLockRank(l *mutex) lockRank {
4646
// when acquiring a non-static lock.
4747
//go:nosplit
4848
func lockWithRank(l *mutex, rank lockRank) {
49-
if l == &debuglock {
50-
// debuglock is only used for println/printlock(). Don't do lock rank
51-
// recording for it, since print/println are used when printing
52-
// out a lock ordering problem below.
49+
if l == &debuglock || l == &paniclk {
50+
// debuglock is only used for println/printlock(). Don't do lock
51+
// rank recording for it, since print/println are used when
52+
// printing out a lock ordering problem below.
53+
//
54+
// paniclk has an ordering problem, since it can be acquired
55+
// during a panic with any other locks held (especially if the
56+
// panic is because of a directed segv), and yet also allg is
57+
// acquired after paniclk in tracebackothers()). This is a genuine
58+
// problem, so for now we don't do lock rank recording for paniclk
59+
// either.
5360
lock2(l)
5461
return
5562
}
@@ -75,26 +82,49 @@ func lockWithRank(l *mutex, rank lockRank) {
7582
})
7683
}
7784

85+
// acquireLockRank acquires a rank which is not associated with a mutex lock
86+
//go:nosplit
87+
func acquireLockRank(rank lockRank) {
88+
gp := getg()
89+
// Log the new class.
90+
systemstack(func() {
91+
i := gp.m.locksHeldLen
92+
if i >= len(gp.m.locksHeld) {
93+
throw("too many locks held concurrently for rank checking")
94+
}
95+
gp.m.locksHeld[i].rank = rank
96+
gp.m.locksHeld[i].lockAddr = 0
97+
gp.m.locksHeldLen++
98+
99+
// i is the index of the lock being acquired
100+
if i > 0 {
101+
checkRanks(gp, gp.m.locksHeld[i-1].rank, rank)
102+
}
103+
})
104+
}
105+
106+
// checkRanks checks if goroutine g, which has mostly recently acquired a lock
107+
// with rank 'prevRank', can now acquire a lock with rank 'rank'.
78108
func checkRanks(gp *g, prevRank, rank lockRank) {
79109
rankOK := false
80-
// If rank < prevRank, then we definitely have a rank error
81-
if prevRank <= rank {
82-
if rank == lockRankLeafRank {
83-
// If new lock is a leaf lock, then the preceding lock can
84-
// be anything except another leaf lock.
85-
rankOK = prevRank < lockRankLeafRank
86-
} else {
87-
// We've already verified the total lock ranking, but we
88-
// also enforce the partial ordering specified by
89-
// lockPartialOrder as well. Two locks with the same rank
90-
// can only be acquired at the same time if explicitly
91-
// listed in the lockPartialOrder table.
92-
list := lockPartialOrder[rank]
93-
for _, entry := range list {
94-
if entry == prevRank {
95-
rankOK = true
96-
break
97-
}
110+
if rank < prevRank {
111+
// If rank < prevRank, then we definitely have a rank error
112+
rankOK = false
113+
} else if rank == lockRankLeafRank {
114+
// If new lock is a leaf lock, then the preceding lock can
115+
// be anything except another leaf lock.
116+
rankOK = prevRank < lockRankLeafRank
117+
} else {
118+
// We've now verified the total lock ranking, but we
119+
// also enforce the partial ordering specified by
120+
// lockPartialOrder as well. Two locks with the same rank
121+
// can only be acquired at the same time if explicitly
122+
// listed in the lockPartialOrder table.
123+
list := lockPartialOrder[rank]
124+
for _, entry := range list {
125+
if entry == prevRank {
126+
rankOK = true
127+
break
98128
}
99129
}
100130
}
@@ -109,11 +139,9 @@ func checkRanks(gp *g, prevRank, rank lockRank) {
109139
}
110140

111141
//go:nosplit
112-
func lockRankRelease(l *mutex) {
113-
if l == &debuglock {
114-
// debuglock is only used for print/println. Don't do lock rank
115-
// recording for it, since print/println are used when printing
116-
// out a lock ordering problem below.
142+
func unlockWithRank(l *mutex) {
143+
if l == &debuglock || l == &paniclk {
144+
// See comment at beginning of lockWithRank.
117145
unlock2(l)
118146
return
119147
}
@@ -125,6 +153,7 @@ func lockRankRelease(l *mutex) {
125153
found = true
126154
copy(gp.m.locksHeld[i:gp.m.locksHeldLen-1], gp.m.locksHeld[i+1:gp.m.locksHeldLen])
127155
gp.m.locksHeldLen--
156+
break
128157
}
129158
}
130159
if !found {
@@ -135,6 +164,27 @@ func lockRankRelease(l *mutex) {
135164
})
136165
}
137166

167+
// releaseLockRank releases a rank which is not associated with a mutex lock
168+
//go:nosplit
169+
func releaseLockRank(rank lockRank) {
170+
gp := getg()
171+
systemstack(func() {
172+
found := false
173+
for i := gp.m.locksHeldLen - 1; i >= 0; i-- {
174+
if gp.m.locksHeld[i].rank == rank && gp.m.locksHeld[i].lockAddr == 0 {
175+
found = true
176+
copy(gp.m.locksHeld[i:gp.m.locksHeldLen-1], gp.m.locksHeld[i+1:gp.m.locksHeldLen])
177+
gp.m.locksHeldLen--
178+
break
179+
}
180+
}
181+
if !found {
182+
println(gp.m.procid, ":", rank.String(), rank)
183+
throw("lockRank release without matching lockRank acquire")
184+
}
185+
})
186+
}
187+
138188
//go:nosplit
139189
func lockWithRankMayAcquire(l *mutex, rank lockRank) {
140190
gp := getg()

src/runtime/proc.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ func casfrom_Gscanstatus(gp *g, oldval, newval uint32) {
760760
dumpgstatus(gp)
761761
throw("casfrom_Gscanstatus: gp->status is not in scan state")
762762
}
763+
releaseLockRank(lockRankGscan)
763764
}
764765

765766
// This will return false if the gp is not in the expected status and the cas fails.
@@ -771,7 +772,12 @@ func castogscanstatus(gp *g, oldval, newval uint32) bool {
771772
_Gwaiting,
772773
_Gsyscall:
773774
if newval == oldval|_Gscan {
774-
return atomic.Cas(&gp.atomicstatus, oldval, newval)
775+
r := atomic.Cas(&gp.atomicstatus, oldval, newval)
776+
if r {
777+
acquireLockRank(lockRankGscan)
778+
}
779+
return r
780+
775781
}
776782
}
777783
print("runtime: castogscanstatus oldval=", hex(oldval), " newval=", hex(newval), "\n")
@@ -792,6 +798,9 @@ func casgstatus(gp *g, oldval, newval uint32) {
792798
})
793799
}
794800

801+
acquireLockRank(lockRankGscan)
802+
releaseLockRank(lockRankGscan)
803+
795804
// See https://golang.org/cl/21503 for justification of the yield delay.
796805
const yieldDelay = 5 * 1000
797806
var nextYield int64
@@ -842,6 +851,7 @@ func casGToPreemptScan(gp *g, old, new uint32) {
842851
if old != _Grunning || new != _Gscan|_Gpreempted {
843852
throw("bad g transition")
844853
}
854+
acquireLockRank(lockRankGscan)
845855
for !atomic.Cas(&gp.atomicstatus, _Grunning, _Gscan|_Gpreempted) {
846856
}
847857
}

src/runtime/stack.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,16 @@ func syncadjustsudogs(gp *g, used uintptr, adjinfo *adjustinfo) uintptr {
794794
var lastc *hchan
795795
for sg := gp.waiting; sg != nil; sg = sg.waitlink {
796796
if sg.c != lastc {
797-
lock(&sg.c.lock)
797+
// There is a ranking cycle here between gscan bit and
798+
// hchan locks. Normally, we only allow acquiring hchan
799+
// locks and then getting a gscan bit. In this case, we
800+
// already have the gscan bit. We allow acquiring hchan
801+
// locks here as a special case, since a deadlock can't
802+
// happen because the G involved must already be
803+
// suspended. So, we get a special hchan lock rank here
804+
// that is lower than gscan, but doesn't allow acquiring
805+
// any other locks other than hchan.
806+
lockWithRank(&sg.c.lock, lockRankHchanLeaf)
798807
}
799808
lastc = sg.c
800809
}

0 commit comments

Comments
 (0)