primary64.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. static const uptr GroupSizeLog = Config::PrimaryGroupSizeLog;
  44. typedef typename Config::SizeClassMap SizeClassMap;
  45. typedef SizeClassAllocator64<Config> ThisT;
  46. typedef SizeClassAllocatorLocalCache<ThisT> CacheT;
  47. typedef typename CacheT::TransferBatch TransferBatch;
  48. typedef typename CacheT::BatchGroup BatchGroup;
  49. static uptr getSizeByClassId(uptr ClassId) {
  50. return (ClassId == SizeClassMap::BatchClassId)
  51. ? roundUpTo(sizeof(TransferBatch), 1U << CompactPtrScale)
  52. : SizeClassMap::getSizeByClassId(ClassId);
  53. }
  54. static bool canAllocate(uptr Size) { return Size <= SizeClassMap::MaxSize; }
  55. void init(s32 ReleaseToOsInterval) {
  56. DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
  57. DCHECK_EQ(PrimaryBase, 0U);
  58. // Reserve the space required for the Primary.
  59. PrimaryBase = reinterpret_cast<uptr>(
  60. map(nullptr, PrimarySize, nullptr, MAP_NOACCESS, &Data));
  61. u32 Seed;
  62. const u64 Time = getMonotonicTime();
  63. if (!getRandom(reinterpret_cast<void *>(&Seed), sizeof(Seed)))
  64. Seed = static_cast<u32>(Time ^ (PrimaryBase >> 12));
  65. const uptr PageSize = getPageSizeCached();
  66. for (uptr I = 0; I < NumClasses; I++) {
  67. RegionInfo *Region = getRegionInfo(I);
  68. // The actual start of a region is offset by a random number of pages
  69. // when PrimaryEnableRandomOffset is set.
  70. Region->RegionBeg = getRegionBaseByClassId(I) +
  71. (Config::PrimaryEnableRandomOffset
  72. ? ((getRandomModN(&Seed, 16) + 1) * PageSize)
  73. : 0);
  74. Region->RandState = getRandomU32(&Seed);
  75. Region->ReleaseInfo.LastReleaseAtNs = Time;
  76. }
  77. setOption(Option::ReleaseInterval, static_cast<sptr>(ReleaseToOsInterval));
  78. }
  79. void unmapTestOnly() {
  80. for (uptr I = 0; I < NumClasses; I++) {
  81. RegionInfo *Region = getRegionInfo(I);
  82. *Region = {};
  83. }
  84. if (PrimaryBase)
  85. unmap(reinterpret_cast<void *>(PrimaryBase), PrimarySize, UNMAP_ALL,
  86. &Data);
  87. PrimaryBase = 0U;
  88. }
  89. TransferBatch *popBatch(CacheT *C, uptr ClassId) {
  90. DCHECK_LT(ClassId, NumClasses);
  91. RegionInfo *Region = getRegionInfo(ClassId);
  92. ScopedLock L(Region->Mutex);
  93. TransferBatch *B = popBatchImpl(C, ClassId);
  94. if (UNLIKELY(!B)) {
  95. if (UNLIKELY(!populateFreeList(C, ClassId, Region)))
  96. return nullptr;
  97. B = popBatchImpl(C, ClassId);
  98. // if `populateFreeList` succeeded, we are supposed to get free blocks.
  99. DCHECK_NE(B, nullptr);
  100. }
  101. Region->Stats.PoppedBlocks += B->getCount();
  102. return B;
  103. }
  104. // Push the array of free blocks to the designated batch group.
  105. void pushBlocks(CacheT *C, uptr ClassId, CompactPtrT *Array, u32 Size) {
  106. DCHECK_LT(ClassId, NumClasses);
  107. DCHECK_GT(Size, 0);
  108. RegionInfo *Region = getRegionInfo(ClassId);
  109. if (ClassId == SizeClassMap::BatchClassId) {
  110. ScopedLock L(Region->Mutex);
  111. // Constructing a batch group in the free list will use two blocks in
  112. // BatchClassId. If we are pushing BatchClassId blocks, we will use the
  113. // blocks in the array directly (can't delegate local cache which will
  114. // cause a recursive allocation). However, The number of free blocks may
  115. // be less than two. Therefore, populate the free list before inserting
  116. // the blocks.
  117. if (Size == 1 && UNLIKELY(!populateFreeList(C, ClassId, Region)))
  118. return;
  119. pushBlocksImpl(C, ClassId, Array, Size);
  120. Region->Stats.PushedBlocks += Size;
  121. return;
  122. }
  123. // TODO(chiahungduan): Consider not doing grouping if the group size is not
  124. // greater than the block size with a certain scale.
  125. // Sort the blocks so that blocks belonging to the same group can be pushed
  126. // together.
  127. bool SameGroup = true;
  128. for (u32 I = 1; I < Size; ++I) {
  129. if (compactPtrGroup(Array[I - 1]) != compactPtrGroup(Array[I]))
  130. SameGroup = false;
  131. CompactPtrT Cur = Array[I];
  132. u32 J = I;
  133. while (J > 0 && compactPtrGroup(Cur) < compactPtrGroup(Array[J - 1])) {
  134. Array[J] = Array[J - 1];
  135. --J;
  136. }
  137. Array[J] = Cur;
  138. }
  139. ScopedLock L(Region->Mutex);
  140. pushBlocksImpl(C, ClassId, Array, Size, SameGroup);
  141. Region->Stats.PushedBlocks += Size;
  142. if (ClassId != SizeClassMap::BatchClassId)
  143. releaseToOSMaybe(Region, ClassId);
  144. }
  145. void disable() {
  146. // The BatchClassId must be locked last since other classes can use it.
  147. for (sptr I = static_cast<sptr>(NumClasses) - 1; I >= 0; I--) {
  148. if (static_cast<uptr>(I) == SizeClassMap::BatchClassId)
  149. continue;
  150. getRegionInfo(static_cast<uptr>(I))->Mutex.lock();
  151. }
  152. getRegionInfo(SizeClassMap::BatchClassId)->Mutex.lock();
  153. }
  154. void enable() {
  155. getRegionInfo(SizeClassMap::BatchClassId)->Mutex.unlock();
  156. for (uptr I = 0; I < NumClasses; I++) {
  157. if (I == SizeClassMap::BatchClassId)
  158. continue;
  159. getRegionInfo(I)->Mutex.unlock();
  160. }
  161. }
  162. template <typename F> void iterateOverBlocks(F Callback) {
  163. for (uptr I = 0; I < NumClasses; I++) {
  164. if (I == SizeClassMap::BatchClassId)
  165. continue;
  166. const RegionInfo *Region = getRegionInfo(I);
  167. const uptr BlockSize = getSizeByClassId(I);
  168. const uptr From = Region->RegionBeg;
  169. const uptr To = From + Region->AllocatedUser;
  170. for (uptr Block = From; Block < To; Block += BlockSize)
  171. Callback(Block);
  172. }
  173. }
  174. void getStats(ScopedString *Str) {
  175. // TODO(kostyak): get the RSS per region.
  176. uptr TotalMapped = 0;
  177. uptr PoppedBlocks = 0;
  178. uptr PushedBlocks = 0;
  179. for (uptr I = 0; I < NumClasses; I++) {
  180. RegionInfo *Region = getRegionInfo(I);
  181. if (Region->MappedUser)
  182. TotalMapped += Region->MappedUser;
  183. PoppedBlocks += Region->Stats.PoppedBlocks;
  184. PushedBlocks += Region->Stats.PushedBlocks;
  185. }
  186. Str->append("Stats: SizeClassAllocator64: %zuM mapped (%uM rss) in %zu "
  187. "allocations; remains %zu\n",
  188. TotalMapped >> 20, 0U, PoppedBlocks,
  189. PoppedBlocks - PushedBlocks);
  190. for (uptr I = 0; I < NumClasses; I++)
  191. getStats(Str, I, 0);
  192. }
  193. bool setOption(Option O, sptr Value) {
  194. if (O == Option::ReleaseInterval) {
  195. const s32 Interval = Max(
  196. Min(static_cast<s32>(Value), Config::PrimaryMaxReleaseToOsIntervalMs),
  197. Config::PrimaryMinReleaseToOsIntervalMs);
  198. atomic_store_relaxed(&ReleaseToOsIntervalMs, Interval);
  199. return true;
  200. }
  201. // Not supported by the Primary, but not an error either.
  202. return true;
  203. }
  204. uptr releaseToOS() {
  205. uptr TotalReleasedBytes = 0;
  206. for (uptr I = 0; I < NumClasses; I++) {
  207. if (I == SizeClassMap::BatchClassId)
  208. continue;
  209. RegionInfo *Region = getRegionInfo(I);
  210. ScopedLock L(Region->Mutex);
  211. TotalReleasedBytes += releaseToOSMaybe(Region, I, /*Force=*/true);
  212. }
  213. return TotalReleasedBytes;
  214. }
  215. const char *getRegionInfoArrayAddress() const {
  216. return reinterpret_cast<const char *>(RegionInfoArray);
  217. }
  218. static uptr getRegionInfoArraySize() { return sizeof(RegionInfoArray); }
  219. uptr getCompactPtrBaseByClassId(uptr ClassId) {
  220. // If we are not compacting pointers, base everything off of 0.
  221. if (sizeof(CompactPtrT) == sizeof(uptr) && CompactPtrScale == 0)
  222. return 0;
  223. return getRegionInfo(ClassId)->RegionBeg;
  224. }
  225. CompactPtrT compactPtr(uptr ClassId, uptr Ptr) {
  226. DCHECK_LE(ClassId, SizeClassMap::LargestClassId);
  227. return compactPtrInternal(getCompactPtrBaseByClassId(ClassId), Ptr);
  228. }
  229. void *decompactPtr(uptr ClassId, CompactPtrT CompactPtr) {
  230. DCHECK_LE(ClassId, SizeClassMap::LargestClassId);
  231. return reinterpret_cast<void *>(
  232. decompactPtrInternal(getCompactPtrBaseByClassId(ClassId), CompactPtr));
  233. }
  234. static BlockInfo findNearestBlock(const char *RegionInfoData, uptr Ptr) {
  235. const RegionInfo *RegionInfoArray =
  236. reinterpret_cast<const RegionInfo *>(RegionInfoData);
  237. uptr ClassId;
  238. uptr MinDistance = -1UL;
  239. for (uptr I = 0; I != NumClasses; ++I) {
  240. if (I == SizeClassMap::BatchClassId)
  241. continue;
  242. uptr Begin = RegionInfoArray[I].RegionBeg;
  243. uptr End = Begin + RegionInfoArray[I].AllocatedUser;
  244. if (Begin > End || End - Begin < SizeClassMap::getSizeByClassId(I))
  245. continue;
  246. uptr RegionDistance;
  247. if (Begin <= Ptr) {
  248. if (Ptr < End)
  249. RegionDistance = 0;
  250. else
  251. RegionDistance = Ptr - End;
  252. } else {
  253. RegionDistance = Begin - Ptr;
  254. }
  255. if (RegionDistance < MinDistance) {
  256. MinDistance = RegionDistance;
  257. ClassId = I;
  258. }
  259. }
  260. BlockInfo B = {};
  261. if (MinDistance <= 8192) {
  262. B.RegionBegin = RegionInfoArray[ClassId].RegionBeg;
  263. B.RegionEnd = B.RegionBegin + RegionInfoArray[ClassId].AllocatedUser;
  264. B.BlockSize = SizeClassMap::getSizeByClassId(ClassId);
  265. B.BlockBegin =
  266. B.RegionBegin + uptr(sptr(Ptr - B.RegionBegin) / sptr(B.BlockSize) *
  267. sptr(B.BlockSize));
  268. while (B.BlockBegin < B.RegionBegin)
  269. B.BlockBegin += B.BlockSize;
  270. while (B.RegionEnd < B.BlockBegin + B.BlockSize)
  271. B.BlockBegin -= B.BlockSize;
  272. }
  273. return B;
  274. }
  275. AtomicOptions Options;
  276. private:
  277. static const uptr RegionSize = 1UL << Config::PrimaryRegionSizeLog;
  278. static const uptr NumClasses = SizeClassMap::NumClasses;
  279. static const uptr PrimarySize = RegionSize * NumClasses;
  280. static const uptr MapSizeIncrement = Config::PrimaryMapSizeIncrement;
  281. // Fill at most this number of batches from the newly map'd memory.
  282. static const u32 MaxNumBatches = SCUDO_ANDROID ? 4U : 8U;
  283. struct RegionStats {
  284. uptr PoppedBlocks;
  285. uptr PushedBlocks;
  286. };
  287. struct ReleaseToOsInfo {
  288. uptr PushedBlocksAtLastRelease;
  289. uptr RangesReleased;
  290. uptr LastReleasedBytes;
  291. u64 LastReleaseAtNs;
  292. };
  293. struct UnpaddedRegionInfo {
  294. HybridMutex Mutex;
  295. SinglyLinkedList<BatchGroup> FreeList;
  296. uptr RegionBeg = 0;
  297. RegionStats Stats = {};
  298. u32 RandState = 0;
  299. uptr MappedUser = 0; // Bytes mapped for user memory.
  300. uptr AllocatedUser = 0; // Bytes allocated for user memory.
  301. MapPlatformData Data = {};
  302. ReleaseToOsInfo ReleaseInfo = {};
  303. bool Exhausted = false;
  304. };
  305. struct RegionInfo : UnpaddedRegionInfo {
  306. char Padding[SCUDO_CACHE_LINE_SIZE -
  307. (sizeof(UnpaddedRegionInfo) % SCUDO_CACHE_LINE_SIZE)] = {};
  308. };
  309. static_assert(sizeof(RegionInfo) % SCUDO_CACHE_LINE_SIZE == 0, "");
  310. uptr PrimaryBase = 0;
  311. MapPlatformData Data = {};
  312. atomic_s32 ReleaseToOsIntervalMs = {};
  313. alignas(SCUDO_CACHE_LINE_SIZE) RegionInfo RegionInfoArray[NumClasses];
  314. RegionInfo *getRegionInfo(uptr ClassId) {
  315. DCHECK_LT(ClassId, NumClasses);
  316. return &RegionInfoArray[ClassId];
  317. }
  318. uptr getRegionBaseByClassId(uptr ClassId) const {
  319. return PrimaryBase + (ClassId << Config::PrimaryRegionSizeLog);
  320. }
  321. static CompactPtrT compactPtrInternal(uptr Base, uptr Ptr) {
  322. return static_cast<CompactPtrT>((Ptr - Base) >> CompactPtrScale);
  323. }
  324. static uptr decompactPtrInternal(uptr Base, CompactPtrT CompactPtr) {
  325. return Base + (static_cast<uptr>(CompactPtr) << CompactPtrScale);
  326. }
  327. static uptr compactPtrGroup(CompactPtrT CompactPtr) {
  328. return static_cast<uptr>(CompactPtr) >> (GroupSizeLog - CompactPtrScale);
  329. }
  330. static uptr batchGroupBase(uptr Base, uptr GroupId) {
  331. return (GroupId << GroupSizeLog) + Base;
  332. }
  333. // Push the blocks to their batch group. The layout will be like,
  334. //
  335. // FreeList - > BG -> BG -> BG
  336. // | | |
  337. // v v v
  338. // TB TB TB
  339. // |
  340. // v
  341. // TB
  342. //
  343. // Each BlockGroup(BG) will associate with unique group id and the free blocks
  344. // are managed by a list of TransferBatch(TB). To reduce the time of inserting
  345. // blocks, BGs are sorted and the input `Array` are supposed to be sorted so
  346. // that we can get better performance of maintaining sorted property.
  347. // Use `SameGroup=true` to indicate that all blocks in the array are from the
  348. // same group then we will skip checking the group id of each block.
  349. //
  350. // Note that this aims to have a better management of dirty pages, i.e., the
  351. // RSS usage won't grow indefinitely. There's an exception that we may not put
  352. // a block to its associated group. While populating new blocks, we may have
  353. // blocks cross different groups. However, most cases will fall into same
  354. // group and they are supposed to be popped soon. In that case, it's not worth
  355. // sorting the array with the almost-sorted property. Therefore, we use
  356. // `SameGroup=true` instead.
  357. //
  358. // The region mutex needs to be held while calling this method.
  359. void pushBlocksImpl(CacheT *C, uptr ClassId, CompactPtrT *Array, u32 Size,
  360. bool SameGroup = false) {
  361. DCHECK_GT(Size, 0U);
  362. RegionInfo *Region = getRegionInfo(ClassId);
  363. auto CreateGroup = [&](uptr GroupId) {
  364. BatchGroup *BG = nullptr;
  365. TransferBatch *TB = nullptr;
  366. if (ClassId == SizeClassMap::BatchClassId) {
  367. DCHECK_GE(Size, 2U);
  368. BG = reinterpret_cast<BatchGroup *>(
  369. decompactPtr(ClassId, Array[Size - 1]));
  370. BG->Batches.clear();
  371. TB = reinterpret_cast<TransferBatch *>(
  372. decompactPtr(ClassId, Array[Size - 2]));
  373. TB->clear();
  374. } else {
  375. BG = C->createGroup();
  376. BG->Batches.clear();
  377. TB = C->createBatch(ClassId, nullptr);
  378. TB->clear();
  379. }
  380. BG->GroupId = GroupId;
  381. BG->Batches.push_front(TB);
  382. BG->PushedBlocks = 0;
  383. BG->PushedBlocksAtLastCheckpoint = 0;
  384. BG->MaxCachedPerBatch =
  385. TransferBatch::getMaxCached(getSizeByClassId(ClassId));
  386. return BG;
  387. };
  388. auto InsertBlocks = [&](BatchGroup *BG, CompactPtrT *Array, u32 Size) {
  389. SinglyLinkedList<TransferBatch> &Batches = BG->Batches;
  390. TransferBatch *CurBatch = Batches.front();
  391. DCHECK_NE(CurBatch, nullptr);
  392. for (u32 I = 0; I < Size;) {
  393. DCHECK_GE(BG->MaxCachedPerBatch, CurBatch->getCount());
  394. u16 UnusedSlots =
  395. static_cast<u16>(BG->MaxCachedPerBatch - CurBatch->getCount());
  396. if (UnusedSlots == 0) {
  397. CurBatch = C->createBatch(
  398. ClassId,
  399. reinterpret_cast<void *>(decompactPtr(ClassId, Array[I])));
  400. CurBatch->clear();
  401. Batches.push_front(CurBatch);
  402. UnusedSlots = BG->MaxCachedPerBatch;
  403. }
  404. // `UnusedSlots` is u16 so the result will be also fit in u16.
  405. u16 AppendSize = static_cast<u16>(Min<u32>(UnusedSlots, Size - I));
  406. CurBatch->appendFromArray(&Array[I], AppendSize);
  407. I += AppendSize;
  408. }
  409. BG->PushedBlocks += Size;
  410. };
  411. BatchGroup *Cur = Region->FreeList.front();
  412. if (ClassId == SizeClassMap::BatchClassId) {
  413. if (Cur == nullptr) {
  414. // Don't need to classify BatchClassId.
  415. Cur = CreateGroup(/*GroupId=*/0);
  416. Region->FreeList.push_front(Cur);
  417. }
  418. InsertBlocks(Cur, Array, Size);
  419. return;
  420. }
  421. // In the following, `Cur` always points to the BatchGroup for blocks that
  422. // will be pushed next. `Prev` is the element right before `Cur`.
  423. BatchGroup *Prev = nullptr;
  424. while (Cur != nullptr && compactPtrGroup(Array[0]) > Cur->GroupId) {
  425. Prev = Cur;
  426. Cur = Cur->Next;
  427. }
  428. if (Cur == nullptr || compactPtrGroup(Array[0]) != Cur->GroupId) {
  429. Cur = CreateGroup(compactPtrGroup(Array[0]));
  430. if (Prev == nullptr)
  431. Region->FreeList.push_front(Cur);
  432. else
  433. Region->FreeList.insert(Prev, Cur);
  434. }
  435. // All the blocks are from the same group, just push without checking group
  436. // id.
  437. if (SameGroup) {
  438. InsertBlocks(Cur, Array, Size);
  439. return;
  440. }
  441. // The blocks are sorted by group id. Determine the segment of group and
  442. // push them to their group together.
  443. u32 Count = 1;
  444. for (u32 I = 1; I < Size; ++I) {
  445. if (compactPtrGroup(Array[I - 1]) != compactPtrGroup(Array[I])) {
  446. DCHECK_EQ(compactPtrGroup(Array[I - 1]), Cur->GroupId);
  447. InsertBlocks(Cur, Array + I - Count, Count);
  448. while (Cur != nullptr && compactPtrGroup(Array[I]) > Cur->GroupId) {
  449. Prev = Cur;
  450. Cur = Cur->Next;
  451. }
  452. if (Cur == nullptr || compactPtrGroup(Array[I]) != Cur->GroupId) {
  453. Cur = CreateGroup(compactPtrGroup(Array[I]));
  454. DCHECK_NE(Prev, nullptr);
  455. Region->FreeList.insert(Prev, Cur);
  456. }
  457. Count = 1;
  458. } else {
  459. ++Count;
  460. }
  461. }
  462. InsertBlocks(Cur, Array + Size - Count, Count);
  463. }
  464. // Pop one TransferBatch from a BatchGroup. The BatchGroup with the smallest
  465. // group id will be considered first.
  466. //
  467. // The region mutex needs to be held while calling this method.
  468. TransferBatch *popBatchImpl(CacheT *C, uptr ClassId) {
  469. RegionInfo *Region = getRegionInfo(ClassId);
  470. if (Region->FreeList.empty())
  471. return nullptr;
  472. SinglyLinkedList<TransferBatch> &Batches =
  473. Region->FreeList.front()->Batches;
  474. DCHECK(!Batches.empty());
  475. TransferBatch *B = Batches.front();
  476. Batches.pop_front();
  477. DCHECK_NE(B, nullptr);
  478. DCHECK_GT(B->getCount(), 0U);
  479. if (Batches.empty()) {
  480. BatchGroup *BG = Region->FreeList.front();
  481. Region->FreeList.pop_front();
  482. // We don't keep BatchGroup with zero blocks to avoid empty-checking while
  483. // allocating. Note that block used by constructing BatchGroup is recorded
  484. // as free blocks in the last element of BatchGroup::Batches. Which means,
  485. // once we pop the last TransferBatch, the block is implicitly
  486. // deallocated.
  487. if (ClassId != SizeClassMap::BatchClassId)
  488. C->deallocate(SizeClassMap::BatchClassId, BG);
  489. }
  490. return B;
  491. }
  492. NOINLINE bool populateFreeList(CacheT *C, uptr ClassId, RegionInfo *Region) {
  493. const uptr Size = getSizeByClassId(ClassId);
  494. const u16 MaxCount = TransferBatch::getMaxCached(Size);
  495. const uptr RegionBeg = Region->RegionBeg;
  496. const uptr MappedUser = Region->MappedUser;
  497. const uptr TotalUserBytes = Region->AllocatedUser + MaxCount * Size;
  498. // Map more space for blocks, if necessary.
  499. if (TotalUserBytes > MappedUser) {
  500. // Do the mmap for the user memory.
  501. const uptr MapSize =
  502. roundUpTo(TotalUserBytes - MappedUser, MapSizeIncrement);
  503. const uptr RegionBase = RegionBeg - getRegionBaseByClassId(ClassId);
  504. if (UNLIKELY(RegionBase + MappedUser + MapSize > RegionSize)) {
  505. if (!Region->Exhausted) {
  506. Region->Exhausted = true;
  507. ScopedString Str;
  508. getStats(&Str);
  509. Str.append(
  510. "Scudo OOM: The process has exhausted %zuM for size class %zu.\n",
  511. RegionSize >> 20, Size);
  512. Str.output();
  513. }
  514. return false;
  515. }
  516. if (MappedUser == 0)
  517. Region->Data = Data;
  518. if (UNLIKELY(!map(
  519. reinterpret_cast<void *>(RegionBeg + MappedUser), MapSize,
  520. "scudo:primary",
  521. MAP_ALLOWNOMEM | MAP_RESIZABLE |
  522. (useMemoryTagging<Config>(Options.load()) ? MAP_MEMTAG : 0),
  523. &Region->Data))) {
  524. return false;
  525. }
  526. Region->MappedUser += MapSize;
  527. C->getStats().add(StatMapped, MapSize);
  528. }
  529. const u32 NumberOfBlocks = Min(
  530. MaxNumBatches * MaxCount,
  531. static_cast<u32>((Region->MappedUser - Region->AllocatedUser) / Size));
  532. DCHECK_GT(NumberOfBlocks, 0);
  533. constexpr u32 ShuffleArraySize =
  534. MaxNumBatches * TransferBatch::MaxNumCached;
  535. CompactPtrT ShuffleArray[ShuffleArraySize];
  536. DCHECK_LE(NumberOfBlocks, ShuffleArraySize);
  537. const uptr CompactPtrBase = getCompactPtrBaseByClassId(ClassId);
  538. uptr P = RegionBeg + Region->AllocatedUser;
  539. for (u32 I = 0; I < NumberOfBlocks; I++, P += Size)
  540. ShuffleArray[I] = compactPtrInternal(CompactPtrBase, P);
  541. // No need to shuffle the batches size class.
  542. if (ClassId != SizeClassMap::BatchClassId)
  543. shuffle(ShuffleArray, NumberOfBlocks, &Region->RandState);
  544. for (u32 I = 0; I < NumberOfBlocks;) {
  545. // `MaxCount` is u16 so the result will also fit in u16.
  546. const u16 N = static_cast<u16>(Min<u32>(MaxCount, NumberOfBlocks - I));
  547. // Note that the N blocks here may have different group ids. Given that
  548. // it only happens when it crosses the group size boundary. Instead of
  549. // sorting them, treat them as same group here to avoid sorting the
  550. // almost-sorted blocks.
  551. pushBlocksImpl(C, ClassId, &ShuffleArray[I], N, /*SameGroup=*/true);
  552. I += N;
  553. }
  554. const uptr AllocatedUser = Size * NumberOfBlocks;
  555. C->getStats().add(StatFree, AllocatedUser);
  556. Region->AllocatedUser += AllocatedUser;
  557. return true;
  558. }
  559. void getStats(ScopedString *Str, uptr ClassId, uptr Rss) {
  560. RegionInfo *Region = getRegionInfo(ClassId);
  561. if (Region->MappedUser == 0)
  562. return;
  563. const uptr InUse = Region->Stats.PoppedBlocks - Region->Stats.PushedBlocks;
  564. const uptr TotalChunks = Region->AllocatedUser / getSizeByClassId(ClassId);
  565. Str->append("%s %02zu (%6zu): mapped: %6zuK popped: %7zu pushed: %7zu "
  566. "inuse: %6zu total: %6zu rss: %6zuK releases: %6zu last "
  567. "released: %6zuK region: 0x%zx (0x%zx)\n",
  568. Region->Exhausted ? "F" : " ", ClassId,
  569. getSizeByClassId(ClassId), Region->MappedUser >> 10,
  570. Region->Stats.PoppedBlocks, Region->Stats.PushedBlocks, InUse,
  571. TotalChunks, Rss >> 10, Region->ReleaseInfo.RangesReleased,
  572. Region->ReleaseInfo.LastReleasedBytes >> 10, Region->RegionBeg,
  573. getRegionBaseByClassId(ClassId));
  574. }
  575. NOINLINE uptr releaseToOSMaybe(RegionInfo *Region, uptr ClassId,
  576. bool Force = false) {
  577. const uptr BlockSize = getSizeByClassId(ClassId);
  578. const uptr PageSize = getPageSizeCached();
  579. DCHECK_GE(Region->Stats.PoppedBlocks, Region->Stats.PushedBlocks);
  580. const uptr BytesInFreeList =
  581. Region->AllocatedUser -
  582. (Region->Stats.PoppedBlocks - Region->Stats.PushedBlocks) * BlockSize;
  583. if (BytesInFreeList < PageSize)
  584. return 0; // No chance to release anything.
  585. const uptr BytesPushed = (Region->Stats.PushedBlocks -
  586. Region->ReleaseInfo.PushedBlocksAtLastRelease) *
  587. BlockSize;
  588. if (BytesPushed < PageSize)
  589. return 0; // Nothing new to release.
  590. bool CheckDensity = BlockSize < PageSize / 16U;
  591. // Releasing smaller blocks is expensive, so we want to make sure that a
  592. // significant amount of bytes are free, and that there has been a good
  593. // amount of batches pushed to the freelist before attempting to release.
  594. if (CheckDensity) {
  595. if (!Force && BytesPushed < Region->AllocatedUser / 16U)
  596. return 0;
  597. }
  598. if (!Force) {
  599. const s32 IntervalMs = atomic_load_relaxed(&ReleaseToOsIntervalMs);
  600. if (IntervalMs < 0)
  601. return 0;
  602. if (Region->ReleaseInfo.LastReleaseAtNs +
  603. static_cast<u64>(IntervalMs) * 1000000 >
  604. getMonotonicTime()) {
  605. return 0; // Memory was returned recently.
  606. }
  607. }
  608. const uptr GroupSize = (1U << GroupSizeLog);
  609. const uptr AllocatedUserEnd = Region->AllocatedUser + Region->RegionBeg;
  610. ReleaseRecorder Recorder(Region->RegionBeg, &Region->Data);
  611. PageReleaseContext Context(BlockSize, Region->AllocatedUser,
  612. /*NumberOfRegions=*/1U);
  613. const uptr CompactPtrBase = getCompactPtrBaseByClassId(ClassId);
  614. auto DecompactPtr = [CompactPtrBase](CompactPtrT CompactPtr) {
  615. return decompactPtrInternal(CompactPtrBase, CompactPtr);
  616. };
  617. for (BatchGroup &BG : Region->FreeList) {
  618. const uptr PushedBytesDelta =
  619. BG.PushedBlocks - BG.PushedBlocksAtLastCheckpoint;
  620. if (PushedBytesDelta * BlockSize < PageSize)
  621. continue;
  622. // Group boundary does not necessarily have the same alignment as Region.
  623. // It may sit across a Region boundary. Which means that we may have the
  624. // following two cases,
  625. //
  626. // 1. Group boundary sits before RegionBeg.
  627. //
  628. // (BatchGroupBeg)
  629. // batchGroupBase RegionBeg BatchGroupEnd
  630. // | | |
  631. // v v v
  632. // +------------+----------------+
  633. // \ /
  634. // ------ GroupSize ------
  635. //
  636. // 2. Group boundary sits after RegionBeg.
  637. //
  638. // (BatchGroupBeg)
  639. // RegionBeg batchGroupBase BatchGroupEnd
  640. // | | |
  641. // v v v
  642. // +-----------+-----------------------------+
  643. // \ /
  644. // ------ GroupSize ------
  645. //
  646. // Note that in the first case, the group range before RegionBeg is never
  647. // used. Therefore, while calculating the used group size, we should
  648. // exclude that part to get the correct size.
  649. const uptr BatchGroupBeg =
  650. Max(batchGroupBase(CompactPtrBase, BG.GroupId), Region->RegionBeg);
  651. DCHECK_GE(AllocatedUserEnd, BatchGroupBeg);
  652. const uptr BatchGroupEnd =
  653. batchGroupBase(CompactPtrBase, BG.GroupId) + GroupSize;
  654. const uptr AllocatedGroupSize = AllocatedUserEnd >= BatchGroupEnd
  655. ? BatchGroupEnd - BatchGroupBeg
  656. : AllocatedUserEnd - BatchGroupBeg;
  657. if (AllocatedGroupSize == 0)
  658. continue;
  659. // TransferBatches are pushed in front of BG.Batches. The first one may
  660. // not have all caches used.
  661. const uptr NumBlocks = (BG.Batches.size() - 1) * BG.MaxCachedPerBatch +
  662. BG.Batches.front()->getCount();
  663. const uptr BytesInBG = NumBlocks * BlockSize;
  664. // Given the randomness property, we try to release the pages only if the
  665. // bytes used by free blocks exceed certain proportion of group size. Note
  666. // that this heuristic only applies when all the spaces in a BatchGroup
  667. // are allocated.
  668. if (CheckDensity && (BytesInBG * 100U) / AllocatedGroupSize <
  669. (100U - 1U - BlockSize / 16U)) {
  670. continue;
  671. }
  672. BG.PushedBlocksAtLastCheckpoint = BG.PushedBlocks;
  673. // Note that we don't always visit blocks in each BatchGroup so that we
  674. // may miss the chance of releasing certain pages that cross BatchGroups.
  675. Context.markFreeBlocks(BG.Batches, DecompactPtr, Region->RegionBeg);
  676. }
  677. if (!Context.hasBlockMarked())
  678. return 0;
  679. auto SkipRegion = [](UNUSED uptr RegionIndex) { return false; };
  680. releaseFreeMemoryToOS(Context, Recorder, SkipRegion);
  681. if (Recorder.getReleasedRangesCount() > 0) {
  682. Region->ReleaseInfo.PushedBlocksAtLastRelease =
  683. Region->Stats.PushedBlocks;
  684. Region->ReleaseInfo.RangesReleased += Recorder.getReleasedRangesCount();
  685. Region->ReleaseInfo.LastReleasedBytes = Recorder.getReleasedBytes();
  686. }
  687. Region->ReleaseInfo.LastReleaseAtNs = getMonotonicTime();
  688. return Recorder.getReleasedBytes();
  689. }
  690. };
  691. } // namespace scudo
  692. #endif // SCUDO_PRIMARY64_H_