Skip to content

Commit

Permalink
runtime: make consistentHeapStats acquire/release nosplit
Browse files Browse the repository at this point in the history
consistentHeapStats is updated during a stack allocation, so a stack
growth during an acquire or release could cause another acquire to
happen before the operation completes fully. This may lead to an invalid
sequence number.

Fixes #49395.

Change-Id: I41ce3393dff80201793e053d4d6394d7b211a5b7
Reviewed-on: https://go-review.googlesource.com/c/go/+/361158
Trust: Michael Knyszek <[email protected]>
Reviewed-by: Michael Pratt <[email protected]>
Reviewed-by: Austin Clements <[email protected]>
  • Loading branch information
mknyszek committed Nov 5, 2021
1 parent 6f32d20 commit df18377
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/runtime/mstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,15 @@ type consistentHeapStats struct {
//
// The caller's P must not change between acquire and
// release. This also means that the caller should not
// acquire a P or release its P in between.
// acquire a P or release its P in between. A P also must
// not acquire a given consistentHeapStats if it hasn't
// yet released it.
//
// nosplit because a stack growth in this function could
// lead to a stack allocation that could reenter the
// function.
//
//go:nosplit
func (m *consistentHeapStats) acquire() *heapStatsDelta {
if pp := getg().m.p.ptr(); pp != nil {
seq := atomic.Xadd(&pp.statsSeq, 1)
Expand All @@ -814,6 +822,12 @@ func (m *consistentHeapStats) acquire() *heapStatsDelta {
// The caller's P must not change between acquire and
// release. This also means that the caller should not
// acquire a P or release its P in between.
//
// nosplit because a stack growth in this function could
// lead to a stack allocation that causes another acquire
// before this operation has completed.
//
//go:nosplit
func (m *consistentHeapStats) release() {
if pp := getg().m.p.ptr(); pp != nil {
seq := atomic.Xadd(&pp.statsSeq, 1)
Expand Down

0 comments on commit df18377

Please sign in to comment.