mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: ensure mheap lock stack growth invariant is maintained
Currently there's an invariant in the runtime wherein the heap lock
can only be acquired on the system stack, otherwise a self-deadlock
could occur if the stack grows while the lock is held.
This invariant is upheld and documented in a number of situations (e.g.
allocManual, freeManual) but there are other places where the invariant
is either not maintained at all which risks self-deadlock (e.g.
setGCPercent, gcResetMarkState, allocmcache) or is maintained but
undocumented (e.g. gcSweep, readGCStats_m).
This change adds go:systemstack to any function that acquires the heap
lock or adds a systemstack(func() { ... }) around the critical section,
where appropriate. It also documents the invariant on (*mheap).lock
directly and updates repetitive documentation to refer to that comment.
Fixes #32105.
Change-Id: I702b1290709c118b837389c78efde25c51a2cafb
Reviewed-on: https://go-review.googlesource.com/c/go/+/177857
Run-TryBot: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
parent
db3255536c
commit
7ed7669c0d
5 changed files with 64 additions and 35 deletions
|
|
@ -29,6 +29,8 @@ const minPhysPageSize = 4096
|
|||
//
|
||||
//go:notinheap
|
||||
type mheap struct {
|
||||
// lock must only be acquired on the system stack, otherwise a g
|
||||
// could self-deadlock if its stack grows with the lock held.
|
||||
lock mutex
|
||||
free mTreap // free spans
|
||||
sweepgen uint32 // sweep generation, see comment in mspan
|
||||
|
|
@ -1095,9 +1097,8 @@ func (h *mheap) alloc(npage uintptr, spanclass spanClass, large bool, needzero b
|
|||
// The memory backing the returned span may not be zeroed if
|
||||
// span.needzero is set.
|
||||
//
|
||||
// allocManual must be called on the system stack to prevent stack
|
||||
// growth. Since this is used by the stack allocator, stack growth
|
||||
// during allocManual would self-deadlock.
|
||||
// allocManual must be called on the system stack because it acquires
|
||||
// the heap lock. See mheap for details.
|
||||
//
|
||||
//go:systemstack
|
||||
func (h *mheap) allocManual(npage uintptr, stat *uint64) *mspan {
|
||||
|
|
@ -1303,8 +1304,8 @@ func (h *mheap) freeSpan(s *mspan, large bool) {
|
|||
// This must only be called when gcphase == _GCoff. See mSpanState for
|
||||
// an explanation.
|
||||
//
|
||||
// freeManual must be called on the system stack to prevent stack
|
||||
// growth, just like allocManual.
|
||||
// freeManual must be called on the system stack because it acquires
|
||||
// the heap lock. See mheap for details.
|
||||
//
|
||||
//go:systemstack
|
||||
func (h *mheap) freeManual(s *mspan, stat *uint64) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue