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/os_linux.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/runtime/os_linux.go') diff --git a/src/runtime/os_linux.go b/src/runtime/os_linux.go index e18ef8e776..979761cc6a 100644 --- a/src/runtime/os_linux.go +++ b/src/runtime/os_linux.go @@ -31,6 +31,10 @@ type mOS struct { // needPerThreadSyscall indicates that a per-thread syscall is required // for doAllThreadsSyscall. needPerThreadSyscall atomic.Uint8 + + // This is a pointer to a chunk of memory allocated with a special + // mmap invocation in vgetrandomGetState(). + vgetrandomState uintptr } //go:noescape @@ -344,6 +348,7 @@ func osinit() { ncpu = getproccount() physHugePageSize = getHugePageSize() osArchInit() + vgetrandomInit() } var urandom_dev = []byte("/dev/urandom\x00") @@ -400,6 +405,10 @@ func unminit() { // Called from exitm, but not from drop, to undo the effect of thread-owned // resources in minit, semacreate, or elsewhere. Do not take locks after calling this. func mdestroy(mp *m) { + if mp.vgetrandomState != 0 { + vgetrandomPutState(mp.vgetrandomState) + mp.vgetrandomState = 0 + } } // #ifdef GOARCH_386 -- cgit v1.3-5-g9baa