Debian Codegraph @

src/runtime/vgetrandom_linux.go

raw
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build linux && (amd64 || arm64 || arm64be || ppc64 || ppc64le || loong64 || s390x)
6
7 package runtime
8
9 import (
10 "internal/cpu"
11 "unsafe"
12 )
13
14 //go:noescape
15 func vgetrandom1(buf *byte, length uintptr, flags uint32, state uintptr, stateSize uintptr) int
16
17 var vgetrandomAlloc struct {
18 states []uintptr
19 statesLock mutex
20 stateSize uintptr
21 mmapProt int32
22 mmapFlags int32
23 }
24
25 func vgetrandomInit() {
26 if vdsoGetrandomSym == 0 {
27 return
28 }
29
30 var params struct {
31 SizeOfOpaqueState uint32
32 MmapProt uint32
33 MmapFlags uint32
34 reserved [13]uint32
35 }
36 if vgetrandom1(nil, 0, 0, uintptr(unsafe.Pointer(&params)), ^uintptr(0)) != 0 {
37 return
38 }
39 vgetrandomAlloc.stateSize = uintptr(params.SizeOfOpaqueState)
40 vgetrandomAlloc.mmapProt = int32(params.MmapProt)
41 vgetrandomAlloc.mmapFlags = int32(params.MmapFlags)
42
43 lockInit(&vgetrandomAlloc.statesLock, lockRankVgetrandom)
44 }
45
46 func vgetrandomGetState() uintptr {
47 lock(&vgetrandomAlloc.statesLock)
48 if len(vgetrandomAlloc.states) == 0 {
49 num := uintptr(numCPUStartup) // Just a reasonable size hint to start.
50 stateSizeCacheAligned := (vgetrandomAlloc.stateSize + cpu.CacheLineSize - 1) &^ (cpu.CacheLineSize - 1)
51 allocSize := (num*stateSizeCacheAligned + physPageSize - 1) &^ (physPageSize - 1)
52 num = (physPageSize / stateSizeCacheAligned) * (allocSize / physPageSize)
53 p, err := mmap(nil, allocSize, vgetrandomAlloc.mmapProt, vgetrandomAlloc.mmapFlags, -1, 0)
54 if err != 0 {
55 unlock(&vgetrandomAlloc.statesLock)
56 return 0
57 }
58 setVMAName(p, allocSize, "getrandom states")
59 newBlock := uintptr(p)
60 if vgetrandomAlloc.states == nil {
61 vgetrandomAlloc.states = make([]uintptr, 0, num)
62 }
63 for i := uintptr(0); i < num; i++ {
64 if (newBlock&(physPageSize-1))+vgetrandomAlloc.stateSize > physPageSize {
65 newBlock = (newBlock + physPageSize - 1) &^ (physPageSize - 1)
66 }
67 vgetrandomAlloc.states = append(vgetrandomAlloc.states, newBlock)
68 newBlock += stateSizeCacheAligned
69 }
70 }
71 state := vgetrandomAlloc.states[len(vgetrandomAlloc.states)-1]
72 vgetrandomAlloc.states = vgetrandomAlloc.states[:len(vgetrandomAlloc.states)-1]
73 unlock(&vgetrandomAlloc.statesLock)
74 return state
75 }
76
77 // Free vgetrandom state from the M (if any) prior to destroying the M.
78 //
79 // This may allocate, so it must have a P.
80 func vgetrandomDestroy(mp *m) {
81 if mp.vgetrandomState == 0 {
82 return
83 }
84
85 lock(&vgetrandomAlloc.statesLock)
86 vgetrandomAlloc.states = append(vgetrandomAlloc.states, mp.vgetrandomState)
87 unlock(&vgetrandomAlloc.statesLock)
88 }
89
90 // This is exported for use in internal/syscall/unix as well as x/sys/unix.
91 //
92 //go:linkname vgetrandom
93 func vgetrandom(p []byte, flags uint32) (ret int, supported bool) {
94 if vgetrandomAlloc.stateSize == 0 {
95 return -1, false
96 }
97
98 // We use getg().m instead of acquirem() here, because always taking
99 // the lock is slightly more expensive than not always taking the lock.
100 // However, we *do* require that m doesn't migrate elsewhere during the
101 // execution of the vDSO. So, we exploit two details:
102 // 1) Asynchronous preemption is aborted when PC is in the runtime.
103 // 2) Most of the time, this function only calls vgetrandom1(), which
104 // does not have a preamble that synchronously preempts.
105 // We do need to take the lock when getting a new state for m, but this
106 // is very much the slow path, in the sense that it only ever happens
107 // once over the entire lifetime of an m. So, a simple getg().m suffices.
108 mp := getg().m
109
110 if mp.vgetrandomState == 0 {
111 mp.locks++
112 state := vgetrandomGetState()
113 mp.locks--
114 if state == 0 {
115 return -1, false
116 }
117 mp.vgetrandomState = state
118 }
119 return vgetrandom1(unsafe.SliceData(p), uintptr(len(p)), flags, mp.vgetrandomState, vgetrandomAlloc.stateSize), true
120 }