Skip to content

Commit 35c7234

Browse files
committed
runtime: add always-preempt maymorestack hook
This adds a maymorestack hook that forces a preemption at every possible cooperative preemption point. This would have helped us catch several recent preemption-related bugs earlier, including #47302, #47304, and #47441. For #48297. Change-Id: Ib82c973589c8a7223900e1842913b8591938fb9f Reviewed-on: https://go-review.googlesource.com/c/go/+/359796 Trust: Austin Clements <[email protected]> Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 3839b60 commit 35c7234

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/runtime/debug.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,37 @@ func NumGoroutine() int {
6161
func debug_modinfo() string {
6262
return modinfo
6363
}
64+
65+
// mayMoreStackPreempt is a maymorestack hook that forces a preemption
66+
// at every possible cooperative preemption point.
67+
//
68+
// This is valuable to apply to the runtime, which can be sensitive to
69+
// preemption points. To apply this to all preemption points in the
70+
// runtime and runtime-like code, use the following in bash or zsh:
71+
//
72+
// X=(-{gc,asm}flags={runtime/...,reflect,sync}=-d=maymorestack=runtime.mayMoreStackPreempt) GOFLAGS=${X[@]}
73+
//
74+
// This must be deeply nosplit because it is called from a function
75+
// prologue before the stack is set up and because the compiler will
76+
// call it from any splittable prologue (leading to infinite
77+
// recursion).
78+
//
79+
// Ideally it should also use very little stack because the linker
80+
// doesn't currently account for this in nosplit stack depth checking.
81+
//
82+
//go:nosplit
83+
//
84+
// Ensure mayMoreStackPreempt can be called for all ABIs.
85+
//
86+
//go:linkname mayMoreStackPreempt
87+
func mayMoreStackPreempt() {
88+
// Don't do anything on the g0 or gsignal stack.
89+
g := getg()
90+
if g == g.m.g0 || g == g.m.gsignal {
91+
return
92+
}
93+
// Force a preemption, unless the stack is already poisoned.
94+
if g.stackguard0 < stackPoisonMin {
95+
g.stackguard0 = stackPreempt
96+
}
97+
}

src/runtime/stack.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ const (
144144
// Force a stack movement. Used for debugging.
145145
// 0xfffffeed in hex.
146146
stackForceMove = uintptrMask & -275
147+
148+
// stackPoisonMin is the lowest allowed stack poison value.
149+
stackPoisonMin = uintptrMask & -4096
147150
)
148151

149152
// Global pool of spans that have free stacks.

0 commit comments

Comments
 (0)