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_s390x.s | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/runtime/sys_linux_s390x.s') diff --git a/src/runtime/sys_linux_s390x.s b/src/runtime/sys_linux_s390x.s index adf5612c3c..7da4a52729 100644 --- a/src/runtime/sys_linux_s390x.s +++ b/src/runtime/sys_linux_s390x.s @@ -604,3 +604,53 @@ TEXT runtime·socket(SB),$0-20 MOVD $0, 2(R0) // unimplemented, only needed for android; declared in stubs_linux.go MOVW R0, ret+16(FP) RET + +// func vgetrandom1(buf *byte, length uintptr, flags uint32, state uintptr, stateSize uintptr) int +TEXT runtime·vgetrandom1(SB),NOSPLIT,$16-48 + MOVD buf+0(FP), R2 + MOVD length+8(FP), R3 + MOVW flags+16(FP), R4 + MOVD state+24(FP), R5 + MOVD stateSize+32(FP), R6 + + MOVD R15, R7 + + MOVD runtime·vdsoGetrandomSym(SB), R1 + MOVD g_m(g), R9 + + MOVD m_vdsoPC(R9), R12 + MOVD R12, 8(R15) + MOVD m_vdsoSP(R9), R12 + MOVD R12, 16(R15) + MOVD R14, m_vdsoPC(R9) + MOVD $buf+0(FP), R12 + MOVD R12, m_vdsoSP(R9) + + SUB $160, R15 + MOVD $~7, R12 + AND R12, R15 + + MOVB runtime·iscgo(SB), R12 + CMPBNE R12, $0, nosaveg + MOVD m_gsignal(R9), R12 + CMPBEQ R12, $0, nosaveg + CMPBEQ g, R12, nosaveg + MOVD (g_stack+stack_lo)(R12), R12 + MOVD g, (R12) + + BL R1 + + MOVD $0, (R12) + JMP restore + +nosaveg: + BL R1 + +restore: + MOVD R7, R15 + MOVD 16(R15), R12 + MOVD R12, m_vdsoSP(R9) + MOVD 8(R15), R12 + MOVD R12, m_vdsoPC(R9) + MOVD R2, ret+40(FP) + RET -- cgit v1.3