From eb6f2c24cd17c0ca1df7e343f8d9187eef7d6e13 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 22 Sep 2024 03:45:29 +0200 Subject: runtime: use vDSO for getrandom() on linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Linux 6.11 supports calling getrandom() from the vDSO. It operates on a thread-local opaque state allocated with mmap using flags specified by the vDSO. Opaque states are allocated in chunks, ideally ncpu at a time as a hint, rounding up to as many fit in a complete page. On first use, a state is assigned to an m, which owns that state, until the m exits, at which point it is given back to the pool. Performance appears to be quite good: │ sec/op │ sec/op vs base │ Read/4-16 222.45n ± 3% 27.13n ± 6% -87.80% (p=0.000 n=10) │ B/s │ B/s vs base │ Read/4-16 17.15Mi ± 3% 140.61Mi ± 6% +719.82% (p=0.000 n=10) Fixes #69577. Change-Id: Ib6f44e8f2f3940c94d970eaada0eb566ec297dc7 Reviewed-on: https://go-review.googlesource.com/c/go/+/614835 Reviewed-by: Filippo Valsorda Reviewed-by: Cuong Manh Le Auto-Submit: Jason Donenfeld Reviewed-by: Paul Murphy LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Michael Pratt --- src/runtime/sys_linux_amd64.s | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/runtime/sys_linux_amd64.s') diff --git a/src/runtime/sys_linux_amd64.s b/src/runtime/sys_linux_amd64.s index b6c64dc095..941f70b0e8 100644 --- a/src/runtime/sys_linux_amd64.s +++ b/src/runtime/sys_linux_amd64.s @@ -704,3 +704,36 @@ TEXT runtime·sbrk0(SB),NOSPLIT,$0-8 SYSCALL MOVQ AX, ret+0(FP) RET + +// func vgetrandom1(buf *byte, length uintptr, flags uint32, state uintptr, stateSize uintptr) int +TEXT runtime·vgetrandom1(SB),NOSPLIT,$16-48 + MOVQ SI, R8 // stateSize + MOVL CX, DX // flags + MOVQ DI, CX // state + MOVQ BX, SI // length + MOVQ AX, DI // buf + + MOVQ SP, R12 + + MOVQ runtime·vdsoGetrandomSym(SB), AX + MOVQ g_m(R14), BX + + MOVQ m_vdsoPC(BX), R9 + MOVQ R9, 0(SP) + MOVQ m_vdsoSP(BX), R9 + MOVQ R9, 8(SP) + LEAQ buf+0(FP), R9 + MOVQ R9, m_vdsoSP(BX) + MOVQ -8(R9), R9 + MOVQ R9, m_vdsoPC(BX) + + ANDQ $~15, SP + + CALL AX + + MOVQ R12, SP + MOVQ 8(SP), R9 + MOVQ R9, m_vdsoSP(BX) + MOVQ 0(SP), R9 + MOVQ R9, m_vdsoPC(BX) + RET -- cgit v1.3