Skip to content

Commit aba3a15

Browse files
rscjproberts
authored andcommitted
runtime: fix net poll races
The netpoll code was written long ago, when the only multiprocessors that Go ran on were x86. It assumed that an atomic store would trigger a full memory barrier and then used that barrier to order otherwise racy access to a handful of fields, including pollDesc.closing. On ARM64, this code has finally failed, because the atomic store is on a value completely unrelated to any of the racily-accessed fields, and the ARMv8 hardware, unlike x86, is clever enough not to do a full memory barrier for a simple atomic store. We are seeing a constant background rate of trybot failures where the net/http tests deadlock - a netpollblock has clearly happened after the pollDesc has begun to close. The code that does the racy reads is netpollcheckerr, which needs to be able to run without acquiring a lock. This CL fixes the race, without introducing unnecessary inefficiency or deadlock, by arranging for every updater of the relevant fields to publish a summary as a single atomic uint32, and then having netpollcheckerr use a single atomic load to fetch the relevant bits and then proceed as before. Fixes golang#45211 (until proven otherwise!). Change-Id: Ib6788c8da4d00b7bda84d55ca3fdffb5a64c1a0a Reviewed-on: https://go-review.googlesource.com/c/go/+/378234 Trust: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> Trust: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent aba8225 commit aba3a15

File tree

6 files changed

+119
-57
lines changed

6 files changed

+119
-57
lines changed

src/runtime/netpoll.go

Lines changed: 113 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,99 @@ const pollBlockSize = 4 * 1024
7171
//go:notinheap
7272
type pollDesc struct {
7373
link *pollDesc // in pollcache, protected by pollcache.lock
74+
fd uintptr // constant for pollDesc usage lifetime
75+
76+
// atomicInfo holds bits from closing, rd, and wd,
77+
// which are only ever written while holding the lock,
78+
// summarized for use by netpollcheckerr,
79+
// which cannot acquire the lock.
80+
// After writing these fields under lock in a way that
81+
// might change the summary, code must call publishInfo
82+
// before releasing the lock.
83+
// Code that changes fields and then calls netpollunblock
84+
// (while still holding the lock) must call publishInfo
85+
// before calling netpollunblock, because publishInfo is what
86+
// stops netpollblock from blocking anew
87+
// (by changing the result of netpollcheckerr).
88+
// atomicInfo also holds the eventErr bit,
89+
// recording whether a poll event on the fd got an error;
90+
// atomicInfo is the only source of truth for that bit.
91+
atomicInfo atomic.Uint32 // atomic pollInfo
92+
93+
// rg, wg are accessed atomically and hold g pointers.
94+
// (Using atomic.Uintptr here is similar to using guintptr elsewhere.)
95+
rg atomic.Uintptr // pdReady, pdWait, G waiting for read or nil
96+
wg atomic.Uintptr // pdReady, pdWait, G waiting for write or nil
7497

75-
// The lock protects pollOpen, pollSetDeadline, pollUnblock and deadlineimpl operations.
76-
// This fully covers seq, rt and wt variables. fd is constant throughout the PollDesc lifetime.
77-
// pollReset, pollWait, pollWaitCanceled and runtime·netpollready (IO readiness notification)
78-
// proceed w/o taking the lock. So closing, everr, rg, rd, wg and wd are manipulated
79-
// in a lock-free way by all operations.
80-
// TODO(golang.org/issue/49008): audit these lock-free fields for continued correctness.
81-
// NOTE(dvyukov): the following code uses uintptr to store *g (rg/wg),
82-
// that will blow up when GC starts moving objects.
8398
lock mutex // protects the following fields
84-
fd uintptr
8599
closing bool
86-
everr bool // marks event scanning error happened
87100
user uint32 // user settable cookie
88101
rseq uintptr // protects from stale read timers
89-
rg uintptr // pdReady, pdWait, G waiting for read or nil. Accessed atomically.
90102
rt timer // read deadline timer (set if rt.f != nil)
91-
rd int64 // read deadline
103+
rd int64 // read deadline (a nanotime in the future, -1 when expired)
92104
wseq uintptr // protects from stale write timers
93-
wg uintptr // pdReady, pdWait, G waiting for write or nil. Accessed atomically.
94105
wt timer // write deadline timer
95-
wd int64 // write deadline
106+
wd int64 // write deadline (a nanotime in the future, -1 when expired)
96107
self *pollDesc // storage for indirect interface. See (*pollDesc).makeArg.
97108
}
98109

110+
// pollInfo is the bits needed by netpollcheckerr, stored atomically,
111+
// mostly duplicating state that is manipulated under lock in pollDesc.
112+
// The one exception is the pollEventErr bit, which is maintained only
113+
// in the pollInfo.
114+
type pollInfo uint32
115+
116+
const (
117+
pollClosing = 1 << iota
118+
pollEventErr
119+
pollExpiredReadDeadline
120+
pollExpiredWriteDeadline
121+
)
122+
123+
func (i pollInfo) closing() bool { return i&pollClosing != 0 }
124+
func (i pollInfo) eventErr() bool { return i&pollEventErr != 0 }
125+
func (i pollInfo) expiredReadDeadline() bool { return i&pollExpiredReadDeadline != 0 }
126+
func (i pollInfo) expiredWriteDeadline() bool { return i&pollExpiredWriteDeadline != 0 }
127+
128+
// info returns the pollInfo corresponding to pd.
129+
func (pd *pollDesc) info() pollInfo {
130+
return pollInfo(pd.atomicInfo.Load())
131+
}
132+
133+
// publishInfo updates pd.atomicInfo (returned by pd.info)
134+
// using the other values in pd.
135+
// It must be called while holding pd.lock,
136+
// and it must be called after changing anything
137+
// that might affect the info bits.
138+
// In practice this means after changing closing
139+
// or changing rd or wd from < 0 to >= 0.
140+
func (pd *pollDesc) publishInfo() {
141+
var info uint32
142+
if pd.closing {
143+
info |= pollClosing
144+
}
145+
if pd.rd < 0 {
146+
info |= pollExpiredReadDeadline
147+
}
148+
if pd.wd < 0 {
149+
info |= pollExpiredWriteDeadline
150+
}
151+
152+
// Set all of x except the pollEventErr bit.
153+
x := pd.atomicInfo.Load()
154+
for !pd.atomicInfo.CompareAndSwap(x, (x&pollEventErr)|info) {
155+
x = pd.atomicInfo.Load()
156+
}
157+
}
158+
159+
// setEventErr sets the result of pd.info().eventErr() to b.
160+
func (pd *pollDesc) setEventErr(b bool) {
161+
x := pd.atomicInfo.Load()
162+
for (x&pollEventErr != 0) != b && !pd.atomicInfo.CompareAndSwap(x, x^pollEventErr) {
163+
x = pd.atomicInfo.Load()
164+
}
165+
}
166+
99167
type pollCache struct {
100168
lock mutex
101169
first *pollDesc
@@ -147,24 +215,25 @@ func poll_runtime_isPollServerDescriptor(fd uintptr) bool {
147215
func poll_runtime_pollOpen(fd uintptr) (*pollDesc, int) {
148216
pd := pollcache.alloc()
149217
lock(&pd.lock)
150-
wg := atomic.Loaduintptr(&pd.wg)
218+
wg := pd.wg.Load()
151219
if wg != 0 && wg != pdReady {
152220
throw("runtime: blocked write on free polldesc")
153221
}
154-
rg := atomic.Loaduintptr(&pd.rg)
222+
rg := pd.rg.Load()
155223
if rg != 0 && rg != pdReady {
156224
throw("runtime: blocked read on free polldesc")
157225
}
158226
pd.fd = fd
159227
pd.closing = false
160-
pd.everr = false
228+
pd.setEventErr(false)
161229
pd.rseq++
162-
atomic.Storeuintptr(&pd.rg, 0)
230+
pd.rg.Store(0)
163231
pd.rd = 0
164232
pd.wseq++
165-
atomic.Storeuintptr(&pd.wg, 0)
233+
pd.wg.Store(0)
166234
pd.wd = 0
167235
pd.self = pd
236+
pd.publishInfo()
168237
unlock(&pd.lock)
169238

170239
errno := netpollopen(fd, pd)
@@ -180,11 +249,11 @@ func poll_runtime_pollClose(pd *pollDesc) {
180249
if !pd.closing {
181250
throw("runtime: close polldesc w/o unblock")
182251
}
183-
wg := atomic.Loaduintptr(&pd.wg)
252+
wg := pd.wg.Load()
184253
if wg != 0 && wg != pdReady {
185254
throw("runtime: blocked write on closing polldesc")
186255
}
187-
rg := atomic.Loaduintptr(&pd.rg)
256+
rg := pd.rg.Load()
188257
if rg != 0 && rg != pdReady {
189258
throw("runtime: blocked read on closing polldesc")
190259
}
@@ -209,9 +278,9 @@ func poll_runtime_pollReset(pd *pollDesc, mode int) int {
209278
return errcode
210279
}
211280
if mode == 'r' {
212-
atomic.Storeuintptr(&pd.rg, 0)
281+
pd.rg.Store(0)
213282
} else if mode == 'w' {
214-
atomic.Storeuintptr(&pd.wg, 0)
283+
pd.wg.Store(0)
215284
}
216285
return pollNoError
217286
}
@@ -273,6 +342,7 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
273342
if mode == 'w' || mode == 'r'+'w' {
274343
pd.wd = d
275344
}
345+
pd.publishInfo()
276346
combo := pd.rd > 0 && pd.rd == pd.wd
277347
rtf := netpollReadDeadline
278348
if combo {
@@ -314,15 +384,13 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
314384
}
315385
}
316386
// If we set the new deadline in the past, unblock currently pending IO if any.
387+
// Note that pd.publishInfo has already been called, above, immediately after modifying rd and wd.
317388
var rg, wg *g
318-
if pd.rd < 0 || pd.wd < 0 {
319-
atomic.StorepNoWB(noescape(unsafe.Pointer(&wg)), nil) // full memory barrier between stores to rd/wd and load of rg/wg in netpollunblock
320-
if pd.rd < 0 {
321-
rg = netpollunblock(pd, 'r', false)
322-
}
323-
if pd.wd < 0 {
324-
wg = netpollunblock(pd, 'w', false)
325-
}
389+
if pd.rd < 0 {
390+
rg = netpollunblock(pd, 'r', false)
391+
}
392+
if pd.wd < 0 {
393+
wg = netpollunblock(pd, 'w', false)
326394
}
327395
unlock(&pd.lock)
328396
if rg != nil {
@@ -343,7 +411,7 @@ func poll_runtime_pollUnblock(pd *pollDesc) {
343411
pd.rseq++
344412
pd.wseq++
345413
var rg, wg *g
346-
atomic.StorepNoWB(noescape(unsafe.Pointer(&rg)), nil) // full memory barrier between store to closing and read of rg/wg in netpollunblock
414+
pd.publishInfo()
347415
rg = netpollunblock(pd, 'r', false)
348416
wg = netpollunblock(pd, 'w', false)
349417
if pd.rt.f != nil {
@@ -388,16 +456,17 @@ func netpollready(toRun *gList, pd *pollDesc, mode int32) {
388456
}
389457

390458
func netpollcheckerr(pd *pollDesc, mode int32) int {
391-
if pd.closing {
459+
info := pd.info()
460+
if info.closing() {
392461
return pollErrClosing
393462
}
394-
if (mode == 'r' && pd.rd < 0) || (mode == 'w' && pd.wd < 0) {
463+
if (mode == 'r' && info.expiredReadDeadline()) || (mode == 'w' && info.expiredWriteDeadline()) {
395464
return pollErrTimeout
396465
}
397466
// Report an event scanning error only on a read event.
398467
// An error on a write event will be captured in a subsequent
399468
// write call that is able to report a more specific error.
400-
if mode == 'r' && pd.everr {
469+
if mode == 'r' && info.eventErr() {
401470
return pollErrNotPollable
402471
}
403472
return pollNoError
@@ -432,28 +501,28 @@ func netpollblock(pd *pollDesc, mode int32, waitio bool) bool {
432501
// set the gpp semaphore to pdWait
433502
for {
434503
// Consume notification if already ready.
435-
if atomic.Casuintptr(gpp, pdReady, 0) {
504+
if gpp.CompareAndSwap(pdReady, 0) {
436505
return true
437506
}
438-
if atomic.Casuintptr(gpp, 0, pdWait) {
507+
if gpp.CompareAndSwap(0, pdWait) {
439508
break
440509
}
441510

442511
// Double check that this isn't corrupt; otherwise we'd loop
443512
// forever.
444-
if v := atomic.Loaduintptr(gpp); v != pdReady && v != 0 {
513+
if v := gpp.Load(); v != pdReady && v != 0 {
445514
throw("runtime: double wait")
446515
}
447516
}
448517

449518
// need to recheck error states after setting gpp to pdWait
450519
// this is necessary because runtime_pollUnblock/runtime_pollSetDeadline/deadlineimpl
451-
// do the opposite: store to closing/rd/wd, membarrier, load of rg/wg
520+
// do the opposite: store to closing/rd/wd, publishInfo, load of rg/wg
452521
if waitio || netpollcheckerr(pd, mode) == pollNoError {
453522
gopark(netpollblockcommit, unsafe.Pointer(gpp), waitReasonIOWait, traceEvGoBlockNet, 5)
454523
}
455524
// be careful to not lose concurrent pdReady notification
456-
old := atomic.Xchguintptr(gpp, 0)
525+
old := gpp.Swap(0)
457526
if old > pdWait {
458527
throw("runtime: corrupted polldesc")
459528
}
@@ -467,7 +536,7 @@ func netpollunblock(pd *pollDesc, mode int32, ioready bool) *g {
467536
}
468537

469538
for {
470-
old := atomic.Loaduintptr(gpp)
539+
old := gpp.Load()
471540
if old == pdReady {
472541
return nil
473542
}
@@ -480,7 +549,7 @@ func netpollunblock(pd *pollDesc, mode int32, ioready bool) *g {
480549
if ioready {
481550
new = pdReady
482551
}
483-
if atomic.Casuintptr(gpp, old, new) {
552+
if gpp.CompareAndSwap(old, new) {
484553
if old == pdWait {
485554
old = 0
486555
}
@@ -508,7 +577,7 @@ func netpolldeadlineimpl(pd *pollDesc, seq uintptr, read, write bool) {
508577
throw("runtime: inconsistent read deadline")
509578
}
510579
pd.rd = -1
511-
atomic.StorepNoWB(unsafe.Pointer(&pd.rt.f), nil) // full memory barrier between store to rd and load of rg in netpollunblock
580+
pd.publishInfo()
512581
rg = netpollunblock(pd, 'r', false)
513582
}
514583
var wg *g
@@ -517,7 +586,7 @@ func netpolldeadlineimpl(pd *pollDesc, seq uintptr, read, write bool) {
517586
throw("runtime: inconsistent write deadline")
518587
}
519588
pd.wd = -1
520-
atomic.StorepNoWB(unsafe.Pointer(&pd.wt.f), nil) // full memory barrier between store to wd and load of wg in netpollunblock
589+
pd.publishInfo()
521590
wg = netpollunblock(pd, 'w', false)
522591
}
523592
unlock(&pd.lock)

src/runtime/netpoll_aix.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,7 @@ retry:
212212
pfd.events &= ^_POLLOUT
213213
}
214214
if mode != 0 {
215-
pds[i].everr = false
216-
if pfd.revents == _POLLERR {
217-
pds[i].everr = true
218-
}
215+
pds[i].setEventErr(pfd.revents == _POLLERR)
219216
netpollready(&toRun, pds[i], mode)
220217
n--
221218
}

src/runtime/netpoll_epoll.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,7 @@ retry:
168168
}
169169
if mode != 0 {
170170
pd := *(**pollDesc)(unsafe.Pointer(&ev.data))
171-
pd.everr = false
172-
if ev.events == _EPOLLERR {
173-
pd.everr = true
174-
}
171+
pd.setEventErr(ev.events == _EPOLLERR)
175172
netpollready(&toRun, pd, mode)
176173
}
177174
}

src/runtime/netpoll_kqueue.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ retry:
179179
}
180180
if mode != 0 {
181181
pd := (*pollDesc)(unsafe.Pointer(ev.udata))
182-
pd.everr = false
183-
if ev.flags == _EV_ERROR {
184-
pd.everr = true
185-
}
182+
pd.setEventErr(ev.flags == _EV_ERROR)
186183
netpollready(&toRun, pd, mode)
187184
}
188185
}

src/runtime/netpoll_solaris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func netpollclose(fd uintptr) int32 {
158158
// this call, port_getn will return one and only one event for that
159159
// particular descriptor, so this function needs to be called again.
160160
func netpollupdate(pd *pollDesc, set, clear uint32) {
161-
if pd.closing {
161+
if pd.info().closing() {
162162
return
163163
}
164164

src/runtime/runtime2.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ func efaceOf(ep *any) *eface {
255255
// so I can't see them ever moving. If we did want to start moving data
256256
// in the GC, we'd need to allocate the goroutine structs from an
257257
// alternate arena. Using guintptr doesn't make that problem any worse.
258+
// Note that pollDesc.rg, pollDesc.wg also store g in uintptr form,
259+
// so they would need to be updated too if g's start moving.
258260
type guintptr uintptr
259261

260262
//go:nosplit

0 commit comments

Comments
 (0)