Skip to content

Commit 9e27a08

Browse files
[release-branch.go1.14] runtime: block signals in needm before allocating M
Otherwise, if a signal occurs just after we allocated the M, we can deadlock if the signal handler needs to allocate an M itself. For #42207 Fixes #42635 Change-Id: I76f44547f419e8b1c14cbf49bf602c6e645d8c14 Reviewed-on: https://go-review.googlesource.com/c/go/+/265759 Trust: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> (cherry picked from commit 368c401) Reviewed-on: https://go-review.googlesource.com/c/go/+/271848
1 parent 7177852 commit 9e27a08

File tree

7 files changed

+131
-17
lines changed

7 files changed

+131
-17
lines changed

src/runtime/crash_cgo_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,16 @@ func findTrace(text, top string) []string {
549549
}
550550
return nil
551551
}
552+
553+
// Issue #42207.
554+
func TestNeedmDeadlock(t *testing.T) {
555+
switch runtime.GOOS {
556+
case "plan9", "windows":
557+
t.Skipf("no signals on %s", runtime.GOOS)
558+
}
559+
output := runTestProg(t, "testprogcgo", "NeedmDeadlock")
560+
want := "OK\n"
561+
if output != want {
562+
t.Fatalf("want %s, got %s\n", want, output)
563+
}
564+
}

src/runtime/os_js.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func mpreinit(mp *m) {
5959
}
6060

