1//===-- tsd_shared.h --------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef SCUDO_TSD_SHARED_H_
10#define SCUDO_TSD_SHARED_H_
11
12#include "tsd.h"
13
14#include "string_utils.h"
15
16#if SCUDO_HAS_PLATFORM_TLS_SLOT
17// This is a platform-provided header that needs to be on the include path when
18// Scudo is compiled. It must declare a function with the prototype:
19// uintptr_t *getPlatformAllocatorTlsSlot()
20// that returns the address of a thread-local word of storage reserved for
21// Scudo, that must be zero-initialized in newly created threads.
22#include "scudo_platform_tls_slot.h"
23#endif
24
25namespace scudo {
26
27template <class Allocator, u32 TSDsArraySize, u32 DefaultTSDCount>
28struct TSDRegistrySharedT {
29 using ThisT = TSDRegistrySharedT<Allocator, TSDsArraySize, DefaultTSDCount>;
30
31 struct ScopedTSD {
32 ALWAYS_INLINE ScopedTSD(ThisT &TSDRegistry) {
33 CurrentTSD = TSDRegistry.getTSDAndLock();
34 DCHECK_NE(CurrentTSD, nullptr);
35 }
36
37 ~ScopedTSD() { CurrentTSD->unlock(); }
38
39 TSD<Allocator> &operator*() { return *CurrentTSD; }
40
41 TSD<Allocator> *operator->() {
42 CurrentTSD->assertLocked(/*BypassCheck=*/false);
43 return CurrentTSD;
44 }
45
46 private:
47 TSD<Allocator> *CurrentTSD;
48 };
49
50 void init(Allocator *Instance) EXCLUDES(Mutex) {
51 ScopedLock L(Mutex);
52 // If more than one thread is initializing at the exact same moment, the
53 // threads that lose don't need to do anything.
54 if (UNLIKELY(atomic_load_relaxed(&Initialized) != 0))
55 return;
56
57 Instance->init();
58 for (u32 I = 0; I < TSDsArraySize; I++)
59 TSDs[I].init(Instance);
60 const u32 NumberOfCPUs = getNumberOfCPUs();
61 setNumberOfTSDs((NumberOfCPUs == 0) ? DefaultTSDCount
62 : Min(A: NumberOfCPUs, B: DefaultTSDCount));
63 atomic_store_relaxed(A: &Initialized, V: 1);
64 }
65
66 void initOnceMaybe(Allocator *Instance) {
67 if (LIKELY(atomic_load_relaxed(&Initialized) != 0))
68 return;
69 init(Instance); // Sets Initialized.
70 }
71
72 void unmapTestOnly(Allocator *Instance) {
73 ScopedLock L(Mutex);
74 for (u32 I = 0; I < TSDsArraySize; I++) {
75 TSDs[I].commitBack(Instance);
76 TSDs[I] = {};
77 }
78 setCurrentTSD(nullptr);
79 atomic_store_relaxed(A: &Initialized, V: 0);
80 }
81
82 void drainCaches(Allocator *Instance) {
83 u32 TotalTSDs = atomic_load_relaxed(A: &NumberOfTSDs);
84 for (uptr I = 0; I < TotalTSDs; ++I) {
85 TSDs[I].lock();
86 Instance->drainCache(&TSDs[I]);
87 TSDs[I].unlock();
88 }
89 }
90
91 ALWAYS_INLINE void initThreadMaybe(Allocator *Instance,
92 UNUSED bool MinimalInit) {
93 if (LIKELY(getCurrentTSD()))
94 return;
95 initThread(Instance);
96 }
97
98 void disable() NO_THREAD_SAFETY_ANALYSIS {
99 Mutex.lock();
100 for (u32 I = 0; I < TSDsArraySize; I++)
101 TSDs[I].lock();
102 }
103
104 void enable() NO_THREAD_SAFETY_ANALYSIS {
105 for (s32 I = static_cast<s32>(TSDsArraySize - 1); I >= 0; I--)
106 TSDs[I].unlock();
107 Mutex.unlock();
108 }
109
110 bool setOption(Option O, sptr Value) {
111 if (O == Option::MaxTSDsCount) {
112 ScopedLock L(Mutex);
113 return setNumberOfTSDs(static_cast<u32>(Value));
114 }
115 if (O == Option::ThreadDisableMemInit)
116 setDisableMemInit(Value);
117 // Not supported by the TSD Registry, but not an error either.
118 return true;
119 }
120
121 bool getDisableMemInit() const { return *getTlsPtr() & 1; }
122
123 void getStats(ScopedString *Str) {
124 u32 TotalTSDs = atomic_load_relaxed(A: &NumberOfTSDs);
125 Str->append(Format: "Stats: SharedTSDs: %u available; total %u\n", TotalTSDs,
126 TSDsArraySize);
127 for (uptr I = 0; I < TotalTSDs; ++I) {
128 TSDs[I].lock();
129 // Theoretically, we want to mark TSD::lock()/TSD::unlock() with proper
130 // thread annotations. However, given the TSD is only locked on shared
131 // path, do the assertion in a separate path to avoid confusing the
132 // analyzer.
133 TSDs[I].assertLocked(/*BypassCheck=*/true);
134 Str->append(Format: " Shared TSD[%zu]:\n", I);
135 TSDs[I].getSizeClassAllocator().getStats(Str);
136 TSDs[I].unlock();
137 }
138 }
139
140private:
141 ALWAYS_INLINE TSD<Allocator> *getTSDAndLock() NO_THREAD_SAFETY_ANALYSIS {
142 TSD<Allocator> *TSD = getCurrentTSD();
143 DCHECK(TSD);
144 if (TSDsArraySize == 1U) {
145 // Only 1 TSD, lock and return.
146 TSD->lock();
147 return TSD;
148 }
149
150 if (TSD->tryLock())
151 return TSD;
152 return getTSDSlow(CurrentTSD: TSD);
153 }
154
155 ALWAYS_INLINE uptr *getTlsPtr() const {
156#if SCUDO_HAS_PLATFORM_TLS_SLOT
157 return reinterpret_cast<uptr *>(getPlatformAllocatorTlsSlot());
158#else
159 static thread_local uptr ThreadTSD;
160 return &ThreadTSD;
161#endif
162 }
163
164 static_assert(alignof(TSD<Allocator>) >= 2, "");
165
166 ALWAYS_INLINE void setCurrentTSD(TSD<Allocator> *CurrentTSD) {
167 *getTlsPtr() &= 1;
168 *getTlsPtr() |= reinterpret_cast<uptr>(CurrentTSD);
169 }
170
171 ALWAYS_INLINE TSD<Allocator> *getCurrentTSD() {
172 return reinterpret_cast<TSD<Allocator> *>(*getTlsPtr() & ~1ULL);
173 }
174
175 // Requires a lock to avoid multiple threads trying to set the TSDs at once.
176 bool setNumberOfTSDs(u32 N) REQUIRES(Mutex) {
177 DCHECK_GT(N, 0);
178 const u32 TotalTSDs = atomic_load_relaxed(A: &NumberOfTSDs);
179 // In order to avoid needing locks for these values, the number of TSDs
180 // can never be decreased.
181 if (N < TotalTSDs)
182 return false;
183
184 if (N > TSDsArraySize)
185 N = TSDsArraySize;
186
187 // In order to avoid locks around the CoPrimes, compute all of the
188 // new values and then set them in place after.
189 u32 NewCoPrimes[TSDsArraySize] = {};
190 u32 NewNumberOfCoPrimes = 0;
191
192 // Compute all the coprimes of the TSDs. This will be used to walk the
193 // array of TSDs in a random order. For details, see:
194 // https://lemire.me/blog/2017/09/18/visiting-all-values-in-an-array-exactly-once-in-random-order/
195 for (u32 I = 0; I < N; I++) {
196 u32 A = I + 1;
197 u32 B = N;
198 // Find the GCD between I + 1 and N. If 1, they are coprimes.
199 while (B != 0) {
200 const u32 T = A;
201 A = B;
202 B = T % B;
203 }
204 if (A == 1)
205 NewCoPrimes[NewNumberOfCoPrimes++] = I + 1;
206 }
207
208 // Set these values in this particular order so that if getTSDSlow
209 // is called while they are being modified, nothing crashes, or encounters
210 // a zero CoPrime value.
211 for (u32 I = 0; I < NewNumberOfCoPrimes; I++) {
212 atomic_store(&CoPrimes[I], NewCoPrimes[I], memory_order_release);
213 }
214 atomic_store(A: &NumberOfCoPrimes, V: NewNumberOfCoPrimes, MO: memory_order_release);
215 atomic_store(A: &NumberOfTSDs, V: N, MO: memory_order_release);
216 return true;
217 }
218
219 void setDisableMemInit(bool B) {
220 *getTlsPtr() &= ~1ULL;
221 *getTlsPtr() |= B;
222 }
223
224 NOINLINE void initThread(Allocator *Instance) {
225 initOnceMaybe(Instance);
226 // Initial context assignment is done in a plain round-robin fashion.
227 const u32 Index = atomic_fetch_add(A: &CurrentIndex, V: 1U, MO: memory_order_relaxed);
228 setCurrentTSD(&TSDs[Index % atomic_load_relaxed(A: &NumberOfTSDs)]);
229 Instance->callPostInitCallback();
230 }
231
232 // TSDs is an array of locks which is not supported for marking thread-safety
233 // capability.
234 NOINLINE TSD<Allocator> *getTSDSlow(TSD<Allocator> *CurrentTSD) {
235 const u32 TotalTSDs = atomic_load_relaxed(A: &NumberOfTSDs);
236 if (UNLIKELY(TotalTSDs == 1U)) {
237 CurrentTSD->lock();
238 return CurrentTSD;
239 }
240
241 // Use the Precedence of the current TSD as our random seed. Since we are
242 // in the slow path, it means that tryLock failed, and as a result it's
243 // very likely that said Precedence is non-zero.
244 const u32 R = static_cast<u32>(CurrentTSD->getPrecedence());
245 const u32 TotalCoPrimes = atomic_load_relaxed(A: &NumberOfCoPrimes);
246 DCHECK_NE(TotalCoPrimes, 0U);
247 const u32 Inc = atomic_load_relaxed(&CoPrimes[R % TotalCoPrimes]);
248
249 u32 Index = R % TotalTSDs;
250 uptr LowestPrecedence = UINTPTR_MAX;
251 TSD<Allocator> *CandidateTSD = nullptr;
252 // Go randomly through at most 4 contexts and find a candidate.
253 for (u32 I = 0; I < Min(A: 4U, B: TotalTSDs); I++) {
254 if (TSDs[Index].tryLock()) {
255 setCurrentTSD(&TSDs[Index]);
256 return &TSDs[Index];
257 }
258 const uptr Precedence = TSDs[Index].getPrecedence();
259 // A 0 precedence here means another thread just locked this TSD.
260 if (Precedence && Precedence < LowestPrecedence) {
261 CandidateTSD = &TSDs[Index];
262 LowestPrecedence = Precedence;
263 }
264 Index += Inc;
265 if (Index >= TotalTSDs)
266 Index -= TotalTSDs;
267 }
268 if (CandidateTSD) {
269 CandidateTSD->lock();
270 setCurrentTSD(CandidateTSD);
271 return CandidateTSD;
272 }
273 // Last resort, stick with the current one.
274 CurrentTSD->lock();
275 return CurrentTSD;
276 }
277
278 atomic_u32 CurrentIndex = {};
279 atomic_u32 NumberOfTSDs = {};
280 atomic_u32 NumberOfCoPrimes = {};
281 atomic_u32 CoPrimes[TSDsArraySize] = {};
282 atomic_u8 Initialized = {};
283 // Used for global initialization and TSDs access.
284 // Acquiring the global initialization should only lock once in normal
285 // operation, which is why using it for TSDs access should not cause
286 // any interference.
287 HybridMutex Mutex;
288 TSD<Allocator> TSDs[TSDsArraySize];
289};
290
291} // namespace scudo
292
293#endif // SCUDO_TSD_SHARED_H_
294