primary32.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. //===-- primary32.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_PRIMARY32_H_
  9. #define SCUDO_PRIMARY32_H_
  10. #include "bytemap.h"
  11. #include "common.h"
  12. #include "list.h"
  13. #include "local_cache.h"
  14. #include "options.h"
  15. #include "release.h"
  16. #include "report.h"
  17. #include "stats.h"
  18. #include "string_utils.h"
  19. namespace scudo {
  20. // SizeClassAllocator32 is an allocator for 32 or 64-bit address space.
  21. //
  22. // It maps Regions of 2^RegionSizeLog bytes aligned on a 2^RegionSizeLog bytes
  23. // boundary, and keeps a bytemap of the mappable address space to track the size
  24. // class they are associated with.
  25. //
  26. // Mapped regions are split into equally sized Blocks according to the size
  27. // class they belong to, and the associated pointers are shuffled to prevent any
  28. // predictable address pattern (the predictability increases with the block
  29. // size).
  30. //
  31. // Regions for size class 0 are special and used to hold TransferBatches, which
  32. // allow to transfer arrays of pointers from the global size class freelist to
  33. // the thread specific freelist for said class, and back.
  34. //
  35. // Memory used by this allocator is never unmapped but can be partially
  36. // reclaimed if the platform allows for it.
  37. template <typename Config> class SizeClassAllocator32 {
  38. public:
  39. typedef typename Config::PrimaryCompactPtrT CompactPtrT;
  40. typedef typename Config::SizeClassMap SizeClassMap;
  41. static const uptr GroupSizeLog = Config::PrimaryGroupSizeLog;
  42. // The bytemap can only track UINT8_MAX - 1 classes.
  43. static_assert(SizeClassMap::LargestClassId <= (UINT8_MAX - 1), "");
  44. // Regions should be large enough to hold the largest Block.
  45. static_assert((1UL << Config::PrimaryRegionSizeLog) >= SizeClassMap::MaxSize,
  46. "");
  47. typedef SizeClassAllocator32<Config> ThisT;
  48. typedef SizeClassAllocatorLocalCache<ThisT> CacheT;
  49. typedef typename CacheT::TransferBatch TransferBatch;
  50. typedef typename CacheT::BatchGroup BatchGroup;
  51. static uptr getSizeByClassId(uptr ClassId) {
  52. return (ClassId == SizeClassMap::BatchClassId)
  53. ? sizeof(TransferBatch)
  54. : SizeClassMap::getSizeByClassId(ClassId);
  55. }
  56. static bool canAllocate(uptr Size) { return Size <= SizeClassMap::MaxSize; }
  57. void init(s32 ReleaseToOsInterval) {
  58. if (SCUDO_FUCHSIA)
  59. reportError("SizeClassAllocator32 is not supported on Fuchsia");
  60. if (SCUDO_TRUSTY)
  61. reportError("SizeClassAllocator32 is not supported on Trusty");
  62. DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
  63. PossibleRegions.init();
  64. u32 Seed;
  65. const u64 Time = getMonotonicTime();
  66. if (!getRandom(reinterpret_cast<void *>(&Seed), sizeof(Seed)))
  67. Seed = static_cast<u32>(
  68. Time ^ (reinterpret_cast<uptr>(SizeClassInfoArray) >> 6));
  69. for (uptr I = 0; I < NumClasses; I++) {
  70. SizeClassInfo *Sci = getSizeClassInfo(I);
  71. Sci->RandState = getRandomU32(&Seed);
  72. // Sci->MaxRegionIndex is already initialized to 0.
  73. Sci->MinRegionIndex = NumRegions;
  74. Sci->ReleaseInfo.LastReleaseAtNs = Time;
  75. }
  76. setOption(Option::ReleaseInterval, static_cast<sptr>(ReleaseToOsInterval));
  77. }
  78. void unmapTestOnly() {
  79. while (NumberOfStashedRegions > 0)
  80. unmap(reinterpret_cast<void *>(RegionsStash[--NumberOfStashedRegions]),
  81. RegionSize);
  82. uptr MinRegionIndex = NumRegions, MaxRegionIndex = 0;
  83. for (uptr I = 0; I < NumClasses; I++) {
  84. SizeClassInfo *Sci = getSizeClassInfo(I);
  85. if (Sci->MinRegionIndex < MinRegionIndex)
  86. MinRegionIndex = Sci->MinRegionIndex;
  87. if (Sci->MaxRegionIndex > MaxRegionIndex)
  88. MaxRegionIndex = Sci->MaxRegionIndex;
  89. *Sci = {};
  90. }
  91. for (uptr I = MinRegionIndex; I < MaxRegionIndex; I++)
  92. if (PossibleRegions[I])
  93. unmap(reinterpret_cast<void *>(I * RegionSize), RegionSize);
  94. PossibleRegions.unmapTestOnly();
  95. }
  96. CompactPtrT compactPtr(UNUSED uptr ClassId, uptr Ptr) const {
  97. return static_cast<CompactPtrT>(Ptr);
  98. }
  99. void *decompactPtr(UNUSED uptr ClassId, CompactPtrT CompactPtr) const {
  100. return reinterpret_cast<void *>(static_cast<uptr>(CompactPtr));
  101. }
  102. uptr compactPtrGroup(CompactPtrT CompactPtr) {
  103. return CompactPtr >> GroupSizeLog;
  104. }
  105. TransferBatch *popBatch(CacheT *C, uptr ClassId) {
  106. DCHECK_LT(ClassId, NumClasses);
  107. SizeClassInfo *Sci = getSizeClassInfo(ClassId);
  108. ScopedLock L(Sci->Mutex);
  109. TransferBatch *B = popBatchImpl(C, ClassId);
  110. if (UNLIKELY(!B)) {
  111. if (UNLIKELY(!populateFreeList(C, ClassId, Sci)))
  112. return nullptr;
  113. B = popBatchImpl(C, ClassId);
  114. // if `populateFreeList` succeeded, we are supposed to get free blocks.
  115. DCHECK_NE(B, nullptr);
  116. }
  117. Sci->Stats.PoppedBlocks += B->getCount();
  118. return B;
  119. }
  120. // Push the array of free blocks to the designated batch group.
  121. void pushBlocks(CacheT *C, uptr ClassId, CompactPtrT *Array, u32 Size) {
  122. DCHECK_LT(ClassId, NumClasses);
  123. DCHECK_GT(Size, 0);
  124. SizeClassInfo *Sci = getSizeClassInfo(ClassId);
  125. if (ClassId == SizeClassMap::BatchClassId) {
  126. ScopedLock L(Sci->Mutex);
  127. // Constructing a batch group in the free list will use two blocks in
  128. // BatchClassId. If we are pushing BatchClassId blocks, we will use the
  129. // blocks in the array directly (can't delegate local cache which will
  130. // cause a recursive allocation). However, The number of free blocks may
  131. // be less than two. Therefore, populate the free list before inserting
  132. // the blocks.
  133. if (Size == 1 && !populateFreeList(C, ClassId, Sci))
  134. return;
  135. pushBlocksImpl(C, ClassId, Array, Size);
  136. Sci->Stats.PushedBlocks += Size;
  137. return;
  138. }
  139. // TODO(chiahungduan): Consider not doing grouping if the group size is not
  140. // greater than the block size with a certain scale.
  141. // Sort the blocks so that blocks belonging to the same group can be pushed
  142. // together.
  143. bool SameGroup = true;
  144. for (u32 I = 1; I < Size; ++I) {
  145. if (compactPtrGroup(Array[I - 1]) != compactPtrGroup(Array[I]))
  146. SameGroup = false;
  147. CompactPtrT Cur = Array[I];
  148. u32 J = I;
  149. while (J > 0 && compactPtrGroup(Cur) < compactPtrGroup(Array[J - 1])) {
  150. Array[J] = Array[J - 1];
  151. --J;
  152. }
  153. Array[J] = Cur;
  154. }
  155. ScopedLock L(Sci->Mutex);
  156. pushBlocksImpl(C, ClassId, Array, Size, SameGroup);
  157. Sci->Stats.PushedBlocks += Size;
  158. if (ClassId != SizeClassMap::BatchClassId)
  159. releaseToOSMaybe(Sci, ClassId);
  160. }
  161. void disable() {
  162. // The BatchClassId must be locked last since other classes can use it.
  163. for (sptr I = static_cast<sptr>(NumClasses) - 1; I >= 0; I--) {
  164. if (static_cast<uptr>(I) == SizeClassMap::BatchClassId)
  165. continue;
  166. getSizeClassInfo(static_cast<uptr>(I))->Mutex.lock();
  167. }
  168. getSizeClassInfo(SizeClassMap::BatchClassId)->Mutex.lock();
  169. RegionsStashMutex.lock();
  170. PossibleRegions.disable();
  171. }
  172. void enable() {
  173. PossibleRegions.enable();
  174. RegionsStashMutex.unlock();
  175. getSizeClassInfo(SizeClassMap::BatchClassId)->Mutex.unlock();
  176. for (uptr I = 0; I < NumClasses; I++) {
  177. if (I == SizeClassMap::BatchClassId)
  178. continue;
  179. getSizeClassInfo(I)->Mutex.unlock();
  180. }
  181. }
  182. template <typename F> void iterateOverBlocks(F Callback) {
  183. uptr MinRegionIndex = NumRegions, MaxRegionIndex = 0;
  184. for (uptr I = 0; I < NumClasses; I++) {
  185. SizeClassInfo *Sci = getSizeClassInfo(I);
  186. if (Sci->MinRegionIndex < MinRegionIndex)
  187. MinRegionIndex = Sci->MinRegionIndex;
  188. if (Sci->MaxRegionIndex > MaxRegionIndex)
  189. MaxRegionIndex = Sci->MaxRegionIndex;
  190. }
  191. for (uptr I = MinRegionIndex; I <= MaxRegionIndex; I++)
  192. if (PossibleRegions[I] &&
  193. (PossibleRegions[I] - 1U) != SizeClassMap::BatchClassId) {
  194. const uptr BlockSize = getSizeByClassId(PossibleRegions[I] - 1U);
  195. const uptr From = I * RegionSize;
  196. const uptr To = From + (RegionSize / BlockSize) * BlockSize;
  197. for (uptr Block = From; Block < To; Block += BlockSize)
  198. Callback(Block);
  199. }
  200. }
  201. void getStats(ScopedString *Str) {
  202. // TODO(kostyak): get the RSS per region.
  203. uptr TotalMapped = 0;
  204. uptr PoppedBlocks = 0;
  205. uptr PushedBlocks = 0;
  206. for (uptr I = 0; I < NumClasses; I++) {
  207. SizeClassInfo *Sci = getSizeClassInfo(I);
  208. TotalMapped += Sci->AllocatedUser;
  209. PoppedBlocks += Sci->Stats.PoppedBlocks;
  210. PushedBlocks += Sci->Stats.PushedBlocks;
  211. }
  212. Str->append("Stats: SizeClassAllocator32: %zuM mapped in %zu allocations; "
  213. "remains %zu\n",
  214. TotalMapped >> 20, PoppedBlocks, PoppedBlocks - PushedBlocks);
  215. for (uptr I = 0; I < NumClasses; I++)
  216. getStats(Str, I, 0);
  217. }
  218. bool setOption(Option O, sptr Value) {
  219. if (O == Option::ReleaseInterval) {
  220. const s32 Interval = Max(
  221. Min(static_cast<s32>(Value), Config::PrimaryMaxReleaseToOsIntervalMs),
  222. Config::PrimaryMinReleaseToOsIntervalMs);
  223. atomic_store_relaxed(&ReleaseToOsIntervalMs, Interval);
  224. return true;
  225. }
  226. // Not supported by the Primary, but not an error either.
  227. return true;
  228. }
  229. uptr releaseToOS() {
  230. uptr TotalReleasedBytes = 0;
  231. for (uptr I = 0; I < NumClasses; I++) {
  232. if (I == SizeClassMap::BatchClassId)
  233. continue;
  234. SizeClassInfo *Sci = getSizeClassInfo(I);
  235. ScopedLock L(Sci->Mutex);
  236. TotalReleasedBytes += releaseToOSMaybe(Sci, I, /*Force=*/true);
  237. }
  238. return TotalReleasedBytes;
  239. }
  240. const char *getRegionInfoArrayAddress() const { return nullptr; }
  241. static uptr getRegionInfoArraySize() { return 0; }
  242. static BlockInfo findNearestBlock(UNUSED const char *RegionInfoData,
  243. UNUSED uptr Ptr) {
  244. return {};
  245. }
  246. AtomicOptions Options;
  247. private:
  248. static const uptr NumClasses = SizeClassMap::NumClasses;
  249. static const uptr RegionSize = 1UL << Config::PrimaryRegionSizeLog;
  250. static const uptr NumRegions =
  251. SCUDO_MMAP_RANGE_SIZE >> Config::PrimaryRegionSizeLog;
  252. static const u32 MaxNumBatches = SCUDO_ANDROID ? 4U : 8U;
  253. typedef FlatByteMap<NumRegions> ByteMap;
  254. struct SizeClassStats {
  255. uptr PoppedBlocks;
  256. uptr PushedBlocks;
  257. };
  258. struct ReleaseToOsInfo {
  259. uptr PushedBlocksAtLastRelease;
  260. uptr RangesReleased;
  261. uptr LastReleasedBytes;
  262. u64 LastReleaseAtNs;
  263. };
  264. struct alignas(SCUDO_CACHE_LINE_SIZE) SizeClassInfo {
  265. HybridMutex Mutex;
  266. SinglyLinkedList<BatchGroup> FreeList;
  267. uptr CurrentRegion;
  268. uptr CurrentRegionAllocated;
  269. SizeClassStats Stats;
  270. u32 RandState;
  271. uptr AllocatedUser;
  272. // Lowest & highest region index allocated for this size class, to avoid
  273. // looping through the whole NumRegions.
  274. uptr MinRegionIndex;
  275. uptr MaxRegionIndex;
  276. ReleaseToOsInfo ReleaseInfo;
  277. };
  278. static_assert(sizeof(SizeClassInfo) % SCUDO_CACHE_LINE_SIZE == 0, "");
  279. uptr computeRegionId(uptr Mem) {
  280. const uptr Id = Mem >> Config::PrimaryRegionSizeLog;
  281. CHECK_LT(Id, NumRegions);
  282. return Id;
  283. }
  284. uptr allocateRegionSlow() {
  285. uptr MapSize = 2 * RegionSize;
  286. const uptr MapBase = reinterpret_cast<uptr>(
  287. map(nullptr, MapSize, "scudo:primary", MAP_ALLOWNOMEM));
  288. if (!MapBase)
  289. return 0;
  290. const uptr MapEnd = MapBase + MapSize;
  291. uptr Region = MapBase;
  292. if (isAligned(Region, RegionSize)) {
  293. ScopedLock L(RegionsStashMutex);
  294. if (NumberOfStashedRegions < MaxStashedRegions)
  295. RegionsStash[NumberOfStashedRegions++] = MapBase + RegionSize;
  296. else
  297. MapSize = RegionSize;
  298. } else {
  299. Region = roundUpTo(MapBase, RegionSize);
  300. unmap(reinterpret_cast<void *>(MapBase), Region - MapBase);
  301. MapSize = RegionSize;
  302. }
  303. const uptr End = Region + MapSize;
  304. if (End != MapEnd)
  305. unmap(reinterpret_cast<void *>(End), MapEnd - End);
  306. return Region;
  307. }
  308. uptr allocateRegion(SizeClassInfo *Sci, uptr ClassId) {
  309. DCHECK_LT(ClassId, NumClasses);
  310. uptr Region = 0;
  311. {
  312. ScopedLock L(RegionsStashMutex);
  313. if (NumberOfStashedRegions > 0)
  314. Region = RegionsStash[--NumberOfStashedRegions];
  315. }
  316. if (!Region)
  317. Region = allocateRegionSlow();
  318. if (LIKELY(Region)) {
  319. // Sci->Mutex is held by the caller, updating the Min/Max is safe.
  320. const uptr RegionIndex = computeRegionId(Region);
  321. if (RegionIndex < Sci->MinRegionIndex)
  322. Sci->MinRegionIndex = RegionIndex;
  323. if (RegionIndex > Sci->MaxRegionIndex)
  324. Sci->MaxRegionIndex = RegionIndex;
  325. PossibleRegions.set(RegionIndex, static_cast<u8>(ClassId + 1U));
  326. }
  327. return Region;
  328. }
  329. SizeClassInfo *getSizeClassInfo(uptr ClassId) {
  330. DCHECK_LT(ClassId, NumClasses);
  331. return &SizeClassInfoArray[ClassId];
  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. SizeClassInfo *Sci = getSizeClassInfo(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 = Sci->FreeList.front();
  412. if (ClassId == SizeClassMap::BatchClassId) {
  413. if (Cur == nullptr) {
  414. // Don't need to classify BatchClassId.
  415. Cur = CreateGroup(/*GroupId=*/0);
  416. Sci->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. Sci->FreeList.push_front(Cur);
  432. else
  433. Sci->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. Sci->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. SizeClassInfo *Sci = getSizeClassInfo(ClassId);
  470. if (Sci->FreeList.empty())
  471. return nullptr;
  472. SinglyLinkedList<TransferBatch> &Batches = Sci->FreeList.front()->Batches;
  473. DCHECK(!Batches.empty());
  474. TransferBatch *B = Batches.front();
  475. Batches.pop_front();
  476. DCHECK_NE(B, nullptr);
  477. DCHECK_GT(B->getCount(), 0U);
  478. if (Batches.empty()) {
  479. BatchGroup *BG = Sci->FreeList.front();
  480. Sci->FreeList.pop_front();
  481. // We don't keep BatchGroup with zero blocks to avoid empty-checking while
  482. // allocating. Note that block used by constructing BatchGroup is recorded
  483. // as free blocks in the last element of BatchGroup::Batches. Which means,
  484. // once we pop the last TransferBatch, the block is implicitly
  485. // deallocated.
  486. if (ClassId != SizeClassMap::BatchClassId)
  487. C->deallocate(SizeClassMap::BatchClassId, BG);
  488. }
  489. return B;
  490. }
  491. NOINLINE bool populateFreeList(CacheT *C, uptr ClassId, SizeClassInfo *Sci) {
  492. uptr Region;
  493. uptr Offset;
  494. // If the size-class currently has a region associated to it, use it. The
  495. // newly created blocks will be located after the currently allocated memory
  496. // for that region (up to RegionSize). Otherwise, create a new region, where
  497. // the new blocks will be carved from the beginning.
  498. if (Sci->CurrentRegion) {
  499. Region = Sci->CurrentRegion;
  500. DCHECK_GT(Sci->CurrentRegionAllocated, 0U);
  501. Offset = Sci->CurrentRegionAllocated;
  502. } else {
  503. DCHECK_EQ(Sci->CurrentRegionAllocated, 0U);
  504. Region = allocateRegion(Sci, ClassId);
  505. if (UNLIKELY(!Region))
  506. return false;
  507. C->getStats().add(StatMapped, RegionSize);
  508. Sci->CurrentRegion = Region;
  509. Offset = 0;
  510. }
  511. const uptr Size = getSizeByClassId(ClassId);
  512. const u16 MaxCount = TransferBatch::getMaxCached(Size);
  513. DCHECK_GT(MaxCount, 0U);
  514. // The maximum number of blocks we should carve in the region is dictated
  515. // by the maximum number of batches we want to fill, and the amount of
  516. // memory left in the current region (we use the lowest of the two). This
  517. // will not be 0 as we ensure that a region can at least hold one block (via
  518. // static_assert and at the end of this function).
  519. const u32 NumberOfBlocks =
  520. Min(MaxNumBatches * MaxCount,
  521. static_cast<u32>((RegionSize - Offset) / Size));
  522. DCHECK_GT(NumberOfBlocks, 0U);
  523. constexpr u32 ShuffleArraySize =
  524. MaxNumBatches * TransferBatch::MaxNumCached;
  525. // Fill the transfer batches and put them in the size-class freelist. We
  526. // need to randomize the blocks for security purposes, so we first fill a
  527. // local array that we then shuffle before populating the batches.
  528. CompactPtrT ShuffleArray[ShuffleArraySize];
  529. DCHECK_LE(NumberOfBlocks, ShuffleArraySize);
  530. uptr P = Region + Offset;
  531. for (u32 I = 0; I < NumberOfBlocks; I++, P += Size)
  532. ShuffleArray[I] = reinterpret_cast<CompactPtrT>(P);
  533. // No need to shuffle the batches size class.
  534. if (ClassId != SizeClassMap::BatchClassId)
  535. shuffle(ShuffleArray, NumberOfBlocks, &Sci->RandState);
  536. for (u32 I = 0; I < NumberOfBlocks;) {
  537. // `MaxCount` is u16 so the result will also fit in u16.
  538. const u16 N = static_cast<u16>(Min<u32>(MaxCount, NumberOfBlocks - I));
  539. // Note that the N blocks here may have different group ids. Given that
  540. // it only happens when it crosses the group size boundary. Instead of
  541. // sorting them, treat them as same group here to avoid sorting the
  542. // almost-sorted blocks.
  543. pushBlocksImpl(C, ClassId, &ShuffleArray[I], N, /*SameGroup=*/true);
  544. I += N;
  545. }
  546. const uptr AllocatedUser = Size * NumberOfBlocks;
  547. C->getStats().add(StatFree, AllocatedUser);
  548. DCHECK_LE(Sci->CurrentRegionAllocated + AllocatedUser, RegionSize);
  549. // If there is not enough room in the region currently associated to fit
  550. // more blocks, we deassociate the region by resetting CurrentRegion and
  551. // CurrentRegionAllocated. Otherwise, update the allocated amount.
  552. if (RegionSize - (Sci->CurrentRegionAllocated + AllocatedUser) < Size) {
  553. Sci->CurrentRegion = 0;
  554. Sci->CurrentRegionAllocated = 0;
  555. } else {
  556. Sci->CurrentRegionAllocated += AllocatedUser;
  557. }
  558. Sci->AllocatedUser += AllocatedUser;
  559. return true;
  560. }
  561. void getStats(ScopedString *Str, uptr ClassId, uptr Rss) {
  562. SizeClassInfo *Sci = getSizeClassInfo(ClassId);
  563. if (Sci->AllocatedUser == 0)
  564. return;
  565. const uptr InUse = Sci->Stats.PoppedBlocks - Sci->Stats.PushedBlocks;
  566. const uptr AvailableChunks = Sci->AllocatedUser / getSizeByClassId(ClassId);
  567. Str->append(" %02zu (%6zu): mapped: %6zuK popped: %7zu pushed: %7zu "
  568. "inuse: %6zu avail: %6zu rss: %6zuK releases: %6zu\n",
  569. ClassId, getSizeByClassId(ClassId), Sci->AllocatedUser >> 10,
  570. Sci->Stats.PoppedBlocks, Sci->Stats.PushedBlocks, InUse,
  571. AvailableChunks, Rss >> 10, Sci->ReleaseInfo.RangesReleased);
  572. }
  573. NOINLINE uptr releaseToOSMaybe(SizeClassInfo *Sci, uptr ClassId,
  574. bool Force = false) {
  575. const uptr BlockSize = getSizeByClassId(ClassId);
  576. const uptr PageSize = getPageSizeCached();
  577. DCHECK_GE(Sci->Stats.PoppedBlocks, Sci->Stats.PushedBlocks);
  578. const uptr BytesInFreeList =
  579. Sci->AllocatedUser -
  580. (Sci->Stats.PoppedBlocks - Sci->Stats.PushedBlocks) * BlockSize;
  581. if (BytesInFreeList < PageSize)
  582. return 0; // No chance to release anything.
  583. const uptr BytesPushed =
  584. (Sci->Stats.PushedBlocks - Sci->ReleaseInfo.PushedBlocksAtLastRelease) *
  585. BlockSize;
  586. if (BytesPushed < PageSize)
  587. return 0; // Nothing new to release.
  588. const bool CheckDensity = BlockSize < PageSize / 16U;
  589. // Releasing smaller blocks is expensive, so we want to make sure that a
  590. // significant amount of bytes are free, and that there has been a good
  591. // amount of batches pushed to the freelist before attempting to release.
  592. if (CheckDensity) {
  593. if (!Force && BytesPushed < Sci->AllocatedUser / 16U)
  594. return 0;
  595. }
  596. if (!Force) {
  597. const s32 IntervalMs = atomic_load_relaxed(&ReleaseToOsIntervalMs);
  598. if (IntervalMs < 0)
  599. return 0;
  600. if (Sci->ReleaseInfo.LastReleaseAtNs +
  601. static_cast<u64>(IntervalMs) * 1000000 >
  602. getMonotonicTime()) {
  603. return 0; // Memory was returned recently.
  604. }
  605. }
  606. const uptr First = Sci->MinRegionIndex;
  607. const uptr Last = Sci->MaxRegionIndex;
  608. DCHECK_NE(Last, 0U);
  609. DCHECK_LE(First, Last);
  610. uptr TotalReleasedBytes = 0;
  611. const uptr Base = First * RegionSize;
  612. const uptr NumberOfRegions = Last - First + 1U;
  613. const uptr GroupSize = (1U << GroupSizeLog);
  614. const uptr CurRegionGroupId =
  615. compactPtrGroup(compactPtr(ClassId, Sci->CurrentRegion));
  616. ReleaseRecorder Recorder(Base);
  617. PageReleaseContext Context(BlockSize, RegionSize, NumberOfRegions);
  618. auto DecompactPtr = [](CompactPtrT CompactPtr) {
  619. return reinterpret_cast<uptr>(CompactPtr);
  620. };
  621. for (BatchGroup &BG : Sci->FreeList) {
  622. const uptr PushedBytesDelta =
  623. BG.PushedBlocks - BG.PushedBlocksAtLastCheckpoint;
  624. if (PushedBytesDelta * BlockSize < PageSize)
  625. continue;
  626. uptr AllocatedGroupSize = BG.GroupId == CurRegionGroupId
  627. ? Sci->CurrentRegionAllocated
  628. : GroupSize;
  629. if (AllocatedGroupSize == 0)
  630. continue;
  631. // TransferBatches are pushed in front of BG.Batches. The first one may
  632. // not have all caches used.
  633. const uptr NumBlocks = (BG.Batches.size() - 1) * BG.MaxCachedPerBatch +
  634. BG.Batches.front()->getCount();
  635. const uptr BytesInBG = NumBlocks * BlockSize;
  636. // Given the randomness property, we try to release the pages only if the
  637. // bytes used by free blocks exceed certain proportion of allocated
  638. // spaces.
  639. if (CheckDensity && (BytesInBG * 100U) / AllocatedGroupSize <
  640. (100U - 1U - BlockSize / 16U)) {
  641. continue;
  642. }
  643. BG.PushedBlocksAtLastCheckpoint = BG.PushedBlocks;
  644. // Note that we don't always visit blocks in each BatchGroup so that we
  645. // may miss the chance of releasing certain pages that cross BatchGroups.
  646. Context.markFreeBlocks(BG.Batches, DecompactPtr, Base);
  647. }
  648. if (!Context.hasBlockMarked())
  649. return 0;
  650. auto SkipRegion = [this, First, ClassId](uptr RegionIndex) {
  651. return (PossibleRegions[First + RegionIndex] - 1U) != ClassId;
  652. };
  653. releaseFreeMemoryToOS(Context, Recorder, SkipRegion);
  654. if (Recorder.getReleasedRangesCount() > 0) {
  655. Sci->ReleaseInfo.PushedBlocksAtLastRelease = Sci->Stats.PushedBlocks;
  656. Sci->ReleaseInfo.RangesReleased += Recorder.getReleasedRangesCount();
  657. Sci->ReleaseInfo.LastReleasedBytes = Recorder.getReleasedBytes();
  658. TotalReleasedBytes += Sci->ReleaseInfo.LastReleasedBytes;
  659. }
  660. Sci->ReleaseInfo.LastReleaseAtNs = getMonotonicTime();
  661. return TotalReleasedBytes;
  662. }
  663. SizeClassInfo SizeClassInfoArray[NumClasses] = {};
  664. // Track the regions in use, 0 is unused, otherwise store ClassId + 1.
  665. ByteMap PossibleRegions = {};
  666. atomic_s32 ReleaseToOsIntervalMs = {};
  667. // Unless several threads request regions simultaneously from different size
  668. // classes, the stash rarely contains more than 1 entry.
  669. static constexpr uptr MaxStashedRegions = 4;
  670. HybridMutex RegionsStashMutex;
  671. uptr NumberOfStashedRegions = 0;
  672. uptr RegionsStash[MaxStashedRegions] = {};
  673. };
  674. } // namespace scudo
  675. #endif // SCUDO_PRIMARY32_H_