6161
//go:nosplit
62-
func msigsave(mp *m) {
62+
func sigsave(p *sigset) {
6363
}
6464

6565
//go:nosplit

src/runtime/os_plan9.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func mpreinit(mp *m) {
167167
mp.errstr = (*byte)(mallocgc(_ERRMAX, nil, true))
168168
}
169169

170-
func msigsave(mp *m) {
170+
func sigsave(p *sigset) {
171171
}
172172

173173
func msigrestore(sigmask sigset) {

src/runtime/os_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ func mpreinit(mp *m) {
832832
}
833833

834834
//go:nosplit
835-
func msigsave(mp *m) {
835+
func sigsave(p *sigset) {
836836
}
837837

838838
//go:nosplit

src/runtime/proc.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func schedinit() {
551551
typelinksinit() // uses maps, activeModules
552552
itabsinit() // uses activeModules
553553

554-
msigsave(_g_.m)
554+
sigsave(&_g_.m.sigmask)
555555
initSigmask = _g_.m.sigmask
556556

557557
goargs()
@@ -1476,6 +1476,18 @@ func needm(x byte) {
14761476
exit(1)
14771477
}
14781478

1479+
// Save and block signals before getting an M.
1480+
// The signal handler may call needm itself,
1481+
// and we must avoid a deadlock. Also, once g is installed,
1482+
// any incoming signals will try to execute,
1483+
// but we won't have the sigaltstack settings and other data
1484+
// set up appropriately until the end of minit, which will
1485+
// unblock the signals. This is the same dance as when
1486+
// starting a new m to run Go code via newosproc.
1487+
var sigmask sigset
1488+
sigsave(&sigmask)
1489+
sigblock()
1490+
14791491
// Lock extra list, take head, unlock popped list.
14801492
// nilokay=false is safe here because of the invariant above,
14811493
// that the extra list always contains or will soon contain
@@ -1493,14 +1505,8 @@ func needm(x byte) {
14931505
extraMCount--
14941506
unlockextra(mp.schedlink.ptr())
14951507

1496-
// Save and block signals before installing g.
1497-
// Once g is installed, any incoming signals will try to execute,
1498-
// but we won't have the sigaltstack settings and other data
1499-
// set up appropriately until the end of minit, which will
1500-
// unblock the signals. This is the same dance as when
1501-
// starting a new m to run Go code via newosproc.
1502-
msigsave(mp)
1503-
sigblock()
1508+
// Store the original signal mask for use by minit.
1509+
mp.sigmask = sigmask
15041510

15051511
// Install g (= m->g0) and set the stack bounds
15061512
// to match the current stack. We don't actually know
@@ -3317,7 +3323,7 @@ func beforefork() {
33173323
// a signal handler before exec if a signal is sent to the process
33183324
// group. See issue #18600.
33193325
gp.m.locks++
3320-
msigsave(gp.m)
3326+
sigsave(&gp.m.sigmask)
33213327
sigblock()
33223328

33233329
// This function is called before fork in syscall package.

src/runtime/signal_unix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,15 +1007,15 @@ func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool {
10071007
return true
10081008
}
10091009

1010-
// msigsave saves the current thread's signal mask into mp.sigmask.
1010+
// sigsave saves the current thread's signal mask into *p.
10111011
// This is used to preserve the non-Go signal mask when a non-Go
10121012
// thread calls a Go function.
10131013
// This is nosplit and nowritebarrierrec because it is called by needm
10141014
// which may be called on a non-Go thread with no g available.
10151015
//go:nosplit
10161016
//go:nowritebarrierrec
1017-
func msigsave(mp *m) {
1018-
sigprocmask(_SIG_SETMASK, nil, &mp.sigmask)
1017+
func sigsave(p *sigset) {
1018+
sigprocmask(_SIG_SETMASK, nil, p)
10191019
}
10201020

10211021
// msigrestore sets the current thread's signal mask to sigmask.
@@ -1087,7 +1087,7 @@ func minitSignalStack() {
10871087
// thread's signal mask. When this is called all signals have been
10881088
// blocked for the thread. This starts with m.sigmask, which was set
10891089
// either from initSigmask for a newly created thread or by calling
1090-
// msigsave if this is a non-Go thread calling a Go function. It
1090+
// sigsave if this is a non-Go thread calling a Go function. It
10911091
// removes all essential signals from the mask, thus causing those
10921092
// signals to not be blocked. Then it sets the thread's signal mask.
10931093
// After this is called the thread can receive signals.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !plan9,!windows
6+
7+
package main
8+
9+
// This is for issue #42207.
10+
// During a call to needm we could get a SIGCHLD signal
11+
// which would itself call needm, causing a deadlock.
12+
13+
/*
14+
#include <signal.h>
15+
#include <pthread.h>
16+
#include <sched.h>
17+
#include <unistd.h>
18+
19+
extern void GoNeedM();
20+
21+
#define SIGNALERS 10
22+
23+
static void* needmSignalThread(void* p) {
24+
pthread_t* pt = (pthread_t*)(p);
25+
int i;
26+
27+
for (i = 0; i < 100; i++) {
28+
if (pthread_kill(*pt, SIGCHLD) < 0) {
29+
return NULL;
30+
}
31+
usleep(1);
32+
}
33+
return NULL;
34+
}
35+
36+
// We don't need many calls, as the deadlock is only likely
37+
// to occur the first couple of times that needm is called.
38+
// After that there will likely be an extra M available.
39+
#define CALLS 10
40+
41+
static void* needmCallbackThread(void* p) {
42+
int i;
43+
44+
for (i = 0; i < SIGNALERS; i++) {
45+
sched_yield(); // Help the signal threads get started.
46+
}
47+
for (i = 0; i < CALLS; i++) {
48+
GoNeedM();
49+
}
50+
return NULL;
51+
}
52+
53+
static void runNeedmSignalThread() {
54+
int i;
55+
pthread_t caller;
56+
pthread_t s[SIGNALERS];
57+
58+
pthread_create(&caller, NULL, needmCallbackThread, NULL);
59+
for (i = 0; i < SIGNALERS; i++) {
60+
pthread_create(&s[i], NULL, needmSignalThread, &caller);
61+
}
62+
for (i = 0; i < SIGNALERS; i++) {
63+
pthread_join(s[i], NULL);
64+
}
65+
pthread_join(caller, NULL);
66+
}
67+
*/
68+
import "C"
69+
70+
import (
71+
"fmt"
72+
"os"
73+
"time"
74+
)
75+
76+
func init() {
77+
register("NeedmDeadlock", NeedmDeadlock)
78+
}
79+
80+
//export GoNeedM
81+
func GoNeedM() {
82+
}
83+
84+
func NeedmDeadlock() {
85+
// The failure symptom is that the program hangs because of a
86+
// deadlock in needm, so set an alarm.
87+
go func() {
88+
time.Sleep(5 * time.Second)
89+
fmt.Println("Hung for 5 seconds")
90+
os.Exit(1)
91+
}()
92+
93+
C.runNeedmSignalThread()
94+
fmt.Println("OK")
95+
}

0 commit comments

Comments
 (0)