primary64.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //===-- primary64.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_PRIMARY64_H_
  9. #define SCUDO_PRIMARY64_H_
  10. #include "bytemap.h"
  11. #include "common.h"
  12. #include "list.h"
  13. #include "local_cache.h"
  14. #include "memtag.h"
  15. #include "options.h"
  16. #include "release.h"
  17. #include "stats.h"
  18. #include "string_utils.h"
  19. namespace scudo {
  20. // SizeClassAllocator64 is an allocator tuned for 64-bit address space.
  21. //
  22. // It starts by reserving NumClasses * 2^RegionSizeLog bytes, equally divided in
  23. // Regions, specific to each size class. Note that the base of that mapping is
  24. // random (based to the platform specific map() capabilities). If
  25. // PrimaryEnableRandomOffset is set, each Region actually starts at a random
  26. // offset from its base.
  27. //
  28. // Regions are mapped incrementally on demand to fulfill allocation requests,
  29. // those mappings being split into equally sized Blocks based on the size class
  30. // they belong to. The Blocks created are shuffled to prevent predictable
  31. // address patterns (the predictability increases with the size of the Blocks).
  32. //
  33. // The 1st Region (for size class 0) holds the TransferBatches. This is a
  34. // structure used to transfer arrays of available pointers from the class size
  35. // freelist to the thread specific freelist, and back.
  36. //
  37. // The memory used by this allocator is never unmapped, but can be partially
  38. // released if the platform allows for it.
  39. template <typename Config> class SizeClassAllocator64 {
  40. public:
  41. typedef typename Config::PrimaryCompactPtrT CompactPtrT;
  42. static const uptr CompactPtrScale = Config::PrimaryCompactPtrScale;
  43. typedef typename Config::SizeClassMap SizeClassMap;
  44. typedef SizeClassAllocator64<Config> ThisT;
  45. typedef SizeClassAllocatorLocalCache<ThisT> CacheT;
  46. typedef typename CacheT::TransferBatch TransferBatch;
  47. static uptr getSizeByClassId(uptr ClassId) {
  48. return (ClassId == SizeClassMap::BatchClassId)
  49. ? roundUpTo(sizeof(TransferBatch), 1U << CompactPtrScale)
  50. : SizeClassMap::getSizeByClassId(ClassId);
  51. }
  52. static bool canAllocate(uptr Size) { return Size <= SizeClassMap::MaxSize; }
  53. void init(s32 ReleaseToOsInterval) {
  54. DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
  55. DCHECK_EQ(PrimaryBase, 0U);
  56. // Reserve the space required for the Primary.
  57. PrimaryBase = reinterpret_cast<uptr>(
  58. map(nullptr, PrimarySize, nullptr, MAP_NOACCESS, &Data));
  59. u32 Seed;
  60. const u64 Time = getMonotonicTime();
  61. if (!getRandom(reinterpret_cast<void *>(&Seed), sizeof(Seed)))
  62. Seed = static_cast<u32>(Time ^ (PrimaryBase >> 12));
  63. const uptr PageSize = getPageSizeCached();
  64. for (uptr I = 0; I < NumClasses; I++) {
  65. RegionInfo *Region = getRegionInfo(I);
  66. // The actual start of a region is offset by a random number of pages
  67. // when PrimaryEnableRandomOffset is set.
  68. Region->RegionBeg = getRegionBaseByClassId(I) +
  69. (Config::PrimaryEnableRandomOffset
  70. ? ((getRandomModN(&Seed, 16) + 1) * PageSize)
  71. : 0);
  72. Region->RandState = getRandomU32(&Seed);
  73. Region->ReleaseInfo.LastReleaseAtNs = Time;
  74. }
  75. setOption(Option::ReleaseInterval, static_cast<sptr>(ReleaseToOsInterval));
  76. }
  77. void unmapTestOnly() {
  78. for (uptr I = 0; I < NumClasses; I++) {
  79. RegionInfo *Region = getRegionInfo(I);
  80. *Region = {};
  81. }
  82. unmap(reinterpret_cast<void *>(PrimaryBase), PrimarySize, UNMAP_ALL, &Data);
  83. PrimaryBase = 0U;
  84. }
  85. TransferBatch *popBatch(CacheT *C, uptr ClassId) {
  86. DCHECK_LT(ClassId, NumClasses);
  87. RegionInfo *Region = getRegionInfo(ClassId);
  88. ScopedLock L(Region->Mutex);
  89. TransferBatch *B = Region->FreeList.front();
  90. if (B) {
  91. Region->FreeList.pop_front();
  92. } else {
  93. B = populateFreeList(C, ClassId, Region);
  94. if (UNLIKELY(!B))
  95. return nullptr;
  96. }
  97. DCHECK_GT(B->getCount(), 0);
  98. Region->Stats.PoppedBlocks += B->getCount();
  99. return B;
  100. }
  101. void pushBatch(uptr ClassId, TransferBatch *B) {
  102. DCHECK_GT(B->getCount(), 0);
  103. RegionInfo *Region = getRegionInfo(ClassId);
  104. ScopedLock L(Region->Mutex);
  105. Region->FreeList.push_front(B);
  106. Region->Stats.PushedBlocks += B->getCount();
  107. if (ClassId != SizeClassMap::BatchClassId)
  108. releaseToOSMaybe(Region, ClassId);
  109. }
  110. void disable() {
  111. // The BatchClassId must be locked last since other classes can use it.
  112. for (sptr I = static_cast<sptr>(NumClasses) - 1; I >= 0; I--) {
  113. if (static_cast<uptr>(I) == SizeClassMap::BatchClassId)
  114. continue;
  115. getRegionInfo(static_cast<uptr>(I))->Mutex.lock();
  116. }
  117. getRegionInfo(SizeClassMap::BatchClassId)->Mutex.lock();
  118. }
  119. void enable() {
  120. getRegionInfo(SizeClassMap::BatchClassId)->Mutex.unlock();
  121. for (uptr I = 0; I < NumClasses; I++) {
  122. if (I == SizeClassMap::BatchClassId)
  123. continue;
  124. getRegionInfo(I)->Mutex.unlock();
  125. }
  126. }
  127. template <typename F> void iterateOverBlocks(F Callback) {
  128. for (uptr I = 0; I < NumClasses; I++) {
  129. if (I == SizeClassMap::BatchClassId)
  130. continue;
  131. const RegionInfo *Region = getRegionInfo(I);
  132. const uptr BlockSize = getSizeByClassId(I);
  133. const uptr From = Region->RegionBeg;
  134. const uptr To = From + Region->AllocatedUser;
  135. for (uptr Block = From; Block < To; Block += BlockSize)
  136. Callback(Block);
  137. }
  138. }
  139. void getStats(ScopedString *Str) {
  140. // TODO(kostyak): get the RSS per region.
  141. uptr TotalMapped = 0;
  142. uptr PoppedBlocks = 0;
  143. uptr PushedBlocks = 0;
  144. for (uptr I = 0; I < NumClasses; I++) {
  145. RegionInfo *Region = getRegionInfo(I);
  146. if (Region->MappedUser)
  147. TotalMapped += Region->MappedUser;
  148. PoppedBlocks += Region->Stats.PoppedBlocks;
  149. PushedBlocks += Region->Stats.PushedBlocks;
  150. }
  151. Str->append("Stats: SizeClassAllocator64: %zuM mapped (%uM rss) in %zu "
  152. "allocations; remains %zu\n",
  153. TotalMapped >> 20, 0U, PoppedBlocks,
  154. PoppedBlocks - PushedBlocks);
  155. for (uptr I = 0; I < NumClasses; I++)
  156. getStats(Str, I, 0);
  157. }
  158. bool setOption(Option O, sptr Value) {
  159. if (O == Option::ReleaseInterval) {
  160. const s32 Interval = Max(
  161. Min(static_cast<s32>(Value), Config::PrimaryMaxReleaseToOsIntervalMs),
  162. Config::PrimaryMinReleaseToOsIntervalMs);
  163. atomic_store_relaxed(&ReleaseToOsIntervalMs, Interval);
  164. return true;
  165. }
  166. // Not supported by the Primary, but not an error either.
  167. return true;
  168. }
  169. uptr releaseToOS() {
  170. uptr TotalReleasedBytes = 0;
  171. for (uptr I = 0; I < NumClasses; I++) {
  172. if (I == SizeClassMap::BatchClassId)
  173. continue;
  174. RegionInfo *Region = getRegionInfo(I);
  175. ScopedLock L(Region->Mutex);
  176. TotalReleasedBytes += releaseToOSMaybe(Region, I, /*Force=*/true);
  177. }
  178. return TotalReleasedBytes;
  179. }
  180. const char *getRegionInfoArrayAddress() const {
  181. return reinterpret_cast<const char *>(RegionInfoArray);
  182. }
  183. static uptr getRegionInfoArraySize() { return sizeof(RegionInfoArray); }
  184. uptr getCompactPtrBaseByClassId(uptr ClassId) {
  185. // If we are not compacting pointers, base everything off of 0.
  186. if (sizeof(CompactPtrT) == sizeof(uptr) && CompactPtrScale == 0)
  187. return 0;
  188. return getRegionInfo(ClassId)->RegionBeg;
  189. }
  190. CompactPtrT compactPtr(uptr ClassId, uptr Ptr) {
  191. DCHECK_LE(ClassId, SizeClassMap::LargestClassId);
  192. return compactPtrInternal(getCompactPtrBaseByClassId(ClassId), Ptr);
  193. }
  194. void *decompactPtr(uptr ClassId, CompactPtrT CompactPtr) {
  195. DCHECK_LE(ClassId, SizeClassMap::LargestClassId);
  196. return reinterpret_cast<void *>(
  197. decompactPtrInternal(getCompactPtrBaseByClassId(ClassId), CompactPtr));
  198. }
  199. static BlockInfo findNearestBlock(const char *RegionInfoData, uptr Ptr) {
  200. const RegionInfo *RegionInfoArray =
  201. reinterpret_cast<const RegionInfo *>(RegionInfoData);
  202. uptr ClassId;
  203. uptr MinDistance = -1UL;
  204. for (uptr I = 0; I != NumClasses; ++I) {
  205. if (I == SizeClassMap::BatchClassId)
  206. continue;
  207. uptr Begin = RegionInfoArray[I].RegionBeg;
  208. uptr End = Begin + RegionInfoArray[I].AllocatedUser;
  209. if (Begin > End || End - Begin < SizeClassMap::getSizeByClassId(I))
  210. continue;
  211. uptr RegionDistance;
  212. if (Begin <= Ptr) {
  213. if (Ptr < End)
  214. RegionDistance = 0;
  215. else
  216. RegionDistance = Ptr - End;
  217. } else {
  218. RegionDistance = Begin - Ptr;
  219. }
  220. if (RegionDistance < MinDistance) {
  221. MinDistance = RegionDistance;
  222. ClassId = I;
  223. }
  224. }
  225. BlockInfo B = {};
  226. if (MinDistance <= 8192) {
  227. B.RegionBegin = RegionInfoArray[ClassId].RegionBeg;
  228. B.RegionEnd = B.RegionBegin + RegionInfoArray[ClassId].AllocatedUser;
  229. B.BlockSize = SizeClassMap::getSizeByClassId(ClassId);
  230. B.BlockBegin =
  231. B.RegionBegin + uptr(sptr(Ptr - B.RegionBegin) / sptr(B.BlockSize) *
  232. sptr(B.BlockSize));
  233. while (B.BlockBegin < B.RegionBegin)
  234. B.BlockBegin += B.BlockSize;
  235. while (B.RegionEnd < B.BlockBegin + B.BlockSize)
  236. B.BlockBegin -= B.BlockSize;
  237. }
  238. return B;
  239. }
  240. AtomicOptions Options;
  241. private:
  242. static const uptr RegionSize = 1UL << Config::PrimaryRegionSizeLog;
  243. static const uptr NumClasses = SizeClassMap::NumClasses;
  244. static const uptr PrimarySize = RegionSize * NumClasses;
  245. static const uptr MapSizeIncrement = Config::PrimaryMapSizeIncrement;
  246. // Fill at most this number of batches from the newly map'd memory.
  247. static const u32 MaxNumBatches = SCUDO_ANDROID ? 4U : 8U;
  248. struct RegionStats {
  249. uptr PoppedBlocks;
  250. uptr PushedBlocks;
  251. };
  252. struct ReleaseToOsInfo {
  253. uptr PushedBlocksAtLastRelease;
  254. uptr RangesReleased;
  255. uptr LastReleasedBytes;
  256. u64 LastReleaseAtNs;
  257. };
  258. struct UnpaddedRegionInfo {
  259. HybridMutex Mutex;
  260. SinglyLinkedList<TransferBatch> FreeList;
  261. uptr RegionBeg = 0;
  262. RegionStats Stats = {};
  263. u32 RandState = 0;
  264. uptr MappedUser = 0; // Bytes mapped for user memory.
  265. uptr AllocatedUser = 0; // Bytes allocated for user memory.
  266. MapPlatformData Data = {};
  267. ReleaseToOsInfo ReleaseInfo = {};
  268. bool Exhausted = false;
  269. };
  270. struct RegionInfo : UnpaddedRegionInfo {
  271. char Padding[SCUDO_CACHE_LINE_SIZE -
  272. (sizeof(UnpaddedRegionInfo) % SCUDO_CACHE_LINE_SIZE)] = {};
  273. };
  274. static_assert(sizeof(RegionInfo) % SCUDO_CACHE_LINE_SIZE == 0, "");
  275. uptr PrimaryBase = 0;
  276. MapPlatformData Data = {};
  277. atomic_s32 ReleaseToOsIntervalMs = {};
  278. alignas(SCUDO_CACHE_LINE_SIZE) RegionInfo RegionInfoArray[NumClasses];
  279. RegionInfo *getRegionInfo(uptr ClassId) {
  280. DCHECK_LT(ClassId, NumClasses);
  281. return &RegionInfoArray[ClassId];
  282. }
  283. uptr getRegionBaseByClassId(uptr ClassId) const {
  284. return PrimaryBase + (ClassId << Config::PrimaryRegionSizeLog);
  285. }
  286. static CompactPtrT compactPtrInternal(uptr Base, uptr Ptr) {
  287. return static_cast<CompactPtrT>((Ptr - Base) >> CompactPtrScale);
  288. }
  289. static uptr decompactPtrInternal(uptr Base, CompactPtrT CompactPtr) {
  290. return Base + (static_cast<uptr>(CompactPtr) << CompactPtrScale);
  291. }
  292. NOINLINE TransferBatch *populateFreeList(CacheT *C, uptr ClassId,
  293. RegionInfo *Region) {
  294. const uptr Size = getSizeByClassId(ClassId);
  295. const u32 MaxCount = TransferBatch::getMaxCached(Size);
  296. const uptr RegionBeg = Region->RegionBeg;
  297. const uptr MappedUser = Region->MappedUser;
  298. const uptr TotalUserBytes = Region->AllocatedUser + MaxCount * Size;
  299. // Map more space for blocks, if necessary.
  300. if (TotalUserBytes > MappedUser) {
  301. // Do the mmap for the user memory.
  302. const uptr MapSize =
  303. roundUpTo(TotalUserBytes - MappedUser, MapSizeIncrement);
  304. const uptr RegionBase = RegionBeg - getRegionBaseByClassId(ClassId);
  305. if (UNLIKELY(RegionBase + MappedUser + MapSize > RegionSize)) {
  306. if (!Region->Exhausted) {
  307. Region->Exhausted = true;
  308. ScopedString Str;
  309. getStats(&Str);
  310. Str.append(
  311. "Scudo OOM: The process has exhausted %zuM for size class %zu.\n",
  312. RegionSize >> 20, Size);
  313. Str.output();
  314. }
  315. return nullptr;
  316. }
  317. if (MappedUser == 0)
  318. Region->Data = Data;
  319. if (UNLIKELY(!map(
  320. reinterpret_cast<void *>(RegionBeg + MappedUser), MapSize,
  321. "scudo:primary",
  322. MAP_ALLOWNOMEM | MAP_RESIZABLE |
  323. (useMemoryTagging<Config>(Options.load()) ? MAP_MEMTAG : 0),
  324. &Region->Data)))
  325. return nullptr;
  326. Region->MappedUser += MapSize;
  327. C->getStats().add(StatMapped, MapSize);
  328. }
  329. const u32 NumberOfBlocks = Min(
  330. MaxNumBatches * MaxCount,
  331. static_cast<u32>((Region->MappedUser - Region->AllocatedUser) / Size));
  332. DCHECK_GT(NumberOfBlocks, 0);
  333. constexpr u32 ShuffleArraySize =
  334. MaxNumBatches * TransferBatch::MaxNumCached;
  335. CompactPtrT ShuffleArray[ShuffleArraySize];
  336. DCHECK_LE(NumberOfBlocks, ShuffleArraySize);
  337. const uptr CompactPtrBase = getCompactPtrBaseByClassId(ClassId);
  338. uptr P = RegionBeg + Region->AllocatedUser;
  339. for (u32 I = 0; I < NumberOfBlocks; I++, P += Size)
  340. ShuffleArray[I] = compactPtrInternal(CompactPtrBase, P);
  341. // No need to shuffle the batches size class.
  342. if (ClassId != SizeClassMap::BatchClassId)
  343. shuffle(ShuffleArray, NumberOfBlocks, &Region->RandState);
  344. for (u32 I = 0; I < NumberOfBlocks;) {
  345. TransferBatch *B =
  346. C->createBatch(ClassId, reinterpret_cast<void *>(decompactPtrInternal(
  347. CompactPtrBase, ShuffleArray[I])));
  348. if (UNLIKELY(!B))
  349. return nullptr;
  350. const u32 N = Min(MaxCount, NumberOfBlocks - I);
  351. B->setFromArray(&ShuffleArray[I], N);
  352. Region->FreeList.push_back(B);
  353. I += N;
  354. }
  355. TransferBatch *B = Region->FreeList.front();
  356. Region->FreeList.pop_front();
  357. DCHECK(B);
  358. DCHECK_GT(B->getCount(), 0);
  359. const uptr AllocatedUser = Size * NumberOfBlocks;
  360. C->getStats().add(StatFree, AllocatedUser);
  361. Region->AllocatedUser += AllocatedUser;
  362. return B;
  363. }
  364. void getStats(ScopedString *Str, uptr ClassId, uptr Rss) {
  365. RegionInfo *Region = getRegionInfo(ClassId);
  366. if (Region->MappedUser == 0)
  367. return;
  368. const uptr InUse = Region->Stats.PoppedBlocks - Region->Stats.PushedBlocks;
  369. const uptr TotalChunks = Region->AllocatedUser / getSizeByClassId(ClassId);
  370. Str->append("%s %02zu (%6zu): mapped: %6zuK popped: %7zu pushed: %7zu "
  371. "inuse: %6zu total: %6zu rss: %6zuK releases: %6zu last "
  372. "released: %6zuK region: 0x%zx (0x%zx)\n",
  373. Region->Exhausted ? "F" : " ", ClassId,
  374. getSizeByClassId(ClassId), Region->MappedUser >> 10,
  375. Region->Stats.PoppedBlocks, Region->Stats.PushedBlocks, InUse,
  376. TotalChunks, Rss >> 10, Region->ReleaseInfo.RangesReleased,
  377. Region->ReleaseInfo.LastReleasedBytes >> 10, Region->RegionBeg,
  378. getRegionBaseByClassId(ClassId));
  379. }
  380. NOINLINE uptr releaseToOSMaybe(RegionInfo *Region, uptr ClassId,
  381. bool Force = false) {
  382. const uptr BlockSize = getSizeByClassId(ClassId);
  383. const uptr PageSize = getPageSizeCached();
  384. DCHECK_GE(Region->Stats.PoppedBlocks, Region->Stats.PushedBlocks);
  385. const uptr BytesInFreeList =
  386. Region->AllocatedUser -
  387. (Region->Stats.PoppedBlocks - Region->Stats.PushedBlocks) * BlockSize;
  388. if (BytesInFreeList < PageSize)
  389. return 0; // No chance to release anything.
  390. const uptr BytesPushed = (Region->Stats.PushedBlocks -
  391. Region->ReleaseInfo.PushedBlocksAtLastRelease) *
  392. BlockSize;
  393. if (BytesPushed < PageSize)
  394. return 0; // Nothing new to release.
  395. // Releasing smaller blocks is expensive, so we want to make sure that a
  396. // significant amount of bytes are free, and that there has been a good
  397. // amount of batches pushed to the freelist before attempting to release.
  398. if (BlockSize < PageSize / 16U) {
  399. if (!Force && BytesPushed < Region->AllocatedUser / 16U)
  400. return 0;
  401. // We want 8x% to 9x% free bytes (the larger the block, the lower the %).
  402. if ((BytesInFreeList * 100U) / Region->AllocatedUser <
  403. (100U - 1U - BlockSize / 16U))
  404. return 0;
  405. }
  406. if (!Force) {
  407. const s32 IntervalMs = atomic_load_relaxed(&ReleaseToOsIntervalMs);
  408. if (IntervalMs < 0)
  409. return 0;
  410. if (Region->ReleaseInfo.LastReleaseAtNs +
  411. static_cast<u64>(IntervalMs) * 1000000 >
  412. getMonotonicTime()) {
  413. return 0; // Memory was returned recently.
  414. }
  415. }
  416. ReleaseRecorder Recorder(Region->RegionBeg, &Region->Data);
  417. const uptr CompactPtrBase = getCompactPtrBaseByClassId(ClassId);
  418. auto DecompactPtr = [CompactPtrBase](CompactPtrT CompactPtr) {
  419. return decompactPtrInternal(CompactPtrBase, CompactPtr);
  420. };
  421. auto SkipRegion = [](UNUSED uptr RegionIndex) { return false; };
  422. releaseFreeMemoryToOS(Region->FreeList, Region->AllocatedUser, 1U,
  423. BlockSize, &Recorder, DecompactPtr, SkipRegion);
  424. if (Recorder.getReleasedRangesCount() > 0) {
  425. Region->ReleaseInfo.PushedBlocksAtLastRelease =
  426. Region->Stats.PushedBlocks;
  427. Region->ReleaseInfo.RangesReleased += Recorder.getReleasedRangesCount();
  428. Region->ReleaseInfo.LastReleasedBytes = Recorder.getReleasedBytes();
  429. }
  430. Region->ReleaseInfo.LastReleaseAtNs = getMonotonicTime();
  431. return Recorder.getReleasedBytes();
  432. }
  433. };
  434. } // namespace scudo
  435. #endif // SCUDO_PRIMARY64_H_