1 //===-- tsd_shared.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef SCUDO_TSD_SHARED_H_
10 #define SCUDO_TSD_SHARED_H_
12 #include "linux.h" // for getAndroidTlsPtr()
19 template <class Allocator, u32 MaxTSDCount> struct TSDRegistrySharedT {
20 void initLinkerInitialized(Allocator *Instance) {
21 Instance->initLinkerInitialized();
22 CHECK_EQ(pthread_key_create(&PThreadKey, nullptr), 0); // For non-TLS
23 NumberOfTSDs = Min(Max(1U, getNumberOfCPUs()), MaxTSDCount);
24 TSDs = reinterpret_cast<TSD<Allocator> *>(
25 map(nullptr, sizeof(TSD<Allocator>) * NumberOfTSDs, "scudo:tsd"));
26 for (u32 I = 0; I < NumberOfTSDs; I++)
27 TSDs[I].initLinkerInitialized(Instance);
28 // Compute all the coprimes of NumberOfTSDs. This will be used to walk the
29 // array of TSDs in a random order. For details, see:
30 // https://lemire.me/blog/2017/09/18/visiting-all-values-in-an-array-exactly-once-in-random-order/
31 for (u32 I = 0; I < NumberOfTSDs; I++) {
34 // Find the GCD between I + 1 and NumberOfTSDs. If 1, they are coprimes.
41 CoPrimes[NumberOfCoPrimes++] = I + 1;
45 void init(Allocator *Instance) {
46 memset(this, 0, sizeof(*this));
47 initLinkerInitialized(Instance);
50 void unmapTestOnly() {
51 unmap(reinterpret_cast<void *>(TSDs),
52 sizeof(TSD<Allocator>) * NumberOfTSDs);
55 ALWAYS_INLINE void initThreadMaybe(Allocator *Instance,
56 UNUSED bool MinimalInit) {
57 if (LIKELY(getCurrentTSD()))
62 ALWAYS_INLINE TSD<Allocator> *getTSDAndLock(bool *UnlockRequired) {
63 TSD<Allocator> *TSD = getCurrentTSD();
65 *UnlockRequired = true;
66 // Try to lock the currently associated context.
69 // If that fails, go down the slow path.
70 return getTSDAndLockSlow(TSD);
74 ALWAYS_INLINE void setCurrentTSD(TSD<Allocator> *CurrentTSD) {
76 *getAndroidTlsPtr() = reinterpret_cast<uptr>(CurrentTSD);
78 ThreadTSD = CurrentTSD;
81 pthread_setspecific(PThreadKey, reinterpret_cast<void *>(CurrentTSD)),
86 ALWAYS_INLINE TSD<Allocator> *getCurrentTSD() {
88 return reinterpret_cast<TSD<Allocator> *>(*getAndroidTlsPtr());
92 return reinterpret_cast<TSD<Allocator> *>(pthread_getspecific(PThreadKey));
96 void initOnceMaybe(Allocator *Instance) {
100 initLinkerInitialized(Instance); // Sets Initialized.
103 NOINLINE void initThread(Allocator *Instance) {
104 initOnceMaybe(Instance);
105 // Initial context assignment is done in a plain round-robin fashion.
106 const u32 Index = atomic_fetch_add(&CurrentIndex, 1U, memory_order_relaxed);
107 setCurrentTSD(&TSDs[Index % NumberOfTSDs]);
110 NOINLINE TSD<Allocator> *getTSDAndLockSlow(TSD<Allocator> *CurrentTSD) {
111 if (MaxTSDCount > 1U && NumberOfTSDs > 1U) {
112 // Use the Precedence of the current TSD as our random seed. Since we are
113 // in the slow path, it means that tryLock failed, and as a result it's
114 // very likely that said Precedence is non-zero.
115 u32 RandState = static_cast<u32>(CurrentTSD->getPrecedence());
116 const u32 R = getRandomU32(&RandState);
117 const u32 Inc = CoPrimes[R % NumberOfCoPrimes];
118 u32 Index = R % NumberOfTSDs;
119 uptr LowestPrecedence = UINTPTR_MAX;
120 TSD<Allocator> *CandidateTSD = nullptr;
121 // Go randomly through at most 4 contexts and find a candidate.
122 for (u32 I = 0; I < Min(4U, NumberOfTSDs); I++) {
123 if (TSDs[Index].tryLock()) {
124 setCurrentTSD(&TSDs[Index]);
127 const uptr Precedence = TSDs[Index].getPrecedence();
128 // A 0 precedence here means another thread just locked this TSD.
129 if (Precedence && Precedence < LowestPrecedence) {
130 CandidateTSD = &TSDs[Index];
131 LowestPrecedence = Precedence;
134 if (Index >= NumberOfTSDs)
135 Index -= NumberOfTSDs;
138 CandidateTSD->lock();
139 setCurrentTSD(CandidateTSD);
143 // Last resort, stick with the current one.
148 pthread_key_t PThreadKey;
149 atomic_u32 CurrentIndex;
151 TSD<Allocator> *TSDs;
152 u32 NumberOfCoPrimes;
153 u32 CoPrimes[MaxTSDCount];
156 #if SCUDO_LINUX && !SCUDO_ANDROID
157 static THREADLOCAL TSD<Allocator> *ThreadTSD;
161 #if SCUDO_LINUX && !SCUDO_ANDROID
162 template <class Allocator, u32 MaxTSDCount>
163 THREADLOCAL TSD<Allocator>
164 *TSDRegistrySharedT<Allocator, MaxTSDCount>::ThreadTSD;
169 #endif // SCUDO_TSD_SHARED_H_