Skip to content

Commit

Permalink
runtime: add always-preempt maymorestack hook
Browse files Browse the repository at this point in the history
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 golang#47302,
 golang#47304, and golang#47441.

For golang#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]>
  • Loading branch information
aclements committed Nov 5, 2021
1 parent 3839b60 commit 35c7234
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/runtime/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,37 @@ func NumGoroutine() int {
func debug_modinfo() string {
return modinfo
}

// mayMoreStackPreempt is a maymorestack hook that forces a preemption
// at every possible cooperative preemption point.
//
// This is valuable to apply to the runtime, which can be sensitive to
// preemption points. To apply this to all preemption points in the
// runtime and runtime-like code, use the following in bash or zsh:
//
// X=(-{gc,asm}flags={runtime/...,reflect,sync}=-d=maymorestack=runtime.mayMoreStackPreempt) GOFLAGS=${X[@]}
//
// This must be deeply nosplit because it is called from a function
// prologue before the stack is set up and because the compiler will
// call it from any splittable prologue (leading to infinite
// recursion).
//
// Ideally it should also use very little stack because the linker
// doesn't currently account for this in nosplit stack depth checking.
//
//go:nosplit
//
// Ensure mayMoreStackPreempt can be called for all ABIs.
//
//go:linkname mayMoreStackPreempt
func mayMoreStackPreempt() {
// Don't do anything on the g0 or gsignal stack.
g := getg()
if g == g.m.g0 || g == g.m.gsignal {
return
}
// Force a preemption, unless the stack is already poisoned.
if g.stackguard0 < stackPoisonMin {
g.stackguard0 = stackPreempt
}
}
3 changes: 3 additions & 0 deletions src/runtime/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ const (
// Force a stack movement. Used for debugging.
// 0xfffffeed in hex.
stackForceMove = uintptrMask & -275

// stackPoisonMin is the lowest allowed stack poison value.
stackPoisonMin = uintptrMask & -4096
)

// Global pool of spans that have free stacks.
Expand Down

0 comments on commit 35c7234

Please sign in to comment.