tsd_shared.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifndef SCUDO_TSD_SHARED_H_
  9. #define SCUDO_TSD_SHARED_H_
  10. #include "tsd.h"
  11. #if SCUDO_HAS_PLATFORM_TLS_SLOT
  12. // This is a platform-provided header that needs to be on the include path when
  13. // Scudo is compiled. It must declare a function with the prototype:
  14. // uintptr_t *getPlatformAllocatorTlsSlot()
  15. // that returns the address of a thread-local word of storage reserved for
  16. // Scudo, that must be zero-initialized in newly created threads.
  17. #error #include "scudo_platform_tls_slot.h"
  18. #endif
  19. namespace scudo {
  20. template <class Allocator, u32 TSDsArraySize, u32 DefaultTSDCount>
  21. struct TSDRegistrySharedT {
  22. void init(Allocator *Instance) {
  23. DCHECK(!Initialized);
  24. Instance->init();
  25. for (u32 I = 0; I < TSDsArraySize; I++)
  26. TSDs[I].init(Instance);
  27. const u32 NumberOfCPUs = getNumberOfCPUs();
  28. setNumberOfTSDs((NumberOfCPUs == 0) ? DefaultTSDCount
  29. : Min(NumberOfCPUs, DefaultTSDCount));
  30. Initialized = true;
  31. }
  32. void initOnceMaybe(Allocator *Instance) {
  33. ScopedLock L(Mutex);
  34. if (LIKELY(Initialized))
  35. return;
  36. init(Instance); // Sets Initialized.
  37. }
  38. void unmapTestOnly(Allocator *Instance) {
  39. for (u32 I = 0; I < TSDsArraySize; I++) {
  40. TSDs[I].commitBack(Instance);
  41. TSDs[I] = {};
  42. }
  43. setCurrentTSD(nullptr);
  44. Initialized = false;
  45. }
  46. ALWAYS_INLINE void initThreadMaybe(Allocator *Instance,
  47. UNUSED bool MinimalInit) {
  48. if (LIKELY(getCurrentTSD()))
  49. return;
  50. initThread(Instance);
  51. }
  52. ALWAYS_INLINE TSD<Allocator> *getTSDAndLock(bool *UnlockRequired) {
  53. TSD<Allocator> *TSD = getCurrentTSD();
  54. DCHECK(TSD);
  55. *UnlockRequired = true;
  56. // Try to lock the currently associated context.
  57. if (TSD->tryLock())
  58. return TSD;
  59. // If that fails, go down the slow path.
  60. if (TSDsArraySize == 1U) {
  61. // Only 1 TSD, not need to go any further.
  62. // The compiler will optimize this one way or the other.
  63. TSD->lock();
  64. return TSD;
  65. }
  66. return getTSDAndLockSlow(TSD);
  67. }
  68. void disable() {
  69. Mutex.lock();
  70. for (u32 I = 0; I < TSDsArraySize; I++)
  71. TSDs[I].lock();
  72. }
  73. void enable() {
  74. for (s32 I = static_cast<s32>(TSDsArraySize - 1); I >= 0; I--)
  75. TSDs[I].unlock();
  76. Mutex.unlock();
  77. }
  78. bool setOption(Option O, sptr Value) {
  79. if (O == Option::MaxTSDsCount)
  80. return setNumberOfTSDs(static_cast<u32>(Value));
  81. if (O == Option::ThreadDisableMemInit)
  82. setDisableMemInit(Value);
  83. // Not supported by the TSD Registry, but not an error either.
  84. return true;
  85. }
  86. bool getDisableMemInit() const { return *getTlsPtr() & 1; }
  87. private:
  88. ALWAYS_INLINE uptr *getTlsPtr() const {
  89. #if SCUDO_HAS_PLATFORM_TLS_SLOT
  90. return reinterpret_cast<uptr *>(getPlatformAllocatorTlsSlot());
  91. #else
  92. static thread_local uptr ThreadTSD;
  93. return &ThreadTSD;
  94. #endif
  95. }
  96. static_assert(alignof(TSD<Allocator>) >= 2, "");
  97. ALWAYS_INLINE void setCurrentTSD(TSD<Allocator> *CurrentTSD) {
  98. *getTlsPtr() &= 1;
  99. *getTlsPtr() |= reinterpret_cast<uptr>(CurrentTSD);
  100. }
  101. ALWAYS_INLINE TSD<Allocator> *getCurrentTSD() {
  102. return reinterpret_cast<TSD<Allocator> *>(*getTlsPtr() & ~1ULL);
  103. }
  104. bool setNumberOfTSDs(u32 N) {
  105. ScopedLock L(MutexTSDs);
  106. if (N < NumberOfTSDs)
  107. return false;
  108. if (N > TSDsArraySize)
  109. N = TSDsArraySize;
  110. NumberOfTSDs = N;
  111. NumberOfCoPrimes = 0;
  112. // Compute all the coprimes of NumberOfTSDs. This will be used to walk the
  113. // array of TSDs in a random order. For details, see:
  114. // https://lemire.me/blog/2017/09/18/visiting-all-values-in-an-array-exactly-once-in-random-order/
  115. for (u32 I = 0; I < N; I++) {
  116. u32 A = I + 1;
  117. u32 B = N;
  118. // Find the GCD between I + 1 and N. If 1, they are coprimes.
  119. while (B != 0) {
  120. const u32 T = A;
  121. A = B;
  122. B = T % B;
  123. }
  124. if (A == 1)
  125. CoPrimes[NumberOfCoPrimes++] = I + 1;
  126. }
  127. return true;
  128. }
  129. void setDisableMemInit(bool B) {
  130. *getTlsPtr() &= ~1ULL;
  131. *getTlsPtr() |= B;
  132. }
  133. NOINLINE void initThread(Allocator *Instance) {
  134. initOnceMaybe(Instance);
  135. // Initial context assignment is done in a plain round-robin fashion.
  136. const u32 Index = atomic_fetch_add(&CurrentIndex, 1U, memory_order_relaxed);
  137. setCurrentTSD(&TSDs[Index % NumberOfTSDs]);
  138. Instance->callPostInitCallback();
  139. }
  140. NOINLINE TSD<Allocator> *getTSDAndLockSlow(TSD<Allocator> *CurrentTSD) {
  141. // Use the Precedence of the current TSD as our random seed. Since we are
  142. // in the slow path, it means that tryLock failed, and as a result it's
  143. // very likely that said Precedence is non-zero.
  144. const u32 R = static_cast<u32>(CurrentTSD->getPrecedence());
  145. u32 N, Inc;
  146. {
  147. ScopedLock L(MutexTSDs);
  148. N = NumberOfTSDs;
  149. DCHECK_NE(NumberOfCoPrimes, 0U);
  150. Inc = CoPrimes[R % NumberOfCoPrimes];
  151. }
  152. if (N > 1U) {
  153. u32 Index = R % N;
  154. uptr LowestPrecedence = UINTPTR_MAX;
  155. TSD<Allocator> *CandidateTSD = nullptr;
  156. // Go randomly through at most 4 contexts and find a candidate.
  157. for (u32 I = 0; I < Min(4U, N); I++) {
  158. if (TSDs[Index].tryLock()) {
  159. setCurrentTSD(&TSDs[Index]);
  160. return &TSDs[Index];
  161. }
  162. const uptr Precedence = TSDs[Index].getPrecedence();
  163. // A 0 precedence here means another thread just locked this TSD.
  164. if (Precedence && Precedence < LowestPrecedence) {
  165. CandidateTSD = &TSDs[Index];
  166. LowestPrecedence = Precedence;
  167. }
  168. Index += Inc;
  169. if (Index >= N)
  170. Index -= N;
  171. }
  172. if (CandidateTSD) {
  173. CandidateTSD->lock();
  174. setCurrentTSD(CandidateTSD);
  175. return CandidateTSD;
  176. }
  177. }
  178. // Last resort, stick with the current one.
  179. CurrentTSD->lock();
  180. return CurrentTSD;
  181. }
  182. atomic_u32 CurrentIndex = {};
  183. u32 NumberOfTSDs = 0;
  184. u32 NumberOfCoPrimes = 0;
  185. u32 CoPrimes[TSDsArraySize] = {};
  186. bool Initialized = false;
  187. HybridMutex Mutex;
  188. HybridMutex MutexTSDs;
  189. TSD<Allocator> TSDs[TSDsArraySize];
  190. };
  191. } // namespace scudo
  192. #endif // SCUDO_TSD_SHARED_H_