Commit f3b5a2bc authored by Austin Clements's avatar Austin Clements

runtime: prevent descheduling while holding rwmutex read lock

Currently only the rwmutex write lock prevents descheduling. The read
lock does not. This leads to the following situation:

1. A reader acquires the lock and gets descheduled.

2. GOMAXPROCS writers attempt to acquire the lock (or at least one
writer does, followed by readers). This blocks all of the Ps.

3. There is no 3. The descheduled reader never gets to run again
because there are no Ps, so it never releases the lock and the system
deadlocks.

Fix this by preventing descheduling while holding the read lock. This
requires also rewriting TestParallelRWMutexReaders to always create
enough GOMAXPROCS and to use non-blocking operations for
synchronization.

Fixes #20903.

Change-Id: Ibd460663a7e5a555be5490e13b2eaaa295fac39f
Reviewed-on: https://go-review.googlesource.com/47632
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent bb3be403
...@@ -31,6 +31,11 @@ const rwmutexMaxReaders = 1 << 30 ...@@ -31,6 +31,11 @@ const rwmutexMaxReaders = 1 << 30
// rlock locks rw for reading. // rlock locks rw for reading.
func (rw *rwmutex) rlock() { func (rw *rwmutex) rlock() {
// The reader must not be allowed to lose its P or else other
// things blocking on the lock may consume all of the Ps and
// deadlock (issue #20903). Alternatively, we could drop the P
// while sleeping.
acquirem()
if int32(atomic.Xadd(&rw.readerCount, 1)) < 0 { if int32(atomic.Xadd(&rw.readerCount, 1)) < 0 {
// A writer is pending. Park on the reader queue. // A writer is pending. Park on the reader queue.
systemstack(func() { systemstack(func() {
...@@ -70,11 +75,12 @@ func (rw *rwmutex) runlock() { ...@@ -70,11 +75,12 @@ func (rw *rwmutex) runlock() {
unlock(&rw.rLock) unlock(&rw.rLock)
} }
} }
releasem(getg().m)
} }
// lock locks rw for writing. // lock locks rw for writing.
func (rw *rwmutex) lock() { func (rw *rwmutex) lock() {
// Resolve competition with other writers. // Resolve competition with other writers and stick to our P.
lock(&rw.wLock) lock(&rw.wLock)
m := getg().m m := getg().m
// Announce that there is a pending writer. // Announce that there is a pending writer.
......
...@@ -16,30 +16,29 @@ import ( ...@@ -16,30 +16,29 @@ import (
"testing" "testing"
) )
func parallelReader(m *RWMutex, clocked, cunlock, cdone chan bool) { func parallelReader(m *RWMutex, clocked chan bool, cunlock *uint32, cdone chan bool) {
m.RLock() m.RLock()
clocked <- true clocked <- true
<-cunlock for atomic.LoadUint32(cunlock) == 0 {
}
m.RUnlock() m.RUnlock()
cdone <- true cdone <- true
} }
func doTestParallelReaders(numReaders, gomaxprocs int) { func doTestParallelReaders(numReaders int) {
GOMAXPROCS(gomaxprocs) GOMAXPROCS(numReaders + 1)
var m RWMutex var m RWMutex
clocked := make(chan bool) clocked := make(chan bool, numReaders)
cunlock := make(chan bool) var cunlock uint32
cdone := make(chan bool) cdone := make(chan bool)
for i := 0; i < numReaders; i++ { for i := 0; i < numReaders; i++ {
go parallelReader(&m, clocked, cunlock, cdone) go parallelReader(&m, clocked, &cunlock, cdone)
} }
// Wait for all parallel RLock()s to succeed. // Wait for all parallel RLock()s to succeed.
for i := 0; i < numReaders; i++ { for i := 0; i < numReaders; i++ {
<-clocked <-clocked
} }
for i := 0; i < numReaders; i++ { atomic.StoreUint32(&cunlock, 1)
cunlock <- true
}
// Wait for the goroutines to finish. // Wait for the goroutines to finish.
for i := 0; i < numReaders; i++ { for i := 0; i < numReaders; i++ {
<-cdone <-cdone
...@@ -48,9 +47,9 @@ func doTestParallelReaders(numReaders, gomaxprocs int) { ...@@ -48,9 +47,9 @@ func doTestParallelReaders(numReaders, gomaxprocs int) {
func TestParallelRWMutexReaders(t *testing.T) { func TestParallelRWMutexReaders(t *testing.T) {
defer GOMAXPROCS(GOMAXPROCS(-1)) defer GOMAXPROCS(GOMAXPROCS(-1))
doTestParallelReaders(1, 4) doTestParallelReaders(1)
doTestParallelReaders(3, 4) doTestParallelReaders(3)
doTestParallelReaders(4, 2) doTestParallelReaders(4)
} }
func reader(rwm *RWMutex, num_iterations int, activity *int32, cdone chan bool) { func reader(rwm *RWMutex, num_iterations int, activity *int32, cdone chan bool) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment