Skip to content

Commit b8caed8

Browse files
committed
runtime: initialize extra M for cgo during mstart
Previously the extra m needed for cgo callbacks was created on the first callback. This works for cgo, however the cgocallback mechanism is also borrowed by badsignal which can run before any cgo calls are made. Now we initialize the extra M at runtime startup before any signal handlers are registered, so badsignal cannot be called until the extra M is ready. Updates #10207. Change-Id: Iddda2c80db6dc52d8b60e2b269670fbaa704c7b3 Reviewed-on: https://go-review.googlesource.com/7978 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: David Crawshaw <[email protected]>
1 parent 63f59b6 commit b8caed8

File tree

3 files changed

+5
-14
lines changed

3 files changed

+5
-14
lines changed

src/runtime/cgocall.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ func cgocall_errno(fn, arg unsafe.Pointer) int32 {
101101
racereleasemerge(unsafe.Pointer(&racecgosync))
102102
}
103103

104-
// Create an extra M for callbacks on threads not created by Go on first cgo call.
105-
if needextram == 1 && cas(&needextram, 1, 0) {
106-
systemstack(newextram)
107-
}
108-
109104
/*
110105
* Lock g to m to ensure we stay on the same stack if we do a
111106
* cgo callback. Add entry to defer stack in case of panic.

src/runtime/proc1.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,11 @@ func mstart1() {
717717
// Install signal handlers; after minit so that minit can
718718
// prepare the thread to be able to handle the signals.
719719
if _g_.m == &m0 {
720+
// Create an extra M for callbacks on threads not created by Go.
721+
if needextram == 1 {
722+
needextram = 0
723+
newextram()
724+
}
720725
initsig()
721726
}
722727

src/runtime/sigqueue.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,5 @@ func signal_ignore(s uint32) {
165165
// This runs on a foreign stack, without an m or a g. No stack split.
166166
//go:nosplit
167167
func badsignal(sig uintptr) {
168-
// Some external libraries, for example, OpenBLAS, create worker threads in
169-
// a global constructor. If we're doing cpu profiling, and the SIGPROF signal
170-
// comes to one of the foreign threads before we make our first cgo call, the
171-
// call to cgocallback below will bring down the whole process.
172-
// It's better to miss a few SIGPROF signals than to abort in this case.
173-
// See http://golang.org/issue/9456.
174-
if _SIGPROF != 0 && sig == _SIGPROF && needextram != 0 {
175-
return
176-
}
177168
cgocallback(unsafe.Pointer(funcPC(sigsend)), noescape(unsafe.Pointer(&sig)), unsafe.Sizeof(sig))
178169
}

0 commit comments

Comments
 (0)