]> Cypherpunks repositories - gostls13.git/commit
runtime: save scalar registers off stack in amd64 async preemption
authorAustin Clements <austin@google.com>
Wed, 30 Apr 2025 02:55:40 +0000 (22:55 -0400)
committerGopher Robot <gobot@golang.org>
Tue, 5 Aug 2025 20:58:52 +0000 (13:58 -0700)
commitaf0c4fe2ca51918bd82231ac48a28a0d0ef15c98
treed895c4802fb8aeb94d8c6d1d634bc74f152fb134
parente73afaae69bb6f098e3c1dbd98cfe134843910a1
runtime: save scalar registers off stack in amd64 async preemption

Asynchronous preemption must save all registers that could be in use
by Go code. Currently, it saves all of these to the goroutine stack.
As a result, the stack frame requirements of asynchronous preemption
can be rather high. On amd64, this requires 368 bytes of stack space,
most of which is the XMM registers. Several RISC architectures are
around 0.5 KiB.

As we add support for SIMD instructions, this is going to become a
problem. The AVX-512 register state is 2.5 KiB. This well exceeds the
nosplit limit, and even if it didn't, could constrain when we can
asynchronously preempt goroutines on small stacks.

This CL fixes this by moving pure scalar state stored in non-GP
registers off the stack and into an allocated "extended register
state" object. To reduce space overhead, we only allocate these
objects as needed. While in the theoretical limit, every G could need
this register state, in practice very few do at a time.

However, we can't allocate when we're in the middle of saving the
register state during an asynchronous preemption, so we reserve
scratch space on every P to temporarily store the register state,
which can then be copied out to an allocated state object later by Go
code.

This commit only implements this for amd64, since that's where we're
about to add much more vector state, but it lays the groundwork for
doing this on any architecture that could benefit.

This is a cherry-pick of CL 680898 plus bug fix CL 684836 from the
dev.simd branch.

Change-Id: I123a95e21c11d5c10942d70e27f84d2d99bbf735
Reviewed-on: https://go-review.googlesource.com/c/go/+/669195
Auto-Submit: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
13 files changed:
src/runtime/export_test.go
src/runtime/lockrank.go
src/runtime/mheap.go
src/runtime/mklockrank.go
src/runtime/mkpreempt.go
src/runtime/preempt.go
src/runtime/preempt_amd64.go [new file with mode: 0644]
src/runtime/preempt_amd64.s
src/runtime/preempt_noxreg.go [new file with mode: 0644]
src/runtime/preempt_xreg.go [new file with mode: 0644]
src/runtime/proc.go
src/runtime/runtime2.go
src/runtime/sizeof_test.go