Skip to content

Commit 84b070f

Browse files
zx2c4andybons
authored andcommitted
[release-branch.go1.13] runtime: monitor for suspend/resume to kick timeouts
Starting in Windows 8, the wait functions don't take into account suspend time, even though the monotonic counters do. This results in timer buckets stalling on resume. Therefore, this commit makes it so that on resume, we return from the wait functions and recalculate the amount of time left to wait. This is a cherry pick of CL 191957 and its cleanup, CL 198417. Updates #31528 Fixes #34130 Change-Id: I0db02cc72188cb620954e87a0180e0a3c83f4a56 Reviewed-on: https://go-review.googlesource.com/c/go/+/193607 Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
1 parent 17a492f commit 84b070f

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

src/runtime/os_windows.go

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
//go:cgo_import_dynamic runtime._VirtualFree VirtualFree%3 "kernel32.dll"
5050
//go:cgo_import_dynamic runtime._VirtualQuery VirtualQuery%3 "kernel32.dll"
5151
//go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject%2 "kernel32.dll"
52+
//go:cgo_import_dynamic runtime._WaitForMultipleObjects WaitForMultipleObjects%4 "kernel32.dll"
5253
//go:cgo_import_dynamic runtime._WriteConsoleW WriteConsoleW%5 "kernel32.dll"
5354
//go:cgo_import_dynamic runtime._WriteFile WriteFile%5 "kernel32.dll"
5455

@@ -96,6 +97,7 @@ var (
9697
_VirtualFree,
9798
_VirtualQuery,
9899
_WaitForSingleObject,
100+
_WaitForMultipleObjects,
99101
_WriteConsoleW,
100102
_WriteFile,
101103
_ stdFunction
@@ -139,7 +141,8 @@ func tstart_stdcall(newm *m)
139141
func ctrlhandler()
140142

141143
type mOS struct {
142-
waitsema uintptr // semaphore for parking on locks
144+
waitsema uintptr // semaphore for parking on locks
145+
resumesema uintptr // semaphore to indicate suspend/resume
143146
}
144147

145148
//go:linkname os_sigpipe os.sigpipe
@@ -258,6 +261,40 @@ func loadOptionalSyscalls() {
258261
}
259262
}
260263

264+
func monitorSuspendResume() {
265+
const _DEVICE_NOTIFY_CALLBACK = 2
266+
type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct {
267+
callback uintptr
268+
context uintptr
269+
}
270+
271+
powrprof := windowsLoadSystemLib([]byte("powrprof.dll\000"))
272+
if powrprof == 0 {
273+
return // Running on Windows 7, where we don't need it anyway.
274+
}
275+
powerRegisterSuspendResumeNotification := windowsFindfunc(powrprof, []byte("PowerRegisterSuspendResumeNotification\000"))
276+
if powerRegisterSuspendResumeNotification == nil {
277+
return // Running on Windows 7, where we don't need it anyway.
278+
}
279+
var fn interface{} = func(context uintptr, changeType uint32, setting uintptr) uintptr {
280+
for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
281+
if mp.resumesema != 0 {
282+
stdcall1(_SetEvent, mp.resumesema)
283+
}
284+
}
285+
return 0
286+
}
287+
params := _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS{
288+
callback: compileCallback(*efaceOf(&fn), true),
289+
}
290+
handle := uintptr(0)
291+
if stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
292+
uintptr(unsafe.Pointer(&params)),
293+
uintptr(unsafe.Pointer(&handle))) != 0 {
294+
throw("PowerRegisterSuspendResumeNotification failure")
295+
}
296+
}
297+
261298
//go:nosplit
262299
func getLoadLibrary() uintptr {
263300
return uintptr(unsafe.Pointer(_LoadLibraryW))
@@ -488,6 +525,10 @@ func goenvs() {
488525
}
489526

490527
stdcall1(_FreeEnvironmentStringsW, uintptr(strings))
528+
529+
// We call this all the way here, late in init, so that malloc works
530+
// for the callback function this generates.
531+
monitorSuspendResume()
491532
}
492533

493534
// exiting is set to non-zero when the process is exiting.
@@ -606,19 +647,32 @@ func semasleep(ns int64) int32 {
606647
_WAIT_FAILED = 0xFFFFFFFF
607648
)
608649

609-
// store ms in ns to save stack space
650+
var result uintptr
610651
if ns < 0 {
611-
ns = _INFINITE
652+
result = stdcall2(_WaitForSingleObject, getg().m.waitsema, uintptr(_INFINITE))
612653
} else {
613-
ns = int64(timediv(ns, 1000000, nil))
614-
if ns == 0 {
615-
ns = 1
654+
start := nanotime()
655+
elapsed := int64(0)
656+
for {
657+
ms := int64(timediv(ns-elapsed, 1000000, nil))
658+
if ms == 0 {
659+
ms = 1
660+
}
661+
result = stdcall4(_WaitForMultipleObjects, 2,
662+
uintptr(unsafe.Pointer(&[2]uintptr{getg().m.waitsema, getg().m.resumesema})),
663+
0, uintptr(ms))
664+
if result != _WAIT_OBJECT_0+1 {
665+
// Not a suspend/resume event
666+
break
667+
}
668+
elapsed = nanotime() - start
669+
if elapsed >= ns {
670+
return -1
671+
}
616672
}
617673
}
618-
619-
result := stdcall2(_WaitForSingleObject, getg().m.waitsema, uintptr(ns))
620674
switch result {
621-
case _WAIT_OBJECT_0: //signaled
675+
case _WAIT_OBJECT_0: // Signaled
622676
return 0
623677

624678
case _WAIT_TIMEOUT:
@@ -667,6 +721,15 @@ func semacreate(mp *m) {
667721
throw("runtime.semacreate")
668722
})
669723
}
724+
mp.resumesema = stdcall4(_CreateEventA, 0, 0, 0, 0)
725+
if mp.resumesema == 0 {
726+
systemstack(func() {
727+
print("runtime: createevent failed; errno=", getlasterror(), "\n")
728+
throw("runtime.semacreate")
729+
})
730+
stdcall1(_CloseHandle, mp.waitsema)
731+
mp.waitsema = 0
732+
}
670733
}
671734

672735
// May run with m.p==nil, so write barriers are not allowed. This

src/runtime/syscall_windows.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ func compileCallback(fn eface, cleanstack bool) (code uintptr) {
7474
argsize += uintptrSize
7575
}
7676

77-
lock(&cbs.lock)
78-
defer unlock(&cbs.lock)
77+
lock(&cbs.lock) // We don't unlock this in a defer because this is used from the system stack.
7978

8079
n := cbs.n
8180
for i := 0; i < n; i++ {
8281
if cbs.ctxt[i].gobody == fn.data && cbs.ctxt[i].isCleanstack() == cleanstack {
83-
return callbackasmAddr(i)
82+
r := callbackasmAddr(i)
83+
unlock(&cbs.lock)
84+
return r
8485
}
8586
}
8687
if n >= cb_max {
88+
unlock(&cbs.lock)
8789
throw("too many callback functions")
8890
}
8991

@@ -99,7 +101,9 @@ func compileCallback(fn eface, cleanstack bool) (code uintptr) {
99101
cbs.ctxt[n] = c
100102
cbs.n++
101103

102-
return callbackasmAddr(n)
104+
r := callbackasmAddr(n)
105+
unlock(&cbs.lock)
106+
return r
103107
}
104108

105109
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800

0 commit comments

Comments
 (0)